pokusew / nfc-pcsc

Easy reading and writing NFC tags and cards in Node.js
MIT License
533 stars 132 forks source link

MIFARE Classic: Writing in sectors other than 4, 5 or 6 appears to literally break the NFC tag's memory #119

Closed sniirful closed 3 years ago

sniirful commented 3 years ago

Literally what I said in the title, after writing to those sectors on my MIFARE Classic 1K I can't do anything else with my NFC tags. What's happening and how can I fix that?

EDIT: Authentication straight up fails with error 0x6300

pokusew commented 3 years ago

Hi @nSneerfulBike,

You are probably writing application data into so-called sector trailers.

Take a look into MIFARE Classic 1K data sheet. The Memory organization is described in Section 8.6:

... memory is organized in 16 sectors of 4 blocks. One block contains 16 bytes.

What's important, it's that the last block in each sector is the "sector trailer" (see Section 8.6.3), which contains access conditions for the other 3 data blocks of the sector.

In other words, sector trailers are not for application data. If you write some random data to it, you will change access conditions and authentication keys A and B for the corresponding sector in an unexpected way.

When accessing these blocks with this library, blocks are numbered as follows:

I hope it explains and solves your problem. 🙂 I've closed the issue, but feel free to comment on it or re-open if you have further questions or face any problems.


PS Don't forget to star ⭐️ my library if you find it useful. 😃 Thanks.

sniirful commented 3 years ago

I get how it works, I solved the problem now after making a lot of attempts and destroying so many sectors. I'd really consider this a bug, since if I have let's say a 64-bytes long Buffer and I want to write it, I'd assume that this module takes care of not destroying the sectors, but it doesn't. It just writes everything and doesn't care about the sector trailers.