]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
CHG: a bit code clean up,
[proxmark3-svn] / client / cmdhfmf.c
index bd27ba0caa40ac08250d631a305d9d7b4a33e150..ba8295ec9aa7337c38578436426d1b8a8028d30d 100644 (file)
@@ -63,13 +63,8 @@ start:
                }\r
                \r
                UsbCommand resp;\r
-               if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r
+               if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
                        isOK  = resp.arg[0];\r
-                       uid = (uint32_t)bytes_to_num(resp.d.asBytes +  0, 4);\r
-                       nt =  (uint32_t)bytes_to_num(resp.d.asBytes +  4, 4);\r
-                       par_list = bytes_to_num(resp.d.asBytes +  8, 8);\r
-                       ks_list = bytes_to_num(resp.d.asBytes +  16, 8);\r
-                       nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
                        printf("\n\n");\r
                        switch (isOK) {\r
                                case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
@@ -79,6 +74,11 @@ start:
                                                  PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;\r
                                default: ;\r
                        }\r
+                       uid = (uint32_t)bytes_to_num(resp.d.asBytes +  0, 4);\r
+                       nt =  (uint32_t)bytes_to_num(resp.d.asBytes +  4, 4);\r
+                       par_list = bytes_to_num(resp.d.asBytes +  8, 8);\r
+                       ks_list = bytes_to_num(resp.d.asBytes +  16, 8);\r
+                       nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
                        break;\r
                }\r
        }       \r
@@ -96,14 +96,12 @@ start:
                c.arg[0] = false;\r
                goto start;\r
        } else {\r
-               isOK = 0;\r
-               printf("------------------------------------------------------------------\n");\r
                PrintAndLog("Found valid key: %012"llx" \n", r_key);\r
        }\r
+       \r
        t1 = clock() - t1;\r
-       if ( t1 > 0 ){\r
-               PrintAndLog("Time in darkside: %.0f ticks - %4.2f sec\n (%u)", (float)t1, ((float)t1)/CLOCKS_PER_SEC, CLOCKS_PER_SEC);\r
-       }\r
+       if ( t1 > 0 )\r
+               PrintAndLog("Time in darkside: %.0f ticks\n", (float)t1);\r
        return 0;\r
 }\r
 \r
@@ -575,7 +573,7 @@ int CmdHF14AMfNested(const char *Cmd)
        uint8_t trgKeyType = 0;\r
        uint8_t SectorsCnt = 0;\r
        uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
-       uint8_t keyBlock[14*6];\r
+       uint8_t keyBlock[6*6];\r
        uint64_t key64 = 0;\r
        bool transferToEml = false;\r
        \r
@@ -595,10 +593,11 @@ int CmdHF14AMfNested(const char *Cmd)
                PrintAndLog("t - transfer keys into emulator memory");\r
                PrintAndLog("d - write keys to binary file");\r
                PrintAndLog(" ");\r
-               PrintAndLog("      sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r
-               PrintAndLog("      sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r
-               PrintAndLog("      sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r
-               PrintAndLog("      sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r
+               PrintAndLog(" samples:");\r
+               PrintAndLog("              hf mf nested 1 0 A FFFFFFFFFFFF ");\r
+               PrintAndLog("              hf mf nested 1 0 A FFFFFFFFFFFF t ");\r
+               PrintAndLog("              hf mf nested 1 0 A FFFFFFFFFFFF d ");\r
+               PrintAndLog("              hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r
                return 0;\r
        }       \r
        \r
@@ -649,40 +648,35 @@ int CmdHF14AMfNested(const char *Cmd)
        transferToEml |= (ctmp == 'd' || ctmp == 'D');\r
        \r
        if (cmdp == 'o') {\r
-               PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');\r
                int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);\r
-               if (isOK) {\r
-                       switch (isOK) {\r
-                               case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
-                               case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
-                               case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
-                               default : PrintAndLog("Unknown Error.\n");\r
-                       }\r
-                       return 2;\r
-               }\r
-               key64 = bytes_to_num(keyBlock, 6);\r
-               if (key64) {\r
-                       PrintAndLog("Found valid key:%012"llx, key64);\r
-\r
-                       // transfer key to the emulator\r
-                       if (transferToEml) {\r
-                               uint8_t sectortrailer;\r
-                               if (trgBlockNo < 32*4) {        // 4 block sector\r
-                                       sectortrailer = (trgBlockNo & 0x03) + 3;\r
-                               } else {                                        // 16 block sector\r
-                                       sectortrailer = (trgBlockNo & 0x0f) + 15;\r
+               switch (isOK) {\r
+                       case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
+                       case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
+                       case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
+                       case -4 : PrintAndLog("No valid key found"); break;\r
+                       case -5 : \r
+                               key64 = bytes_to_num(keyBlock, 6);\r
+\r
+                               // transfer key to the emulator\r
+                               if (transferToEml) {\r
+                                       uint8_t sectortrailer;\r
+                                       if (trgBlockNo < 32*4) {        // 4 block sector\r
+                                               sectortrailer = (trgBlockNo & 0x03) + 3;\r
+                                       } else {                                        // 16 block sector\r
+                                               sectortrailer = (trgBlockNo & 0x0f) + 15;\r
+                                       }\r
+                                       mfEmlGetMem(keyBlock, sectortrailer, 1);\r
+                       \r
+                                       if (!trgKeyType)\r
+                                               num_to_bytes(key64, 6, keyBlock);\r
+                                       else\r
+                                               num_to_bytes(key64, 6, &keyBlock[10]);\r
+                                       mfEmlSetMem(keyBlock, sectortrailer, 1);                \r
                                }\r
-                               mfEmlGetMem(keyBlock, sectortrailer, 1);\r
-               \r
-                               if (!trgKeyType)\r
-                                       num_to_bytes(key64, 6, keyBlock);\r
-                               else\r
-                                       num_to_bytes(key64, 6, &keyBlock[10]);\r
-                               mfEmlSetMem(keyBlock, sectortrailer, 1);                \r
-                       }\r
-               } else {\r
-                       PrintAndLog("No valid key found");\r
+                               return 0;\r
+                       default : PrintAndLog("Unknown Error.\n");\r
                }\r
+               return 2;\r
        }\r
        else { // ------------------------------------  multiple sectors working\r
                clock_t t1 = clock();\r
@@ -697,14 +691,6 @@ int CmdHF14AMfNested(const char *Cmd)
                num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
                num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r
                num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r
-               num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));\r
-               num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));\r
-               num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));\r
-               num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));\r
-               num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));\r
-               num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));\r
-               num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));\r
-               num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));\r
 \r
                PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
                for (i = 0; i < SectorsCnt; i++) {\r
@@ -719,44 +705,52 @@ int CmdHF14AMfNested(const char *Cmd)
                                }\r
                        }\r
                }\r
+               clock_t t2 = clock() - t1;\r
+               if ( t2 > 0 )\r
+                       PrintAndLog("Time to check 6 known keys: %.0f ticks", (float)t2 );\r
+\r
+               PrintAndLog("enter nested..."); \r
                \r
                // nested sectors\r
                iterations = 0;\r
-               PrintAndLog("nested...");\r
                bool calibrate = true;\r
+\r
                for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r
-                       for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
-                               for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r
+                       for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; ++sectorNo) {\r
+                               for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) { \r
+\r
                                        if (e_sector[sectorNo].foundKey[trgKeyType]) continue;\r
-                                       PrintAndLog("-----------------------------------------------");\r
-                                       int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);\r
-                                       if(isOK) {\r
-                                               switch (isOK) {\r
-                                                       case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
-                                                       case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
-                                                       case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
-                                                       default : PrintAndLog("Unknown Error.\n");\r
-                                               }\r
-                                               free(e_sector);\r
-                                               return 2;\r
-                                       } else {\r
-                                               calibrate = false;\r
-                                       }\r
                                        \r
-                                       iterations++;\r
-\r
-                                       key64 = bytes_to_num(keyBlock, 6);\r
-                                       if (key64) {\r
-                                               PrintAndLog("Found valid key:%012"llx, key64);\r
-                                               e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
-                                               e_sector[sectorNo].Key[trgKeyType] = key64;\r
+                                       int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);\r
+                                       switch (isOK) {\r
+                                               case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
+                                               case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
+                                               case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
+                                               case -4 : //key not found\r
+                                                       calibrate = false;\r
+                                                       iterations++;\r
+                                                       continue; \r
+                                               case -5 :\r
+                                                       calibrate = false;\r
+                                                       iterations++;\r
+                                                       e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
+                                                       e_sector[sectorNo].Key[trgKeyType] = bytes_to_num(keyBlock, 6);\r
+                                                       continue;\r
+                                                       \r
+                                               default : PrintAndLog("Unknown Error.\n");\r
                                        }\r
+                                       free(e_sector);\r
+                                       return 2;\r
                                }\r
                        }\r
                }\r
+               \r
+               t1 = clock() - t1;\r
+               if ( t1 > 0 )\r
+                       PrintAndLog("Time in nested: %.0f ticks \n", (float)t1);\r
 \r
                // 20160116 If Sector A is found, but not Sector B,  try just reading it of the tag?\r
-               PrintAndLog("testing to read B...");\r
+               PrintAndLog("trying to read key B...");\r
                for (i = 0; i < SectorsCnt; i++) {\r
                        // KEY A  but not KEY B\r
                        if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {\r
@@ -780,15 +774,12 @@ int CmdHF14AMfNested(const char *Cmd)
                                key64 = bytes_to_num(data+10, 6);\r
                                if (key64) {\r
                                        PrintAndLog("Data:%s", sprint_hex(data+10, 6));\r
-                                       e_sector[i].foundKey[1] = 1;\r
+                                       e_sector[i].foundKey[1] = TRUE;\r
                                        e_sector[i].Key[1] = key64;\r
                                }\r
                        }\r
                }\r
-               \r
-               t1 = clock() - t1;\r
-               if ( t1 > 0 )\r
-                       PrintAndLog("Time in nested: %.0f ticks %4.2f sec (%4.2f sec per key)\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC, ((float)t1)/iterations/CLOCKS_PER_SEC);\r
+\r
                \r
                //print them\r
                printKeyTable( SectorsCnt, e_sector );\r
@@ -993,8 +984,7 @@ int CmdHF14AMfChk(const char *Cmd)
        keyBlock = calloc(stKeyBlock, 6);\r
        if (keyBlock == NULL) return 1;\r
 \r
-       uint64_t defaultKeys[] =\r
-       {\r
+       uint64_t defaultKeys[] = {\r
                0xffffffffffff, // Default key (first key used by program if no user defined key)\r
                0x000000000000, // Blank key\r
                0xa0a1a2a3a4a5, // NFCForum MAD key\r
@@ -1012,9 +1002,8 @@ int CmdHF14AMfChk(const char *Cmd)
        int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);\r
 \r
        for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
-       {\r
                num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
-       }\r
+\r
        \r
        if (param_getchar(Cmd, 0)=='*') {\r
                blockNo = 3;\r
@@ -1025,9 +1014,9 @@ int CmdHF14AMfChk(const char *Cmd)
                        case '4': SectorsCnt = 40; break;\r
                        default:  SectorsCnt = 16;\r
                }\r
-       }\r
-       else\r
+       } else {\r
                blockNo = param_get8(Cmd, 0);\r
+       }\r
        \r
        ctmp = param_getchar(Cmd, 1);\r
        switch (ctmp) { \r
@@ -1061,7 +1050,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                }\r
                                keyBlock = p;\r
                        }\r
-                       PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
+                       PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
                        (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
                        (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4],     (keyBlock + 6*keycnt)[5], 6);\r
                        keycnt++;\r
@@ -1101,7 +1090,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                        }\r
                                        memset(keyBlock + 6 * keycnt, 0, 6);\r
                                        num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
-                                       PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
+                                       PrintAndLog("check key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
                                        keycnt++;\r
                                        memset(buf, 0, sizeof(buf));\r
                                }\r
@@ -1118,7 +1107,7 @@ int CmdHF14AMfChk(const char *Cmd)
        if (keycnt == 0) {\r
                PrintAndLog("No key specified, trying default keys");\r
                for (;keycnt < defaultKeysSize; keycnt++)\r
-                       PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
+                       PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
                                (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
                                (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4],     (keyBlock + 6*keycnt)[5], 6);\r
        }\r
@@ -1131,21 +1120,20 @@ int CmdHF14AMfChk(const char *Cmd)
        }\r
 \r
        uint8_t trgKeyType = 0;\r
+       uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;\r
        \r
        // time\r
        clock_t t1 = clock();\r
        \r
        // check keys.\r
-       for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) {\r
+       for (trgKeyType = !keyType;  trgKeyType < 2;  (keyType==2) ? (++trgKeyType) : (trgKeyType=2) ) {\r
+\r
                int b = blockNo;\r
                for (int i = 0; i < SectorsCnt; ++i) {\r
                        \r
                        // skip already found keys.\r
                        if (e_sector[i].foundKey[trgKeyType]) continue;\r
                        \r
-                       PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, trgKeyType ? 'B':'A', keycnt);\r
-                       \r
-                       uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;\r
                        \r
                        for (uint32_t c = 0; c < keycnt; c += max_keys) {\r
                                \r
@@ -1153,18 +1141,24 @@ int CmdHF14AMfChk(const char *Cmd)
                                \r
                                res = mfCheckKeys(b, trgKeyType, true, size, &keyBlock[6*c], &key64);\r
                                if (!res) {\r
-                                       PrintAndLog("Found valid key:[%012"llx"]",key64);                                       \r
+                                       //PrintAndLog("Sector:%3d Block:%3d, key type: %C  -- Found key [%012"llx"]", i, b, trgKeyType ? 'B':'A', key64);\r
+                                                                                \r
                                        e_sector[i].Key[trgKeyType] = key64;\r
                                        e_sector[i].foundKey[trgKeyType] = TRUE;\r
                                        break;\r
-                               } else {\r
+                               } else {                                        \r
                                        e_sector[i].Key[trgKeyType] = 0xffffffffffff;\r
                                        e_sector[i].foundKey[trgKeyType] = FALSE;\r
                                }\r
+                               printf(".");\r
                        }\r
                        b < 127 ? ( b +=4 ) : ( b += 16 );      \r
                }\r
        }\r
+       t1 = clock() - t1;\r
+       if ( t1 > 0 )\r
+               printf("\nTime in checkkeys: %.0f ticks\n", (float)t1);\r
+\r
        // 20160116 If Sector A is found, but not Sector B,  try just reading it of the tag?\r
        PrintAndLog("testing to read B...");\r
        for (i = 0; i < SectorsCnt; i++) {\r
@@ -1195,10 +1189,7 @@ int CmdHF14AMfChk(const char *Cmd)
                        }\r
                }\r
        }\r
-       \r
-       t1 = clock() - t1;\r
-       if ( t1 > 0 )\r
-               printf("Time in checkkeys: %.0f ticks  %1.2f sec (%1.2f sec per key)\n\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC, ((float)t1)/keycnt/CLOCKS_PER_SEC);\r
+\r
 \r
        //print them\r
        printKeyTable( SectorsCnt, e_sector );\r
Impressum, Datenschutz