]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
433a093ddf278324f185c0a4f4f2712820b0b56b
[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 "parity.h" // for parity
32 #include "util.h" // for param_get8,32
33
34 static int CmdHelp(const char *Cmd);
35
36
37 /**
38 * Packs a "short" (<38-bit) HID ID from component parts.
39 *
40 * This only works with 26, 34, 35 and 37 bit card IDs.
41 *
42 * NOTE: Parity calculation is only supported on 26-bit tags. Other card lengths
43 * may have invalid parity.
44 *
45 * Returns false on invalid inputs.
46 */
47 bool pack_short_hid(/* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const short_hid_info *info) {
48 uint32_t high = 0, low = 0;
49
50 switch (info->fmtLen) {
51 case 26: // HID H10301
52 low |= (info->cardnum & 0xffff) << 1;
53 low |= (info->fc & 0xff) << 17;
54
55 if (info->parityValid) {
56 // Calculate parity
57 low |= oddparity32((low >> 1) & 0xfff) & 1;
58 low |= (evenparity32((low >> 13) & 0xfff) & 1) << 25;
59 }
60 break;
61
62 case 34:
63 low |= (info->cardnum & 0xffff) << 1;
64 low |= (info->fc & 0x7fff) << 17;
65 high |= (info->fc & 0x8000) >> 15;
66 // TODO: Calculate parity
67 break;
68
69 case 35:
70 low |= (info->cardnum & 0xfffff) << 1;
71 low |= (info->fc & 0x7ff) << 21;
72 high |= (info->fc & 0x800) >> 11;
73 // TODO: Calculate parity
74 break;
75
76 case 37:
77 low |= (info->cardnum & 0x7ffff) << 1;
78 low |= (info->fc & 0xfff) << 20;
79 high |= (info->fc & 0xf000) >> 12;
80 // TODO: Calculate parity
81 break;
82
83 default:
84 // Invalid / unsupported length
85 return false;
86 }
87
88 // Set the highest bit
89 if (info->fmtLen != 37) {
90 // Bit 37 is always set
91 high |= 0x20;
92
93 // Set the bit corresponding to the length.
94 if (info->fmtLen < 32) {
95 low |= 1 << info->fmtLen;
96 } else {
97 high |= 1 << (info->fmtLen - 32);
98 }
99 }
100
101 // Return result only if successful.
102 *hi = high;
103 *lo = low;
104 return true;
105 }
106
107
108 /**
109 * Unpacks a "short" (<38-bit) HID ID into its component parts.
110 *
111 * This only works with 26, 34, 35 and 37 bit card IDs.
112 *
113 * NOTE: Parity checking is only supported on 26-bit tags.
114 *
115 * Returns false on invalid inputs.
116 */
117 bool unpack_short_hid(short_hid_info *out, uint32_t hi, uint32_t lo) {
118 memset(out, 0, sizeof(short_hid_info));
119
120 if (((hi >> 5) & 1) == 1) {
121 // if bit 38 is set then < 37 bit format is used
122 uint32_t lo2 = 0;
123 // get bits 21-37 to check for format len bit
124 lo2 = (((hi & 31) << 12) | (lo >> 20));
125 uint8_t idx3 = 1;
126 // find last bit set to 1 (format len bit)
127 while (lo2 > 1) {
128 lo2 = lo2 >> 1;
129 idx3++;
130 }
131
132 out->fmtLen = idx3 + 19;
133
134 switch (out->fmtLen) {
135 case 26: // HID H10301
136 out->cardnum = (lo >> 1) & 0xFFFF;
137 out->fc = (lo >> 17) & 0xFF;
138
139 if (g_debugMode) {
140 PrintAndLog("oddparity : input=%x, calculated=%d, provided=%d",
141 (lo >> 1) & 0xFFF, oddparity32((lo >> 1) & 0xFFF), lo & 1);
142 PrintAndLog("evenparity: input=%x, calculated=%d, provided=%d",
143 (lo >> 13) & 0xFFF, evenparity32((lo >> 13) & 0xFFF) & 1, (lo >> 25) & 1);
144 }
145
146 out->parityValid =
147 (oddparity32((lo >> 1) & 0xFFF) == (lo & 1)) &&
148 ((evenparity32((lo >> 13) & 0xFFF) & 1) == ((lo >> 25) & 1));
149 break;
150
151 case 34:
152 out->cardnum = (lo >> 1) & 0xFFFF;
153 out->fc = ((hi & 1) << 15) | (lo >> 17);
154 // TODO: Calculate parity
155 break;
156
157 case 35:
158 out->cardnum = (lo >> 1) & 0xFFFFF;
159 out->fc = ((hi & 1) << 11) | (lo >> 21);
160 // TODO: Calculate parity
161 break;
162
163 default:
164 return false;
165 }
166 } else {
167 // If bit 38 is not set, then 37 bit format is used
168 out->fmtLen = 37;
169 out->cardnum = (lo >> 1) & 0x7FFFF;
170 out->fc = ((hi & 0xF) << 12) | (lo >> 20);
171 // TODO: Calculate parity
172 }
173 return true;
174 }
175
176
177 /**
178 * Converts a hex string to component "hi" and "lo" 32-bit integers, one nibble
179 * at a time.
180 *
181 * Returns the number of nibbles (4 bits) entered.
182 */
183 int hexstring_to_int64(/* out */ uint32_t* hi, /* out */ uint32_t* lo, const char* str) {
184 // TODO: Replace this with param_gethex when it supports arbitrary length
185 // inputs.
186 int n = 0, i = 0;
187
188 while (sscanf(&str[i++], "%1x", &n ) == 1) {
189 *hi = (*hi << 4) | (*lo >> 28);
190 *lo = (*lo << 4) | (n & 0xf);
191 }
192
193 return i - 1;
194 }
195
196 //by marshmellow (based on existing demod + holiman's refactor)
197 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
198 //print full HID Prox ID and some bit format details if found
199 int CmdFSKdemodHID(const char *Cmd)
200 {
201 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
202 uint32_t hi2=0, hi=0, lo=0;
203
204 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
205 size_t BitLen = getFromGraphBuf(BitStream);
206 if (BitLen==0) return 0;
207 //get binary from fsk wave
208 int waveIdx = 0;
209 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx);
210 if (idx<0){
211 if (g_debugMode){
212 if (idx==-1){
213 PrintAndLog("DEBUG: Just Noise Detected");
214 } else if (idx == -2) {
215 PrintAndLog("DEBUG: Error demoding fsk");
216 } else if (idx == -3) {
217 PrintAndLog("DEBUG: Preamble not found");
218 } else if (idx == -4) {
219 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
220 } else {
221 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
222 }
223 }
224 return 0;
225 }
226 if (hi2==0 && hi==0 && lo==0) {
227 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
228 return 0;
229 }
230 if (hi2 != 0){ //extra large HID tags
231 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
232 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF);
233 }
234 else { //standard HID tags <38 bits
235 short_hid_info card_info;
236 bool ret = unpack_short_hid(&card_info, (uint32_t)hi, (uint32_t)lo);
237 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
238 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
239 card_info.fmtLen, card_info.fc, card_info.cardnum);
240
241 if (card_info.fmtLen == 26) {
242 PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
243 }
244
245 if (!ret) {
246 PrintAndLog("Invalid or unsupported tag length.");
247 }
248 }
249 setDemodBuf(BitStream,BitLen,idx);
250 setClockGrid(50, waveIdx + (idx*50));
251 if (g_debugMode){
252 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
253 printDemodBuff();
254 }
255 return 1;
256 }
257
258 int CmdHIDReadFSK(const char *Cmd)
259 {
260 int findone=0;
261 if(Cmd[0]=='1') findone=1;
262 UsbCommand c={CMD_HID_DEMOD_FSK};
263 c.arg[0]=findone;
264 SendCommand(&c);
265 return 0;
266 }
267
268 int CmdHIDSim(const char *Cmd)
269 {
270 uint32_t hi = 0, lo = 0;
271 hexstring_to_int64(&hi, &lo, Cmd);
272 if (hi >= 0x40) {
273 PrintAndLog("This looks like a long tag ID. Use 'lf simfsk' for long tags. Aborting!");
274 return 0;
275 }
276
277 PrintAndLog("Emulating tag with ID %x%08x", hi, lo);
278 PrintAndLog("Press pm3-button to abort simulation");
279
280 UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}};
281 SendCommand(&c);
282 return 0;
283 }
284
285 int CmdHIDClone(const char *Cmd)
286 {
287 unsigned int hi2 = 0, hi = 0, lo = 0;
288 UsbCommand c;
289
290 if (strchr(Cmd,'l') != 0) {
291 int n = 0, i = 0;
292
293 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
294 hi2 = (hi2 << 4) | (hi >> 28);
295 hi = (hi << 4) | (lo >> 28);
296 lo = (lo << 4) | (n & 0xf);
297 }
298
299 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
300
301 c.d.asBytes[0] = 1;
302 }
303 else {
304 hexstring_to_int64(&hi, &lo, Cmd);
305 if (hi >= 0x40) {
306 PrintAndLog("This looks like a long tag ID. Aborting!");
307 return 0;
308 }
309
310 PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
311
312 hi2 = 0;
313 c.d.asBytes[0] = 0;
314 }
315
316 c.cmd = CMD_HID_CLONE_TAG;
317 c.arg[0] = hi2;
318 c.arg[1] = hi;
319 c.arg[2] = lo;
320
321 SendCommand(&c);
322 return 0;
323 }
324
325
326 int CmdHIDPack(const char *Cmd) {
327 uint32_t hi = 0, lo = 0;
328 short_hid_info card_info;
329
330 if (strlen(Cmd)<3) {
331 PrintAndLog("Usage: lf hid pack <length> <facility code (decimal)> <card number (decimal)>");
332 PrintAndLog(" sample: lf hid pack 26 123 4567");
333 return 0;
334 }
335
336 card_info.fmtLen = param_get8(Cmd, 0);
337 card_info.fc = param_get32ex(Cmd, 1, 0, 10);
338 card_info.cardnum = param_get32ex(Cmd, 2, 0, 10);
339 card_info.parityValid = true;
340
341 // TODO
342 if (card_info.fmtLen != 26) {
343 PrintAndLog("Warning: Parity bits are only calculated for 26 bit IDs -- this may be invalid!");
344 }
345
346 bool ret = pack_short_hid(&hi, &lo, &card_info);
347
348 if (ret) {
349 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
350 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
351 card_info.fmtLen, card_info.fc, card_info.cardnum);
352 } else {
353 PrintAndLog("Invalid or unsupported tag length.");
354 }
355 return 0;
356 }
357
358
359 int CmdHIDUnpack(const char *Cmd)
360 {
361 uint32_t hi = 0, lo = 0;
362 if (strlen(Cmd)<1) {
363 PrintAndLog("Usage: lf hid unpack <ID>");
364 PrintAndLog(" sample: lf hid unpack 2006f623ae");
365 return 0;
366 }
367
368 hexstring_to_int64(&hi, &lo, Cmd);
369 if (hi >= 0x40) {
370 PrintAndLog("This looks like a long tag ID. Aborting!");
371 return 0;
372 }
373
374 short_hid_info card_info;
375 bool ret = unpack_short_hid(&card_info, hi, lo);
376
377 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
378 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
379 card_info.fmtLen, card_info.fc, card_info.cardnum);
380
381 if (card_info.fmtLen == 26) {
382 PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
383 }
384
385 if (!ret) {
386 PrintAndLog("Invalid or unsupported tag length.");
387 }
388 return 0;
389 }
390
391
392 static command_t CommandTable[] =
393 {
394 {"help", CmdHelp, 1, "This help"},
395 {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
396 {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
397 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
398 {"clone", CmdHIDClone, 0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"},
399 {"pack", CmdHIDPack, 1, "<len> <fc> <num> -- packs a <38 bit (short) HID ID from its length, facility code and card number"},
400 {"unpack", CmdHIDUnpack, 1, "<ID> -- unpacks a <38 bit (short) HID ID to its length, facility code and card number"},
401 {NULL, NULL, 0, NULL}
402 };
403
404 int CmdLFHID(const char *Cmd)
405 {
406 CmdsParse(CommandTable, Cmd);
407 return 0;
408 }
409
410 int CmdHelp(const char *Cmd)
411 {
412 CmdsHelp(CommandTable);
413 return 0;
414 }
Impressum, Datenschutz