From 314253c3590ebd2ab94a3982116dbb5222c4f0f5 Mon Sep 17 00:00:00 2001 From: Brian Pow Date: Mon, 5 Feb 2018 00:30:40 +0800 Subject: [PATCH] CHG: 'hf mf dump' now use hf-mf-UID-key.bin and hf-mf-UID-data.bin instead of dumpkey.bin and dumpdata.bin as default name --- client/cmdhfmf.c | 107 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 29 deletions(-) diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index a95b3d7b9..997cf1275 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -82,7 +82,7 @@ int usage_hf14_nested(void){ PrintAndLog(" h this help"); PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, - 1K"); PrintAndLog(" t transfer keys into emulator memory"); - PrintAndLog(" d write keys to binary file `dumpkeys.bin`"); + PrintAndLog(" d write keys to binary file `hf-mf--key.bin`"); PrintAndLog(" "); PrintAndLog("samples:"); PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF "); @@ -340,6 +340,43 @@ int usage_hf14_nack(void) { return 0; } +int GetHFMF14AUID(uint8_t *uid) { + UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}}; + clearCommandBuffer(); + SendCommand(&c); + UsbCommand resp; + if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { + //if (!silent) + PrintAndLog("iso14443a card select failed"); + DropField(); + return 0; + } + + iso14a_card_select_t card; + memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t)); + memcpy(uid, card.uid, card.uidlen * sizeof(uint8_t)); + return 1; +} + +char * GenerateFilename(const char *prefix, const char *suffix){ + uint8_t uid[4] = {0,0,0,0}; + int len=0; + char * fptr = malloc (sizeof (char) * (strlen(prefix) + strlen(suffix)) + sizeof(uid)*2 + 1); + + if (!GetHFMF14AUID(uid)) { + PrintAndLog("No tag found."); + return NULL; + } + + strcpy(fptr, prefix); + len=strlen(fptr); + for (int i=0;i 1 || cmdp == 'h' || cmdp == 'H') return usage_hf14_restore(); - - if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) { - PrintAndLog("Could not find file dumpkeys.bin"); + + filename=GenerateFilename("hf-mf-","-key.bin"); + if ((fkeys = fopen(filename,"rb")) == NULL) { + PrintAndLog("Could not find file %s",fkeys); return 1; } @@ -752,7 +796,7 @@ int CmdHF14AMfRestore(const char *Cmd) { for (sectorNo = 0; sectorNo < numSectors; sectorNo++) { bytes_read = fread( keyA[sectorNo], 1, 6, fkeys ); if ( bytes_read != 6) { - PrintAndLog("File reading error (dumpkeys.bin)."); + PrintAndLog("File reading error (%s).", filename); fclose(fkeys); return 2; } @@ -761,7 +805,7 @@ int CmdHF14AMfRestore(const char *Cmd) { for (sectorNo = 0; sectorNo < numSectors; sectorNo++) { bytes_read = fread( keyB[sectorNo], 1, 6, fkeys ); if ( bytes_read != 6) { - PrintAndLog("File reading error (dumpkeys.bin)."); + PrintAndLog("File reading error (%s).", filename); fclose(fkeys); return 2; } @@ -769,11 +813,12 @@ int CmdHF14AMfRestore(const char *Cmd) { fclose(fkeys); - if ((fdump = fopen("dumpdata.bin","rb")) == NULL) { - PrintAndLog("Could not find file dumpdata.bin"); + filename=GenerateFilename("hf-mf-","-data.bin"); + if ((fdump = fopen(filename,"rb")) == NULL) { + PrintAndLog("Could not find file %s", filename); return 1; } - PrintAndLog("Restoring dumpdata.bin to card"); + PrintAndLog("Restoring %s to card", filename); for (sectorNo = 0; sectorNo < numSectors; sectorNo++) { for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) { @@ -781,7 +826,7 @@ int CmdHF14AMfRestore(const char *Cmd) { memcpy(c.d.asBytes, key, 6); bytes_read = fread(bldata, 1, 16, fdump); if ( bytes_read != 16) { - PrintAndLog("File reading error (dumpdata.bin)."); + PrintAndLog("File reading error (%s).", filename); fclose(fdump); fdump = NULL; return 2; @@ -838,7 +883,7 @@ int CmdHF14AMfNested(const char *Cmd) { FILE *fkeys; uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - + char *fptr; if (strlen(Cmd)<3) return usage_hf14_nested(); char cmdp, ctmp; @@ -1031,14 +1076,14 @@ int CmdHF14AMfNested(const char *Cmd) { // Create dump file if (createDumpFile) { - - if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { - PrintAndLog("[!] could not create file dumpkeys.bin"); + fptr = GenerateFilename("hf-mf-","-key.bin"); + if ((fkeys = fopen(fptr, "wb")) == NULL) { + PrintAndLog("[!] could not create file %s", fptr); free(e_sector); return 1; } - PrintAndLog("[+] saving keys to binary file dumpkeys.bin..."); + PrintAndLog("[+] saving keys to binary file %s...", fptr); for (i=0; i