diff --git a/CHANGELOG.md b/CHANGELOG.md index c255ce01f..2942caed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Fixed `trace list` annotations - none reads past its frame, SEOS answers are no longer shown as reader commands, contactless S-blocks, 14b slots, Xerox WUP2 and the Hitag ยต field offsets are corrected (@matrix) - Fixed `trace list -t ht2` and `-c` - a frame over 30 bytes no longer overwrites the Hitag 2 state or stops the client, a frame longer than the rows shown no longer overflows the row buffer (@matrix) - Fixed `trace extract` - no write past the UID buffer or read past the trace, DESFire AES challenges are extracted again (@matrix) +- Fixed `lf indala clone`, `sim` and `brute` with `--fc`/`--cn` - parity and checksum bits as on genuine cards (@matrix) +- Fixed `lf hitag wrbl`, `hts wrbl` and `htu wrbl` - a page out of range or data shorter than 4 bytes is refused (@matrix) +- Fixed `lf hitag eview -1/-s/-m` and `htu rdbl -c` - no read past the label table or the reply (@matrix) +- Fixed `lf hitag` and `lf search` Paxton id - no garbage before the form factor (@matrix) +- Fixed `lf hitag hts rdbl`, `hts -m` and `ta -h` - page 32 under LCK1, mode above 3 refused, help shown (@matrix) +- Fixed `lf noralsy demod` - one demod error outside the frame, like a cut first bit, no longer loses the read (@matrix) +- Fixed `lf noralsy demod` - a card with the 0xBB0314FF first block no longer gets the unknown bits warning (@matrix) +- 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) diff --git a/client/src/cmdlfdestron.c b/client/src/cmdlfdestron.c index a9c6b1bd2..f9f1b06e3 100644 --- a/client/src/cmdlfdestron.c +++ b/client/src/cmdlfdestron.c @@ -17,6 +17,7 @@ //----------------------------------------------------------------------------- #include "cmdlfdestron.h" #include // tolower +#include // calloc #include // 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", "", "5 bytes max"), + arg_str1("u", "uid", "", "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", "", "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[] = { diff --git a/client/src/cmdlfhitag.c b/client/src/cmdlfhitag.c index 23fba9f0b..13d5e26ba 100644 --- a/client/src/cmdlfhitag.c +++ b/client/src/cmdlfhitag.c @@ -250,7 +250,7 @@ static void print_hitag2_paxton(bool show_header, const uint8_t *data) { // [=] 4/0x04 | 39 04 21 1C | 9.!. | RW | User // [=] 5/0x05 | AC 3F 00 06 | .?.. | RW | User - char formfactor[16]; + char formfactor[16] = {0}; switch (data[23]) { case 0x06: { strcat(formfactor, "isocard"); @@ -332,7 +332,7 @@ static void print_hitag2_configuration(uint32_t uid, uint8_t config) { PrintAndLogEx(NORMAL, ""); } -const char *annotation[] = { +static const char *annotation[] = { "UID", "Pwd", "Key/Pwd", "Config", "User", "User", "User", "User", "User", "User", "User", "User" @@ -410,7 +410,7 @@ static void print_hitag2_blocks(uint8_t *d, uint16_t n) { , sprint_hex(d + (i * HITAG_BLOCK_SIZE), HITAG_BLOCK_SIZE) , sprint_ascii(d + (i * HITAG_BLOCK_SIZE), HITAG_BLOCK_SIZE) , lckstr - , annotation[i] + , (i < ARRAYLEN(annotation)) ? annotation[i] : "User" ); } PrintAndLogEx(INFO, "--------+-------------+-------+-----+---------"); @@ -1226,6 +1226,11 @@ static int CmdLFHitag2CheckChallenges(const char *Cmd) { "Test recorded authentications (replay?)", "lf hitag ta" ); + void *argtable[] = { + arg_param_begin, + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); CLIParserFree(ctx); lf_hitag_data_t packet; @@ -1334,6 +1339,11 @@ static int CmdLFHitagWriter(const char *Cmd) { return PM3_EINVARG; } + if ((page < 0) || (page >= HITAG2_MAX_BLOCKS)) { + PrintAndLogEx(WARNING, "Page address out-of-range. Max is %d, got %d", HITAG2_MAX_BLOCKS - 1, page); + return PM3_EINVARG; + } + if (nalen != 0 && nalen != 8) { PrintAndLogEx(WARNING, "Wrong NR/AR len expected 0 or 8, got %d", nalen); return PM3_EINVARG; diff --git a/client/src/cmdlfhitaghts.c b/client/src/cmdlfhitaghts.c index 8fb0d9e79..64a7b3fdd 100644 --- a/client/src/cmdlfhitaghts.c +++ b/client/src/cmdlfhitaghts.c @@ -229,9 +229,9 @@ static int process_hitags_common_args(CLIParserContext *ctx, lf_hitag_data_t *co return PM3_EINVARG; } - uint8_t mode = arg_get_int_def(ctx, 5, 3); + int mode = arg_get_int_def(ctx, 5, 3); - if (mode > 3) { + if ((mode < 0) || (mode > 3)) { PrintAndLogEx(WARNING, "Wrong response protocol mode, expected 0, 1, 2 or 3, got %d", mode); return PM3_EINVARG; } @@ -416,6 +416,7 @@ static int CmdLFHitagSRead(const char *Cmd) { if (page > 255) { PrintAndLogEx(WARNING, "Page address Invalid."); + CLIParserFree(ctx); return PM3_EINVARG; } @@ -423,6 +424,7 @@ static int CmdLFHitagSRead(const char *Cmd) { if (count > HITAGS_MAX_PAGES) { PrintAndLogEx(WARNING, "No more than 64 pages can be read at once."); + CLIParserFree(ctx); return PM3_EINVARG; } @@ -480,7 +482,6 @@ static int CmdLFHitagSRead(const char *Cmd) { // access right if (page_addr == HITAGS_UID_PADR) { PrintAndLogEx(NORMAL, _RED_("RO ")NOLF); - \ } else if (packet.cmd == HTSF_82xx && page_addr > 40) { // using an 82xx (pages>40 are RO) PrintAndLogEx(NORMAL, _RED_("RO ")NOLF); } else if (page_addr == HITAGS_CONFIG_PADR) { @@ -524,7 +525,7 @@ static int CmdLFHitagSRead(const char *Cmd) { PrintAndLogEx(NORMAL, _RED_("RO ")NOLF); else PrintAndLogEx(NORMAL, _GREEN_("RW ")NOLF); - } else if (24 <= page_addr && page_addr <= 32) { + } else if (24 <= page_addr && page_addr <= 31) { if (card->config_page.s.LCK2) PrintAndLogEx(NORMAL, _RED_("RO ")NOLF); else @@ -701,6 +702,7 @@ static int CmdLFHitagSRestore(const char *Cmd) { int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; CLIParamStrToBuf(arg_get_str(ctx, 6), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + CLIParserFree(ctx); if (fnlen == 0) { PrintAndLogEx(ERR, "Must specify a file"); @@ -930,6 +932,16 @@ static int CmdLFHitagSWrite(const char *Cmd) { CLIParserFree(ctx); + if ((page < 0) || (page > 255)) { + PrintAndLogEx(WARNING, "Page address Invalid."); + return PM3_EINVARG; + } + + if (data_len != sizeof(data)) { + PrintAndLogEx(WARNING, "Wrong DATA len expected 4, got %d", data_len); + return PM3_EINVARG; + } + packet.page = page; memcpy(packet.data, data, sizeof(packet.data)); diff --git a/client/src/cmdlfhitagu.c b/client/src/cmdlfhitagu.c index aa9111529..573539a6b 100644 --- a/client/src/cmdlfhitagu.c +++ b/client/src/cmdlfhitagu.c @@ -372,13 +372,16 @@ static int CmdLFHitagURead(const char *Cmd) { if (page >= HITAGU_MAX_BLOCKS) { PrintAndLogEx(WARNING, "Block address out-of-range. Max is 255, got %u", page); + CLIParserFree(ctx); return PM3_EINVARG; } uint32_t count = arg_get_int_def(ctx, 4, 1); - if (count > HITAGU_MAX_BLOCKS) { - PrintAndLogEx(WARNING, "No more than %d blocks can be read at once", HITAGU_MAX_BLOCKS); + // the read response holds HITAGU_MAX_PAGE_ADVANCED_PLUS blocks + if (count > HITAGU_MAX_PAGE_ADVANCED_PLUS) { + PrintAndLogEx(WARNING, "No more than %d blocks can be read at once", HITAGU_MAX_PAGE_ADVANCED_PLUS); + CLIParserFree(ctx); return PM3_EINVARG; } @@ -445,7 +448,7 @@ static int CmdLFHitagURead(const char *Cmd) { count = (user_blocks > page) ? (user_blocks - page) : HITAGU_MAX_BLOCKS; } - for (int i = 0; i < count; ++i) { + for (int i = 0; (i < count) && (i < HITAGU_MAX_PAGE_ADVANCED_PLUS); ++i) { int page_addr = page + i; if (page_addr >= HITAGU_MAX_BLOCKS) { @@ -668,6 +671,16 @@ static int CmdLFHitagUWrite(const char *Cmd) { CLIParserFree(ctx); + if ((page < 0) || (page >= HITAGU_MAX_BLOCKS)) { + PrintAndLogEx(WARNING, "Block address out-of-range. Max is 255, got %d", page); + return PM3_EINVARG; + } + + if (data_len != sizeof(data)) { + PrintAndLogEx(WARNING, "Wrong DATA len expected 4, got %d", data_len); + return PM3_EINVARG; + } + packet.page = page; memcpy(packet.data, data, sizeof(packet.data)); diff --git a/client/src/cmdlfindala.c b/client/src/cmdlfindala.c index 2843b43cf..ae47c3872 100644 --- a/client/src/cmdlfindala.c +++ b/client/src/cmdlfindala.c @@ -1162,13 +1162,15 @@ int getIndalaBits(uint8_t fc, uint16_t cn, uint8_t *bits) { chk += ((cn >> 5) & 1); //y11 == 67 - 30 = 37 chk += ((cn >> 2) & 1); //y14 == 89 - 30 = 59 chk += (cn & 1); //y16 == 71 - 30 = 41 + chk += evenparity8(fc & 0xEE); // x1 x2 x3 x5 x6 x7 - if ((chk & 1) == 0) { // If the sum is even, checksum is '10' (binary) = 2. - bits[62] = 1; - bits[63] = 0; - } else { // If the sum is odd, checksum is '01' (binary) = 1. + // fitted to the genuine Indala and Keri W cards on record + if ((chk & 1) == 0) { // If the sum is even, checksum is '01' (binary) = 1. bits[62] = 0; bits[63] = 1; + } else { // If the sum is odd, checksum is '10' (binary) = 2. + bits[62] = 1; + bits[63] = 0; } // add parity @@ -1178,20 +1180,9 @@ int getIndalaBits(uint8_t fc, uint16_t cn, uint8_t *bits) { // 92 = 62 // 93 = 63 - bits[34] = 0; // parity for odd bits - bits[38] = 0; // parity for even bits - uint8_t p1 = 1; - uint8_t p2 = 1; - - for (int i = 33; i < 64; i++) { - if (i % 2) - p1 ^= bits[i]; - else - p2 ^= bits[i]; - } - - bits[34] = p1; // parity for odd bits - bits[38] = p2; // parity for even bits + // 26-bit wiegand parity, as in getIndalaBits4041x + bits[34] = evenparity16((fc << 4) | (cn >> 12)); + bits[38] = oddparity16(cn & 0x0fff); return PM3_SUCCESS; } diff --git a/client/src/cmdlfnoralsy.c b/client/src/cmdlfnoralsy.c index b10ee7f97..e3278ddfb 100644 --- a/client/src/cmdlfnoralsy.c +++ b/client/src/cmdlfnoralsy.c @@ -46,7 +46,7 @@ int demodNoralsy(bool verbose) { (void) verbose; // unused so far //ASK / Manchester bool st = true; - if (ASKDemod_ext(32, 0, 0, 0, false, false, false, 1, &st) != PM3_SUCCESS) { + if (ASKDemod_ext(32, 0, 100, 0, false, false, false, 1, &st) != PM3_SUCCESS) { if (g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: Error - Noralsy: ASK/Manchester Demod failed"); return PM3_ESOFT; } @@ -104,8 +104,8 @@ int demodNoralsy(bool verbose) { } PrintAndLogEx(SUCCESS, "Noralsy - Card: " _GREEN_("%u")", Year: " _GREEN_("%u") ", Raw: %08X%08X%08X", cardid, year, raw1, raw2, raw3); - if (raw1 != 0xBB0214FF) { - PrintAndLogEx(WARNING, "Unknown bits set in first block! Expected 0xBB0214FF, Found: 0x%08X", raw1); + if ((raw1 != 0xBB0214FF) && (raw1 != 0xBB0314FF)) { + PrintAndLogEx(WARNING, "Unknown bits set in first block! Expected 0xBB0214FF or 0xBB0314FF, Found: 0x%08X", raw1); PrintAndLogEx(WARNING, "Please post this output in forum to further research on this format"); } return PM3_SUCCESS; @@ -346,7 +346,7 @@ int detectNoralsy(uint8_t *dest, size_t *size) { } /* * -* 2520116 | BB0214FF2529900116360000 | 10111011 00000011 00010100 11111111 00100101 00101001 10010000 00000001 00010110 00110110 00000000 00000000 +* 2520116 | BB0314FF2529900116360000 | 10111011 00000011 00010100 11111111 00100101 00101001 10010000 00000001 00010110 00110110 00000000 00000000 * aaa*aaaaiiiYY*iiiicc---- **** iiiiiiii iiiiYYYY YYYY**** iiiiiiii iiiiiiii cccccccc * * a = fixed value BB0*14FF diff --git a/doc/commands.json b/doc/commands.json index 3ab340c7b..7bc8d6832 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -11796,7 +11796,7 @@ "offline": false, "options": [ "-h, --help This help", - "-u, --uid 5 bytes max", + "-u, --uid 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 5 hex bytes, 00-7F each" ], - "usage": "lf destron sim [-h]" + "usage": "lf destron sim [-h] -u " }, "lf em 410x brute": { "command": "lf em 410x brute", diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 2be27a9e1..3280d6e10 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -567,6 +567,7 @@ while true; do echo -e "\n${C_BLUE}Testing LF:${C_NC}" if ! CheckExecute "lf hitag2 test" "$CLIENTBIN -c 'lf hitag test'" "Tests \( ok"; then break; fi + if ! CheckExecute "lf hitag ta help" "$CLIENTBIN -c 'lf hitag ta -h'" "Test recorded authentications"; then break; fi if ! CheckExecute "lf cotag demod test 1/4" "$CLIENTBIN -c 'data load -f traces/lf_cotag_220_8331.pm3; lf cotag demod -c 272 -v'" \ "data hex: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 5 8 2 4 E 0 0 0 0 8 0 2 3 D F 7 7"; then break; fi if ! CheckExecute "lf cotag demod test 2/4" "$CLIENTBIN -c 'data load -f traces/cotag/lf_cotag_passive_02402447_700000.pm3; lf cotag demod -v'" \ @@ -583,6 +584,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