Merge pull request #3758 from matrix/fix-destron-sim

lf destron: implement sim, refuse uids the frame can't carry
This commit is contained in:
Iceman authored and GitHub committed 2026-11-08 08:23:00 +08:00
commit bf3d37e00a
4 files changed
+88 -24

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 `lf destron sim` - simulates the FDX-A frame that `lf destron clone` writes (@matrix)
- Fixed `lf destron clone` - a uid that is not 5 bytes of 00-7F is refused instead of written as another id or with bad parity (@matrix)
- Fixed `lf fdxb` decoding - ISO 11784:2024 and ISO 24631-6 fields and names, and many more fixes. See commit message (@matrix)
- Fixed `lf fdxb clone` and `sim` - a country above 1023 or a national code above 274877906943 is refused instead of cut, `sim` sends the data block flag for `--extended 000000` (@matrix)
- Added `lf fdxb clone` and `sim` `--biothermo` and `--no-datablock` - a Bio-Thermo temperature, or any `--extended` trailer, sent with the data block flag clear as Bio-Thermo chips do (@matrix)
+79 -19
View File
@@ -17,6 +17,7 @@
//-----------------------------------------------------------------------------
#include "cmdlfdestron.h"
#include <ctype.h> // tolower
#include <stdlib.h> // calloc
#include <string.h> // memcpy
#include "commonutil.h" // ARRAYLEN
#include "common.h"
@@ -134,6 +135,37 @@ static int CmdDestronReader(const char *Cmd) {
return PM3_SUCCESS;
}
// a uid is five bytes of 7 bits, the top bit of each carries its parity
static int checkDestronUid(const uint8_t *data, int datalen) {
if (datalen != 5) {
PrintAndLogEx(FAILED, "Uid must include 5 hex bytes (10 hex symbols), got " _YELLOW_("%d"), datalen);
return PM3_EINVARG;
}
for (int i = 0; i < datalen; i++) {
if (data[i] & 0x80) {
PrintAndLogEx(FAILED, "Uid byte %d is " _YELLOW_("%02X") ", each byte holds 7 bits (00-7F)", i + 1, data[i]);
return PM3_EINVARG;
}
}
return PM3_SUCCESS;
}
// the three data blocks of a frame, as cloned and simulated
static void getDestronBlocks(const uint8_t *data, int datalen, uint32_t *blocks) {
uint8_t data_ex[12 + 24] = {0}; // ManchesterEncode need extra room
for (int i = 0; i < datalen; i++) {
data_ex[i + 1] = ~(data [i] | (oddparity8(data[i]) << 7));
}
// manchester encode it
for (int i = 0; i < 3; i++) {
blocks[i] = manchesterEncode2Bytes((data_ex[i * 2] << 8) + data_ex[i * 2 + 1]);
}
// inject preamble
blocks[0] = (blocks[0] & 0xFFFF) | 0xAAE20000;
}
static int CmdDestronClone(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "lf destron clone",
@@ -145,7 +177,7 @@ static int CmdDestronClone(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_str1("u", "uid", "<hex>", "5 bytes max"),
arg_str1("u", "uid", "<hex>", "5 hex bytes, 00-7F each"),
arg_lit0(NULL, "q5", "optional - specify writing to Q5/T5555 tag"),
arg_lit0(NULL, "em", "optional - specify writing to EM4305/4469 tag"),
arg_param_end
@@ -166,8 +198,7 @@ static int CmdDestronClone(const char *Cmd) {
return PM3_EINVARG;
}
if (datalen > 5) {
PrintAndLogEx(FAILED, "Uid is max 5 bytes. (got %u)", datalen);
if (checkDestronUid(data, datalen) != PM3_SUCCESS) {
return PM3_EINVARG;
}
@@ -187,17 +218,7 @@ static int CmdDestronClone(const char *Cmd) {
snprintf(cardtype, sizeof(cardtype), "EM4305/4469");
}
uint8_t data_ex[12 + 24] = {0}; // ManchesterEncode need extra room
for (int i = 0; i < datalen; i++) {
data_ex[i + 1] = ~(data [i] | (oddparity8(data[i]) << 7));
}
// manchester encode it
for (int i = 0; i < 3; i++) {
blocks[i + 1] = manchesterEncode2Bytes((data_ex[i * 2] << 8) + data_ex[i * 2 + 1]);
}
// inject preamble
blocks[1] = (blocks[1] & 0xFFFF) | 0xAAE20000;
getDestronBlocks(data, datalen, blocks + 1);
PrintAndLogEx(INFO, "Preparing to clone Destron tag to " _YELLOW_("%s") " with ID: " _YELLOW_("%s")
, cardtype
@@ -220,18 +241,57 @@ static int CmdDestronClone(const char *Cmd) {
static int CmdDestronSim(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "lf destron sim",
"Try to find Destron preamble, if found decode / descramble data",
"lf destron sim"
"Enables simulation of Destron card with specified uid.\n"
"Simulation runs until the button is pressed or another USB command is issued.",
"lf destron sim --uid 1A2B3C4D5E"
);
void *argtable[] = {
arg_param_begin,
arg_str1("u", "uid", "<hex>", "5 hex bytes, 00-7F each"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
CLIExecWithReturn(ctx, Cmd, argtable, false);
uint8_t data[8];
int datalen = 0;
CLIGetHexWithReturn(ctx, 1, data, &datalen);
CLIParserFree(ctx);
PrintAndLogEx(INFO, " To be implemented, feel free to contribute!");
return PM3_ENOTIMPL;
if (checkDestronUid(data, datalen) != PM3_SUCCESS) {
return PM3_EINVARG;
}
uint32_t blocks[3] = {0};
getDestronBlocks(data, datalen, blocks);
PrintAndLogEx(SUCCESS, "Simulating Destron - ID: " _YELLOW_("%s") ", Raw: " _YELLOW_("%08X%08X%08X")
, sprint_hex_inrow(data, datalen)
, blocks[0], blocks[1], blocks[2]
);
uint8_t bs[96];
for (size_t i = 0; i < ARRAYLEN(blocks); i++) {
num_to_bytebits(blocks[i], 32, bs + (i * 32));
}
// FSK2 as in clone: 1 is fc/8, 0 is fc/10
lf_fsksim_t *payload = calloc(1, sizeof(lf_fsksim_t) + sizeof(bs));
if (payload == NULL) {
PrintAndLogEx(WARNING, "Failed to allocate memory");
return PM3_EMALLOC;
}
payload->fchigh = 8;
payload->fclow = 10;
payload->separator = 0;
payload->clock = 50;
memcpy(payload->data, bs, sizeof(bs));
clearCommandBuffer();
SendCommandNG(CMD_LF_FSK_SIMULATE, (uint8_t *)payload, sizeof(lf_fsksim_t) + sizeof(bs));
free(payload);
return lfsim_wait_check(CMD_LF_FSK_SIMULATE);
}
static command_t CommandTable[] = {
+6 -5
View File
@@ -11796,7 +11796,7 @@
"offline": false,
"options": [
"-h, --help This help",
"-u, --uid <hex> 5 bytes max",
"-u, --uid <hex> 5 hex bytes, 00-7F each",
"--q5 optional - specify writing to Q5/T5555 tag",
"--em optional - specify writing to EM4305/4469 tag"
],
@@ -11829,15 +11829,16 @@
},
"lf destron sim": {
"command": "lf destron sim",
"description": "Try to find Destron preamble, if found decode / descramble data",
"description": "Enables simulation of Destron card with specified uid. Simulation runs until the button is pressed or another USB command is issued.",
"notes": [
"lf destron sim"
"lf destron sim --uid 1A2B3C4D5E"
],
"offline": false,
"options": [
"-h, --help This help"
"-h, --help This help",
"-u, --uid <hex> 5 hex bytes, 00-7F each"
],
"usage": "lf destron sim [-h]"
"usage": "lf destron sim [-h] -u <hex>"
},
"lf em 410x brute": {
"command": "lf em 410x brute",
+1
View File
@@ -577,6 +577,7 @@ while true; do
if ! CheckExecute "lf EM4x70 recover test 2/3" "$CLIENTBIN -c 'lf em 4x70 recover --key 022A028C02BE --rnd 7D5167003571F8 --frn 982DBCC0 --grn 36C0E0'" "022a028c02be366866191b60"; then break; fi
if ! CheckExecute "lf EM4x70 recover test 3/3" "$CLIENTBIN -c 'lf em 4x70 recover --key 022A028C02BE --rnd 7D5167003571F8 --frn 982DBCC0 --grn 36C0E0'" "022a028c02bef1e352c2718d"; then break; fi
if ! CheckExecute "lf FDX-A FECAVA test" "$CLIENTBIN -c 'data load -f traces/lf_EM4305_fdxa_destron.pm3;lf search -1'" "FDX-A FECAVA Destron ID found"; then break; fi
if ! CheckExecute "lf destron sim help" "$CLIENTBIN -c 'lf destron sim -h'" "lf destron sim --uid 1A2B3C4D5E"; then break; fi
if ! CheckExecute "lf FDX-B test" "$CLIENTBIN -c 'data load -f traces/lf_HomeAgain1600.pm3;lf search -1'" "FDX-B ID found"; then break; fi
if ! CheckExecute "lf FDX/BioThermo test" "$CLIENTBIN -c 'data load -f traces/lf_FDXB_Bio-Thermo.pm3; lf fdxb demod'" "95.20 F / 35.11 C"; then break; fi
if ! CheckExecute "lf GPROXII test" "$CLIENTBIN -c 'data load -f traces/lf_GProx_36_30_14489.pm3; lf search -1'" "Guardall G-Prox II ID found"; then break; fi