]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
CHG: removed unused code.
[proxmark3-svn] / client / cmdhfmf.c
index 24d04dc2de934753a56a286f7c27e2d63519ef00..72cbf8f76780e11403794c4a00aa1e3e5c2d6613 100644 (file)
@@ -96,8 +96,7 @@ int CmdHF14AMfWrBl(const char *Cmd)
        uint8_t blockNo = 0;\r
        uint8_t keyType = 0;\r
        uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
-       uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
-       \r
+       uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  \r
        char cmdp       = 0x00;\r
 \r
        if (strlen(Cmd)<3) {\r
@@ -124,10 +123,10 @@ int CmdHF14AMfWrBl(const char *Cmd)
        PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
        PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r
        \r
-  UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r
+       UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r
        memcpy(c.d.asBytes, key, 6);\r
        memcpy(c.d.asBytes + 10, bldata, 16);\r
-  SendCommand(&c);\r
+       SendCommand(&c);\r
 \r
        UsbCommand resp;\r
        if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
@@ -144,11 +143,9 @@ int CmdHF14AMfRdBl(const char *Cmd)
 {\r
        uint8_t blockNo = 0;\r
        uint8_t keyType = 0;\r
-       uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
-       \r
+       uint8_t key[6] = {0, 0, 0, 0, 0, 0};    \r
        char cmdp       = 0x00;\r
 \r
-\r
        if (strlen(Cmd)<3) {\r
                PrintAndLog("Usage:  hf mf rdbl    <block number> <key A/B> <key (12 hex symbols)>");\r
                PrintAndLog("        sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r
@@ -168,9 +165,9 @@ int CmdHF14AMfRdBl(const char *Cmd)
        }\r
        PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
        \r
-  UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r
+       UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r
        memcpy(c.d.asBytes, key, 6);\r
-  SendCommand(&c);\r
+       SendCommand(&c);\r
 \r
        UsbCommand resp;\r
        if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
@@ -432,13 +429,13 @@ int CmdHF14AMfDump(const char *Cmd)
 \r
 int CmdHF14AMfRestore(const char *Cmd)\r
 {\r
-       uint8_t sectorNo,blockNo;\r
+       uint8_t sectorNo,blockNo = 0;\r
        uint8_t keyType = 0;\r
-       uint8_t key[6] = {0xFF};\r
+       uint8_t key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};\r
        uint8_t bldata[16] = {0x00};\r
        uint8_t keyA[40][6];\r
        uint8_t keyB[40][6];\r
-       uint8_t numSectors;\r
+       uint8_t numSectors = 0;\r
        \r
        FILE *fdump;\r
        FILE *fkeys;\r
@@ -550,12 +547,10 @@ int CmdHF14AMfNested(const char *Cmd)
        uint8_t keyBlock[13*6];\r
        uint64_t key64 = 0;\r
        bool transferToEml = false;\r
-       \r
        bool createDumpFile = false;\r
        FILE *fkeys;\r
        uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
        uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
-       \r
        char cmdp, ctmp;\r
 \r
        if (strlen(Cmd)<3) {\r
@@ -686,7 +681,6 @@ int CmdHF14AMfNested(const char *Cmd)
                                }\r
                        }\r
                }\r
-               \r
                // nested sectors\r
                iterations = 0;\r
                PrintAndLog("nested...");\r
@@ -791,7 +785,7 @@ int CmdHF14AMfChk(const char *Cmd)
 \r
        FILE * f;\r
        char filename[FILE_PATH_SIZE]={0};\r
-       char buf[13];\r
+       char buf[13]  = {0x00};\r
        uint8_t *keyBlock = NULL, *p;\r
        uint8_t stKeyBlock = 20;\r
        \r
@@ -1023,6 +1017,7 @@ int CmdHF14AMf1kSim(const char *Cmd)
                PrintAndLog("           x    (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r
                PrintAndLog("");\r
                PrintAndLog("           sample: hf mf sim u 0a0a0a0a ");\r
+               PrintAndLog("                 : hf mf sim u 0a0a0a0a i x");\r
                return 0;\r
        }\r
        uint8_t pnr = 0;\r
@@ -1138,11 +1133,9 @@ int CmdHF14AMfEClear(const char *Cmd)
 \r
 int CmdHF14AMfESet(const char *Cmd)\r
 {\r
-       uint8_t memBlock[16];\r
+       uint8_t memBlock[16] = {0x00};\r
        uint8_t blockNo = 0;\r
 \r
-       memset(memBlock, 0x00, sizeof(memBlock));\r
-\r
        if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
                PrintAndLog("Usage:  hf mf eset <block number> <block data (32 hex symbols)>");\r
                PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
@@ -1167,7 +1160,7 @@ int CmdHF14AMfESet(const char *Cmd)
 int CmdHF14AMfELoad(const char *Cmd)\r
 {\r
        FILE * f;\r
-       char filename[FILE_PATH_SIZE];\r
+       char filename[FILE_PATH_SIZE] = {0x00};\r
        char *fnameptr = filename;\r
        char buf[64] = {0x00};\r
        uint8_t buf8[64] = {0x00};\r
@@ -1263,9 +1256,9 @@ int CmdHF14AMfELoad(const char *Cmd)
 int CmdHF14AMfESave(const char *Cmd)\r
 {\r
        FILE * f;\r
-       char filename[FILE_PATH_SIZE];\r
+       char filename[FILE_PATH_SIZE] = {0x00};\r
        char * fnameptr = filename;\r
-       uint8_t buf[64];\r
+       uint8_t buf[64] = {0x00};\r
        int i, j, len, numBlocks;\r
        int nameParamNo = 1;\r
        \r
@@ -1387,8 +1380,8 @@ int CmdHF14AMfEKeyPrn(const char *Cmd)
 {\r
        int i;\r
        uint8_t numSectors;\r
-       uint8_t data[16];\r
-       uint64_t keyA, keyB;\r
+       uint8_t data[16] = {0x00};\r
+       uint64_t keyA, keyB = 0;\r
        \r
        if (param_getchar(Cmd, 0) == 'h') {\r
                PrintAndLog("It prints the keys loaded in the emulator memory");\r
@@ -1433,27 +1426,60 @@ int CmdHF14AMfCSetUID(const char *Cmd)
        uint8_t wipeCard = 0;\r
        uint8_t uid[8] = {0x00};\r
        uint8_t oldUid[8] = {0x00};\r
+       uint8_t atqa[2] = {0x00};\r
+       uint8_t sak[1] = {0x00};\r
+       uint8_t atqaPresent = 1;\r
        int res;\r
-\r
-       if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
-               PrintAndLog("Usage:  hf mf csetuid <UID 8 hex symbols> <w>");\r
-               PrintAndLog("sample:  hf mf csetuid 01020304 w");\r
-               PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r
-               PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
+       char ctmp;\r
+       int argi=0;\r
+\r
+       if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {\r
+               PrintAndLog("Usage:  hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");\r
+               PrintAndLog("sample:  hf mf csetuid 01020304");\r
+               PrintAndLog("sample:  hf mf csetuid 01020304 0004 08 w");\r
+               PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");\r
+               PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");\r
                return 0;\r
-       }       \r
+       }\r
 \r
-       if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r
+       if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {\r
                PrintAndLog("UID must include 8 HEX symbols");\r
                return 1;\r
        }\r
+       argi++;\r
+\r
+       ctmp = param_getchar(Cmd, argi);\r
+       if (ctmp == 'w' || ctmp == 'W') {\r
+               wipeCard = 1;\r
+               atqaPresent = 0;\r
+       }\r
+\r
+       if (atqaPresent) {\r
+               if (param_getchar(Cmd, argi)) {\r
+                       if (param_gethex(Cmd, argi, atqa, 4)) {\r
+                               PrintAndLog("ATQA must include 4 HEX symbols");\r
+                               return 1;\r
+                       }\r
+                       argi++;\r
+                       if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {\r
+                               PrintAndLog("SAK must include 2 HEX symbols");\r
+                               return 1;\r
+                       }\r
+                       argi++;\r
+               } else\r
+                       atqaPresent = 0;\r
+       }\r
+\r
+       if(!wipeCard) {\r
+               ctmp = param_getchar(Cmd, argi);\r
+               if (ctmp == 'w' || ctmp == 'W') {\r
+                       wipeCard = 1;\r
+               }\r
+       }\r
 \r
-       char ctmp = param_getchar(Cmd, 1);\r
-       if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r
-       \r
        PrintAndLog("--wipe card:%s  uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));\r
 \r
-       res = mfCSetUID(uid, oldUid, wipeCard);\r
+       res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);\r
        if (res) {\r
                        PrintAndLog("Can't set UID. error=%d", res);\r
                        return 1;\r
@@ -1466,16 +1492,16 @@ int CmdHF14AMfCSetUID(const char *Cmd)
 \r
 int CmdHF14AMfCSetBlk(const char *Cmd)\r
 {\r
-       uint8_t uid[8] = {0x00};\r
        uint8_t memBlock[16] = {0x00};\r
        uint8_t blockNo = 0;\r
-       int res;\r
+       bool wipeCard = FALSE;\r
+       int res = 0;    \r
 \r
        if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
-               PrintAndLog("Usage:  hf mf csetblk <block number> <block data (32 hex symbols)>");\r
+               PrintAndLog("Usage:  hf mf csetblk <block number> <block data (32 hex symbols)> [w]");\r
                PrintAndLog("sample:  hf mf csetblk 1 01020304050607080910111213141516");\r
-               PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r
-               PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
+               PrintAndLog("Set block data for magic Chinese card (only works with such cards)");\r
+               PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");\r
                return 0;\r
        }       \r
 \r
@@ -1486,14 +1512,16 @@ int CmdHF14AMfCSetBlk(const char *Cmd)
                return 1;\r
        }\r
 \r
+       char ctmp = param_getchar(Cmd, 2);\r
+       wipeCard = (ctmp == 'w' || ctmp == 'W');\r
+\r
        PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\r
 \r
-       res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r
+       res = mfCSetBlock(blockNo, memBlock, NULL, wipeCard, CSETBLOCK_SINGLE_OPER);\r
        if (res) {\r
-                       PrintAndLog("Can't write block. error=%d", res);\r
-                       return 1;\r
-               }\r
-       \r
+               PrintAndLog("Can't write block. error=%d", res);\r
+               return 1;\r
+       }       \r
        return 0;\r
 }\r
 \r
@@ -1506,7 +1534,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
        char buf[64] = {0x00};\r
        uint8_t buf8[64] = {0x00};\r
        uint8_t fillFromEmulator = 0;\r
-       int i, len, blockNum, flags;\r
+       int i, len, blockNum, flags=0;\r
        \r
        if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
                PrintAndLog("It loads magic Chinese card from the file `filename.eml`");\r
@@ -1521,15 +1549,14 @@ int CmdHF14AMfCLoad(const char *Cmd)
        if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
        \r
        if (fillFromEmulator) {\r
-               flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
                for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
                        if (mfEmlGetMem(buf8, blockNum, 1)) {\r
                                PrintAndLog("Cant get block: %d", blockNum);\r
                                return 2;\r
                        }\r
-                       \r
-                       if (blockNum == 2) flags = 0;\r
-                       if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
+                       if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;                               // switch on field and send magic sequence\r
+                       if (blockNum == 1) flags = 0;                                                                                                   // just write\r
+                       if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;             // Done. Magic Halt and switch off field.\r
 \r
                        if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
                                PrintAndLog("Cant set magic card block: %d", blockNum);\r
@@ -1554,7 +1581,6 @@ int CmdHF14AMfCLoad(const char *Cmd)
                }\r
        \r
                blockNum = 0;\r
-               flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
                while(!feof(f)){\r
                \r
                        memset(buf, 0, sizeof(buf));\r
@@ -1564,7 +1590,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
                                return 2;\r
                        }\r
 \r
-                       if (strlen(buf) < 32){\r
+                       if (strlen(buf) < 32) {\r
                                if(strlen(buf) && feof(f))\r
                                        break;\r
                                PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
@@ -1573,8 +1599,9 @@ int CmdHF14AMfCLoad(const char *Cmd)
                        for (i = 0; i < 32; i += 2)\r
                                sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
 \r
-                       if (blockNum == 2) flags = 0;\r
-                       if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
+                       if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;                               // switch on field and send magic sequence\r
+                       if (blockNum == 1) flags = 0;                                                                                                   // just write\r
+                       if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;             // Done. Switch off field.\r
 \r
                        if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
                                PrintAndLog("Can't set magic card block: %d", blockNum);\r
@@ -1597,15 +1624,14 @@ int CmdHF14AMfCLoad(const char *Cmd)
 }\r
 \r
 int CmdHF14AMfCGetBlk(const char *Cmd) {\r
-       uint8_t memBlock[16];\r
+       uint8_t memBlock[16] = {0x00};\r
        uint8_t blockNo = 0;\r
        int res;\r
-       memset(memBlock, 0x00, sizeof(memBlock));\r
 \r
        if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
                PrintAndLog("Usage:  hf mf cgetblk <block number>");\r
                PrintAndLog("sample:  hf mf cgetblk 1");\r
-               PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r
+               PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");\r
                return 0;\r
        }       \r
 \r
@@ -1632,7 +1658,7 @@ int CmdHF14AMfCGetSc(const char *Cmd) {
        if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
                PrintAndLog("Usage:  hf mf cgetsc <sector number>");\r
                PrintAndLog("sample:  hf mf cgetsc 0");\r
-               PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r
+               PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");\r
                return 0;\r
        }       \r
 \r
@@ -1765,15 +1791,16 @@ int CmdHF14AMfSniff(const char *Cmd){
        int res = 0;\r
        int len = 0;\r
        int blockLen = 0;\r
-       int num = 0;\r
        int pckNum = 0;\r
+       int num = 0;\r
        uint8_t uid[7] = {0x00};\r
        uint8_t uid_len;\r
        uint8_t atqa[2] = {0x00};\r
        uint8_t sak;\r
        bool isTag;\r
-       uint8_t buf[3000] = {0x00};\r
-       uint8_t * bufPtr = buf;\r
+       uint8_t *buf = NULL;\r
+       uint16_t bufsize = 0;\r
+       uint8_t *bufPtr = NULL;\r
        \r
        char ctmp = param_getchar(Cmd, 0);\r
        if ( ctmp == 'h' || ctmp == 'H' ) {\r
@@ -1816,32 +1843,47 @@ int CmdHF14AMfSniff(const char *Cmd){
                        break;\r
                }\r
                \r
-    UsbCommand resp;\r
-    if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
+               UsbCommand resp;\r
+               if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
                        res = resp.arg[0] & 0xff;\r
-                       len = resp.arg[1];\r
-                       num = resp.arg[2];\r
-                       \r
-                       if (res == 0) return 0;\r
-                       if (res == 1) {\r
-                               if (num ==0) {\r
+                       uint16_t traceLen = resp.arg[1];\r
+                       len = resp.arg[2];\r
+\r
+                       if (res == 0) return 0;                                         // we are done\r
+\r
+                       if (res == 1) {                                                         // there is (more) data to be transferred\r
+                               if (pckNum == 0) {                                              // first packet, (re)allocate necessary buffer\r
+                                       if (traceLen > bufsize) {\r
+                                               uint8_t *p;\r
+                                               if (buf == NULL) {                              // not yet allocated\r
+                                                       p = malloc(traceLen);\r
+                                               } else {                                                // need more memory\r
+                                                       p = realloc(buf, traceLen);\r
+                                               }\r
+                                               if (p == NULL) {\r
+                                                       PrintAndLog("Cannot allocate memory for trace");\r
+                                                       free(buf);\r
+                                                       return 2;\r
+                                               }\r
+                                               buf = p;\r
+                                       }\r
                                        bufPtr = buf;\r
-                                       memset(buf, 0x00, 3000);\r
+                                       bufsize = traceLen;\r
+                                       memset(buf, 0x00, traceLen);\r
                                }\r
                                memcpy(bufPtr, resp.d.asBytes, len);\r
                                bufPtr += len;\r
                                pckNum++;\r
                        }\r
-                       if (res == 2) {\r
+\r
+                       if (res == 2) {                                                         // received all data, start displaying\r
                                blockLen = bufPtr - buf;\r
                                bufPtr = buf;\r
                                printf(">\n");\r
                                PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
-                               num = 0;\r
                                while (bufPtr - buf < blockLen) {\r
-                                       bufPtr += 6;\r
+                                       bufPtr += 6;                                            // skip (void) timing information\r
                                        len = *((uint16_t *)bufPtr);\r
-\r
                                        if(len & 0x8000) {\r
                                                isTag = true;\r
                                                len &= 0x7fff;\r
@@ -1850,12 +1892,10 @@ int CmdHF14AMfSniff(const char *Cmd){
                                        }\r
                                        bufPtr += 2;\r
                                        if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
-                                       \r
                                                memcpy(uid, bufPtr + 2, 7);\r
                                                memcpy(atqa, bufPtr + 2 + 7, 2);\r
                                                uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
                                                sak = bufPtr[11];\r
-                                               \r
                                                PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
                                                        sprint_hex(uid + (7 - uid_len), uid_len),\r
                                                        atqa[1], \r
@@ -1873,18 +1913,21 @@ int CmdHF14AMfSniff(const char *Cmd){
                                                        AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
                                                if (wantDecrypt) \r
                                                        mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
+                                               num++;  \r
                                        }\r
                                        bufPtr += len;\r
                                        bufPtr += ((len-1)/8+1);        // ignore parity\r
-                                       num++;\r
                                }\r
+                               pckNum = 0;\r
                        }\r
                } // resp not NULL\r
        } // while (true)\r
-       \r
+\r
+       free(buf);\r
        return 0;\r
 }\r
 \r
+\r
 static command_t CommandTable[] =\r
 {\r
   {"help",             CmdHelp,                                1, "This help"},\r
Impressum, Datenschutz