]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdlft55xx.c
minor help text changes
[proxmark3-svn] / client / cmdlft55xx.c
index 04931dfe80a90e0e70f1a11eeac5b1b18017c094..fc587298cfd3588d8dd46595ac48a5615b6f30b7 100644 (file)
 #include "../common/iso14443crc.h"\r
 #include "cmdhf14a.h"\r
 \r
-#define CONFIGURATION_BLOCK 0x00\r
-#define TRACE_BLOCK 0x01\r
+#define T55x7_CONFIGURATION_BLOCK 0x00\r
+#define T55x7_PAGE0 0x00\r
+#define T55x7_PAGE1 0x01\r
+#define T55x7_PWD      0x00000010\r
 #define REGULAR_READ_MODE_BLOCK 0xFF\r
 \r
 // Default configuration\r
@@ -124,13 +126,15 @@ int usage_t55xx_dump(){
        return 0;\r
 }\r
 int usage_t55xx_detect(){\r
-       PrintAndLog("Usage:  lf t55xx detect [1]");\r
+       PrintAndLog("Usage:  lf t55xx detect [1] [p <password>]");\r
        PrintAndLog("Options:");\r
-       PrintAndLog("     [graph buffer data]  - if set, use Graphbuffer otherwise read data from tag.");\r
+       PrintAndLog("     1             - if set, use Graphbuffer otherwise read data from tag.");\r
+       PrintAndLog("     p <password>  - OPTIONAL password (8 hex characters)");\r
        PrintAndLog("");\r
        PrintAndLog("Examples:");\r
        PrintAndLog("      lf t55xx detect");\r
        PrintAndLog("      lf t55xx detect 1");\r
+       PrintAndLog("      lf t55xx detect p 11223344");\r
        PrintAndLog("");\r
        return 0;\r
 }\r
@@ -145,9 +149,32 @@ int usage_t55xx_wakup(){
                PrintAndLog("      lf t55xx wakeup p 11223344  - send wakeup password");\r
        return 0;\r
 }\r
+int usage_t55xx_bruteforce(){\r
+       PrintAndLog("This command uses A) bruteforce to scan a number range");\r
+       PrintAndLog("                  B) a dictionary attack");\r
+       PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]");\r
+       PrintAndLog("       password must be 4 bytes (8 hex symbols)");\r
+       PrintAndLog("Options:");\r
+       PrintAndLog("     h           - this help");\r
+       PrintAndLog("     <start_pwd> - 4 byte hex value to start pwd search at");\r
+       PrintAndLog("     <end_pwd>   - 4 byte hex value to end pwd search at");\r
+       PrintAndLog("     i <*.dic>   - loads a default keys dictionary file <*.dic>");\r
+       PrintAndLog("");\r
+       PrintAndLog("Examples:");\r
+       PrintAndLog("       lf t55xx bruteforce aaaaaaaa bbbbbbbb");\r
+       PrintAndLog("       lf t55xx bruteforce i default_pwd.dic");\r
+       PrintAndLog("");\r
+       return 0;\r
+}\r
 \r
 static int CmdHelp(const char *Cmd);\r
 \r
+void printT5xxHeader(uint8_t page){\r
+       PrintAndLog("Reading Page %d:", page);\r
+       PrintAndLog("blk | hex data | binary");\r
+       PrintAndLog("----+----------+---------------------------------");       \r
+}\r
+\r
 int CmdT55xxSetConfig(const char *Cmd) {\r
 \r
        uint8_t offset = 0;\r
@@ -256,7 +283,7 @@ int T55xxReadBlock(uint8_t block, bool page1, bool usepwd, bool override, uint32
        if ( usepwd ) {\r
                // try reading the config block and verify that PWD bit is set before doing this!\r
                if ( !override ) {\r
-                       if ( !AquireData(0, CONFIGURATION_BLOCK, false, 0 ) ) return 0;\r
+                       if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, false, 0 ) ) return 0;\r
                        if ( !tryDetectModulation() ) {\r
                                PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits.");\r
                                return 0;\r
@@ -324,8 +351,8 @@ int CmdT55xxReadBlock(const char *Cmd) {
                PrintAndLog("Block must be between 0 and 7");\r
                return 0;\r
        }\r
-       PrintAndLog("Reading Page %d:", page1); \r
-       PrintAndLog("blk | hex data | binary");\r
+\r
+       printT5xxHeader(page1);\r
        return T55xxReadBlock(block, page1, usepwd, override, password);\r
 }\r
 \r
@@ -386,15 +413,41 @@ bool DecodeT55xxBlock(){
 }\r
 \r
 int CmdT55xxDetect(const char *Cmd){\r
+       bool errors = FALSE;\r
+       bool useGB = FALSE;\r
+       bool usepwd = FALSE;\r
+       uint32_t password = 0;\r
+       uint8_t cmdp = 0;\r
 \r
-       char cmdp = param_getchar(Cmd, 0);\r
-       if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')\r
-               return usage_t55xx_detect();\r
+       while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
+               switch(param_getchar(Cmd, cmdp)) {\r
+               case 'h':\r
+               case 'H':\r
+                       return usage_t55xx_detect();\r
+               case 'p':\r
+               case 'P':\r
+                       password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
+                       usepwd = TRUE;\r
+                       cmdp += 2;\r
+                       break;\r
+               case '1':\r
+                       // use Graphbuffer data\r
+                       useGB = TRUE;\r
+                       cmdp++;\r
+                       break;\r
+               default:\r
+                       PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
+                       errors = true;\r
+                       break;\r
+               }\r
+       }\r
+       if (errors) return usage_t55xx_detect();\r
        \r
-       if (strlen(Cmd)==0)\r
-               if ( !AquireData(0, CONFIGURATION_BLOCK, false, 0) )\r
+       if ( !useGB) {\r
+               if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, usepwd, password) )\r
                        return 0;\r
-\r
+       }\r
+       \r
        if ( !tryDetectModulation() )\r
                PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");\r
 \r
@@ -407,7 +460,7 @@ bool tryDetectModulation(){
        t55xx_conf_block_t tests[15];\r
        int bitRate=0;\r
        uint8_t fc1 = 0, fc2 = 0, clk=0;\r
-       save_restoreGB(1);\r
+\r
        if (GetFskClock("", FALSE, FALSE)){ \r
                fskClocks(&fc1, &fc2, &clk, FALSE);\r
                if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){\r
@@ -466,7 +519,7 @@ bool tryDetectModulation(){
                        }\r
                }\r
                //undo trim from ask\r
-               save_restoreGB(0);\r
+               //save_restoreGB(0);\r
                clk = GetNrzClock("", FALSE, FALSE);\r
                if (clk>0) {\r
                        if ( NRZrawDemod("0 0 1", FALSE)  && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
@@ -486,9 +539,9 @@ bool tryDetectModulation(){
                        }\r
                }\r
                \r
-               //undo trim from nrz\r
-               save_restoreGB(0);\r
+               // allow undo\r
                // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
+               save_restoreGB(1);\r
                CmdLtrim("160");\r
                clk = GetPskClock("", FALSE, FALSE);\r
                if (clk>0) {\r
@@ -529,7 +582,9 @@ bool tryDetectModulation(){
                                }\r
                        } // inverse waves does not affect this demod\r
                }\r
-       }               \r
+               //undo trim samples\r
+               save_restoreGB(0);\r
+       }       \r
        if ( hits == 1) {\r
                config.modulation = tests[0].modulation;\r
                config.bitrate = tests[0].bitrate;\r
@@ -718,7 +773,7 @@ void printT55xxBlock(const char *blockNum){
 \r
        for (; i < endpos; ++i)\r
                bits[i - config.offset]=DemodBuffer[i];\r
-       //print second round of read data (more accurate due to antenna settling)\r
+\r
        blockData = PackBits(0, 32, bits);\r
 \r
        PrintAndLog("  %s | %08X | %s", blockNum, blockData, sprint_bin(bits,32));\r
@@ -786,8 +841,8 @@ int CmdT55xxWakeUp(const char *Cmd) {
 \r
 int CmdT55xxWriteBlock(const char *Cmd) {\r
        uint8_t block = 0xFF; //default to invalid block\r
-       uint32_t data = 0xFFFFFFFF; //default to blank Block \r
-       uint32_t password = 0xFFFFFFFF; //default to blank Block 7\r
+       uint32_t data = 0; //default to blank Block \r
+       uint32_t password = 0; //default to blank Block 7\r
        bool usepwd = false;\r
        bool page1 = false;     \r
        bool gotdata = false;\r
@@ -836,13 +891,15 @@ int CmdT55xxWriteBlock(const char *Cmd) {
        UsbCommand resp;\r
        c.d.asBytes[0] = (page1) ? 0x2 : 0; \r
 \r
-       PrintAndLog("Writing to page: %d  block: %d  data : 0x%08X", page1, block, data);\r
+       char pwdStr[16] = {0};\r
+       snprintf(pwdStr, sizeof(pwdStr), "pwd: 0x%08X", password);\r
+\r
+       PrintAndLog("Writing page %d  block: %02d  data: 0x%08X %s", page1, block, data,  (usepwd) ? pwdStr : "" );\r
 \r
        //Password mode\r
        if (usepwd) {\r
                c.arg[2] = password;\r
                c.d.asBytes[0] |= 0x1; \r
-               PrintAndLog("pwd   : 0x%08X", password);\r
        }\r
        clearCommandBuffer();\r
        SendCommand(&c);\r
@@ -861,7 +918,7 @@ int CmdT55xxReadTrace(const char *Cmd) {
                return usage_t55xx_trace();\r
 \r
        if (strlen(Cmd)==0)\r
-               if ( !AquireData( TRACE_BLOCK, REGULAR_READ_MODE_BLOCK, pwdmode, password ) )\r
+               if ( !AquireData( T55x7_PAGE1, REGULAR_READ_MODE_BLOCK, pwdmode, password ) )\r
                        return 0;\r
        \r
        if (!DecodeT55xxBlock()) return 0;\r
@@ -950,7 +1007,7 @@ int CmdT55xxInfo(const char *Cmd){
                return usage_t55xx_info();\r
        \r
        if (strlen(Cmd)==0)\r
-               if ( !AquireData( 0, CONFIGURATION_BLOCK, pwdmode, password ) )\r
+               if ( !AquireData( T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, pwdmode, password ) )\r
                        return 1;\r
 \r
        if (!DecodeT55xxBlock()) return 1;\r
@@ -1014,32 +1071,21 @@ int CmdT55xxDump(const char *Cmd){
                        override = true;\r
        }\r
        \r
-       PrintAndLog("Reading Page 0:"); \r
-       PrintAndLog("blk | hex data | binary");\r
-       for ( uint8_t i = 0; i <8; ++i){\r
+       printT5xxHeader(0);\r
+       for ( uint8_t i = 0; i <8; ++i)\r
                T55xxReadBlock(i, 0, usepwd, override, password);\r
-               /*memset(s,0,sizeof(s));\r
-               if ( hasPwd ) {\r
-                       if ( override ) {\r
-                               sprintf(s,"b %d p %02x%02x%02x%02x o", i, pwd[0],pwd[1],pwd[2],pwd[3]);                         \r
-                       } else {\r
-                               sprintf(s,"b %d p %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);                           \r
-                       }\r
-               } else {\r
-                       sprintf(s,"b %d", i);\r
-               }\r
-               CmdT55xxReadBlock(s);*/\r
-       }\r
-       PrintAndLog("Reading Page 1:"); \r
-       PrintAndLog("blk | hex data | binary");\r
-       for ( uint8_t   i = 0; i<4; i++){\r
+\r
+       printT5xxHeader(1);\r
+       for ( uint8_t   i = 0; i<4; i++)\r
                T55xxReadBlock(i, 1, usepwd, override, password);               \r
-       }\r
+\r
        return 1;\r
 }\r
 \r
 int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){\r
-\r
+       // arg0 bitmodes:\r
+       // bit0 = pwdmode\r
+       // bit1 = page to read from\r
        uint8_t arg0 = (page<<1) | pwdmode;\r
        UsbCommand c = {CMD_T55XX_READ_BLOCK, {arg0, block, password}};\r
 \r
@@ -1250,48 +1296,189 @@ int CmdResetRead(const char *Cmd) {
 int CmdT55xxWipe(const char *Cmd) {\r
        char writeData[20] = {0};\r
        char *ptrData = writeData;\r
-       uint8_t blk = 0;\r
+\r
        PrintAndLog("\nBeginning Wipe of a T55xx tag (assuming the tag is not password protected)\n");\r
+\r
        //try with the default password to reset block 0  (with a pwd should work even if pwd bit not set)\r
-       snprintf(ptrData,sizeof(writeData),"b %d d 00088040 p 0", blk);\r
-       if (!CmdT55xxWriteBlock(ptrData)){\r
-               PrintAndLog("Error writing blk %d", blk);\r
-       }\r
-       blk = 1;\r
-       for (; blk<8; blk++) {\r
+       snprintf(ptrData,sizeof(writeData),"b 0 d 00088040 p 0");\r
+\r
+       if (!CmdT55xxWriteBlock(ptrData))\r
+               PrintAndLog("Error writing blk 0");\r
+\r
+       for (uint8_t blk = 1; blk<8; blk++) {\r
                snprintf(ptrData,sizeof(writeData),"b %d d 0", blk);\r
-               if (!CmdT55xxWriteBlock(ptrData)){\r
+               if (!CmdT55xxWriteBlock(ptrData))\r
                        PrintAndLog("Error writing blk %d", blk);\r
+\r
+               memset(writeData, sizeof(writeData), 0x00);\r
+       }\r
+       return 0;\r
+}\r
+\r
+int CmdT55xxBruteForce(const char *Cmd) {\r
+\r
+       // load a default pwd file.\r
+       char buf[9];\r
+       char filename[FILE_PATH_SIZE]={0};\r
+       int keycnt = 0;\r
+       uint8_t stKeyBlock = 20;\r
+       uint8_t *keyBlock = NULL, *p;\r
+       keyBlock = calloc(stKeyBlock, 6);\r
+       if (keyBlock == NULL) return 1;\r
+\r
+       uint32_t start_password = 0x00000000; //start password\r
+       uint32_t end_password   = 0xFFFFFFFF; //end   password\r
+       bool found = false;\r
+\r
+       char cmdp = param_getchar(Cmd, 0);\r
+       if (cmdp == 'h' || cmdp == 'H') return usage_t55xx_bruteforce();\r
+\r
+       if (cmdp == 'i' || cmdp == 'I') {\r
+\r
+               int len = strlen(Cmd+2);\r
+               if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
+               memcpy(filename, Cmd+2, len);\r
+\r
+               FILE * f = fopen( filename , "r");\r
+\r
+               if ( !f ) {\r
+                       PrintAndLog("File: %s: not found or locked.", filename);\r
+                       free(keyBlock);\r
+                       return 1;\r
                }\r
+\r
+               while( fgets(buf, sizeof(buf), f) ){\r
+                       if (strlen(buf) < 8 || buf[7] == '\n') continue;\r
+\r
+                       while (fgetc(f) != '\n' && !feof(f)) ;  //goto next line\r
+\r
+                       //The line start with # is comment, skip\r
+                       if( buf[0]=='#' ) continue;\r
+\r
+                       if (!isxdigit(buf[0])){\r
+                               PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf);\r
+                               continue;\r
+                       }\r
+                       \r
+                       buf[8] = 0;\r
+\r
+                       if ( stKeyBlock - keycnt < 2) {\r
+                               p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
+                               if (!p) {\r
+                                       PrintAndLog("Cannot allocate memory for defaultKeys");\r
+                                       free(keyBlock);\r
+                                       return 2;\r
+                               }\r
+                               keyBlock = p;\r
+                       }\r
+                       memset(keyBlock + 4 * keycnt, 0, 4);\r
+                       num_to_bytes(strtoll(buf, NULL, 16), 4, keyBlock + 4*keycnt);\r
+                       PrintAndLog("chk custom pwd[%2d] %08X", keycnt, bytes_to_num(keyBlock + 4*keycnt, 4));\r
+                       keycnt++;\r
+                       memset(buf, 0, sizeof(buf));\r
+               }\r
+               fclose(f);\r
+               \r
+               if (keycnt == 0) {\r
+                       PrintAndLog("No keys found in file");\r
+                       return 1;\r
+               }\r
+               PrintAndLog("Loaded %d keys", keycnt);\r
+               \r
+               // loop\r
+               uint64_t testpwd = 0x00;\r
+               for (uint16_t c = 0; c < keycnt; ++c ) {\r
+\r
+                       if (ukbhit()) {\r
+                               getchar();\r
+                               printf("\naborted via keyboard!\n");\r
+                               return 0;\r
+                       }\r
+\r
+                       testpwd = bytes_to_num(keyBlock + 4*c, 4);\r
+\r
+                       PrintAndLog("Testing %08X", testpwd);\r
+\r
+                       if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, TRUE, testpwd)) {\r
+                               PrintAndLog("Aquireing data from device failed. Quitting");\r
+                               return 0;\r
+                       }\r
+\r
+                       found = tryDetectModulation();\r
+\r
+                       if ( found ) {\r
+                               PrintAndLog("Found valid password: [%08X]", testpwd);\r
+                               return 0;\r
+                       }\r
+               }\r
+               PrintAndLog("Password NOT found.");\r
+               return 0;\r
        }\r
+\r
+       // Try to read Block 7, first :)\r
+\r
+       // incremental pwd range search\r
+       start_password = param_get32ex(Cmd, 0, 0, 16);\r
+       end_password = param_get32ex(Cmd, 1, 0, 16);\r
+       \r
+       if ( start_password >= end_password ) return usage_t55xx_bruteforce();\r
+\r
+       PrintAndLog("Search password range [%08X -> %08X]", start_password, end_password);\r
+\r
+       uint32_t i = start_password;\r
+\r
+       while ((!found) && (i <= end_password)){\r
+\r
+       printf(".");\r
+       fflush(stdout);\r
+       if (ukbhit()) {\r
+               getchar();\r
+               printf("\naborted via keyboard!\n");\r
+               return 0;\r
+       }\r
+\r
+       if (!AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, TRUE, i)) {\r
+               PrintAndLog("Aquireing data from device failed. Quitting");\r
+               return 0;\r
+       }\r
+       found = tryDetectModulation();\r
+           \r
+       if (found) break;\r
+       i++;\r
+       }\r
+\r
+       PrintAndLog("");\r
+\r
+       if (found)\r
+       PrintAndLog("Found valid password: [%08x]", i);\r
+       else\r
+       PrintAndLog("Password NOT found. Last tried: [%08x]", --i);\r
        return 0;\r
 }\r
 \r
-static command_t CommandTable[] =\r
-{\r
-  {"help",     CmdHelp,           1, "This help"},\r
-  {"config",   CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},\r
-  {"detect",   CmdT55xxDetect,    0, "[1] Try detecting the tag modulation from reading the configuration block."},\r
-  {"read",     CmdT55xxReadBlock, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"},\r
-  {"resetread",CmdResetRead,      0, "Send Reset Cmd then lf read the stream to attempt to identify the start of it (needs a demod and/or plot after)"},\r
-  {"write",    CmdT55xxWriteBlock,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"},\r
-  {"trace",    CmdT55xxReadTrace, 0, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"},\r
-  {"info",     CmdT55xxInfo,      0, "[1] Show T55x7 configuration data (page 0/ blk 0)"},\r
-  {"dump",     CmdT55xxDump,      0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"},\r
-  {"special",  special,           0, "Show block changes with 64 different offsets"},\r
-  {"wakeup",   CmdT55xxWakeUp,    0, "Send AOR wakeup command"},\r
-  {"wipe",     CmdT55xxWipe,      0, "Wipe a T55xx tag and set defaults (will destroy any data on tag)"},\r
+static command_t CommandTable[] = {\r
+  {"help",      CmdHelp,           1, "This help"},\r
+       {"bruteforce",CmdT55xxBruteForce,0, "<start password> <end password> [i <*.dic>] Simple bruteforce attack to find password"},\r
+  {"config",    CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},\r
+  {"detect",    CmdT55xxDetect,    1, "[1] Try detecting the tag modulation from reading the configuration block."},\r
+  {"read",      CmdT55xxReadBlock, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"},\r
+  {"resetread", CmdResetRead,      0, "Send Reset Cmd then lf read the stream to attempt to identify the start of it (needs a demod and/or plot after)"},\r
+  {"write",     CmdT55xxWriteBlock,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"},\r
+  {"trace",     CmdT55xxReadTrace, 1, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"},\r
+  {"info",      CmdT55xxInfo,      1, "[1] Show T55x7 configuration data (page 0/ blk 0)"},\r
+  {"dump",      CmdT55xxDump,      0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"},\r
+  {"special",   special,           0, "Show block changes with 64 different offsets"},\r
+  {"wakeup",    CmdT55xxWakeUp,    0, "Send AOR wakeup command"},\r
+  {"wipe",      CmdT55xxWipe,      0, "Wipe a T55xx tag and set defaults (will destroy any data on tag)"},\r
   {NULL, NULL, 0, NULL}\r
 };\r
 \r
-int CmdLFT55XX(const char *Cmd)\r
-{\r
+int CmdLFT55XX(const char *Cmd) {\r
   CmdsParse(CommandTable, Cmd);\r
   return 0;\r
 }\r
 \r
-int CmdHelp(const char *Cmd)\r
-{\r
+int CmdHelp(const char *Cmd) {\r
   CmdsHelp(CommandTable);\r
   return 0;\r
 }\r
Impressum, Datenschutz