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