]>
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 (known)
11 // RF interface, programming a T55x7 clone, 26-bit HID H10301 encoding:
12 // http://www.proxmark.org/files/Documents/125%20kHz%20-%20HID/HID_format_example.pdf
14 // "Understanding Card Data Formats"
15 // https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
17 // "What Format Do You Need?"
18 // https://www.hidglobal.com/sites/default/files/resource_files/hid-prox-br-en.pdf
19 //-----------------------------------------------------------------------------
28 #include "cmdparser.h"
29 #include "cmddata.h" //for g_debugMode, demodbuff cmds
30 #include "lfdemod.h" // for HIDdemodFSK
31 #include "parity.h" // for parity
32 #include "util.h" // for param_get8,32
34 static int CmdHelp(const char *Cmd
);
38 * Packs a "short" (<38-bit) HID ID from component parts.
40 * This only works with 26, 34, 35 and 37 bit card IDs.
42 * NOTE: Parity calculation is only supported on 26-bit tags. Other card lengths
43 * may have invalid parity.
45 * Returns false on invalid inputs.
47 bool pack_short_hid(/* out */ uint32_t *hi
, /* out */ uint32_t *lo
, /* in */ const short_hid_info
*info
) {
48 uint32_t high
= 0, low
= 0;
50 switch (info
->fmtLen
) {
51 case 26: // HID H10301
52 low
|= (info
->cardnum
& 0xffff) << 1;
53 low
|= (info
->fc
& 0xff) << 17;
55 if (info
->parityValid
) {
57 low
|= oddparity32((low
>> 1) & 0xfff) & 1;
58 low
|= (evenparity32((low
>> 13) & 0xfff) & 1) << 25;
63 low
|= (info
->cardnum
& 0xffff) << 1;
64 low
|= (info
->fc
& 0x7fff) << 17;
65 high
|= (info
->fc
& 0x8000) >> 15;
66 // TODO: Calculate parity
70 low
|= (info
->cardnum
& 0xfffff) << 1;
71 low
|= (info
->fc
& 0x7ff) << 21;
72 high
|= (info
->fc
& 0x800) >> 11;
73 // TODO: Calculate parity
77 low
|= (info
->cardnum
& 0x7ffff) << 1;
78 low
|= (info
->fc
& 0xfff) << 20;
79 high
|= (info
->fc
& 0xf000) >> 12;
80 // TODO: Calculate parity
84 // Invalid / unsupported length
88 // Set the highest bit
89 if (info
->fmtLen
!= 37) {
90 // Bit 37 is always set
93 // Set the bit corresponding to the length.
94 if (info
->fmtLen
< 32) {
95 low
|= 1 << info
->fmtLen
;
97 high
|= 1 << (info
->fmtLen
- 32);
101 // Return result only if successful.
109 * Unpacks a "short" (<38-bit) HID ID into its component parts.
111 * This only works with 26, 34, 35 and 37 bit card IDs.
113 * NOTE: Parity checking is only supported on 26-bit tags.
115 * Returns false on invalid inputs.
117 bool unpack_short_hid(short_hid_info
*out
, uint32_t hi
, uint32_t lo
) {
118 memset(out
, 0, sizeof(short_hid_info
));
120 if (((hi
>> 5) & 1) == 1) {
121 // if bit 38 is set then < 37 bit format is used
123 // get bits 21-37 to check for format len bit
124 lo2
= (((hi
& 31) << 12) | (lo
>> 20));
126 // find last bit set to 1 (format len bit)
132 out
->fmtLen
= idx3
+ 19;
134 switch (out
->fmtLen
) {
135 case 26: // HID H10301
136 out
->cardnum
= (lo
>> 1) & 0xFFFF;
137 out
->fc
= (lo
>> 17) & 0xFF;
140 PrintAndLog("oddparity : input=%x, calculated=%d, provided=%d",
141 (lo
>> 1) & 0xFFF, oddparity32((lo
>> 1) & 0xFFF), lo
& 1);
142 PrintAndLog("evenparity: input=%x, calculated=%d, provided=%d",
143 (lo
>> 13) & 0xFFF, evenparity32((lo
>> 13) & 0xFFF) & 1, (lo
>> 25) & 1);
147 (oddparity32((lo
>> 1) & 0xFFF) == (lo
& 1)) &&
148 ((evenparity32((lo
>> 13) & 0xFFF) & 1) == ((lo
>> 25) & 1));
152 out
->cardnum
= (lo
>> 1) & 0xFFFF;
153 out
->fc
= ((hi
& 1) << 15) | (lo
>> 17);
154 // TODO: Calculate parity
158 out
->cardnum
= (lo
>> 1) & 0xFFFFF;
159 out
->fc
= ((hi
& 1) << 11) | (lo
>> 21);
160 // TODO: Calculate parity
167 // If bit 38 is not set, then 37 bit format is used
169 out
->cardnum
= (lo
>> 1) & 0x7FFFF;
170 out
->fc
= ((hi
& 0xF) << 12) | (lo
>> 20);
171 // TODO: Calculate parity
178 * Converts a hex string to component "hi" and "lo" 32-bit integers, one nibble
181 * Returns the number of nibbles (4 bits) entered.
183 int hexstring_to_int64(/* out */ uint32_t* hi
, /* out */ uint32_t* lo
, const char* str
) {
184 // TODO: Replace this with param_gethex when it supports arbitrary length
188 while (sscanf(&str
[i
++], "%1x", &n
) == 1) {
189 *hi
= (*hi
<< 4) | (*lo
>> 28);
190 *lo
= (*lo
<< 4) | (n
& 0xf);
196 //by marshmellow (based on existing demod + holiman's refactor)
197 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
198 //print full HID Prox ID and some bit format details if found
199 int CmdFSKdemodHID(const char *Cmd
)
201 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
202 uint32_t hi2
=0, hi
=0, lo
=0;
204 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
205 size_t BitLen
= getFromGraphBuf(BitStream
);
206 if (BitLen
==0) return 0;
207 //get binary from fsk wave
209 int idx
= HIDdemodFSK(BitStream
,&BitLen
,&hi2
,&hi
,&lo
, &waveIdx
);
213 PrintAndLog("DEBUG: Just Noise Detected");
214 } else if (idx
== -2) {
215 PrintAndLog("DEBUG: Error demoding fsk");
216 } else if (idx
== -3) {
217 PrintAndLog("DEBUG: Preamble not found");
218 } else if (idx
== -4) {
219 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen
);
221 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
226 if (hi2
==0 && hi
==0 && lo
==0) {
227 if (g_debugMode
) PrintAndLog("DEBUG: Error - no values found");
230 if (hi2
!= 0){ //extra large HID tags
231 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
232 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
234 else { //standard HID tags <38 bits
235 short_hid_info card_info
;
236 bool ret
= unpack_short_hid(&card_info
, (uint32_t)hi
, (uint32_t)lo
);
237 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
238 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF,
239 card_info
.fmtLen
, card_info
.fc
, card_info
.cardnum
);
241 if (card_info
.fmtLen
== 26) {
242 PrintAndLog("Parity: %s", card_info
.parityValid
? "valid" : "invalid");
246 PrintAndLog("Invalid or unsupported tag length.");
249 setDemodBuf(BitStream
,BitLen
,idx
);
250 setClockGrid(50, waveIdx
+ (idx
*50));
252 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
258 int CmdHIDReadFSK(const char *Cmd
)
261 if(Cmd
[0]=='1') findone
=1;
262 UsbCommand c
={CMD_HID_DEMOD_FSK
};
268 int CmdHIDSim(const char *Cmd
)
270 uint32_t hi
= 0, lo
= 0;
271 hexstring_to_int64(&hi
, &lo
, Cmd
);
273 PrintAndLog("This looks like a long tag ID. Use 'lf simfsk' for long tags. Aborting!");
277 PrintAndLog("Emulating tag with ID %x%08x", hi
, lo
);
278 PrintAndLog("Press pm3-button to abort simulation");
280 UsbCommand c
= {CMD_HID_SIM_TAG
, {hi
, lo
, 0}};
285 int CmdHIDClone(const char *Cmd
)
287 unsigned int hi2
= 0, hi
= 0, lo
= 0;
290 if (strchr(Cmd
,'l') != 0) {
293 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
294 hi2
= (hi2
<< 4) | (hi
>> 28);
295 hi
= (hi
<< 4) | (lo
>> 28);
296 lo
= (lo
<< 4) | (n
& 0xf);
299 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2
, hi
, lo
);
304 hexstring_to_int64(&hi
, &lo
, Cmd
);
306 PrintAndLog("This looks like a long tag ID. Aborting!");
310 PrintAndLog("Cloning tag with ID %x%08x", hi
, lo
);
316 c
.cmd
= CMD_HID_CLONE_TAG
;
326 int CmdHIDPack(const char *Cmd
) {
327 uint32_t hi
= 0, lo
= 0;
328 short_hid_info card_info
;
331 PrintAndLog("Usage: lf hid pack <length> <facility code (decimal)> <card number (decimal)>");
332 PrintAndLog(" sample: lf hid pack 26 123 4567");
336 card_info
.fmtLen
= param_get8(Cmd
, 0);
337 card_info
.fc
= param_get32ex(Cmd
, 1, 0, 10);
338 card_info
.cardnum
= param_get32ex(Cmd
, 2, 0, 10);
339 card_info
.parityValid
= true;
342 if (card_info
.fmtLen
!= 26) {
343 PrintAndLog("Warning: Parity bits are only calculated for 26 bit IDs -- this may be invalid!");
346 bool ret
= pack_short_hid(&hi
, &lo
, &card_info
);
349 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
350 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF,
351 card_info
.fmtLen
, card_info
.fc
, card_info
.cardnum
);
353 PrintAndLog("Invalid or unsupported tag length.");
359 int CmdHIDUnpack(const char *Cmd
)
361 uint32_t hi
= 0, lo
= 0;
363 PrintAndLog("Usage: lf hid unpack <ID>");
364 PrintAndLog(" sample: lf hid unpack 2006f623ae");
368 hexstring_to_int64(&hi
, &lo
, Cmd
);
370 PrintAndLog("This looks like a long tag ID. Aborting!");
374 short_hid_info card_info
;
375 bool ret
= unpack_short_hid(&card_info
, hi
, lo
);
377 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
378 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF,
379 card_info
.fmtLen
, card_info
.fc
, card_info
.cardnum
);
381 if (card_info
.fmtLen
== 26) {
382 PrintAndLog("Parity: %s", card_info
.parityValid
? "valid" : "invalid");
386 PrintAndLog("Invalid or unsupported tag length.");
392 static command_t CommandTable
[] =
394 {"help", CmdHelp
, 1, "This help"},
395 {"demod", CmdFSKdemodHID
, 1, "Demodulate HID Prox from GraphBuffer"},
396 {"read", CmdHIDReadFSK
, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
397 {"sim", CmdHIDSim
, 0, "<ID> -- HID tag simulator"},
398 {"clone", CmdHIDClone
, 0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"},
399 {"pack", CmdHIDPack
, 1, "<len> <fc> <num> -- packs a <38 bit (short) HID ID from its length, facility code and card number"},
400 {"unpack", CmdHIDUnpack
, 1, "<ID> -- unpacks a <38 bit (short) HID ID to its length, facility code and card number"},
401 {NULL
, NULL
, 0, NULL
}
404 int CmdLFHID(const char *Cmd
)
406 CmdsParse(CommandTable
, Cmd
);
410 int CmdHelp(const char *Cmd
)
412 CmdsHelp(CommandTable
);