]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
Ndef and MAD (#801)
[proxmark3-svn] / client / cmdhfmf.c
index fa952cb9be55bba7e03323476e5b17e931253325..1c006fbf6924d15c46009c5d048b447474a067f7 100644 (file)
 #include "util_posix.h"\r
 #include "usb_cmd.h"\r
 #include "ui.h"\r
-#include "mifarehost.h"\r
+#include "mifare/mifarehost.h"\r
 #include "mifare.h"\r
-#include "mfkey.h"\r
+#include "mifare/mfkey.h"\r
 #include "hardnested/hardnested_bf_core.h"\r
 #include "cliparser/cliparser.h"\r
 #include "cmdhf14a.h"\r
-#include "mifare4.h"\r
+#include "mifare/mifare4.h"\r
+#include "mifare/mad.h"\r
+#include "mifare/ndef.h"\r
+#include "emv/dump.h"\r
 \r
 #define NESTED_SECTOR_RETRY     10                     // how often we try mfested() until we give up\r
 \r
@@ -215,6 +218,15 @@ int CmdHF14AMfRdSc(const char *Cmd)
                                PrintAndLog("data   : %s", sprint_hex(data + i * 16, 16));\r
                        }\r
                        PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));\r
+                       \r
+                       PrintAndLogEx(NORMAL, "Trailer decoded:");\r
+                        int bln = mfFirstBlockOfSector(sectorNo);\r
+                        int blinc = (mfNumBlocksPerSector(sectorNo) > 4) ? 5 : 1;\r
+                        for (i = 0; i < 4; i++) {\r
+                                PrintAndLogEx(NORMAL, "Access block %d%s: %s", bln, ((blinc > 1) && (i < 3) ? "+" : "") , mfGetAccessConditionsDesc(i, &(data + (sectorNo<32?3:15) * 16)[6]));\r
+                                bln += blinc;\r
+                        }\r
+                        PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&(data + (sectorNo<32?3:15) * 16)[9], 1));\r
                }\r
        } else {\r
                PrintAndLog("Command execute timeout");\r
@@ -267,8 +279,7 @@ int CmdHF14AMfDump(const char *Cmd)
 {\r
        uint8_t sectorNo, blockNo;\r
 \r
-       uint8_t keyA[40][6];\r
-       uint8_t keyB[40][6];\r
+       uint8_t keys[2][40][6];\r
        uint8_t rights[40][4];\r
        uint8_t carddata[256][16];\r
        uint8_t numSectors = 16;\r
@@ -281,38 +292,39 @@ int CmdHF14AMfDump(const char *Cmd)
        char cmdp = param_getchar(Cmd, 0);\r
        numSectors = ParamCardSizeSectors(cmdp);\r
 \r
-       if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r
-               PrintAndLog("Usage:   hf mf dump [card memory]");\r
+       if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') {\r
+               PrintAndLog("Usage:   hf mf dump [card memory] [k|m]");\r
                PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
+               PrintAndLog("  k: Always try using both Key A and Key B for each sector, even if access bits would prohibit it");\r
+               PrintAndLog("  m: When missing access bits or keys, replace that block with NULL");\r
                PrintAndLog("");\r
                PrintAndLog("Samples: hf mf dump");\r
                PrintAndLog("         hf mf dump 4");\r
+               PrintAndLog("         hf mf dump 4 m");\r
                return 0;\r
        }\r
 \r
+       char opts = param_getchar(Cmd, 1);\r
+       bool useBothKeysAlways = false;\r
+       if (opts == 'k' || opts == 'K') useBothKeysAlways = true;\r
+       bool nullMissingKeys = false;\r
+       if (opts == 'm' || opts == 'M') nullMissingKeys = true;\r
+\r
        if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r
                PrintAndLog("Could not find file dumpkeys.bin");\r
                return 1;\r
        }\r
 \r
-       // Read keys A from file\r
-       for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
-               size_t bytes_read = fread(keyA[sectorNo], 1, 6, fin);\r
-               if (bytes_read != 6) {\r
-                       PrintAndLog("File reading error.");\r
-                       fclose(fin);\r
-                       return 2;\r
-               }\r
-       }\r
-\r
-       // Read keys B from file\r
-       for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
-               size_t bytes_read = fread(keyB[sectorNo], 1, 6, fin);\r
-               if (bytes_read != 6) {\r
-                       PrintAndLog("File reading error.");\r
-                       fclose(fin);\r
-                       return 2;\r
-               }\r
+       // Read keys from file\r
+       for (int group=0; group<=1; group++) {\r
+               for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
+                       size_t bytes_read = fread(keys[group][sectorNo], 1, 6, fin);\r
+                       if (bytes_read != 6) {\r
+                               PrintAndLog("File reading error.");\r
+                               fclose(fin);\r
+                               return 2;\r
+                       }\r
+               }               \r
        }\r
 \r
        fclose(fin);\r
@@ -324,7 +336,8 @@ int CmdHF14AMfDump(const char *Cmd)
        for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
                for (tries = 0; tries < 3; tries++) {\r
                        UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};\r
-                       memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
+                       // At least the Access Conditions can always be read with key A.\r
+                       memcpy(c.d.asBytes, keys[0][sectorNo], 6);\r
                        SendCommand(&c);\r
 \r
                        if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
@@ -360,23 +373,41 @@ int CmdHF14AMfDump(const char *Cmd)
                        for (tries = 0; tries < 3; tries++) {\r
                                if (blockNo == NumBlocksPerSector(sectorNo) - 1) {              // sector trailer. At least the Access Conditions can always be read with key A.\r
                                        UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r
-                                       memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
+                                       memcpy(c.d.asBytes, keys[0][sectorNo], 6);\r
                                        SendCommand(&c);\r
                                        received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
+                               } else if (useBothKeysAlways) {\r
+                                       // Always try both keys, even if access conditions wouldn't work.\r
+                                       for (int k=0; k<=1; k++) {\r
+                                               UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r
+                                               memcpy(c.d.asBytes, keys[k][sectorNo], 6);\r
+                                               SendCommand(&c);\r
+                                               received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
+\r
+                                               // Don't try the other one on success.\r
+                                               if (resp.arg[0] & 0xff) break;\r
+                                       }\r
                                } else {                                                                                                // data block. Check if it can be read with key A or key B\r
                                        uint8_t data_area = sectorNo<32?blockNo:blockNo/5;\r
                                        if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) {   // only key B would work\r
                                                UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r
-                                               memcpy(c.d.asBytes, keyB[sectorNo], 6);\r
+                                               memcpy(c.d.asBytes, keys[1][sectorNo], 6);\r
                                                SendCommand(&c);\r
                                                received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
                                        } else if (rights[sectorNo][data_area] == 0x07) {                                                                               // no key would work\r
-                                               isOK = false;\r
                                                PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);\r
-                                               tries = 2;\r
+                                               if (nullMissingKeys) {\r
+                                                       memset(resp.d.asBytes, 0, 16);\r
+                                                       resp.arg[0] = 1;\r
+                                                       PrintAndLog("  ... filling the block with NULL");\r
+                                                       received = true;\r
+                                               } else {\r
+                                                       isOK = false;\r
+                                                       tries = 2;\r
+                                               }\r
                                        } else {                                                                                                                                                                // key A would work\r
                                                UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r
-                                               memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
+                                               memcpy(c.d.asBytes, keys[0][sectorNo], 6);\r
                                                SendCommand(&c);\r
                                                received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
                                        }\r
@@ -391,18 +422,8 @@ int CmdHF14AMfDump(const char *Cmd)
                                isOK  = resp.arg[0] & 0xff;\r
                                uint8_t *data  = resp.d.asBytes;\r
                                if (blockNo == NumBlocksPerSector(sectorNo) - 1) {              // sector trailer. Fill in the keys.\r
-                                       data[0]  = (keyA[sectorNo][0]);\r
-                                       data[1]  = (keyA[sectorNo][1]);\r
-                                       data[2]  = (keyA[sectorNo][2]);\r
-                                       data[3]  = (keyA[sectorNo][3]);\r
-                                       data[4]  = (keyA[sectorNo][4]);\r
-                                       data[5]  = (keyA[sectorNo][5]);\r
-                                       data[10] = (keyB[sectorNo][0]);\r
-                                       data[11] = (keyB[sectorNo][1]);\r
-                                       data[12] = (keyB[sectorNo][2]);\r
-                                       data[13] = (keyB[sectorNo][3]);\r
-                                       data[14] = (keyB[sectorNo][4]);\r
-                                       data[15] = (keyB[sectorNo][5]);\r
+                                       memcpy(data, keys[0][sectorNo], 6);\r
+                                       memcpy(data + 10, keys[1][sectorNo], 6);\r
                                }\r
                                if (isOK) {\r
                                        memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);\r
@@ -2350,6 +2371,19 @@ int CmdHF14AMfCGetSc(const char *Cmd) {
                }\r
 \r
                PrintAndLog("block %3d data:%s", baseblock + i, sprint_hex(memBlock, 16));\r
+               \r
+               if (mfIsSectorTrailer(baseblock + i)) {\r
+                PrintAndLogEx(NORMAL, "Trailer decoded:");\r
+                PrintAndLogEx(NORMAL, "Key A: %s", sprint_hex_inrow(memBlock, 6));\r
+                PrintAndLogEx(NORMAL, "Key B: %s", sprint_hex_inrow(&memBlock[10], 6));\r
+                int bln = baseblock;\r
+                int blinc = (mfNumBlocksPerSector(sectorNo) > 4) ? 5 : 1;\r
+                for (int i = 0; i < 4; i++) {\r
+                        PrintAndLogEx(NORMAL, "Access block %d%s: %s", bln, ((blinc > 1) && (i < 3) ? "+" : "") , mfGetAccessConditionsDesc(i, &memBlock[6]));\r
+                        bln += blinc;\r
+                }\r
+                PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&memBlock[9], 1));\r
+        }\r
        }\r
        return 0;\r
 }\r
@@ -2681,6 +2715,201 @@ int CmdHF14AMfAuth4(const char *cmd) {
        return MifareAuth4(NULL, keyn, key, true, false, true);\r
 }\r
 \r
+// https://www.nxp.com/docs/en/application-note/AN10787.pdf\r
+int CmdHF14AMfMAD(const char *cmd) {\r
+\r
+    CLIParserInit("hf mf mad",\r
+                  "Checks and prints Mifare Application Directory (MAD)",\r
+                  "Usage:\n\thf mf mad -> shows MAD if exists\n"\r
+                  "\thf mf mad -a 03e1 -k ffffffffffff -b -> shows NDEF data if exists. read card with custom key and key B\n");\r
+\r
+    void *argtable[] = {\r
+        arg_param_begin,\r
+        arg_lit0("vV",  "verbose",  "show technical data"),\r
+        arg_str0("aA",  "aid",      "print all sectors with aid", NULL),\r
+        arg_str0("kK",  "key",      "key for printing sectors", NULL),\r
+        arg_lit0("bB",  "keyb",     "use key B for access printing sectors (by default: key A)"),\r
+        arg_param_end\r
+    };\r
+    CLIExecWithReturn(cmd, argtable, true);\r
+    bool verbose = arg_get_lit(1);\r
+    uint8_t aid[2] = {0};\r
+    int aidlen;\r
+    CLIGetHexWithReturn(2, aid, &aidlen);\r
+    uint8_t key[6] = {0};\r
+    int keylen;\r
+    CLIGetHexWithReturn(3, key, &keylen);\r
+    bool keyB = arg_get_lit(4);\r
+\r
+    CLIParserFree();\r
+\r
+    if (aidlen != 2 && keylen > 0) {\r
+        PrintAndLogEx(WARNING, "do not need a key without aid.");\r
+    }\r
+\r
+    uint8_t sector0[16 * 4] = {0};\r
+    uint8_t sector10[16 * 4] = {0};\r
+    if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) {\r
+        PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys.");\r
+        return 2;\r
+    }\r
+\r
+    if (verbose) {\r
+        for (int i = 0; i < 4; i ++)\r
+            PrintAndLogEx(NORMAL, "[%d] %s", i, sprint_hex(&sector0[i * 16], 16));\r
+    }\r
+\r
+    bool haveMAD2 = false;\r
+    MAD1DecodeAndPrint(sector0, verbose, &haveMAD2);\r
+\r
+    if (haveMAD2) {\r
+        if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) {\r
+            PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys.");\r
+            return 2;\r
+        }\r
+\r
+        MAD2DecodeAndPrint(sector10, verbose);\r
+    }\r
+\r
+    if (aidlen == 2) {\r
+        uint16_t aaid = (aid[0] << 8) + aid[1];\r
+        PrintAndLogEx(NORMAL, "\n-------------- AID 0x%04x ---------------", aaid);\r
+\r
+        uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};\r
+        size_t madlen = 0;\r
+        if (MADDecode(sector0, sector10, mad, &madlen)) {\r
+            PrintAndLogEx(ERR, "can't decode mad.");\r
+            return 10;\r
+        }\r
+\r
+        uint8_t akey[6] = {0};\r
+        memcpy(akey, g_mifare_ndef_key, 6);\r
+        if (keylen == 6) {\r
+            memcpy(akey, key, 6);\r
+        }\r
+\r
+        for (int i = 0; i < madlen; i++) {\r
+            if (aaid == mad[i]) {\r
+                uint8_t vsector[16 * 4] = {0};\r
+                if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, akey, vsector)) {\r
+                    PrintAndLogEx(NORMAL, "");\r
+                    PrintAndLogEx(ERR, "read sector %d error.", i + 1);\r
+                    return 2;\r
+                }\r
+\r
+                for (int j = 0; j < (verbose ? 4 : 3); j ++)\r
+                    PrintAndLogEx(NORMAL, " [%03d] %s", (i + 1) * 4 + j, sprint_hex(&vsector[j * 16], 16));\r
+            }\r
+        }\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+int CmdHFMFNDEF(const char *cmd) {\r
+\r
+    CLIParserInit("hf mf ndef",\r
+                  "Prints NFC Data Exchange Format (NDEF)",\r
+                  "Usage:\n\thf mf ndef -> shows NDEF data\n"\r
+                  "\thf mf ndef -a 03e1 -k ffffffffffff -b -> shows NDEF data with custom AID, key and with key B\n");\r
+\r
+    void *argtable[] = {\r
+        arg_param_begin,\r
+        arg_litn("vV",  "verbose",  0, 2, "show technical data"),\r
+        arg_str0("aA",  "aid",      "replace default aid for NDEF", NULL),\r
+        arg_str0("kK",  "key",      "replace default key for NDEF", NULL),\r
+        arg_lit0("bB",  "keyb",     "use key B for access sectors (by default: key A)"),\r
+        arg_param_end\r
+    };\r
+    CLIExecWithReturn(cmd, argtable, true);\r
+\r
+    bool verbose = arg_get_lit(1);\r
+    bool verbose2 = arg_get_lit(1) > 1;\r
+    uint8_t aid[2] = {0};\r
+    int aidlen;\r
+    CLIGetHexWithReturn(2, aid, &aidlen);\r
+    uint8_t key[6] = {0};\r
+    int keylen;\r
+    CLIGetHexWithReturn(3, key, &keylen);\r
+    bool keyB = arg_get_lit(4);\r
+\r
+    CLIParserFree();\r
+\r
+    uint16_t ndefAID = 0x03e1;\r
+    if (aidlen == 2)\r
+        ndefAID = (aid[0] << 8) + aid[1];\r
+\r
+    uint8_t ndefkey[6] = {0};\r
+    memcpy(ndefkey, g_mifare_ndef_key, 6);\r
+    if (keylen == 6) {\r
+        memcpy(ndefkey, key, 6);\r
+    }\r
+\r
+    uint8_t sector0[16 * 4] = {0};\r
+    uint8_t sector10[16 * 4] = {0};\r
+    uint8_t data[4096] = {0};\r
+    int datalen = 0;\r
+\r
+    PrintAndLogEx(NORMAL, "");\r
+\r
+    if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) {\r
+        PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys.");\r
+        return 2;\r
+    }\r
+\r
+    bool haveMAD2 = false;\r
+    int res = MADCheck(sector0, NULL, verbose, &haveMAD2);\r
+    if (res) {\r
+        PrintAndLogEx(ERR, "MAD error %d.", res);\r
+        return res;\r
+    }\r
+\r
+    if (haveMAD2) {\r
+        if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) {\r
+            PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys.");\r
+            return 2;\r
+        }\r
+    }\r
+\r
+    uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};\r
+    size_t madlen = 0;\r
+    if (MADDecode(sector0, (haveMAD2 ? sector10 : NULL), mad, &madlen)) {\r
+        PrintAndLogEx(ERR, "can't decode mad.");\r
+        return 10;\r
+    }\r
+\r
+    printf("data reading:");\r
+    for (int i = 0; i < madlen; i++) {\r
+        if (ndefAID == mad[i]) {\r
+            uint8_t vsector[16 * 4] = {0};\r
+            if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, ndefkey, vsector)) {\r
+                PrintAndLogEx(ERR, "read sector %d error.", i + 1);\r
+                return 2;\r
+            }\r
+\r
+            memcpy(&data[datalen], vsector, 16 * 3);\r
+            datalen += 16 * 3;\r
+\r
+            printf(".");\r
+        }\r
+    }\r
+    printf(" OK\n");\r
+\r
+    if (!datalen) {\r
+        PrintAndLogEx(ERR, "no NDEF data.");\r
+        return 11;\r
+    }\r
+\r
+    if (verbose2) {\r
+        PrintAndLogEx(NORMAL, "NDEF data:");\r
+        dump_buffer(data, datalen, stdout, 1);\r
+    }\r
+\r
+    NDEFDecodeAndPrint(data, datalen, verbose);\r
+\r
+    return 0;\r
+}\r
+\r
 static command_t CommandTable[] =\r
 {\r
   {"help",             CmdHelp,                 1, "This help"},\r
@@ -2712,6 +2941,8 @@ static command_t CommandTable[] =
   {"cload",            CmdHF14AMfCLoad,         0, "Load dump into magic Chinese card"},\r
   {"csave",            CmdHF14AMfCSave,         0, "Save dump from magic Chinese card into file or emulator"},\r
   {"decrypt",          CmdDecryptTraceCmds,     1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
+  {"mad",              CmdHF14AMfMAD,           0, "Checks and prints MAD"},\r
+  {"ndef",             CmdHFMFNDEF,             0, "Prints NDEF records from card"},\r
   {NULL,               NULL,                    0, NULL}\r
 };\r
 \r
Impressum, Datenschutz