]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
CHG: Remake the "hf mf chkkeys" It now prints the found keys like "hf mf nested...
authoriceman1001 <iceman@iuse.se>
Wed, 20 Jan 2016 18:51:30 +0000 (19:51 +0100)
committericeman1001 <iceman@iuse.se>
Wed, 20 Jan 2016 18:51:30 +0000 (19:51 +0100)
client/cmdhfmf.c
client/cmdhfmf.h
client/cmdhfmfdesfire.c

index 1d516ebc32243f678bcfa17eb6bbcd23861e2d34..e10a88f3940c7ef37b7cf6496fa64609f70834b2 100644 (file)
@@ -54,7 +54,7 @@ start:
                }\r
                \r
                UsbCommand resp;\r
-               if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {\r
+               if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\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
@@ -93,7 +93,7 @@ start:
        }\r
        t1 = clock() - t1;\r
        if ( t1 > 0 ){\r
-               PrintAndLog("Time in darkside: %f ticks - %1.2f sec\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC);\r
+               PrintAndLog("Time in darkside: %.0f ticks - %4.2f sec\n (%u)", (float)t1, ((float)t1)/CLOCKS_PER_SEC, CLOCKS_PER_SEC);\r
        }\r
        return 0;\r
 }\r
@@ -706,7 +706,7 @@ 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
@@ -763,39 +763,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
                t1 = clock() - t1;\r
-               if ( t1 > 0 ) {\r
-                       PrintAndLog("Time in nested: %f ticks %1.2f sec (%1.2f sec per key)\n\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC, ((float)t1)/iterations/CLOCKS_PER_SEC);\r
-               }\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
@@ -979,6 +964,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
@@ -987,6 +974,8 @@ 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
@@ -1124,80 +1113,137 @@ int CmdHF14AMfChk(const char *Cmd)
        }\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 t1 = clock();\r
-               \r
-       for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {\r
-               int b=blockNo;\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
+                       \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
+                               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("Found valid key:[%012"llx"]",key64);                                       \r
+                                       e_sector[i].Key[trgKeyType] = key64;\r
+                                       e_sector[i].foundKey[trgKeyType] = TRUE;\r
+                                       break;\r
                                } else {\r
-                                       PrintAndLog("Command execute timeout");\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
-       t1 = clock() - t1;\r
-       if ( t1 > 0 ){\r
-               printf("Time in checkkeys: %f 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
+       // 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
+                       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
+                       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
        }\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];\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
-                       }\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
+       \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
index 6c0b206cc2370d47522a1b5278ae9ea887cd8d28..029e12d5cc75b7d68a7acbb22e40e42ec65ac2aa 100644 (file)
@@ -56,4 +56,5 @@ int CmdHF14AMfCLoad(const char* cmd);
 int CmdHF14AMfCSave(const char* cmd);\r
 int CmdHf14MfDecryptBytes(const char *Cmd);\r
 \r
+void printKeyTable( uint8_t sectorscnt, sector *e_sector );\r
 #endif\r
index 576792d0297fb10814dd36b9191223f24dc503d5..5eada6e9f1df8f175e9ba77f77a5aa24c5a744be 100644 (file)
@@ -33,8 +33,8 @@ int CmdHF14AMfDESAuth(const char *Cmd){
         
     uint8_t blockNo = 0;
     //keyNo=0;
-    uint32_t cuid=0;
-    uint8_t reply[16];
+    uint32_t cuid = 0;
+    uint8_t reply[16] = {0x00};
     //DES_cblock r1_b1;
     uint8_t b1[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
     uint8_t b2[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
@@ -126,8 +126,8 @@ int CmdHF14AMfAESAuth(const char *Cmd){
         
     uint8_t blockNo = 0;
     //keyNo=0;
-    uint32_t cuid=0;
-    uint8_t reply[32];
+    uint32_t cuid = 0;
+    uint8_t reply[32] = {0x00};
     //DES_cblock r1_b1;
     //unsigned char * b1, b2, nr, b0, r0, r1;
     
Impressum, Datenschutz