From 1677df863db73a25ee7347b95889882d59c17c0a Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 19 Aug 2021 15:03:19 +0300 Subject: [PATCH] get file id / iso id works --- client/src/cmdhfmfdes.c | 33 ++++++++++++++++----------- client/src/mifare/desfirecore.c | 6 ++++- client/src/mifare/desfirecore.h | 1 + client/src/mifare/desfiresecurechan.c | 4 ++++ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index b9f18f7ab..5d3a4c2a0 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1984,7 +1984,7 @@ static int CmdHF14ADesAuth(const char *Cmd) { SetAPDULogging(APDULogging); CLIParserFree(ctx); - res = DesfireSelectAndAuthenticateW(&dctx, securechann, selectway, id, false, 0, false, verbose); + 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)); @@ -3106,6 +3106,7 @@ static int CmdHF14ADesGetFileIDs(const char *Cmd) { arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2/lrp"), arg_str0(NULL, "aid", "", "Application ID (3 hex bytes, big endian)"), + arg_str0(NULL, "appisoid", "", "Application ISO ID (ISO DF ID) (2 hex bytes, big endian). Works only for ISO read commands."), arg_lit0(NULL, "no-auth", "execute without authentication"), arg_param_end }; @@ -3113,12 +3114,13 @@ static int CmdHF14ADesGetFileIDs(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; @@ -3127,9 +3129,10 @@ static int CmdHF14ADesGetFileIDs(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 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); return res; } @@ -3148,7 +3151,7 @@ static int CmdHF14ADesGetFileIDs(const char *Cmd) { for (int i = 0; i < buflen; i++) PrintAndLogEx(INFO, "File ID: %02x", buf[i]); } else { - PrintAndLogEx(INFO, "There is no files in the application %06x", appid); + PrintAndLogEx(INFO, "There is no files in the application %06x", id); } DropField(); @@ -3160,7 +3163,8 @@ static int CmdHF14ADesGetFileISOIDs(const char *Cmd) { CLIParserInit(&ctx, "hf mfdes getfileisoids", "Get File IDs list from card. Master key needs to be provided or flag --no-auth set.", "hf mfdes getfileisoids --aid 123456 -> execute with defaults from `default` command\n" - "hf mfdes getfileisoids -n 0 -t des -k 0000000000000000 -f none --aid 123456 -> execute with default factory setup"); + "hf mfdes getfileisoids -n 0 -t des -k 0000000000000000 -f none --aid 123456 -> execute with default factory setup\n" + "hf mfdes getfileisoids --appisoid df01 -> get file iso ids from Desfire Light with factory card settings"); void *argtable[] = { arg_param_begin, @@ -3175,6 +3179,7 @@ static int CmdHF14ADesGetFileISOIDs(const char *Cmd) { arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2/lrp"), arg_str0(NULL, "aid", "", "Application ID (3 hex bytes, big endian)"), + arg_str0(NULL, "appisoid", "", "Application ISO ID (ISO DF ID) (2 hex bytes, big endian). Works only for ISO read commands."), arg_lit0(NULL, "no-auth", "execute without authentication"), arg_param_end }; @@ -3182,12 +3187,13 @@ static int CmdHF14ADesGetFileISOIDs(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; @@ -3196,9 +3202,10 @@ static int CmdHF14ADesGetFileISOIDs(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 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); return res; } @@ -3215,9 +3222,9 @@ static int CmdHF14ADesGetFileISOIDs(const char *Cmd) { if (buflen > 1) { PrintAndLogEx(INFO, "---- " _CYAN_("File ISO ID list") " ----"); for (int i = 0; i < buflen; i += 2) - PrintAndLogEx(INFO, "File ID: %02x%02x", buf[i], buf[i + 1]); + PrintAndLogEx(INFO, "File ID: %04x", MemLeToUint2byte(&buf[i])); } else { - PrintAndLogEx(INFO, "There is no files in the application %06x", appid); + PrintAndLogEx(INFO, "There is no files in the application %06x", id); } DropField(); diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 0daafd483..934cf38ef 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1029,6 +1029,10 @@ int DesfireSelectAndAuthenticateW(DesfireContext *dctx, DesfireSecureChannel sec return PM3_SUCCESS; } +int DesfireSelectAndAuthenticateAppW(DesfireContext *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool noauth, bool verbose) { + return DesfireSelectAndAuthenticateW(dctx, secureChannel, way, id, false, 0, noauth, verbose); +} + int DesfireSelectAndAuthenticateISO(DesfireContext *dctx, DesfireSecureChannel secureChannel, bool useaid, uint32_t aid, uint16_t isoappid, bool selectfile, uint16_t isofileid, bool noauth, bool verbose) { return DesfireSelectAndAuthenticateW(dctx, secureChannel, useaid ? ISW6bAID : ISWIsoID, useaid ? aid : isoappid, selectfile, isofileid, noauth, verbose); } @@ -2943,7 +2947,7 @@ int DesfireSelectEx(DesfireContext *ctx, bool fieldon, DesfireISOSelectWay way, return DesfireSelectAIDHexNoFieldOn(ctx, id); } else if (way == ISWIsoID) { uint8_t data[2] = {0}; - Uint2byteToMemLe(data, id); + Uint2byteToMemBe(data, id); return DesfireISOSelectEx(ctx, fieldon, ISSMFDFEF, data, 2, resp, &resplen); } else if (way == ISWDFName) { return DesfireISOSelect(ctx, ISSMFDFEF, NULL, 0, resp, &resplen); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 300ebe8d6..a72cc000a 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -179,6 +179,7 @@ const char *DesfireAuthErrorToStr(int error); int DesfireSelectAndAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool verbose); int DesfireSelectAndAuthenticateEx(DesfireContext *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool noauth, bool verbose); int DesfireSelectAndAuthenticateW(DesfireContext *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool selectfile, uint16_t isofileid, bool noauth, bool verbose); +int DesfireSelectAndAuthenticateAppW(DesfireContext *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool noauth, bool verbose); int DesfireSelectAndAuthenticateISO(DesfireContext *dctx, DesfireSecureChannel secureChannel, bool useaid, uint32_t aid, uint16_t isoappid, bool selectfile, uint16_t isofileid, bool noauth, bool verbose); int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel, bool verbose); void DesfireCheckAuthCommands(uint32_t appAID, char *dfname, uint8_t keyNum, AuthCommandsChk *authCmdCheck); diff --git a/client/src/mifare/desfiresecurechan.c b/client/src/mifare/desfiresecurechan.c index 8befbdb2f..a52118547 100644 --- a/client/src/mifare/desfiresecurechan.c +++ b/client/src/mifare/desfiresecurechan.c @@ -141,6 +141,10 @@ static const AllowedChannelModesS AllowedChannelModes[] = { // LRP channel separately {MFDES_AUTHENTICATE_EV2F, DACLRP, DCCNative, DCMPlain}, {MFDES_AUTHENTICATE_EV2NF, DACLRP, DCCNative, DCMPlain}, + + {MFDES_GET_FILE_IDS, DACLRP, DCCNative, DCMMACed}, + {MFDES_GET_ISOFILE_IDS, DACLRP, DCCNative, DCMMACed}, + {MFDES_GET_FILE_SETTINGS, DACLRP, DCCNative, DCMMACed}, }; #define CMD_HEADER_LEN_ALL 0xffff