diff --git a/client/src/cmdlfsecurakey.c b/client/src/cmdlfsecurakey.c index 30e997421..333530e46 100644 --- a/client/src/cmdlfsecurakey.c +++ b/client/src/cmdlfsecurakey.c @@ -14,7 +14,7 @@ // See LICENSE.txt for the text of the license. //----------------------------------------------------------------------------- // Low frequency Securakey tag commands -// ASK/Manchester, RF/40, 96 bits long (unknown cs) +// ASK/Manchester, RF/40, 96 bits long (standard) or 64 bits long (short) //----------------------------------------------------------------------------- #include "cmdlfsecurakey.h" #include // memcpy @@ -34,39 +34,29 @@ static int CmdHelp(const char *Cmd); -//see ASKDemod for what args are accepted -int demodSecurakey(bool verbose) { - (void) verbose; // unused so far +static void securakey_short_encode(uint32_t cn, uint8_t *raw) { + uint32_t raw1 = 0x7FC00000; + uint32_t raw2 = (cn & 0xFF) | ((cn << 1) & 0x0001FE00) | ((cn << 2) & 0x03FC0000); + num_to_bytes(raw1, 4, raw); + num_to_bytes(raw2, 4, raw + 4); +} - //ASK / Manchester - bool st = false; - if (ASKDemod_ext(40, 0, 0, 0, false, false, false, 1, &st) != PM3_SUCCESS) { - PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: ASK/Manchester Demod failed"); +static int securakey_decode_64(const uint8_t *bits) { + uint32_t raw1 = bytebits_to_byte((uint8_t *)bits, 32); + uint32_t raw2 = bytebits_to_byte((uint8_t *)bits + 32, 32); + if (raw1 != 0x7FC00000 || (raw2 & 0x04020100) != 0) { return PM3_ESOFT; } - if (st) - return PM3_ESOFT; - - size_t size = g_DemodBufferLen; - int ans = detectSecurakey(g_DemodBuffer, &size); - if (ans < 0) { - if (ans == -1) - PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: too few bits found"); - else if (ans == -2) - PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: preamble not found"); - else if (ans == -3) - PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: Size not correct: %zu", size); - else - PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: ans: %d", ans); - return PM3_ESOFT; - } - setDemodBuff(g_DemodBuffer, 96, ans); - setClockGrid(g_DemodClock, g_DemodStartIdx + (ans * g_DemodClock)); + uint32_t cn = (raw2 & 0xFF) | ((raw2 >> 1) & 0xFF00) | ((raw2 >> 2) & 0xFF0000); + PrintAndLogEx(SUCCESS, "Securakey Short - CN: " _GREEN_("%u") ", Raw: %08X%08X", cn, raw1, raw2); + return PM3_SUCCESS; +} +static int securakey_decode_96(const uint8_t *bits, bool verbose) { //got a good demod - uint32_t raw1 = bytebits_to_byte(g_DemodBuffer, 32); - uint32_t raw2 = bytebits_to_byte(g_DemodBuffer + 32, 32); - uint32_t raw3 = bytebits_to_byte(g_DemodBuffer + 64, 32); + uint32_t raw1 = bytebits_to_byte((uint8_t *)bits, 32); + uint32_t raw2 = bytebits_to_byte((uint8_t *)bits + 32, 32); + uint32_t raw3 = bytebits_to_byte((uint8_t *)bits + 64, 32); // 26 bit format // preamble ??bitlen reserved EPx xxxxxxxy yyyyyyyy yyyyyyyOP CS? CS2? @@ -81,13 +71,13 @@ int demodSecurakey(bool verbose) { // standard wiegand parities. // unknown checksum 11 bits? at the end uint8_t bits_no_spacer[85]; - memcpy(bits_no_spacer, g_DemodBuffer + 11, 85); + memcpy(bits_no_spacer, bits + 11, 85); // remove marker bits (0's every 9th digit after preamble) (pType = 3 (always 0s)) - size = removeParity(bits_no_spacer, 0, 9, 3, 85); + size_t size = removeParity(bits_no_spacer, 0, 9, 3, 85); if (size != 85 - 9) { PrintAndLogEx(DEBUG, "DEBUG: Error removeParity: %zu", size); - return 0; + return PM3_ESOFT; } uint8_t bitLen = (uint8_t)bytebits_to_byte(bits_no_spacer + 2, 6); @@ -96,6 +86,10 @@ int demodSecurakey(bool verbose) { PrintAndLogEx(DEBUG, "DEBUG: Error bitLen too long: %u", bitLen); return PM3_ESOFT; } + if (bitLen < 18) { + PrintAndLogEx(DEBUG, "DEBUG: Error bitLen too short: %u", bitLen); + return PM3_ESOFT; + } // get left 1/2 wiegand & right 1/2 wiegand (for parity test and wiegand print) lWiegand = bytebits_to_byte(bits_no_spacer + 48 - bitLen, bitLen / 2); rWiegand = bytebits_to_byte(bits_no_spacer + 48 - bitLen + bitLen / 2, bitLen / 2); @@ -122,20 +116,91 @@ int demodSecurakey(bool verbose) { return PM3_SUCCESS; } +//see ASKDemod for what args are accepted +int demodSecurakey(bool verbose) { + //ASK / Manchester + bool st = false; + if (ASKDemod_ext(40, 0, 0, 0, false, false, false, 1, &st) != PM3_SUCCESS) { + PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: ASK/Manchester Demod failed"); + return PM3_ESOFT; + } + if (st) + return PM3_ESOFT; + + size_t size = g_DemodBufferLen; + int ans = detectSecurakey(g_DemodBuffer, &size); + if (ans < 0) { + if (ans == -1) + PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: too few bits found"); + else if (ans == -2) + PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: preamble not found"); + else if (ans == -3) + PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: Size not correct: %zu", size); + else + PrintAndLogEx(DEBUG, "DEBUG: Error - Securakey: ans: %d", ans); + return PM3_ESOFT; + } + setDemodBuff(g_DemodBuffer, size, ans); + setClockGrid(g_DemodClock, g_DemodStartIdx + (ans * g_DemodClock)); + + if (size == 64) { + return securakey_decode_64(g_DemodBuffer); + } + return securakey_decode_96(g_DemodBuffer, verbose); +} + static int CmdSecurakeyDemod(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "lf securakey demod", "Try to find Securakey preamble, if found decode / descramble data", - "lf securakey demod" + "lf securakey demod\n" + "lf securakey demod --raw 7FCB400001ADEA5344300000\n" + "lf securakey demod --raw 7fc000000065cc92" ); void *argtable[] = { arg_param_begin, + arg_str0("r", "raw", "", "raw hex data. 8 bytes (short) or 12 bytes (standard)"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); + + int raw_len = 0; + uint8_t raw[12] = {0}; + int res = CLIParamHexToBuf(arg_get_str(ctx, 1), raw, sizeof(raw), &raw_len); CLIParserFree(ctx); - return demodSecurakey(true); + + if (res) { + return PM3_EINVARG; + } + + if (raw_len == 0) { + return demodSecurakey(true); + } + + if (raw_len == 8) { + uint8_t bits[64]; + bytes_to_bytebits(raw, 8, bits); + if (securakey_decode_64(bits) != PM3_SUCCESS) { + PrintAndLogEx(FAILED, "Invalid Securakey Short raw data"); + return PM3_ESOFT; + } + return PM3_SUCCESS; + } + + if (raw_len == 12) { + uint8_t bits[96]; + bytes_to_bytebits(raw, 12, bits); + size_t size = sizeof(bits); + if (detectSecurakey(bits, &size) != 0 || size != 96) { + PrintAndLogEx(FAILED, "Invalid Securakey raw data"); + return PM3_ESOFT; + } + return securakey_decode_96(bits, true); + } + + PrintAndLogEx(FAILED, "Raw data must be 8 bytes (short) or 12 bytes (standard), got " _YELLOW_("%d"), raw_len); + return PM3_EINVARG; } static int CmdSecurakeyReader(const char *Cmd) { @@ -173,12 +238,14 @@ static int CmdSecurakeyClone(const char *Cmd) { "clone a Securakey tag to a T55x7, Q5/T5555 or EM4305/4469 tag.", "lf securakey clone --raw 7FCB400001ADEA5344300000 -> encode for T55x7 tag\n" "lf securakey clone --raw 7FCB400001ADEA5344300000 --q5 -> encode for Q5/T5555 tag\n" - "lf securakey clone --raw 7FCB400001ADEA5344300000 --em -> encode for EM4305/4469" + "lf securakey clone --raw 7FCB400001ADEA5344300000 --em -> encode for EM4305/4469\n" + "lf securakey clone --cn 1697426 -> encode 24-bit short for T55x7 tag" ); void *argtable[] = { arg_param_begin, - arg_str1("r", "raw", "", "raw hex data. 12 bytes"), + arg_str0("r", "raw", "", "raw hex data. 8 bytes (short) or 12 bytes (standard)"), + arg_int0(NULL, "cn", "", "24-bit card number (Securakey Short)"), 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 @@ -186,51 +253,77 @@ static int CmdSecurakeyClone(const char *Cmd) { CLIExecWithReturn(ctx, Cmd, argtable, false); int raw_len = 0; - // skip first block, 3*4 = 12 bytes left uint8_t raw[12] = {0}; - CLIGetHexWithReturn(ctx, 1, raw, &raw_len); - bool q5 = arg_get_lit(ctx, 2); - bool em = arg_get_lit(ctx, 3); + int res = CLIParamHexToBuf(arg_get_str(ctx, 1), raw, sizeof(raw), &raw_len); + int cn_arg = arg_get_int_def(ctx, 2, -1); + bool q5 = arg_get_lit(ctx, 3); + bool em = arg_get_lit(ctx, 4); CLIParserFree(ctx); + if (res) { + return PM3_EINVARG; + } + if (q5 && em) { PrintAndLogEx(FAILED, "Can't specify both Q5 and EM4305 at the same time"); return PM3_EINVARG; } - if (raw_len != 12) { - PrintAndLogEx(ERR, "Data must be 12 bytes (24 HEX characters) %d", raw_len); + bool use_raw = (raw_len > 0); + if (use_raw && cn_arg != -1) { + PrintAndLogEx(FAILED, "Can't specify both --raw and --cn at the same time"); + return PM3_EINVARG; + } + if (!use_raw && cn_arg == -1) { + PrintAndLogEx(FAILED, "Must specify either --raw or --cn"); return PM3_EINVARG; } - uint32_t blocks[4]; - for (uint8_t i = 1; i < ARRAYLEN(blocks); i++) { + if (!use_raw) { + if (cn_arg < 0 || cn_arg > 0xFFFFFF) { + PrintAndLogEx(FAILED, "CN must be between 0 and 16777215"); + return PM3_EINVARG; + } + securakey_short_encode((uint32_t)cn_arg, raw); + raw_len = 8; + } else if (raw_len != 8 && raw_len != 12) { + PrintAndLogEx(ERR, "Data must be 8 or 12 bytes (16 or 24 HEX characters), got %d", raw_len); + return PM3_EINVARG; + } + + size_t data_blocks = (raw_len == 8) ? 2 : 3; + uint32_t blocks[4] = {0}; + for (size_t i = 1; i <= data_blocks; i++) { blocks[i] = bytes_to_num(raw + ((i - 1) * 4), sizeof(uint32_t)); } - //Securakey - compat mode, ASK/Man, data rate 40, 3 data blocks - blocks[0] = T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_40 | 3 << T55x7_MAXBLOCK_SHIFT; + //Securakey - compat mode, ASK/Man, data rate 40, 2 or 3 data blocks + blocks[0] = (raw_len == 8) ? T55X7_SECURAKEY_SHORT_CONFIG_BLOCK + : (T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_40 | 3 << T55x7_MAXBLOCK_SHIFT); char cardtype[16] = {"T55x7"}; // Q5 if (q5) { - blocks[0] = T5555_FIXED | T5555_MODULATION_MANCHESTER | T5555_SET_BITRATE(40) | T5555_ST_TERMINATOR | 3 << T5555_MAXBLOCK_SHIFT; + blocks[0] = T5555_FIXED | T5555_MODULATION_MANCHESTER | T5555_SET_BITRATE(40) | T5555_ST_TERMINATOR | (uint32_t)(data_blocks << T5555_MAXBLOCK_SHIFT); snprintf(cardtype, sizeof(cardtype), "Q5/T5555"); } // EM4305 if (em) { - blocks[0] = EM4305_SECURAKEY_CONFIG_BLOCK; + blocks[0] = (raw_len == 8) ? EM4305_SECURAKEY_SHORT_CONFIG_BLOCK : EM4305_SECURAKEY_CONFIG_BLOCK; snprintf(cardtype, sizeof(cardtype), "EM4305/4469"); } - PrintAndLogEx(INFO, "Preparing to clone Securakey to " _YELLOW_("%s") " with raw hex", cardtype); - print_blocks(blocks, ARRAYLEN(blocks)); + PrintAndLogEx(INFO, "Preparing to clone Securakey%s to " _YELLOW_("%s") " with %s", + (raw_len == 8) ? " Short" : "", + cardtype, + use_raw ? "raw hex" : "card number" + ); + print_blocks(blocks, data_blocks + 1); - int res; if (em) { - res = em4x05_clone_tag(blocks, ARRAYLEN(blocks), 0, false); + res = em4x05_clone_tag(blocks, data_blocks + 1, 0, false); } else { - res = clone_t55xx_tag(blocks, ARRAYLEN(blocks)); + res = clone_t55xx_tag(blocks, data_blocks + 1); } PrintAndLogEx(SUCCESS, "Done!"); PrintAndLogEx(HINT, "Hint: Try " _YELLOW_("`lf securakey reader`") " to verify"); @@ -241,35 +334,62 @@ static int CmdSecurakeySim(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "lf securakey sim", - "Enables simulation of secura card with specified card number.\n" + "Enables simulation of Securakey card with raw hex or 24-bit card number.\n" "Simulation runs until the button is pressed or another USB command is issued.", - "lf securakey sim --raw 7FCB400001ADEA5344300000" + "lf securakey sim --raw 7FCB400001ADEA5344300000\n" + "lf securakey sim --cn 1697426" ); void *argtable[] = { arg_param_begin, - arg_str0("r", "raw", "", " raw hex data. 12 bytes"), + arg_str0("r", "raw", "", "raw hex data. 8 bytes (short) or 12 bytes (standard)"), + arg_int0(NULL, "cn", "", "24-bit card number (Securakey Short)"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); int raw_len = 0; - // skip first block, 3*4 = 12 bytes left uint8_t raw[12] = {0}; - CLIGetHexWithReturn(ctx, 1, raw, &raw_len); + int res = CLIParamHexToBuf(arg_get_str(ctx, 1), raw, sizeof(raw), &raw_len); + int cn_arg = arg_get_int_def(ctx, 2, -1); CLIParserFree(ctx); - if (raw_len != 12) { - PrintAndLogEx(ERR, "Data must be 12 bytes (24 HEX characters) %d", raw_len); + if (res) { return PM3_EINVARG; } - PrintAndLogEx(SUCCESS, "Simulating SecuraKey - raw " _YELLOW_("%s"), sprint_hex_inrow(raw, sizeof(raw))); + bool use_raw = (raw_len > 0); + if (use_raw && cn_arg != -1) { + PrintAndLogEx(FAILED, "Can't specify both --raw and --cn at the same time"); + return PM3_EINVARG; + } + if (!use_raw && cn_arg == -1) { + PrintAndLogEx(FAILED, "Must specify either --raw or --cn"); + return PM3_EINVARG; + } - uint8_t bs[sizeof(raw) * 8]; - bytes_to_bytebits(raw, sizeof(raw), bs); + if (!use_raw) { + if (cn_arg < 0 || cn_arg > 0xFFFFFF) { + PrintAndLogEx(FAILED, "CN must be between 0 and 16777215"); + return PM3_EINVARG; + } + securakey_short_encode((uint32_t)cn_arg, raw); + raw_len = 8; + } else if (raw_len != 8 && raw_len != 12) { + PrintAndLogEx(ERR, "Data must be 8 or 12 bytes (16 or 24 HEX characters), got %d", raw_len); + return PM3_EINVARG; + } - lf_asksim_t *payload = calloc(1, sizeof(lf_asksim_t) + sizeof(bs)); + PrintAndLogEx(SUCCESS, "Simulating SecuraKey%s - raw " _YELLOW_("%s"), + (raw_len == 8) ? " Short" : "", + sprint_hex_inrow(raw, (size_t)raw_len) + ); + + size_t bit_len = (size_t)raw_len * 8; + uint8_t bs[96] = {0}; + bytes_to_bytebits(raw, (size_t)raw_len, bs); + + lf_asksim_t *payload = calloc(1, sizeof(lf_asksim_t) + bit_len); if (payload == NULL) { PrintAndLogEx(WARNING, "Failed to allocate memory"); return PM3_EMALLOC; @@ -278,20 +398,13 @@ static int CmdSecurakeySim(const char *Cmd) { payload->invert = 0; payload->separator = 0; payload->clock = 40; - memcpy(payload->data, bs, sizeof(bs)); + memcpy(payload->data, bs, bit_len); clearCommandBuffer(); - SendCommandNG(CMD_LF_ASK_SIMULATE, (uint8_t *)payload, sizeof(lf_asksim_t) + sizeof(bs)); + SendCommandNG(CMD_LF_ASK_SIMULATE, (uint8_t *)payload, sizeof(lf_asksim_t) + bit_len); free(payload); - PacketResponseNG resp; - WaitForResponse(CMD_LF_ASK_SIMULATE, &resp); - - PrintAndLogEx(INFO, "Done!"); - if (resp.status != PM3_EOPABORTED) { - return resp.status; - } - return PM3_SUCCESS; + return lfsim_wait_check(CMD_LF_ASK_SIMULATE); } static command_t CommandTable[] = { @@ -317,13 +430,60 @@ int CmdLFSecurakey(const char *Cmd) { // by marshmellow // find Securakey preamble in already demoded data int detectSecurakey(uint8_t *dest, size_t *size) { - if (*size < 96) return -1; //make sure buffer has data - size_t startIdx = 0; - uint8_t preamble[] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1}; - if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx)) - return -2; //preamble not found - if (*size != 96) return -3; //wrong demoded size - //return start position - return (int)startIdx; + size_t orig_size = *size; + if (orig_size < 64) return -1; //make sure buffer has data + + int long_err = -2; + size_t long_err_size = orig_size; + + if (orig_size >= 96) { + size_t startIdx = 0; + size_t found_size = orig_size; + uint8_t preamble[] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1}; + if (preambleSearch(dest, preamble, sizeof(preamble), &found_size, &startIdx)) { + if (found_size == 96) { + *size = 96; + return (int)startIdx; + } + long_err = -3; + long_err_size = found_size; + } + } + + // Fallback: 64-bit Securakey Short (`0x7FC00000` preamble + marker-free CN bytes) + static const uint8_t short_preamble[] = { + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + for (size_t i = 0; i + 64 + sizeof(short_preamble) <= orig_size; i++) { + if (memcmp(dest + i, short_preamble, sizeof(short_preamble)) == 0 && + memcmp(dest + i + 64, short_preamble, sizeof(short_preamble)) == 0) { + uint32_t raw2 = bytebits_to_byte(dest + i + 32, 32); + if ((raw2 & 0x04020100) == 0) { + *size = 64; + return (int)i; + } + } + } + + if (orig_size < 96) { + for (size_t i = 0; i + 64 <= orig_size; i++) { + if (memcmp(dest + i, short_preamble, sizeof(short_preamble)) == 0) { + uint32_t raw2 = bytebits_to_byte(dest + i + 32, 32); + if ((raw2 & 0x04020100) == 0) { + *size = 64; + return (int)i; + } + } + } + return -1; + } + + if (long_err == -3) { + *size = long_err_size; + return -3; + } + return -2; //preamble not found }