diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 6f8c09046..a83c78713 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5958,6 +5958,162 @@ static int CmdHF14ADesGetAppNames(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesGetFileIDs(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes getfileids", + "Get File IDs list from card. Master key needs to be provided or flag --no-auth set.", + "hf mfdes getfileids --aid 123456 -> execute with defaults from `default` command\n" + "hf mfdes getfileids -n 0 -t des -k 0000000000000000 -f none --aid 123456 -> execute with default factory setup"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + 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 (3 hex bytes, big endian)"), + arg_lit0(NULL, "no-auth", "execute without authentication"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + bool noauth = arg_get_lit(ctx, 12); + + DesfireContext dctx; + int securechann = defaultSecureChannel; + uint32_t appid = 0x000000; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, &securechann, DCMMACed, &appid); + if (res) { + CLIParserFree(ctx); + return res; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + if (noauth) { + res = DesfireSelectAIDHex(&dctx, appid, false, 0); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + DropField(); + return res; + } + } else { + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + + uint8_t buf[APDU_RES_LEN] = {0}; + size_t buflen = 0; + + res = DesfireGetFileIDList(&dctx, buf, &buflen); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire GetFileIDList command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + if (buflen >= 3) { + PrintAndLogEx(INFO, "---- " _CYAN_("File ID list") " ----"); + 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); + } + + DropField(); + return PM3_SUCCESS; +} + +static int CmdHF14ADesGetFileISOIDs(const char *Cmd) { + CLIParserContext *ctx; + 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"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + 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 (3 hex bytes, big endian)"), + arg_lit0(NULL, "no-auth", "execute without authentication"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + bool noauth = arg_get_lit(ctx, 12); + + DesfireContext dctx; + int securechann = defaultSecureChannel; + uint32_t appid = 0x000000; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, &securechann, DCMMACed, &appid); + if (res) { + CLIParserFree(ctx); + return res; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + if (noauth) { + res = DesfireSelectAIDHex(&dctx, appid, false, 0); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + DropField(); + return res; + } + } else { + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + + uint8_t buf[APDU_RES_LEN] = {0}; + size_t buflen = 0; + + res = DesfireGetFileISOIDList(&dctx, buf, &buflen); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire GetFileISOIDList command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + if (buflen >= 3) { + 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]); + } else { + PrintAndLogEx(INFO, "There is no files in the application %06x", appid); + } + + DropField(); + return PM3_SUCCESS; +} + static int CmdHF14ADesTest(const char *Cmd) { DesfireTest(true); return PM3_SUCCESS; @@ -5991,6 +6147,8 @@ static command_t CommandTable[] = { {"getaids", CmdHF14ADesGetAIDs, IfPm3Iso14443a, "[new]Get Application IDs list"}, {"getappnames", CmdHF14ADesGetAppNames, IfPm3Iso14443a, "[new]Get Applications list"}, {"-----------", CmdHelp, IfPm3Iso14443a, "----------------------- " _CYAN_("Files") " -----------------------"}, + {"getfileids", CmdHF14ADesGetFileIDs, IfPm3Iso14443a, "[new]Get File IDs list"}, + {"getfileisoids", CmdHF14ADesGetFileISOIDs, IfPm3Iso14443a, "[new]Get File ISO IDs list"}, {"changevalue", CmdHF14ADesChangeValue, IfPm3Iso14443a, "Write value of a value file (credit/debit/clear)"}, {"clearfile", CmdHF14ADesClearRecordFile, IfPm3Iso14443a, "Clear record File"}, {"createfile", CmdHF14ADesCreateFile, IfPm3Iso14443a, "Create Standard/Backup File"}, diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index a0611120d..a48f130d2 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1013,6 +1013,22 @@ int DesfireSetConfigurationCmd(DesfireContext *dctx, uint8_t *data, size_t len, return DesfireCommand(dctx, MFDES_CHANGE_CONFIGURATION, data, len, resp, resplen, -1); } +int DesfireGetFileIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { + return DesfireCommandRxData(dctx, MFDES_GET_FILE_IDS, resp, resplen, -1); +} + +int DesfireGetFileISOIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { + return DesfireCommandRxData(dctx, MFDES_GET_ISOFILE_IDS, resp, resplen, -1); +} + +int DesfireCreateFile(DesfireContext *dctx, uint8_t *fdata, size_t fdatalen) { + return DesfireCommandTxData(dctx, MFDES_CREATE_STD_DATA_FILE, fdata, fdatalen); +} + +int DesfireDeleteFile(DesfireContext *dctx, uint8_t fid) { + return DesfireCommandTxData(dctx, MFDES_DELETE_FILE, &fid, 1); +} + uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType) { switch (keyType) { case T_DES: diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index d96ffc54f..43b76dbd7 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -61,4 +61,9 @@ int DesfireChangeKey(DesfireContext *dctx, bool change_master_key, uint8_t newke int DesfireSetConfigurationCmd(DesfireContext *dctx, uint8_t *data, size_t len, uint8_t *resp, size_t *resplen); int DesfireSetConfiguration(DesfireContext *dctx, uint8_t paramid, uint8_t *param, size_t paramlen); +int DesfireGetFileIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); +int DesfireGetFileISOIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); +int DesfireCreateFile(DesfireContext *dctx, uint8_t *fdata, size_t fdatalen); +int DesfireDeleteFile(DesfireContext *dctx, uint8_t fid); + #endif // __DESFIRECORE_H diff --git a/client/src/mifare/desfiresecurechan.c b/client/src/mifare/desfiresecurechan.c index fdf292fca..3b9c1006e 100644 --- a/client/src/mifare/desfiresecurechan.c +++ b/client/src/mifare/desfiresecurechan.c @@ -47,6 +47,8 @@ AllowedChannelModesS AllowedChannelModes[] = { {MFDES_ROLL_KEY_SETTINGS, DACd40, DCCNative, DCMMACed}, {MFDES_COMMIT_READER_ID, DACd40, DCCNative, DCMMACed}, {MFDES_FORMAT_PICC, DACd40, DCCNative, DCMMACed}, + {MFDES_GET_FILE_IDS, DACd40, DCCNative, DCMMACed}, + {MFDES_GET_ISOFILE_IDS, DACd40, DCCNative, DCMMACed}, {MFDES_GET_UID, DACd40, DCCNative, DCMEncrypted}, {MFDES_CHANGE_KEY_SETTINGS, DACd40, DCCNative, DCMEncrypted}, @@ -65,6 +67,8 @@ AllowedChannelModesS AllowedChannelModes[] = { {MFDES_GET_DF_NAMES, DACEV1, DCCNative, DCMMACed}, {MFDES_GET_KEY_SETTINGS, DACEV1, DCCNative, DCMMACed}, {MFDES_FORMAT_PICC, DACEV1, DCCNative, DCMMACed}, + {MFDES_GET_FILE_IDS, DACEV1, DCCNative, DCMMACed}, + {MFDES_GET_ISOFILE_IDS, DACEV1, DCCNative, DCMMACed}, {MFDES_GET_UID, DACEV1, DCCNative, DCMEncrypted}, {MFDES_CHANGE_KEY_SETTINGS, DACEV1, DCCNative, DCMEncrypted},