Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5934

Automation, sensing and robotics • Re: RFID reading and writing

$
0
0
If you can't read any blocks on card A, it sounds like you may have overwritten the sector trailers (blocks: 3, 7, 11 etc.). If you changed the first 6 bytes of these blocks to anything other than 0xFF then you'll have changed the key in which case you'd need to authenticate with the new key. However, if you have overwritten the following 3 bytes (bytes 6,7,8) with anything other than values 0xFF, 0x07 and 0x80 respectively then the card may be irrecoverably unusable. Those 3 bytes control the access to the 3 preceding blocks and the sector trailer itself.

Similarly for card C, it sounds like the sector trailer blocks for all but the first sector may have been changed.

Try this program to test the cards. It's written for the Pico and it uses a slightly different MFRC522.py module but I've adapted it to try to match the code example you provided. Change the pin numbers / card variables and any other code to suit your Pi.

Code:

from machine import Pin, SoftSPIfrom mfrc522 import MFRC522#Pico code so change thissck = Pin(2, Pin.OUT)copi = Pin(3, Pin.OUT)cipo = Pin(4, Pin.OUT)spi = SoftSPI(baudrate=100000, polarity=0, phase=0, sck=sck, mosi=copi, miso=cipo)sda = Pin(5, Pin.OUT)#end of Pico codereader = MFRC522(spi, sda)defaultkey = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]PICC_AUTHENT1A = 0x60DataBlocks1k = tuple([b for b in range(1,64) if b % 4 != 3])def GetSectorTrailerBlockNo(AbsoluteBlockNo):    if type(AbsoluteBlockNo) != int or AbsoluteBlockNo<0 or AbsoluteBlockNo>63:        raise Exception("The AbsoluteBlockNo must be an integer between 0 and 63")    return (((AbsoluteBlockNo // 4) * 4) +3)        while True:    try:        (status, tag_type) = reader.request(reader.CARD_REQIDL)        if status == reader.OK:            print("Card detected")            (status, cardID) = reader.anticoll()            if status == reader.OK:                print("uid:    0x%02x%02x%02x%02x" % (cardID[0], cardID[1], cardID[2], cardID[3]))                if reader.select_tag(cardID) == reader.OK:                    last_sector_trailer_authd = -1                    for block in range(0,64):                        sector_trailer_block = GetSectorTrailerBlockNo(block)                        if last_sector_trailer_authd != sector_trailer_block:                            if reader.auth(PICC_AUTHENT1A, sector_trailer_block, defaultkey, cardID) == reader.OK:                                last_sector_trailer_authd = sector_trailer_block                            else:                                print(f"Authentication error for block {block}")                                break                        data = reader.read(block)                        print(f"Block: {block:02} ", end='')                        for b in data:                            print(f"{b:02X}", end='')                        print("")                    reader.stop_crypto1()                else:                    print("Could not select card")    except KeyboardInterrupt:        break

If all the keys are set to the default and the access bits haven't been changed then you should get something like:

Code:

Card detecteduid:    0x03cb7b9aBlock: 00 03CB7B9A290804006263646566676869Block: 01 00000000000000000000000000000000Block: 02 00000000000000000000000000000000Block: 03 000000000000FF078069FFFFFFFFFFFFBlock: 04 00000000000000000000000000000000Block: 05 00000000000000000000000000000000Block: 06 00000000000000000000000000000000Block: 07 000000000000FF078069FFFFFFFFFFFF...Block: 60 00000000000000000000000000000000Block: 61 00000000000000000000000000000000Block: 62 00000000000000000000000000000000Block: 63 000000000000FF078069FFFFFFFFFFFF
I would expect the above output for card B. Card A will fail after displaying the UID and card C will fail when reading block 4.

For your other questions: you can't write to the first block in the first sector (absolute block 0) for Mifare 1K cards - I'm assuming you are using 1K cards as you mention 64 sectors. The first block contains the card UID and other manufacturer data. In the above example you can see the UID matches the first 4 bytes of block 0. You can write to the second and third blocks (absolute block 1 and 2) in the first sector.

Statistics: Posted by jm3220 — Sun Mar 03, 2024 2:29 am



Viewing all articles
Browse latest Browse all 5934

Latest Images

Trending Articles



Latest Images