]> cvs.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
ADD: @pwpiwi 's latest changes to 'hf mf hardnested'
[proxmark3-svn] / client / cmdhfmf.c
index d43a0ef2448c39a4a51c32e23d89f088df30cdf8..94a0b217d1d3707dc6d688a2171c5c90600c4d66 100644 (file)
@@ -804,7 +804,7 @@ int CmdHF14AMfNestedHard(const char *Cmd)
        char ctmp;\r
        ctmp = param_getchar(Cmd, 0);\r
 \r
-       if (ctmp != 'R' && ctmp != 'r' && strlen(Cmd) < 20) {\r
+       if (ctmp != 'R' && ctmp != 'r' && ctmp != 'T' && ctmp != 't' && strlen(Cmd) < 20) {\r
                PrintAndLog("Usage:");\r
                PrintAndLog("      hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");\r
                PrintAndLog("                       <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");\r
@@ -829,15 +829,17 @@ int CmdHF14AMfNestedHard(const char *Cmd)
        bool nonce_file_read = false;\r
        bool nonce_file_write = false;\r
        bool slow = false;\r
+       int tests = 0;\r
+       \r
        \r
        if (ctmp == 'R' || ctmp == 'r') {\r
                nonce_file_read = true;\r
                if (!param_gethex(Cmd, 1, trgkey, 12)) {\r
                        know_target_key = true;\r
                }\r
-\r
+       } else if (ctmp == 'T' || ctmp == 't') {\r
+               tests = param_get32ex(Cmd, 1, 100, 10);\r
        } else {\r
-\r
                blockNo = param_get8(Cmd, 0);\r
                ctmp = param_getchar(Cmd, 1);\r
                if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
@@ -883,13 +885,14 @@ int CmdHF14AMfNestedHard(const char *Cmd)
                }\r
        }\r
 \r
-       PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s ", \r
+       PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d ", \r
                        trgBlockNo, \r
                        trgKeyType?'B':'A', \r
                        trgkey[0], trgkey[1], trgkey[2], trgkey[3], trgkey[4], trgkey[5],\r
                        know_target_key?"":" (not set)",\r
                        nonce_file_write?"write":nonce_file_read?"read":"none",\r
-                       slow?"Yes":"No");\r
+                       slow?"Yes":"No",\r
+                       tests);\r
 \r
        int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key?trgkey:NULL, nonce_file_read, nonce_file_write, slow);\r
 \r
@@ -988,6 +991,7 @@ int CmdHF14AMfChk(const char *Cmd)
                break;\r
        default:\r
                PrintAndLog("Key type must be A , B or ?");\r
+               free(keyBlock);\r
                return 1;\r
        };\r
        \r
@@ -1039,6 +1043,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                                if (!p) {\r
                                                        PrintAndLog("Cannot allocate memory for defKeys");\r
                                                        free(keyBlock);\r
+                                                       free(f);\r
                                                        return 2;\r
                                                }\r
                                                keyBlock = p;\r
@@ -1195,15 +1200,21 @@ int CmdHF14AMf1kSim(const char *Cmd)
 \r
        if(flags & FLAG_INTERACTIVE)\r
        {               \r
-               PrintAndLog("Press pm3-button to abort simulation");\r
-               \r
                uint8_t data[40];\r
                uint8_t key[6];\r
 \r
                UsbCommand resp;                \r
+               PrintAndLog("Press pm3-button or send another cmd to abort simulation");\r
+               //while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                       //We're waiting only 1.5 s at a time, otherwise we get the\r
+                       // annoying message about "Waiting for a response... "\r
+               //}\r
                while(!ukbhit() ){\r
-                       if ( WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {\r
-                               if ( (resp.arg[0] & 0xffff) == CMD_SIMULATE_MIFARE_CARD ){\r
+                       if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) continue;\r
+\r
+                       if ( !(flags & FLAG_NR_AR_ATTACK) ) break;\r
+                       if ( (resp.arg[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD ) break;\r
+\r
                                        memset(data, 0x00, sizeof(data));\r
                                        memset(key, 0x00, sizeof(key));\r
                                        int len = (resp.arg[1] > sizeof(data)) ? sizeof(data) : resp.arg[1];\r
@@ -1213,23 +1224,21 @@ int CmdHF14AMf1kSim(const char *Cmd)
                                        uint64_t corr_uid = 0;\r
                                        if ( memcmp(data, "\x00\x00\x00\x00", 4) == 0 ) {\r
                                                corr_uid = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];\r
-                                       }\r
-                                       else {\r
+                               tryMfk32(corr_uid, data, key);\r
+                       } else {\r
                                                corr_uid |= (uint64_t)data[2] << 48; \r
                                                corr_uid |= (uint64_t)data[1] << 40; \r
                                                corr_uid |= (uint64_t)data[0] << 32;\r
-                                               corr_uid |= data[7] << 24;\r
-                                               corr_uid |= data[6] << 16;\r
-                                               corr_uid |= data[5] << 8;\r
-                                               corr_uid |= data[4];\r
+                                               corr_uid |= (uint64_t)data[7] << 24;\r
+                                               corr_uid |= (uint64_t)data[6] << 16;\r
+                                               corr_uid |= (uint64_t)data[5] << 8;\r
+                                               corr_uid |= (uint64_t)data[4];\r
+                               tryMfk64(corr_uid, data, key);\r
                                        }\r
-                                       tryMfk32(corr_uid, data, key);\r
-                                       //tryMfk64(corr_uid, data, key);\r
                                        PrintAndLog("--");\r
                                }\r
                        }\r
-               }\r
-       }\r
+       \r
        return 0;\r
 }\r
 \r
@@ -1334,7 +1343,7 @@ int CmdHF14AMfELoad(const char *Cmd)
                \r
        if ( ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {\r
                PrintAndLog("It loads emul dump from the file `filename.eml`");\r
-               PrintAndLog("Usage:  hf mf eload [card memory] <file name w/o `.eml`>");\r
+               PrintAndLog("Usage:  hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");\r
                PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");\r
                PrintAndLog("");\r
                PrintAndLog(" sample: hf mf eload filename");\r
@@ -1348,17 +1357,19 @@ int CmdHF14AMfELoad(const char *Cmd)
                case '\0': numBlocks = 16*4; break;\r
                case '2' : numBlocks = 32*4; break;\r
                case '4' : numBlocks = 256; break;\r
-               case 'U' : // fall through  ,  NTAG 215 has 135blocks a 540 bytes.\r
-               case 'u' : numBlocks = 135; blockWidth = 8; break;\r
+               case 'U' : // fall through\r
+               case 'u' : numBlocks = 255; blockWidth = 8; break;\r
                default:  {\r
                        numBlocks = 16*4;\r
                        nameParamNo = 0;\r
                }\r
        }\r
+       uint32_t numblk2 = param_get32ex(Cmd,2,0,10);\r
+       if (numblk2 > 0) numBlocks = numblk2;   \r
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
-       if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+       if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
 \r
        fnameptr += len;\r
 \r
@@ -1455,7 +1466,7 @@ int CmdHF14AMfESave(const char *Cmd)
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
-       if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+       if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
        \r
        // user supplied filename?\r
        if (len < 1) {\r
@@ -1732,7 +1743,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
-               if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+               if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
 \r
                memcpy(filename, Cmd, len);\r
                fnameptr += len;\r
@@ -1816,7 +1827,7 @@ int CmdHF14AMfCGetBlk(const char *Cmd) {
                return 1;\r
        }\r
        \r
-       PrintAndLog("data:%s", sprint_hex(data, sizeof(data)));\r
+       PrintAndLog("data: %s", sprint_hex(data, sizeof(data)));\r
        return 0;\r
 }\r
 \r
@@ -1902,7 +1913,7 @@ int CmdHF14AMfCSave(const char *Cmd) {
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
-               if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+               if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
        \r
                // get filename based on UID\r
                if (len < 1) {\r
@@ -2021,7 +2032,10 @@ int CmdHF14AMfSniff(const char *Cmd){
                        uint16_t traceLen = resp.arg[1];\r
                        len = resp.arg[2];\r
 \r
-                       if (res == 0) return 0;                                         // we are done\r
+                       if (res == 0) {\r
+                               free(buf);\r
+                               return 0;                                               // we are done\r
+                       }\r
 \r
                        if (res == 1) {                                                         // there is (more) data to be transferred\r
                                if (pckNum == 0) {                                              // first packet, (re)allocate necessary buffer\r
@@ -2043,6 +2057,11 @@ int CmdHF14AMfSniff(const char *Cmd){
                                        bufsize = traceLen;\r
                                        memset(buf, 0x00, traceLen);\r
                                }\r
+                               if (bufPtr == NULL) {\r
+                                       PrintAndLog("Cannot allocate memory for trace");\r
+                                       free(buf);\r
+                                       return 2;\r
+                               }\r
                                memcpy(bufPtr, resp.d.asBytes, len);\r
                                bufPtr += len;\r
                                pckNum++;\r
@@ -2119,48 +2138,46 @@ int CmdHf14MfDecryptBytes(const char *Cmd){
        return tryDecryptWord( nt, ar_enc, at_enc, data, len);\r
 }\r
 \r
-static command_t CommandTable[] =\r
-{\r
-  {"help",             CmdHelp,                                1, "This help"},\r
-  {"dbg",              CmdHF14AMfDbg,                  0, "Set default debug mode"},\r
-  {"rdbl",             CmdHF14AMfRdBl,                 0, "Read MIFARE classic block"},\r
-  {"rdsc",             CmdHF14AMfRdSc,                 0, "Read MIFARE classic sector"},\r
-  {"dump",             CmdHF14AMfDump,                 0, "Dump MIFARE classic tag to binary file"},\r
-  {"restore",  CmdHF14AMfRestore,              0, "Restore MIFARE classic binary file to BLANK tag"},\r
-  {"wrbl",             CmdHF14AMfWrBl,                 0, "Write MIFARE classic block"},\r
-  {"chk",              CmdHF14AMfChk,                  0, "Test block keys"},\r
-  {"mifare",   CmdHF14AMifare,                 0, "Read parity error messages."},\r
-  {"nested",   CmdHF14AMfNested,               0, "Test nested authentication"},\r
+static command_t CommandTable[] = {\r
+       {"help",                CmdHelp,                                1, "This help"},\r
+       {"dbg",                 CmdHF14AMfDbg,                  0, "Set default debug mode"},\r
+       {"rdbl",                CmdHF14AMfRdBl,                 0, "Read MIFARE classic block"},\r
+       {"rdsc",                CmdHF14AMfRdSc,                 0, "Read MIFARE classic sector"},\r
+       {"dump",                CmdHF14AMfDump,                 0, "Dump MIFARE classic tag to binary file"},\r
+       {"restore",             CmdHF14AMfRestore,              0, "Restore MIFARE classic binary file to BLANK tag"},\r
+       {"wrbl",                CmdHF14AMfWrBl,                 0, "Write MIFARE classic block"},\r
+       {"chk",                 CmdHF14AMfChk,                  0, "Test block keys"},\r
+       {"mifare",              CmdHF14AMifare,                 0, "Read parity error messages."},\r
+       {"nested",              CmdHF14AMfNested,               0, "Test nested authentication"},\r
        {"hardnested",  CmdHF14AMfNestedHard,   0, "Nested attack for hardened Mifare cards"},\r
-  {"sniff",            CmdHF14AMfSniff,                0, "Sniff card-reader communication"},\r
-  {"sim",              CmdHF14AMf1kSim,                0, "Simulate MIFARE card"},\r
-  {"eclr",             CmdHF14AMfEClear,               0, "Clear simulator memory block"},\r
-  {"eget",             CmdHF14AMfEGet,                 0, "Get simulator memory block"},\r
-  {"eset",             CmdHF14AMfESet,                 0, "Set simulator memory block"},\r
-  {"eload",            CmdHF14AMfELoad,                0, "Load from file emul dump"},\r
-  {"esave",            CmdHF14AMfESave,                0, "Save to file emul dump"},\r
-  {"ecfill",   CmdHF14AMfECFill,               0, "Fill simulator memory with help of keys from simulator"},\r
-  {"ekeyprn",  CmdHF14AMfEKeyPrn,              0, "Print keys from simulator memory"},\r
-  {"csetuid",  CmdHF14AMfCSetUID,              0, "Set UID for magic Chinese card"},\r
-  {"csetblk",  CmdHF14AMfCSetBlk,              0, "Write block - Magic Chinese card"},\r
-  {"cgetblk",  CmdHF14AMfCGetBlk,              0, "Read block - Magic Chinese card"},\r
-  {"cgetsc",   CmdHF14AMfCGetSc,               0, "Read sector - Magic Chinese card"},\r
-  {"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",   CmdHf14MfDecryptBytes,  1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
-  {NULL, NULL, 0, NULL}\r
+       {"sniff",               CmdHF14AMfSniff,                0, "Sniff card-reader communication"},\r
+       {"sim",                 CmdHF14AMf1kSim,                0, "Simulate MIFARE card"},\r
+       {"eclr",                CmdHF14AMfEClear,               0, "Clear simulator memory block"},\r
+       {"eget",                CmdHF14AMfEGet,                 0, "Get simulator memory block"},\r
+       {"eset",                CmdHF14AMfESet,                 0, "Set simulator memory block"},\r
+       {"eload",               CmdHF14AMfELoad,                0, "Load from file emul dump"},\r
+       {"esave",               CmdHF14AMfESave,                0, "Save to file emul dump"},\r
+       {"ecfill",              CmdHF14AMfECFill,               0, "Fill simulator memory with help of keys from simulator"},\r
+       {"ekeyprn",             CmdHF14AMfEKeyPrn,              0, "Print keys from simulator memory"},\r
+       {"csetuid",             CmdHF14AMfCSetUID,              0, "Set UID for magic Chinese card"},\r
+       {"csetblk",             CmdHF14AMfCSetBlk,              0, "Write block - Magic Chinese card"},\r
+       {"cgetblk",             CmdHF14AMfCGetBlk,              0, "Read block - Magic Chinese card"},\r
+       {"cgetsc",              CmdHF14AMfCGetSc,               0, "Read sector - Magic Chinese card"},\r
+       {"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",             CmdHf14MfDecryptBytes,  1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
+       {NULL, NULL, 0, NULL}\r
 };\r
 \r
-int CmdHFMF(const char *Cmd)\r
-{\r
+int CmdHFMF(const char *Cmd) {\r
        // flush\r
-       WaitForResponseTimeout(CMD_ACK,NULL,100);\r
+       clearCommandBuffer();\r
+       //WaitForResponseTimeout(CMD_ACK,NULL,100);\r
        CmdsParse(CommandTable, Cmd);\r
        return 0;\r
 }\r
 \r
-int CmdHelp(const char *Cmd)\r
-{\r
+int CmdHelp(const char *Cmd) {\r
        CmdsHelp(CommandTable);\r
        return 0;\r
 }\r
Impressum, Datenschutz