LoginSignup
1
0

More than 1 year has passed since last update.

RaspberyPi + libnfc + PN532 NFC RFID module + mfd ファイルを使って Mifare カードを読む

Posted at

(Felica/Mifare/NFC チャレンジシリーズ) その他の記事はこちら 「Felica/Mifare/NFC でいろいろ実験」
https://qiita.com/nanbuwks/items/1f416d6e45a87250ee0a


Raspberry Pi で libnfc + PN532 NFC RFID module
https://qiita.com/nanbuwks/items/0c73add503b354774035

では Mifare カードを認識するところまで行いました。

今回は、 libnfc を使って Mifare カードの中身を読み込んでみます。

util プログラム

libnfc をインストールすると、libnfc-bin として以下のプログラムが使用できるようになります。

  • nfc-barcode
  • nfc-jewel
  • nfc-mfclassic
  • nfc-read-forum-tag3
  • nfc-scan-device
  • nfc-emulate-forum-tag4
  • nfc-list
  • nfc-mfultralight
  • nfc-relay-picc

これらの特徴として、読み書きするデータはカードまるごと、更にデータはファイルでやり取りをするようです。

mifare を読み込むのは nfc-mfclassic が使えそうです。

$ nfc-mfclassic
Usage: nfc-mfclassic f|r|R|w|W a|b u|U<01ab23cd> <dump.mfd> [<keys.mfd> [f]]
  f|r|R|w|W     - Perform format (f) or read from (r) or unlocked read from (R) or write to (w) or unlocked write to (W) card
                  *** format will reset all keys to FFFFFFFFFFFF and all data to 00 and all ACLs to default
                  *** unlocked read does not require authentication and will reveal A and B keys
                  *** note that unlocked write will attempt to overwrite block 0 including UID
                  *** unlocking only works with special Mifare 1K cards (Chinese clones)
  a|A|b|B       - Use A or B keys for action; Halt on errors (a|b) or tolerate errors (A|B)
  u|U           - Use any (u) uid or supply a uid specifically as U01ab23cd.
  <dump.mfd>    - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)
  <keys.mfd>    - MiFare Dump (MFD) that contain the keys (optional)
  f             - Force using the keyfile even if UID does not match (optional)
Examples: 

  Read card to file, using key A:

    nfc-mfclassic r a u mycard.mfd

  Write file to blank card, using key A:

    nfc-mfclassic w a u mycard.mfd

  Write new data and/or keys to previously written card, using key A:

    nfc-mfclassic w a u newdata.mfd mycard.mfd

  Format/wipe card (note two passes required to ensure writes for all ACL cases):

    nfc-mfclassic f A u dummy.mfd keyfile.mfd f
    nfc-mfclassic f B u dummy.mfd keyfile.mfd f

  Read card to file, using key A and uid 0x01 0xab 0x23 0xcd:

    nfc-mfclassic r a U01ab23cd mycard.mfd

これに基づいて、読み込みを行ってみます。
読み出しや鍵指定に使うファイルは、mfd 形式のようです。


$ xxd -r -p > key.mfd

以下のように標準入力から入力し、Ctrl+Dを押して鍵を作ります。


000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFF00000000000000000000

中身は以下のように確認できます。

$ xxd key.mfd
00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000030: ffff ffff ffff 0000 0000 0000 0000 0000  ................

これは keyA に FFFFFFFFFFFF を指定しています。

これを使って読んでみます。


$ nfc-mfclassic r key.mfd u mycard.mfd
NFC reader: Itead_PN532 opened
Found MIFARE Classic card:
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04  
       UID (NFCID1): c5  b2  0f  ad  
      SAK (SEL_RES): 08  
RATS support: no
Guessing size: seems to be a 1024-byte card
Reading out 64 blocks |................................................................|
Done, 64 of 64 blocks read.
Writing data to file: mycard.mfd ...Done.

読めた中身を確認します。


$  xxd mycard.mfd
00000000: c5b2 0fad d508 0400 6263 6465 6667 6869  ........bcdefghi
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 ff07 8069 ffff ffff ffff  .........i......
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 ff07 8069 ffff ffff ffff  .........i......
.
.
.

なお、key.mfd ファイルを作りましたが


$ nfc-mfclassic r dummy.mfd u mycard.mfd

として、存在しない鍵ファイルを使っても読めました。
デフォルト鍵を使う場合はこれでもいい??

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0