]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
FIX: that OEM variable that is unused.
[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 static int CmdHelp(const char *Cmd);
14
15 int 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 }
28 int 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 }
39 int 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 }
52 int 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
67 int 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
75 int 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
96 int 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
137 typedef 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){
228 void 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
276 static 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
318 int CmdHIDWiegand(const char *Cmd) {
319 uint32_t oem = 0, fc = 0;
320 uint64_t cardnum = 0;
321
322 uint32_t blocks[2], wiegand[2];
323
324 uint8_t bits[96];
325 uint8_t *bs = bits;
326 memset(bs, 0, sizeof(bits));
327
328 uint8_t ctmp = param_getchar(Cmd, 0);
329 if ( strlen(Cmd) == 0 || strlen(Cmd) < 3 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_wiegand();
330
331 oem = param_get8(Cmd, 0);
332 fc = param_get32ex(Cmd, 1, 0, 10);
333 cardnum = param_get64ex(Cmd, 2, 0, 10);
334
335 //
336 uint8_t ftmlen[] = {26,33,34,35,37,38,40};
337
338 PrintAndLog("HID | OEM | FC | CN | Wiegand | HID Formatted");
339 PrintAndLog("----+-----+-----+-------+-----------+--------------------");
340 for (uint8_t i = 0; i < sizeof(ftmlen); i++){
341 calcWiegand( ftmlen[i], fc, cardnum, bs);
342 blocks[0] = bytebits_to_byte(bs,32);
343 blocks[1] = bytebits_to_byte(bs+32,32);
344 PrintAndLog(" %d | %d | %d | %llu | %08X%08X | %08X%08X ",
345 ftmlen,
346 oem,
347 fc,
348 cardnum,
349 wiegand[0],
350 wiegand[1],
351 blocks[0],
352 blocks[1]
353 );
354 }
355 PrintAndLog("----+-----+-----+-------+-----------+--------------------");
356 return 0;
357 }
358
359 int CmdHIDBrute(const char *Cmd){
360
361 bool error = TRUE;
362 uint8_t fc = 0, fmtlen = 0;
363
364 uint8_t bits[96];
365 uint8_t *bs = bits;
366 memset(bs, 0, sizeof(bits));
367
368 UsbCommand c = {CMD_HID_SIM_TAG, {0, 0, 0}};
369
370 char cmdp = param_getchar(Cmd, 0);
371 if (strlen(Cmd) > 2 || strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_hid_brute();
372
373 fmtlen = param_get8(Cmd, 0);
374 uint8_t ftms[] = {26,33,34,35,37};
375 for ( uint8_t i = 0; i < sizeof(ftms); i++){
376 if ( ftms[i] == fmtlen ) {
377 error = FALSE;
378 }
379 }
380
381 if ( error ) return usage_lf_hid_brute();
382
383 fc = param_get8(Cmd, 1);
384 if ( fc == 0) return usage_lf_hid_brute();
385
386 PrintAndLog("Brute-forcing HID reader");
387 PrintAndLog("Press pm3-button to abort simulation or run another command");
388
389 for ( uint16_t cn = 1; cn < 0xFFFF; ++cn){
390 if (ukbhit()) {
391 PrintAndLog("aborted via keyboard!");
392 c.cmd = CMD_PING;
393 c.arg[0] = 0x00;
394 c.arg[1] = 0x00;
395 c.arg[2] = 0x00;
396 clearCommandBuffer();
397 SendCommand(&c);
398 return 1;
399 }
400
401 calcWiegand( fmtlen, fc, cn, bs);
402
403 c.arg[0] = bytebits_to_byte(bs,32);
404 c.arg[1] = bytebits_to_byte(bs+32,32);
405 clearCommandBuffer();
406 SendCommand(&c);
407
408 PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
409 // pause
410 sleep(1);
411 }
412 return 0;
413 }
414
415 static command_t CommandTable[] = {
416 {"help", CmdHelp, 1, "This help"},
417 {"fskdemod",CmdHIDDemodFSK, 0, "[1] Realtime HID FSK demodulator (option '1' for one tag only)"},
418 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
419 {"clone", CmdHIDClone, 0, "<ID> [L] -- Clone HID to T55x7"},
420 {"wiegand", CmdHIDWiegand, 0, "<OEM> <facility code> <card number> -- convert facility code/card number to Wiegand code"},
421 {"brute", CmdHIDBrute, 0, "<format length> <facility code> -- brute force card number"},
422 {NULL, NULL, 0, NULL}
423 };
424
425 int CmdLFHID(const char *Cmd) {
426 clearCommandBuffer();
427 CmdsParse(CommandTable, Cmd);
428 return 0;
429 }
430
431 int CmdHelp(const char *Cmd) {
432 CmdsHelp(CommandTable);
433 return 0;
434 }
Impressum, Datenschutz