Search code examples
pythonnfcmifare

MIFARE classic 1k MFRC522-python lib


Im learning to use The MFRC522 module with the Raspberry pi. Im using MFRC522-python lib.

I was able to read and write data from the mifare 1k until I have tried to change Key. I tried changing the sector 7. It was like this:

[0, 0, 0, 0, 0, 0, 255, 7, 128, 105, 255, 255, 255, 255, 255, 255]

and after the changing like this:

[0, 0, 0, 0, 0, 0, 255, 7, 128, 105, 250, 250, 250, 250, 250, 250]

after this modification i could read the sector 0-3 with key A:

255, 255, 255, 255, 255, 255

and the sector 7 with key B:

250,250,250,250,250,250

trying to restore the initial settings I made a mistake by changing sector 7 as:

[255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

now i can read only the UID. I know that the 6-7-8 block contains permissions but i can't find a good doc about this theme. how can i reset initial settings?

I am trying with this code:

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID  and authenticate
while continue_reading:

# Scan for cards    
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

# If a card is found
if status == MIFAREReader.MI_OK:
    print "Card detected"

# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()

# If we have the UID, continue
if status == MIFAREReader.MI_OK:

    # Print UID
    print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])

    # This is the default key for authentication
    key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
    setas = [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x07,0x80,0x69,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
    sect = 7
    # Select the scanned tag
    MIFAREReader.MFRC522_SelectTag(uid)

    # Authenticate
    status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, sect, key, uid)

    # Check if authenticated
    if status == MIFAREReader.MI_OK:
        MIFAREReader.MFRC522_Read(sect)

        MIFAREReader.MFRC522_Write(sect,setas)

        MIFAREReader.MFRC522_Read(sect)
        MIFAREReader.MFRC522_StopCrypto1()
    else:
        print "Authentication error"

Solution

  • good doc about Mifare classic 1k here u can learn how to set access bites.

    I have completely block all access to the entire sector.

    NB:

    To further complicate things, there are also 3 “negated” bits per block that are stored as the opposite value of the “normal” bits. So, if
    C10 is 1, then C10 must be 0. The tag will check to ensure these negated values all check out, and if they don’t then the tag will assume
    the tag’s memory is corrupt or there has been some sort of tampering attempt and completely block all access to the entire sector. It is
    essential that the access bits are properly written to the tag or sectors with invalid sector trailers will be unreadable.
    

    Other good docs can be found in the adafruit documentation like this: learn Adafruit