]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
lf hid: Added encode/decode support for Issue Level
[proxmark3-svn] / client / cmdlfhid.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 // Low frequency HID commands (known)
9 //
10 // Useful resources:
11 // RF interface, programming a T55x7 clone, 26-bit HID H10301 encoding:
12 // http://www.proxmark.org/files/Documents/125%20kHz%20-%20HID/HID_format_example.pdf
13 //
14 // "Understanding Card Data Formats"
15 // https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
16 //
17 // "What Format Do You Need?"
18 // https://www.hidglobal.com/sites/default/files/resource_files/hid-prox-br-en.pdf
19 //-----------------------------------------------------------------------------
20
21 #include "cmdlfhid.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include "comms.h"
26 #include "ui.h"
27 #include "graph.h"
28 #include "cmdparser.h"
29 #include "cmddata.h" //for g_debugMode, demodbuff cmds
30 #include "lfdemod.h" // for HIDdemodFSK
31 #include "hidcardformats.h"
32 #include "hidcardformatutils.h"
33 #include "util.h" // for param_get8,32,64
34
35
36 /**
37 * Converts a hex string to component "hi2", "hi" and "lo" 32-bit integers, one nibble
38 * at a time.
39 *
40 * Returns the number of nibbles (4 bits) entered.
41 */
42 int hexstring_to_int96(/* out */ uint32_t* hi2,/* out */ uint32_t* hi, /* out */ uint32_t* lo, const char* str) {
43 // TODO: Replace this with param_gethex when it supports arbitrary length
44 // inputs.
45 int n = 0, i = 0;
46
47 while (sscanf(&str[i++], "%1x", &n ) == 1) {
48 *hi2 = (*hi2 << 4) | (*hi >> 28);
49 *hi = (*hi << 4) | (*lo >> 28);
50 *lo = (*lo << 4) | (n & 0xf);
51 }
52
53 return i - 1;
54 }
55
56 //by marshmellow (based on existing demod + holiman's refactor)
57 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
58 //print full HID Prox ID and some bit format details if found
59 int CmdFSKdemodHID(const char *Cmd)
60 {
61 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
62 uint32_t hi2=0, hi=0, lo=0;
63
64 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
65 size_t BitLen = getFromGraphBuf(BitStream);
66 if (BitLen==0) return 0;
67 //get binary from fsk wave
68 int waveIdx = 0;
69 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx);
70 if (idx<0){
71 if (g_debugMode){
72 if (idx==-1){
73 PrintAndLog("DEBUG: Just Noise Detected");
74 } else if (idx == -2) {
75 PrintAndLog("DEBUG: Error demoding fsk");
76 } else if (idx == -3) {
77 PrintAndLog("DEBUG: Preamble not found");
78 } else if (idx == -4) {
79 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
80 } else {
81 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
82 }
83 }
84 return 0;
85 }
86 if (hi2==0 && hi==0 && lo==0) {
87 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
88 return 0;
89 }
90
91 if (hi2 != 0)
92 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
93 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo
94 );
95 else
96 PrintAndLog("HID Prox TAG ID: %x%08x",
97 (unsigned int) hi, (unsigned int) lo
98 );
99
100 hidproxmessage_t packed = initialize_proxmessage_object(hi2, hi, lo);
101 bool ret = HIDTryUnpack(&packed, false);
102
103 if (!ret) {
104 PrintAndLog("Invalid or unsupported tag length.");
105 }
106 setDemodBuf(BitStream,BitLen,idx);
107 setClockGrid(50, waveIdx + (idx*50));
108 if (g_debugMode){
109 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
110 printDemodBuff();
111 }
112 return 1;
113 }
114
115 int CmdHIDReadFSK(const char *Cmd)
116 {
117 int findone=0;
118 if(Cmd[0]=='1') findone=1;
119 UsbCommand c={CMD_HID_DEMOD_FSK};
120 c.arg[0]=findone;
121 SendCommand(&c);
122 return 0;
123 }
124
125 int CmdHIDSim(const char *Cmd)
126 {
127 uint32_t hi2 = 0, hi = 0, lo = 0;
128 hexstring_to_int96(&hi2, &hi, &lo, Cmd);
129 if (hi >= 0x40 || hi2 != 0) {
130 PrintAndLog("This looks like a long tag ID. Use 'lf simfsk' for long tags. Aborting!");
131 return 0;
132 }
133
134 PrintAndLog("Emulating tag with ID %x%08x", hi, lo);
135 PrintAndLog("Press pm3-button to abort simulation");
136
137 UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}};
138 SendCommand(&c);
139 return 0;
140 }
141
142 int CmdHIDClone(const char *Cmd)
143 {
144 unsigned int hi2 = 0, hi = 0, lo = 0;
145 UsbCommand c;
146 hexstring_to_int96(&hi2, &hi, &lo, Cmd);
147
148 if (hi >= 0x40 || hi2 != 0) {
149 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
150 c.d.asBytes[0] = 1;
151 } else {
152 PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
153 c.d.asBytes[0] = 0;
154 }
155
156 c.cmd = CMD_HID_CLONE_TAG;
157 c.arg[0] = (hi2 & 0x000FFFFF);
158 c.arg[1] = hi;
159 c.arg[2] = lo;
160
161 SendCommand(&c);
162 return 0;
163 }
164
165 int CmdHIDDecode(const char *Cmd){
166 if (strlen(Cmd)<3) {
167 PrintAndLog("Usage: lf hid decode <id> {p}");
168 PrintAndLog(" (optional) p: Ignore invalid parity");
169 PrintAndLog(" sample: lf hid decode 2006f623ae");
170 return 0;
171 }
172
173 uint32_t top = 0, mid = 0, bot = 0;
174 bool ignoreParity = false;
175 hexstring_to_int96(&top, &mid, &bot, Cmd);
176 hidproxmessage_t packed = initialize_proxmessage_object(top, mid, bot);
177
178 char opt = param_getchar(Cmd, 1);
179 if (opt == 'p') ignoreParity = true;
180
181 HIDTryUnpack(&packed, ignoreParity);
182 return 0;
183 }
184 int CmdHIDEncode(const char *Cmd) {
185 if (strlen(Cmd) == 0) {
186 PrintAndLog("Usage: lf hid encode <format> <facility code (decimal)> <card number (decimal)> [issue level (decimal)]");
187 PrintAndLog(" sample: lf hid encode H10301 123 4567");
188 return 0;
189 }
190
191 int formatIndex = -1;
192 if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
193 HIDListFormats();
194 return 0;
195 } else {
196 char format[16];
197 memset(format, 0, sizeof(format));
198 param_getstr(Cmd, 0, format, sizeof(format));
199 formatIndex = HIDFindCardFormat(format);
200 if (formatIndex == -1) {
201 HIDListFormats();
202 return 0;
203 }
204 }
205
206 hidproxcard_t card;
207 memset(&card, 0, sizeof(hidproxcard_t));
208 card.FacilityCode = param_get32ex(Cmd, 1, 0, 10);
209 card.CardNumber = param_get64ex(Cmd, 2, 0, 10);
210 card.IssueLevel = param_get32ex(Cmd, 3, 0, 10);
211 card.ParitySupported = true; // Try to encode parity if supported.
212
213 hidproxmessage_t packed;
214 memset(&packed, 0, sizeof(hidproxmessage_t));
215 if (HIDPack(formatIndex, &card, &packed)){
216 if (packed.top != 0) {
217 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
218 (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot);
219 } else {
220 PrintAndLog("HID Prox TAG ID: %x%08x",
221 (unsigned int)packed.mid, (unsigned int)packed.bot);
222 }
223 } else {
224 PrintAndLog("The provided data could not be encoded with the selected format.");
225 }
226 return 0;
227 }
228
229 int CmdHIDWrite(const char *Cmd) {
230 if (strlen(Cmd) == 0) {
231 PrintAndLog("Usage: lf hid write <format> <facility code (decimal)> <card number (decimal)> [issue level (decimal)]");
232 PrintAndLog(" sample: lf hid write H10301 123 4567");
233 return 0;
234 }
235
236 int formatIndex = -1;
237 if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
238 HIDListFormats();
239 return 0;
240 } else {
241 char format[16];
242 memset(format, 0, sizeof(format));
243 param_getstr(Cmd, 0, format, sizeof(format));
244 formatIndex = HIDFindCardFormat(format);
245 if (formatIndex == -1) {
246 HIDListFormats();
247 return 0;
248 }
249 }
250
251 hidproxcard_t card;
252 memset(&card, 0, sizeof(hidproxcard_t));
253 card.FacilityCode = param_get32ex(Cmd, 1, 0, 10);
254 card.CardNumber = param_get64ex(Cmd, 2, 0, 10);
255 card.IssueLevel = param_get32ex(Cmd, 3, 0, 10);
256 card.ParitySupported = true; // Try to encode parity if supported.
257
258 hidproxmessage_t packed;
259 memset(&packed, 0, sizeof(hidproxmessage_t));
260 if (HIDPack(formatIndex, &card, &packed)){
261 UsbCommand c;
262 if (packed.top != 0) {
263 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
264 (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot);
265 c.d.asBytes[0] = 1;
266 } else {
267 PrintAndLog("HID Prox TAG ID: %x%08x",
268 (unsigned int)packed.mid, (unsigned int)packed.bot);
269 c.d.asBytes[0] = 0;
270 }
271
272 c.cmd = CMD_HID_CLONE_TAG;
273 c.arg[0] = (packed.top & 0x000FFFFF);
274 c.arg[1] = packed.mid;
275 c.arg[2] = packed.bot;
276 SendCommand(&c);
277
278 } else {
279 PrintAndLog("The provided data could not be encoded with the selected format.");
280 }
281 return 0;
282 }
283
284 static int CmdHelp(const char *Cmd); // define this now so the below won't error out.
285 static command_t CommandTable[] =
286 {
287 {"help", CmdHelp, 1, "This help"},
288 {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
289 {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
290 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
291 {"clone", CmdHIDClone, 0, "<ID> -- Clone HID to T55x7 (tag must be in antenna)"},
292 {"decode", CmdHIDDecode, 1, "<ID> -- Try to decode an HID tag and show its contents"},
293 {"encode", CmdHIDEncode, 1, "<format> <fc> <num> -- Encode an HID ID with the specified format, facility code and card number"},
294 {"write", CmdHIDWrite, 0, "<format> <fc> <num> -- Encode and write to a T55x7 tag (tag must be in antenna)"},
295 {NULL, NULL, 0, NULL}
296 };
297
298 int CmdLFHID(const char *Cmd)
299 {
300 CmdsParse(CommandTable, Cmd);
301 return 0;
302 }
303
304 int CmdHelp(const char *Cmd)
305 {
306 CmdsHelp(CommandTable);
307 return 0;
308 }
Impressum, Datenschutz