X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/39864b0bd41dd5e896bcb8eeabcf2c3932f2203a..42daa759c16328607aff6ab35ef97656ad2050da:/client/cmdhfmf.c diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index 98006d41..956bbc0e 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -9,40 +9,37 @@ //----------------------------------------------------------------------------- #include "cmdhfmf.h" -#include "proxmark3.h" 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,39 +47,44 @@ start: break; } - UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000); - if (resp != NULL) { - 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); - + UsbCommand resp; + 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)) return 2; - printf("------------------------------------------------------------------\n"); - PrintAndLog("Key found:%012llx \n", 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); + } else { + printf("------------------------------------------------------------------\n"); + PrintAndLog("Key found:%012"llx" \n", r_key); - num_to_bytes(r_key, 6, keyBlock); - isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key); + num_to_bytes(r_key, 6, keyBlock); + isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key); + } if (!isOK) - PrintAndLog("Found valid key:%012llx", r_key); + PrintAndLog("Found valid key:%012"llx, r_key); else { - 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; } @@ -126,11 +128,10 @@ int CmdHF14AMfWrBl(const char *Cmd) memcpy(c.d.asBytes, key, 6); memcpy(c.d.asBytes + 10, bldata, 16); SendCommand(&c); - UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500); - - if (resp != NULL) { - uint8_t isOK = resp->arg[0] & 0xff; + UsbCommand resp; + if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { + uint8_t isOK = resp.arg[0] & 0xff; PrintAndLog("isOk:%02x", isOK); } else { PrintAndLog("Command execute timeout"); @@ -170,11 +171,11 @@ int CmdHF14AMfRdBl(const char *Cmd) UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}}; memcpy(c.d.asBytes, key, 6); SendCommand(&c); - UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500); - if (resp != NULL) { - uint8_t isOK = resp->arg[0] & 0xff; - uint8_t * data = resp->d.asBytes; + UsbCommand resp; + if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { + uint8_t isOK = resp.arg[0] & 0xff; + uint8_t * data = resp.d.asBytes; if (isOK) PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16)); @@ -225,12 +226,12 @@ int CmdHF14AMfRdSc(const char *Cmd) UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}}; memcpy(c.d.asBytes, key, 6); SendCommand(&c); - UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500); PrintAndLog(" "); - if (resp != NULL) { - isOK = resp->arg[0] & 0xff; - data = resp->d.asBytes; + UsbCommand resp; + if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { + isOK = resp.arg[0] & 0xff; + data = resp.d.asBytes; PrintAndLog("isOk:%02x", isOK); if (isOK) @@ -241,13 +242,11 @@ int CmdHF14AMfRdSc(const char *Cmd) PrintAndLog("Command1 execute timeout"); } - // response2 - resp = WaitForResponseTimeout(CMD_ACK, 500); + // response2 PrintAndLog(" "); - - if (resp != NULL) { - isOK = resp->arg[0] & 0xff; - data = resp->d.asBytes; + if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { + isOK = resp.arg[0] & 0xff; + data = resp.d.asBytes; if (isOK) for (i = 0; i < 2; i++) { @@ -271,7 +270,7 @@ int CmdHF14AMfDump(const char *Cmd) FILE *fin; FILE *fout; - UsbCommand *resp; + UsbCommand resp; if ((fin = fopen("dumpkeys.bin","rb")) == NULL) { PrintAndLog("Could not find file dumpkeys.bin"); @@ -286,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 @@ -302,11 +307,10 @@ int CmdHF14AMfDump(const char *Cmd) UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}}; memcpy(c.d.asBytes, keyA[i], 6); SendCommand(&c); - resp = WaitForResponseTimeout(CMD_ACK, 1500); - if (resp != NULL) { - uint8_t isOK = resp->arg[0] & 0xff; - uint8_t *data = resp->d.asBytes; + if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { + uint8_t isOK = resp.arg[0] & 0xff; + uint8_t *data = resp.d.asBytes; if (isOK){ rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2); rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3); @@ -328,20 +332,23 @@ int CmdHF14AMfDump(const char *Cmd) PrintAndLog("|----- Dumping all blocks to file... -----|"); PrintAndLog("|-----------------------------------------|"); + for (i=0 ; i<16 ; i++) { for (j=0 ; j<4 ; j++) { - if (j == 3){ + bool received = false; + + if (j == 3){ UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}}; memcpy(c.d.asBytes, keyA[i], 6); SendCommand(&c); - resp = WaitForResponseTimeout(CMD_ACK, 1500); + received = WaitForResponseTimeout(CMD_ACK,&resp,1500); } else{ if ((rights[i][j] == 6) | (rights[i][j] == 5)) { UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}}; memcpy(c.d.asBytes, keyB[i], 6); SendCommand(&c); - resp = WaitForResponseTimeout(CMD_ACK, 1500); + received = WaitForResponseTimeout(CMD_ACK,&resp,1500); } else if (rights[i][j] == 7) { PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j); @@ -350,13 +357,13 @@ int CmdHF14AMfDump(const char *Cmd) UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}}; memcpy(c.d.asBytes, keyA[i], 6); SendCommand(&c); - resp = WaitForResponseTimeout(CMD_ACK, 1500); + received = WaitForResponseTimeout(CMD_ACK,&resp,1500); } } - if (resp != NULL) { - uint8_t isOK = resp->arg[0] & 0xff; - uint8_t *data = resp->d.asBytes; + if (received) { + uint8_t isOK = resp.arg[0] & 0xff; + uint8_t *data = resp.d.asBytes; if (j == 3) { data[0] = (keyA[i][0]); data[1] = (keyA[i][1]); @@ -413,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"); @@ -426,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]); @@ -457,10 +473,10 @@ int CmdHF14AMfRestore(const char *Cmd) memcpy(c.d.asBytes + 10, bldata, 16); SendCommand(&c); - UsbCommand *resp = WaitForResponseTimeout(CMD_ACK, 1500); - if (resp != NULL) { - uint8_t isOK = resp->arg[0] & 0xff; + UsbCommand resp; + if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { + uint8_t isOK = resp.arg[0] & 0xff; PrintAndLog("isOk:%02x", isOK); } else { PrintAndLog("Command execute timeout"); @@ -484,7 +500,7 @@ int CmdHF14AMfNested(const char *Cmd) uint8_t blDiff = 0; int SectorsCnt = 0; uint8_t key[6] = {0, 0, 0, 0, 0, 0}; - uint8_t keyBlock[16 * 6]; + uint8_t keyBlock[6*6]; uint64_t key64 = 0; int transferToEml = 0; @@ -556,21 +572,13 @@ int CmdHF14AMfNested(const char *Cmd) PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType); if (cmdp == 'o') { - if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) { + if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) { PrintAndLog("Nested error."); return 2; } - - for (i = 0; i < 16; i++) { - PrintAndLog("count=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6)); - } - - // test keys - res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64); - if (res) - res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64); - if (!res) { - PrintAndLog("Found valid key:%012llx", key64); + key64 = bytes_to_num(keyBlock, 6); + if (key64) { + PrintAndLog("Found valid key:%012"llx, key64); // transfer key to the emulator if (transferToEml) { @@ -587,6 +595,9 @@ int CmdHF14AMfNested(const char *Cmd) } } else { // ------------------------------------ multiple sectors working + clock_t time1; + time1 = clock(); + blDiff = blockNo % 4; PrintAndLog("Block shift=%d", blDiff); e_sector = calloc(SectorsCnt, sizeof(sector)); @@ -594,10 +605,10 @@ int CmdHF14AMfNested(const char *Cmd) //test current key 4 sectors memcpy(keyBlock, key, 6); - num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6)); - num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6)); - num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6)); - num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6)); + num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6)); + num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6)); + num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6)); + num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6)); num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6)); PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt); @@ -612,38 +623,47 @@ int CmdHF14AMfNested(const char *Cmd) e_sector[i].foundKey[j] = 1; } } - } + } + // nested sectors iterations = 0; PrintAndLog("nested..."); + bool calibrate = true; for (i = 0; i < NESTED_SECTOR_RETRY; i++) { - for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) + for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) { for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue; - if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue; + PrintAndLog("-----------------------------------------------"); + if(mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, calibrate)) { + PrintAndLog("Nested error.\n"); + return 2; + } + else { + calibrate = false; + } iterations++; - - //try keys from nested - res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64); - if (res) - res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64); - if (!res) { - PrintAndLog("Found valid key:%012llx", key64); + + key64 = bytes_to_num(keyBlock, 6); + if (key64) { + PrintAndLog("Found valid key:%012"llx, key64); e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1; e_sector[trgBlockNo / 4].Key[trgKeyType] = key64; } } + } } - PrintAndLog("Iterations count: %d", iterations); + printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/1000.0, ((float)clock() - time1)/iterations/1000.0); + + PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations); //print them PrintAndLog("|---|----------------|---|----------------|---|"); PrintAndLog("|sec|key A |res|key B |res|"); PrintAndLog("|---|----------------|---|----------------|---|"); for (i = 0; i < SectorsCnt; i++) { - PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i, + PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i, e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]); } PrintAndLog("|---|----------------|---|----------------|---|"); @@ -813,14 +833,17 @@ 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; while (fgetc(f) != '\n' && !feof(f)) ; //goto next line - if( buf[0]=='#' ) continue; //The line start with # is remcommnet,skip + if( buf[0]=='#' ) continue; //The line start with # is comment, skip if (!isxdigit(buf[0])){ PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf); @@ -840,7 +863,7 @@ int CmdHF14AMfChk(const char *Cmd) } memset(keyBlock + 6 * keycnt, 0, 6); num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt); - PrintAndLog("chk custom key[%d] %012llx", keycnt, bytes_to_num(keyBlock + 6*keycnt, 6)); + PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6)); keycnt++; } } else { @@ -864,24 +887,19 @@ int CmdHF14AMfChk(const char *Cmd) int b=blockNo; for (int i=0; i8?8:keycnt; - for (int c = 0; c < keycnt; c+=size) { - size=keycnt-c>8?8:keycnt-c; - res = mfCheckKeys(b, t, size, keyBlock +6*c, &key64); + uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt; + for (uint32_t c = 0; c < keycnt; c+=max_keys) { + uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c; + res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64); if (res !=1) { if (!res) { - PrintAndLog("Found valid key:[%012llx]",key64); + PrintAndLog("Found valid key:[%012"llx"]",key64); if (transferToEml) { uint8_t block[16]; mfEmlGetMem(block, get_trailer_block(b), 1); num_to_bytes(key64, 6, block + t*10); mfEmlSetMem(block, get_trailer_block(b), 1); } - break; - } - else { - printf("Not found yet, keycnt:%d\r", c+size); - fflush(stdout); } } else { PrintAndLog("Command execute timeout"); @@ -984,10 +1002,6 @@ int CmdHF14AMfEGet(const char *Cmd) } blockNo = param_get8(Cmd, 0); - if (blockNo >= 32 * 4 + 8 * 16) { - PrintAndLog("Block number must be in [0..255] as in MIFARE classic."); - return 1; - } PrintAndLog(" "); if (!mfEmlGetMem(data, blockNo, 3)) { @@ -1028,10 +1042,6 @@ int CmdHF14AMfESet(const char *Cmd) } blockNo = param_get8(Cmd, 0); - if (blockNo >= 32 * 4 + 8 * 16) { - PrintAndLog("Block number must be in [0..255] as in MIFARE classic."); - return 1; - } if (param_gethex(Cmd, 1, memBlock, 32)) { PrintAndLog("block data must include 32 HEX symbols"); @@ -1082,7 +1092,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)) @@ -1210,7 +1223,7 @@ int CmdHF14AMfEKeyPrn(const char *Cmd) } keyA = bytes_to_num(data, 6); keyB = bytes_to_num(data + 10, 6); - PrintAndLog("|%03d| %012llx | %012llx |", i, keyA, keyB); + PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB); } PrintAndLog("|---|----------------|----------------|"); @@ -1269,10 +1282,6 @@ int CmdHF14AMfCSetBlk(const char *Cmd) } blockNo = param_get8(Cmd, 0); - if (blockNo >= 32 * 4 + 8 * 16) { - PrintAndLog("Block number must be in [0..255] as in MIFARE classic."); - return 1; - } if (param_gethex(Cmd, 1, memBlock, 32)) { PrintAndLog("block data must include 32 HEX symbols"); @@ -1353,7 +1362,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)) @@ -1400,10 +1412,6 @@ int CmdHF14AMfCGetBlk(const char *Cmd) { } blockNo = param_get8(Cmd, 0); - if (blockNo >= 32 * 4 + 8 * 16) { - PrintAndLog("Block number must be in [0..255] as in MIFARE classic."); - return 1; - } PrintAndLog("--block number:%02x ", blockNo); @@ -1542,6 +1550,13 @@ int CmdHF14AMfCSave(const char *Cmd) { } int CmdHF14AMfSniff(const char *Cmd){ + // params + bool wantLogToFile = 0; + bool wantDecrypt = 0; + //bool wantSaveToEml = 0; TODO + bool wantSaveToEmlFile = 0; + + //var int res = 0; int len = 0; int blockLen = 0; @@ -1551,16 +1566,31 @@ int CmdHF14AMfSniff(const char *Cmd){ uint8_t atqa[2]; uint8_t sak; bool isTag; + uint32_t parity; uint8_t buf[3000]; uint8_t * bufPtr = buf; memset(buf, 0x00, 3000); if (param_getchar(Cmd, 0) == 'h') { - PrintAndLog("Usage: hf mf sniff "); - PrintAndLog(" sample: hf mf sniff "); + PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file."); + PrintAndLog("You can specify:"); + PrintAndLog(" l - save encrypted sequence to logfile `uid.log`"); + PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`"); + PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory"); + PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`"); + PrintAndLog("Usage: hf mf sniff [l][d][e][r]"); + PrintAndLog(" sample: hf mf sniff l d e"); return 0; } + for (int i = 0; i < 4; i++) { + char ctmp = param_getchar(Cmd, i); + if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true; + if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true; + //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO + if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true; + } + printf("-------------------------------------------------------------------------\n"); printf("Executing command. \n"); printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n"); @@ -1580,11 +1610,11 @@ int CmdHF14AMfSniff(const char *Cmd){ break; } - UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000); - if (resp != NULL) { - res = resp->arg[0] & 0xff; - len = resp->arg[1]; - num = resp->arg[2]; + UsbCommand resp; + if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) { + res = resp.arg[0] & 0xff; + len = resp.arg[1]; + num = resp.arg[2]; if (res == 0) return 0; if (res == 1) { @@ -1592,7 +1622,7 @@ int CmdHF14AMfSniff(const char *Cmd){ bufPtr = buf; memset(buf, 0x00, 3000); } - memcpy(bufPtr, resp->d.asBytes, len); + memcpy(bufPtr, resp.d.asBytes, len); bufPtr += len; pckNum++; } @@ -1604,16 +1634,26 @@ int CmdHF14AMfSniff(const char *Cmd){ num = 0; while (bufPtr - buf + 9 < blockLen) { isTag = bufPtr[3] & 0x80 ? true:false; - bufPtr += 8; + bufPtr += 4; + parity = *((uint32_t *)(bufPtr)); + bufPtr += 4; len = bufPtr[0]; bufPtr++; if ((len == 14) && (bufPtr[0] = 0xff) && (bufPtr[1] = 0xff)) { memcpy(uid, bufPtr + 2, 7); memcpy(atqa, bufPtr + 2 + 7, 2); sak = bufPtr[11]; + PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak); + if (wantLogToFile) { + FillFileNameByUID(logHexFileName, uid, ".log", 7); + AddLogCurrentDT(logHexFileName); + } + if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile); } else { PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len)); + if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len); + if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile); } bufPtr += len; num++; @@ -1657,7 +1697,7 @@ static command_t CommandTable[] = int CmdHFMF(const char *Cmd) { // flush - while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ; + WaitForResponseTimeout(CMD_ACK,NULL,100); CmdsParse(CommandTable, Cmd); return 0;