hf mfdes sim: self contained DESFire simulation, and stop bitstream downloads eating emulator memory
The DESFire simulation gets its own ISO 14443-A loop in armsrc/desfiresim.c rather than hooking into SimulateIso14443aTag(). That function is complicated enough without a DESFire state machine threaded through it, and the hook had already got the ATQA wrong once. It still borrows the library helpers from iso14443a.c -- the precompiled activation answers, the receive call, the send calls -- so there is no duplicated state machine, and iso14443a.c goes back to knowing nothing about DESFire. desfiresim.h exports one symbol. Three fixes were needed to make it actually answer a reader. 1. ISO 7816 wrapping. A reader sends `02 90 60 00 00 00`, not `02 60`. The simulation read in[0] as the DESFire command and so saw 0x90, the class byte, answering ILLEGAL_COMMAND_CODE to everything -- and in native form (status || data) when the reader wanted the wrapped form (data || 91 SW). Both framings are handled now, including the Lc/Le distinction: five bytes means no data and in[4] is Le, longer means in[4] is Lc with data following. 2. A bitstream download frees and clears BigBuf to get scratch space for the decompressor. SimulateIso14443aTagEx() and Mifare1ksim() both guard against this by calling FpgaDownloadAndGo_keep_EM() before they allocate anything; the new loop did not, so iso14443a_setup() wiped the emulator memory holding the card image and the precompiled answers after they had been filled. The pointers survive, the bytes do not, and the tag then clocks out zeros. 3. iso14443a_setup() itself used the plain FpgaDownloadAndGo(), which does BigBuf_free() and BigBuf_Clear_ext() -- taking the emulator memory with it. Every 14a command comes through there, so a card image that `eload` had just put in place was destroyed by the next `hf 14a` command whenever the HF bitstream was not already resident. Demonstrated before and after: `eload` then `hf 14a reader` then `eview` used to report "No DESFire card image in emulator memory" and now returns the image intact. This affected every emulator memory user, not only DESFire. Verified against a second Proxmark3 acting as reader, simulating a real DESFire EV1 8K dump: activation (UID 04268512A25680, ATQA 03 44, SAK 20, ATS 06 75 77 81 02 80), the three frame GetVersion chain over 0xAF, GetFreeMem, GetApplicationIDs, SelectApplication, GetKeySettings and GetFileSettings. `hf mfdes info`, `getaids`, `freemem`, `lsapp` and `lsfiles` all read correctly, including per application key types (AES, 2TDEA, 3TDEA) and all five EV1 file types with their real settings -- a value file holding 1000 with limits [0..10000], a linear record 2/8 of 16 bytes, a cyclic record 1/4 of 24 bytes, standard files of 256 and 64 bytes, and a backup file of 128 bytes in MAC mode with keyed rights 1200. Not implemented yet: GetDFNames (0x6D) and GetISOFileIDs (0x61), so a reader sees empty ISO IDs and DF names. Everything else answers ILLEGAL_COMMAND_CODE, which is what a PICC says to a command it does not have. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d882c716af
commit
2d70fcb4ff
8 files changed
+282
-174
No files matched your search
@@ -1071,7 +1071,7 @@ int CmdHF14ASim(const char *Cmd) {
|
||||
"Use type 7 for Mifare Ultralight EV1, Amiibo (NTAG215 pack 0x8080)",
|
||||
"hf 14a sim -t 1 --uid 11223344 -> MIFARE Classic 1k\n"
|
||||
"hf 14a sim -t 2 -> MIFARE Ultralight\n"
|
||||
"hf 14a sim -t 3 -> MIFARE DESFire (card image from `hf mfdes eload`)\n"
|
||||
"hf 14a sim -t 3 -> MIFARE Desfire\n"
|
||||
"hf 14a sim -t 4 -> ISO/IEC 14443-4\n"
|
||||
"hf 14a sim -t 5 -> MIFARE Tnp3xxx\n"
|
||||
"hf 14a sim -t 6 -> MIFARE Mini\n"
|
||||
|
||||
+5
-31
@@ -9092,44 +9092,18 @@ static int CmdHF14ADesSim(const char *Cmd) {
|
||||
"Load one first with `hf mfdes eload`, and read back what a reader\n"
|
||||
"changed with `hf mfdes esave`.\n"
|
||||
"\n"
|
||||
"This is `hf 14a sim -t 3` with the DESFire command set behind it:\n"
|
||||
"UID, ATQA, SAK and ATS all come out of the loaded image",
|
||||
"hf mfdes sim\n"
|
||||
"hf mfdes sim -n 1 -> stop after the first reader session\n");
|
||||
"hf mfdes sim");
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_int0("n", "num", "<dec>", "Exit after <num> commands have been answered. 0 = infinite"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
uint8_t exit_after = arg_get_int_def(ctx, 1, 0);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
// Same payload the 14a simulation takes -- there is one 14443a tag loop on
|
||||
// the device and this is it. Tag type 3 is MIFARE DESFire; the UID is left
|
||||
// for the device to lift out of the card image header, which is why no UID
|
||||
// is supplied here.
|
||||
struct {
|
||||
uint8_t tagtype;
|
||||
uint16_t flags;
|
||||
uint8_t uid[10];
|
||||
uint8_t exitAfter;
|
||||
uint8_t rats[20];
|
||||
uint8_t ulauth_1a1_len;
|
||||
uint8_t ulauth_1a2_len;
|
||||
uint8_t ulauth_1a1[16];
|
||||
uint8_t ulauth_1a2[16];
|
||||
bool ulauth_1a2_mirror;
|
||||
} PACKED payload;
|
||||
|
||||
memset(&payload, 0x00, sizeof(payload));
|
||||
payload.tagtype = 3;
|
||||
payload.exitAfter = exit_after;
|
||||
FLAG_SET_UID_IN_EMUL(payload.flags);
|
||||
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_HF_ISO14443A_SIMULATE, (uint8_t *)&payload, sizeof(payload));
|
||||
SendCommandNG(CMD_HF_DESFIRE_SIMULATE, NULL, 0);
|
||||
|
||||
PrintAndLogEx(INFO, "Press " _GREEN_("pm3 button") " or " _GREEN_("<Enter>") " to abort simulation");
|
||||
|
||||
@@ -9139,13 +9113,13 @@ static int CmdHF14ADesSim(const char *Cmd) {
|
||||
|
||||
keypress = kbd_enter_pressed();
|
||||
|
||||
if (WaitForResponseTimeout(CMD_HF_MIFARE_SIMULATE, &resp, 1500) == false) {
|
||||
if (WaitForResponseTimeout(CMD_HF_DESFIRE_SIMULATE, &resp, 1500) == false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(WARNING, "Simulation stopped");
|
||||
PrintAndLogEx(HINT, "Hint: load a card image with " _YELLOW_("`hf mfdes eload -f <fn>`"));
|
||||
PrintAndLogEx(WARNING, "Simulation stopped, no card image in emulator memory?");
|
||||
PrintAndLogEx(HINT, "Hint: load one with " _YELLOW_("`hf mfdes eload -f <fn>`"));
|
||||
return resp.status;
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user