From 82597a32c5707014b0809c7b30a6e9c186ac88b0 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 22 Sep 2026 11:05:07 +0200 Subject: [PATCH] fix: don't treat a RATS NAK as a successful ISO14443-4 activation iso14443a_select_cardEx() only rejected silence after RATS. A card that answered with a 4 bit NAK came back as Demod.len == 1, was stored as a one byte ATS and reported to the client as select status 1, meaning L4 activated. The client then sent GetVersion (90 60 00 00 00) as an I-block to a card that never left ISO14443-3, and every caller ended with 'timeout while waiting for reply' / 'No tag detected or other tag communication error (-4)'. The bogus TL of 0x04 also made iso14a_set_ATS_times() look for a T0 and read past the single received byte. Check that the answer to RATS is a well formed ATS, TL plus the bytes TL counts plus the two CRC bytes, before accepting it. Anything else returns 2, selected but ISO14443-3 only, so the card stays usable over the Classic path instead of failing detection. The client side 'try to request ATS even if the tag claims not to support it' retry had the same hole and stored whatever came back in card.ats, so guard all four copies of it the same way. Reported on a clone with ATQA 0004 / SAK 28 that advertises ISO14443-4 it does not implement. hf mf autopwn on it needed hf 14a config --rats skip to get past detection; with this it takes the Classic path on its own. Fixes #3659 Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 1 + armsrc/iso14443a.c | 9 +++++++++ client/src/cmdhf14a.c | 8 +++++--- client/src/cmdhfmf.c | 24 +++++++++++++++--------- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4214034d..229ccb8d1 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 14a info` now correctly handles NAK when receiving RATS (@iceman1001) - Fixed `hf mfu restore` to support Ultralight AES (@iceman1001) - Fixed `hf mfu info` to print UL-AES config pages again (@iceman1001) diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index e0af71124..c89130d95 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -4076,6 +4076,15 @@ int iso14443a_select_cardEx(uint8_t *uid_ptr, iso14a_card_select_t *p_card, uint return 0; } + // An ATS is TL, the bytes TL counts, and two CRC bytes on top. + // Anything shorter, like a 4 bit NAK is not ATS + if (len < 3 || resp[0] != len - 2) { + if (g_dbglevel >= DBG_INFO) { + Dbprintf("Card answered RATS with %i byte(s), not an ATS. Staying at ISO14443-3", len); + } + return 2; + } + if (p_card) { memcpy(p_card->ats, resp, sizeof(p_card->ats)); p_card->ats_len = len; diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index ba3b0bf73..dfac1011e 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -2994,9 +2994,11 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_2863); - card.ats_len = rlen_2863; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_2863 > 3) && (resp.data.asBytes[0] == rlen_2863 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_2863); + card.ats_len = rlen_2863; // note: ats_len includes CRC Bytes select_status = 4; } } diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index b37506de7..7d3f8b6fc 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -405,9 +405,11 @@ int mf_read_uid(uint8_t *uid, int *uidlen, int *nxptype) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_398); - card.ats_len = rlen_398; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_398 > 3) && (resp.data.asBytes[0] == rlen_398 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_398); + card.ats_len = rlen_398; // note: ats_len includes CRC Bytes select_status = 4; } } @@ -3199,9 +3201,11 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_3120); - card.ats_len = rlen_3120; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_3120 > 3) && (resp.data.asBytes[0] == rlen_3120 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_3120); + card.ats_len = rlen_3120; // note: ats_len includes CRC Bytes select_status = 4; } } @@ -11458,9 +11462,11 @@ static int CmdHF14AMfInfo(const char *Cmd) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_11266); - card.ats_len = rlen_11266; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_11266 > 3) && (resp.data.asBytes[0] == rlen_11266 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_11266); + card.ats_len = rlen_11266; // note: ats_len includes CRC Bytes select_status = 4; } }