From 49e5b502f50b599aed0247f03d9cdce1e57b5850 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Sat, 7 Nov 2026 00:25:21 +0100 Subject: [PATCH 1/2] lf keri: refuse values the frame can't carry, print the sim id clone took negative FC and CN, and MS values above 5 and 22 bits, without a word: MS FC 40 wrote the card of FC 8. sim let ids of 32 bits or more change the fixed bits, so --id 2147483648 simulated id 0, and printed the shifted frame as Internal Id. The sim help example used --cn, the option is --id, and the clone hint named lf keri read. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + client/src/cmdlfkeri.c | 32 ++++++++++++++++++++++++++------ doc/commands.json | 4 ++-- tools/pm3_tests.sh | 1 + 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1bedd944..2eb0d46dd 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 `lf keri clone` and `sim` - values the frame can't carry are refused instead of cut, `sim` prints the id and its raw, the help example and the clone hint name `--id` and `lf keri reader` (@matrix) - Fixed flashing with redirected output - the progress animation is drawn only when stdout is a terminal (@matrix) - Fixed `proxmark3 --flash`, `--dumpmem` and `--reboot-to-bootloader` exit status - a failure exits 1, a failed flash no longer ends with `All done` (@matrix) - Fixed `data rawdemod` - `--max`, `--samples` and the `--p2` clock no longer wrap above 255, `--fs` refuses values above 255 (@matrix) diff --git a/client/src/cmdlfkeri.c b/client/src/cmdlfkeri.c index 4fc5cac72..996f14c89 100644 --- a/client/src/cmdlfkeri.c +++ b/client/src/cmdlfkeri.c @@ -248,8 +248,8 @@ static int CmdKeriClone(const char *Cmd) { int typeLen = sizeof(keritype) - 1; // CLIGetStrWithReturn does not guarantee string to be null-terminated CLIGetStrWithReturn(ctx, 1, keritype, &typeLen); - uint32_t fc = arg_get_int_def(ctx, 2, 0); - uint32_t cid = arg_get_int_def(ctx, 3, 0); + int fc_arg = arg_get_int_def(ctx, 2, 0); + int cn_arg = arg_get_int_def(ctx, 3, 0); bool q5 = arg_get_lit(ctx, 4); bool em = arg_get_lit(ctx, 5); CLIParserFree(ctx); @@ -259,6 +259,20 @@ static int CmdKeriClone(const char *Cmd) { return PM3_EINVARG; } + if (fc_arg < 0 || cn_arg < 0) { + PrintAndLogEx(FAILED, "FC and CN can't be negative"); + return PM3_EINVARG; + } + + // the MS scramble carries 5 bits of fc and 22 of cn + if (keritype[0] == 'm' && (fc_arg > 31 || cn_arg > 0x3FFFFF)) { + PrintAndLogEx(FAILED, "MS FC has a max value of 31 and CN has a max value of 4194303"); + return PM3_EINVARG; + } + + uint32_t fc = (uint32_t)fc_arg; + uint32_t cid = (uint32_t)cn_arg; + // Setup card data/build internal id uint32_t internalid = 0; switch (keritype[0]) { @@ -310,7 +324,7 @@ static int CmdKeriClone(const char *Cmd) { } PrintAndLogEx(SUCCESS, "Done!"); - PrintAndLogEx(HINT, "Hint: Try " _YELLOW_("`lf keri read`") " to verify"); + PrintAndLogEx(HINT, "Hint: Try " _YELLOW_("`lf keri reader`") " to verify"); return res; } @@ -320,7 +334,7 @@ static int CmdKeriSim(const char *Cmd) { CLIParserInit(&ctx, "lf keri sim", "Enables simulation of KERI card with internal ID.\n" "You supply a KERI card id and it will converted to a KERI internal ID.", - "lf keri sim --cn 112233" + "lf keri sim --id 112233" ); void *argtable[] = { @@ -332,6 +346,14 @@ static int CmdKeriSim(const char *Cmd) { uint64_t internalid = arg_get_u64_def(ctx, 1, 0); CLIParserFree(ctx); + // the frame carries 31 bits of internal id + if (internalid > 0x7FFFFFFF) { + PrintAndLogEx(FAILED, "Internal Id has a max value of 2147483647"); + return PM3_EINVARG; + } + + PrintAndLogEx(SUCCESS, "Simulating KERI - Internal Id " _YELLOW_("%" PRIu64) ", Raw: E0000000%08" PRIX64, internalid, internalid | 0x80000000); + internalid |= 0x80000000; internalid <<= 3; internalid += 7; @@ -343,8 +365,6 @@ static int CmdKeriSim(const char *Cmd) { bs[j++] = ((internalid >> i) & 1); } - PrintAndLogEx(SUCCESS, "Simulating KERI - Internal Id " _YELLOW_("%" PRIu64), internalid); - lf_psksim_t *payload = calloc(1, sizeof(lf_psksim_t) + sizeof(bs)); if (payload == NULL) { PrintAndLogEx(WARNING, "Failed to allocate memory"); diff --git a/doc/commands.json b/doc/commands.json index fd2b16458..e078e175f 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -13773,7 +13773,7 @@ "command": "lf keri sim", "description": "Enables simulation of KERI card with internal ID. You supply a KERI card id and it will converted to a KERI internal ID.", "notes": [ - "lf keri sim --cn 112233" + "lf keri sim --id 112233" ], "offline": false, "options": [ @@ -16812,6 +16812,6 @@ "metadata": { "commands_extracted": 936, "extracted_by": "PM3Help2JSON v1.00", - "extracted_on": "2026-10-23T22:15:17+00:00" + "extracted_on": "2026-11-06T23:18:49+00:00" } } diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 675fdd4dd..b613bb90b 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -614,6 +614,7 @@ while true; do if ! CheckExecute "lf T55 keri_msid test" "$CLIENTBIN -c 'data load -f traces/lf_ATA5577_keri_msid.pm3; lf search -1'" "KERI ID found"; then break; fi if ! CheckExecute "lf T55 keri_msid test 2" "$CLIENTBIN -c 'data load -f traces/lf_ATA5577_keri_msid.pm3; lf keri demod'" \ "Descrambled MS - FC: 6 Card: 12345"; then break; fi + if ! CheckExecute "lf keri sim help example" "$CLIENTBIN -c 'lf keri sim -h'" "lf keri sim --id 112233"; then break; fi # if ! CheckExecute "lf T55 motorola test" "$CLIENTBIN -c 'data load -f traces/lf_ATA5577_motorola.pm3; lf search -1'" "Indala ID found"; then break; fi if ! CheckExecute "lf T55 motorola test 2" "$CLIENTBIN -c 'data load -f traces/lf_ATA5577_motorola.pm3; lf motorola demod'" \ "Motorola - fmt: 26 FC: 258 Card: 2, Raw: A0000000A0002021"; then break; fi From 43223e01925a31887eda04e12b592e179348eae2 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Sat, 7 Nov 2026 01:05:20 +0100 Subject: [PATCH 2/2] hf secc sim: code the UID size in the ATQA The sim sent ATQA 02 00 for 4, 7 and 10 byte UIDs, because the ATQA override replaces the size bits the firmware sets. The PN533 of an SCL3711 stopped after cascade level 1 and never found a 7 or 10 byte UID. The ATQA now codes the size of the UID the device simulates, 4 bytes from emulator memory when the UID is all zero, and the start message says when that happens. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + client/src/cmdhfsecc.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1bedd944..0938a5f6a 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 secc sim` - the ATQA codes the UID size, so readers that check it select a 7 or 10 byte UID, and a zero UID prints as taken from emulator memory (@matrix) - Fixed flashing with redirected output - the progress animation is drawn only when stdout is a terminal (@matrix) - Fixed `proxmark3 --flash`, `--dumpmem` and `--reboot-to-bootloader` exit status - a failure exits 1, a failed flash no longer ends with `All done` (@matrix) - Fixed `data rawdemod` - `--max`, `--samples` and the `--p2` clock no longer wrap above 255, `--fs` refuses values above 255 (@matrix) diff --git a/client/src/cmdhfsecc.c b/client/src/cmdhfsecc.c index ae1ebda62..195ec6a44 100644 --- a/client/src/cmdhfsecc.c +++ b/client/src/cmdhfsecc.c @@ -475,6 +475,11 @@ static int CmdHFHIDConfigSim(const char *Cmd) { char uid_str[2 * sizeof(uid) + 1]; strncpy(uid_str, sprint_hex_inrow(uid, uidlen), sizeof(uid_str) - 1); uid_str[sizeof(uid_str) - 1] = '\0'; + // an all-zero UID makes the device take a 4-byte UID from emulator memory + bool uid_from_emul = (memcmp(uid, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", sizeof(uid)) == 0); + if (uid_from_emul) { + snprintf(uid_str, sizeof(uid_str), "from emulator memory"); + } PrintAndLogEx(INFO, "HID Config Card sim:" " UID " _YELLOW_("%s") " AID " _YELLOW_("%s") @@ -490,7 +495,8 @@ static int CmdHFHIDConfigSim(const char *Cmd) { payload.tagtype = 4; // ISO14443-4 base type; ATQA/SAK/ATS overridden by ARM payload.flags = flags; payload.exitAfter = exitAfterNReads; - payload.atqa[0] = 0x02; // HID Config Card ATQA high byte + // HID Config Card ATQA high byte, b8 b7 code the UID size (ISO/IEC 14443-3) + payload.atqa[0] = 0x02 | ((uid_from_emul || uidlen == 4) ? 0x00 : ((uidlen == 7) ? 0x40 : 0x80)); payload.atqa[1] = 0x00; // HID Config Card ATQA low byte payload.sak = 0x38; // HID Config Card SAK payload.ats_len = (uint8_t)ats_len;