]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
reveng -g (model search given just hexstr with crc)
authormarshmellow42 <marshmellowrf@gmail.com>
Wed, 10 Jun 2015 05:33:37 +0000 (01:33 -0400)
committermarshmellow42 <marshmellowrf@gmail.com>
Wed, 10 Jun 2015 05:33:37 +0000 (01:33 -0400)
client/cmdcrc.c
client/cmdcrc.h
client/cmdmain.c
client/scripting.c

index f756cde43e2f33c87006569a7d896cad5cb5ab9a..006804eebd710b42d381550a719a39b2ea8c64c1 100644 (file)
@@ -54,7 +54,7 @@ int split(char *str, char *arr[MAX_ARGS]){
                char *tmp = calloc(len + 1, sizeof(char));
                memcpy(tmp, &str[beginIndex], len);
                arr[wordCnt++] = tmp;
-               //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
+               //PrintAndLog("DEBUG cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
                beginIndex = endIndex;
                if (wordCnt == maxWords)
                        break;
@@ -70,17 +70,23 @@ int CmdCrc(const char *Cmd)
        memcpy(Cmd2 + 7, Cmd, 50);
        char *argv[MAX_ARGS];
        int argc = split(Cmd2, argv);
-       //PrintAndLog("argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
-       reveng_main(argc, argv);
+
+       if (argc == 3 && memcmp(argv[1],"-g",2)==0) {
+               CmdrevengSearch(argv[2]);
+       } else {
+               reveng_main(argc, argv);
+       }
+       //PrintAndLog("DEBUG argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
        for(int i = 0; i < argc; ++i){
-               //puts(arr[i]);
                free(argv[i]);
        }
 
        return 0;
 }
 
-int GetModels(char *Models[], int *count, uint32_t *width){
+//returns array of model names and the count of models returning
+//  as well as a width array for the width of each model
+int GetModels(char *Models[], int *count, uint8_t *width){
        /* default values */
        static model_t model = {
                PZERO,          /* no CRC polynomial, user must specify */
@@ -105,7 +111,7 @@ int GetModels(char *Models[], int *count, uint32_t *width){
        
        int args = 0, psets, pass;
        int Cnt = 0;
-       if (*width == 0) { //reveng -D
+       if (width[0] == 0) { //reveng -D
                *count = mcount();
                if(!*count)
                        return uerr("no preset models available");
@@ -120,6 +126,7 @@ int GetModels(char *Models[], int *count, uint32_t *width){
 
                        memcpy(tmp, model.name, size);
                        Models[mode] = tmp;
+                       width[mode] = plen(model.spoly);
                }
                mfree(&model);
        } else { //reveng -s
@@ -127,13 +134,13 @@ int GetModels(char *Models[], int *count, uint32_t *width){
                if(~model.flags & P_MULXN)
                        return uerr("cannot search for non-Williams compliant models");
 
-               praloc(&model.spoly, *width);
-               praloc(&model.init, *width);
-               praloc(&model.xorout, *width);
+               praloc(&model.spoly, (unsigned long)width[0]);
+               praloc(&model.init, (unsigned long)width[0]);
+               praloc(&model.xorout, (unsigned long)width[0]);
                if(!plen(model.spoly))
-                       palloc(&model.spoly, *width);
+                       palloc(&model.spoly, (unsigned long)width[0]);
                else
-                       *width = plen(model.spoly);
+                       width[0] = (uint8_t)plen(model.spoly);
 
                /* special case if qpoly is zero, search to end of range */
                if(!ptst(qpoly))
@@ -173,7 +180,7 @@ int GetModels(char *Models[], int *count, uint32_t *width){
                                        mbynum(&pset, --psets);
                                        
                                        /* skip if different width, or refin or refout don't match */
-                                       if(plen(pset.spoly) != *width || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT))
+                                       if(plen(pset.spoly) != width[0] || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT))
                                                continue;
                                        /* skip if the preset doesn't match specified parameters */
                                        if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly))
@@ -210,6 +217,7 @@ int GetModels(char *Models[], int *count, uint32_t *width){
                                                        PrintAndLog("out of memory?");
                                                        return 0;
                                                }
+                                               width[Cnt] = width[0];
                                                memcpy(tmp, pset.name, size);
                                                Models[Cnt++] = tmp;
                                                *count = Cnt;
@@ -264,18 +272,20 @@ int GetModels(char *Models[], int *count, uint32_t *width){
 int CmdrevengTest(const char *Cmd){
        char *Models[80];
        int count = 0;
-       uint32_t width = 0;
+       uint8_t widtharr[80] = {0};
+       uint8_t width = 0;
        width = param_get8(Cmd, 0);
        //PrintAndLog("width: %d",width);
        if (width > 89)
                return uerr("Width cannot exceed 89");
 
-       int ans = GetModels(Models, &count, &width);
+       widtharr[0] = width;
+       int ans = GetModels(Models, &count, widtharr);
        if (!ans) return 0;
        
        PrintAndLog("Count: %d",count);
        for (int i = 0; i < count; i++){
-               PrintAndLog("Model %d: %s",i,Models[i]);
+               PrintAndLog("Model %d: %s, width: %d",i,Models[i], widtharr[i]);
                free(Models[i]);
        }
        return 1;
@@ -438,3 +448,100 @@ int CmdrevengTestC(const char *Cmd){
        PrintAndLog("Result: %s",result);
        return 1;
 }
+
+//returns a calloced string (needs to be freed)
+char *SwapEndianStr(const char *inStr, const size_t len, const uint8_t blockSize){
+       char *tmp = calloc(len+1, sizeof(char));
+       for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
+               for (size_t i = 0; i < blockSize; i+=2){
+                       tmp[i+(blockSize*block)] = inStr[(blockSize-1-i-1)+(blockSize*block)];
+                       tmp[i+(blockSize*block)+1] = inStr[(blockSize-1-i)+(blockSize*block)];
+               }
+       }
+       return tmp;
+}
+
+// takes hex string in and searches for a matching result (hex string must include checksum)
+int CmdrevengSearch(const char *Cmd){
+       char inHexStr[50] = {0x00};
+       int dataLen = param_getstr(Cmd, 0, inHexStr);
+       if (dataLen < 4) return 0;
+
+       char *Models[80];
+       int count = 0;
+       uint8_t width[80];
+       width[0] = 0;
+       uint8_t crcChars = 0;
+       char result[30];
+       char revResult[30];
+       int ans = GetModels(Models, &count, width);
+       bool found = false;
+       if (!ans) return 0;
+       
+       // try each model and get result
+       for (int i = 0; i < count; i++){
+               /*if (found) {
+                       free(Models[i]);
+                       continue;
+               }*/
+               // round up to # of characters in this model's crc
+               crcChars = ((width[i]+7)/8)*2; 
+               // can't test a model that has more crc digits than our data
+               if (crcChars >= dataLen) 
+                       continue;
+               memset(result, 0, 30);
+               char *inCRC = calloc(crcChars+1, sizeof(char));
+               memcpy(inCRC, inHexStr+(dataLen-crcChars), crcChars);
+
+               char *outHex = calloc(dataLen-crcChars+1, sizeof(char));
+               memcpy(outHex, inHexStr, dataLen-crcChars);
+
+               //PrintAndLog("DEBUG: dataLen: %d, crcChars: %d, Model: %s, CRC: %s, width: %d, outHex: %s",dataLen, crcChars, Models[i], inCRC, width[i], outHex);
+               ans = RunModel(Models[i], outHex, false, 0, result);
+               if (ans) {
+                       //test for match
+                       if (memcmp(result, inCRC, crcChars)==0){
+                               PrintAndLog("\nFound a match!\nModel: %s\nValue: %s\n",Models[i], result);
+                               //optional - stop searching if found...
+                               found = true;
+                       } else {
+                               if (crcChars > 2){
+                                       char *swapEndian = SwapEndianStr(result, crcChars, crcChars);
+                                       if (memcmp(swapEndian, inCRC, crcChars)==0){
+                                               PrintAndLog("\nFound a match!\nModel: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
+                                               //optional - stop searching if found...
+                                               found = true;
+                                       }
+                                       free(swapEndian);
+                               }
+                       }
+               }
+               
+               //if (!found){
+                       ans = RunModel(Models[i], outHex, true, 0, revResult);
+                       if (ans) {
+                               //test for match
+                               if (memcmp(revResult, inCRC, crcChars)==0){
+                                       PrintAndLog("\nFound a match!\nModel Reversed: %s\nValue: %s\n",Models[i], revResult);
+                                       //optional - stop searching if found...
+                                       found = true;
+                               } else {
+                                       if (crcChars > 2){
+                                               char *swapEndian = SwapEndianStr(revResult, crcChars, crcChars);
+                                               if (memcmp(swapEndian, inCRC, crcChars)==0){
+                                                       PrintAndLog("\nFound a match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
+                                                       //optional - stop searching if found...
+                                                       found = true;
+                                               }
+                                               free(swapEndian);
+                                       }
+                               }
+                       }
+               //}
+               free(inCRC);
+               free(outHex);
+               free(Models[i]);
+       }
+       if (!found) PrintAndLog("\nNo matches found\n");
+       return 1;
+}
index 5b324020e64e5374a65b0959ed2225eece9fff85..5041554d2b5f8fb9b8ff403f2868cb45a6756abb 100644 (file)
@@ -14,6 +14,7 @@
 int CmdCrc(const char *Cmd);
 int CmdrevengTest(const char *Cmd);
 int CmdrevengTestC(const char *Cmd);
-int GetModels(char *Models[], int *count, uint32_t *width);
+int CmdrevengSearch(const char *Cmd);
+int GetModels(char *Models[], int *count, uint8_t *width);
 int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *result);
 #endif
index 21886ca003fc8cec88c358fb0cb85daa4b24834a..33fe10c8b97a999e1be62fa563b0862231c18f91 100644 (file)
@@ -37,8 +37,8 @@ static int CmdRev(const char *Cmd);
 /*
 static int CmdrevengT(const char *Cmd);
 static int CmdrevengC(const char *Cmd);
+static int CmdrevengA(const char *Cmd);
 */
-
 //For storing command that are received from the device
 static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
 //Points to the next empty position to write to
@@ -54,10 +54,11 @@ static command_t CommandTable[] =
   {"hw",    CmdHW,    1, "{ Hardware commands... }"},
   {"lf",    CmdLF,    1, "{ Low Frequency commands... }"},
   {"reveng",CmdRev,   1, "Crc calculations from the software reveng1-30"},
-   // for testing reveng api - cmdcrc.c
-  //{"revengt",CmdrevengT,1, "TEST Crc calculations from the software reveng1-30"},
-  //{"revengc",CmdrevengC,1, "TEST Crc calculations from the software reveng1-30"},
-  
+  /* // for testing reveng api - cmdcrc.c
+  {"revenga",CmdrevengA,1, "TEST Crc calculations from the software reveng1-30"},
+  {"revengt",CmdrevengT,1, "TEST Crc calculations from the software reveng1-30"},
+  {"revengc",CmdrevengC,1, "TEST Crc calculations from the software reveng1-30"},
+  */
   {"script",CmdScript,1, "{ Scripting commands }"},
   {"quit",  CmdQuit,  1, "Exit program"},
   {"exit",  CmdQuit,  1, "Exit program"},
@@ -85,8 +86,12 @@ int CmdRev(const char *Cmd)
   CmdCrc(Cmd);
   return 0;
 }
-/*
- // for testing reveng api - cmdcrc.c
+
+/* // for testing reveng api - cmdcrc.c
+int CmdrevengA(const char *Cmd)
+{
+  return CmdrevengSearch(Cmd);
+}
 int CmdrevengT(const char *Cmd)
 {
   return CmdrevengTest(Cmd);
index cc6498ce5997a52a85adef7a645a6d067c7c3bde..88afef34049c8c71b8556fc43313ea5683e711f5 100644 (file)
@@ -397,8 +397,9 @@ static int l_reveng_models(lua_State *L){
        
        if( in_width > 89 ) return returnToLuaWithError(L,"Width cannot exceed 89, got %d", in_width);
 
-       uint32_t width = (uint32_t)in_width;
-       int ans = GetModels(models, &count, &width);
+       uint8_t width[80];
+       width[0] = (uint8_t)in_width;
+       int ans = GetModels(models, &count, width);
        if (!ans) return 0;
        
        lua_newtable(L);
Impressum, Datenschutz