]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
Updated nameing (svn -> git) & fixed whitespaces.
[proxmark3-svn] / client / cmdhfmf.c
index 8d78b0e6f8665daa2fa2625ea5772001427428e4..a26652566e812d353a6a1f739ed4a296902561f6 100644 (file)
@@ -142,76 +142,112 @@ int CmdHF14AMfWrBl(const char *Cmd)
 \r
 int CmdHF14AMfUWrBl(const char *Cmd)\r
 {\r
-        uint8_t blockNo = 0;\r
-        uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
+       uint8_t blockNo = 0;\r
+       bool chinese_card=0;\r
+       uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
        UsbCommand resp;\r
-        \r
-        if (strlen(Cmd)<3) {\r
-                PrintAndLog("Usage:  hf mf uwrbl    <block number> <block data (8 hex symbols)>");\r
-                PrintAndLog("        sample: hf mf uwrbl 0 01020304");\r
-                return 0;\r
-        }       \r
+       \r
+       if (strlen(Cmd)<3) {\r
+               PrintAndLog("Usage:  hf mf uwrbl    <block number> <block data (8 hex symbols)> <w>");\r
+               PrintAndLog("        sample: hf mf uwrbl 0 01020304");\r
+               return 0;\r
+       }      \r
 \r
-        blockNo = param_get8(Cmd, 0);\r
-        if (param_gethex(Cmd, 1, bldata, 8)) {\r
-                PrintAndLog("Block data must include 8 HEX symbols");\r
-                return 1;\r
-        }\r
+       blockNo = param_get8(Cmd, 0);\r
+       if (param_gethex(Cmd, 1, bldata, 8)) {\r
+               PrintAndLog("Block data must include 8 HEX symbols");\r
+               return 1;\r
+       }\r
+       \r
+       if (strchr(Cmd,'w') != 0) {\r
+         chinese_card=1;\r
+       }\r
+       \r
+       switch(blockNo){\r
+               case 0:\r
+                       if (!chinese_card){\r
+                               PrintAndLog("Access Denied");\r
+                       }else{\r
+                               PrintAndLog("--specialblock no:%02x", blockNo);\r
+                               PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r
+                               UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
+                               memcpy(d.d.asBytes,bldata, 4);\r
+                               SendCommand(&d);\r
+\r
+                               if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                                       uint8_t isOK  = resp.arg[0] & 0xff;\r
+                                       PrintAndLog("isOk:%02x", isOK);\r
+                               } else {\r
+                                       PrintAndLog("Command execute timeout");\r
+                             }\r
+                       }\r
+                       break;\r
+               case 1:\r
+                         if (!chinese_card){\r
+                               PrintAndLog("Access Denied");\r
+                         }else{\r
+                               PrintAndLog("--specialblock no:%02x", blockNo);\r
+                               PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r
+                               UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
+                               memcpy(d.d.asBytes,bldata, 4);\r
+                               SendCommand(&d);\r
+\r
+                               if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                               uint8_t isOK  = resp.arg[0] & 0xff;\r
+                               PrintAndLog("isOk:%02x", isOK);\r
+                               } else {\r
+                                       PrintAndLog("Command execute timeout");\r
+                               }\r
+                       }\r
+                       break;\r
+               case 2:\r
+                       if (!chinese_card){\r
+                               PrintAndLog("Access Denied");\r
+                       }else{\r
+                               PrintAndLog("--specialblock no:%02x", blockNo);\r
+                               PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r
+                               UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
+                               memcpy(c.d.asBytes, bldata, 4);\r
+                               SendCommand(&c);\r
 \r
-       switch(blockNo)\r
-       {\r
-       case 0:\r
-               PrintAndLog("Access Denied");\r
-               break;\r
-       case 1:\r
-               PrintAndLog("Access Denied");\r
-               break;\r
-       case 2:\r
-               PrintAndLog("--specialblock no:%02x", blockNo);\r
-                PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r
-                UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
-                memcpy(c.d.asBytes, bldata, 4);\r
-                SendCommand(&c);\r
-\r
-                if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
-                        uint8_t isOK  = resp.arg[0] & 0xff;\r
-                        PrintAndLog("isOk:%02x", isOK);\r
-                } else {\r
-                        PrintAndLog("Command execute timeout");\r
-                }\r
-               break;\r
-       case 3:\r
-               PrintAndLog("--specialblock no:%02x", blockNo);\r
-                PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r
-                UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
-                memcpy(d.d.asBytes,bldata, 4);\r
-                SendCommand(&d);\r
-\r
-                if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
-                        uint8_t isOK  = resp.arg[0] & 0xff;\r
-                        PrintAndLog("isOk:%02x", isOK);\r
-                } else {\r
-                        PrintAndLog("Command execute timeout");\r
-                }\r
-               break;\r
-       default: \r
-               PrintAndLog("--block no:%02x", blockNo);\r
-               PrintAndLog("--data: %s", sprint_hex(bldata, 4));               \r
-               //UsbCommand e = {CMD_MIFAREU_WRITEBL_COMPAT, {blockNo}};\r
-               //memcpy(e.d.asBytes,bldata, 16);\r
-               UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
-                memcpy(e.d.asBytes,bldata, 4);\r
-               SendCommand(&e);\r
-\r
-               if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
-                       uint8_t isOK  = resp.arg[0] & 0xff;\r
-                       PrintAndLog("isOk:%02x", isOK);\r
-               } else {\r
-                       PrintAndLog("Command execute timeout");\r
-               }\r
-               break;\r
+                               if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                                       uint8_t isOK  = resp.arg[0] & 0xff;\r
+                                       PrintAndLog("isOk:%02x", isOK);\r
+                               } else {\r
+                                       PrintAndLog("Command execute timeout");\r
+                               }\r
+                       }\r
+                       break;\r
+               case 3:\r
+                       PrintAndLog("--specialblock no:%02x", blockNo);\r
+                       PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r
+                       UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
+                       memcpy(d.d.asBytes,bldata, 4);\r
+                       SendCommand(&d);\r
+\r
+                       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                               uint8_t isOK  = resp.arg[0] & 0xff;\r
+                               PrintAndLog("isOk:%02x", isOK);\r
+                       } else {\r
+                               PrintAndLog("Command execute timeout");\r
+                       }\r
+                       break;\r
+               default: \r
+                       PrintAndLog("--block no:%02x", blockNo);\r
+                       PrintAndLog("--data: %s", sprint_hex(bldata, 4));               \r
+                       UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};\r
+                       memcpy(e.d.asBytes,bldata, 4);\r
+                       SendCommand(&e);\r
+\r
+                       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                               uint8_t isOK  = resp.arg[0] & 0xff;\r
+                               PrintAndLog("isOk:%02x", isOK);\r
+                       } else {\r
+                               PrintAndLog("Command execute timeout");\r
+                     }\r
+                     break;\r
        }\r
-        return 0;\r
+       return 0;\r
 }\r
 \r
 int CmdHF14AMfRdBl(const char *Cmd)\r
@@ -947,19 +983,28 @@ int CmdHF14AMfChk(const char *Cmd)
        keyBlock = calloc(stKeyBlock, 6);\r
        if (keyBlock == NULL) return 1;\r
 \r
-       num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 0 * 6)); // Default key (first key used by program if no user defined key)\r
-       num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 1 * 6)); // Blank key\r
-       num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 2 * 6)); // NFCForum MAD key\r
-       num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
-       num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 4 * 6));\r
-       num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 5 * 6));\r
-       num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 6 * 6));\r
-       num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 7 * 6));\r
-       num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 8 * 6));\r
-       num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 9 * 6));\r
-       num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 10 * 6));\r
-       num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 11 * 6));\r
-       num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 12 * 6));\r
+       uint64_t defaultKeys[] =\r
+       {\r
+               0xffffffffffff, // Default key (first key used by program if no user defined key)\r
+               0x000000000000, // Blank key\r
+               0xa0a1a2a3a4a5, // NFCForum MAD key\r
+               0xb0b1b2b3b4b5,\r
+               0xaabbccddeeff,\r
+               0x4d3a99c351dd,\r
+               0x1a982c7e459a,\r
+               0xd3f7d3f7d3f7,\r
+               0x714c5c886e97,\r
+               0x587ee5f9350f,\r
+               0xa0478cc39091,\r
+               0x533cb6c723f6,\r
+               0x8fd0a4f256e9\r
+       };\r
+       int defaultKeysSize = (sizeof(defaultKeys) / 7) - 1;\r
+\r
+       for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
+       {\r
+               num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
+       }\r
        \r
        if (strlen(Cmd)<3) {\r
                PrintAndLog("Usage:  hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r
@@ -1029,13 +1074,7 @@ int CmdHF14AMfChk(const char *Cmd)
                        }\r
                        \r
                        if ( (f = fopen( filename , "r")) ) {\r
-                               while( !feof(f) ){\r
-                                       memset(buf, 0, sizeof(buf));\r
-                                       if (fgets(buf, sizeof(buf), f) == NULL) {\r
-                                               PrintAndLog("File reading error.");\r
-                                               return 2;\r
-                                       }\r
-          \r
+                               while( fgets(buf, sizeof(buf), f) ){\r
                                        if (strlen(buf) < 12 || buf[11] == '\n')\r
                                                continue;\r
                                \r
@@ -1063,6 +1102,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                        num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
                                        PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
                                        keycnt++;\r
+                                       memset(buf, 0, sizeof(buf));\r
                                }\r
                        } else {\r
                                PrintAndLog("File: %s: not found or locked.", filename);\r
@@ -1075,7 +1115,7 @@ int CmdHF14AMfChk(const char *Cmd)
        \r
        if (keycnt == 0) {\r
                PrintAndLog("No key specified,try default keys");\r
-               for (;keycnt <=12; keycnt++)\r
+               for (;keycnt < defaultKeysSize; keycnt++)\r
                        PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r
                        (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
                        (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4],     (keyBlock + 6*keycnt)[5], 6);\r
@@ -1843,8 +1883,9 @@ int CmdHF14AMfSniff(const char *Cmd){
        printf("Press the key on pc keyboard to abort the client.\n");\r
        printf("-------------------------------------------------------------------------\n");\r
 \r
-  UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
-  SendCommand(&c);\r
+       UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
+       clearCommandBuffer();\r
+       SendCommand(&c);\r
 \r
        // wait cycle\r
        while (true) {\r
@@ -1891,7 +1932,7 @@ int CmdHF14AMfSniff(const char *Cmd){
                                                sak = bufPtr[11];\r
                                                \r
                                                PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);\r
-                                               if (wantLogToFile) {\r
+                                               if (wantLogToFile || wantDecrypt) {\r
                                                        FillFileNameByUID(logHexFileName, uid, ".log", 7);\r
                                                        AddLogCurrentDT(logHexFileName);\r
                                                }                                               \r
@@ -1907,7 +1948,8 @@ int CmdHF14AMfSniff(const char *Cmd){
                        }\r
                } // resp not NILL\r
        } // while (true)\r
-  return 0;\r
+       \r
+       return 0;\r
 }\r
 \r
 static command_t CommandTable[] =\r
Impressum, Datenschutz