]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
Fix #912
[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 (known)
9 //
10 // Useful resources:
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
13 //
14 // "Understanding Card Data Formats"
15 // https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
16 //
17 // "What Format Do You Need?"
18 // https://www.hidglobal.com/sites/default/files/resource_files/hid-prox-br-en.pdf
19 //-----------------------------------------------------------------------------
20
21 #include "cmdlfhid.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include "comms.h"
26 #include "ui.h"
27 #include "graph.h"
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
34
35
36 /**
37 * Converts a hex string to component "hi2", "hi" and "lo" 32-bit integers, one nibble
38 * at a time.
39 *
40 * Returns the number of nibbles (4 bits) entered.
41 */
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
44 // inputs.
45 int n = 0, i = 0;
46
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);
51 }
52
53 return i - 1;
54 }
55
56 void usage_encode(){
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");
63 }
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);
68 } else {
69 PrintAndLog("HID Prox TAG ID: %x%08x",
70 (uint32_t)packed->mid, (uint32_t)packed->bot);
71 }
72 }
73 bool Encode(/* in */ const char *Cmd, /* out */ hidproxmessage_t *packed){
74 int formatIndex = -1;
75 char format[16];
76 memset(format, 0, sizeof(format));
77 if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
78 usage_encode();
79 return false;
80 } else {
81 param_getstr(Cmd, 0, format, sizeof(format));
82 formatIndex = HIDFindCardFormat(format);
83 if (formatIndex == -1) {
84 printf("Unknown format: %s\r\n", format);
85 return false;
86 }
87 }
88 hidproxcard_t data;
89 memset(&data, 0, sizeof(hidproxcard_t));
90 uint8_t cmdp = 1;
91 while(param_getchar(Cmd, cmdp) != 0x00) {
92 switch(param_getchar(Cmd, cmdp)) {
93 case 'I':
94 case 'i':
95 data.IssueLevel = param_get32ex(Cmd, cmdp+1, 0, 10);
96 cmdp += 2;
97 break;
98 case 'F':
99 case 'f':
100 data.FacilityCode = param_get32ex(Cmd, cmdp+1, 0, 10);
101 cmdp += 2;
102 break;
103 case 'C':
104 case 'c':
105 data.CardNumber = param_get64ex(Cmd, cmdp+1, 0, 10);
106 cmdp += 2;
107 break;
108 case 'o':
109 case 'O':
110 data.OEM = param_get32ex(Cmd, cmdp+1, 0, 10);
111 cmdp += 2;
112 break;
113 default:
114 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
115 return false;
116 }
117 }
118 memset(packed, 0, sizeof(hidproxmessage_t));
119 if (!HIDPack(formatIndex, &data, packed))
120 {
121 PrintAndLog("The card data could not be encoded in the selected format.");
122 return false;
123 } else {
124 return true;
125 }
126
127 }
128 void Write(hidproxmessage_t *packed){
129 UsbCommand c;
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;
136 SendCommand(&c);
137 }
138
139
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)
144 {
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;
147
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
152 int waveIdx = 0;
153 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx);
154 if (idx<0){
155 if (g_debugMode){
156 if (idx==-1){
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);
164 } else {
165 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
166 }
167 }
168 return 0;
169 }
170 if (hi2==0 && hi==0 && lo==0) {
171 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
172 return 0;
173 }
174
175 hidproxmessage_t packed = initialize_proxmessage_object(hi2, hi, lo);
176 PrintProxTagId(&packed);
177
178 bool ret = HIDTryUnpack(&packed, false);
179 if (!ret) {
180 PrintAndLog("Invalid or unsupported tag length.");
181 }
182 setDemodBuf(BitStream,BitLen,idx);
183 setClockGrid(50, waveIdx + (idx*50));
184 if (g_debugMode){
185 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
186 printDemodBuff();
187 }
188 return 1;
189 }
190
191 int CmdHIDReadFSK(const char *Cmd)
192 {
193 int findone=0;
194 if(Cmd[0]=='1') findone=1;
195 UsbCommand c={CMD_HID_DEMOD_FSK};
196 c.arg[0]=findone;
197 SendCommand(&c);
198 return 0;
199 }
200
201 int CmdHIDSim(const char *Cmd)
202 {
203 uint32_t hi2 = 0, hi = 0, lo = 0;
204 hid_hexstring_to_int96(&hi2, &hi, &lo, Cmd);
205 if (hi2 != 0) {
206 PrintAndLog("Emulating tag with ID %x%08x%08x", hi2, hi, lo);
207 } else {
208 PrintAndLog("Emulating tag with ID %x%08x", hi, lo);
209 }
210
211 PrintAndLog("Press pm3-button to abort simulation");
212
213 UsbCommand c = {CMD_HID_SIM_TAG, {hi2, hi, lo}};
214 SendCommand(&c);
215 return 0;
216 }
217
218 int CmdHIDClone(const char *Cmd)
219 {
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);
223 Write(&packed);
224 return 0;
225 }
226
227 int CmdHIDDecode(const char *Cmd){
228 if (strlen(Cmd)<3) {
229 PrintAndLog("Usage: lf hid decode <id> {p}");
230 PrintAndLog(" (optional) p: Ignore invalid parity");
231 PrintAndLog(" sample: lf hid decode 2006f623ae");
232 return 0;
233 }
234
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);
239
240 char opt = param_getchar(Cmd, 1);
241 if (opt == 'p') ignoreParity = true;
242
243 HIDTryUnpack(&packed, ignoreParity);
244 return 0;
245 }
246 int CmdHIDEncode(const char *Cmd) {
247 if (strlen(Cmd) == 0) {
248 usage_encode();
249 return 0;
250 }
251
252 hidproxmessage_t packed;
253 memset(&packed, 0, sizeof(hidproxmessage_t));
254 if (Encode(Cmd, &packed))
255 PrintProxTagId(&packed);
256 return 0;
257 }
258
259 int CmdHIDWrite(const char *Cmd) {
260 if (strlen(Cmd) == 0) {
261 usage_encode();
262 return 0;
263 }
264 hidproxmessage_t packed;
265 memset(&packed, 0, sizeof(hidproxmessage_t));
266 if (Encode(Cmd, &packed)){
267 PrintProxTagId(&packed);
268 Write(&packed);
269 }
270 return 0;
271 }
272
273 int CmdHIDFormats(){
274 HIDListFormats();
275 return 0;
276 }
277 static int CmdHelp(const char *Cmd); // define this now so the below won't error out.
278 static command_t CommandTable[] =
279 {
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}
290 };
291
292 int CmdLFHID(const char *Cmd)
293 {
294 CmdsParse(CommandTable, Cmd);
295 return 0;
296 }
297
298 int CmdHelp(const char *Cmd)
299 {
300 CmdsHelp(CommandTable);
301 return 0;
302 }
Impressum, Datenschutz