textual
[RRG-proxmark3.git] / client / src / cmdwiegand.c
blobe29db6a7b367325438788d3f43e64d7889bed4ab
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 iceman <iceman at iuse.se>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Trace commands
9 //-----------------------------------------------------------------------------
10 #include "cmdwiegand.h"
12 #include <ctype.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "cmdparser.h" // command_t
16 #include "cliparser.h"
17 #include "comms.h"
18 #include "pm3_cmd.h"
19 #include "protocols.h"
20 #include "parity.h" // oddparity
21 #include "cmdhflist.h" // annotations
22 #include "wiegand_formats.h"
23 #include "wiegand_formatutils.h"
24 #include "util.h"
26 static int CmdHelp(const char *Cmd);
28 int CmdWiegandList(const char *Cmd) {
30 CLIParserContext *ctx;
31 CLIParserInit(&ctx, "wiegand info",
32 "List available wiegand formats",
33 "wiegand list"
36 void *argtable[] = {
37 arg_param_begin,
38 arg_param_end
40 CLIExecWithReturn(ctx, Cmd, argtable, true);
41 CLIParserFree(ctx);
43 HIDListFormats();
44 return PM3_SUCCESS;
47 int CmdWiegandEncode(const char *Cmd) {
49 CLIParserContext *ctx;
50 CLIParserInit(&ctx, "wiegand encode",
51 "Encode wiegand formatted number to raw hex",
52 "wiegand encode --fc 101 --cn 1337 -> show all formats\n"
53 "wiegand encode -w H10301 --fc 101 --cn 1337 -> H10301 format "
56 void *argtable[] = {
57 arg_param_begin,
58 arg_u64_0(NULL, "fc", "<dec>", "facility number"),
59 arg_u64_1(NULL, "cn", "<dec>", "card number"),
60 arg_u64_0(NULL, "issue", "<dec>", "issue level"),
61 arg_u64_0(NULL, "oem", "<dec>", "OEM code"),
62 arg_str0("w", "wiegand", "<format>", "see `wiegand list` for available formats"),
63 arg_lit0(NULL, "pre", "add HID ProxII preamble to wiegand output"),
64 arg_param_end
66 CLIExecWithReturn(ctx, Cmd, argtable, true);
68 wiegand_card_t data;
69 memset(&data, 0, sizeof(wiegand_card_t));
71 data.FacilityCode = arg_get_u32_def(ctx, 1, 0);
72 data.CardNumber = arg_get_u64_def(ctx, 2, 0);
73 data.IssueLevel = arg_get_u32_def(ctx, 3, 0);
74 data.OEM = arg_get_u32_def(ctx, 4, 0);
76 int len = 0;
77 char format[16] = {0};
78 CLIParamStrToBuf(arg_get_str(ctx, 5), (uint8_t *)format, sizeof(format), &len);
79 bool preamble = arg_get_lit(ctx, 6);
80 CLIParserFree(ctx);
82 int idx = -1;
83 if (len) {
84 idx = HIDFindCardFormat(format);
85 if (idx == -1) {
86 PrintAndLogEx(WARNING, "Unknown format: %s", format);
87 return PM3_EINVARG;
91 if (idx != -1) {
92 wiegand_message_t packed;
93 memset(&packed, 0, sizeof(wiegand_message_t));
94 if (HIDPack(idx, &data, &packed, preamble) == false) {
95 PrintAndLogEx(WARNING, "The card data could not be encoded in the selected format.");
96 return PM3_ESOFT;
98 print_wiegand_code(&packed);
99 } else {
100 // try all formats and print only the ones that work.
101 HIDPackTryAll(&data, preamble);
103 return PM3_SUCCESS;
106 int CmdWiegandDecode(const char *Cmd) {
108 CLIParserContext *ctx;
109 CLIParserInit(&ctx, "wiegand decode",
110 "Decode raw hex or binary to wiegand format",
111 "wiegand decode --raw 2006f623ae"
114 void *argtable[] = {
115 arg_param_begin,
116 arg_strx0("r", "raw", "<hex>", "raw hex to be decoded"),
117 arg_str0("b", "bin", "<bin>", "binary string to be decoded"),
118 arg_param_end
120 CLIExecWithReturn(ctx, Cmd, argtable, true);
121 int hlen = 0;
122 char hex[40] = {0};
123 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)hex, sizeof(hex), &hlen);
125 int blen = 0;
126 uint8_t binarr[100] = {0x00};
127 int res = CLIParamBinToBuf(arg_get_str(ctx, 2), binarr, sizeof(binarr), &blen);
128 CLIParserFree(ctx);
130 if (res) {
131 PrintAndLogEx(FAILED, "Error parsing binary string");
132 return PM3_EINVARG;
135 uint32_t top = 0, mid = 0, bot = 0;
137 if (hlen) {
138 res = hexstring_to_u96(&top, &mid, &bot, hex);
139 if (res != hlen) {
140 PrintAndLogEx(ERR, "hex string contains none hex chars");
141 return PM3_EINVARG;
143 } else if (blen) {
144 int n = binarray_to_u96(&top, &mid, &bot, binarr, blen);
145 if (n != blen) {
146 PrintAndLogEx(ERR, "Binary string contains none <0|1> chars");
147 return PM3_EINVARG;
149 } else {
150 PrintAndLogEx(ERR, "empty input");
151 return PM3_EINVARG;
153 wiegand_message_t packed = initialize_message_object(top, mid, bot, blen);
154 HIDTryUnpack(&packed);
155 return PM3_SUCCESS;
158 static command_t CommandTable[] = {
159 {"help", CmdHelp, AlwaysAvailable, "This help"},
160 {"list", CmdWiegandList, AlwaysAvailable, "List available wiegand formats"},
161 {"encode", CmdWiegandEncode, AlwaysAvailable, "Encode to wiegand raw hex (currently for HID Prox)"},
162 {"decode", CmdWiegandDecode, AlwaysAvailable, "Convert raw hex to decoded wiegand format (currently for HID Prox)"},
163 {NULL, NULL, NULL, NULL}
166 static int CmdHelp(const char *Cmd) {
167 (void)Cmd; // Cmd is not used so far
168 CmdsHelp(CommandTable);
169 return PM3_SUCCESS;
172 int CmdWiegand(const char *Cmd) {
173 clearCommandBuffer();
174 return CmdsParse(CommandTable, Cmd);