From cefad274871e5aa2a7bc32e3308f2ba764e53666 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 30 Jul 2021 20:10:21 +0300 Subject: [PATCH] app select works --- client/src/cmdhfmfdes.c | 34 +++++++++++++++++++++++++++------ client/src/mifare/desfirecore.c | 8 ++++---- client/src/mifare/desfirecore.h | 2 +- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index f8d00fd7e..325afe0f5 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -3053,6 +3053,7 @@ static int CmdHF14ADesSelectApp(const char *Cmd) { arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), arg_str0(NULL, "aid", "", "Application ID of application for some parameters (3 hex bytes, big endian)"), + arg_str0(NULL, "dfname", "", "Application DF Name (string, max 16 chars). Selects application via ISO SELECT command"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); @@ -3068,18 +3069,39 @@ static int CmdHF14ADesSelectApp(const char *Cmd) { CLIParserFree(ctx); return res; } + + uint8_t dfname[32] = {0}; + int dfnamelen = 16; + CLIGetStrWithReturn(ctx, 12, dfname, &dfnamelen); SetAPDULogging(APDULogging); CLIParserFree(ctx); - res = DesfireSelectAndAuthenticateEx(&dctx, securechann, appid, true, verbose); - if (res != PM3_SUCCESS) { - DropField(); - PrintAndLogEx(FAILED, "Select application 0x%06x " _RED_("failed") " ", appid); - return res; + if (dfnamelen > 0) { // dctx.cmdSet == DCCISO ? + uint8_t resp[250] = {0}; + size_t resplen = 0; + res = DesfireISOSelect(&dctx, (char *)dfname, resp, &resplen); + if (res != PM3_SUCCESS) { + DropField(); + PrintAndLogEx(FAILED, "ISO Select application " _RED_("failed")); + return res; + } + + if (resplen > 0) + PrintAndLogEx(FAILED, "Application " _CYAN_("FCI template") " [%zu]%s", resplen, sprint_hex(resp, resplen)); + + PrintAndLogEx(SUCCESS, "Application `%s` selected " _GREEN_("succesfully") " ", (char *)dfname); + } else { + res = DesfireSelectAndAuthenticateEx(&dctx, securechann, appid, true, verbose); + if (res != PM3_SUCCESS) { + DropField(); + PrintAndLogEx(FAILED, "Select application 0x%06x " _RED_("failed") " ", appid); + return res; + } + + PrintAndLogEx(SUCCESS, "Application 0x%06x selected " _GREEN_("succesfully") " ", appid); } - PrintAndLogEx(SUCCESS, "Application 0x%06x selected " _GREEN_("succesfully") " ", appid); DropField(); return res; diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 07279f6cf..e3b4be02e 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -2175,14 +2175,14 @@ int DesfireSetConfiguration(DesfireContext *dctx, uint8_t paramid, uint8_t *para return res; } -int DesfireISOSelect(DesfireContext *dctx, bool sel_by_df_name, uint8_t *id, uint8_t idlen, uint8_t *resp, size_t *resplen) { +int DesfireISOSelect(DesfireContext *dctx, char *dfname, uint8_t *resp, size_t *resplen) { sAPDU apdu = {0}; apdu.CLA = 0x00; apdu.INS = ISO7816_SELECT_FILE; - apdu.P1 = (sel_by_df_name) ? 0x04 : 0x00; + apdu.P1 = 0x04; apdu.P2 = 0x00; - apdu.Lc = idlen; - apdu.data = id; + apdu.Lc = strnlen(dfname, 16); + apdu.data = (uint8_t *)dfname; uint16_t sw = 0; int res = DesfireExchangeISO(true, dctx, apdu, APDU_INCLUDE_LE_00, resp, resplen, &sw); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 16d4be8d3..2f1edfe13 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -167,7 +167,7 @@ int DesfireReadRecords(DesfireContext *dctx, uint8_t fnum, uint32_t recnum, uint int DesfireWriteRecord(DesfireContext *dctx, uint8_t fnum, uint32_t offset, uint32_t len, uint8_t *data); int DesfireUpdateRecord(DesfireContext *dctx, uint8_t fnum, uint32_t recnum, uint32_t offset, uint32_t len, uint8_t *data); -int DesfireISOSelect(DesfireContext *dctx, bool sel_by_df_name, uint8_t *id, uint8_t idlen, uint8_t *resp, size_t *resplen); +int DesfireISOSelect(DesfireContext *dctx, char *dfname, uint8_t *resp, size_t *resplen); int DesfireISOGetChallenge(DesfireContext *dctx, DesfireCryptoAlgorythm keytype, uint8_t *resp, size_t *resplen); int DesfireISOExternalAuth(DesfireContext *dctx, uint8_t keynum, DesfireCryptoAlgorythm keytype); int DesfireISOInternalAuth(DesfireContext *dctx, uint8_t keynum, DesfireCryptoAlgorythm keytype, uint8_t *data, uint8_t *resp, size_t *resplen);