]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
CHG: Syntax suger
[proxmark3-svn] / client / cmdhfmf.c
index b189e19bfaafe712dd9e5efe73dfe15c1a2a75cd..d354952b10d8a2ab8d227c7f9b4ed92b4bb4dbe9 100644 (file)
@@ -21,8 +21,17 @@ int CmdHF14AMifare(const char *Cmd)
        uint64_t par_list = 0, ks_list = 0, r_key = 0;\r
        int16_t isOK = 0;\r
        int tmpchar; \r
+       uint8_t blockNo = 0;\r
+       \r
+       char cmdp = param_getchar(Cmd, 0);      \r
+       if ( cmdp == 'H' || cmdp == 'h') {\r
+               PrintAndLog("Usage:  hf mf mifare <block number>");\r
+               PrintAndLog("        sample: hf mf mifare 0");\r
+               return 0;\r
+       }       \r
        \r
-       UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r
+       blockNo = param_get8(Cmd, 0);\r
+       UsbCommand c = {CMD_READER_MIFARE, {true, blockNo, 0}};\r
 \r
        // message\r
        printf("-------------------------------------------------------------------------\n");\r
@@ -30,7 +39,7 @@ int CmdHF14AMifare(const char *Cmd)
        printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");\r
        printf("-------------------------------------------------------------------------\n");\r
 \r
-       clock_t t = clock();\r
+       clock_t t1 = clock();\r
        \r
 start:\r
     clearCommandBuffer();\r
@@ -54,7 +63,7 @@ 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
@@ -87,13 +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
-       t = clock() - t;\r
-       //printf("Time in darkside: %d ticks - %1.2f seconds\n", t, ((float)t)/CLOCKS_PER_SEC);\r
-       printf("Time in darkside: %Lf ticks - %1.2Lf seconds\n", (long double)t, ((long double)t)/CLOCKS_PER_SEC);\r
+       \r
+       t1 = clock() - t1;\r
+       if ( t1 > 0 )\r
+               PrintAndLog("Time in darkside: %.0f ticks - %4.2f sec\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC);\r
        return 0;\r
 }\r
 \r
@@ -512,9 +520,9 @@ int CmdHF14AMfRestore(const char *Cmd)
        for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
                for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
                        UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};\r
-                       memcpy(c.d.asBytes, key, 6);\r
-                       \r
-                       if (fread(bldata, 1, 16, fdump) == 0) {\r
+                       memcpy(c.d.asBytes, key, 6);                    \r
+                       bytes_read = fread(bldata, 1, 16, fdump);\r
+                       if ( bytes_read == 0) {\r
                                PrintAndLog("File reading error (dumpdata.bin).");\r
                                fclose(fdump);\r
                                return 2;\r
@@ -565,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
@@ -639,43 +647,38 @@ 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 time1 = clock();\r
+               clock_t t1 = clock();\r
 \r
                e_sector = calloc(SectorsCnt, sizeof(sector));\r
                if (e_sector == NULL) return 1;\r
@@ -687,14 +690,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
@@ -705,54 +700,59 @@ int CmdHF14AMfNested(const char *Cmd)
                                \r
                                if (!res) {\r
                                        e_sector[i].Key[j] = key64;\r
-                                       e_sector[i].foundKey[j] = 1;\r
+                                       e_sector[i].foundKey[j] = TRUE;\r
                                }\r
                        }\r
                }\r
+               clock_t t2 = clock() - t1;\r
+               if ( t2 > 0 )\r
+                       PrintAndLog("Time to check 6 known keys: %.0f ticks %4.2f sec", (float)t2, ((float)t2)/CLOCKS_PER_SEC);\r
+       \r
                \r
                // nested sectors\r
                iterations = 0;\r
-               PrintAndLog("nested...");\r
+               PrintAndLog("enter nested...");\r
                bool calibrate = true;\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
                // 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
                                \r
                                uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
                                \r
+                               PrintAndLog("Reading block %d", sectrail);\r
+                                                       \r
                                UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};\r
                                num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A\r
                                clearCommandBuffer();\r
@@ -762,36 +762,24 @@ int CmdHF14AMfNested(const char *Cmd)
                                if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;\r
                                        \r
                                uint8_t isOK  = resp.arg[0] & 0xff;\r
-                               uint8_t *data = resp.d.asBytes;\r
-\r
-                               if (isOK) {\r
+                               if (!isOK) continue;\r
 \r
-                                       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].Key[1] = key64;\r
-                                       }\r
+                               uint8_t *data = resp.d.asBytes;\r
+                               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].Key[1] = key64;\r
                                }\r
                        }\r
                }\r
                \r
-               PrintAndLog("Time in nested: %1.2f (%1.2f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/iterations/CLOCKS_PER_SEC);\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
-               PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r
                //print them\r
-               PrintAndLog("|---|----------------|---|----------------|---|");\r
-               PrintAndLog("|sec|key A           |res|key B           |res|");\r
-               PrintAndLog("|---|----------------|---|----------------|---|");\r
-               for (i = 0; i < SectorsCnt; i++) {\r
-                       PrintAndLog("|%03d|  %012"llx"  | %d |  %012"llx"  | %d |", i,\r
-                               e_sector[i].Key[0], \r
-                               e_sector[i].foundKey[0], \r
-                               e_sector[i].Key[1], \r
-                               e_sector[i].foundKey[1]\r
-                       );\r
-               }\r
-               PrintAndLog("|---|----------------|---|----------------|---|");\r
+               printKeyTable( SectorsCnt, e_sector );\r
                \r
                // transfer them to the emulator\r
                if (transferToEml) {\r
@@ -975,6 +963,8 @@ int CmdHF14AMfChk(const char *Cmd)
        uint8_t *keyBlock = NULL, *p;\r
        uint8_t stKeyBlock = 20;\r
        \r
+       sector *e_sector = NULL;\r
+       \r
        int i, res;\r
        int     keycnt = 0;\r
        char ctmp       = 0x00;\r
@@ -983,14 +973,15 @@ int CmdHF14AMfChk(const char *Cmd)
        uint8_t keyType = 0;\r
        uint64_t key64 = 0;\r
        \r
+       uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
+               \r
        int transferToEml = 0;\r
        int createDumpFile = 0;\r
 \r
        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
@@ -1008,9 +999,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
@@ -1021,9 +1011,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
@@ -1057,7 +1047,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                }\r
                                keyBlock = p;\r
                        }\r
-                       PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
+                       PrintAndLog("check 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
@@ -1097,7 +1087,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 custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
                                        keycnt++;\r
                                        memset(buf, 0, sizeof(buf));\r
                                }\r
@@ -1114,84 +1104,144 @@ 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("check default 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
        \r
        // initialize storage for found keys\r
-       bool validKey[2][40];\r
-       uint8_t foundKey[2][40][6];\r
-       for (uint16_t t = 0; t < 2; t++) {\r
-               for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
-                       validKey[t][sectorNo] = false;\r
-                       for (uint16_t i = 0; i < 6; i++) {\r
-                               foundKey[t][sectorNo][i] = 0xff;\r
-                       }\r
-               }\r
+       e_sector = calloc(SectorsCnt, sizeof(sector));\r
+       if (e_sector == NULL) {\r
+               free(keyBlock);\r
+               return 1;\r
        }\r
+\r
+       uint8_t trgKeyType = 0;\r
+       \r
        // time\r
-       clock_t time1 = clock();\r
-               \r
-       for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {\r
-               int b=blockNo;\r
+       clock_t t1 = clock();\r
+       \r
+       // check keys.\r
+       for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) {\r
+               int b = blockNo;\r
                for (int i = 0; i < SectorsCnt; ++i) {\r
-                       PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);\r
-                       uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r
-                       for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r
-                               uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r
-                               res = mfCheckKeys(b, t, true, size, &keyBlock[6*c], &key64);\r
-                               if (res != 1) {\r
-                                       if (!res) {\r
-                                               PrintAndLog("Found valid key:[%012"llx"]",key64);\r
-                                               num_to_bytes(key64, 6, foundKey[t][i]);\r
-                                               validKey[t][i] = true;\r
-                                       } \r
-                               } else {\r
-                                       PrintAndLog("Command execute timeout");\r
+                       \r
+                       // skip already found keys.\r
+                       if (e_sector[i].foundKey[trgKeyType]) continue;\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
+                               uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;\r
+                               \r
+                               res = mfCheckKeys(b, trgKeyType, true, size, &keyBlock[6*c], &key64);\r
+                               if (!res) {\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
+                                       e_sector[i].Key[trgKeyType] = 0xffffffffffff;\r
+                                       e_sector[i].foundKey[trgKeyType] = FALSE;\r
                                }\r
                        }\r
-                       b<127?(b+=4):(b+=16);   \r
+                       b < 127 ? ( b +=4 ) : ( b += 16 );      \r
                }\r
        }\r
-       printf("Time in checkkeys: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/keycnt/CLOCKS_PER_SEC);\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
+               // KEY A  but not KEY B\r
+               if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {\r
+                                               \r
+                       uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
+                       \r
+                       PrintAndLog("Reading block %d", sectrail);\r
+                       \r
+                       UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};\r
+                       num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A\r
+                       clearCommandBuffer();\r
+                       SendCommand(&c);\r
 \r
-       if (transferToEml) {\r
-               uint8_t block[16];\r
-               for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
-                       if (validKey[0][sectorNo] || validKey[1][sectorNo]) {\r
-                               mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
-                               for (uint16_t t = 0; t < 2; t++) {\r
-                                       if (validKey[t][sectorNo]) {\r
-                                               memcpy(block + t*10, foundKey[t][sectorNo], 6);\r
-                                       }\r
-                               }\r
-                               mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
+                       UsbCommand resp;\r
+                       if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;\r
+                               \r
+                       uint8_t isOK  = resp.arg[0] & 0xff;\r
+                       if (!isOK) continue;\r
+\r
+                       uint8_t *data = resp.d.asBytes;\r
+                       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].Key[1] = key64;\r
                        }\r
                }\r
-               PrintAndLog("Found keys have been transferred to the emulator memory");\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
+       //print them\r
+       printKeyTable( SectorsCnt, e_sector );\r
+       \r
+       if (transferToEml) {\r
+               uint8_t block[16] = {0x00};\r
+               for (uint8_t i = 0; i < SectorsCnt; ++i ) {\r
+                       mfEmlGetMem(block, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r
+                       if (e_sector[i].foundKey[0])\r
+                               num_to_bytes(e_sector[i].Key[0], 6, block);\r
+                       if (e_sector[i].foundKey[1])\r
+                               num_to_bytes(e_sector[i].Key[1], 6, block+10);\r
+                       mfEmlSetMem(block, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r
+               }\r
+               PrintAndLog("Found keys have been transferred to the emulator memory");\r
+       }\r
+       \r
        if (createDumpFile) {\r
                FILE *fkeys = fopen("dumpkeys.bin","wb");\r
                if (fkeys == NULL) { \r
                        PrintAndLog("Could not create file dumpkeys.bin");\r
                        free(keyBlock);\r
+                       free(e_sector);\r
                        return 1;\r
                }\r
-               for (uint16_t t = 0; t < 2; t++) {\r
-                       fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);\r
+               PrintAndLog("Printing keys to binary file dumpkeys.bin...");\r
+       \r
+               for( i=0; i<SectorsCnt; i++) {\r
+                       num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
+                       fwrite ( tempkey, 1, 6, fkeys );\r
+               }\r
+               for(i=0; i<SectorsCnt; i++) {\r
+                       num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
+                       fwrite ( tempkey, 1, 6, fkeys );\r
                }\r
                fclose(fkeys);\r
-               PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");\r
+               PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");                    \r
        }\r
-\r
+       \r
        free(keyBlock);\r
+       free(e_sector);\r
        PrintAndLog("");\r
        return 0;\r
 }\r
 \r
+void printKeyTable( uint8_t sectorscnt, sector *e_sector ){\r
+       PrintAndLog("|---|----------------|---|----------------|---|");\r
+       PrintAndLog("|sec|key A           |res|key B           |res|");\r
+       PrintAndLog("|---|----------------|---|----------------|---|");\r
+       for (uint8_t i = 0; i < sectorscnt; ++i) {\r
+               PrintAndLog("|%03d|  %012"llx"  | %d |  %012"llx"  | %d |", i,\r
+                       e_sector[i].Key[0], e_sector[i].foundKey[0], \r
+                       e_sector[i].Key[1], e_sector[i].foundKey[1]\r
+               );\r
+       }\r
+       PrintAndLog("|---|----------------|---|----------------|---|");\r
+}\r
+\r
 int CmdHF14AMf1kSim(const char *Cmd)\r
 {\r
        uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};\r
Impressum, Datenschutz