From 1e6fcd0291e55e4c37be58c3970a170c7f279912 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 25 Nov 2020 10:51:54 +0100 Subject: [PATCH] added lf securakey sim - untested --- client/src/cmdlfsecurakey.c | 63 +++++++++++++++++++++++-------------- client/src/util.c | 19 +++++++++++ client/src/util.h | 1 + 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/client/src/cmdlfsecurakey.c b/client/src/cmdlfsecurakey.c index 7315dc9cd..4582f1205 100644 --- a/client/src/cmdlfsecurakey.c +++ b/client/src/cmdlfsecurakey.c @@ -8,10 +8,8 @@ // ASK/Manchester, RF/40, 96 bits long (unknown cs) //----------------------------------------------------------------------------- #include "cmdlfsecurakey.h" - #include // memcpy #include // tolower - #include "commonutil.h" // ARRAYLEN #include "cmdparser.h" // command_t #include "comms.h" @@ -133,22 +131,20 @@ static int CmdSecurakeyClone(const char *Cmd) { void *argtable[] = { arg_param_begin, - arg_str0("r", "raw", "", " raw hex data. 12 bytes max"), + arg_str0("r", "raw", "", " raw hex data. 12 bytes"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); - int datalen = 0; + int raw_len = 0; // skip first block, 3*4 = 12 bytes left uint8_t raw[12] = {0}; - CLIGetHexWithReturn(ctx, 1, raw, &datalen); + CLIGetHexWithReturn(ctx, 1, raw, &raw_len); - if (datalen > 0) { - if (datalen != 12) { - PrintAndLogEx(ERR, "Data must be 8 bytes (16 HEX characters)"); - CLIParserFree(ctx); - return PM3_EINVARG; - } + if (raw_len != 12) { + PrintAndLogEx(ERR, "Data must be 12 bytes (24 HEX characters)"); + CLIParserFree(ctx); + return PM3_EINVARG; } CLIParserFree(ctx); @@ -170,35 +166,56 @@ static int CmdSecurakeyClone(const char *Cmd) { } static int CmdSecurakeySim(const char *Cmd) { - PrintAndLogEx(INFO, _RED_("To be implemented, feel free to contribute!")); - return PM3_SUCCESS; CLIParserContext *ctx; CLIParserInit(&ctx, "lf securakey sim", - "Enables simulation of viking card with specified card number.\n" + "Enables simulation of secura card with specified card number.\n" "Simulation runs until the button is pressed or another USB command is issued.", "lf securakey sim --raw 7FCB400001ADEA5344300000" ); void *argtable[] = { arg_param_begin, - arg_int0("r", "raw", "", " raw hex data. 12 bytes max"), + arg_str0("r", "raw", "", " raw hex data. 12 bytes"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); - int datalen = 0; + + int raw_len = 0; // skip first block, 3*4 = 12 bytes left uint8_t raw[12] = {0}; - CLIGetHexWithReturn(ctx, 1, raw, &datalen); + CLIGetHexWithReturn(ctx, 1, raw, &raw_len); - if (datalen > 0) { - if (datalen != 12) { - PrintAndLogEx(ERR, "Data must be 8 bytes (16 HEX characters)"); - CLIParserFree(ctx); - return PM3_EINVARG; - } + if (raw_len != 12) { + PrintAndLogEx(ERR, "Data must be 12 bytes (24 HEX characters) %d", raw_len); + CLIParserFree(ctx); + return PM3_EINVARG; } CLIParserFree(ctx); + + PrintAndLogEx(SUCCESS, "Simulating SecuraKey - raw " _YELLOW_("%s"), sprint_hex_inrow(raw, sizeof(raw))); + + uint8_t bs[sizeof(raw) * 8]; + bytes_to_bytebits(raw, sizeof(raw), bs); + + lf_asksim_t *payload = calloc(1, sizeof(lf_asksim_t) + sizeof(bs)); + payload->encoding = 1; + payload->invert = 0; + payload->separator = 0; + payload->clock = 40; + memcpy(payload->data, bs, sizeof(bs)); + + clearCommandBuffer(); + SendCommandNG(CMD_LF_ASK_SIMULATE, (uint8_t *)payload, sizeof(lf_asksim_t) + sizeof(bs)); + free(payload); + + PacketResponseNG resp; + WaitForResponse(CMD_LF_ASK_SIMULATE, &resp); + + PrintAndLogEx(INFO, "Done"); + if (resp.status != PM3_EOPABORTED) + return resp.status; + return PM3_SUCCESS; } diff --git a/client/src/util.c b/client/src/util.c index 76a2d7885..e9fd171e2 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -446,6 +446,25 @@ void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest) { } } +void bytes_to_bytebits(void* src, size_t srclen, void* dest) { + + uint8_t *s = (uint8_t*)src; + uint8_t *d = (uint8_t*)dest; + + uint32_t i = srclen * 8; + while (srclen--) { + uint8_t b = s[srclen]; + d[--i] = (b >> 0) & 1; + d[--i] = (b >> 1) & 1; + d[--i] = (b >> 2) & 1; + d[--i] = (b >> 3) & 1; + d[--i] = (b >> 4) & 1; + d[--i] = (b >> 5) & 1; + d[--i] = (b >> 6) & 1; + d[--i] = (b >> 7) & 1; + } +} + // aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp // to // hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii diff --git a/client/src/util.h b/client/src/util.h index 51100731e..28ca74917 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -56,6 +56,7 @@ void print_blocks(uint32_t *data, size_t len); int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen); void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest); void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest); +void bytes_to_bytebits(void* src, size_t srclen, void* dest); // Swap endian on arrays up to 64bytes. uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize);