make hf 14a antifuzz actually reach a reader

Every answer was modulated after the reader's frame arrived, landing
~1812 carrier periods late against the 1172 the FDT allows, so readers
timed out before any of it mattered. All answers are now precomputed the
way hf 14a sim does it, and a phone answers at 1172 exactly.

Cascade mode sent a cascade tag at every level, which a conforming reader
rejects in the last level its ATQA announced - it dropped the card before
ever sending a SELECT. The cascade tag now appears only below that level;
it is the SAK that lies about the UID being incomplete, and that is what
walks a reader up to cascade level 7.

A bit oriented ANTICOLLISION was answered with the whole UID instead of
the bits the reader still lacked, handing it a frame of the wrong length.
One encoder now builds any tail, with or without collisions in it.

--coll leaves the first UID byte clean and collides every bit after it, so
a reader resolves 24 bits one round trip at a time, is then given a BCC
that checks out, and is cascaded into the same again - 183 answers per
poll against the 3 a real card needs. Colliding byte 0 as well only
produced a UID of all ones, which readers drop before asking for the BCC.

Reader frames that go unanswered are traced instead of silently dropped,
LED B marks an attempt in progress and LED C flips on each step deeper,
and the closing line reports rounds run and how far a reader got.

trace list -t 14a decodes SEL 0x99..0x9F, shows how many UID bits a bit
oriented ANTICOLL claims, and no longer flags those CRC-less frames as
bad CRC.

Co-Authored-By: Claude Opus 5 (1M context)
This commit is contained in:
iceman1001andClaude Opus 5 (1M context) committed 2026-09-12 21:18:26 +02:00
1 parent 25b3c88acf
commit 255576fa43
7 files changed
+558 -94

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]
- Added `hf 14a antifuzz --coll` - the UID collides from the 9th bit (@iceman1001)
- Changed `trace list -t 14a` - decodes SEL 0x99..0x9F, shows how many UID bits a bit oriented ANTICOLL claims (@iceman1001)
- Fixed `hf iclass encode` / `hf iclass tagsim` / `hf mf encodehid` / `lf hid sim` / `lf hid clone` / `lf hid brute` - `--cn` was read with a 32 bit getter, silently truncating card numbers of wide formats like H10302 (@iceman1001)
- Changed `hf mf eload` - the device now zeroes the emulator memory before the upload (@iceman1001)
- Fixed `hf mf sim` / `hf 14a sim` - the anticollision responses were prepared in BigBuf before `iso14443a_setup()` (@iceman1001)
+4 -1
View File
@@ -2348,9 +2348,12 @@ static void PacketReceived(PacketCommandNG *packet) {
case CMD_HF_ISO14443A_ANTIFUZZ: {
struct p {
uint8_t flag;
uint8_t mode;
} PACKED;
struct p *payload = (struct p *) packet->data.asBytes;
iso14443a_antifuzz(payload->flag);
// older clients only send the flag byte
uint8_t mode = (packet->length >= sizeof(struct p)) ? payload->mode : ANTIFUZZ_MODE_CASCADE;
iso14443a_antifuzz(payload->flag, mode);
break;
}
// EPA related
+439 -42
View File
@@ -3206,6 +3206,18 @@ static uint16_t ReaderReceiveOffset(uint8_t *receivedAnswer, uint16_t answer_len
return Demod.len;
}
// End of the last frame we transmitted, and start of the last frame we
// received, both in SSP clock ticks. Callers that need the tag's frame delay
// time take the difference; LogTrace scales the same values by 16 to get
// carrier periods.
uint32_t iso14a_last_tx_end(void) {
return LastTimeProxToAirStart + LastProxToAirDuration;
}
uint32_t iso14a_last_rx_start(void) {
return Demod.startTime;
}
static uint16_t ReaderReceiveEx(uint8_t *receivedAnswer, uint16_t answer_maxlen, uint8_t *par, bool no_parity) {
if (GetIso14443aAnswerFromTag(receivedAnswer, answer_maxlen, par, 0, no_parity) == false) {
return 0;
@@ -3219,10 +3231,255 @@ uint16_t ReaderReceive(uint8_t *receivedAnswer, uint16_t answer_maxlen, uint8_t
return ReaderReceiveEx(receivedAnswer, answer_maxlen, par, false);
}
// Number of symbols in a bit oriented ANTICOLLISION answer that starts `known`
// bits into the 40 bit UID+BCC of one cascade level. The byte the reader split
// is completed without a parity bit, every whole byte after it carries one.
static uint16_t anticoll_answer_symbols(uint8_t known) {
uint16_t bits = 40 - known;
uint16_t split = known & 7;
uint16_t symbols = 0;
if (split) {
uint16_t first = 8 - split; // completes the split byte, no parity
symbols += first;
bits -= first;
}
// whole bytes from here on, one parity bit each
symbols += bits + (bits / 8);
return symbols;
}
// Encode the tail of a bit oriented ANTICOLLISION answer: the `40 - known` bits
// of UID+BCC that the reader has not resolved yet. The byte the reader split is
// completed without a parity bit, every whole byte after it carries one, so
// CodeIso14443aAsTagPar() cannot build this - it only speaks whole bytes.
// known == 0 is the ordinary "send me the whole UID" answer.
//
// Every bit from `coll_from` on is sent as a collision instead of as data, and a
// byte holding one cannot carry a real parity bit either. coll_from == 40 is a
// clean answer, 0 collides the whole frame.
static void CodeIso14443aAsTagAnticollTail(const uint8_t *uid, uint8_t known, uint8_t coll_from) {
tosend_reset();
tosend_t *ts = get_tosend();
// 1 correction byte + 1 start bit + 1 stop bit on top of the payload
if (3 + (int)anticoll_answer_symbols(known) > TOSEND_BUFFER_SIZE) {
Dbprintf("CodeIso14443aAsTagAnticollTail: frame too large (%u known)", known);
return;
}
// Correction bit, might be removed when not needed
tosend_stuffbit(0);
tosend_stuffbit(0);
tosend_stuffbit(0);
tosend_stuffbit(0);
tosend_stuffbit(1); // <-----
tosend_stuffbit(0);
tosend_stuffbit(0);
tosend_stuffbit(0);
// Send startbit
ts->buf[++ts->max] = SEC_D;
LastProxToAirDuration = 8 * ts->max - 4;
uint8_t first = known >> 3; // byte the reader stopped in
uint8_t split = known & 7; // bits of it the reader already has
for (uint8_t i = first; i < 5; i++) {
uint8_t skip = (i == first) ? split : 0;
bool collided = false;
for (uint8_t j = skip; j < 8; j++) {
if (((i * 8) + j) >= coll_from) {
ts->buf[++ts->max] = SEC_COLL;
LastProxToAirDuration = 8 * ts->max;
collided = true;
} else if (uid[i] & (1 << j)) {
ts->buf[++ts->max] = SEC_D;
LastProxToAirDuration = 8 * ts->max - 4;
} else {
ts->buf[++ts->max] = SEC_E;
LastProxToAirDuration = 8 * ts->max;
}
}
// no parity bit for the byte the reader split
if (skip) {
continue;
}
if (collided) {
ts->buf[++ts->max] = SEC_COLL;
LastProxToAirDuration = 8 * ts->max;
} else if (oddparity8(uid[i])) {
ts->buf[++ts->max] = SEC_D;
LastProxToAirDuration = 8 * ts->max - 4;
} else {
ts->buf[++ts->max] = SEC_E;
LastProxToAirDuration = 8 * ts->max;
}
}
// Send stopbit
ts->buf[++ts->max] = SEC_F;
// Convert from last byte pos to length
ts->max++;
}
// Store whatever CodeIso14443aAsTag*() just built into `buffer` and describe it
// in `response_info`, the way prepare_allocated_tag_modulation() does for the
// frames it can encode itself.
static bool antifuzz_store_modulation(tag_response_info_t *response_info,
uint8_t **buffer, size_t *max_buffer_size) {
const tosend_t *ts = get_tosend();
if (ts->max <= 0 || (size_t)ts->max > *max_buffer_size) {
return false;
}
response_info->modulation = *buffer;
memcpy(response_info->modulation, ts->buf, ts->max);
response_info->modulation_n = ts->max;
response_info->ProxToAirDuration = LastProxToAirDuration;
*buffer += ts->max;
*max_buffer_size -= ts->max;
return true;
}
// Modulate an all-collision answer into `buffer` and describe it in
// `response_info`, the way prepare_allocated_tag_modulation() does for ordinary
// frames. CodeIso14443aAsTag() cannot build these: a bit oriented answer is not
// a whole number of bytes and every symbol in it is a collision.
// One place to notice a reader getting deeper than it has been in this
// anticollision attempt. `depth` counts 41 steps per cascade level - the 40 bit
// counts a reader can claim, plus the SELECT that follows them - so a reader
// climbing levels keeps making progress rather than restarting the count.
static void antifuzz_track_depth(uint8_t level, uint8_t known, uint16_t *depth,
uint8_t *max_level, uint8_t *max_known) {
uint16_t step = ((uint16_t)(level - 1) * 41) + known + 1;
if (step > *depth) {
*depth = step;
LED_C_INV();
}
if (level > *max_level) {
*max_level = level;
*max_known = known;
} else if (level == *max_level && known > *max_known) {
*max_known = known;
}
}
// Precomputed answers. Modulating a frame takes long enough that the reader's
// frame delay window is gone before the answer is ready - the same reason
// `hf 14a sim` builds its answers up front - so everything this loop can send is
// modulated before the field is even up.
#define ANTIFUZZ_MAX_LEVEL 7 // SEL 0x93..0x9F
#define ANTIFUZZ_NVB_COUNT 40 // 0..39 UID bits already known to the reader
// Collision mode keeps the UID clean up to here and collides everything from it
// on. Byte 0 is what a reader sanity checks a UID by, so leaving exactly that
// much alone is what lets the other 24 bits collide without the reader deciding
// the card is junk and dropping it - every branch of the tree still resolves to
// a UID starting with a real manufacturer byte. One collided bit costs the
// reader one round trip, so this is also what sets how hard it has to work.
#define ANTIFUZZ_COLL_FROM 8
#define ANTIFUZZ_MAX_ANNOUNCED 3 // cascade levels ISO 14443-3 actually defines
#define ANTIFUZZ_RESP_ATQA 0
#define ANTIFUZZ_RESP_SAK 1
// One block of ANTIFUZZ_NVB_COUNT answers per UID the mode can hand out. Cascade
// mode has one UID per cascade level; collision mode only needs two, one with a
// cascade tag and one without, and puts its BCC answers above them. The two
// layouts overlap - only ever one of them is prepared.
#define ANTIFUZZ_RESP_BASE 2
// Collision mode needs a block per announced cascade level plus one of filler,
// and above them one clean BCC answer per value it could owe a reader. Cascade
// mode uses a block per level and none of the BCC answers, so the two layouts
// overlap - only ever one of them is prepared.
#define ANTIFUZZ_MAX_BLOCKS (ANTIFUZZ_MAX_ANNOUNCED + 1)
#define ANTIFUZZ_RESP_BCC (ANTIFUZZ_RESP_BASE + (ANTIFUZZ_MAX_BLOCKS * ANTIFUZZ_NVB_COUNT))
#define ANTIFUZZ_RESP_COUNT (ANTIFUZZ_RESP_BCC + 256 > ANTIFUZZ_RESP_BASE + (ANTIFUZZ_MAX_LEVEL * ANTIFUZZ_NVB_COUNT) \
? ANTIFUZZ_RESP_BCC + 256 \
: ANTIFUZZ_RESP_BASE + (ANTIFUZZ_MAX_LEVEL * ANTIFUZZ_NVB_COUNT))
// worst case is cascade mode: 1025 bytes of modulation per cascade level, plus
// the two fixed answers
#define ANTIFUZZ_MOD_SIZE ((ANTIFUZZ_MAX_LEVEL * 1025) + 128)
// The UIDs `hf 14a antifuzz` hands out, one per ATQA size. A reader collects
// them three bytes at a time behind a cascade tag, until the last cascade level
// its ATQA announced, which carries four.
static const uint8_t antifuzz_uid_4b[4] = { 0x9B, 0x49, 0x03, 0x51 };
static const uint8_t antifuzz_uid_7b[7] = { 0x04, 0x49, 0x03, 0x52, 0x92, 0x9C, 0x80 };
static const uint8_t antifuzz_uid_10b[10] = { 0x04, 0x49, 0x03, 0x53, 0x92, 0x9C, 0xAB, 0xD1, 0x4F, 0x80 };
// Block b answers for cascade level b + 1, and the last block also answers for
// every level past it.
static uint8_t antifuzz_block_of(uint8_t count, uint8_t level) {
return (level <= count) ? (level - 1) : (count - 1);
}
// The four UID bytes a cascade level hands out, with its BCC appended. Levels
// below the last announced one carry a cascade tag and three UID bytes, the last
// announced level carries four, and anything past it is off the end of the UID
// altogether - it is only ever reached because the SAK keeps claiming the UID is
// incomplete, so it gets filler that is obviously not part of the card.
static void antifuzz_level_uid(uint8_t *uid, uint8_t level, uint8_t announced_levels, const uint8_t *src) {
if (level > announced_levels) {
// 0x88 would read as one more cascade tag and 0x08 as a random ID
uid[0] = 0x11;
uid[1] = level;
uid[2] = 0x01;
uid[3] = 0x00;
} else if (level < announced_levels) {
uid[0] = MIFARE_SELECT_CT;
memcpy(uid + 1, src + (3 * (level - 1)), 3);
} else {
memcpy(uid, src + (3 * (level - 1)), 4);
}
uid[4] = uid[0] ^ uid[1] ^ uid[2] ^ uid[3];
}
// This function misstreats the ISO 14443a anticollision procedure.
// by fooling the reader there is a collision and forceing the reader to
// increase the uid bytes. The might be an overflow, DoS will occur.
void iso14443a_antifuzz(uint32_t flags) {
//
// ANTIFUZZ_MODE_CASCADE
// Every SAK keeps the cascade bit set, i.e. "the UID is not complete yet", so
// a reader that believes the card asks for one more cascade level and keeps
// growing its UID buffer past the three levels ISO 14443-3 defines. We answer
// SEL bytes 0x93 up to 0x9F, so a reader that simply increments is followed
// all the way up.
//
// The UID matters only in that the reader has to accept it: a conforming
// reader counts the cascade levels the ATQA promised and throws the card away
// if it meets a cascade tag in the last of them, so the cascade tag is sent
// only below that level.
//
// ANTIFUZZ_MODE_COLLISION
// Byte 0 of the UID goes out clean and every bit after it collides, so the
// reader has to resolve 24 bits one round trip at a time. Whatever it picks
// along the way it tells us, which means that once it has all 32 bits the BCC
// it is owed is computable - so it gets a real one, its SELECT succeeds, and
// the SAK sends it round the same 26 frame exercise one cascade level up.
//
// Colliding byte 0 as well only ever produced a UID of all ones, which readers
// recognise as junk and drop before they even ask for the BCC. Leaving that
// one byte alone is what buys the other 24 bits: every branch of the tree
// still resolves to a UID starting with a real manufacturer byte, so there is
// never a reason to bail out early.
//
// Between them that is 183 answers per poll against the 3 a real card needs,
// and it leans on the bit tree walker, the NVB parser, the partial byte
// reassembler and the cascade handler all at once.
void iso14443a_antifuzz(uint32_t flags, uint8_t mode) {
// We need to listen to the high-frequency, peak-detected path.
iso14443a_setup(FPGA_HF_ISO14443A_TAGSIM_LISTEN);
@@ -3236,8 +3493,14 @@ void iso14443a_antifuzz(uint32_t flags) {
// allocate buffers:
uint8_t *received = BigBuf_calloc(MAX_FRAME_SIZE);
uint8_t *receivedPar = BigBuf_calloc(MAX_PARITY_SIZE);
uint8_t *resp = BigBuf_calloc(20);
if (received == NULL || receivedPar == NULL || resp == NULL) {
tag_response_info_t *responses = (tag_response_info_t *)BigBuf_calloc(ANTIFUZZ_RESP_COUNT * sizeof(tag_response_info_t));
uint8_t *respdata = BigBuf_calloc(64);
uint8_t *modulation = BigBuf_calloc(ANTIFUZZ_MOD_SIZE);
// one byte per possible BCC, so each precomputed answer has something stable
// to point its trace payload at
uint8_t *bcc_bytes = (mode == ANTIFUZZ_MODE_COLLISION) ? BigBuf_calloc(256) : respdata;
if (received == NULL || receivedPar == NULL || responses == NULL || respdata == NULL ||
modulation == NULL || bcc_bytes == NULL) {
if (g_dbglevel >= DBG_ERROR) DbpString("Anti-fuzz: failed to allocate buffers");
reply_ng(CMD_HF_ISO14443A_ANTIFUZZ, PM3_EMALLOC, NULL, 0);
switch_off();
@@ -3245,56 +3508,190 @@ void iso14443a_antifuzz(uint32_t flags) {
return;
}
// BigBuf_calloc() already zeroed the receive buffers
memset(resp, 0xFF, 20);
// ATQA, bit 7..6 of the first byte announce the UID size, and that size is
// also how many cascade levels the reader expects to walk before the UID is
// complete: 1 for a 4 byte UID, 2 for 7 bytes, 3 for 10 bytes.
uint8_t atqa0 = 0x04;
uint8_t announced_levels = 1;
const uint8_t *uid_src = antifuzz_uid_4b;
if (IS_FLAG_UID_IN_DATA(flags, 7)) {
atqa0 = 0x44;
announced_levels = 2;
uid_src = antifuzz_uid_7b;
} else if (IS_FLAG_UID_IN_DATA(flags, 10)) {
atqa0 = 0x84;
announced_levels = 3;
uid_src = antifuzz_uid_10b;
}
uint8_t *free_buffer = modulation;
size_t free_size = ANTIFUZZ_MOD_SIZE;
uint8_t *d = respdata;
bool ok = true;
responses[ANTIFUZZ_RESP_ATQA].response = d;
responses[ANTIFUZZ_RESP_ATQA].response_n = 2;
d[0] = atqa0;
d[1] = 0x00;
d += 2;
// SAK with the cascade bit set - this, not the UID, is what keeps the reader
// asking for one more level
responses[ANTIFUZZ_RESP_SAK].response = d;
responses[ANTIFUZZ_RESP_SAK].response_n = 3;
d[0] = 0x04;
AddCrc14A(d, 1);
d += 3;
ok = prepare_allocated_tag_modulation(&responses[ANTIFUZZ_RESP_ATQA], &free_buffer, &free_size) &&
prepare_allocated_tag_modulation(&responses[ANTIFUZZ_RESP_SAK], &free_buffer, &free_size);
// Cascade mode walks a reader past every level ISO 14443-3 defines, so it
// needs an answer for each. Collision mode makes the reader earn each level,
// and only needs the announced ones plus a block of filler beyond them.
uint8_t nblocks = (mode == ANTIFUZZ_MODE_COLLISION) ? (announced_levels + 1) : ANTIFUZZ_MAX_LEVEL;
uint8_t coll_from = (mode == ANTIFUZZ_MODE_COLLISION) ? ANTIFUZZ_COLL_FROM : ANTIFUZZ_NVB_COUNT;
for (uint8_t b = 0; ok && b < nblocks; b++) {
uint8_t *uid = d;
d += 5;
antifuzz_level_uid(uid, b + 1, announced_levels, uid_src);
// One answer per NVB the reader can ask with. Cascade mode never
// collides, so a conforming reader only ever asks with NVB 0x20 there,
// but answering a bit oriented request with the whole UID hands it a
// frame of the wrong length and it drops the card.
for (uint8_t known = 0; ok && known < ANTIFUZZ_NVB_COUNT; known++) {
tag_response_info_t *ri = &responses[ANTIFUZZ_RESP_BASE + (b * ANTIFUZZ_NVB_COUNT) + known];
ri->response = uid + (known >> 3);
ri->response_n = 5 - (known >> 3);
CodeIso14443aAsTagAnticollTail(uid, known, coll_from);
ok = antifuzz_store_modulation(ri, &free_buffer, &free_size);
}
}
if (mode == ANTIFUZZ_MODE_COLLISION) {
// The one answer in this mode that does not collide. By the time the
// reader asks with all 32 UID bits it has told us every one it settled
// on, so the BCC it is owed is just those four bytes XORed - whichever
// way it came down the tree. All 256 are ready, and none of them cares
// which cascade level or which UID it is answering for.
for (uint16_t b = 0; ok && b < 256; b++) {
tag_response_info_t *ri = &responses[ANTIFUZZ_RESP_BCC + b];
bcc_bytes[b] = (uint8_t)b;
ri->response = &bcc_bytes[b];
ri->response_n = 1;
ok = prepare_allocated_tag_modulation(ri, &free_buffer, &free_size);
}
}
if (ok == false) {
if (g_dbglevel >= DBG_ERROR) DbpString("Anti-fuzz: failed to prepare tag answers");
reply_ng(CMD_HF_ISO14443A_ANTIFUZZ, PM3_EMALLOC, NULL, 0);
switch_off();
BigBuf_free_keep_EM();
return;
}
uint8_t max_level = 0; // deepest cascade level a reader was driven to
uint8_t max_known = 0; // and the most UID bits it claimed at that level
uint16_t rounds = 0; // anticollision attempts it started
uint16_t depth = 0; // how far into the current attempt it has got
// LED A lit for the whole run
// LED B lit while an anticollision attempt is in progress, dark once the
// reader has given up on it and gone back to polling
// LED C flips every time the reader gets one step deeper than it has been
// in this attempt, so a fast flicker is a reader walking the UID tree
// LED D flips on every frame we answer
LED_A_ON();
LED_B_OFF();
LED_C_OFF();
for (;;) {
WDT_HIT();
// Clean receive command buffer
if (GetIso14443aCommandFromReader(received, MAX_FRAME_SIZE, receivedPar, &len) == false) {
Dbprintf("Anti-fuzz stopped. Trace length: %d ", BigBuf_get_traceLen());
Dbprintf("Anti-fuzz stopped. " _YELLOW_("%u") " anticollision rounds, deepest was cascade level "
_YELLOW_("%u") " at " _YELLOW_("%u") " UID bits, trace length " _YELLOW_("%d"),
rounds, max_level, max_known, BigBuf_get_traceLen());
break;
}
if (received[0] == ISO14443A_CMD_WUPA || received[0] == ISO14443A_CMD_REQA) {
resp[0] = 0x04;
resp[1] = 0x00;
if (IS_FLAG_UID_IN_DATA(flags, 7)) {
resp[0] = 0x44;
}
EmSendCmd(resp, 2);
continue;
}
// Received request for UID (cascade 1)
//if (received[1] >= 0x20 && received[1] <= 0x57 && received[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT) {
if (received[1] >= 0x20 && received[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT) {
resp[0] = 0xFF;
resp[1] = 0xFF;
resp[2] = 0xFF;
resp[3] = 0xFF;
resp[4] = resp[0] ^ resp[1] ^ resp[2] ^ resp[3];
if (IS_FLAG_UID_IN_DATA(flags, 7)) {
resp[0] = MIFARE_SELECT_CT;
}
// trigger a faulty/collision response
EmSendCmdEx(resp, 5, true);
if (g_dbglevel >= DBG_EXTENDED) Dbprintf("ANTICOLL or SELECT %x", received[1]);
if (len == 1 && (received[0] == ISO14443A_CMD_WUPA || received[0] == ISO14443A_CMD_REQA)) {
// polling from scratch, so whatever the reader had collected is gone
LED_B_OFF();
depth = 0;
EmSendPrecompiledCmd(&responses[ANTIFUZZ_RESP_ATQA]);
LED_D_INV();
continue;
} else if (received[1] == 0x20 && received[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2) { // Received request for UID (cascade 2)
if (g_dbglevel >= DBG_EXTENDED) Dbprintf("ANTICOLL or SELECT_2");
} else if (received[1] == 0x70 && received[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT) { // Received a SELECT (cascade 1)
} else if (received[1] == 0x70 && received[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2) { // Received a SELECT (cascade 2)
} else {
Dbprintf("unknown command %x", received[0]);
}
// Everything below is a SEL frame. ISO 14443-3 only defines cascade
// levels 1..3 (0x93 / 0x95 / 0x97), the levels above them are the whole
// point of the exercise.
if (len < 2 || received[0] < ISO14443A_CMD_ANTICOLL_OR_SELECT ||
received[0] > 0x9F || (received[0] & 1) == 0) {
// a frame we do not answer is never traced by EmLogTrace, and a
// reader that walked off somewhere unexpected is exactly what this
// command is looking for, so log it here
LogTrace(received, len, Uart.startTime * 16 - DELAY_AIR2ARM_AS_TAG,
Uart.endTime * 16 - DELAY_AIR2ARM_AS_TAG, Uart.parity, true);
if (g_dbglevel >= DBG_EXTENDED) Dbprintf("unknown command %02x (len %d)", received[0], len);
continue;
}
uint8_t level = ((received[0] - ISO14443A_CMD_ANTICOLL_OR_SELECT) >> 1) + 1;
if (level > max_level && g_dbglevel >= DBG_INFO) {
Dbprintf("cascade level " _YELLOW_("%u") " (SEL %02x)", level, received[0]);
}
if (depth == 0) {
rounds++;
LED_B_ON();
}
// worked out once, before the frame delay this has to answer inside
uint16_t block = ANTIFUZZ_RESP_BASE + (antifuzz_block_of(nblocks, level) * ANTIFUZZ_NVB_COUNT);
uint8_t nvb = received[1];
// SELECT: SEL 70 uid0 uid1 uid2 uid3 bcc crc crc. The reader claims all
// 40 bits here, which is as deep as a cascade level goes.
if (nvb == 0x70) {
antifuzz_track_depth(level, 40, &depth, &max_level, &max_known);
EmSendPrecompiledCmd(&responses[ANTIFUZZ_RESP_SAK]);
LED_D_INV();
continue;
}
// ANTICOLLISION. NVB counts SEL and NVB themselves, so 0x20 means "no
// UID bits known" and 0x67 means "39 bits known".
if (nvb < 0x20 || nvb > 0x67 || (nvb & 0x0F) > 7) {
LogTrace(received, len, Uart.startTime * 16 - DELAY_AIR2ARM_AS_TAG,
Uart.endTime * 16 - DELAY_AIR2ARM_AS_TAG, Uart.parity, true);
if (g_dbglevel >= DBG_EXTENDED) Dbprintf("bad NVB %02x (SEL %02x)", nvb, received[0]);
continue;
}
uint8_t known = (uint8_t)((((nvb >> 4) - 2) * 8) + (nvb & 0x0F));
antifuzz_track_depth(level, known, &depth, &max_level, &max_known);
// SEL 60 u0 u1 u2 u3 - the reader has resolved the whole UID and wants the
// BCC for it. Give it the real one: its SELECT then succeeds, and the
// SAK cascades it into the same exercise one level deeper.
if (mode == ANTIFUZZ_MODE_COLLISION && known == 32 && len >= 6) {
uint8_t bcc = received[2] ^ received[3] ^ received[4] ^ received[5];
EmSendPrecompiledCmd(&responses[ANTIFUZZ_RESP_BCC + bcc]);
LED_D_INV();
continue;
}
EmSendPrecompiledCmd(&responses[block + known]);
LED_D_INV();
}
reply_ng(CMD_HF_ISO14443A_ANTIFUZZ, PM3_SUCCESS, NULL, 0);
+3 -1
View File
@@ -168,13 +168,15 @@ bool SimulateIso14443aInit(uint8_t tagType, uint16_t flags, uint8_t *data,
uint8_t *ulc_key);
bool GetIso14443aCommandFromReader(uint8_t *received, uint16_t received_maxlen, uint8_t *par, int *len);
void iso14443a_antifuzz(uint32_t flags);
void iso14443a_antifuzz(uint32_t flags, uint8_t mode);
int GetATQA(uint8_t *resp, uint16_t resp_len, uint8_t *resp_par, const iso14a_polling_parameters_t *polling_parameters);
void ReaderIso14443a(PacketCommandNG *c);
void ReaderTransmit(const uint8_t *frame, uint16_t len, uint32_t *timing);
void ReaderTransmitBitsPar(const uint8_t *frame, uint16_t bits, uint8_t *par, uint32_t *timing);
void ReaderTransmitPar(const uint8_t *frame, uint16_t len, uint8_t *par, uint32_t *timing);
uint16_t ReaderReceive(uint8_t *receivedAnswer, uint16_t answer_maxlen, uint8_t *par);
uint32_t iso14a_last_tx_end(void);
uint32_t iso14a_last_rx_start(void);
void iso14443a_setup(uint8_t fpga_minor_mode);
int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, uint16_t data_len, uint8_t *res);
+35 -5
View File
@@ -2049,20 +2049,28 @@ static int CmdHF14AAntiFuzz(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf 14a antifuzz",
"Tries to fuzz the ISO14443a anticollision phase",
"hf 14a antifuzz -4\n");
"Tries to fuzz the ISO14443a anticollision phase.\n"
"Default keeps the cascade bit set in every SAK, so the reader asks for\n"
"one more cascade level until its UID buffer gives up. With --coll only\n"
"the first UID byte goes out clean and every bit after it collides, so the\n"
"reader has to resolve 24 bits one round trip at a time; it is then given a\n"
"BCC that checks out, and the SAK sends it round again one level deeper.",
"hf 14a antifuzz -4\n"
"hf 14a antifuzz --10 --coll\n");
void *argtable[] = {
arg_param_begin,
arg_lit0("4", NULL, "4 byte uid"),
arg_lit0("7", NULL, "7 byte uid"),
arg_lit0(NULL, "10", "10 byte uid"),
arg_lit0("4", NULL, "4 byte uid"),
arg_lit0("7", NULL, "7 byte uid"),
arg_lit0(NULL, "10", "10 byte uid"),
arg_lit0(NULL, "coll", "collision storm instead of cascade overflow"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
struct {
uint8_t flag;
uint8_t mode;
} PACKED param;
param.flag = 0;
FLAG_SET_UID_IN_DATA(param.flag, 4);
@@ -2072,10 +2080,32 @@ static int CmdHF14AAntiFuzz(const char *Cmd) {
if (arg_get_lit(ctx, 3)) {
FLAG_SET_UID_IN_DATA(param.flag, 10);
}
param.mode = arg_get_lit(ctx, 4) ? ANTIFUZZ_MODE_COLLISION : ANTIFUZZ_MODE_CASCADE;
CLIParserFree(ctx);
clearCommandBuffer();
SendCommandNG(CMD_HF_ISO14443A_ANTIFUZZ, (uint8_t *)&param, sizeof(param));
PrintAndLogEx(INFO, "Press " _GREEN_("pm3 button") " or " _GREEN_("<Enter>") " to abort");
PacketResponseNG resp;
bool keypress = kbd_enter_pressed();
while (keypress == false) {
keypress = kbd_enter_pressed();
if (WaitForResponseTimeout(CMD_HF_ISO14443A_ANTIFUZZ, &resp, 500)) {
break;
}
}
if (keypress) {
// inform device to break the fuzz loop since the client has exited
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
WaitForResponse(CMD_HF_ISO14443A_ANTIFUZZ, &resp);
}
PrintAndLogEx(INFO, "Done!");
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf 14a list") "` to view the fuzzed anticollision");
return PM3_SUCCESS;
}
+67 -39
View File
@@ -49,7 +49,12 @@ enum MifareAuthSeq {
static enum MifareAuthSeq MifareAuthState;
static AuthData_t AuthData;
// Set while the previous reader frame was a bit oriented ANTICOLLISION, whose
// answer completes a split byte and so carries no CRC either.
static bool gs_14a_anticoll_answer = false;
void ClearAuthData(void) {
gs_14a_anticoll_answer = false;
AuthData.uid = 0;
AuthData.nt = 0;
AuthData.first_auth = true;
@@ -74,6 +79,29 @@ static uint8_t *gs_mfuc_key = NULL;
*/
uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint8_t n) {
// Neither half of a bit oriented ANTICOLLISION exchange carries a CRC: the
// request stops mid byte and the answer completes it. NVB counts SEL and
// NVB themselves, so 0x20..0x67 is the anticollision range and only a
// SELECT, NVB 0x70, ends in a CRC. `hf 14a antifuzz` pushes a reader past
// cascade level 3, hence the whole odd 0x93..0x9F range.
//
// The answer carries nothing that identifies it, so it is recognised by the
// request it follows - frames reach us in order.
if (isResponse == false) {
gs_14a_anticoll_answer = (n >= 2 &&
(d[0] & 1) &&
d[0] >= ISO14443A_CMD_ANTICOLL_OR_SELECT &&
d[0] <= 0x9F &&
d[1] >= 0x20 && d[1] <= 0x67);
if (gs_14a_anticoll_answer) {
return 2;
}
} else if (gs_14a_anticoll_answer) {
gs_14a_anticoll_answer = false;
return 2;
}
if (n < 3) {
return 2;
}
@@ -82,11 +110,6 @@ uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint8_t n) {
return 2;
}
if (d[1] == 0x50 &&
d[0] >= ISO14443A_CMD_ANTICOLL_OR_SELECT &&
d[0] <= ISO14443A_CMD_ANTICOLL_OR_SELECT_3) {
return 2;
}
return check_crc(CRC_14443_A, d, n);
}
@@ -266,40 +289,45 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i
case ISO14443A_CMD_WUPA:
snprintf(exp, size, "WUPA");
break;
case ISO14443A_CMD_ANTICOLL_OR_SELECT: {
// 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
// 93 50 = Bit oriented anti-collision (usage: 9350+ up to 5bytes, 9350 answer - up to 5bytes UID+BCC)
// 93 70 = Select (usage: 9370+5bytes 9370 answer - answer: 1byte SAK)
if (cmd[1] == 0x70)
snprintf(exp, size, "SELECT_UID");
else if (cmd[1] == 0x20 || cmd[1] == 0x50)
snprintf(exp, size, "ANTICOLL");
else
snprintf(exp, size, "SELECT_XXX");
break;
}
case ISO14443A_CMD_ANTICOLL_OR_SELECT_2: {
//95 20 = Anticollision of cascade level2
//95 50 = Bit oriented anti-collision level2
//95 70 = Select of cascade level2
if (cmd[1] == 0x70)
snprintf(exp, size, "SELECT_UID-2");
else if (cmd[1] == 0x20 || cmd[1] == 0x50)
snprintf(exp, size, "ANTICOLL-2");
else
snprintf(exp, size, "SELECT_XXX-2");
break;
}
case ISO14443A_CMD_ANTICOLL_OR_SELECT_3: {
//97 20 = Anticollision of cascade level3
//97 50 = Bit oriented anti-collision level3
//97 70 = Select of cascade level3
if (cmd[1] == 0x70)
snprintf(exp, size, "SELECT_UID-3");
else if (cmd[1] == 0x20 || cmd[1] == 0x50)
snprintf(exp, size, "ANTICOLL-3");
else
snprintf(exp, size, "SELECT_XXX-3");
// 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
// 93 50 = Bit oriented anti-collision (usage: 9350+ up to 5bytes, 9350 answer - up to 5bytes UID+BCC)
// 93 70 = Select (usage: 9370+5bytes 9370 answer - answer: 1byte SAK)
// 95 xx / 97 xx are the same for cascade level 2 and 3. ISO 14443-3
// defines no level above 3, but `hf 14a antifuzz` walks a reader up
// the whole odd 0x93..0x9F range, so decode that range too.
case ISO14443A_CMD_ANTICOLL_OR_SELECT:
case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:
case ISO14443A_CMD_ANTICOLL_OR_SELECT_3:
case 0x99:
case 0x9B:
case 0x9D:
case 0x9F: {
// one suffix for every level past the first, so an ordinary
// trace keeps reading "ANTICOLL" / "SELECT_UID"
char lvl[8] = {0};
uint8_t level = ((cmd[0] - ISO14443A_CMD_ANTICOLL_OR_SELECT) >> 1) + 1;
if (level > 1) {
snprintf(lvl, sizeof(lvl), "-%u", level);
}
if (cmdsize < 2) {
snprintf(exp, size, "SELECT_XXX%s", lvl);
} else if (cmd[1] == 0x70) {
snprintf(exp, size, "SELECT_UID%s", lvl);
} else if (cmd[1] >= 0x20 && cmd[1] <= 0x67 && (cmd[1] & 0x0F) < 8) {
// NVB counts SEL and NVB themselves, so 0x20 is "no UID bits
// known" and 0x67 is "39 known". Anything in between is a
// bit oriented request, and how far the reader has got is
// the whole story of a collision storm.
uint8_t known = (uint8_t)((((cmd[1] >> 4) - 2) * 8) + (cmd[1] & 0x0F));
if (known) {
snprintf(exp, size, "ANTICOLL%s(" _MAGENTA_("%u") " bit%s)", lvl, known, (known == 1) ? "" : "s");
} else {
snprintf(exp, size, "ANTICOLL%s", lvl);
}
} else {
snprintf(exp, size, "SELECT_XXX%s", lvl);
}
break;
}
case ISO14443A_CMD_REQA:
+8 -6
View File
@@ -1326,18 +1326,20 @@
},
"hf 14a antifuzz": {
"command": "hf 14a antifuzz",
"description": "Tries to fuzz the ISO14443a anticollision phase",
"description": "Tries to fuzz the ISO14443a anticollision phase. Default keeps the cascade bit set in every SAK, so the reader asks for one more cascade level until its UID buffer gives up. With --coll only the first UID byte goes out clean and every bit after it collides, so the reader has to resolve 24 bits one round trip at a time; it is then given a BCC that checks out, and the SAK sends it round again one level deeper.",
"notes": [
"hf 14a antifuzz -4"
"hf 14a antifuzz -4",
"hf 14a antifuzz --10 --coll"
],
"offline": false,
"options": [
"-h, --help This help",
"-4 4 byte uid",
"-7 7 byte uid",
"--10 10 byte uid"
"--10 10 byte uid",
"--coll collision storm instead of cascade overflow"
],
"usage": "hf 14a antifuzz [-h47] [--10]"
"usage": "hf 14a antifuzz [-h47] [--10] [--coll]"
},
"hf 14a apdu": {
"command": "hf 14a apdu",
@@ -16522,8 +16524,8 @@
}
},
"metadata": {
"commands_extracted": 920,
"commands_extracted": 919,
"extracted_by": "PM3Help2JSON v1.00",
"extracted_on": "2026-09-12T12:22:55+00:00"
"extracted_on": "2026-09-12T19:16:35+00:00"
}
}