wiegand - now uses cli parse
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#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 <format>] [<field> <value (decimal)>] {...}");
|
||||
PrintAndLogEx(NORMAL, "Options:");
|
||||
PrintAndLogEx(NORMAL, " w <format> see `wiegand list` for available formats");
|
||||
PrintAndLogEx(NORMAL, " c <value> card number");
|
||||
PrintAndLogEx(NORMAL, " f <value> facility code");
|
||||
PrintAndLogEx(NORMAL, " i <value> issue Level");
|
||||
PrintAndLogEx(NORMAL, " o <value> 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] <p>");
|
||||
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", "<dec>", "facility number"),
|
||||
arg_int1(NULL, "cn", "<dec>", "card number"),
|
||||
arg_int0(NULL, "issue", "<dec>", "issue level"),
|
||||
arg_int0(NULL, "oem", "<dec>", "OEM code"),
|
||||
arg_strx1("w", "wiegand", "<format>", "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", "<hex>", "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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user