]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
BUG: forgot to remove
[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
9 //-----------------------------------------------------------------------------
10
11 #include "cmdlfhid.h"
12
13 #ifndef BITS
14 # define BITS 96
15 #endif
16
17 static int CmdHelp(const char *Cmd);
18
19 int usage_lf_hid_wiegand(void){
20 PrintAndLog("This command converts facility code/card number to Wiegand code");
21 PrintAndLog("Usage: lf hid wiegand [h] [OEM] [FC] [CN]");
22
23 PrintAndLog("Options:");
24 PrintAndLog(" h - This help");
25 PrintAndLog(" OEM - OEM number / site code");
26 PrintAndLog(" FC - facility code");
27 PrintAndLog(" CN - card number");
28 PrintAndLog("Examples:");
29 PrintAndLog(" lf hid wiegand 0 101 2001");
30 return 0;
31 }
32 int usage_lf_hid_sim(void){
33 PrintAndLog("HID Tag simulator");
34 PrintAndLog("");
35 PrintAndLog("Usage: lf hid sim [h] [ID]");
36 PrintAndLog("Options:");
37 PrintAndLog(" h - This help");
38 PrintAndLog(" ID - HID id");
39 PrintAndLog("Examples:");
40 PrintAndLog(" lf hid sim 224");
41 return 0;
42 }
43 int usage_lf_hid_clone(void){
44 PrintAndLog("Clone HID to T55x7. Tag must be on antenna. ");
45 PrintAndLog("");
46 PrintAndLog("Usage: lf hid clone [h] [ID] <L>");
47 PrintAndLog("Options:");
48 PrintAndLog(" h - This help");
49 PrintAndLog(" ID - HID id");
50 PrintAndLog(" L - 84bit ID");
51 PrintAndLog("Examples:");
52 PrintAndLog(" lf hid clone 224");
53 PrintAndLog(" lf hid clone 224 L");
54 return 0;
55 }
56 int usage_lf_hid_brute(void){
57 PrintAndLog("Enables bruteforce of HID readers with specified facility code.");
58 PrintAndLog("This is a attack against reader. if cardnumber is given, it starts with it and goes up / down one step");
59 PrintAndLog("if cardnumber is not given, it starts with 1 and goes up to 65535");
60 PrintAndLog("");
61 PrintAndLog("Usage: lf hid brute [h] a <format> f <facility-code> c <cardnumber> d <delay>");
62 PrintAndLog("Options :");
63 PrintAndLog(" h : This help");
64 PrintAndLog(" a <format> : 26|33|34|35|37|40|44|84");
65 PrintAndLog(" f <facility-code> : 8-bit value HID facility code");
66 PrintAndLog(" c <cardnumber> : (optional) cardnumber to start with, max 65535");
67 PrintAndLog(" d <delay> : delay betweens attempts in ms. Default 1000ms");
68 PrintAndLog("");
69 PrintAndLog("Samples:");
70 PrintAndLog(" lf hid brute a 26 f 224");
71 PrintAndLog(" lf hid brute a 26 f 21 d 2000");
72 PrintAndLog(" lf hid brute a 26 f 21 c 200 d 2000");
73 return 0;
74 }
75
76 static int sendPing(void){
77 UsbCommand ping = {CMD_PING, {1, 2, 3}};
78 SendCommand(&ping);
79 SendCommand(&ping);
80 SendCommand(&ping);
81 clearCommandBuffer();
82 UsbCommand resp;
83 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000))
84 return 0;
85 return 1;
86 }
87 static bool sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bs){
88
89 PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
90
91 calcWiegand( fmtlen, fc, cn, bs);
92
93 uint64_t arg1 = bytebits_to_byte(bs,32);
94 uint64_t arg2 = bytebits_to_byte(bs+32,32);
95 UsbCommand c = {CMD_HID_SIM_TAG, {arg1, arg2, 0}};
96 clearCommandBuffer();
97 SendCommand(&c);
98 msleep(delay);
99 sendPing();
100 return TRUE;
101 }
102
103 int CmdHIDDemodFSK(const char *Cmd) {
104 uint8_t findone = ( Cmd[0] == '1' ) ? 1 : 0;
105 UsbCommand c = {CMD_HID_DEMOD_FSK, {findone, 0 , 0}};
106 clearCommandBuffer();
107 SendCommand(&c);
108 return 0;
109 }
110
111 int CmdHIDSim(const char *Cmd) {
112 unsigned int hi = 0, lo = 0;
113 int n = 0, i = 0;
114
115 uint8_t ctmp = param_getchar(Cmd, 0);
116 if ( strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_sim();
117
118 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
119 hi = (hi << 4) | (lo >> 28);
120 lo = (lo << 4) | (n & 0xf);
121 }
122
123 PrintAndLog("Emulating tag with ID %x%16x", hi, lo);
124 PrintAndLog("Press pm3-button to abort simulation");
125
126 UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}};
127 clearCommandBuffer();
128 SendCommand(&c);
129 return 0;
130 }
131
132 int CmdHIDClone(const char *Cmd) {
133
134 uint32_t hi2 = 0, hi = 0, lo = 0;
135 int n = 0, i = 0;
136 UsbCommand c;
137
138 uint8_t ctmp = param_getchar(Cmd, 0);
139 if ( strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_clone();
140
141 if (strchr(Cmd,'l') != 0) {
142 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
143 hi2 = (hi2 << 4) | (hi >> 28);
144 hi = (hi << 4) | (lo >> 28);
145 lo = (lo << 4) | (n & 0xf);
146 }
147
148 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
149
150 c.d.asBytes[0] = 1;
151 } else {
152 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
153 hi = (hi << 4) | (lo >> 28);
154 lo = (lo << 4) | (n & 0xf);
155 }
156
157 PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
158
159 hi2 = 0;
160 c.d.asBytes[0] = 0;
161 }
162
163 c.cmd = CMD_HID_CLONE_TAG;
164 c.arg[0] = hi2;
165 c.arg[1] = hi;
166 c.arg[2] = lo;
167
168 clearCommandBuffer();
169 SendCommand(&c);
170 return 0;
171 }
172 // struct to handle wiegand
173 typedef struct {
174 uint8_t FormatLen;
175 uint8_t SiteCode;
176 uint8_t FacilityCode;
177 uint8_t CardNumber;
178 uint8_t* Wiegand;
179 size_t Wiegand_n;
180 } wiegand_t;
181
182 static void addHIDMarker(uint8_t fmtlen, uint8_t *out) {
183 // temp array
184 uint8_t arr[BITS];
185 memset(arr, 0, BITS);
186
187 // copy inpu
188 uint8_t pos = sizeof(arr)-fmtlen;
189 memcpy(arr+pos, out, fmtlen);
190
191 switch(fmtlen) {
192 case 26:{
193 // start sentinel, BITS-bit 27 = 1
194 arr[BITS-27] = 1;
195 // fmt smaller than 37 used, bit37 = 1
196 arr[BITS-38] = 1;
197 memcpy(out, arr, BITS);
198 break;
199 }
200 case 34:
201 // start sentinel, BITS-bit 27 = 1
202 arr[BITS-35] = 1;
203
204 // fmt smaller than 37 used, bit37 = 1
205 arr[BITS-38] = 1;
206 memcpy(out, arr, BITS);
207 break;
208 default:break;
209 }
210 }
211
212 // static void getParity34(uint32_t *hi, uint32_t *lo){
213 // uint32_t result = 0;
214 // int i;
215
216 // // even parity
217 // for (i = 7;i >= 0;i--)
218 // result ^= (*hi >> i) & i;
219 // for (i = 31;i >= 24;i--)
220 // result ^= (*lo >> i) & 1;
221
222 // *hi |= result << 2;
223
224 // // odd parity bit
225 // result = 0;
226 // for (i = 23;i >= 1;i--)
227 // result ^= (*lo >> i) & 1;
228
229 // *lo |= !result;
230 // }
231 // static void getParity37H(uint32_t *hi, uint32_t *lo){
232 // uint32_t result = 0;
233 // int i;
234
235 // // even parity
236 // for (i = 4;i >= 0;i--)
237 // result ^= (*hi >> i) & 1;
238 // for (i = 31;i >= 20;i--)
239 // result ^= (*lo >> i) & 1;
240 // *hi |= result << 4;
241
242 // // odd parity
243 // result = 0;
244 // for (i = 19;i >= 1;i--)
245 // result ^= (*lo >> i) & 1;
246 // *lo |= result;
247 // }
248
249 //static void calc26(uint16_t fc, uint32_t cardno, uint8_t *out){
250 static void calc26(uint16_t fc, uint32_t cardno, uint8_t *out){
251 uint8_t wiegand[24];
252 num_to_bytebits(fc, 8, wiegand);
253 num_to_bytebits(cardno, 16, wiegand+8);
254 wiegand_add_parity(out, wiegand, sizeof(wiegand) );
255 }
256 // static void calc33(uint16_t fc, uint32_t cardno, uint8_t *out){
257 // }
258 static void calc34(uint16_t fc, uint32_t cardno, uint8_t *out){
259 uint8_t wiegand[32];
260 num_to_bytebits(fc, 16, wiegand);
261 num_to_bytebits(cardno, 16, wiegand + 16);
262 wiegand_add_parity(out, wiegand, sizeof(wiegand) );
263 }
264 // static void calc35(uint16_t fc, uint32_t cardno, uint8_t *out){
265 // *lo = ((cardno & 0xFFFFF) << 1) | fc << 21;
266 // *hi = (1 << 5) | ((fc >> 11) & 1);
267 // }
268 static void calc37S(uint16_t fc, uint32_t cardno, uint8_t *out){
269 // FC 2 - 17 - 16 bit
270 // cardno 18 - 36 - 19 bit
271 // Even P1 1 - 19
272 // Odd P37 19 - 36
273 uint8_t wiegand[35];
274 num_to_bytebits(fc, 16, wiegand);
275 num_to_bytebits(cardno, 19, wiegand + 16);
276 wiegand_add_parity(out, wiegand, sizeof(wiegand) );
277 }
278 static void calc37H(uint64_t cardno, uint8_t *out){
279 // SC NONE
280 // cardno 1-35 34 bits
281 // Even Parity 0th bit 1-18
282 // Odd Parity 36th bit 19-35
283 uint8_t wiegand[37];
284 num_to_bytebits( (uint32_t)(cardno >> 32), 2, wiegand);
285 num_to_bytebits( (uint32_t)(cardno >> 0), 32, wiegand + 2);
286 wiegand_add_parity(out, wiegand, sizeof(wiegand) );
287
288 printf("%x %x\n", (uint32_t)(cardno >> 32), (uint32_t)cardno );
289 }
290 // static void calc40(uint64_t cardno, uint8_t *out){
291 // cardno = (cardno & 0xFFFFFFFFFF);
292 // *lo = ((cardno & 0xFFFFFFFF) << 1 );
293 // *hi = (cardno >> 31);
294 // }
295
296 void calcWiegand(uint8_t fmtlen, uint16_t fc, uint64_t cardno, uint8_t *bits){
297 uint32_t cn32 = (cardno & 0xFFFFFFFF);
298 switch ( fmtlen ) {
299 case 26: calc26(fc, cn32, bits); break;
300 // case 33 : calc33(fc, cn32, bits); break;
301 case 34: calc34(fc, cn32, bits); break;
302 // case 35 : calc35(fc, cn32, bits); break;
303 case 37: calc37S(fc, cn32, bits); break;
304 case 38: calc37H(cardno, bits); break;
305 // case 40 : calc40(cardno, bits); break;
306 // case 44 : { break; }
307 // case 84 : { break; }
308 default: break;
309 }
310 }
311
312 int CmdHIDWiegand(const char *Cmd) {
313 uint32_t oem = 0, fc = 0;
314 uint64_t cardnum = 0;
315 uint64_t blocks = 0, wiegand = 0;
316
317 uint8_t bits[BITS];
318 uint8_t *bs = bits;
319 memset(bs, 0, sizeof(bits));
320
321 uint8_t ctmp = param_getchar(Cmd, 0);
322 if ( strlen(Cmd) == 0 || strlen(Cmd) < 3 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_wiegand();
323
324 oem = param_get8(Cmd, 0);
325 fc = param_get32ex(Cmd, 1, 0, 10);
326 cardnum = param_get64ex(Cmd, 2, 0, 10);
327
328 uint8_t fmtlen[] = {26,33,34,35,37,38,40};
329
330 PrintAndLog("HID | OEM | FC | CN | Wiegand | HID Formatted");
331 PrintAndLog("----+-----+------+---------+-----------+--------------------");
332 for (uint8_t i = 0; i < sizeof(fmtlen); i++){
333 memset(bits, 0x00, sizeof(bits));
334 calcWiegand( fmtlen[i], fc, cardnum, bs);
335 printf("ice:: %s \n", sprint_bin(bs, fmtlen[i]));
336 wiegand = (uint64_t)bytebits_to_byte(bs, 32) << 32 | bytebits_to_byte(bs+32, 32);
337
338 addHIDMarker(fmtlen[i], bs);
339 printf("ice:: %s\n", sprint_bin(bs, BITS));
340 blocks = (uint64_t)bytebits_to_byte(bs+32, 32) << 32 | bytebits_to_byte(bs+64, 32);
341 uint8_t shifts = 64-fmtlen[i];
342 wiegand >>= shifts;
343
344 PrintAndLog(" %u | %03u | %03u | %" PRIu64 " | %" PRIX64 " | %" PRIX64 ,
345 fmtlen[i],
346 oem,
347 fc,
348 cardnum,
349 wiegand,
350 blocks
351 );
352 }
353 PrintAndLog("----+-----+-----+-------+-----------+--------------------");
354 return 0;
355 }
356
357 int CmdHIDBrute(const char *Cmd){
358
359 bool errors = false;
360 uint32_t fc = 0, cn = 0, delay = 1000;
361 uint8_t fmtlen = 0;
362 uint8_t bits[96];
363 uint8_t *bs = bits;
364 memset(bs, 0, sizeof(bits));
365 uint8_t cmdp = 0;
366
367 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
368 switch(param_getchar(Cmd, cmdp)) {
369 case 'h':
370 case 'H':
371 return usage_lf_hid_brute();
372 case 'f':
373 case 'F':
374 fc = param_get32ex(Cmd ,cmdp+1, 0, 10);
375 if ( !fc )
376 errors = true;
377 cmdp += 2;
378 break;
379 case 'd':
380 case 'D':
381 // delay between attemps, defaults to 1000ms.
382 delay = param_get32ex(Cmd, cmdp+1, 1000, 10);
383 cmdp += 2;
384 break;
385 case 'c':
386 case 'C':
387 cn = param_get32ex(Cmd, cmdp+1, 0, 10);
388 // truncate cardnumber.
389 cn &= 0xFFFF;
390 cmdp += 2;
391 break;
392 case 'a':
393 case 'A':
394 fmtlen = param_get8(Cmd, cmdp+1);
395 cmdp += 2;
396 bool is_ftm_ok = FALSE;
397 uint8_t ftms[] = {26,33,34,35,37};
398 for ( uint8_t i = 0; i < sizeof(ftms); i++){
399 if ( ftms[i] == fmtlen ) {
400 is_ftm_ok = TRUE;
401 }
402 }
403 // negated
404 errors = !is_ftm_ok;
405 break;
406 default:
407 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
408 errors = true;
409 break;
410 }
411 }
412 if ( fc == 0 ) errors = true;
413 if ( errors ) return usage_lf_hid_brute();
414
415 PrintAndLog("Brute-forcing HID reader");
416 PrintAndLog("Press pm3-button to abort simulation or run another command");
417
418 uint16_t up = cn;
419 uint16_t down = cn;
420
421 for (;;){
422
423 if ( offline ) {
424 printf("Device offline\n");
425 return 2;
426 }
427
428 if (ukbhit()) {
429 PrintAndLog("aborted via keyboard!");
430 return sendPing();
431 }
432
433 // Do one up
434 if ( up < 0xFFFF )
435 if ( !sendTry(fmtlen, fc, up++, delay, bs)) return 1;
436
437 // Do one down (if cardnumber is given)
438 if ( cn > 1 )
439 if ( down > 1 )
440 if ( !sendTry(fmtlen, fc, --down, delay, bs)) return 1;
441 }
442 return 0;
443 }
444
445 static command_t CommandTable[] = {
446 {"help", CmdHelp, 1, "This help"},
447 {"fskdemod",CmdHIDDemodFSK, 0, "Realtime HID FSK demodulator"},
448 {"sim", CmdHIDSim, 0, "HID tag simulator"},
449 {"clone", CmdHIDClone, 0, "Clone HID to T55x7"},
450 {"wiegand", CmdHIDWiegand, 1, "Convert facility code/card number to Wiegand code"},
451 {"brute", CmdHIDBrute, 0, "Bruteforce card number against reader"},
452 {NULL, NULL, 0, NULL}
453 };
454
455 int CmdLFHID(const char *Cmd) {
456 clearCommandBuffer();
457 CmdsParse(CommandTable, Cmd);
458 return 0;
459 }
460
461 int CmdHelp(const char *Cmd) {
462 CmdsHelp(CommandTable);
463 return 0;
464 }
Impressum, Datenschutz