MOSの覚書

趣味人の公開メモ帳

NFC カードのパスワードは破れるか

手元に 6Byte のパスワードが掛けられた NFC Type-A(Mifare Classic 1K)のカードがあります.これのパスワードを破ることはできるでしょうか?

結論を言うと無理です.解散!!!

...ではおもしろくないので,如何にしてパスワード突破を試み,無理だと悟ったかを紹介しようと思います

本題

ちなみに手元にあるカードっていうのは学生証です.中のデータ読んでみたくて NFC カードリーダモジュールを買ってみました.Amazon にて 2 個セット 700 円くらい.安いですね

https://www.amazon.co.jp/gp/product/B088FLF8MC/

こちらを購入し,Arduino と接続しました

接続方式は SPI です.本体の印字が一部おかしくてちょっと迷った

そして以下のコードを実行します.サンプルコードを丸々コピって改造し,多重 for ループで解読を試みました
パスワードはどうも 6Byte あるそうです

/*
 * ----------------------------------------------------------------------------
 * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
 * for further details and other examples.
 * 
 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
 * 
 * Released into the public domain.
 * ----------------------------------------------------------------------------
 * Example sketch/program which will try the most used default keys listed in 
 * https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys to dump the
 * block 0 of a MIFARE RFID card using a RFID-RC522 reader.
 * 
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
 *
 */

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

// Number of known default keys (hard-coded)
// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
#define NR_KNOWN_KEYS   256

/*
 * Initialize.
 */
void setup() {
    Serial.begin(115200);         // Initialize serial communications with the PC
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
    Serial.println(F("Try the all keys to print block 0 of a MIFARE PICC."));
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

/*
 * Try using the PICC (the tag/card) with the given key to access block 0.
 * On success, it will show the key details, and dump the block data on Serial.
 *
 * @return true when the given key worked, false otherwise.
 */
bool try_key(MFRC522::MIFARE_Key *key)
{
    bool result = false;
    byte buffer[18];
    byte block = 0;
    MFRC522::StatusCode status;

    // Serial.println(F("Authenticating using key A..."));
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        // Serial.print(F("PCD_Authenticate() failed: "));
        // Serial.println(mfrc522.GetStatusCodeName(status));
        return false;
    }

    // Read block
    byte byteCount = sizeof(buffer);
    status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
    if (status != MFRC522::STATUS_OK) {
        // Serial.print(F("MIFARE_Read() failed: "));
        // Serial.println(mfrc522.GetStatusCodeName(status));
    }
    else {
        // Successful read
        result = true;
        Serial.print(F("Success with key:"));
        dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
        Serial.println();
        // Dump block data
        Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
        dump_byte_array(buffer, 16);
        Serial.println();
    }
    Serial.println();

    mfrc522.PICC_HaltA();       // Halt PICC
    mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
    return result;
}

/*
 * Main loop.
 */
void loop() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;

  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));
  
  // Try the break key
  MFRC522::MIFARE_Key key;
  for(int16_t f = 0; f < NR_KNOWN_KEYS; f++) {
    for(int16_t e = 0; e < NR_KNOWN_KEYS; e++) {
      for(int16_t d = 0; d < NR_KNOWN_KEYS; d++) {
        for(int16_t c = 0; c < NR_KNOWN_KEYS; c++) {
          for(int16_t b = 0; b < NR_KNOWN_KEYS; b++) {
            char txt[64];
            sprintf(txt, "Test Key : 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", f, e, d, c, b, 0x00);
            Serial.print(txt);

            for(int16_t a = 0; a < NR_KNOWN_KEYS; a++) {
              byte TestKeys[6] = {f, e, d, c, b, a};

              for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
                key.keyByte[i] = TestKeys[i];                
              }

              // try key
              if (try_key(&key)) {
                // Found and reported on the key and block,
                // no need to try other keys for this PICC

                sprintf(txt, "Success Key : 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", f, e, d, c, b, a);
                Serial.print(txt);

                break;
              }

              // http://arduino.stackexchange.com/a/14316
              if ( ! mfrc522.PICC_IsNewCardPresent()) {
                break;
              }

              if ( ! mfrc522.PICC_ReadCardSerial()) {
                break;
              }
            }
          }
        }
      }
    }
  }
}

実行している様子がこちら.ガチの自分の学生証なので正面が映らないように横から撮ってます

シリアルモニタの表示はこちら
UID は伏せてます

14:58:10.163 -> Try the all keys to print block 0 of a MIFARE PICC.
14:58:15.491 -> Card UID: ** ** ** **
14:58:15.491 -> PICC type: MIFARE 1KB
14:58:15.491 -> Test Key : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
14:58:23.428 -> Test Key : 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
14:58:31.337 -> Test Key : 0x00, 0x00, 0x00, 0x00, 0x02, 0x00

こんな具合に,256 ループごとに進捗を表示する感じで実行させていきつつ,どれくらい時間が掛かるかを計算してみました
シリアルモニタのタイムコードを見ると,大体 8 秒おきにきてるので 256 探索で 8 秒掛かっていそうですね

突然ですが,ここで皆さんに文章題を解いてもらいます.何年ぶりでしょうね?

問)探索すべきパターンは 48bit 分あります.このうち 256 パターンを探索するのに 8 秒かかりました.全探索を終えるにはどれくらいの時間が掛かるでしょうか?

一緒に解いていきましょう

まずは探索すべきパターン数について.これは簡単で 2 の 48 乗ですね
このうち 2 の 8 乗分の探索に 2 の 3 乗秒掛かったので,1 秒で 2 の 5 乗パターン探索できたことになります
ということは 2 の 48 乗割ることの 2 の 5 乗秒掛かるわけなので 2 の 43 乗秒掛かるということがわかりました.途方もない数字な予感がしますが続けましょう

2 の 43 乗が実際にどんな値なのかを知るために関数電卓を叩きました.すると 8.796093022 x1012 だそうです.指数表記になっちゃいましたね
仕方がないので Python で計算しました.すると以下の通り

C:\Users\user>python3
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**43
8796093022208

8796093022208 秒だそうです.これを年日時分秒に変換したいのですが,いい方法が見当たらなかったので泥臭く計算します

まず 8796093022208 から秒を取り出します.単純に 60 で割ったあまりですね
これまた Python を使いましょう

>>> 2**43%60
8

秒は 8 と求まりました

こんな具合に残りも計算していきますと,こんな感じ

>>> 2**43/60%60
50.133331298828125
>>> 2**43/60/60%24
4.835555553436279
>>> 2**43/60/60/24%365.2429
287.2270814708236
>>> 2**43/60/60/24/365.2429
278736.7864001777

よって 278736 年 287 日 4 時間 50 分 8 秒と求まりました.無理!!!!

という訳です.たかだか 6Byte とあなどるなかれ.一回のアクセスに結構時間かかるのもあって全探索は到底やってられないですね.セキュリティは十分そう

余談

学生証のパスワード突破は無理と分かりました.ではせっかく買ったこれ,何に使いましょうか?

今のところ考えているのは,付属のカードを利用したセキュリティキーとか,専用カードでないと解錠できない金庫とか,面白そうだなって思ってます
もしやるとしたらまた何かしらの形で発信すると思います

それでは今日はこの辺で