- \r
- PrintAndLog("<phase1: %s", sprint_hex(data, datalen));\r
- \r
- if (datalen < 1) {\r
- PrintAndLog("ERROR: card response length: %d", datalen);\r
- DropField();\r
- return 3;\r
- }\r
- \r
- if (data[0] != 0x90) {\r
- PrintAndLog("ERROR: card response error: %02x", data[2]);\r
- DropField();\r
- return 3;\r
- }\r
-\r
- if (datalen != 19) { // code 1b + 16b + crc 2b\r
- PrintAndLog("ERROR: card response must be 19 bytes long instead of: %d", datalen);\r
- DropField();\r
- return 3;\r
- }\r
- \r
- aes_decode(NULL, key, &data[1], Rnd2, 16);\r
- Rnd2[16] = Rnd2[0];\r
- PrintAndLog("Rnd2: %s", sprint_hex(Rnd2, 16));\r
-\r
- uint8_t cmd2[33] = {0};\r
- cmd2[0] = 0x72;\r
-\r
- uint8_t raw[32] = {0};\r
- memmove(raw, Rnd1, 16);\r
- memmove(&raw[16], &Rnd2[1], 16);\r
-\r
- aes_encode(NULL, key, raw, &cmd2[1], 32);\r
- PrintAndLog(">phase2: %s", sprint_hex(cmd2, 33));\r
- \r
- res = ExchangeRAW14a(cmd2, sizeof(cmd2), false, false, data, sizeof(data), &datalen);\r
+\r
+ if (verbose) {\r
+ for (int i = 0; i < 4; i ++)\r
+ PrintAndLogEx(NORMAL, "[%d] %s", i, sprint_hex(§or0[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