]> cvs.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
update travis config (#662)
[proxmark3-svn] / client / cmdhfmf.c
index 553803c1ef371f2d6bbcb069733bd785b67b0406..e2a4ba1e2f09da71decceb0f82ad37f8857655a4 100644 (file)
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <ctype.h>\r
-#include "proxmark3.h"\r
+#include "comms.h"\r
 #include "cmdmain.h"\r
 #include "cmdhfmfhard.h"\r
+#include "parity.h"\r
 #include "util.h"\r
 #include "util_posix.h"\r
 #include "usb_cmd.h"\r
@@ -25,6 +26,7 @@
 #include "mifarehost.h"\r
 #include "mifare.h"\r
 #include "mfkey.h"\r
+#include "hardnested/hardnested_bf_core.h"\r
 \r
 #define NESTED_SECTOR_RETRY     10                     // how often we try mfested() until we give up\r
 \r
@@ -527,13 +529,12 @@ int CmdHF14AMfRestore(const char *Cmd)
 //----------------------------------------------\r
 //   Nested\r
 //----------------------------------------------\r
-# define NESTED_KEY_COUNT 15\r
 \r
 static void parseParamTDS(const char *Cmd, const uint8_t indx, bool *paramT, bool *paramD, uint8_t *timeout) {\r
        char ctmp3[3] = {0};\r
        int len = param_getlength(Cmd, indx);\r
        if (len > 0 && len < 4){\r
-               param_getstr(Cmd, indx, ctmp3);\r
+               param_getstr(Cmd, indx, ctmp3, sizeof(ctmp3));\r
                \r
                *paramT |= (ctmp3[0] == 't' || ctmp3[0] == 'T');\r
                *paramD |= (ctmp3[0] == 'd' || ctmp3[0] == 'D');\r
@@ -563,7 +564,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[NESTED_KEY_COUNT * 6];\r
+       uint8_t keyBlock[MifareDefaultKeysSize * 6];\r
        uint64_t key64 = 0;\r
        // timeout in units. (ms * 106)/10 or us*0.0106\r
        uint8_t btimeout14a = MF_CHKKEYS_DEFTIMEOUT; // fast by default\r
@@ -683,9 +684,9 @@ int CmdHF14AMfNested(const char *Cmd)
                        if (transferToEml) {\r
                                uint8_t sectortrailer;\r
                                if (trgBlockNo < 32*4) {        // 4 block sector\r
-                                       sectortrailer = (trgBlockNo & 0x03) + 3;\r
+                                       sectortrailer = trgBlockNo | 0x03;\r
                                } else {                                        // 16 block sector\r
-                                       sectortrailer = (trgBlockNo & 0x0f) + 15;\r
+                                       sectortrailer = trgBlockNo | 0x0f;\r
                                }\r
                                mfEmlGetMem(keyBlock, sectortrailer, 1);\r
 \r
@@ -713,7 +714,7 @@ int CmdHF14AMfNested(const char *Cmd)
                }\r
 \r
                PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
-               mfCheckKeysSec(SectorsCnt, 2, btimeout14a, true, NESTED_KEY_COUNT, keyBlock, e_sector);\r
+               mfCheckKeysSec(SectorsCnt, 2, btimeout14a, true, MifareDefaultKeysSize, keyBlock, e_sector);\r
                \r
                // get known key from array\r
                bool keyFound = false;\r
@@ -725,7 +726,6 @@ int CmdHF14AMfNested(const char *Cmd)
                                                blockNo = i * 4;\r
                                                keyType = j;\r
                                                num_to_bytes(e_sector[i].Key[j], 6, key);\r
-                                               \r
                                                keyFound = true;\r
                                                break;\r
                                        }\r
@@ -736,6 +736,7 @@ int CmdHF14AMfNested(const char *Cmd)
                        // Can't found a key....\r
                        if (!keyFound) {\r
                                PrintAndLog("Can't found any of the known keys.");\r
+                               free(e_sector);\r
                                return 4;\r
                        }\r
                        PrintAndLog("--auto key. block no:%3d, key type:%c key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
@@ -863,6 +864,13 @@ int CmdHF14AMfNestedHard(const char *Cmd)
                PrintAndLog("      w: Acquire nonces and write them to binary file nonces.bin");\r
                PrintAndLog("      s: Slower acquisition (required by some non standard cards)");\r
                PrintAndLog("      r: Read nonces.bin and start attack");\r
+               PrintAndLog("      iX: set type of SIMD instructions. Without this flag programs autodetect it.");\r
+               PrintAndLog("        i5: AVX512");\r
+               PrintAndLog("        i2: AVX2");\r
+               PrintAndLog("        ia: AVX");\r
+               PrintAndLog("        is: SSE2");\r
+               PrintAndLog("        im: MMX");\r
+               PrintAndLog("        in: none (use CPU regular instruction set)");\r
                PrintAndLog(" ");\r
                PrintAndLog("      sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");\r
                PrintAndLog("      sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");\r
@@ -881,15 +889,20 @@ int CmdHF14AMfNestedHard(const char *Cmd)
        int tests = 0;\r
 \r
 \r
+       uint16_t iindx = 0;\r
        if (ctmp == 'R' || ctmp == 'r') {\r
                nonce_file_read = true;\r
+               iindx = 1;\r
                if (!param_gethex(Cmd, 1, trgkey, 12)) {\r
                        know_target_key = true;\r
+                       iindx = 2;\r
                }\r
        } else if (ctmp == 'T' || ctmp == 't') {\r
                tests = param_get32ex(Cmd, 1, 100, 10);\r
+               iindx = 2;\r
                if (!param_gethex(Cmd, 2, trgkey, 12)) {\r
                        know_target_key = true;\r
+                       iindx = 3;\r
                }\r
        } else {\r
                blockNo = param_get8(Cmd, 0);\r
@@ -923,19 +936,54 @@ int CmdHF14AMfNestedHard(const char *Cmd)
                        know_target_key = true;\r
                        i++;\r
                }\r
+               iindx = i;\r
 \r
                while ((ctmp = param_getchar(Cmd, i))) {\r
                        if (ctmp == 's' || ctmp == 'S') {\r
                                slow = true;\r
                        } else if (ctmp == 'w' || ctmp == 'W') {\r
                                nonce_file_write = true;\r
+                       } else if (param_getlength(Cmd, i) == 2 && ctmp == 'i') {\r
+                               iindx = i;\r
                        } else {\r
-                               PrintAndLog("Possible options are w and/or s");\r
+                               PrintAndLog("Possible options are w , s and/or iX");\r
                                return 1;\r
                        }\r
                        i++;\r
                }\r
        }\r
+       \r
+       SetSIMDInstr(SIMD_AUTO);\r
+       if (iindx > 0) {\r
+               while ((ctmp = param_getchar(Cmd, iindx))) {\r
+                       if (param_getlength(Cmd, iindx) == 2 && ctmp == 'i') {\r
+                               switch(param_getchar_indx(Cmd, 1, iindx)) {\r
+                                       case '5':\r
+                                               SetSIMDInstr(SIMD_AVX512);\r
+                                               break;\r
+                                       case '2':\r
+                                               SetSIMDInstr(SIMD_AVX2);\r
+                                               break;\r
+                                       case 'a':\r
+                                               SetSIMDInstr(SIMD_AVX);\r
+                                               break;\r
+                                       case 's':\r
+                                               SetSIMDInstr(SIMD_SSE2);\r
+                                               break;\r
+                                       case 'm':\r
+                                               SetSIMDInstr(SIMD_MMX);\r
+                                               break;\r
+                                       case 'n':\r
+                                               SetSIMDInstr(SIMD_NONE);\r
+                                               break;\r
+                                       default:\r
+                                               PrintAndLog("Unknown SIMD type. %c", param_getchar_indx(Cmd, 1, iindx));\r
+                                               return 1;\r
+                               }\r
+                       }\r
+                       iindx++;\r
+               }       \r
+       }\r
 \r
        PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d ",\r
                        trgBlockNo,\r
@@ -988,6 +1036,7 @@ int CmdHF14AMfChk(const char *Cmd)
        int i, res;\r
        int     keycnt = 0;\r
        char ctmp       = 0x00;\r
+       int clen = 0;\r
        char ctmp3[3]   = {0x00};\r
        uint8_t blockNo = 0;\r
        uint8_t SectorsCnt = 0;\r
@@ -1016,34 +1065,38 @@ int CmdHF14AMfChk(const char *Cmd)
                blockNo = param_get8(Cmd, 0);\r
 \r
        ctmp = param_getchar(Cmd, 1);\r
-       switch (ctmp) {\r
-       case 'a': case 'A':\r
-               keyType = 0;\r
-               break;\r
-       case 'b': case 'B':\r
-               keyType = 1;\r
-               break;\r
-       case '?':\r
-               keyType = 2;\r
-               break;\r
-       default:\r
-               PrintAndLog("Key type must be A , B or ?");\r
-               free(keyBlock);\r
-               return 1;\r
-       };\r
+       clen = param_getlength(Cmd, 1);\r
+       if (clen == 1) {\r
+               switch (ctmp) {\r
+               case 'a': case 'A':\r
+                       keyType = 0;\r
+                       break;\r
+               case 'b': case 'B':\r
+                       keyType = 1;\r
+                       break;\r
+               case '?':\r
+                       keyType = 2;\r
+                       break;\r
+               default:\r
+                       PrintAndLog("Key type must be A , B or ?");\r
+                       free(keyBlock);\r
+                       return 1;\r
+               };\r
+       }\r
 \r
        // transfer to emulator & create dump file\r
        ctmp = param_getchar(Cmd, 2);\r
-       if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
-       if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
+       clen = param_getlength(Cmd, 2);\r
+       if (clen == 1 && (ctmp == 't' || ctmp == 'T')) transferToEml = 1;\r
+       if (clen == 1 && (ctmp == 'd' || ctmp == 'D')) createDumpFile = 1;\r
        \r
        param3InUse = transferToEml | createDumpFile;\r
        \r
        timeout14a = 500; // fast by default\r
        // double parameters - ts, ds\r
-       int clen = param_getlength(Cmd, 2);\r
+       clen = param_getlength(Cmd, 2);\r
        if (clen == 2 || clen == 3){\r
-               param_getstr(Cmd, 2, ctmp3);\r
+               param_getstr(Cmd, 2, ctmp3, sizeof(ctmp3));\r
                ctmp = ctmp3[1];\r
        }\r
        //parse\r
@@ -1075,7 +1128,7 @@ int CmdHF14AMfChk(const char *Cmd)
                        keycnt++;\r
                } else {\r
                        // May be a dic file\r
-                       if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {\r
+                       if ( param_getstr(Cmd, 2 + i, filename, sizeof(filename)) >= FILE_PATH_SIZE ) {\r
                                PrintAndLog("File name too long");\r
                                free(keyBlock);\r
                                return 2;\r
@@ -1090,7 +1143,7 @@ int CmdHF14AMfChk(const char *Cmd)
 \r
                                        if( buf[0]=='#' ) continue;     //The line start with # is comment, skip\r
 \r
-                                       if (!isxdigit(buf[0])){\r
+                                       if (!isxdigit((unsigned char)buf[0])){\r
                                                PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
                                                continue;\r
                                        }\r
@@ -1134,7 +1187,10 @@ int CmdHF14AMfChk(const char *Cmd)
 \r
        // initialize storage for found keys\r
        e_sector = calloc(SectorsCnt, sizeof(sector_t));\r
-       if (e_sector == NULL) return 1;\r
+       if (e_sector == NULL) {\r
+               free(keyBlock);\r
+               return 1;\r
+       }\r
        for (uint8_t keyAB = 0; keyAB < 2; keyAB++) {\r
                for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
                        e_sector[sectorNo].Key[keyAB] = 0xffffffffffff;\r
@@ -1398,7 +1454,7 @@ int CmdHF14AMf1kSim(const char *Cmd) {
                        break;\r
                case 'f':\r
                case 'F':\r
-                       len = param_getstr(Cmd, cmdp+1, filename);\r
+                       len = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));\r
                        if (len < 1) {\r
                                PrintAndLog("error no filename found");\r
                                return 0;\r
@@ -1633,10 +1689,7 @@ int CmdHF14AMfESet(const char *Cmd)
        }\r
 \r
        //  1 - blocks count\r
-       UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
-       memcpy(c.d.asBytes, memBlock, 16);\r
-       SendCommand(&c);\r
-       return 0;\r
+       return mfEmlSetMem(memBlock, blockNo, 1);\r
 }\r
 \r
 \r
@@ -1674,7 +1727,7 @@ int CmdHF14AMfELoad(const char *Cmd)
                }\r
        }\r
 \r
-       len = param_getstr(Cmd,nameParamNo,filename);\r
+       len = param_getstr(Cmd,nameParamNo,filename,sizeof(filename));\r
 \r
        if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
 \r
@@ -1773,7 +1826,7 @@ int CmdHF14AMfESave(const char *Cmd)
                }\r
        }\r
 \r
-       len = param_getstr(Cmd,nameParamNo,filename);\r
+       len = param_getstr(Cmd,nameParamNo,filename,sizeof(filename));\r
 \r
        if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
 \r
@@ -1855,7 +1908,7 @@ int CmdHF14AMfECFill(const char *Cmd)
                default:   numSectors = 16;\r
        }\r
 \r
-       printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r
+       printf("--params: numSectors: %d, keyType:%d\n", numSectors, keyType);\r
        UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r
        SendCommand(&c);\r
        return 0;\r
@@ -1986,8 +2039,8 @@ int CmdHF14AMfCWipe(const char *Cmd)
        bool fillCard = false;\r
        \r
        if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
-               PrintAndLog("Usage:  hf mf cwipe [card size] [w] [p]");\r
-               PrintAndLog("sample:  hf mf cwipe 1 w s");\r
+               PrintAndLog("Usage:  hf mf cwipe [card size] [w] [f]");\r
+               PrintAndLog("sample:  hf mf cwipe 1 w f");\r
                PrintAndLog("[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
                PrintAndLog("w - Wipe magic Chinese card (only works with gen:1a cards)");\r
                PrintAndLog("f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)");\r
@@ -2137,7 +2190,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
                }\r
                return 0;\r
        } else {\r
-               param_getstr(Cmd, 0, filename);\r
+               param_getstr(Cmd, 0, filename, sizeof(filename));\r
 \r
                len = strlen(filename);\r
                if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
@@ -2348,7 +2401,7 @@ int CmdHF14AMfCSave(const char *Cmd) {
                }\r
                return 0;\r
        } else {\r
-               param_getstr(Cmd, 0, filename);\r
+               param_getstr(Cmd, 0, filename, sizeof(filename));\r
 \r
                len = strlen(filename);\r
                if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
@@ -2421,6 +2474,7 @@ int CmdHF14AMfSniff(const char *Cmd){
        //var\r
        int res = 0;\r
        int len = 0;\r
+       int parlen = 0;\r
        int blockLen = 0;\r
        int pckNum = 0;\r
        int num = 0;\r
@@ -2432,6 +2486,7 @@ int CmdHF14AMfSniff(const char *Cmd){
        uint8_t *buf = NULL;\r
        uint16_t bufsize = 0;\r
        uint8_t *bufPtr = NULL;\r
+       uint8_t parity[16];\r
 \r
        char ctmp = param_getchar(Cmd, 0);\r
        if ( ctmp == 'h' || ctmp == 'H' ) {\r
@@ -2475,14 +2530,13 @@ int CmdHF14AMfSniff(const char *Cmd){
                }\r
 \r
                UsbCommand resp;\r
-               if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
+               if (WaitForResponseTimeoutW(CMD_ACK, &resp, 2000, false)) {\r
                        res = resp.arg[0] & 0xff;\r
                        uint16_t traceLen = resp.arg[1];\r
                        len = resp.arg[2];\r
 \r
                        if (res == 0) {                                                         // we are done\r
-                               free(buf);\r
-                               return 0;\r
+                               break;\r
                        }\r
 \r
                        if (res == 1) {                                                         // there is (more) data to be transferred\r
@@ -2524,6 +2578,7 @@ int CmdHF14AMfSniff(const char *Cmd){
                                        } else {\r
                                                isTag = false;\r
                                        }\r
+                                       parlen = (len - 1) / 8 + 1;\r
                                        bufPtr += 2;\r
                                        if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
                                                memcpy(uid, bufPtr + 2, 7);\r
@@ -2542,15 +2597,22 @@ int CmdHF14AMfSniff(const char *Cmd){
                                                if (wantDecrypt)\r
                                                        mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
                                        } else {\r
-                                               PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
+                                               oddparitybuf(bufPtr, len, parity);\r
+                                               PrintAndLog("%s(%d):%s [%s] c[%s]%c", \r
+                                                       isTag ? "TAG":"RDR", \r
+                                                       num, \r
+                                                       sprint_hex(bufPtr, len), \r
+                                                       printBitsPar(bufPtr + len, len), \r
+                                                       printBitsPar(parity, len),\r
+                                                       memcmp(bufPtr + len, parity, len / 8 + 1) ? '!' : ' ');\r
                                                if (wantLogToFile)\r
                                                        AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
                                                if (wantDecrypt)\r
-                                                       mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
+                                                       mfTraceDecode(bufPtr, len, bufPtr[len], wantSaveToEmlFile);\r
                                                num++;\r
                                        }\r
                                        bufPtr += len;\r
-                                       bufPtr += ((len-1)/8+1);        // ignore parity\r
+                                       bufPtr += parlen;       // ignore parity\r
                                }\r
                                pckNum = 0;\r
                        }\r
@@ -2558,6 +2620,9 @@ int CmdHF14AMfSniff(const char *Cmd){
        } // while (true)\r
 \r
        free(buf);\r
+       \r
+       msleep(300); // wait for exiting arm side.\r
+       PrintAndLog("Done.");\r
        return 0;\r
 }\r
 \r
@@ -2604,11 +2669,9 @@ static command_t CommandTable[] =
 \r
 int CmdHFMF(const char *Cmd)\r
 {\r
-       // flush\r
-       WaitForResponseTimeout(CMD_ACK,NULL,100);\r
-\r
-  CmdsParse(CommandTable, Cmd);\r
-  return 0;\r
+       (void)WaitForResponseTimeout(CMD_ACK,NULL,100);\r
+       CmdsParse(CommandTable, Cmd);\r
+       return 0;\r
 }\r
 \r
 int CmdHelp(const char *Cmd)\r
Impressum, Datenschutz