]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Low frequency HID commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
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]");
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");
28 int usage_lf_hid_sim(void){
29 PrintAndLog("HID Tag simulator");
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");
39 int usage_lf_hid_clone(void){
40 PrintAndLog("Clone HID to T55x7. Tag must be on antenna. ");
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");
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.");
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");
63 PrintAndLog("Sample : lf hid brute 26 224");
67 int CmdHIDDemodFSK(const char *Cmd
) {
68 int findone
= ( Cmd
[0] == '1' ) ? 1 : 0;
69 UsbCommand c
= {CMD_HID_DEMOD_FSK
, {findone
, 0 , 0}};
75 int CmdHIDSim(const char *Cmd
) {
76 unsigned int hi
= 0, lo
= 0;
79 uint8_t ctmp
= param_getchar(Cmd
, 0);
80 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_hid_sim();
82 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
83 hi
= (hi
<< 4) | (lo
>> 28);
84 lo
= (lo
<< 4) | (n
& 0xf);
87 PrintAndLog("Emulating tag with ID %x%16x", hi
, lo
);
88 PrintAndLog("Press pm3-button to abort simulation");
90 UsbCommand c
= {CMD_HID_SIM_TAG
, {hi
, lo
, 0}};
96 int CmdHIDClone(const char *Cmd
) {
98 unsigned int hi2
= 0, hi
= 0, lo
= 0;
102 uint8_t ctmp
= param_getchar(Cmd
, 0);
103 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_hid_clone();
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);
112 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2
, hi
, lo
);
116 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
117 hi
= (hi
<< 4) | (lo
>> 28);
118 lo
= (lo
<< 4) | (n
& 0xf);
121 PrintAndLog("Cloning tag with ID %x%08x", hi
, lo
);
127 c
.cmd
= CMD_HID_CLONE_TAG
;
132 clearCommandBuffer();
136 // struct to handle wiegand
140 uint8_t FacilityCode
;
146 // static void addHIDMarker(uint8_t fmtlen, uint8_t *out) {
149 //static void getParity26(uint32_t *hi, uint32_t *lo){
150 // uint32_t result = 0;
153 // for (i = 24;i >= 13;i--)
154 // result ^= (*lo >> i) & 1;
155 // // even parity 26th bit
156 // *lo |= result << 25;
160 // for (i = 12;i >= 1;i--)
161 // result ^= (*lo >> i) & 1;
165 // static void getParity33(uint32_t *hi, uint32_t *lo){
168 // static void getParity34(uint32_t *hi, uint32_t *lo){
169 // uint32_t result = 0;
173 // for (i = 7;i >= 0;i--)
174 // result ^= (*hi >> i) & i;
175 // for (i = 31;i >= 24;i--)
176 // result ^= (*lo >> i) & 1;
178 // *hi |= result << 2;
182 // for (i = 23;i >= 1;i--)
183 // result ^= (*lo >> i) & 1;
187 // static void getParity35(uint32_t *hi, uint32_t *lo){
189 // static void getParity37S(uint32_t *hi,uint32_t *lo){
190 // uint32_t result = 0;
194 // for (i = 4; i >= 0; i--)
195 // result ^= (*hi >> i) & 1;
197 // for (i = 31; i >= 20; i--)
198 // result ^= (*lo >> i) & 1;
204 // for (i = 19; i >= 1; i--)
205 // result ^= (*lo >> i) & 1;
209 // static void getParity37H(uint32_t *hi, uint32_t *lo){
210 // uint32_t result = 0;
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;
222 // for (i = 19;i >= 1;i--)
223 // result ^= (*lo >> i) & 1;
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
){
231 num_to_bytebits(fc
, 8, wiegand
);
232 num_to_bytebits(cardno
, 16, wiegand
+8);
233 wiegand_add_parity(out
, wiegand
, sizeof(wiegand
) );
235 // *out |= (1 << 26); // why this?
236 // *out |= (1 << 37); // bit format for hid?
238 // static void calc33(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){
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);
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);
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
255 // // Odd P37 19 - 36
258 // *lo = ((fc << 20) | (cardno & 0x7FFFF) << 1);
261 // static void calc37H(uint64_t cardno, uint32_t *hi, uint32_t *lo){
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);
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);
276 static void calcWiegand(uint8_t fmtlen
, uint16_t fc
, uint64_t cardno
, uint8_t *bits
){
278 // uint32_t hi = 0, lo = 0;
279 // uint32_t cn32 = (cardno & 0xFFFFFFFF);
280 // switch ( fmtlen ) {
282 // calc26(fc, cn32, bits);
283 // addHIDFormatMarker(fmtlen, bits);
287 // // calc33(fc, cn32, hi, lo);
288 // // getParity33(hi, lo);
292 // calc34(fc, cn32, hi, lo);
293 // getParity34(hi, lo);
297 // calc35(fc, cn32, hi, lo);
298 // getParity35(hi, lo);
302 // calc37S(fc, cn32, hi, lo);
303 // getParity37S(hi, lo);
307 // calc37H(cn32, hi, lo);
308 // getParity37H(hi, lo);
311 // case 40 : calc40(cardno, hi, lo); break;
312 // case 44 : { break; }
313 // case 84 : { break; }
318 int CmdHIDWiegand(const char *Cmd
) {
319 uint32_t oem
= 0, fc
= 0;
320 uint64_t cardnum
= 0;
322 uint32_t blocks
[2] = {0,0};
323 uint32_t wiegand
[2] = {0,0};
327 memset(bs
, 0, sizeof(bits
));
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();
332 oem
= param_get8(Cmd
, 0);
333 fc
= param_get32ex(Cmd
, 1, 0, 10);
334 cardnum
= param_get64ex(Cmd
, 2, 0, 10);
337 uint8_t ftmlen
[] = {26,33,34,35,37,38,40};
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 ",
356 PrintAndLog("----+-----+-----+-------+-----------+--------------------");
360 int CmdHIDBrute(const char *Cmd
){
363 uint8_t fc
= 0, fmtlen
= 0;
367 memset(bs
, 0, sizeof(bits
));
369 UsbCommand c
= {CMD_HID_SIM_TAG
, {0, 0, 0}};
371 char cmdp
= param_getchar(Cmd
, 0);
372 if (strlen(Cmd
) > 2 || strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_hid_brute();
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
) {
382 if ( error
) return usage_lf_hid_brute();
384 fc
= param_get8(Cmd
, 1);
385 if ( fc
== 0) return usage_lf_hid_brute();
387 PrintAndLog("Brute-forcing HID reader");
388 PrintAndLog("Press pm3-button to abort simulation or run another command");
390 for ( uint16_t cn
= 1; cn
< 0xFFFF; ++cn
){
392 PrintAndLog("aborted via keyboard!");
397 clearCommandBuffer();
402 calcWiegand( fmtlen
, fc
, cn
, bs
);
404 c
.arg
[0] = bytebits_to_byte(bs
,32);
405 c
.arg
[1] = bytebits_to_byte(bs
+32,32);
406 clearCommandBuffer();
409 PrintAndLog("Trying FC: %u; CN: %u", fc
, cn
);
416 static 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
}
426 int CmdLFHID(const char *Cmd
) {
427 clearCommandBuffer();
428 CmdsParse(CommandTable
, Cmd
);
432 int CmdHelp(const char *Cmd
) {
433 CmdsHelp(CommandTable
);