]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
Merge pull request #122 from marshmellow42/master
[proxmark3-svn] / client / cmdhfmf.c
index d0852ea5bdf16fd05f69180fcbdd26125d0a8a8b..5abda060d8e4e40ae83f71977e4d864ad39dfaff 100644 (file)
@@ -434,7 +434,7 @@ int CmdHF14AMfRestore(const char *Cmd)
 {\r
        uint8_t sectorNo,blockNo;\r
        uint8_t keyType = 0;\r
 {\r
        uint8_t sectorNo,blockNo;\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 bldata[16] = {0x00};\r
        uint8_t keyA[40][6];\r
        uint8_t keyB[40][6];\r
@@ -547,7 +547,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 trgKeyType = 0;\r
        uint8_t SectorsCnt = 0;\r
        uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
-       uint8_t keyBlock[13*6];\r
+       uint8_t keyBlock[14*6];\r
        uint64_t key64 = 0;\r
        bool transferToEml = false;\r
        \r
        uint64_t key64 = 0;\r
        bool transferToEml = false;\r
        \r
@@ -1200,7 +1200,7 @@ int CmdHF14AMfELoad(const char *Cmd)
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
-       if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
+       if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
 \r
        fnameptr += len;\r
 \r
 \r
        fnameptr += len;\r
 \r
@@ -1299,17 +1299,20 @@ int CmdHF14AMfESave(const char *Cmd)
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
-       if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
+       if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
        \r
        // user supplied filename?\r
        if (len < 1) {\r
                // get filename (UID from memory)\r
                if (mfEmlGetMem(buf, 0, 1)) {\r
                        PrintAndLog("Can\'t get UID from block: %d", 0);\r
        \r
        // user supplied filename?\r
        if (len < 1) {\r
                // get filename (UID from memory)\r
                if (mfEmlGetMem(buf, 0, 1)) {\r
                        PrintAndLog("Can\'t get UID from block: %d", 0);\r
-                       sprintf(filename, "dump.eml"); \r
+                       len = sprintf(fnameptr, "dump");\r
+                       fnameptr += len;\r
+               }\r
+               else {\r
+                       for (j = 0; j < 7; j++, fnameptr += 2)\r
+                               sprintf(fnameptr, "%02X", buf[j]);\r
                }\r
                }\r
-               for (j = 0; j < 7; j++, fnameptr += 2)\r
-                       sprintf(fnameptr, "%02X", buf[j]); \r
        } else {\r
                fnameptr += len;\r
        }\r
        } else {\r
                fnameptr += len;\r
        }\r
@@ -1499,16 +1502,16 @@ int CmdHF14AMfCSetUID(const char *Cmd)
 \r
 int CmdHF14AMfCSetBlk(const char *Cmd)\r
 {\r
 \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
        uint8_t memBlock[16] = {0x00};\r
        uint8_t blockNo = 0;\r
+       bool wipeCard = FALSE;\r
        int res;\r
 \r
        if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
        int res;\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("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 wipe the card then add 'w' at the end of the command line");\r
                return 0;\r
        }       \r
 \r
                return 0;\r
        }       \r
 \r
@@ -1519,14 +1522,15 @@ int CmdHF14AMfCSetBlk(const char *Cmd)
                return 1;\r
        }\r
 \r
                return 1;\r
        }\r
 \r
+       char ctmp = param_getchar(Cmd, 2);\r
+       wipeCard = (ctmp == 'w' || ctmp == 'W');\r
        PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\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
        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
        return 0;\r
 }\r
 \r
@@ -1539,7 +1543,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
        char buf[64] = {0x00};\r
        uint8_t buf8[64] = {0x00};\r
        uint8_t fillFromEmulator = 0;\r
        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
        \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
@@ -1554,15 +1558,14 @@ int CmdHF14AMfCLoad(const char *Cmd)
        if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
        \r
        if (fillFromEmulator) {\r
        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
                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
 \r
                        if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
                                PrintAndLog("Cant set magic card block: %d", blockNum);\r
@@ -1572,7 +1575,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
-               if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
+               if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
 \r
                memcpy(filename, Cmd, len);\r
                fnameptr += len;\r
 \r
                memcpy(filename, Cmd, len);\r
                fnameptr += len;\r
@@ -1587,27 +1590,29 @@ int CmdHF14AMfCLoad(const char *Cmd)
                }\r
        \r
                blockNum = 0;\r
                }\r
        \r
                blockNum = 0;\r
-               flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
                while(!feof(f)){\r
                \r
                        memset(buf, 0, sizeof(buf));\r
                        \r
                        if (fgets(buf, sizeof(buf), f) == NULL) {\r
                while(!feof(f)){\r
                \r
                        memset(buf, 0, sizeof(buf));\r
                        \r
                        if (fgets(buf, sizeof(buf), f) == NULL) {\r
+                               fclose(f);\r
                                PrintAndLog("File reading error.");\r
                                return 2;\r
                        }\r
 \r
                                PrintAndLog("File reading error.");\r
                                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
                                if(strlen(buf) && feof(f))\r
                                        break;\r
                                PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
+                               fclose(f);\r
                                return 2;\r
                        }\r
                        for (i = 0; i < 32; i += 2)\r
                                sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
 \r
                                return 2;\r
                        }\r
                        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
 \r
                        if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
                                PrintAndLog("Can't set magic card block: %d", blockNum);\r
@@ -1638,7 +1643,7 @@ int CmdHF14AMfCGetBlk(const char *Cmd) {
        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
        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
                return 0;\r
        }       \r
 \r
@@ -1665,7 +1670,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
        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
                return 0;\r
        }       \r
 \r
@@ -1739,16 +1744,19 @@ int CmdHF14AMfCSave(const char *Cmd) {
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
-               if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
+               if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
        \r
                if (len < 1) {\r
                        // get filename\r
                        if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
                                PrintAndLog("Cant get block: %d", 0);\r
        \r
                if (len < 1) {\r
                        // get filename\r
                        if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
                                PrintAndLog("Cant get block: %d", 0);\r
-                               return 1;\r
+                               len = sprintf(fnameptr, "dump");\r
+                               fnameptr += len;\r
+                       }\r
+                       else {\r
+                               for (j = 0; j < 7; j++, fnameptr += 2)\r
+                                       sprintf(fnameptr, "%02x", buf[j]); \r
                        }\r
                        }\r
-                       for (j = 0; j < 7; j++, fnameptr += 2)\r
-                               sprintf(fnameptr, "%02x", buf[j]); \r
                } else {\r
                        memcpy(filename, Cmd, len);\r
                        fnameptr += len;\r
                } else {\r
                        memcpy(filename, Cmd, len);\r
                        fnameptr += len;\r
Impressum, Datenschutz