diff --git a/client/src/cmdwiegand.c b/client/src/cmdwiegand.c index 19f166284..3be648ed4 100644 --- a/client/src/cmdwiegand.c +++ b/client/src/cmdwiegand.c @@ -13,6 +13,7 @@ #include #include #include "cmdparser.h" // command_t +#include "cliparser.h" #include "comms.h" #include "pm3_cmd.h" #include "protocols.h" @@ -24,164 +25,126 @@ static int CmdHelp(const char *Cmd); -static int usage_wiegand_list(void) { - PrintAndLogEx(NORMAL, "List available wiegand formats"); - return PM3_SUCCESS; -} -static int usage_wiegand_encode(void) { - PrintAndLogEx(NORMAL, "Encode wiegand formatted number to raw hex"); - PrintAndLogEx(NORMAL, "Usage: wiegand encode [w ] [ ] {...}"); - PrintAndLogEx(NORMAL, "Options:"); - PrintAndLogEx(NORMAL, " w see `wiegand list` for available formats"); - PrintAndLogEx(NORMAL, " c card number"); - PrintAndLogEx(NORMAL, " f facility code"); - PrintAndLogEx(NORMAL, " i issue Level"); - PrintAndLogEx(NORMAL, " o OEM code"); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "samples:"); - PrintAndLogEx(NORMAL, " wiegand encode w H10301 f 101 c 1337"); - return PM3_SUCCESS; -} -static int usage_wiegand_decode(void) { - PrintAndLogEx(NORMAL, "Decode raw hex to wiegand format"); - PrintAndLogEx(NORMAL, "Usage: wiegand decode [id]

"); - PrintAndLogEx(NORMAL, " p ignore invalid parity"); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "Samples:"); - PrintAndLogEx(NORMAL, " wiegand decode 2006f623ae"); - return PM3_SUCCESS; -} - -static void PrintTagId(wiegand_message_t *packed) { +static void print_wiegand_code(wiegand_message_t *packed) { + const char* s = "Encoded wiegand: "; if (packed->Top != 0) { - PrintAndLogEx(SUCCESS, "Card ID: %X%08X%08X", - (uint32_t)packed->Top, - (uint32_t)packed->Mid, - (uint32_t)packed->Bot) - ; + PrintAndLogEx(SUCCESS, "%s" _GREEN_("%X%08X%08X"), + s, + (uint32_t)packed->Top, + (uint32_t)packed->Mid, + (uint32_t)packed->Bot + ); } else { - PrintAndLogEx(SUCCESS, "Card ID: %X%08X", - (uint32_t)packed->Mid, - (uint32_t)packed->Bot) - ; + PrintAndLogEx(SUCCESS, "%s" _YELLOW_("%X%08X"), + s, + (uint32_t)packed->Mid, + (uint32_t)packed->Bot + ); } } int CmdWiegandList(const char *Cmd) { - bool errors = false; - char cmdp = 0; - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'h': - return usage_wiegand_list(); - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } - } + + CLIParserContext *ctx; + CLIParserInit(&ctx, "wiegand info", + "List available wiegand formats", + "wiegand list" + ); + + void *argtable[] = { + arg_param_begin, + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIParserFree(ctx); + HIDListFormats(); return PM3_SUCCESS; } int CmdWiegandEncode(const char *Cmd) { - int format_idx = -1; - char format[16] = {0}; + CLIParserContext *ctx; + CLIParserInit(&ctx, "wiegand encode", + "Encode wiegand formatted number to raw hex", + "wiegand encode -w H10301 --fc 101 --cn 1337" + ); + + void *argtable[] = { + arg_param_begin, + arg_int0(NULL, "fc", "", "facility number"), + arg_int1(NULL, "cn", "", "card number"), + arg_int0(NULL, "issue", "", "issue level"), + arg_int0(NULL, "oem", "", "OEM code"), + arg_strx1("w", "wiegand", "", "see `wiegand list` for available formats"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); wiegand_card_t data; memset(&data, 0, sizeof(wiegand_card_t)); + + data.FacilityCode = (uint32_t)arg_get_int_def(ctx, 1, 0); + data.CardNumber = (uint64_t)arg_get_int_def(ctx, 2, 0); + data.IssueLevel = (uint32_t)arg_get_int_def(ctx, 3, 0); + data.OEM = (uint32_t)arg_get_int_def(ctx, 4, 0); - bool errors = false; - char cmdp = 0; - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'h': - return usage_wiegand_encode(); - case 'w': - param_getstr(Cmd, cmdp + 1, format, sizeof(format)); - format_idx = HIDFindCardFormat(format); - if (format_idx == -1) { - PrintAndLogEx(WARNING, "Unknown format: %s", format); - errors = true; - } - cmdp += 2; - break; - case 'i': - data.IssueLevel = param_get32ex(Cmd, cmdp + 1, 0, 10); - cmdp += 2; - break; - case 'f': - data.FacilityCode = param_get32ex(Cmd, cmdp + 1, 0, 10); - cmdp += 2; - break; - case 'c': - data.CardNumber = param_get64ex(Cmd, cmdp + 1, 0, 10); - cmdp += 2; - break; - case 'o': - data.OEM = param_get32ex(Cmd, cmdp + 1, 0, 10); - cmdp += 2; - break; - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } + int len = 0; + char format[16] = {0}; + CLIParamStrToBuf(arg_get_str(ctx, 5), (uint8_t*)format, sizeof(format), &len); + CLIParserFree(ctx); + + int idx = HIDFindCardFormat(format); + if (idx == -1) { + PrintAndLogEx(WARNING, "Unknown format: %s", format); + return PM3_EINVARG; } - if (errors || cmdp == 0) return usage_wiegand_encode(); wiegand_message_t packed; memset(&packed, 0, sizeof(wiegand_message_t)); - if (HIDPack(format_idx, &data, &packed) == false) { + if (HIDPack(idx, &data, &packed) == false) { PrintAndLogEx(WARNING, "The card data could not be encoded in the selected format."); return PM3_ESOFT; } - PrintTagId(&packed); + print_wiegand_code(&packed); return PM3_SUCCESS; } int CmdWiegandDecode(const char *Cmd) { - uint32_t top = 0, mid = 0, bot = 0; - bool ignore_parity = false, gothex = false; - bool errors = false; - char cmdp = 0; - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - uint32_t slen = param_getlength(Cmd, cmdp); - slen++; // null termin - if (slen > 2) { - char *s = calloc(slen, sizeof(uint8_t)); - param_getstr(Cmd, cmdp, s, slen); - hexstring_to_u96(&top, &mid, &bot, s); - free(s); - gothex = true; - cmdp++; - continue; - } - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'h': - return usage_wiegand_decode(); - case 'p': - ignore_parity = true; - cmdp++; - break; - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } - } - if (gothex == false) - errors = true; + CLIParserContext *ctx; + CLIParserInit(&ctx, "wiegand decode", + "Decode raw hex to wiegand format", + "wiegand decode --raw 2006f623ae" + ); - if (errors || cmdp < 1) return usage_wiegand_decode(); + void *argtable[] = { + arg_param_begin, + arg_lit0("p", "parity", "ignore invalid parity"), + arg_strx1(NULL, "raw", "", "raw hex to be decoded"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + + bool ignore_parity = arg_get_lit(ctx, 1); + int len = 0; + char hex[40] = {0}; + CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t*)hex, sizeof(hex), &len); + CLIParserFree(ctx); + + if (len == 0) { + PrintAndLogEx(ERR, "empty input"); + return PM3_EINVARG; + } + + uint32_t top = 0, mid = 0, bot = 0; + hexstring_to_u96(&top, &mid, &bot, hex); wiegand_message_t packed = initialize_message_object(top, mid, bot); - HIDTryUnpack(&packed, ignore_parity); + return PM3_SUCCESS; } diff --git a/doc/cheatsheet.md b/doc/cheatsheet.md index 02701b53d..bee2b2b5e 100644 --- a/doc/cheatsheet.md +++ b/doc/cheatsheet.md @@ -332,23 +332,25 @@ Convert Site & Facility code to Wiegand raw hex ``` Options --- -w o f c i -w : wiegand format to use -o : OEM number / site code -f : facility code -c : card number -i : issue level +-w --oem --fc --cn --issue -pm3 --> wiegand encode 0 56 150 +-w : wiegand format to use +--oem : OEM number / site code +--fc : facility code +--cn : card number +--issue : issue level + +pm3 --> wiegand encode -w H10301 --oem 0 --fc 56 --cn 150 ``` Convert Site & Facility code from Wiegand raw hex to numbers ``` Options --- -p : ignore parity errors +-p : ignore parity errors +--raw : raw hex to be decoded -pm3 --> wiegand decode 2006f623ae +pm3 --> wiegand decode --raw 2006f623ae ``` ## HID Prox @@ -556,7 +558,7 @@ pm3 --> script list View lua helptext ``` -pm3 --> script run -h +pm3 --> script run -h ``` @@ -599,15 +601,15 @@ Load default keys into flash memory (RDV4 only) ``` Options --- -o : offset in memory -f : file name -m : upload 6 bytes keys (mifare key dictionary) -i : upload 8 bytes keys (iClass key dictionary) -t : upload 4 bytes keys (pwd dictionary) +-o : offset in memory +-f : file name +--mfc : upload 6 bytes keys (mifare key dictionary) +--iclass : upload 8 bytes keys (iClass key dictionary) +--t55xx : upload 4 bytes keys (pwd dictionary) -pm3 --> mem load f mfc_default_keys m -pm3 --> mem load f t55xx_default_pwds t -pm3 --> mem load f iclass_default_keys i +pm3 --> mem load -f mfc_default_keys --mfc +pm3 --> mem load -f t55xx_default_pwds --t5xx +pm3 --> mem load -f iclass_default_keys --iclass ``` ## Sim Module