X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/55acbb2a3903f7940d0db5fae2e8f4fdab51d1b8..d0b14ad5e71ea33e9d0a5f226522df98142d552c:/client/cmdhfmf.c diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index 6abfdda2..5927616b 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -9,7 +9,6 @@ //----------------------------------------------------------------------------- #include "cmdhfmf.h" -#include "proxmark3.h" static int CmdHelp(const char *Cmd); @@ -50,14 +49,14 @@ start: break; } - UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000); - if (resp != NULL) { - isOK = resp->arg[0] & 0xff; + UsbCommand resp; + if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) { + 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); + 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); printf("\n\n"); PrintAndLog("isOk:%02x", isOK); @@ -71,17 +70,22 @@ start: 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, 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:%012llx \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); else { - PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt); + if (isOK != 2) PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt); c.arg[0] = nt; goto start; } @@ -126,11 +130,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 +173,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 +228,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 +244,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 +272,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"); @@ -302,11 +303,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 +328,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 +353,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]); @@ -457,10 +460,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"); @@ -984,10 +987,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 +1027,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"); @@ -1269,10 +1264,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"); @@ -1400,10 +1391,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); @@ -1545,7 +1532,7 @@ int CmdHF14AMfSniff(const char *Cmd){ // params bool wantLogToFile = 0; bool wantDecrypt = 0; - bool wantSaveToEml = 0; + //bool wantSaveToEml = 0; TODO bool wantSaveToEmlFile = 0; //var @@ -1558,6 +1545,7 @@ 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); @@ -1568,7 +1556,7 @@ int CmdHF14AMfSniff(const char *Cmd){ 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(" n/a r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`"); + 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; @@ -1578,7 +1566,7 @@ int CmdHF14AMfSniff(const char *Cmd){ 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; + //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true; } @@ -1601,11 +1589,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) { @@ -1613,7 +1601,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++; } @@ -1625,7 +1613,9 @@ 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)) { @@ -1635,14 +1625,14 @@ int CmdHF14AMfSniff(const char *Cmd){ 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"); + FillFileNameByUID(logHexFileName, uid, ".log", 7); AddLogCurrentDT(logHexFileName); } - if (wantDecrypt) mfTraceInit(uid, atqa, sak); + 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); + if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile); } bufPtr += len; num++; @@ -1686,7 +1676,7 @@ static command_t CommandTable[] = int CmdHFMF(const char *Cmd) { // flush - while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ; + while (!WaitForResponseTimeout(CMD_ACK,NULL,500)); CmdsParse(CommandTable, Cmd); return 0;