From 73d03881410752a8350aae5d68b4b09313c7dc6b Mon Sep 17 00:00:00 2001 From: kormax <3392860+kormax@users.noreply.github.com> Date: Sun, 7 Jun 2026 16:17:05 +0300 Subject: [PATCH] Add 'hf mfdes getversion' command --- CHANGELOG.md | 1 + client/src/cmdhfmfdes.c | 92 +++++++++++++++++++++++++++++++++ client/src/mifare/desfirecore.c | 4 ++ client/src/mifare/desfirecore.h | 1 + client/src/pm3line_vocabulary.h | 1 + doc/commands.json | 16 ++++++ doc/commands.md | 1 + doc/desfire.md | 7 +++ 8 files changed, 123 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a70994ff2..50c9ba007 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Added `hf mfdes getversion` command (@kormax) - Changed `doc/magic_cards_notes.md` - updated the ID82xx / Hitag ยต clone section with current Proxmark3 support, chip variations, default passwords and detection notes (@mishamyte) - Added two Mifare Classic keys into extensive dictionary which are hardcoded into Mifare Plus SE (@team-orangeBlue) - Fixed `hf mfp rdbl` when using "read multiple" blocks by decrypting the entire buffer instead of one block only (@team-orangeBlue) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 0c623c603..1fb5f1057 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1133,6 +1133,97 @@ static int CmdHF14ADesInfo(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesGetVersion(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes getversion", + "Request and print DESFire GetVersion data. Optionally select an ISO DF name first.", + "hf mfdes getversion -> request and print card version/type information\n" + "hf mfdes getversion --dfname D2760000850100 -> select DF by name, then request and print version/type information"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "Show APDU requests and responses"), + arg_lit0("v", "verbose", "Verbose output"), + arg_str0(NULL, "dfname", "", "Application ISO DF Name (1-16 hex bytes, big endian)"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + + uint8_t dfname[64] = {0}; + int dfnamelen = 0; + if (CLIParamHexToBuf(arg_get_str(ctx, 3), dfname, sizeof(dfname), &dfnamelen)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } + if (dfnamelen > 16) { + PrintAndLogEx(ERR, "DF name must be 1-16 bytes, got %d", dfnamelen); + CLIParserFree(ctx); + return PM3_EINVARG; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + DesfireContext_t dctx = {0}; + dctx.commMode = DCMPlain; + dctx.cmdSet = DCCNativeISO; + + DropField(); + + int res = PM3_SUCCESS; + if (dfnamelen > 0) { + uint8_t select_resp[250] = {0}; + size_t select_resplen = 0; + res = DesfireISOSelect(&dctx, ISSDFName, dfname, (uint8_t)dfnamelen, select_resp, &select_resplen); + if (res != PM3_SUCCESS) { + DropField(); + PrintAndLogEx(ERR, "Select DF name %s " _RED_("failed") ". Result: %d", sprint_hex_inrow(dfname, dfnamelen), res); + return res; + } + + if (verbose) { + PrintAndLogEx(SUCCESS, "DF name %s selected " _GREEN_("ok"), sprint_hex_inrow(dfname, dfnamelen)); + } + } else { + res = DesfireAnticollision(verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + + mfdes_info_res_t info = {0}; + uint8_t version[sizeof(info.versionHW) + sizeof(info.versionSW) + sizeof(info.details)] = {0}; + size_t version_len = 0; + res = DesfireGetVersion(&dctx, version, &version_len); + if (res != PM3_SUCCESS) { + DropField(); + PrintAndLogEx(ERR, "Desfire GetVersion command " _RED_("error") ". Result: %d", res); + return res; + } + + if (version_len != sizeof(version)) { + DropField(); + PrintAndLogEx(ERR, "Desfire GetVersion returned wrong length: %zu bytes, expected %zu", version_len, sizeof(version)); + return PM3_ESOFT; + } + + info.isOK = 1; + memcpy(info.versionHW, version, sizeof(info.versionHW)); + memcpy(info.versionSW, version + sizeof(info.versionHW), sizeof(info.versionSW)); + memcpy(info.details, version + sizeof(info.versionHW) + sizeof(info.versionSW), sizeof(info.details)); + memcpy(info.uid, info.details, sizeof(info.uid)); + info.uidlen = sizeof(info.uid); + + DesfirePrintVersionInfo(&info, NULL); + + DropField(); + return PM3_SUCCESS; +} + static void DesFill2bPattern( uint8_t deskeyList[MAX_KEYS_LIST_LEN][8], uint32_t *deskeyListLen, uint8_t aeskeyList[MAX_KEYS_LIST_LEN][16], uint32_t *aeskeyListLen, @@ -9608,6 +9699,7 @@ static command_t CommandTable[] = { {"detect", CmdHF14aDesDetect, IfPm3Iso14443a, "Detect key type and tries to find one from the list"}, {"formatpicc", CmdHF14ADesFormatPICC, IfPm3Iso14443a, "Format PICC"}, {"freemem", CmdHF14ADesGetFreeMem, IfPm3Iso14443a, "Get free memory size"}, + {"getversion", CmdHF14ADesGetVersion, IfPm3Iso14443a, "Get version/type information"}, {"getuid", CmdHF14ADesGetUID, IfPm3Iso14443a, "Get uid from card"}, {"pc", CmdHF14ADesPC, IfPm3Iso14443a, "Run proximity check"}, {"info", CmdHF14ADesInfo, IfPm3Iso14443a, "Tag information"}, diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index d178982b5..d06691744 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -2192,6 +2192,10 @@ int DesfireFormatPICC(DesfireContext_t *dctx) { return DesfireCommandNoData(dctx, MFDES_FORMAT_PICC); } +int DesfireGetVersion(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen) { + return DesfireCommandRxData(dctx, MFDES_GET_VERSION, resp, resplen, 28); +} + int DesfireGetFreeMem(DesfireContext_t *dctx, uint32_t *freemem) { *freemem = 0; diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 41a90d33d..ab18f5d20 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -199,6 +199,7 @@ void DesfireCheckAuthCommands(DesfireISOSelectWay way, uint32_t appID, char *dfn void DesfireCheckAuthCommandsPrint(AuthCommandsChk_t *authCmdCheck); int DesfireFormatPICC(DesfireContext_t *dctx); +int DesfireGetVersion(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen); int DesfireGetFreeMem(DesfireContext_t *dctx, uint32_t *freemem); int DesfireGetUID(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen); int DesfireGetAIDList(DesfireContext_t *dctx, uint8_t *resp, size_t *resplen); diff --git a/client/src/pm3line_vocabulary.h b/client/src/pm3line_vocabulary.h index 76f5de452..126c75f69 100644 --- a/client/src/pm3line_vocabulary.h +++ b/client/src/pm3line_vocabulary.h @@ -495,6 +495,7 @@ const static vocabulary_t vocabulary[] = { { 0, "hf mfdes detect" }, { 0, "hf mfdes formatpicc" }, { 0, "hf mfdes freemem" }, + { 0, "hf mfdes getversion" }, { 0, "hf mfdes getuid" }, { 0, "hf mfdes pc" }, { 0, "hf mfdes info" }, diff --git a/doc/commands.json b/doc/commands.json index f1c476b9d..840b53b75 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -7053,6 +7053,22 @@ ], "usage": "hf mfdes getuid [-hav] [-n ] [-t ] [-k ] [--kdf ] [-i ] [-m ] [-c ] [--schann ] [--aid ] [--isoid ] [--dfname ]" }, + "hf mfdes getversion": { + "command": "hf mfdes getversion", + "description": "Request and print DESFire GetVersion data. Optionally select an ISO DF name first.", + "notes": [ + "hf mfdes getversion -> request and print card version/type information", + "hf mfdes getversion --dfname D2760000850100 -> select DF by name, then request and print version/type information" + ], + "offline": false, + "options": [ + "-h, --help This help", + "-a, --apdu Show APDU requests and responses", + "-v, --verbose Verbose output", + "--dfname Application ISO DF Name (1-16 hex bytes, big endian)" + ], + "usage": "hf mfdes getversion [-hav] [--dfname ]" + }, "hf mfdes help": { "command": "hf mfdes help", "description": "help This help list List DESFire (ISO 14443A) history makemfclicense Generate a Mifare Classic license for DESFire EV3C test Regression crypto tests --------------------------------------------------------------------------------------- hf mfdes list available offline: yes Alias of `trace list -t des -c` with selected protocol data to annotate trace buffer You can load a trace from file (see `trace load -h`) or it be downloaded from device by default It accepts all other arguments of `trace list`. Note that some might not be relevant for this specific protocol", diff --git a/doc/commands.md b/doc/commands.md index 1c5f161b6..16dabf449 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -723,6 +723,7 @@ Check column "offline" for their availability. |`hf mfdes detect `|N |`Detect key type and tries to find one from the list` |`hf mfdes formatpicc `|N |`Format PICC` |`hf mfdes freemem `|N |`Get free memory size` +|`hf mfdes getversion `|N |`Get version/type information` |`hf mfdes getuid `|N |`Get uid from card` |`hf mfdes pc `|N |`Run proximity check` |`hf mfdes info `|N |`Tag information` diff --git a/doc/desfire.md b/doc/desfire.md index 65f1af748..1462e76fe 100644 --- a/doc/desfire.md +++ b/doc/desfire.md @@ -177,6 +177,13 @@ The card can return UID in encrypted communication mode. Needs to authenticate w `hf mfdes getuid -s ev2 -t aes -k 11223344556677889900112233445566` - via ev2 secure channel with specified aes key +### How to get version/type information +^[Top](#top) + +`hf mfdes getversion` - request and print card version/type information + +`hf mfdes getversion --dfname D2760000850100` - select an application by ISO DF name before requesting version/type information + ### How to get/set default communication channel settings ^[Top](#top)