]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/mifarecmd.c
(implementing suggestion #94)
[proxmark3-svn] / armsrc / mifarecmd.c
index 939c90028796ef823e0e0c3d5acfbd841cf4f48f..14d2b68a10ac578121e3a66d590c2733405d2439 100644 (file)
@@ -645,6 +645,9 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
 \r
 \r
        // statistics on nonce distance\r
+       int16_t isOK = 0;\r
+       #define NESTED_MAX_TRIES 12\r
+       uint16_t unsuccessfull_tries = 0;\r
        if (calibrate) {        // for first call only. Otherwise reuse previous calibration\r
                LED_B_ON();\r
                WDT_HIT();\r
@@ -655,6 +658,12 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                \r
                for (rtr = 0; rtr < 17; rtr++) {\r
 \r
+                       // Test if the action was cancelled\r
+                       if(BUTTON_PRESS()) {\r
+                               isOK = -2;\r
+                               break;\r
+                       }\r
+\r
                        // prepare next select. No need to power down the card.\r
                        if(mifare_classic_halt(pcs, cuid)) {\r
                                if (MF_DBGLEVEL >= 1)   Dbprintf("Nested: Halt error");\r
@@ -702,14 +711,17 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
                                        delta_time = auth2_time - auth1_time + 32;  // allow some slack for proper timing\r
                                }\r
                                if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
+                       } else {\r
+                               unsuccessfull_tries++;\r
+                               if (unsuccessfull_tries > NESTED_MAX_TRIES) {   // card isn't vulnerable to nested attack (random numbers are not predictable)\r
+                                       isOK = -3;\r
+                               }\r
                        }\r
                }\r
-               \r
-               if (rtr <= 1)   return;\r
 \r
                davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
                \r
-               if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);\r
+               if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);\r
 \r
                dmin = davg - 2;\r
                dmax = davg + 2;\r
@@ -722,7 +734,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
        LED_C_ON();\r
 \r
        //  get crypted nonces for target sector\r
-       for(i=0; i < 2; i++) { // look for exactly two different nonces\r
+       for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
 \r
                target_nt[i] = 0;\r
                while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
@@ -800,7 +812,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
        memcpy(buf+16, &target_ks[1], 4);\r
        \r
        LED_B_ON();\r
-       cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
+       cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
        LED_B_OFF();\r
 \r
        if (MF_DBGLEVEL >= 3)   DbpString("NESTED FINISHED");\r
Impressum, Datenschutz