]>
cvs.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 //-----------------------------------------------------------------------------
17 static int CmdHelp(const char *Cmd
);
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]");
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");
32 int usage_lf_hid_sim(void){
33 PrintAndLog("HID Tag simulator");
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");
43 int usage_lf_hid_clone(void){
44 PrintAndLog("Clone HID to T55x7. Tag must be on antenna. ");
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");
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");
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");
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");
76 static int sendPing(void){
77 UsbCommand ping
= {CMD_PING
, {1, 2, 3}};
83 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1000))
87 static bool sendTry(uint8_t fmtlen
, uint32_t fc
, uint32_t cn
, uint32_t delay
, uint8_t *bs
){
89 PrintAndLog("Trying FC: %u; CN: %u", fc
, cn
);
91 calcWiegand( fmtlen
, fc
, cn
, bs
);
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}};
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();
111 int CmdHIDSim(const char *Cmd
) {
112 unsigned int hi
= 0, lo
= 0;
115 uint8_t ctmp
= param_getchar(Cmd
, 0);
116 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_hid_sim();
118 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
119 hi
= (hi
<< 4) | (lo
>> 28);
120 lo
= (lo
<< 4) | (n
& 0xf);
123 PrintAndLog("Emulating tag with ID %x%16x", hi
, lo
);
124 PrintAndLog("Press pm3-button to abort simulation");
126 UsbCommand c
= {CMD_HID_SIM_TAG
, {hi
, lo
, 0}};
127 clearCommandBuffer();
132 int CmdHIDClone(const char *Cmd
) {
134 uint32_t hi2
= 0, hi
= 0, lo
= 0;
138 uint8_t ctmp
= param_getchar(Cmd
, 0);
139 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_hid_clone();
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);
148 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2
, hi
, lo
);
152 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
153 hi
= (hi
<< 4) | (lo
>> 28);
154 lo
= (lo
<< 4) | (n
& 0xf);
157 PrintAndLog("Cloning tag with ID %x%08x", hi
, lo
);
163 c
.cmd
= CMD_HID_CLONE_TAG
;
168 clearCommandBuffer();
172 // struct to handle wiegand
176 uint8_t FacilityCode
;
182 static void addHIDMarker(uint8_t fmtlen
, uint8_t *out
) {
185 memset(arr
, 0, BITS
);
188 uint8_t pos
= sizeof(arr
)-fmtlen
;
189 memcpy(arr
+pos
, out
, fmtlen
);
193 // start sentinel, BITS-bit 27 = 1
195 // fmt smaller than 37 used, bit37 = 1
197 memcpy(out
, arr
, BITS
);
201 // start sentinel, BITS-bit 27 = 1
204 // fmt smaller than 37 used, bit37 = 1
206 memcpy(out
, arr
, BITS
);
212 // static void getParity34(uint32_t *hi, uint32_t *lo){
213 // uint32_t result = 0;
217 // for (i = 7;i >= 0;i--)
218 // result ^= (*hi >> i) & i;
219 // for (i = 31;i >= 24;i--)
220 // result ^= (*lo >> i) & 1;
222 // *hi |= result << 2;
226 // for (i = 23;i >= 1;i--)
227 // result ^= (*lo >> i) & 1;
231 // static void getParity37H(uint32_t *hi, uint32_t *lo){
232 // uint32_t result = 0;
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;
244 // for (i = 19;i >= 1;i--)
245 // result ^= (*lo >> i) & 1;
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
){
252 num_to_bytebits(fc
, 8, wiegand
);
253 num_to_bytebits(cardno
, 16, wiegand
+8);
254 wiegand_add_parity(out
, wiegand
, sizeof(wiegand
) );
256 // static void calc33(uint16_t fc, uint32_t cardno, uint8_t *out){
258 static void calc34(uint16_t fc
, uint32_t cardno
, uint8_t *out
){
260 num_to_bytebits(fc
, 16, wiegand
);
261 num_to_bytebits(cardno
, 16, wiegand
+ 16);
262 wiegand_add_parity(out
, wiegand
, sizeof(wiegand
) );
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);
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
274 num_to_bytebits(fc
, 16, wiegand
);
275 num_to_bytebits(cardno
, 19, wiegand
+ 16);
276 wiegand_add_parity(out
, wiegand
, sizeof(wiegand
) );
278 static void calc37H(uint64_t cardno
, uint8_t *out
){
280 // cardno 1-35 34 bits
281 // Even Parity 0th bit 1-18
282 // Odd Parity 36th bit 19-35
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
) );
288 printf("%x %x\n", (uint32_t)(cardno
>> 32), (uint32_t)cardno
);
290 // static void calc40(uint64_t cardno, uint8_t *out){
291 // cardno = (cardno & 0xFFFFFFFFFF);
292 // *lo = ((cardno & 0xFFFFFFFF) << 1 );
293 // *hi = (cardno >> 31);
296 void calcWiegand(uint8_t fmtlen
, uint16_t fc
, uint64_t cardno
, uint8_t *bits
){
297 uint32_t cn32
= (cardno
& 0xFFFFFFFF);
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; }
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;
319 memset(bs
, 0, sizeof(bits
));
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();
324 oem
= param_get8(Cmd
, 0);
325 fc
= param_get32ex(Cmd
, 1, 0, 10);
326 cardnum
= param_get64ex(Cmd
, 2, 0, 10);
328 uint8_t fmtlen
[] = {26,33,34,35,37,38,40};
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);
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
];
344 PrintAndLog(" %u | %03u | %03u | %llu | %llX | %llX",
353 PrintAndLog("----+-----+-----+-------+-----------+--------------------");
357 int CmdHIDBrute(const char *Cmd
){
360 uint32_t fc
= 0, cn
= 0, delay
= 1000;
364 memset(bs
, 0, sizeof(bits
));
367 while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) {
368 switch(param_getchar(Cmd
, cmdp
)) {
371 return usage_lf_hid_brute();
374 fc
= param_get32ex(Cmd
,cmdp
+1, 0, 10);
381 // delay between attemps, defaults to 1000ms.
382 delay
= param_get32ex(Cmd
, cmdp
+1, 1000, 10);
387 cn
= param_get32ex(Cmd
, cmdp
+1, 0, 10);
388 // truncate cardnumber.
394 fmtlen
= param_get8(Cmd
, cmdp
+1);
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
) {
407 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
412 if ( fc
== 0 ) errors
= true;
413 if ( errors
) return usage_lf_hid_brute();
415 PrintAndLog("Brute-forcing HID reader");
416 PrintAndLog("Press pm3-button to abort simulation or run another command");
424 printf("Device offline\n");
429 PrintAndLog("aborted via keyboard!");
435 if ( !sendTry(fmtlen
, fc
, up
++, delay
, bs
)) return 1;
437 // Do one down (if cardnumber is given)
440 if ( !sendTry(fmtlen
, fc
, --down
, delay
, bs
)) return 1;
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
}
455 int CmdLFHID(const char *Cmd
) {
456 clearCommandBuffer();
457 CmdsParse(CommandTable
, Cmd
);
461 int CmdHelp(const char *Cmd
) {
462 CmdsHelp(CommandTable
);