diff --git a/CHANGELOG.md b/CHANGELOG.md index bbdcf5061..b8a40a173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,15 +3,16 @@ 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 relay` command where it relays between two pm3 devices over internet. Thanks to Moerno for the code! (@iceman1001) - Changed `hf mf acl` command to have more recognized generic configurations (@team-orangeBlue) - Added `hf mfp acl` command (@team-orangeBlue) -- Modified `data_hex_crc.lua` script, it now takes a `-s` parameter and the output is now in alternative row colors. (@iceman1001) +- Modified `data_hex_crc.lua` script, it now takes a `-s` parameter and the output is now in alternative row colors. (@iceman1001) - Added `hf mfdes brutedamslot` command (@kormax) - Added `hf mfdes getdelegateappinfo` command (@kormax) - Added `hf secc info` command to retrieve the Card Recognition Template(@antiklesys) - Added `hf mfdes createdelegateapp` command (@kormax) - Optimized `hf iclass legbrute` throughput: replaced recursive `suc()`/`output()` cipher functions with iterative loops, added 256-entry LUT for the `select()` function eliminating redundant bit arithmetic and halving key lookups per state step, switched successor state to in-place pointer update removing per-call struct copies, added `doMAC_brute()` with byte-wise LSB-first processing and direct output bit packing eliminating all bitstream struct overhead and output reversal calls per key candidate, and replaced per-iteration 64-bit modulo progress check with a countdown counter (@antiklesys) -- Improved `hf iclass legbrute`: fixed multithreaded key-range partitioning so threads cover non-overlapping slices of the 40-bit keyspace, added ETA display, keyboard abort with resume hint, `_Atomic` correctness for shared state, `pthread_create` error handling, and thread count capped at available CPUs (@antiklesys) +- Improved `hf iclass legbrute` fixed multithreaded key-range partitioning so threads cover non-overlapping slices of the 40-bit keyspace, added ETA display, keyboard abort with resume hint, `_Atomic` correctness for shared state, `pthread_create` error handling, and thread count capped at available CPUs (@antiklesys) - Added wildcard support to `hf secc sim` payloads (@antiklesys) - Added `hf secc` to build a base for simulating basic function of iclass SE config cards (@antiklesys) - Improved SIO parsing for `hf iclass view` based on Iceman's "Dismantling the SEOS Protocol" talk (@antiklesys) diff --git a/client/src/cmdlf.c b/client/src/cmdlf.c index f3c03dde0..690df5603 100644 --- a/client/src/cmdlf.c +++ b/client/src/cmdlf.c @@ -22,6 +22,9 @@ #include #include #include +#include +#include + #include "cmdparser.h" // command_t #include "comms.h" #include "commonutil.h" // ARRAYLEN @@ -70,6 +73,8 @@ #include "crc.h" #include "pm3_cmd.h" // for LF_CMDREAD_MAX_EXTRA_SYMBOLS #include "fpga.h" // for set_fpga_mode +#include "util_posix.h" // msleep + static int CmdHelp(const char *Cmd); @@ -1704,6 +1709,214 @@ static int check_autocorrelate(const char *prefix, int clock) { return PM3_EFAILED; } +static int lf_relay_tag(uint64_t samples, uint16_t port) { + + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + PrintAndLogEx(ERR, "Failed to create socket"); + return PM3_EFAILED; + } + + struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = INADDR_ANY }; + int opt = 1; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + PrintAndLogEx(ERR, "Failed to bind to port " _RED_("%u"), port); + close(sock); + return PM3_EFAILED; + } + + if (listen(sock, 1) < 0) { + PrintAndLogEx(ERR, "Failed to listen on socket"); + close(sock); + return PM3_EFAILED; + } + + PrintAndLogEx(INFO, "Relay listening on port " _YELLOW_("%u") "...", port); + + int client = accept(sock, NULL, NULL); + if (client < 0) { + PrintAndLogEx(ERR, "Failed to accept connection"); + close(sock); + return PM3_EFAILED; + } + + while (true) { + + if (kbd_enter_pressed()) { + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); + PrintAndLogEx(DEBUG, "\naborted via keyboard!"); + msleep(300); + break; + } + + lf_read_internal(false, false, samples); + + if (g_GraphTraceLen > 1000 && !getSignalProperties()->isnoise) { + + PrintAndLogEx(INFO, "Tag detected! Sending %zu samples to Client...", g_GraphTraceLen); + + uint32_t len = (uint32_t)g_GraphTraceLen; + if (send(client, &len, sizeof(len), 0) < 0) { + break; + } + + if (send(client, g_GraphBuffer, len * sizeof(int32_t), 0) < 0) { + break; + } + + msleep(500); + } + + } + close(client); + close(sock); + return PM3_SUCCESS; +} + +static int lf_relay_rdr(const char *ip, uint16_t port) { + + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + PrintAndLogEx(ERR, "Failed to create socket"); + return PM3_EFAILED; + } + + struct sockaddr_in addr = {0}; + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + + if (inet_pton(AF_INET, ip, &addr.sin_addr) <= 0) { + PrintAndLogEx(ERR, "Invalid IP address... %s:%u", ip, port); + close(sock); + return PM3_EFAILED; + } + + if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + PrintAndLogEx(ERR, "Connection error to %s:%u", ip, port); + close(sock); + return PM3_EFAILED; + } + + PrintAndLogEx(INFO, "Relay connected to %s:%u", ip, port); + PrintAndLogEx(INFO, "Waiting for signal..."); + PrintAndLogEx(INFO, "Press " _GREEN_("") " to exit"); + PrintAndLogEx(NORMAL, ""); + + int n = 0; + do { + + if (kbd_enter_pressed()) { + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); + PrintAndLogEx(DEBUG, "\naborted via keyboard!"); + msleep(300); + break; + } + + uint32_t incoming_len = 0; + + n = recv(sock, &incoming_len, sizeof(incoming_len), MSG_WAITALL); + + if (n > 0 && incoming_len > 0) { + + if (incoming_len > MAX_GRAPH_TRACE_LEN) { + PrintAndLogEx(ERR, "Received length " _RED_("%u") " exceeds buffer size %u, dropping", incoming_len, MAX_GRAPH_TRACE_LEN); + break; + } + + PrintAndLogEx(INFO, "Received " _YELLOW_("%u") " samples. Processing...", incoming_len); + ssize_t rx = recv(sock, g_GraphBuffer, incoming_len * sizeof(int32_t), MSG_WAITALL); + + if (rx != (ssize_t)(incoming_len * sizeof(int32_t))) { + PrintAndLogEx(ERR, "Short read: expected %u bytes, got %zd", incoming_len * (uint32_t)sizeof(int32_t), rx); + break; + } + + // if previous simulation running, we need to break it. + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); + msleep(300); + + g_GraphTraceLen = incoming_len; + lf_chk_bitstream(); + lfsim_upload_gb(); + struct { + uint16_t len; + uint16_t gap; + } PACKED payload; + payload.len = (g_GraphTraceLen > UINT16_MAX) ? UINT16_MAX : (uint16_t)g_GraphTraceLen; + payload.gap = 0; + + clearCommandBuffer(); + SendCommandNG(CMD_LF_SIMULATE, (uint8_t *)&payload, sizeof(payload)); + + PrintAndLogEx(SUCCESS, "Simulation active."); + } + + } while (n > 0); + + close(sock); + + return PM3_SUCCESS; +} + +int CmdLFRelay(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "lf relay", + "Relay LF signal between two Proxmark3 devices over TCP.\n" + "By default it uses PORT 8000.\n" + "One device acts as a Tag Proxy, the other as a Reader Client.", + "lf relay --tag -s 7000\n" + "lf relay --rdr --ip 192.168.1.141" + ); + + void *argtable[] = { + arg_param_begin, + arg_lit0(NULL, "tag", "Act as Tag Proxy (Server)"), + arg_lit0(NULL, "rdr", "Act as Reader Client (Connects to Proxy)"), + arg_str0("i", "ip", "", "Target IP address for Reader mode"), + arg_u64_0("s", "samples", "", "Number of samples to collect (default 40000)"), + arg_u64_0("p", "port", "", "Port number (def: 8000)"), + arg_param_end + }; + + CLIExecWithReturn(ctx, Cmd, argtable, true); + + bool is_tag = arg_get_lit(ctx, 1); + bool is_rdr = arg_get_lit(ctx, 2); + + int iplen = 0; + char ip[256] = { 0 }; + CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)ip, sizeof(ip), &iplen); + + uint64_t samples = arg_get_u64_def(ctx, 4, 40000); + uint16_t port = arg_get_u32_def(ctx, 5, 8000) & 0xFFFF; + + CLIParserFree(ctx); + + // sanitize + if ((is_tag + is_rdr) == 0) { + PrintAndLogEx(ERR, "Specify either --tag or --rdr"); + return PM3_EINVARG; + } + + if (is_rdr && (iplen == 0)) { + PrintAndLogEx(ERR, "IP address is empty"); + PrintAndLogEx(HINT, "try `" _YELLOW_("lf relay --rdr --ip ") "`"); + return PM3_EINVARG; + } + + // main + if (is_tag) { + return lf_relay_tag(samples, port); + } + + if (is_rdr) { + return lf_relay_rdr(ip, port); + } + + return PM3_SUCCESS; +} + int CmdLFfind(const char *Cmd) { CLIParserContext *ctx; @@ -2173,6 +2386,7 @@ static command_t CommandTable[] = { {"config", CmdLFConfig, IfPm3Lf, "Get/Set config for LF sampling, bit/sample, decimation, frequency"}, {"cmdread", CmdLFCommandRead, IfPm3Lf, "Modulate LF reader field to send command before read"}, {"read", CmdLFRead, IfPm3Lf, "Read LF tag"}, + {"relay", CmdLFRelay, IfPm3Lf, "LF relay between two pm3 devices (tag/rdr mode)"}, {"search", CmdLFfind, AlwaysAvailable, "Read and Search for valid known tag"}, {"sim", CmdLFSim, IfPm3Lf, "Simulate LF tag from buffer"}, {"simask", CmdLFaskSim, IfPm3Lf, "Simulate " _YELLOW_("ASK") " tag"}, diff --git a/client/src/cmdlf.h b/client/src/cmdlf.h index 176021427..add9fbfa4 100644 --- a/client/src/cmdlf.h +++ b/client/src/cmdlf.h @@ -39,6 +39,7 @@ int CmdLFSimBidir(const char *Cmd); int CmdLFSniff(const char *Cmd); int CmdVchDemod(const char *Cmd); int CmdLFfind(const char *Cmd); +int CmdLFRelay(const char *Cmd); int lf_read(bool verbose, uint64_t samples); int lf_sniff(bool realtime, bool verbose, uint64_t samples);