diff --git a/CHANGELOG.md b/CHANGELOG.md index cc072ca30..5f89a3961 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 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) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index a3172ffb5..b7bc5ecda 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -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) { diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index 43dfe66d8..27a0f469c 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -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); diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 1ae39135c..ea1c909ec 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -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: { diff --git a/armsrc/mifareutil.c b/armsrc/mifareutil.c index d9542fd60..e6aa6ac5a 100644 --- a/armsrc/mifareutil.c +++ b/armsrc/mifareutil.c @@ -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) { diff --git a/armsrc/mifareutil.h b/armsrc/mifareutil.h index 9cc0fe766..1bd18161e 100644 --- a/armsrc/mifareutil.h +++ b/armsrc/mifareutil.h @@ -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); diff --git a/client/luascripts/hf_mf_tnp3_sim.lua b/client/luascripts/hf_mf_tnp3_sim.lua index 3ccfc854d..4c37847be 100644 --- a/client/luascripts/hf_mf_tnp3_sim.lua +++ b/client/luascripts/hf_mf_tnp3_sim.lua @@ -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 diff --git a/client/luascripts/hf_mfu_amiibo_sim.lua b/client/luascripts/hf_mfu_amiibo_sim.lua index 0ea66422c..11a5808be 100644 --- a/client/luascripts/hf_mfu_amiibo_sim.lua +++ b/client/luascripts/hf_mfu_amiibo_sim.lua @@ -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 diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index e051a5026..7bee52208 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -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); diff --git a/client/src/cmdhfmfu.c b/client/src/cmdhfmfu.c index 4ed25a6ac..6fe266dfc 100644 --- a/client/src/cmdhfmfu.c +++ b/client/src/cmdhfmfu.c @@ -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!"); diff --git a/client/src/comms.c b/client/src/comms.c index 375a94150..9df6f7839 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -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. diff --git a/client/src/comms.h b/client/src/comms.h index 08fddfb20..122f9f5d5 100644 --- a/client/src/comms.h +++ b/client/src/comms.h @@ -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); diff --git a/client/src/mifare/mifarehost.c b/client/src/mifare/mifarehost.c index 355634822..3c05f8b64 100644 --- a/client/src/mifare/mifarehost.c +++ b/client/src/mifare/mifarehost.c @@ -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