]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/mifarehost.c
hf mf mifare:
[proxmark3-svn] / client / mifarehost.c
index 8a840d4701a8ec852cf653c8c60349823accb492..471fbc4231745dc4a436995ae26e284d05e86488 100644 (file)
@@ -73,13 +73,12 @@ static uint32_t intersection(uint64_t *list1, uint64_t *list2)
 \r
 \r
 // Darkside attack (hf mf mifare)\r
-static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r
+static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint32_t ar, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r
        struct Crypto1State *states;\r
-       uint32_t i, pos, rr; //nr_diff;\r
+       uint32_t i, pos;\r
        uint8_t bt, ks3x[8], par[8][8];\r
        uint64_t key_recovered;\r
-       static uint64_t *keylist;\r
-       rr = 0;\r
+       uint64_t *keylist;\r
 \r
        // Reset the last three significant bits of the reader nonce\r
        nr &= 0xffffff1f;\r
@@ -92,7 +91,7 @@ static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_i
                }\r
        }\r
 \r
-       states = lfsr_common_prefix(nr, rr, ks3x, par, (par_info == 0));\r
+       states = lfsr_common_prefix(nr, ar, ks3x, par, (par_info == 0));\r
 \r
        if (states == NULL) {\r
                *keys = NULL;\r
@@ -116,7 +115,7 @@ static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_i
 int mfDarkside(uint64_t *key)\r
 {\r
        uint32_t uid = 0;\r
-       uint32_t nt = 0, nr = 0;\r
+       uint32_t nt = 0, nr = 0, ar = 0;\r
        uint64_t par_list = 0, ks_list = 0;\r
        uint64_t *keylist = NULL, *last_keylist = NULL;\r
        uint32_t keycount = 0;\r
@@ -159,18 +158,18 @@ int mfDarkside(uint64_t *key)
                                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
+                               nr = (uint32_t)bytes_to_num(resp.d.asBytes + 24, 4);\r
+                               ar = (uint32_t)bytes_to_num(resp.d.asBytes + 28, 4);\r
                                break;\r
                        }\r
                }\r
 \r
                if (par_list == 0 && c.arg[0] == true) {\r
                        PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");\r
-                       PrintAndLog("Attack will take a few seconds longer because we need two consecutive successful runs.");\r
                }\r
                c.arg[0] = false;\r
 \r
-               keycount = nonce2key(uid, nt, nr, par_list, ks_list, &keylist);\r
+               keycount = nonce2key(uid, nt, nr, ar, par_list, ks_list, &keylist);\r
 \r
                if (keycount == 0) {\r
                        PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);\r
@@ -178,12 +177,14 @@ int mfDarkside(uint64_t *key)
                        continue;\r
                }\r
 \r
-               qsort(keylist, keycount, sizeof(*keylist), compare_uint64);\r
-               keycount = intersection(last_keylist, keylist);\r
-               if (keycount == 0) {\r
-                       free(last_keylist);\r
-                       last_keylist = keylist;\r
-                       continue;\r
+               if (par_list == 0) {\r
+                       qsort(keylist, keycount, sizeof(*keylist), compare_uint64);\r
+                       keycount = intersection(last_keylist, keylist);\r
+                       if (keycount == 0) {\r
+                               free(last_keylist);\r
+                               last_keylist = keylist;\r
+                               continue;\r
+                       }\r
                }\r
 \r
                if (keycount > 1) {\r
@@ -198,10 +199,10 @@ int mfDarkside(uint64_t *key)
                for (int i = 0; i < keycount; i += max_keys) {\r
                        int size = keycount - i > max_keys ? max_keys : keycount - i;\r
                        for (int j = 0; j < size; j++) {\r
-                               if (last_keylist == NULL) {\r
-                                       num_to_bytes(keylist[i*max_keys + j], 6, keyBlock);\r
+                               if (par_list == 0) {\r
+                                       num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock+(j*6));\r
                                } else {\r
-                                       num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock);\r
+                                       num_to_bytes(keylist[i*max_keys + j], 6, keyBlock+(j*6));\r
                                }\r
                        }\r
                        if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) {\r
@@ -228,17 +229,46 @@ int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t key
 \r
        *key = -1;\r
 \r
-       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r
+       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), clear_trace, keycnt}}; \r
        memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
        SendCommand(&c);\r
 \r
        UsbCommand resp;\r
-       if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r
+       if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1; \r
        if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
        *key = bytes_to_num(resp.d.asBytes, 6);\r
        return 0;\r
 }\r
 \r
+int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){\r
+\r
+       uint8_t keyPtr = 0;\r
+\r
+       if (e_sector == NULL)\r
+               return -1;\r
+\r
+       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}}; \r
+       memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
+       SendCommand(&c);\r
+\r
+       UsbCommand resp;\r
+       if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth\r
+       if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
+       \r
+       bool foundAKey = false;\r
+       for(int sec = 0; sec < sectorCnt; sec++){\r
+               for(int keyAB = 0; keyAB < 2; keyAB++){\r
+                       keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);\r
+                       if (keyPtr){\r
+                               e_sector[sec].foundKey[keyAB] = true;\r
+                               e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);\r
+                               foundAKey = true;\r
+                       }\r
+               }\r
+       }\r
+       return foundAKey ? 0 : 3;\r
+}\r
+\r
 // Compare 16 Bits out of cryptostate\r
 int Compare16Bits(const void * a, const void * b) {\r
        if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r
@@ -266,7 +296,13 @@ typedef
 \r
 \r
 // wrapper function for multi-threaded lfsr_recovery32\r
-void* nested_worker_thread(void *arg)\r
+void\r
+#ifdef __has_attribute\r
+#if __has_attribute(force_align_arg_pointer)\r
+__attribute__((force_align_arg_pointer)) \r
+#endif\r
+#endif\r
+*nested_worker_thread(void *arg)\r
 {\r
        struct Crypto1State *p1;\r
        StateList_t *statelist = arg;\r
@@ -280,6 +316,7 @@ void* nested_worker_thread(void *arg)
        return statelist->head.slhead;\r
 }\r
 \r
+\r
 int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate)\r
 {\r
        uint16_t i;\r
@@ -511,36 +548,12 @@ int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) {
        return 0;\r
 }\r
 \r
-int mfCIdentify()\r
-{\r
-       UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};\r
+int mfCIdentify() {\r
+       UsbCommand c = {CMD_MIFARE_CIDENT, {0, 0, 0}};\r
        SendCommand(&c);\r
-\r
        UsbCommand resp;\r
        WaitForResponse(CMD_ACK,&resp);\r
 \r
-       iso14a_card_select_t card;\r
-       memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));\r
-\r
-       uint64_t select_status = resp.arg[0];           // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision\r
-\r
-       if(select_status != 0) {\r
-               uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0\r
-               c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;\r
-               c.arg[1] = 2;\r
-               c.arg[2] = 0;\r
-               memcpy(c.d.asBytes, rats, 2);\r
-               SendCommand(&c);\r
-               WaitForResponse(CMD_ACK,&resp);\r
-       }\r
-\r
-       c.cmd = CMD_MIFARE_CIDENT;\r
-       c.arg[0] = 0;\r
-       c.arg[1] = 0;\r
-       c.arg[2] = 0;\r
-       SendCommand(&c);\r
-       WaitForResponse(CMD_ACK,&resp);\r
-\r
        uint8_t isGeneration = resp.arg[0] & 0xff;\r
        switch( isGeneration ){\r
                case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break;\r
@@ -548,13 +561,6 @@ int mfCIdentify()
                default: PrintAndLog("No chinese magic backdoor command detected"); break;\r
        }\r
 \r
-       // disconnect\r
-       c.cmd = CMD_READER_ISO_14443a;\r
-       c.arg[0] = 0;\r
-       c.arg[1] = 0;\r
-       c.arg[2] = 0;\r
-       SendCommand(&c);\r
-\r
        return (int) isGeneration;\r
 }\r
 \r
@@ -611,7 +617,8 @@ int saveTraceCard(void) {
        for (int i = 0; i < 64; i++) {  // blocks\r
                for (int j = 0; j < 16; j++)  // bytes\r
                        fprintf(f, "%02x", *(traceCard + i * 16 + j));\r
-               fprintf(f,"\n");\r
+               if (i < 63)\r
+                       fprintf(f,"\n");\r
        }\r
        fclose(f);\r
        return 0;\r
@@ -828,20 +835,30 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
                if (len ==4) {\r
                        traceState = TRACE_IDLE;\r
 \r
-                       at_enc = bytes_to_num(data, 4);\r
-\r
-                       //  decode key here)\r
-                       ks2 = ar_enc ^ prng_successor(nt, 64);\r
-                       ks3 = at_enc ^ prng_successor(nt, 96);\r
-                       revstate = lfsr_recovery64(ks2, ks3);\r
-                       lfsr_rollback_word(revstate, 0, 0);\r
-                       lfsr_rollback_word(revstate, 0, 0);\r
-                       lfsr_rollback_word(revstate, nr_enc, 1);\r
-                       lfsr_rollback_word(revstate, uid ^ nt, 0);\r
+                       if (!traceCrypto1) {\r
+                               at_enc = bytes_to_num(data, 4);\r
+\r
+                               //  decode key here)\r
+                               ks2 = ar_enc ^ prng_successor(nt, 64);\r
+                               ks3 = at_enc ^ prng_successor(nt, 96);\r
+                               revstate = lfsr_recovery64(ks2, ks3);\r
+                               lfsr_rollback_word(revstate, 0, 0);\r
+                               lfsr_rollback_word(revstate, 0, 0);\r
+                               lfsr_rollback_word(revstate, nr_enc, 1);\r
+                               lfsr_rollback_word(revstate, uid ^ nt, 0);\r
+\r
+                               crypto1_get_lfsr(revstate, &lfsr);\r
+                               printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
+                               AddLogUint64(logHexFileName, "key> ", lfsr);\r
+                       } else {\r
+                               printf("key> nested not implemented!\n");\r
+                               at_enc = bytes_to_num(data, 4);\r
+                               \r
+                               crypto1_destroy(traceCrypto1);\r
 \r
-                       crypto1_get_lfsr(revstate, &lfsr);\r
-                       printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
-                       AddLogUint64(logHexFileName, "key> ", lfsr);\r
+                               // not implemented\r
+                               traceState = TRACE_ERROR;\r
+                       }\r
 \r
                        int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
                        if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
@@ -859,15 +876,6 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
 \r
                        // set cryptosystem state\r
                        traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
-\r
-//     nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
-\r
-       /*      traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r
-               crypto1_word(traceCrypto1, nt ^ uid, 0);\r
-               crypto1_word(traceCrypto1, ar, 1);\r
-               crypto1_word(traceCrypto1, 0, 0);\r
-               crypto1_word(traceCrypto1, 0, 0);*/\r
-\r
                        return 0;\r
                } else {\r
                        traceState = TRACE_ERROR;\r
@@ -905,3 +913,72 @@ int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data,
        return 0;\r
 }\r
 \r
+/** validate_prng_nonce\r
+ * Determine if nonce is deterministic. ie: Suspectable to Darkside attack.\r
+ * returns\r
+ *   true = weak prng\r
+ *   false = hardend prng\r
+ */\r
+bool validate_prng_nonce(uint32_t nonce) {\r
+       uint16_t *dist = 0;\r
+       uint16_t x, i;\r
+\r
+       dist = malloc(2 << 16);\r
+       if(!dist)\r
+               return -1;\r
+\r
+       // init prng table:\r
+       for (x = i = 1; i; ++i) {\r
+               dist[(x & 0xff) << 8 | x >> 8] = i;\r
+               x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15;\r
+       }\r
+       \r
+       uint32_t res = (65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535;\r
+       \r
+       free(dist);     \r
+       return (res == 16);\r
+}\r
+\r
+/* Detect Tag Prng, \r
+* function performs a partial AUTH,  where it tries to authenticate against block0, key A, but only collects tag nonce.\r
+* the tag nonce is check to see if it has a predictable PRNG.\r
+* @returns \r
+*      TRUE if tag uses WEAK prng (ie Now the NACK bug also needs to be present for Darkside attack)\r
+*   FALSE is tag uses HARDEND prng (ie hardnested attack possible, with known key)\r
+*/\r
+int DetectClassicPrng(void){\r
+\r
+       UsbCommand resp, respA; \r
+       uint8_t cmd[] = {0x60, 0x00}; // MIFARE_AUTH_KEYA\r
+       uint32_t flags = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_RATS;\r
+       \r
+       UsbCommand c = {CMD_READER_ISO_14443a, {flags, sizeof(cmd), 0}};\r
+       memcpy(c.d.asBytes, cmd, sizeof(cmd));\r
+\r
+       clearCommandBuffer();\r
+       SendCommand(&c);\r
+       if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {\r
+        PrintAndLog("PRNG UID: Reply timeout.");\r
+               return -1;\r
+       }\r
+       \r
+       // if select tag failed.\r
+       if (resp.arg[0] == 0) {\r
+               PrintAndLog("PRNG error: selecting tag failed, can't detect prng.");\r
+               return -1;\r
+       }\r
+       \r
+       if (!WaitForResponseTimeout(CMD_ACK, &respA, 5000)) {\r
+        PrintAndLog("PRNG data: Reply timeout.");\r
+               return -1;\r
+       }\r
+\r
+       // check respA\r
+       if (respA.arg[0] != 4) {\r
+               PrintAndLog("PRNG data error: Wrong length: %d", respA.arg[0]);\r
+               return -1;\r
+       }\r
+\r
+       uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]);\r
+       return validate_prng_nonce(nonce);\r
+}\r
Impressum, Datenschutz