]>
Commit | Line | Data |
---|---|---|
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 | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <string.h> | |
13 | #include "proxmark3.h" | |
14 | #include "ui.h" | |
15 | #include "graph.h" | |
16 | #include "cmdparser.h" | |
17 | #include "cmdlfhid.h" | |
18 | #include "util.h" | |
19 | #include "cmdmain.h" | |
20 | ||
21 | static int CmdHelp(const char *Cmd); | |
22 | ||
23 | int usage_hid_wiegand(){ | |
24 | PrintAndLog("Usage: lf hid wiegand [h] [formatlenght] [oem] [FacilityCode] [cardnumber]"); | |
25 | PrintAndLog("This command converts FC/Cardnum to wiegand code"); | |
26 | PrintAndLog("Options:"); | |
27 | PrintAndLog(" h - This help"); | |
28 | PrintAndLog(" formatlen - Format length, 26|34|35|37|44|84"); | |
29 | PrintAndLog(" oem - Oem number"); | |
30 | PrintAndLog(" facilitynum - Facility number"); | |
31 | PrintAndLog(" cardnum - Card number"); | |
32 | PrintAndLog("Examples:"); | |
33 | PrintAndLog(" lf hid wiegand 26 0 101 2001"); | |
34 | return 0; | |
35 | } | |
36 | ||
37 | /* | |
38 | int CmdHIDDemod(const char *Cmd) | |
39 | { | |
40 | if (GraphTraceLen < 4800) { | |
41 | PrintAndLog("too short; need at least 4800 samples"); | |
42 | return 0; | |
43 | } | |
44 | ||
45 | GraphTraceLen = 4800; | |
46 | for (int i = 0; i < GraphTraceLen; ++i) { | |
47 | if (GraphBuffer[i] < 0) { | |
48 | GraphBuffer[i] = 0; | |
49 | } else { | |
50 | GraphBuffer[i] = 1; | |
51 | } | |
52 | } | |
53 | RepaintGraphWindow(); | |
54 | return 0; | |
55 | } | |
56 | */ | |
57 | int CmdHIDDemodFSK(const char *Cmd) | |
58 | { | |
59 | int findone = ( Cmd[0] == '1' ) ? 1 : 0; | |
60 | UsbCommand c = {CMD_HID_DEMOD_FSK, {findone, 0 , 0}}; | |
61 | clearCommandBuffer(); | |
62 | SendCommand(&c); | |
63 | return 0; | |
64 | } | |
65 | ||
66 | int CmdHIDSim(const char *Cmd) | |
67 | { | |
68 | unsigned int hi = 0, lo = 0; | |
69 | int n = 0, i = 0; | |
70 | ||
71 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
72 | hi = (hi << 4) | (lo >> 28); | |
73 | lo = (lo << 4) | (n & 0xf); | |
74 | } | |
75 | ||
76 | PrintAndLog("Emulating tag with ID %x%16x", hi, lo); | |
77 | PrintAndLog("Press pm3-button to abort simulation"); | |
78 | ||
79 | UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}}; | |
80 | clearCommandBuffer(); | |
81 | SendCommand(&c); | |
82 | return 0; | |
83 | } | |
84 | ||
85 | int CmdHIDClone(const char *Cmd) | |
86 | { | |
87 | unsigned int hi2 = 0, hi = 0, lo = 0; | |
88 | int n = 0, i = 0; | |
89 | UsbCommand c; | |
90 | ||
91 | if (strchr(Cmd,'l') != 0) { | |
92 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
93 | hi2 = (hi2 << 4) | (hi >> 28); | |
94 | hi = (hi << 4) | (lo >> 28); | |
95 | lo = (lo << 4) | (n & 0xf); | |
96 | } | |
97 | ||
98 | PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo); | |
99 | ||
100 | c.d.asBytes[0] = 1; | |
101 | } else { | |
102 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
103 | hi = (hi << 4) | (lo >> 28); | |
104 | lo = (lo << 4) | (n & 0xf); | |
105 | } | |
106 | ||
107 | PrintAndLog("Cloning tag with ID %x%08x", hi, lo); | |
108 | ||
109 | hi2 = 0; | |
110 | c.d.asBytes[0] = 0; | |
111 | } | |
112 | ||
113 | c.cmd = CMD_HID_CLONE_TAG; | |
114 | c.arg[0] = hi2; | |
115 | c.arg[1] = hi; | |
116 | c.arg[2] = lo; | |
117 | ||
118 | clearCommandBuffer(); | |
119 | SendCommand(&c); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | static void getParity26(uint32_t *hi, uint32_t *lo){ | |
124 | uint32_t result = 0; | |
125 | int i; | |
126 | // even parity | |
127 | for (i = 24;i >= 13;i--) | |
128 | result ^= (*lo >> i) & 1; | |
129 | // even parity 26th bit | |
130 | *lo |= result << 25; | |
131 | ||
132 | // odd parity | |
133 | result = 0; | |
134 | for (i = 12;i >= 1;i--) | |
135 | result ^= (*lo >> i) & 1; | |
136 | *lo |= !result; | |
137 | } | |
138 | static void getParity34(uint32_t *hi, uint32_t *lo){ | |
139 | uint32_t result = 0; | |
140 | int i; | |
141 | ||
142 | // even parity | |
143 | for (i = 7;i >= 0;i--) | |
144 | result ^= (*hi >> i) & i; | |
145 | for (i = 31;i >= 24;i--) | |
146 | result ^= (*lo >> i) & 1; | |
147 | ||
148 | *hi |= result << 2; | |
149 | ||
150 | // odd parity bit | |
151 | result = 0; | |
152 | for (i = 23;i >= 1;i--) | |
153 | result ^= (*lo >> i) & 1; | |
154 | ||
155 | *lo |= !result; | |
156 | } | |
157 | static void getParity35(uint32_t *hi, uint32_t *lo){ | |
158 | *hi = *hi; | |
159 | } | |
160 | static void getParity37S(uint32_t *hi,uint32_t *lo){ | |
161 | uint32_t result = 0; | |
162 | uint8_t i; | |
163 | ||
164 | // even parity | |
165 | for (i = 4; i >= 0; i--) | |
166 | result ^= (*hi >> i) & 1; | |
167 | ||
168 | for (i = 31; i >= 20; i--) | |
169 | result ^= (*lo >> i) & 1; | |
170 | ||
171 | *hi |= result; | |
172 | ||
173 | // odd parity | |
174 | result = 0; | |
175 | for (i = 19; i >= 1; i--) | |
176 | result ^= (*lo >> i) & 1; | |
177 | ||
178 | *lo |= result; | |
179 | } | |
180 | static void getParity37H(uint32_t *hi, uint32_t *lo){ | |
181 | uint32_t result = 0; | |
182 | int i; | |
183 | ||
184 | // even parity | |
185 | for (i = 4;i >= 0;i--) | |
186 | result ^= (*hi >> i) & 1; | |
187 | for (i = 31;i >= 20;i--) | |
188 | result ^= (*lo >> i) & 1; | |
189 | *hi |= result << 4; | |
190 | ||
191 | // odd parity | |
192 | result = 0; | |
193 | for (i = 19;i >= 1;i--) | |
194 | result ^= (*lo >> i) & 1; | |
195 | *lo |= result; | |
196 | } | |
197 | ||
198 | static void calc26(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ | |
199 | *lo = ((cardno & 0xFFFF) << 1) | ((fc & 0xFF) << 17) | (1 << 26); | |
200 | *hi = (1 << 5); | |
201 | } | |
202 | static void calc34(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ | |
203 | // put card number first bit 1 .. 20 // | |
204 | *lo = ((cardno & 0X000F7FFF) << 1) | ((fc & 0XFFFF) << 17); | |
205 | // set bit format for less than 37 bit format | |
206 | *hi = (1 << 5) | (fc >> 15); | |
207 | } | |
208 | static void calc35(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ | |
209 | *lo = ((cardno & 0xFFFFF) << 1) | fc << 21; | |
210 | *hi = (1 << 5) | ((fc >> 11) & 1); | |
211 | } | |
212 | static void calc37S(uint16_t sc, uint32_t cn, uint32_t *hi, uint32_t *lo){ | |
213 | // SC 2 - 17 - 16 bit | |
214 | // CN 18 - 36 - 19 bit | |
215 | // Even P1 1 - 19 | |
216 | // Odd P37 19 - 36 | |
217 | ||
218 | sc = sc & 0xFFFF; | |
219 | *lo = ( (sc << 20) | (cn & 0x7FFFF) << 1); | |
220 | *hi = (sc >> 12); | |
221 | } | |
222 | static void calc37H(uint64_t cn, uint32_t *hi, uint32_t *lo){ | |
223 | // SC NONE | |
224 | // CN 1-35 34 bits | |
225 | // Even Parity 0th bit 1-18 | |
226 | // Odd Parity 36th bit 19-35 | |
227 | cn = (cn & 0x00000003FFFFFFFF); | |
228 | *lo = (cn << 1); | |
229 | *hi = (cn >> 31); | |
230 | } | |
231 | static void calc40(uint64_t cn, uint32_t *hi, uint32_t *lo){ | |
232 | cn = (cn & 0xFFFFFFFFFF); | |
233 | *lo = ((cn & 0xFFFFFFFF) << 1 ); | |
234 | *hi = (cn >> 31); | |
235 | } | |
236 | ||
237 | int CmdHIDWiegand(const char *Cmd) | |
238 | { | |
239 | uint32_t oem; | |
240 | uint32_t fmtlen = 0; | |
241 | uint32_t fc, lo = 0, hi = 0; | |
242 | uint64_t cn = 0; | |
243 | uint32_t cardnum = 0; | |
244 | ||
245 | //uint32_t temp, p | |
246 | uint8_t ctmp = param_getchar(Cmd, 0); | |
247 | if ( strlen(Cmd) < 0 || strlen(Cmd) < 4 || ctmp == 'H' || ctmp == 'h' ) return usage_hid_wiegand(); | |
248 | ||
249 | fmtlen = param_get8(Cmd, 0); | |
250 | oem = param_get8(Cmd, 1); | |
251 | fc = param_get32ex(Cmd, 2, 0, 10); | |
252 | cn = param_get64ex(Cmd, 3, 0, 10); | |
253 | ||
254 | switch ( fmtlen ) { | |
255 | case 26 : { | |
256 | cardnum = (cn & 0xFFFFFFFF); | |
257 | calc26(fc, cardnum, &hi, &lo); | |
258 | getParity26(&hi, &lo); | |
259 | break; | |
260 | } | |
261 | case 34 : { | |
262 | cardnum = (cn & 0xFFFFFFFF); | |
263 | calc34(fc, cardnum, &hi, &lo); | |
264 | getParity34(&hi, &lo); | |
265 | break; | |
266 | } | |
267 | case 35 : { | |
268 | cardnum = (cn & 0xFFFFFFFF); | |
269 | calc35(fc, cardnum, &hi, &lo); | |
270 | getParity35(&hi, &lo); | |
271 | break; | |
272 | } | |
273 | case 37 : { | |
274 | cardnum = (cn & 0xFFFFFFFF); | |
275 | calc37S(fc, cardnum, &hi, &lo); | |
276 | getParity37S(&hi, &lo); | |
277 | break; | |
278 | } | |
279 | case 38 : { | |
280 | cardnum = (cn & 0xFFFFFFFF); | |
281 | calc37H(cardnum, &hi, &lo); | |
282 | getParity37H(&hi, &lo); | |
283 | break; | |
284 | } | |
285 | case 40 : { | |
286 | calc40(cn, &hi, &lo); | |
287 | break; | |
288 | } | |
289 | case 44 : { break; } | |
290 | case 84 : { break; } | |
291 | } | |
292 | PrintAndLog("HID %d bit | FC: %d CN: %lld | Wiegand Code: %08X%08X", fmtlen, fc, cn, hi, lo); | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static command_t CommandTable[] = { | |
297 | {"help", CmdHelp, 1, "This help"}, | |
298 | //{"demod", CmdHIDDemod, 1, "Demodulate HID Prox Card II (not optimal)"}, | |
299 | {"fskdemod", CmdHIDDemodFSK, 0, "['1'] Realtime HID FSK demodulator (option '1' for one tag only)"}, | |
300 | {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"}, | |
301 | {"clone", CmdHIDClone, 0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"}, | |
302 | {"wiegand", CmdHIDWiegand, 1, "<oem> <fmtlen> <fc> <cardnum> -- convert facilitycode, cardnumber to Wiegand code"}, | |
303 | {NULL, NULL, 0, NULL} | |
304 | }; | |
305 | ||
306 | int CmdLFHID(const char *Cmd) { | |
307 | CmdsParse(CommandTable, Cmd); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | int CmdHelp(const char *Cmd) { | |
312 | CmdsHelp(CommandTable); | |
313 | return 0; | |
314 | } |