From 32d47cb6a41792e764934b7e5a3354388f8d3955 Mon Sep 17 00:00:00 2001 From: Jean-Michel Picod Date: Fri, 4 Nov 2022 11:06:59 +0100 Subject: [PATCH] Fix prolematic return codes in mifare. Parts of the code returned positive values for error codes, which could result in the client exiting (return value 2). --- client/src/cmdhfmf.c | 20 ++++++++++---------- client/src/mifare/mad.c | 4 ++-- client/src/mifare/mifare4.c | 14 +++++++------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index d2862bdd6..11aae2f6e 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -115,14 +115,14 @@ static int GetHFMF14AUID(uint8_t *uid, int *uidlen) { if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { PrintAndLogEx(WARNING, "iso14443a card select failed"); DropField(); - return 0; + return PM3_ERFTRANS; } iso14a_card_select_t card; memcpy(&card, (iso14a_card_select_t *)resp.data.asBytes, sizeof(iso14a_card_select_t)); memcpy(uid, card.uid, card.uidlen * sizeof(uint8_t)); *uidlen = card.uidlen; - return 1; + return PM3_SUCCESS; } static char *GenerateFilename(const char *prefix, const char *suffix) { @@ -133,8 +133,8 @@ static char *GenerateFilename(const char *prefix, const char *suffix) { int uidlen = 0; char *fptr = calloc(sizeof(char) * (strlen(prefix) + strlen(suffix)) + sizeof(uid) * 2 + 1, sizeof(uint8_t)); - GetHFMF14AUID(uid, &uidlen); - if (!uidlen) { + int res = GetHFMF14AUID(uid, &uidlen); + if (res != PM3_SUCCESS || !uidlen) { PrintAndLogEx(WARNING, "No tag found."); free(fptr); return NULL; @@ -234,7 +234,7 @@ static bool mfc_value(const uint8_t *d, int32_t *val) { if (val) { *val = a; } - return (val_checks); + return val_checks; } static void mf_print_block(uint8_t blockno, uint8_t *d, bool verbose) { if (blockno == 0) { @@ -788,7 +788,7 @@ static int CmdHF14AMfDump(const char *Cmd) { uint64_t select_status = resp.oldarg[0]; if (select_status == 0) { PrintAndLogEx(WARNING, "iso14443a card select failed"); - return select_status; + return PM3_SUCCESS; } // store card info @@ -4907,7 +4907,7 @@ static int CmdHF14AMfCSave(const char *Cmd) { uint64_t select_status = resp.oldarg[0]; if (select_status == 0) { PrintAndLogEx(WARNING, "iso14443a card select failed"); - return select_status; + return PM3_SUCCESS; } // store card info @@ -5058,7 +5058,7 @@ static int CmdHF14AMfCView(const char *Cmd) { if (select_status == 0) { PrintAndLogEx(WARNING, "iso14443a card select failed"); - return select_status; + return PM3_ERFTRANS; } iso14a_card_select_t card; @@ -5826,7 +5826,7 @@ int CmdHFMFNDEFFormat(const char *Cmd) { uint64_t select_status = resp.oldarg[0]; if (select_status == 0) { PrintAndLogEx(WARNING, "iso14443a card select failed"); - return select_status; + return PM3_SUCCESS; } DropField(); @@ -6834,7 +6834,7 @@ static int CmdHF14AGen4View(const char *Cmd) { if (select_status == 0) { PrintAndLogEx(WARNING, "iso14443a card select failed"); - return select_status; + return PM3_SUCCESS; } iso14a_card_select_t card; diff --git a/client/src/mifare/mad.c b/client/src/mifare/mad.c index 57888c1ad..023f3cbf6 100644 --- a/client/src/mifare/mad.c +++ b/client/src/mifare/mad.c @@ -317,7 +317,7 @@ int MAD1DecodeAndPrint(uint8_t *sector, bool swapmad, bool verbose, bool *haveMA int ibs = MADInfoByteDecode(sector, swapmad, 1, verbose); - if (ibs) { + if (ibs > 0) { PrintAndLogEx(SUCCESS, "Card publisher sector " _MAGENTA_("0x%02x"), ibs); } else { PrintAndLogEx(WARNING, "Card publisher " _RED_("not") " present " _YELLOW_("0x%02x"), ibs); @@ -360,7 +360,7 @@ int MAD2DecodeAndPrint(uint8_t *sector, bool swapmad, bool verbose) { } int ibs = MADInfoByteDecode(sector, swapmad, 2, verbose); - if (ibs) { + if (ibs > 0) { PrintAndLogEx(SUCCESS, "Card publisher sector " _MAGENTA_("0x%02x"), ibs); } else { PrintAndLogEx(WARNING, "Card publisher " _RED_("not") " present " _YELLOW_("0x%02x"), ibs); diff --git a/client/src/mifare/mifare4.c b/client/src/mifare/mifare4.c index 166408223..a1427bc9f 100644 --- a/client/src/mifare/mifare4.c +++ b/client/src/mifare/mifare4.c @@ -198,7 +198,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti if (res) { if (!silentMode) PrintAndLogEx(ERR, "Exchange raw error: %d", res); if (dropFieldIfError) DropField(); - return 2; + return PM3_ERFTRANS; } if (verbose) @@ -207,19 +207,19 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti if (datalen < 1) { if (!silentMode) PrintAndLogEx(ERR, "Card response wrong length: %d", datalen); if (dropFieldIfError) DropField(); - return 3; + return PM3_EWRONGANSWER; } if (data[0] != 0x90) { if (!silentMode) PrintAndLogEx(ERR, "Card response error: %02x %s", data[0], mfpGetErrorDescription(data[0])); if (dropFieldIfError) DropField(); - return 3; + return PM3_EWRONGANSWER; } if (datalen != 19) { // code 1b + 16b + crc 2b if (!silentMode) PrintAndLogEx(ERR, "Card response must be 19 bytes long instead of: %d", datalen); if (dropFieldIfError) DropField(); - return 3; + return PM3_EWRONGANSWER; } aes_decode(NULL, key, &data[1], RndB, 16); @@ -242,7 +242,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti if (res) { if (!silentMode) PrintAndLogEx(ERR, "Exchange raw error: %d", res); if (dropFieldIfError) DropField(); - return 4; + return PM3_ERFTRANS; } if (verbose) @@ -262,7 +262,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti PrintAndLogEx(ERR, "RndA card: %s", sprint_hex(&raw[4], 16)); } if (dropFieldIfError) DropField(); - return 5; + return PM3_EWRONGANSWER; } if (verbose) { @@ -319,7 +319,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti if (verbose) PrintAndLogEx(INFO, "Authentication OK"); - return 0; + return PM3_SUCCESS; } static int intExchangeRAW14aPlus(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {