return codes

This commit is contained in:
iceman1001 2019-05-27 09:15:53 -04:00
commit 548d6429ed
2 changed files with 86 additions and 84 deletions

View file

@ -28,10 +28,10 @@
#define arg_strx1(shortopts, longopts, datatype, glossary) (arg_strn((shortopts), (longopts), (datatype), 1, 250, (glossary))) #define arg_strx1(shortopts, longopts, datatype, glossary) (arg_strn((shortopts), (longopts), (datatype), 1, 250, (glossary)))
#define arg_strx0(shortopts, longopts, datatype, glossary) (arg_strn((shortopts), (longopts), (datatype), 0, 250, (glossary))) #define arg_strx0(shortopts, longopts, datatype, glossary) (arg_strn((shortopts), (longopts), (datatype), 0, 250, (glossary)))
#define CLIExecWithReturn(cmd, atbl, ifempty) if (CLIParserParseString(cmd, atbl, arg_getsize(atbl), ifempty)){CLIParserFree();return 0;} #define CLIExecWithReturn(cmd, atbl, ifempty) if (CLIParserParseString(cmd, atbl, arg_getsize(atbl), ifempty)){CLIParserFree();return PM3_ESOFT;}
#define CLIGetHexBLessWithReturn(paramnum, data, datalen, delta) if (CLIParamHexToBuf(arg_get_str(paramnum), data, sizeof(data) - (delta), datalen)) {CLIParserFree();return 1;} #define CLIGetHexBLessWithReturn(paramnum, data, datalen, delta) if (CLIParamHexToBuf(arg_get_str(paramnum), data, sizeof(data) - (delta), datalen)) {CLIParserFree();return PM3_ESOFT;}
#define CLIGetHexWithReturn(paramnum, data, datalen) if (CLIParamHexToBuf(arg_get_str(paramnum), data, sizeof(data), datalen)) {CLIParserFree();return 1;} #define CLIGetHexWithReturn(paramnum, data, datalen) if (CLIParamHexToBuf(arg_get_str(paramnum), data, sizeof(data), datalen)) {CLIParserFree();return PM3_ESOFT;}
#define CLIGetStrWithReturn(paramnum, data, datalen) if (CLIParamStrToBuf(arg_get_str(paramnum), data, sizeof(data), datalen)) {CLIParserFree();return 1;} #define CLIGetStrWithReturn(paramnum, data, datalen) if (CLIParamStrToBuf(arg_get_str(paramnum), data, sizeof(data), datalen)) {CLIParserFree();return PM3_ESOFT;}
int CLIParserInit(const char *vprogramName, const char *vprogramHint, const char *vprogramHelp); int CLIParserInit(const char *vprogramName, const char *vprogramHint, const char *vprogramHelp);
int CLIParserParseString(const char *str, void *vargtable[], size_t vargtableLen, bool allowEmptyExec); int CLIParserParseString(const char *str, void *vargtable[], size_t vargtableLen, bool allowEmptyExec);

View file

@ -1781,7 +1781,7 @@ out:
free(keyBlock); free(keyBlock);
free(e_sector); free(e_sector);
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfChk(const char *Cmd) { static int CmdHF14AMfChk(const char *Cmd) {
@ -1808,7 +1808,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
int i, keycnt = 0; int i, keycnt = 0;
keyBlock = calloc(MIFARE_DEFAULTKEYS_SIZE, 6); keyBlock = calloc(MIFARE_DEFAULTKEYS_SIZE, 6);
if (keyBlock == NULL) return 1; if (keyBlock == NULL) return PM3_EMALLOC;
for (int cnt = 0; cnt < MIFARE_DEFAULTKEYS_SIZE; cnt++) for (int cnt = 0; cnt < MIFARE_DEFAULTKEYS_SIZE; cnt++)
num_to_bytes(g_mifare_default_keys[cnt], 6, (uint8_t *)(keyBlock + cnt * 6)); num_to_bytes(g_mifare_default_keys[cnt], 6, (uint8_t *)(keyBlock + cnt * 6));
@ -1837,7 +1837,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
default: default:
PrintAndLogEx(FAILED, "Key type must be A , B or ?"); PrintAndLogEx(FAILED, "Key type must be A , B or ?");
free(keyBlock); free(keyBlock);
return 1; return PM3_ESOFT;
}; };
} }
@ -1858,7 +1858,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
if (!p) { if (!p) {
PrintAndLogEx(FAILED, "cannot allocate memory for Keys"); PrintAndLogEx(FAILED, "cannot allocate memory for Keys");
free(keyBlock); free(keyBlock);
return 2; return PM3_EMALLOC;
} }
keyBlock = p; keyBlock = p;
} }
@ -1903,7 +1903,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
PrintAndLogEx(FAILED, "Cannot allocate memory for defKeys"); PrintAndLogEx(FAILED, "Cannot allocate memory for defKeys");
free(keyBlock); free(keyBlock);
fclose(f); fclose(f);
return 2; return PM3_EMALLOC;
} }
keyBlock = p; keyBlock = p;
} }
@ -1930,7 +1930,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
e_sector = calloc(SectorsCnt, sizeof(sector_t)); e_sector = calloc(SectorsCnt, sizeof(sector_t));
if (e_sector == NULL) { if (e_sector == NULL) {
free(keyBlock); free(keyBlock);
return 1; return PM3_EMALLOC;
} }
// empty e_sector // empty e_sector
@ -2048,13 +2048,12 @@ out:
SendCommandNG(CMD_PING, NULL, 0); SendCommandNG(CMD_PING, NULL, 0);
WaitForResponseTimeout(CMD_PING, NULL, 1000); WaitForResponseTimeout(CMD_PING, NULL, 1000);
if (createDumpFile) { if (createDumpFile) {
fptr = GenerateFilename("hf-mf-", "-key.bin"); fptr = GenerateFilename("hf-mf-", "-key.bin");
if (fptr == NULL) { if (fptr == NULL) {
free(keyBlock); free(keyBlock);
free(e_sector); free(e_sector);
return 1; return PM3_EFILE;
} }
FILE *fkeys = fopen(fptr, "wb"); FILE *fkeys = fopen(fptr, "wb");
@ -2062,7 +2061,7 @@ out:
PrintAndLogEx(WARNING, "Could not create file " _YELLOW_("%s"), fptr); PrintAndLogEx(WARNING, "Could not create file " _YELLOW_("%s"), fptr);
free(keyBlock); free(keyBlock);
free(e_sector); free(e_sector);
return 1; return PM3_EFILE;
} }
PrintAndLogEx(INFO, "Printing keys to binary file " _YELLOW_("%s")"...", fptr); PrintAndLogEx(INFO, "Printing keys to binary file " _YELLOW_("%s")"...", fptr);
@ -2081,7 +2080,7 @@ out:
free(keyBlock); free(keyBlock);
free(e_sector); free(e_sector);
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
return 0; return PM3_SUCCESS;
} }
sector_t *k_sector = NULL; sector_t *k_sector = NULL;
@ -2279,7 +2278,7 @@ static int CmdHF14AMfSim(const char *Cmd) {
} }
showSectorTable(); showSectorTable();
} }
return 0; return PM3_SUCCESS;
} }
/* /*
static int CmdHF14AMfSniff(const char *Cmd) { static int CmdHF14AMfSniff(const char *Cmd) {
@ -2439,7 +2438,7 @@ static int CmdHF14AMfSniff(const char *Cmd) {
} // while (true) } // while (true)
free(buf); free(buf);
return 0; return PM3_SUCCESS;
} }
*/ */
int CmdHF14AMfDbg(const char *Cmd) { int CmdHF14AMfDbg(const char *Cmd) {
@ -2451,7 +2450,7 @@ int CmdHF14AMfDbg(const char *Cmd) {
if (dbgMode > 4) return usage_hf14_dbg(); if (dbgMode > 4) return usage_hf14_dbg();
SendCommandNG(CMD_MIFARE_SET_DBGMODE, &dbgMode, 1); SendCommandNG(CMD_MIFARE_SET_DBGMODE, &dbgMode, 1);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfKeyBrute(const char *Cmd) { static int CmdHF14AMfKeyBrute(const char *Cmd) {
@ -2482,7 +2481,7 @@ static int CmdHF14AMfKeyBrute(const char *Cmd) {
t1 = msclock() - t1; t1 = msclock() - t1;
PrintAndLogEx(SUCCESS, "\ntime in keybrute: %.0f seconds\n", (float)t1 / 1000.0); PrintAndLogEx(SUCCESS, "\ntime in keybrute: %.0f seconds\n", (float)t1 / 1000.0);
return 0; return PM3_SUCCESS;
} }
void printKeyTable(uint8_t sectorscnt, sector_t *e_sector) { void printKeyTable(uint8_t sectorscnt, sector_t *e_sector) {
@ -2602,14 +2601,14 @@ int CmdHF14AMfELoad(const char *Cmd) {
int res = loadFileEML(filename, data, &datalen); int res = loadFileEML(filename, data, &datalen);
if (res) { if (res) {
free(data); free(data);
return 1; return PM3_EFILE;
} }
// 64 or 256 blocks. // 64 or 256 blocks.
if ((datalen % blockWidth) != 0) { if ((datalen % blockWidth) != 0) {
PrintAndLogEx(FAILED, "File content error. Size doesn't match blockwidth "); PrintAndLogEx(FAILED, "File content error. Size doesn't match blockwidth ");
free(data); free(data);
return 2; return PM3_ESOFT;
} }
// convert old mfu format to new // convert old mfu format to new
@ -2636,7 +2635,7 @@ int CmdHF14AMfELoad(const char *Cmd) {
if (mfEmlSetMem_xt(data + counter, blockNum, 1, blockWidth)) { if (mfEmlSetMem_xt(data + counter, blockNum, 1, blockWidth)) {
PrintAndLogEx(FAILED, "Cant set emul block: %3d", blockNum); PrintAndLogEx(FAILED, "Cant set emul block: %3d", blockNum);
free(data); free(data);
return 3; return PM3_ESOFT;
} }
printf("."); printf(".");
fflush(stdout); fflush(stdout);
@ -2652,18 +2651,18 @@ int CmdHF14AMfELoad(const char *Cmd) {
if ((blockNum != numBlocks)) { if ((blockNum != numBlocks)) {
PrintAndLogEx(FAILED, "Warning, Ultralight/Ntag file content, Loaded %d blocks into emulator memory", blockNum); PrintAndLogEx(FAILED, "Warning, Ultralight/Ntag file content, Loaded %d blocks into emulator memory", blockNum);
free(data); free(data);
return 0; return PM3_SUCCESS;
} }
} else { } else {
if ((blockNum != numBlocks)) { if ((blockNum != numBlocks)) {
PrintAndLogEx(FAILED, "Error, file content, Only loaded %d blocks, must be %d blocks into emulator memory", blockNum, numBlocks); PrintAndLogEx(FAILED, "Error, file content, Only loaded %d blocks, must be %d blocks into emulator memory", blockNum, numBlocks);
free(data); free(data);
return 4; return PM3_SUCCESS;
} }
} }
PrintAndLogEx(SUCCESS, "Loaded %d blocks from file: " _YELLOW_("%s"), blockNum, filename); PrintAndLogEx(SUCCESS, "Loaded %d blocks from file: " _YELLOW_("%s"), blockNum, filename);
free(data); free(data);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfESave(const char *Cmd) { static int CmdHF14AMfESave(const char *Cmd) {
@ -2690,7 +2689,7 @@ static int CmdHF14AMfESave(const char *Cmd) {
dump = calloc(bytes, sizeof(uint8_t)); dump = calloc(bytes, sizeof(uint8_t));
if (!dump) { if (!dump) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return 1; return PM3_EMALLOC;
} }
memset(dump, 0, bytes); memset(dump, 0, bytes);
@ -2698,7 +2697,7 @@ static int CmdHF14AMfESave(const char *Cmd) {
if (!GetFromDevice(BIG_BUF_EML, dump, bytes, 0, NULL, 2500, false)) { if (!GetFromDevice(BIG_BUF_EML, dump, bytes, 0, NULL, 2500, false)) {
PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); PrintAndLogEx(WARNING, "Fail, transfer from device time-out");
free(dump); free(dump);
return 2; return PM3_ETIMEOUT;
} }
len = param_getstr(Cmd, nameParamNo, filename, sizeof(filename)); len = param_getstr(Cmd, nameParamNo, filename, sizeof(filename));
@ -2714,7 +2713,7 @@ static int CmdHF14AMfESave(const char *Cmd) {
saveFileEML(filename, dump, bytes, MFBLOCK_SIZE); saveFileEML(filename, dump, bytes, MFBLOCK_SIZE);
saveFileJSON(filename, jsfCardMemory, dump, bytes); saveFileJSON(filename, jsfCardMemory, dump, bytes);
free(dump); free(dump);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfECFill(const char *Cmd) { static int CmdHF14AMfECFill(const char *Cmd) {
@ -2727,7 +2726,7 @@ static int CmdHF14AMfECFill(const char *Cmd) {
if (c != 'a' && c != 'b') { if (c != 'a' && c != 'b') {
PrintAndLogEx(WARNING, "Key type must be A or B"); PrintAndLogEx(WARNING, "Key type must be A or B");
return 1; return PM3_ESOFT;
} }
if (c != 'a') if (c != 'a')
keyType = 1; keyType = 1;
@ -2743,7 +2742,7 @@ static int CmdHF14AMfECFill(const char *Cmd) {
PrintAndLogEx(NORMAL, "--params: numSectors: %d, keyType: %c\n", numSectors, (keyType == 0) ? 'A' : 'B'); PrintAndLogEx(NORMAL, "--params: numSectors: %d, keyType: %c\n", numSectors, (keyType == 0) ? 'A' : 'B');
clearCommandBuffer(); clearCommandBuffer();
SendCommandMIX(CMD_MIFARE_EML_CARDLOAD, numSectors, keyType, 0, NULL, 0); SendCommandMIX(CMD_MIFARE_EML_CARDLOAD, numSectors, keyType, 0, NULL, 0);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfEKeyPrn(const char *Cmd) { static int CmdHF14AMfEKeyPrn(const char *Cmd) {
@ -2767,7 +2766,7 @@ static int CmdHF14AMfEKeyPrn(const char *Cmd) {
PrintAndLogEx(NORMAL, "|sec|key A |key B |"); PrintAndLogEx(NORMAL, "|sec|key A |key B |");
PrintAndLogEx(NORMAL, "|---|----------------|----------------|"); PrintAndLogEx(NORMAL, "|---|----------------|----------------|");
for (i = 0; i < numSectors; i++) { for (i = 0; i < numSectors; i++) {
if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) { if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1) != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1); PrintAndLogEx(WARNING, "error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
break; break;
} }
@ -2776,7 +2775,7 @@ static int CmdHF14AMfEKeyPrn(const char *Cmd) {
PrintAndLogEx(NORMAL, "|%03d| %012" PRIx64 " | %012" PRIx64 " |", i, keyA, keyB); PrintAndLogEx(NORMAL, "|%03d| %012" PRIx64 " | %012" PRIx64 " |", i, keyA, keyB);
} }
PrintAndLogEx(NORMAL, "|---|----------------|----------------|"); PrintAndLogEx(NORMAL, "|---|----------------|----------------|");
return 0; return PM3_SUCCESS;
} }
// CHINESE MAGIC COMMANDS // CHINESE MAGIC COMMANDS
@ -2808,12 +2807,12 @@ static int CmdHF14AMfCSetUID(const char *Cmd) {
if (param_getchar(Cmd, argi)) { if (param_getchar(Cmd, argi)) {
if (param_gethex(Cmd, argi, atqa, 4)) { if (param_gethex(Cmd, argi, atqa, 4)) {
PrintAndLogEx(WARNING, "ATQA must include 4 HEX symbols"); PrintAndLogEx(WARNING, "ATQA must include 4 HEX symbols");
return 1; return PM3_ESOFT;
} }
argi++; argi++;
if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) { if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {
PrintAndLogEx(WARNING, "SAK must include 2 HEX symbols"); PrintAndLogEx(WARNING, "SAK must include 2 HEX symbols");
return 1; return PM3_ESOFT;
} }
argi++; argi++;
} else } else
@ -2832,12 +2831,12 @@ static int CmdHF14AMfCSetUID(const char *Cmd) {
res = mfCSetUID(uid, (atqaPresent) ? atqa : NULL, (atqaPresent) ? sak : NULL, oldUid, wipeCard); res = mfCSetUID(uid, (atqaPresent) ? atqa : NULL, (atqaPresent) ? sak : NULL, oldUid, wipeCard);
if (res) { if (res) {
PrintAndLogEx(WARNING, "Can't set UID. error=%d", res); PrintAndLogEx(WARNING, "Can't set UID. error=%d", res);
return 1; return PM3_ESOFT;
} }
PrintAndLogEx(SUCCESS, "old UID:%s", sprint_hex(oldUid, 4)); PrintAndLogEx(SUCCESS, "old UID:%s", sprint_hex(oldUid, 4));
PrintAndLogEx(SUCCESS, "new UID:%s", sprint_hex(uid, 4)); PrintAndLogEx(SUCCESS, "new UID:%s", sprint_hex(uid, 4));
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfCSetBlk(const char *Cmd) { static int CmdHF14AMfCSetBlk(const char *Cmd) {
@ -2862,9 +2861,9 @@ static int CmdHF14AMfCSetBlk(const char *Cmd) {
res = mfCSetBlock(blockNo, block, NULL, params); res = mfCSetBlock(blockNo, block, NULL, params);
if (res) { if (res) {
PrintAndLogEx(WARNING, "Can't write block. error=%d", res); PrintAndLogEx(WARNING, "Can't write block. error=%d", res);
return 1; return PM3_ESOFT;
} }
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfCLoad(const char *Cmd) { static int CmdHF14AMfCLoad(const char *Cmd) {
@ -2900,17 +2899,22 @@ static int CmdHF14AMfCLoad(const char *Cmd) {
if (mfCSetBlock(blockNum, buf8, NULL, flags)) { if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
PrintAndLogEx(WARNING, "Cant set magic card block: %d", blockNum); PrintAndLogEx(WARNING, "Cant set magic card block: %d", blockNum);
return 3; return PM3_ESOFT;
} }
printf("."); printf(".");
fflush(stdout); fflush(stdout);
} }
PrintAndLogEx(NORMAL, "\n"); PrintAndLogEx(NORMAL, "\n");
return 0; return PM3_SUCCESS;
} }
size_t maxdatalen = 4096; size_t maxdatalen = 4096;
uint8_t *data = calloc(maxdatalen, sizeof(uint8_t)); uint8_t *data = calloc(maxdatalen, sizeof(uint8_t));
if (!data) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
size_t datalen = 0; size_t datalen = 0;
int res = 0; int res = 0;
if (fillFromBin) { if (fillFromBin) {
@ -2926,17 +2930,14 @@ static int CmdHF14AMfCLoad(const char *Cmd) {
if (res) { if (res) {
if (data) if (data)
free(data); free(data);
return 1; return PM3_EFILE;
} }
// PrintAndLogEx(INFO, "DATA | %s", sprint_hex(data+1000, 24) );
// 64 or 256blocks. // 64 or 256blocks.
if (datalen != 1024 && datalen != 4096) { if (datalen != 1024 && datalen != 4096) {
PrintAndLogEx(WARNING, "File content error. "); PrintAndLogEx(WARNING, "File content error. ");
free(data); free(data);
return 2; return PM3_EFILE;
} }
PrintAndLogEx(INFO, "Copying to magic card"); PrintAndLogEx(INFO, "Copying to magic card");
@ -2956,7 +2957,7 @@ static int CmdHF14AMfCLoad(const char *Cmd) {
if (mfCSetBlock(blockNum, data + (16 * blockNum), NULL, flags)) { if (mfCSetBlock(blockNum, data + (16 * blockNum), NULL, flags)) {
PrintAndLogEx(WARNING, "Can't set magic card block: %d", blockNum); PrintAndLogEx(WARNING, "Can't set magic card block: %d", blockNum);
free(data); free(data);
return 3; return PM3_ESOFT;
} }
datalen -= 16; datalen -= 16;
@ -2974,19 +2975,18 @@ static int CmdHF14AMfCLoad(const char *Cmd) {
if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16) { if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16) {
PrintAndLogEx(WARNING, "File content error. There must be 64 blocks"); PrintAndLogEx(WARNING, "File content error. There must be 64 blocks");
free(data); free(data);
return 4; return PM3_EFILE;
} }
PrintAndLogEx(SUCCESS, "Card loaded %d blocks from file", blockNum); PrintAndLogEx(SUCCESS, "Card loaded %d blocks from file", blockNum);
free(data); free(data);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfCGetBlk(const char *Cmd) { static int CmdHF14AMfCGetBlk(const char *Cmd) {
uint8_t data[16] = {0}; uint8_t data[16] = {0};
uint8_t blockNo = 0; uint8_t blockNo = 0;
int res; int res;
memset(data, 0x00, sizeof(data));
char ctmp = tolower(param_getchar(Cmd, 0)); char ctmp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) < 1 || ctmp == 'h') return usage_hf14_cgetblk(); if (strlen(Cmd) < 1 || ctmp == 'h') return usage_hf14_cgetblk();
@ -2998,7 +2998,7 @@ static int CmdHF14AMfCGetBlk(const char *Cmd) {
res = mfCGetBlock(blockNo, data, MAGIC_SINGLE); res = mfCGetBlock(blockNo, data, MAGIC_SINGLE);
if (res) { if (res) {
PrintAndLogEx(WARNING, "Can't read block. error=%d", res); PrintAndLogEx(WARNING, "Can't read block. error=%d", res);
return 1; return PM3_ESOFT;
} }
PrintAndLogEx(NORMAL, "data: %s", sprint_hex(data, sizeof(data))); PrintAndLogEx(NORMAL, "data: %s", sprint_hex(data, sizeof(data)));
@ -3016,7 +3016,7 @@ static int CmdHF14AMfCGetBlk(const char *Cmd) {
PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&data[9], 1)); PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&data[9], 1));
} }
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfCGetSc(const char *Cmd) { static int CmdHF14AMfCGetSc(const char *Cmd) {
@ -3030,7 +3030,7 @@ static int CmdHF14AMfCGetSc(const char *Cmd) {
sector = param_get8(Cmd, 0); sector = param_get8(Cmd, 0);
if (sector > 39) { if (sector > 39) {
PrintAndLogEx(WARNING, "Sector number must be less then 40"); PrintAndLogEx(WARNING, "Sector number must be less then 40");
return 1; return PM3_ESOFT;
} }
PrintAndLogEx(NORMAL, "\n # | data | Sector | %02d/ 0x%02X ", sector, sector); PrintAndLogEx(NORMAL, "\n # | data | Sector | %02d/ 0x%02X ", sector, sector);
@ -3051,11 +3051,11 @@ static int CmdHF14AMfCGetSc(const char *Cmd) {
res = mfCGetBlock(start + i, data, flags); res = mfCGetBlock(start + i, data, flags);
if (res) { if (res) {
PrintAndLogEx(WARNING, "Can't read block. %d error=%d", start + i, res); PrintAndLogEx(WARNING, "Can't read block. %d error=%d", start + i, res);
return 1; return PM3_ESOFT;
} }
PrintAndLogEx(NORMAL, "%3d | %s", start + i, sprint_hex(data, 16)); PrintAndLogEx(NORMAL, "%3d | %s", start + i, sprint_hex(data, 16));
} }
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfCSave(const char *Cmd) { static int CmdHF14AMfCSave(const char *Cmd) {
@ -3119,9 +3119,8 @@ static int CmdHF14AMfCSave(const char *Cmd) {
dump = calloc(bytes, sizeof(uint8_t)); dump = calloc(bytes, sizeof(uint8_t));
if (!dump) { if (!dump) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return 1; return PM3_EMALLOC;
} }
memset(dump, 0, bytes);
flags = MAGIC_INIT + MAGIC_WUPC; flags = MAGIC_INIT + MAGIC_WUPC;
for (i = 0; i < numblocks; i++) { for (i = 0; i < numblocks; i++) {
@ -3131,7 +3130,7 @@ static int CmdHF14AMfCSave(const char *Cmd) {
if (mfCGetBlock(i, dump + (i * MFBLOCK_SIZE), flags)) { if (mfCGetBlock(i, dump + (i * MFBLOCK_SIZE), flags)) {
PrintAndLogEx(WARNING, "Cant get block: %d", i); PrintAndLogEx(WARNING, "Cant get block: %d", i);
free(dump); free(dump);
return 2; return PM3_ESOFT;
} }
} }
@ -3163,7 +3162,7 @@ static int CmdHF14AMfCSave(const char *Cmd) {
saveFileEML(filename, dump, bytes, MFBLOCK_SIZE); saveFileEML(filename, dump, bytes, MFBLOCK_SIZE);
saveFileJSON(filename, jsfCardMemory, dump, bytes); saveFileJSON(filename, jsfCardMemory, dump, bytes);
free(dump); free(dump);
return 0; return PM3_SUCCESS;
} }
//needs nt, ar, at, Data to decrypt //needs nt, ar, at, Data to decrypt
@ -3179,7 +3178,7 @@ static int CmdHf14AMfDecryptBytes(const char *Cmd) {
int len = param_getlength(Cmd, 3); int len = param_getlength(Cmd, 3);
if (len & 1) { if (len & 1) {
PrintAndLogEx(WARNING, "Uneven hex string length. LEN=%d", len); PrintAndLogEx(WARNING, "Uneven hex string length. LEN=%d", len);
return 1; return PM3_ESOFT;
} }
PrintAndLogEx(NORMAL, "nt\t%08X", nt); PrintAndLogEx(NORMAL, "nt\t%08X", nt);
@ -3187,11 +3186,16 @@ static int CmdHf14AMfDecryptBytes(const char *Cmd) {
PrintAndLogEx(NORMAL, "at enc\t%08X", at_enc); PrintAndLogEx(NORMAL, "at enc\t%08X", at_enc);
uint8_t *data = calloc(len, sizeof(uint8_t)); uint8_t *data = calloc(len, sizeof(uint8_t));
if (!data) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
param_gethex_ex(Cmd, 3, data, &len); param_gethex_ex(Cmd, 3, data, &len);
len >>= 1; len >>= 1;
tryDecryptWord(nt, ar_enc, at_enc, data, len); tryDecryptWord(nt, ar_enc, at_enc, data, len);
free(data); free(data);
return 0; return PM3_SUCCESS;
} }
static int CmdHf14AMfSetMod(const char *Cmd) { static int CmdHf14AMfSetMod(const char *Cmd) {
@ -3210,7 +3214,7 @@ static int CmdHf14AMfSetMod(const char *Cmd) {
PrintAndLogEx(NORMAL, "Usage: hf mf setmod <0|1> <block 0 key A>"); PrintAndLogEx(NORMAL, "Usage: hf mf setmod <0|1> <block 0 key A>");
PrintAndLogEx(NORMAL, " 0 = normal modulation"); PrintAndLogEx(NORMAL, " 0 = normal modulation");
PrintAndLogEx(NORMAL, " 1 = strong modulation (default)"); PrintAndLogEx(NORMAL, " 1 = strong modulation (default)");
return 1; return PM3_ESOFT;
} }
uint8_t data[7]; uint8_t data[7];
@ -3237,16 +3241,15 @@ static int CmdHf14AMfSetMod(const char *Cmd) {
// Mifare NACK bug detection // Mifare NACK bug detection
static int CmdHf14AMfNack(const char *Cmd) { static int CmdHf14AMfNack(const char *Cmd) {
bool verbose = false;
char ctmp = tolower(param_getchar(Cmd, 0)); char ctmp = tolower(param_getchar(Cmd, 0));
if (ctmp == 'h') return usage_hf14_nack(); if (ctmp == 'h') return usage_hf14_nack();
if (ctmp == 'v') verbose = true; bool verbose = (ctmp == 'v');
if (verbose) if (verbose)
PrintAndLogEx(INFO, "Started testing card for NACK bug. Press key to abort"); PrintAndLogEx(INFO, "Started testing card for NACK bug. Press key to abort");
detect_classic_nackbug(verbose); detect_classic_nackbug(verbose);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfice(const char *Cmd) { static int CmdHF14AMfice(const char *Cmd) {
@ -3284,7 +3287,7 @@ static int CmdHF14AMfice(const char *Cmd) {
default: default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'\n", ctmp); PrintAndLogEx(WARNING, "Unknown parameter '%c'\n", ctmp);
usage_hf14_ice(); usage_hf14_ice();
return 1; return PM3_ESOFT;
} }
cmdp++; cmdp++;
} }
@ -3292,15 +3295,15 @@ static int CmdHF14AMfice(const char *Cmd) {
if (filename[0] == '\0') { if (filename[0] == '\0') {
fptr = GenerateFilename("hf-mf-", "-nonces.bin"); fptr = GenerateFilename("hf-mf-", "-nonces.bin");
if (fptr == NULL) if (fptr == NULL)
return 1; return PM3_EFILE;
strcpy(filename, fptr); strcpy(filename, fptr);
} }
PrintAndLogEx(NORMAL, "Collecting %u nonces \n", limit); PrintAndLogEx(NORMAL, "Collecting "_YELLOW_("%u")"nonces \n", limit);
if ((fnonces = fopen(filename, "wb")) == NULL) { if ((fnonces = fopen(filename, "wb")) == NULL) {
PrintAndLogEx(WARNING, "Could not create file " _YELLOW_("%s"), filename); PrintAndLogEx(WARNING, "Could not create file " _YELLOW_("%s"), filename);
return 3; return PM3_EFILE;
} }
clearCommandBuffer(); clearCommandBuffer();
@ -3352,7 +3355,7 @@ out:
clearCommandBuffer(); clearCommandBuffer();
SendCommandMIX(CMD_MIFARE_ACQUIRE_NONCES, blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, 4, NULL, 0); SendCommandMIX(CMD_MIFARE_ACQUIRE_NONCES, blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, 4, NULL, 0);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfAuth4(const char *Cmd) { static int CmdHF14AMfAuth4(const char *Cmd) {
@ -3380,12 +3383,12 @@ static int CmdHF14AMfAuth4(const char *Cmd) {
if (keynlen != 2) { if (keynlen != 2) {
PrintAndLogEx(ERR, "<Key Num> must be 2 bytes long instead of: %d", keynlen); PrintAndLogEx(ERR, "<Key Num> must be 2 bytes long instead of: %d", keynlen);
return 1; return PM3_ESOFT;
} }
if (keylen != 16) { if (keylen != 16) {
PrintAndLogEx(ERR, "<Key Value> must be 16 bytes long instead of: %d", keylen); PrintAndLogEx(ERR, "<Key Value> must be 16 bytes long instead of: %d", keylen);
return 1; return PM3_ESOFT;
} }
return MifareAuth4(NULL, keyn, key, true, false, true); return MifareAuth4(NULL, keyn, key, true, false, true);
@ -3427,7 +3430,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
uint8_t sector10[16 * 4] = {0}; uint8_t sector10[16 * 4] = {0};
if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) { if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) {
PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys."); PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys.");
return 2; return PM3_ESOFT;
} }
if (verbose) { if (verbose) {
@ -3441,7 +3444,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
if (haveMAD2) { if (haveMAD2) {
if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) { if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) {
PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys."); PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys.");
return 2; return PM3_ESOFT;
} }
MAD2DecodeAndPrint(sector10, verbose); MAD2DecodeAndPrint(sector10, verbose);
@ -3455,7 +3458,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
size_t madlen = 0; size_t madlen = 0;
if (MADDecode(sector0, sector10, mad, &madlen)) { if (MADDecode(sector0, sector10, mad, &madlen)) {
PrintAndLogEx(ERR, "can't decode mad."); PrintAndLogEx(ERR, "can't decode mad.");
return 10; return PM3_ESOFT;
} }
uint8_t akey[6] = {0}; uint8_t akey[6] = {0};
@ -3470,7 +3473,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, akey, vsector)) { if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, akey, vsector)) {
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
PrintAndLogEx(ERR, "read sector %d error.", i + 1); PrintAndLogEx(ERR, "read sector %d error.", i + 1);
return 2; return PM3_ESOFT;
} }
for (int j = 0; j < (verbose ? 4 : 3); j ++) for (int j = 0; j < (verbose ? 4 : 3); j ++)
@ -3479,7 +3482,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
} }
} }
return 0; return PM3_SUCCESS;
} }
static int CmdHFMFNDEF(const char *Cmd) { static int CmdHFMFNDEF(const char *Cmd) {
@ -3530,7 +3533,7 @@ static int CmdHFMFNDEF(const char *Cmd) {
if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) { if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) {
PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys."); PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys.");
return 2; return PM3_ESOFT;
} }
bool haveMAD2 = false; bool haveMAD2 = false;
@ -3543,7 +3546,7 @@ static int CmdHFMFNDEF(const char *Cmd) {
if (haveMAD2) { if (haveMAD2) {
if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) { if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) {
PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys."); PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys.");
return 2; return PM3_ESOFT;
} }
} }
@ -3551,7 +3554,7 @@ static int CmdHFMFNDEF(const char *Cmd) {
size_t madlen = 0; size_t madlen = 0;
if (MADDecode(sector0, (haveMAD2 ? sector10 : NULL), mad, &madlen)) { if (MADDecode(sector0, (haveMAD2 ? sector10 : NULL), mad, &madlen)) {
PrintAndLogEx(ERR, "can't decode mad."); PrintAndLogEx(ERR, "can't decode mad.");
return 10; return PM3_ESOFT;
} }
printf("data reading:"); printf("data reading:");
@ -3560,7 +3563,7 @@ static int CmdHFMFNDEF(const char *Cmd) {
uint8_t vsector[16 * 4] = {0}; uint8_t vsector[16 * 4] = {0};
if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, ndefkey, vsector)) { if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, ndefkey, vsector)) {
PrintAndLogEx(ERR, "read sector %d error.", i + 1); PrintAndLogEx(ERR, "read sector %d error.", i + 1);
return 2; return PM3_ESOFT;
} }
memcpy(&data[datalen], vsector, 16 * 3); memcpy(&data[datalen], vsector, 16 * 3);
@ -3573,7 +3576,7 @@ static int CmdHFMFNDEF(const char *Cmd) {
if (!datalen) { if (!datalen) {
PrintAndLogEx(ERR, "no NDEF data."); PrintAndLogEx(ERR, "no NDEF data.");
return 11; return PM3_SUCCESS;
} }
if (verbose2) { if (verbose2) {
@ -3583,13 +3586,12 @@ static int CmdHFMFNDEF(const char *Cmd) {
NDEFDecodeAndPrint(data, datalen, verbose); NDEFDecodeAndPrint(data, datalen, verbose);
return 0; return PM3_SUCCESS;
} }
static int CmdHF14AMfList(const char *Cmd) { static int CmdHF14AMfList(const char *Cmd) {
(void)Cmd; // Cmd is not used so far (void)Cmd; // Cmd is not used so far
CmdTraceList("mf"); return CmdTraceList("mf");
return 0;
} }
static command_t CommandTable[] = { static command_t CommandTable[] = {
@ -3640,7 +3642,7 @@ static command_t CommandTable[] = {
static int CmdHelp(const char *Cmd) { static int CmdHelp(const char *Cmd) {
(void)Cmd; // Cmd is not used so far (void)Cmd; // Cmd is not used so far
CmdsHelp(CommandTable); CmdsHelp(CommandTable);
return 0; return PM3_SUCCESS;
} }
int CmdHFMF(const char *Cmd) { int CmdHFMF(const char *Cmd) {