From c7ea870718595fa988d0bb867803c2dbd5ecb993 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Sat, 14 Nov 2026 01:30:17 +0100 Subject: [PATCH 1/2] hf 15 dump: fix field offsets, buffer overflow and short block answers - DSFID, AFI, memory size and IC reference were read at fixed positions, so on a tag without DSFID or AFI the block size and count came from the wrong bytes. They are now read after the fields the tag really sent. - A memory larger than the 2048-byte dump buffer overflowed it. It is now refused. - A block answer shorter than the block size was copied as a full block. It is now reported and not copied. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + client/src/cmdhf15.c | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 934881ef2..640c15196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 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 15 dump` - wrong block size on tags without DSFID or AFI, buffer overflow on large memories, short block answers copied as full blocks (@matrix) - Fixed `tools/external_sam_read.py iclass` - the faked answer to the e-purse update is the new value with its halves swapped, as a card answers (@matrix) - Changed `hf mf sen` - default key prioritisation indexed the candidate list instead of rescanning it for each of the ~85k dictionary keys (@iceman1001) - Changed `hf mf sen` - the default-key dictionary check no longer mallocs a cipher state per key and gives up on the first wrong keystream byte, ~6x faster for that phase (@iceman1001) diff --git a/client/src/cmdhf15.c b/client/src/cmdhf15.c index 6185d95ce..bb8ffe400 100644 --- a/client/src/cmdhf15.c +++ b/client/src/cmdhf15.c @@ -2403,30 +2403,28 @@ static int CmdHF15Dump(const char *Cmd) { if (res == PM3_SUCCESS) { memcpy(tag->uid, d + 2, 8); + // optional fields follow the UID in flag order, only when their flag is set if (d[1] & 0x01) { - tag->dsfid = d[dCpt]; + tag->dsfid = d[dCpt++]; } - dCpt++; if (d[1] & 0x02) { - tag->afi = d[dCpt]; + tag->afi = d[dCpt++]; } - dCpt++; if (d[1] & 0x04) { tag->pagesCount = d[dCpt] + 1; tag->bytesPerPage = d[dCpt + 1] + 1; + dCpt += 2; } else { // Set tag memory layout values (if can't be read in SYSINFO) tag->bytesPerPage = blocksize; tag->pagesCount = 128; } - dCpt += 2; if (d[1] & 0x08) { tag->ic = d[dCpt]; } - dCpt++; } else { tag->uid[0] = 0xE0; @@ -2437,6 +2435,21 @@ static int CmdHF15Dump(const char *Cmd) { tag->pagesCount = 128; } + if ((tag->pagesCount > ISO15693_TAG_MAX_PAGES) || + ((tag->pagesCount * tag->bytesPerPage) > ISO15693_TAG_MAX_SIZE) || + (tag->pagesCount == 0) || + (tag->bytesPerPage == 0)) { + + PrintAndLogEx(FAILED, "Tag size error: pagesCount=%d, bytesPerPage=%d", + tag->pagesCount, + tag->bytesPerPage + ); + free(packet); + free(tag); + DropField(); + return PM3_EINVARG; + } + // add length for blockno (1) packet->rawlen++; packet->raw[0] |= ISO15_REQ_OPTION; // Add option to dump lock status @@ -2493,6 +2506,13 @@ static int CmdHF15Dump(const char *Cmd) { break; } + // flags, lock status, block data, CRC + if (resp.length < (2 + tag->bytesPerPage + 2)) { + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(FAILED, "block %d answer too short ( %d bytes, block size %u )", blocknum, resp.length, tag->bytesPerPage); + continue; + } + tag->locks[blocknum] = d[1]; // copy read data From 142260a4d0e03a0bae88494a71e8078b9cad3bcc Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Sat, 14 Nov 2026 04:21:43 +0100 Subject: [PATCH 2/2] hf 15 dump: mask the block size, fit the --bs fallback in the buffer - The block size of the system info is masked with 0x1F, as in hf 15 info: its top 3 bits are RFU. - Without a memory size in the system info, the dump reads up to 128 blocks of --bs bytes, fewer when they do not fit in the 2048-byte buffer (64 with --bs 32), instead of refusing --bs above 16. - --bs above 32, the largest ISO15693 block, is set to 32 with a warning. It was stored in a uint8_t, so --bs 256 became 0 and --bs 260 became 4. Co-Authored-By: Claude Opus 5.5 --- client/src/cmdhf15.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhf15.c b/client/src/cmdhf15.c index bb8ffe400..27450c44c 100644 --- a/client/src/cmdhf15.c +++ b/client/src/cmdhf15.c @@ -2316,6 +2316,12 @@ static int CmdHF15Dump(const char *Cmd) { blocksize = 4; } + // ISO15693 block size field is 5 bits + if (blocksize > 32) { + PrintAndLogEx(WARNING, "Blocksize too large, using maximum 32 bytes"); + blocksize = 32; + } + // default fallback to scan for tag. if (uidlen != ISO15693_UID_LENGTH && !unaddressed) { scan = true; @@ -2414,12 +2420,14 @@ static int CmdHF15Dump(const char *Cmd) { if (d[1] & 0x04) { tag->pagesCount = d[dCpt] + 1; - tag->bytesPerPage = d[dCpt + 1] + 1; + // block size in b5-b1, b8-b6 RFU + tag->bytesPerPage = (d[dCpt + 1] & 0x1F) + 1; dCpt += 2; } else { // Set tag memory layout values (if can't be read in SYSINFO) tag->bytesPerPage = blocksize; - tag->pagesCount = 128; + // 128 blocks, fewer when large blocks would not fit + tag->pagesCount = MIN(128, ISO15693_TAG_MAX_SIZE / blocksize); } if (d[1] & 0x08) { @@ -2432,7 +2440,7 @@ static int CmdHF15Dump(const char *Cmd) { tag->afi = 0; // Set tag memory layout values (if can't be read in SYSINFO) tag->bytesPerPage = blocksize; - tag->pagesCount = 128; + tag->pagesCount = MIN(128, ISO15693_TAG_MAX_SIZE / blocksize); } if ((tag->pagesCount > ISO15693_TAG_MAX_PAGES) ||