X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/660d641a03456e99ea83c68dbd3d03bae2b64573..1c611bbd26066e1a8dd36ffd389b57040a7dfad6:/client/cmdhfmf.c diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index 11288085..96eb8007 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -15,33 +15,31 @@ static int CmdHelp(const char *Cmd); int CmdHF14AMifare(const char *Cmd) { uint32_t uid = 0; - uint32_t nt = 0; + uint32_t nt = 0, nr = 0; uint64_t par_list = 0, ks_list = 0, r_key = 0; uint8_t isOK = 0; uint8_t keyBlock[8] = {0}; - if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, keyBlock, 8)) { - PrintAndLog("Nt must include 8 HEX symbols"); - return 1; - } + UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}}; + + // message + printf("-------------------------------------------------------------------------\n"); + printf("Executing command. Expected execution time: 25sec on average :-)\n"); + printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n"); + printf("-------------------------------------------------------------------------\n"); - UsbCommand c = {CMD_READER_MIFARE, {(uint32_t)bytes_to_num(keyBlock, 4), 0, 0}}; start: - SendCommand(&c); + clearCommandBuffer(); + SendCommand(&c); //flush queue while (ukbhit()) getchar(); - // message - printf("-------------------------------------------------------------------------\n"); - printf("Executing command. It may take up to 30 min.\n"); - printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n"); - printf("-------------------------------------------------------------------------\n"); // wait cycle while (true) { - printf("."); + printf("."); fflush(stdout); if (ukbhit()) { getchar(); @@ -50,27 +48,26 @@ start: } UsbCommand resp; - if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) { + if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { isOK = resp.arg[0] & 0xff; - uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4); nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4); par_list = bytes_to_num(resp.d.asBytes + 8, 8); ks_list = bytes_to_num(resp.d.asBytes + 16, 8); - + nr = bytes_to_num(resp.d.asBytes + 24, 4); printf("\n\n"); - PrintAndLog("isOk:%02x", isOK); if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n"); break; } } + printf("\n"); // error if (isOK != 1) return 1; // execute original function from util nonce2key - if (nonce2key(uid, nt, par_list, ks_list, &r_key)) + if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key)) { isOK = 2; PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); @@ -85,8 +82,9 @@ start: PrintAndLog("Found valid key:%012"llx, r_key); else { - if (isOK != 2) PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt); - c.arg[0] = nt; + if (isOK != 2) PrintAndLog("Found invalid key. "); + PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce..."); + c.arg[0] = false; goto start; } @@ -287,10 +285,16 @@ int CmdHF14AMfDump(const char *Cmd) // Read key file for (i=0 ; i<16 ; i++) { - fread ( keyA[i], 1, 6, fin ); + if (fread( keyA[i], 1, 6, fin ) == 0) { + PrintAndLog("File reading error."); + return 2; + } } for (i=0 ; i<16 ; i++) { - fread ( keyB[i], 1, 6, fin ); + if (fread( keyB[i], 1, 6, fin ) == 0) { + PrintAndLog("File reading error."); + return 2; + } } // Read access rights to sectors @@ -416,10 +420,16 @@ int CmdHF14AMfRestore(const char *Cmd) } for (i=0 ; i<16 ; i++) { - fread(keyA[i], 1, 6, fkeys); + if (fread(keyA[i], 1, 6, fkeys) == 0) { + PrintAndLog("File reading error."); + return 2; + } } for (i=0 ; i<16 ; i++) { - fread(keyB[i], 1, 6, fkeys); + if (fread(keyB[i], 1, 6, fkeys) == 0) { + PrintAndLog("File reading error."); + return 2; + } } PrintAndLog("Restoring dumpdata.bin to card"); @@ -429,7 +439,10 @@ int CmdHF14AMfRestore(const char *Cmd) UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}}; memcpy(c.d.asBytes, key, 6); - fread(bldata, 1, 16, fdump); + if (fread(bldata, 1, 16, fdump) == 0) { + PrintAndLog("File reading error."); + return 2; + } if (j == 3) { bldata[0] = (keyA[i][0]); @@ -816,8 +829,11 @@ int CmdHF14AMfChk(const char *Cmd) if ( (f = fopen( filename , "r")) ) { while( !feof(f) ){ memset(buf, 0, sizeof(buf)); - fgets(buf, sizeof(buf), f); - + if (fgets(buf, sizeof(buf), f) == NULL) { + PrintAndLog("File reading error."); + return 2; + } + if (strlen(buf) < 12 || buf[11] == '\n') continue; @@ -1077,7 +1093,10 @@ int CmdHF14AMfELoad(const char *Cmd) blockNum = 0; while(!feof(f)){ memset(buf, 0, sizeof(buf)); - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) { + PrintAndLog("File reading error."); + return 2; + } if (strlen(buf) < 32){ if(strlen(buf) && feof(f)) @@ -1344,7 +1363,10 @@ int CmdHF14AMfCLoad(const char *Cmd) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; while(!feof(f)){ memset(buf, 0, sizeof(buf)); - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) { + PrintAndLog("File reading error."); + return 2; + } if (strlen(buf) < 32){ if(strlen(buf) && feof(f))