From e55c8f969a301502ac2a182d0c56687bd548616b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 19 Aug 2021 16:16:18 +0300 Subject: [PATCH] refactoring and lsfiles --- client/src/cmdhfmfdes.c | 27 +++++++++++++++------------ client/src/mifare/desfirecore.c | 22 +++++++++++++++++----- client/src/mifare/desfirecore.h | 1 + 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index f631f6f4e..59cf3c4de 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1987,14 +1987,14 @@ static int CmdHF14ADesAuth(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } if (DesfireMFSelected(selectway, id)) PrintAndLogEx(SUCCESS, "PICC selected and authenticated " _GREEN_("succesfully")); else - PrintAndLogEx(SUCCESS, "Application %s " _CYAN_("%0*x") " selected and authenticated " _GREEN_("succesfully"), DesfireSelectWayToStr(selectway), selectway == ISW6bAID ? 6 : 4, id); + PrintAndLogEx(SUCCESS, "Application " _CYAN_("%s") " selected and authenticated " _GREEN_("succesfully"), DesfireWayIDStr(selectway, id)); PrintAndLogEx(SUCCESS, _CYAN_("Context: ")); DesfirePrintContext(&dctx); @@ -3132,7 +3132,7 @@ static int CmdHF14ADesGetFileIDs(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -3205,7 +3205,7 @@ static int CmdHF14ADesGetFileISOIDs(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -3285,7 +3285,7 @@ static int CmdHF14ADesGetFileSettings(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -3300,7 +3300,7 @@ static int CmdHF14ADesGetFileSettings(const char *Cmd) { } if (verbose) - PrintAndLogEx(INFO, "%s %0*x file %02x settings[%zu]: %s", DesfireSelectWayToStr(selectway), (selectway == ISW6bAID) ? 6 : 4, id, fileid, buflen, sprint_hex(buf, buflen)); + PrintAndLogEx(INFO, "%s file %02x settings[%zu]: %s", DesfireWayIDStr(selectway, id), fileid, buflen, sprint_hex(buf, buflen)); DesfirePrintFileSettings(buf, buflen); @@ -5121,7 +5121,8 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mfdes lsfiles", "Show file list. Master key needs to be provided or flag --no-auth set (depend on cards settings).", - "hf mfdes lsfiles --aid 123456 -> show file list for: app=123456 with defaults from `default` command"); + "hf mfdes lsfiles --aid 123456 -> show file list for: app=123456 with defaults from `default` command" + "hf mfdes lsfiles --appisoid df01 --no-auth -> show files from desfire light"); void *argtable[] = { arg_param_begin, @@ -5144,12 +5145,13 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { bool APDULogging = arg_get_lit(ctx, 1); bool verbose = arg_get_lit(ctx, 2); - bool noauth = arg_get_lit(ctx, 12); + bool noauth = arg_get_lit(ctx, 13); DesfireContext dctx; int securechann = defaultSecureChannel; - uint32_t appid = 0x000000; - int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, &securechann, DCMMACed, &appid, NULL); + uint32_t id = 0x000000; + DesfireISOSelectWay selectway = ISW6bAID; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, &securechann, DCMMACed, &id, &selectway); if (res) { CLIParserFree(ctx); return res; @@ -5158,9 +5160,10 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { SetAPDULogging(APDULogging); CLIParserFree(ctx); - res = DesfireSelectAndAuthenticateEx(&dctx, securechann, appid, noauth, verbose); + res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); + PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -5174,7 +5177,7 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { } if (filescount == 0) { - PrintAndLogEx(INFO, "There is no files in the application %06x", appid); + PrintAndLogEx(INFO, "There is no files in the %s", DesfireWayIDStr(selectway, id)); DropField(); return res; } diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 934cf38ef..5d149cb8c 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -312,6 +312,18 @@ const char *DesfireSelectWayToStr(DesfireISOSelectWay way) { return ""; } +char *DesfireWayIDStr(DesfireISOSelectWay way, uint32_t id) { + static char str[200] = {0}; + memset(str, 0, sizeof(str)); + + if (way == ISWMF || way == ISWDFName) + sprintf(str, "%s", DesfireSelectWayToStr(way)); + else + sprintf(str, "%s %0*x", DesfireSelectWayToStr(way), (way == ISW6bAID) ? 6 : 4, id); + + return str; +} + bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id) { switch (way) { case ISW6bAID: @@ -997,7 +1009,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext *dctx, DesfireSecureChannel sec return 202; } if (verbose) - PrintAndLogEx(INFO, "%s %0*x is " _GREEN_("selected"), DesfireSelectWayToStr(way), way == ISW6bAID ? 6 : 4, id); + PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id)); } if (selectfile) { @@ -1008,7 +1020,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext *dctx, DesfireSecureChannel sec } if (verbose) - PrintAndLogEx(INFO, "Application %s %04x file iso id %04x is " _GREEN_("selected"), DesfireSelectWayToStr(way), id, isofileid); + PrintAndLogEx(INFO, "Application %s file iso id %04x is " _GREEN_("selected"), DesfireWayIDStr(way, id), isofileid); } if (!noauth) { @@ -1707,7 +1719,7 @@ int DesfireFillAppList(DesfireContext *dctx, PICCInfoS *PICCInfo, AppListS appLi for (int i = 0; i < buflen; i++) { int indx = AppListSearchAID(DesfireAIDByteToUint(&buf[i * 24 + 1]), appList, PICCInfo->appCount); if (indx >= 0) { - appList[indx].appISONum = MemBeToUint2byte(&buf[i * 24 + 1 + 3]); + appList[indx].appISONum = MemLeToUint2byte(&buf[i * 24 + 1 + 3]); memcpy(appList[indx].appDFName, &buf[i * 24 + 1 + 5], strnlen((char *)&buf[i * 24 + 1 + 5], 16)); } } @@ -2002,7 +2014,7 @@ int DesfireFillFileList(DesfireContext *dctx, FileListS FileList, size_t *filesc if (buflen > 0) { for (int i = 0; i < *filescount; i++) { if (FileList[i].fileSettings.fileType != 0x02 && FileList[i].fileSettings.fileType != 0x05) { - FileList[i].fileISONum = MemBeToUint2byte(&buf[isoindx * 2]); + FileList[i].fileISONum = MemLeToUint2byte(&buf[isoindx * 2]); isoindx++; } } @@ -2624,7 +2636,7 @@ void DesfirePrintCreateFileSettings(uint8_t filetype, uint8_t *data, size_t len) size_t xlen = 1; if (ftyperec->mayHaveISOfid) { if (isoidpresent) { - PrintAndLogEx(SUCCESS, "File ISO number : 0x%04x", MemBeToUint2byte(&data[xlen])); + PrintAndLogEx(SUCCESS, "File ISO number : 0x%04x", MemLeToUint2byte(&data[xlen])); xlen += 2; } else { PrintAndLogEx(SUCCESS, "File ISO number : n/a"); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index a72cc000a..e7e1500af 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -171,6 +171,7 @@ void DesfirePrintMADAID(uint32_t appid, bool verbose); int DesfireGetCardUID(DesfireContext *ctx); const char *DesfireSelectWayToStr(DesfireISOSelectWay way); +char *DesfireWayIDStr(DesfireISOSelectWay way, uint32_t id); bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id); int DesfireSelectEx(DesfireContext *ctx, bool fieldon, DesfireISOSelectWay way, uint32_t id, char *dfname); int DesfireSelect(DesfireContext *ctx, DesfireISOSelectWay way, uint32_t id, char *dfname);