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 "hidcardformats.h"
32 #include "hidcardformatutils.h"
33 #include "util.h" // for param_get8,32,64
37 * Converts a hex string to component "hi2", "hi" and "lo" 32-bit integers, one nibble
40 * Returns the number of nibbles (4 bits) entered.
42 int hid_hexstring_to_int96(/* out */ uint32_t* hi2
,/* out */ uint32_t* hi
, /* out */ uint32_t* lo
, const char* str
) {
43 // TODO: Replace this with param_gethex when it supports arbitrary length
47 while (sscanf(&str
[i
++], "%1x", &n
) == 1) {
48 *hi2
= (*hi2
<< 4) | (*hi
>> 28);
49 *hi
= (*hi
<< 4) | (*lo
>> 28);
50 *lo
= (*lo
<< 4) | (n
& 0xf);
57 PrintAndLog("Usage: lf hid encode <format> [<field> <value (decimal)>] {...}");
58 PrintAndLog(" Fields: c: Card number");
59 PrintAndLog(" f: Facility code");
60 PrintAndLog(" i: Issue Level");
61 PrintAndLog(" o: OEM code");
62 PrintAndLog(" example: lf hid encode H10301 f 123 c 4567");
64 void PrintProxTagId(hidproxmessage_t
*packed
){
65 if (packed
->top
!= 0) {
66 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
67 (uint32_t)packed
->top
, (uint32_t)packed
->mid
, (uint32_t)packed
->bot
);
69 PrintAndLog("HID Prox TAG ID: %x%08x",
70 (uint32_t)packed
->mid
, (uint32_t)packed
->bot
);
73 bool Encode(/* in */ const char *Cmd
, /* out */ hidproxmessage_t
*packed
){
76 memset(format
, 0, sizeof(format
));
77 if (!strcmp(Cmd
, "help") || !strcmp(Cmd
, "h") || !strcmp(Cmd
, "list") || !strcmp(Cmd
, "?")){
81 param_getstr(Cmd
, 0, format
, sizeof(format
));
82 formatIndex
= HIDFindCardFormat(format
);
83 if (formatIndex
== -1) {
84 printf("Unknown format: %s\r\n", format
);
89 memset(&data
, 0, sizeof(hidproxcard_t
));
91 while(param_getchar(Cmd
, cmdp
) != 0x00) {
92 switch(param_getchar(Cmd
, cmdp
)) {
95 data
.IssueLevel
= param_get32ex(Cmd
, cmdp
+1, 0, 10);
100 data
.FacilityCode
= param_get32ex(Cmd
, cmdp
+1, 0, 10);
105 data
.CardNumber
= param_get64ex(Cmd
, cmdp
+1, 0, 10);
110 data
.OEM
= param_get32ex(Cmd
, cmdp
+1, 0, 10);
114 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
118 memset(packed
, 0, sizeof(hidproxmessage_t
));
119 if (!HIDPack(formatIndex
, &data
, packed
))
121 PrintAndLog("The card data could not be encoded in the selected format.");
128 void Write(hidproxmessage_t
*packed
){
130 c
.d
.asBytes
[0] = (packed
->top
!= 0 && ((packed
->mid
& 0xFFFFFFC0) != 0))
131 ? 1 : 0; // Writing long format?
132 c
.cmd
= CMD_HID_CLONE_TAG
;
133 c
.arg
[0] = (packed
->top
& 0x000FFFFF);
134 c
.arg
[1] = packed
->mid
;
135 c
.arg
[2] = packed
->bot
;
140 //by marshmellow (based on existing demod + holiman's refactor)
141 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
142 //print full HID Prox ID and some bit format details if found
143 int CmdFSKdemodHID(const char *Cmd
)
145 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
146 uint32_t hi2
=0, hi
=0, lo
=0;
148 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
149 size_t BitLen
= getFromGraphBuf(BitStream
);
150 if (BitLen
==0) return 0;
151 //get binary from fsk wave
153 int idx
= HIDdemodFSK(BitStream
,&BitLen
,&hi2
,&hi
,&lo
, &waveIdx
);
157 PrintAndLog("DEBUG: Just Noise Detected");
158 } else if (idx
== -2) {
159 PrintAndLog("DEBUG: Error demoding fsk");
160 } else if (idx
== -3) {
161 PrintAndLog("DEBUG: Preamble not found");
162 } else if (idx
== -4) {
163 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen
);
165 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
170 if (hi2
==0 && hi
==0 && lo
==0) {
171 if (g_debugMode
) PrintAndLog("DEBUG: Error - no values found");
175 hidproxmessage_t packed
= initialize_proxmessage_object(hi2
, hi
, lo
);
176 PrintProxTagId(&packed
);
178 bool ret
= HIDTryUnpack(&packed
, false);
180 PrintAndLog("Invalid or unsupported tag length.");
182 setDemodBuf(BitStream
,BitLen
,idx
);
183 setClockGrid(50, waveIdx
+ (idx
*50));
185 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
191 int CmdHIDReadFSK(const char *Cmd
)
194 if(Cmd
[0]=='1') findone
=1;
195 UsbCommand c
={CMD_HID_DEMOD_FSK
};
201 int CmdHIDSim(const char *Cmd
)
203 uint32_t hi2
= 0, hi
= 0, lo
= 0;
204 hid_hexstring_to_int96(&hi2
, &hi
, &lo
, Cmd
);
206 PrintAndLog("Emulating tag with ID %x%08x%08x", hi2
, hi
, lo
);
208 PrintAndLog("Emulating tag with ID %x%08x", hi
, lo
);
211 PrintAndLog("Press pm3-button to abort simulation");
213 UsbCommand c
= {CMD_HID_SIM_TAG
, {hi2
, hi
, lo
}};
218 int CmdHIDClone(const char *Cmd
)
220 unsigned int top
= 0, mid
= 0, bot
= 0;
221 hid_hexstring_to_int96(&top
, &mid
, &bot
, Cmd
);
222 hidproxmessage_t packed
= initialize_proxmessage_object(top
, mid
, bot
);
227 int CmdHIDDecode(const char *Cmd
){
229 PrintAndLog("Usage: lf hid decode <id> {p}");
230 PrintAndLog(" (optional) p: Ignore invalid parity");
231 PrintAndLog(" sample: lf hid decode 2006f623ae");
235 uint32_t top
= 0, mid
= 0, bot
= 0;
236 bool ignoreParity
= false;
237 hid_hexstring_to_int96(&top
, &mid
, &bot
, Cmd
);
238 hidproxmessage_t packed
= initialize_proxmessage_object(top
, mid
, bot
);
240 char opt
= param_getchar(Cmd
, 1);
241 if (opt
== 'p') ignoreParity
= true;
243 HIDTryUnpack(&packed
, ignoreParity
);
246 int CmdHIDEncode(const char *Cmd
) {
247 if (strlen(Cmd
) == 0) {
252 hidproxmessage_t packed
;
253 memset(&packed
, 0, sizeof(hidproxmessage_t
));
254 if (Encode(Cmd
, &packed
))
255 PrintProxTagId(&packed
);
259 int CmdHIDWrite(const char *Cmd
) {
260 if (strlen(Cmd
) == 0) {
264 hidproxmessage_t packed
;
265 memset(&packed
, 0, sizeof(hidproxmessage_t
));
266 if (Encode(Cmd
, &packed
)){
267 PrintProxTagId(&packed
);
277 static int CmdHelp(const char *Cmd
); // define this now so the below won't error out.
278 static command_t CommandTable
[] =
280 {"help", CmdHelp
, 1, "This help"},
281 {"demod", CmdFSKdemodHID
, 1, "Demodulate HID Prox from GraphBuffer"},
282 {"read", CmdHIDReadFSK
, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
283 {"sim", CmdHIDSim
, 0, "<ID> -- HID tag simulator"},
284 {"clone", CmdHIDClone
, 0, "<ID> -- Clone HID to T55x7 (tag must be in antenna)"},
285 {"decode", CmdHIDDecode
, 1, "<ID> -- Try to decode an HID tag and show its contents"},
286 {"encode", CmdHIDEncode
, 1, "<format> <fields> -- Encode an HID ID with the specified format and fields"},
287 {"formats", CmdHIDFormats
, 1, "List supported card formats"},
288 {"write", CmdHIDWrite
, 0, "<format> <fields> -- Encode and write to a T55x7 tag (tag must be in antenna)"},
289 {NULL
, NULL
, 0, NULL
}
292 int CmdLFHID(const char *Cmd
)
294 CmdsParse(CommandTable
, Cmd
);
298 int CmdHelp(const char *Cmd
)
300 CmdsHelp(CommandTable
);