proxmark3/client/src/cmdwiegand.c

169 lines
5.1 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// Copyright (C) 2018 iceman <iceman at iuse.se>
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Trace commands
//-----------------------------------------------------------------------------
#include "cmdwiegand.h"
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "cmdparser.h" // command_t
2020-10-04 17:07:26 +08:00
#include "cliparser.h"
#include "comms.h"
#include "pm3_cmd.h"
#include "protocols.h"
#include "parity.h" // oddparity
#include "cmdhflist.h" // annotations
#include "wiegand_formats.h"
#include "wiegand_formatutils.h"
2019-09-19 16:47:12 +08:00
#include "util.h"
static int CmdHelp(const char *Cmd);
2020-10-04 17:07:26 +08:00
static void print_wiegand_code(wiegand_message_t *packed) {
const char* s = "Encoded wiegand: ";
if (packed->Top != 0) {
2020-10-04 17:07:26 +08:00
PrintAndLogEx(SUCCESS, "%s" _GREEN_("%X%08X%08X"),
s,
(uint32_t)packed->Top,
(uint32_t)packed->Mid,
(uint32_t)packed->Bot
);
} else {
2020-10-04 17:07:26 +08:00
PrintAndLogEx(SUCCESS, "%s" _YELLOW_("%X%08X"),
s,
(uint32_t)packed->Mid,
(uint32_t)packed->Bot
);
}
}
int CmdWiegandList(const char *Cmd) {
2020-10-04 17:07:26 +08:00
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) {
2020-10-04 17:07:26 +08:00
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);
2019-09-19 16:47:12 +08:00
wiegand_card_t data;
memset(&data, 0, sizeof(wiegand_card_t));
2020-10-04 17:07:26 +08:00
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);
int len = 0;
char format[16] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 5), (uint8_t*)format, sizeof(format), &len);
CLIParserFree(ctx);
2020-10-04 17:07:26 +08:00
int idx = HIDFindCardFormat(format);
if (idx == -1) {
PrintAndLogEx(WARNING, "Unknown format: %s", format);
return PM3_EINVARG;
}
wiegand_message_t packed;
memset(&packed, 0, sizeof(wiegand_message_t));
2019-09-19 16:47:12 +08:00
2020-10-04 17:07:26 +08:00
if (HIDPack(idx, &data, &packed) == false) {
PrintAndLogEx(WARNING, "The card data could not be encoded in the selected format.");
return PM3_ESOFT;
}
2019-09-19 16:47:12 +08:00
2020-10-04 17:07:26 +08:00
print_wiegand_code(&packed);
return PM3_SUCCESS;
}
int CmdWiegandDecode(const char *Cmd) {
2020-10-04 17:07:26 +08:00
CLIParserContext *ctx;
CLIParserInit(&ctx, "wiegand decode",
"Decode raw hex to wiegand format",
"wiegand decode --raw 2006f623ae"
);
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;
}
2020-10-04 17:07:26 +08:00
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);
2020-10-04 17:07:26 +08:00
return PM3_SUCCESS;
}
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"list", CmdWiegandList, AlwaysAvailable, "List available wiegand formats"},
2020-10-04 17:09:54 +08:00
{"encode", CmdWiegandEncode, AlwaysAvailable, "Encode to wiegand raw hex"},
{"decode", CmdWiegandDecode, AlwaysAvailable, "Convert raw hex to decoded wiegand format"},
{NULL, NULL, NULL, NULL}
};
static int CmdHelp(const char *Cmd) {
(void)Cmd; // Cmd is not used so far
CmdsHelp(CommandTable);
return PM3_SUCCESS;
}
int CmdWiegand(const char *Cmd) {
clearCommandBuffer();
return CmdsParse(CommandTable, Cmd);
}