hf mf eload: report failed emulator memory writes

The device did not answer CMD_HF_MIFARE_EML_MEMSET, so `hf mf eload`
and `hf mfu eload` printed "Done!" even when the device had no room
left for the emulator memory and nothing was written. BigBuf_Clear_EM()
then ran memset() on a NULL pointer. The device now refuses a MEMSET
packet shorter than its blocks and replies with the status, which the
client waits for. `hf mf eload` also turns fast mode off when its last
packet holds more than one block. hf_mf_tnp3_sim.lua sends the flags
byte added in 4d7d49e08, and both lua sims check the status. The
`hf mfu eload` hint loses a stray backtick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gabriele GristinaandClaude Opus 5.5 committed 2026-11-11 12:51:47 +01:00
1 parent cab165da4d
commit dfffa67063
13 files changed
+73 -23

No files matched your search

+2
View File
@@ -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 mf eload`, `hf mfu eload` - a failed emulator memory write, for example with the device memory full, is reported instead of `Done!`, fast mode no longer stays on after the upload (@matrix)
- Fixed `hf_mf_tnp3_sim.lua`, `hf_mfu_amiibo_sim.lua` - a failed emulator memory write is reported, tnp3 sends the flags byte and its blocks no longer land one byte off (@matrix)
- Changed PM5 bundled BWM firmware to V2 - power save, ESP-IDF 6.1 support, stability fixes (@iceman1001)
- Changed `hf mfu info` - UL-C fingerprint prints PRNG, Vendor and Chip IC lines, weak PRNG and counterfeit / magic chips in green (@iceman1001)
- Added `hf mfu info`, `hf mfu dump -v`, `hf mfu view -v`, `hf mfu eview -v` - UL-C key page lock check from the config pages, flags partial key overwrite / relayed auth attacks (BREAKMEIFYOUCAN, eprint 2026/100) (@iceman1001)
+7 -2
View File
@@ -140,8 +140,13 @@ void BigBuf_Clear_ext(bool verbose) {
}
}
void BigBuf_Clear_EM(void) {
memset(BigBuf_get_EM_addr(), 0, CARD_MEMORY_SIZE);
int BigBuf_Clear_EM(void) {
uint8_t *mem = BigBuf_get_EM_addr();
if (mem == NULL) {
return PM3_EMALLOC;
}
memset(mem, 0, CARD_MEMORY_SIZE);
return PM3_SUCCESS;
}
void BigBuf_Clear_keep_EM(void) {
+1 -1
View File
@@ -69,7 +69,7 @@ void BigBuf_initialize(void);
void BigBuf_Clear(void);
void BigBuf_Clear_ext(bool verbose);
void BigBuf_Clear_keep_EM(void);
void BigBuf_Clear_EM(void);
int BigBuf_Clear_EM(void);
uint8_t *BigBuf_malloc(uint32_t);
uint8_t *BigBuf_calloc(uint32_t);
void BigBuf_free(void);
+15 -5
View File
@@ -2818,13 +2818,23 @@ static void PacketReceived(PacketCommandNG *packet) {
payload->blockwidth = MIFARE_BLOCK_SIZE;
}
// a whole-dump upload zeroes what was there first, so a smaller dump can't
// leave a tail behind that reads back as card data. esetblk never sets this.
if (payload->flags & MFEMUL_MEMSET_CLEAR) {
BigBuf_Clear_EM();
// the data the packet holds
if ((uint32_t)payload->blockcnt * payload->blockwidth > packet->length - sizeof(struct p)) {
reply_ng(CMD_HF_MIFARE_EML_MEMSET, PM3_EINVARG, NULL, 0);
break;
}
emlSetMem_xt(payload->data, payload->blockno, payload->blockcnt, payload->blockwidth);
// a whole-dump upload zeroes what was there first, so a smaller dump can't
// leave a tail behind that reads back as card data. esetblk never sets this.
int res = PM3_SUCCESS;
if (payload->flags & MFEMUL_MEMSET_CLEAR) {
res = BigBuf_Clear_EM();
}
if (res == PM3_SUCCESS) {
res = emlSetMem_xt(payload->data, payload->blockno, payload->blockcnt, payload->blockwidth);
}
reply_ng(CMD_HF_MIFARE_EML_MEMSET, res, NULL, 0);
break;
}
case CMD_HF_MIFARE_EML_MEMGET: {
+2 -2
View File
@@ -937,10 +937,10 @@ uint8_t FirstBlockOfSector(uint8_t sectorNo) {
}
// work with emulator memory
void emlSetMem_xt(uint8_t *data, uint16_t blockNum, uint8_t blocksCount, uint8_t block_width) {
int emlSetMem_xt(uint8_t *data, uint16_t blockNum, uint8_t blocksCount, uint8_t block_width) {
uint32_t offset = blockNum * block_width;
uint32_t len = blocksCount * block_width;
emlSet(data, offset, len);
return emlSet(data, offset, len);
}
void emlGetMem_xt(uint8_t *data, uint16_t blockNum, uint8_t blocksCount, uint8_t block_width) {
+1 -1
View File
@@ -134,7 +134,7 @@ uint8_t SectorTrailer(uint8_t blockNo);
// emulator functions
void emlClearMem(void);
void emlSetMem_xt(uint8_t *data, uint16_t blockNum, uint8_t blocksCount, uint8_t block_width);
int emlSetMem_xt(uint8_t *data, uint16_t blockNum, uint8_t blocksCount, uint8_t block_width);
void emlGetMem_xt(uint8_t *data, uint16_t blockNum, uint8_t blocksCount, uint8_t block_width);
uint64_t emlGetKey(int sectorNum, int keyType);
+7 -5
View File
@@ -244,12 +244,14 @@ local function LoadEmulator(uid, blocks)
io.write( _..',')
io.flush()
core.clearCommandBuffer()
-- payload: u16 blockno, u8 blockcnt, u8 blockwidth, u8 data[]
-- payload: u16 blockno, u8 blockcnt, u8 blockwidth, u8 data[]
local eml_payload = ('%02X%02X%02X%02X'):format(_ % 256, math.floor(_ / 256) % 256, 1, 16) .. blockdata
-- payload: u16 blockno, u8 blockcnt, u8 blockwidth, u8 flags, u8 data[]
local eml_payload = ('%02X%02X%02X%02X%02X'):format(_ % 256, math.floor(_ / 256) % 256, 1, 16, 0) .. blockdata
cmd = Command:newNG{cmd = cmds.CMD_HF_MIFARE_EML_MEMSET, data = eml_payload}
local err, msg = cmd:sendNG(true)
if err == nil then return err, msg end
local res, msg = cmd:sendNG(false)
if res == nil then return msg end
if res.Status ~= 0 then
return ('Setting emulator memory block %d failed, status %d (try "data clear" if the device memory is full)'):format(_, res.Status)
end
end
io.write('\n')
end
+5 -2
View File
@@ -87,8 +87,11 @@ local function LoadEmulator(uid, blocks)
local block_hi = bit32.rshift(i, 8)
local payload = ('%02x%02x%02x%02x%02x'):format(block_lo, block_hi, 1, 4, 0)..blockdata
cmd = Command:newNG{cmd = cmds.CMD_HF_MIFARE_EML_MEMSET, data = payload}
local err, msg = cmd:sendNG(true)
if err == nil then return err, msg end
local res, msg = cmd:sendNG(false)
if res == nil then return msg end
if res.Status ~= 0 then
return ('Setting emulator memory block %d failed, status %d (try "data clear" if the device memory is full)'):format(i, res.Status)
end
end
io.write('\n')
end
+6 -4
View File
@@ -5559,7 +5559,7 @@ int CmdHF14AMfELoad(const char *Cmd) {
uint16_t max_avail_blocks = ((g_conn.max_cmd_data_size - 12) / block_width) * block_width;
while (bytes_read && cnt < block_cnt) {
if (bytes_read == block_width) {
if (bytes_read <= max_avail_blocks) {
// Disable fast mode on last packet
g_conn.block_after_ACK = false;
}
@@ -5571,10 +5571,12 @@ int CmdHF14AMfELoad(const char *Cmd) {
// than the previous one can't leave a tail behind that reads back as card data
uint8_t flags = (cnt == 0) ? MFEMUL_MEMSET_CLEAR : 0;
if (mf_eml_set_mem_xt(data + offset, cnt, blocks_to_send, block_width, flags) != PM3_SUCCESS) {
res = mf_eml_set_mem_xt(data + offset, cnt, blocks_to_send, block_width, flags);
if (res != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Can't set emulator mem at block: %3d", cnt);
g_conn.block_after_ACK = false;
free(data);
return PM3_ESOFT;
return res;
}
cnt += blocks_to_send;
offset += chunk_size;
@@ -5586,7 +5588,7 @@ int CmdHF14AMfELoad(const char *Cmd) {
PrintAndLogEx(NORMAL, "");
if (block_width == MFU_BLOCK_SIZE) {
PrintAndLogEx(HINT, "Hint: You are ready to simulate. See `" _YELLOW_("hf mfu sim -h`") "`");
PrintAndLogEx(HINT, "Hint: You are ready to simulate. See `" _YELLOW_("hf mfu sim -h") "`");
// MFU / NTAG
if ((cnt != block_cnt)) {
PrintAndLogEx(WARNING, "Warning, Ultralight/Ntag file content, Loaded %d blocks of expected %d blocks into emulator memory", cnt, block_cnt);
+3
View File
@@ -4591,6 +4591,9 @@ static int CmdHF14AMfUeLoad(const char *Cmd) {
snprintf(nc, nc_len, "%s --ul", Cmd);
int res = CmdHF14AMfELoad(nc);
free(nc);
if (res != PM3_SUCCESS) {
return res;
}
PrintAndLogEx(HINT, "Hint: Try " _YELLOW_("`hf mfu sim -t 7`") " to simulate an Amiibo.");
PrintAndLogEx(INFO, "Done!");
+21
View File
@@ -1208,6 +1208,27 @@ bool WaitForResponse(uint32_t cmd, PacketResponseNG *response) {
return WaitForResponseTimeoutW(cmd, response, -1, true);
}
// The device allocates the emulator memory on first use, and the trace of an
// earlier command can leave no room for it
void PrintEmulatorAllocError(int status) {
if (status == PM3_EMALLOC) {
PrintAndLogEx(FAILED, "\nNot enough free device memory for the emulator memory");
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("data clear") "` to free it, then try again");
}
}
// Result of an emulator memory write. The first one can wait for an FPGA
// bitstream to load
int WaitForEmulatorWrite(uint32_t cmd) {
PacketResponseNG resp;
if (WaitForResponseTimeout(cmd, &resp, 2500) == false) {
PrintAndLogEx(WARNING, "timeout while waiting for reply");
return PM3_ETIMEOUT;
}
PrintEmulatorAllocError(resp.status);
return resp.status;
}
/**
* Data transfer from Proxmark to client. This method times out after
* ms_timeout milliseconds.
+2
View File
@@ -116,6 +116,8 @@ bool WaitForResponseTimeoutW(uint32_t cmd, PacketResponseNG *response, size_t ms
bool WaitForResponseTimeout(uint32_t cmd, PacketResponseNG *response, size_t ms_timeout);
bool WaitForTxIdle(uint32_t ms_timeout);
bool WaitForResponse(uint32_t cmd, PacketResponseNG *response);
void PrintEmulatorAllocError(int status);
int WaitForEmulatorWrite(uint32_t cmd);
int SetHfFieldTimeout(uint32_t timeout_sec, bool quiet);
+1 -1
View File
@@ -1186,7 +1186,7 @@ int mf_eml_set_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtW
clearCommandBuffer();
SendCommandNG(CMD_HF_MIFARE_EML_MEMSET, (uint8_t *)payload, paylen);
free(payload);
return PM3_SUCCESS;
return WaitForEmulatorWrite(CMD_HF_MIFARE_EML_MEMSET);
}
// "MAGIC" CARD