hf mfdes: ChangeFileSettings, and an esave option to keep what was deleted

ChangeFileSettings is answered by the simulation now. Which form it takes is
decided by the file's own "change access rights" right, not by the reader:
never refuses the command outright, free means the settings arrive as plain text
with no security mechanism at all, and anything else names the key that has to
be authenticated and the settings arrive enciphered under it (M134034 9.5.4).

That enciphered form is the first command whose parameters the reader secured
rather than the card, so it shares the unwrapping the writes use: the file
number stays in the clear, the rest is one CBC run under the session key, and
the CRC32 behind the plaintext is checked before anything is changed. A command
that unwraps its own parameters also has to be kept away from the blanket
command CMAC, or the IV moves twice.

Tested against the simulation: a file whose change right is free goes from
rights eeee to 1234 with no session at all, a file whose change right is key 0
goes from 1200 to 3210 authenticated and enciphered, and a file whose change
right has been set to F refuses every later change and keeps its settings, which
is the point of that value.

Separately, `hf mfdes esave` grew a `--keep` flag. A deleted application or file
stays in the card image as a tombstone, because the memory it held stays spent
until a FormatPICC, and esave was writing those out as though they still
existed. They are now left out by default, so a dump is the card as a reader
sees it, and `--keep` puts them back for when what a reader did is the
interesting part rather than the result it left.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iceman1001andClaude Opus 5 committed 2026-09-15 21:15:41 +02:00
1 parent 734500bdf6
commit 31d05aef53
5 files changed
+424 -8

No files matched your search

+10 -3
View File
@@ -9223,13 +9223,19 @@ static int CmdHF14ADesESave(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes esave",
"Save the card image in emulator memory to a dump file.\n"
"Shows what a reader left behind if one has been talking to the simulation",
"hf mfdes esave -f myfile");
"Shows what a reader left behind if one has been talking to the simulation.\n"
"A deleted application or file is kept in the image as a tombstone, because the\n"
"memory it held stays spent until a FormatPICC. By default those are left out, so\n"
"the dump is the card as a reader sees it. Use `--keep` to save them as well, which\n"
"is what you want to see everything a reader did rather than only the result",
"hf mfdes esave -f myfile\n"
"hf mfdes esave -f myfile --keep -> keep applications and files a reader deleted");
void *argtable[] = {
arg_param_begin,
arg_str0("f", "file", "<fn>", "Filename, if no <fn> UID will be used as filename"),
arg_lit0("v", "verbose", "Verbose output"),
arg_lit0(NULL, "keep", "Keep applications and files that were deleted"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -9238,6 +9244,7 @@ static int CmdHF14ADesESave(const char *Cmd) {
char filename[FILE_PATH_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
bool verbose = arg_get_lit(ctx, 2);
bool keep_deleted = arg_get_lit(ctx, 3);
CLIParserFree(ctx);
size_t emsize = g_conn.em_size;
@@ -9269,7 +9276,7 @@ static int CmdHF14ADesESave(const char *Cmd) {
return PM3_EMALLOC;
}
res = desfire_em_unpack(img, emsize, dump);
res = desfire_em_unpack_ex(img, emsize, dump, keep_deleted);
free(img);
if (res != PM3_SUCCESS) {
+50 -4
View File
@@ -431,6 +431,10 @@ int desfire_em_pack(const desfire_dump_t *dump, uint8_t *out, size_t outlen, siz
}
int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump) {
return desfire_em_unpack_ex(img, imglen, dump, false);
}
int desfire_em_unpack_ex(const uint8_t *img, size_t imglen, desfire_dump_t *dump, bool keep_deleted) {
if (img == NULL || dump == NULL || imglen < sizeof(desfire_em_hdr_t)) {
return PM3_EINVARG;
@@ -491,13 +495,39 @@ int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump) {
return PM3_EINVARG;
}
dump->appcount = hdr->appcount - 1; // app[0] is the PICC
// A deleted application or file is a tombstone in the image, so the memory
// it held stays spent, but it is gone as far as a reader is concerned and it
// does not belong in a dump of the card. Applications are therefore packed
// down as they are copied out, and appmap carries image index -> dump index
// so the key and file tables, which refer to applications by image index,
// can follow.
int appmap[DESFIRE_EM_MAX_APPS + 1];
for (uint16_t i = 0; i < ARRAYLEN(appmap); i++) {
appmap[i] = -1;
}
dump->appcount = 0;
for (uint8_t i = 0; i < hdr->appcount; i++) {
desfire_dump_app_t *a = (i == 0) ? &dump->picc : &dump->app[i - 1];
const desfire_em_app_t *s = &apps[i];
if (i > 0 && (s->flags & DESFIRE_EM_APP_DELETED) && (keep_deleted == false)) {
continue;
}
desfire_dump_app_t *a;
if (i == 0) {
a = &dump->picc; // app[0] is the PICC, never deleted
} else {
if (dump->appcount >= DESFIRE_MAX_APP_COUNT) {
continue;
}
a = &dump->app[dump->appcount];
appmap[i] = dump->appcount;
dump->appcount++;
}
a->aid = s->aid[0] | (s->aid[1] << 8) | (s->aid[2] << 16);
a->isofid = s->isofid;
a->keysettings = s->keysettings;
@@ -517,7 +547,15 @@ int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump) {
continue;
}
desfire_dump_app_t *a = (s->app == 0) ? &dump->picc : &dump->app[s->app - 1];
desfire_dump_app_t *a;
if (s->app == 0) {
a = &dump->picc;
} else {
if (appmap[s->app] < 0) {
continue; // its application was deleted
}
a = &dump->app[appmap[s->app]];
}
if (s->flags & DESFIRE_EM_KEY_VERKNOWN) {
a->keys.versionknown[s->num] = 1;
@@ -537,7 +575,15 @@ int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump) {
continue;
}
desfire_dump_app_t *a = &dump->app[s->app - 1];
if ((s->flags & DESFIRE_EM_FILE_DELETED) && (keep_deleted == false)) {
continue;
}
if (appmap[s->app] < 0) {
continue; // its application was deleted
}
desfire_dump_app_t *a = &dump->app[appmap[s->app]];
if (a->filecount >= DESFIRE_MAX_FILE_COUNT) {
continue;
}
+5
View File
@@ -48,6 +48,11 @@ int desfire_em_pack(const desfire_dump_t *dump, uint8_t *out, size_t outlen, siz
// Walk an image back into a desfire_dump_t. The caller owns the per-file heap
// buffers afterwards and must release them with desfire_dump_free().
// Unpack a card image into a dump. A deleted application or file is a tombstone
// in the image: the memory it held stays spent, but it is gone as far as a
// reader is concerned. `keep_deleted` decides which of those two views you get
// -- false for the card as a reader sees it, true to keep what was there.
int desfire_em_unpack_ex(const uint8_t *img, size_t imglen, desfire_dump_t *dump, bool keep_deleted);
int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump);
// Print an image's structure. Does not need a device.