From 7f740c4e3d5b50898aa468081375e84664e2275d Mon Sep 17 00:00:00 2001 From: tcprst Date: Sun, 20 Dec 2020 21:40:06 -0500 Subject: [PATCH] hf legic reader - continuous mode and cliparser --- client/src/cmdhf.c | 2 +- client/src/cmdhflegic.c | 83 +++++++++++++++++++++++++---------------- client/src/cmdhflegic.h | 2 +- doc/cliparser_todo.txt | 1 - 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/client/src/cmdhf.c b/client/src/cmdhf.c index 41f3d0814..4df232637 100644 --- a/client/src/cmdhf.c +++ b/client/src/cmdhf.c @@ -111,7 +111,7 @@ int CmdHFSearch(const char *Cmd) { PROMPT_CLEARLINE; PrintAndLogEx(INPLACE, " Searching for LEGIC tag..."); if (IfPm3Legicrf()) { - if (readLegicUid(false) == PM3_SUCCESS) { + if (readLegicUid(false, false) == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "\nValid " _GREEN_("LEGIC Prime tag") " found\n"); res = PM3_SUCCESS; } diff --git a/client/src/cmdhflegic.c b/client/src/cmdhflegic.c index d53c68e5e..00eff936d 100644 --- a/client/src/cmdhflegic.c +++ b/client/src/cmdhflegic.c @@ -28,16 +28,6 @@ static int CmdHelp(const char *Cmd); #define MAX_LENGTH 1024 -static int usage_legic_reader(void) { - PrintAndLogEx(NORMAL, "Read UID and type information from a LEGIC Prime tag\n"); - PrintAndLogEx(NORMAL, "Usage: hf legic reader [h]\n"); - PrintAndLogEx(NORMAL, "Options:"); - PrintAndLogEx(NORMAL, " h : this help"); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, _YELLOW_(" hf legic reader")); - return PM3_SUCCESS; -} static int usage_legic_dump(void) { PrintAndLogEx(NORMAL, "Read all memory from LEGIC Prime MIM22, MIM256, MIM1024"); PrintAndLogEx(NORMAL, "and saves bin/eml/json dump file"); @@ -146,6 +136,7 @@ static int CmdLegicInfo(const char *Cmd) { arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIParserFree(ctx); int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0; int crc = 0, wrp = 0, wrc = 0; @@ -810,12 +801,26 @@ void legic_seteml(uint8_t *src, uint32_t offset, uint32_t numofbytes) { SendCommandOLD(CMD_HF_LEGIC_ESET, i, len, 0, src + i, len); } } - static int CmdLegicReader(const char *Cmd) { - char cmdp = tolower(param_getchar(Cmd, 0)); - if (cmdp == 'h') return usage_legic_reader(); + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf legic reader", + "Read UID and type information from a LEGIC Prime tag", + "hf legic reader"); - return readLegicUid(true); + void *argtable[] = { + arg_param_begin, + arg_lit0("@", NULL, "optional - continuous reader mode"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + bool cm = arg_get_lit(ctx, 1); + CLIParserFree(ctx); + + if (cm) { + PrintAndLogEx(INFO, "Press " _GREEN_("") " to exit"); + } + + return readLegicUid(cm, true); } static int CmdLegicDump(const char *Cmd) { @@ -1294,24 +1299,38 @@ int CmdHFLegic(const char *Cmd) { return CmdsParse(CommandTable, Cmd); } -int readLegicUid(bool verbose) { +int readLegicUid(bool loop, bool verbose) { + + do { + legic_card_select_t card; + + int resp = legic_get_type(&card); + + if (loop) { + if (resp != PM3_SUCCESS) { + continue; + } + } else { + switch (resp) { + case PM3_EINVARG: + return PM3_EINVARG; + case PM3_ETIMEOUT: + if (verbose) PrintAndLogEx(WARNING, "command execution time out"); + return PM3_ETIMEOUT; + case PM3_ESOFT: + if (verbose) PrintAndLogEx(WARNING, "legic card select failed"); + return PM3_ESOFT; + default: + break; + } + } + + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(SUCCESS, " MCD: " _GREEN_("%02X"), card.uid[0]); + PrintAndLogEx(SUCCESS, " MSN: " _GREEN_("%s"), sprint_hex(card.uid + 1, sizeof(card.uid) - 1)); + legic_print_type(card.cardsize, 0); + + } while (loop && kbd_enter_pressed() == false); - legic_card_select_t card; - switch (legic_get_type(&card)) { - case PM3_EINVARG: - return PM3_EINVARG; - case PM3_ETIMEOUT: - if (verbose) PrintAndLogEx(WARNING, "command execution time out"); - return PM3_ETIMEOUT; - case PM3_ESOFT: - if (verbose) PrintAndLogEx(WARNING, "legic card select failed"); - return PM3_ESOFT; - default: - break; - } - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(SUCCESS, " MCD: " _GREEN_("%02X"), card.uid[0]); - PrintAndLogEx(SUCCESS, " MSN: " _GREEN_("%s"), sprint_hex(card.uid + 1, sizeof(card.uid) - 1)); - legic_print_type(card.cardsize, 0); return PM3_SUCCESS; } diff --git a/client/src/cmdhflegic.h b/client/src/cmdhflegic.h index 9e9411a33..379ae13c7 100644 --- a/client/src/cmdhflegic.h +++ b/client/src/cmdhflegic.h @@ -17,7 +17,7 @@ int CmdHFLegic(const char *Cmd); -int readLegicUid(bool verbose); +int readLegicUid(bool loop, bool verbose); int legic_print_type(uint32_t tagtype, uint8_t spaces); int legic_get_type(legic_card_select_t *card); void legic_chk_iv(uint32_t *iv); diff --git a/doc/cliparser_todo.txt b/doc/cliparser_todo.txt index 212029e2c..26ee2f34b 100644 --- a/doc/cliparser_todo.txt +++ b/doc/cliparser_todo.txt @@ -78,7 +78,6 @@ hf felica resetmode hf felica litesim hf felica litedump hf fido info -hf legic reader hf legic dump hf legic restore hf legic eload