From 3853540d31d016b714191a84033bb1c3a1c2f9eb Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 12 Sep 2026 23:28:17 +0200 Subject: [PATCH] hf mfdes dump: keep the session across the ISO file id probe DesfireFillFileList() ends with GetISOFileIDs (0x61). An application created without ISO file ids answers 0x91F0, and the PICC ends the authentication on a command error. The client never noticed: dctx->secureChannel still said ev1, so every later command was framed as MACed against a dead session. For hf mfdes dump that meant the first file was read as if its response carried a CMAC. An 8 byte plain free access file came back as exactly 8 bytes of data, which were consumed as the MAC, so the file content was thrown away and reported as Received MAC is not match with calculated received MAC: A0 A1 A2 A3 A4 A5 A6 A7 Read operation returned no data from file 0 The next key protected file then failed with 0x91AE, and only that failure triggered the loop's blind re-authentication, after which the rest of the dump was correct. Dumping an application that does have ISO file ids was never affected, because 0x61 succeeds there. Drop our side of the session when the probe fails so the context tells the truth, and re-authenticate in CmdHF14ADesDump before the read loop. The two warnings the probe printed are demoted to DEBUG: an application without ISO file ids is the normal case, not something to warn about. Key settings 2 bit 0x20 is not usable to predict this. On a DESFire EV2 both an application with working ISO file ids and one without report key settings 0F 83, so the bit reads back as 0 either way. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 2 ++ client/src/cmdhfmfdes.c | 12 ++++++++++++ client/src/mifare/desfirecore.c | 9 +++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b35b1be0c..4eb13dad8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ 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] +- Fixed `hf mfdes dump` - the ISO file id probe ends the PICC session on applications without ISO file ids (@iceman1001) +- Changed `hf mfdes lsfiles` / `hf mfdes dump` - no longer warn about a missing ISO file id list (@iceman1001) - Fixed `hf mfdes read` / `hf mfdes write` - a file whose access right is free (0x0E) is served in plain by the PICC (@iceman1001) - Fixed `hf mfdes value` - credit/debit/limitedcredit used a fixed comm mode, now look on card (@iceman1001) - Fixed `hf mfdes value -o get` / `hf mfdes dump` - a value file read in the wrong comm mode printed a silently wrong value instead of failing (@iceman1001) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 2e1ee30a7..97df7ea5e 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -8322,6 +8322,18 @@ static int CmdHF14ADesDump(const char *Cmd) { return res; } + // the ISO file id probe in DesfireFillFileList makes the PICC end the session on + // applications without ISO file ids, so get it back before reading the files + if (noauth == false && DesfireIsAuthenticated(&dctx) == false) { + DesfireSetCommMode(&dctx, DCMPlain); + 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; + } + } + PrintAndLogEx(NORMAL, ""); PrintAndLogEx(SUCCESS, "Application " _CYAN_("%s") " have " _GREEN_("%zu") " files", DesfireWayIDStr(selectway, id), filescount); diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 73315925c..14ec978a4 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -2358,7 +2358,12 @@ int DesfireFillFileList(DesfireContext_t *dctx, FileList_t FileList, size_t *fil buflen = 0; res = DesfireGetFileISOIDList(dctx, buf, &buflen); if (res != PM3_SUCCESS) { - PrintAndLogEx(WARNING, "Desfire GetFileISOIDList command, res " _RED_("%d"), res); + // An application created without ISO file IDs answers 0x91F0 here, and the PICC + // ends the authentication on a command error. Drop our side of the session too, + // otherwise the next command is still framed as MACed and the plain response to + // it gets parsed as a CMAC. Callers that keep using the card re-authenticate. + dctx->secureChannel = DACNone; + PrintAndLogEx(DEBUG, "Desfire GetFileISOIDList command, res " _RED_("%d") ". Session dropped by the PICC", res); } size_t isoindx = 0; @@ -2373,7 +2378,7 @@ int DesfireFillFileList(DesfireContext_t *dctx, FileList_t FileList, size_t *fil if (isoindx * 2 != buflen) PrintAndLogEx(WARNING, "Wrong ISO ID list length. must be %zu but %zu", buflen, isoindx * 2); } else { - PrintAndLogEx(WARNING, "ISO ID list returned no data"); + PrintAndLogEx(DEBUG, "ISO ID list returned no data"); } *isopresent = (isoindx > 0);