]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Authored by Craig Young <cyoung@tripwire.com> based on cmdlfhid.c structure | |
3 | // | |
4 | // cmdlfhid.c is Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
5 | // | |
6 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
7 | // at your option, any later version. See the LICENSE.txt file for the text of | |
8 | // the license. | |
9 | //----------------------------------------------------------------------------- | |
10 | // Low frequency AWID26 commands | |
11 | //----------------------------------------------------------------------------- | |
12 | ||
13 | #include <stdio.h> // sscanf | |
14 | #include "proxmark3.h" // Definitions, USB controls, etc | |
15 | #include "ui.h" // PrintAndLog | |
16 | #include "cmdparser.h" // CmdsParse, CmdsHelp | |
17 | #include "cmdlfawid.h" // AWID function declarations | |
18 | #include "lfdemod.h" // parityTest | |
19 | #include "util.h" // weigandparity | |
20 | #include "protocols.h" // for T55xx config register definitions | |
21 | #include "cmdmain.h" | |
22 | #include "sleep.h" | |
23 | ||
24 | static int CmdHelp(const char *Cmd); | |
25 | ||
26 | int usage_lf_awid_fskdemod(void) { | |
27 | PrintAndLog("Enables AWID compatible reader mode printing details of scanned AWID26 or AWID50 tags."); | |
28 | PrintAndLog("By default, values are printed and logged until the button is pressed or another USB command is issued."); | |
29 | PrintAndLog("If the [1] option is provided, reader mode is exited after reading a single AWID card."); | |
30 | PrintAndLog(""); | |
31 | PrintAndLog("Usage: lf awid fskdemod [h] [1]"); | |
32 | PrintAndLog("Options:"); | |
33 | PrintAndLog(" h : This help"); | |
34 | PrintAndLog(" 1 : (optional) stop after reading a single card"); | |
35 | PrintAndLog(""); | |
36 | PrintAndLog("Samples:"); | |
37 | PrintAndLog(" lf awid fskdemod"); | |
38 | PrintAndLog(" lf awid fskdemod 1"); | |
39 | return 0; | |
40 | } | |
41 | ||
42 | int usage_lf_awid_sim(void) { | |
43 | PrintAndLog("Enables simulation of AWID card with specified facility-code and card number."); | |
44 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); | |
45 | PrintAndLog(""); | |
46 | PrintAndLog("Usage: lf awid sim [h] <format> <facility-code> <card-number>"); | |
47 | PrintAndLog("Options:"); | |
48 | PrintAndLog(" h : This help"); | |
49 | PrintAndLog(" <format> : format length 26|50"); | |
50 | PrintAndLog(" <facility-code> : 8|16bit value facility code"); | |
51 | PrintAndLog(" <card number> : 16|32-bit value card number"); | |
52 | PrintAndLog(""); | |
53 | PrintAndLog("Samples:"); | |
54 | PrintAndLog(" lf awid sim 26 224 1337"); | |
55 | PrintAndLog(" lf awid sim 50 2001 13371337"); | |
56 | return 0; | |
57 | } | |
58 | ||
59 | int usage_lf_awid_clone(void) { | |
60 | PrintAndLog("Enables cloning of AWID card with specified facility-code and card number onto T55x7."); | |
61 | PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process."); | |
62 | PrintAndLog(""); | |
63 | PrintAndLog("Usage: lf awid clone [h] <format> <facility-code> <card-number> [Q5]"); | |
64 | PrintAndLog("Options:"); | |
65 | PrintAndLog(" h : This help"); | |
66 | PrintAndLog(" <format> : format length 26|50"); | |
67 | PrintAndLog(" <facility-code> : 8|16bit value facility code"); | |
68 | PrintAndLog(" <card number> : 16|32-bit value card number"); | |
69 | PrintAndLog(" Q5 : optional - clone to Q5 (T5555) instead of T55x7 chip"); | |
70 | PrintAndLog(""); | |
71 | PrintAndLog("Samples:"); | |
72 | PrintAndLog(" lf awid clone 26 224 1337"); | |
73 | PrintAndLog(" lf awid clone 50 2001 13371337"); | |
74 | return 0; | |
75 | } | |
76 | ||
77 | int usage_lf_awid_brute(void){ | |
78 | PrintAndLog("Enables bruteforce of AWID reader with specified facility-code."); | |
79 | PrintAndLog("This is a attack against reader. if cardnumber is given, it starts with it and goes up / down one step"); | |
80 | PrintAndLog("if cardnumber is not given, it starts with 1 and goes up to 65535"); | |
81 | PrintAndLog(""); | |
82 | PrintAndLog("Usage: lf awid brute [h] a <format> f <facility-code> c <cardnumber> d <delay>"); | |
83 | PrintAndLog("Options:"); | |
84 | PrintAndLog(" h : This help"); | |
85 | PrintAndLog(" a <format> : format length 26|50"); | |
86 | PrintAndLog(" f <facility-code> : 8|16bit value facility code"); | |
87 | PrintAndLog(" c <cardnumber> : (optional) cardnumber to start with, max 65535"); | |
88 | PrintAndLog(" d <delay> : delay betweens attempts in ms. Default 1000ms"); | |
89 | PrintAndLog(""); | |
90 | PrintAndLog("Samples:"); | |
91 | PrintAndLog(" lf awid brute a 26 f 224"); | |
92 | PrintAndLog(" lf awid brute a 50 f 2001 d 2000"); | |
93 | PrintAndLog(" lf awid brute a 50 f 2001 c 200 d 2000"); | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static boolean sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bs, size_t bs_len){ | |
98 | ||
99 | PrintAndLog("Trying FC: %u; CN: %u", fc, cn); | |
100 | if ( !getAWIDBits(fmtlen, fc, cn, bs)) { | |
101 | PrintAndLog("Error with tag bitstream generation."); | |
102 | return FALSE; | |
103 | } | |
104 | ||
105 | uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8 | |
106 | uint64_t arg2 = 50; // clk RF/50 invert=0 | |
107 | UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, bs_len}}; | |
108 | memcpy(c.d.asBytes, bs, bs_len); | |
109 | clearCommandBuffer(); | |
110 | SendCommand(&c); | |
111 | msleep(delay); | |
112 | return TRUE; | |
113 | } | |
114 | static int sendPing(){ | |
115 | UsbCommand resp; | |
116 | UsbCommand ping = {CMD_PING}; | |
117 | clearCommandBuffer(); | |
118 | SendCommand(&ping); | |
119 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { | |
120 | PrintAndLog("aborted via keyboard!"); | |
121 | return 0; | |
122 | } | |
123 | PrintAndLog("Device didnt respond to ABORT"); | |
124 | return 1; | |
125 | } | |
126 | ||
127 | int CmdAWIDDemodFSK(const char *Cmd) { | |
128 | int findone = 0; | |
129 | if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod(); | |
130 | if (Cmd[0] == '1') findone = 1; | |
131 | ||
132 | UsbCommand c = {CMD_AWID_DEMOD_FSK, {findone, 0, 0}}; | |
133 | clearCommandBuffer(); | |
134 | SendCommand(&c); | |
135 | return 0; | |
136 | } | |
137 | ||
138 | //refactored by marshmellow | |
139 | int getAWIDBits(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint8_t *bits) { | |
140 | ||
141 | // the return bits, preamble 0000 0001 | |
142 | bits[7] = 1; | |
143 | ||
144 | uint8_t pre[66]; | |
145 | memset(pre, 0, sizeof(pre)); | |
146 | ||
147 | // add formatlength | |
148 | num_to_bytebits(fmtlen, 8, pre); | |
149 | ||
150 | // add facilitycode, cardnumber and wiegand parity bits | |
151 | if ( fmtlen == 26 ) { | |
152 | uint8_t wiegand[24]; | |
153 | num_to_bytebits(fc, 8, wiegand); | |
154 | num_to_bytebits(cn, 16, wiegand+8); | |
155 | wiegand_add_parity(pre+8, wiegand, sizeof(wiegand)); | |
156 | } else { | |
157 | uint8_t wiegand[48]; | |
158 | num_to_bytebits(fc, 16, wiegand); | |
159 | num_to_bytebits(cn, 32, wiegand+16); | |
160 | wiegand_add_parity(pre+8, wiegand, sizeof(wiegand)); | |
161 | } | |
162 | ||
163 | // add AWID 4bit parity | |
164 | size_t bitLen = addParity(pre, bits+8, 66, 4, 1); | |
165 | ||
166 | if (bitLen != 88) return 0; | |
167 | return 1; | |
168 | } | |
169 | ||
170 | int CmdAWIDSim(const char *Cmd) { | |
171 | uint32_t fc = 0, cn = 0; | |
172 | uint8_t fmtlen = 0; | |
173 | uint8_t bits[96]; | |
174 | uint8_t *bs = bits; | |
175 | size_t size = sizeof(bits); | |
176 | memset(bs, 0x00, size); | |
177 | ||
178 | uint64_t arg1 = ( 10 << 8 ) + 8; // fcHigh = 10, fcLow = 8 | |
179 | uint64_t arg2 = 50; // clk RF/50 invert=0 | |
180 | ||
181 | char cmdp = param_getchar(Cmd, 0); | |
182 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_sim(); | |
183 | ||
184 | fmtlen = param_get8(Cmd, 0); | |
185 | fc = param_get32ex(Cmd, 1, 0, 10); | |
186 | cn = param_get32ex(Cmd, 2, 0, 10); | |
187 | if ( !fc || !cn) return usage_lf_awid_sim(); | |
188 | ||
189 | switch(fmtlen) { | |
190 | case 26: | |
191 | if ((fc & 0xFF) != fc) { | |
192 | fc &= 0xFF; | |
193 | PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc); | |
194 | } | |
195 | ||
196 | if ((cn & 0xFFFF) != cn) { | |
197 | cn &= 0xFFFF; | |
198 | PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn); | |
199 | } | |
200 | break; | |
201 | case 50: | |
202 | if ((fc & 0xFFFF) != fc) { | |
203 | fc &= 0xFFFF; | |
204 | PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc); | |
205 | } | |
206 | break; | |
207 | default: break; | |
208 | } | |
209 | ||
210 | PrintAndLog("Emulating AWID %u -- FC: %u; CN: %u\n", fmtlen, fc, cn); | |
211 | PrintAndLog("Press pm3-button to abort simulation or run another command"); | |
212 | ||
213 | if (!getAWIDBits(fmtlen, fc, cn, bs)) { | |
214 | PrintAndLog("Error with tag bitstream generation."); | |
215 | return 1; | |
216 | } | |
217 | // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0 | |
218 | // arg1 --- fcHigh<<8 + fcLow | |
219 | // arg2 --- Inversion and clk setting | |
220 | // 96 --- Bitstream length: 96-bits == 12 bytes | |
221 | UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}}; | |
222 | memcpy(c.d.asBytes, bs, size); | |
223 | clearCommandBuffer(); | |
224 | SendCommand(&c); | |
225 | return 0; | |
226 | } | |
227 | ||
228 | int CmdAWIDClone(const char *Cmd) { | |
229 | uint32_t blocks[4] = {T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 3<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0}; | |
230 | uint32_t fc = 0, cn = 0; | |
231 | uint8_t fmtlen = 0; | |
232 | uint8_t bits[96]; | |
233 | uint8_t *bs=bits; | |
234 | memset(bs,0,sizeof(bits)); | |
235 | ||
236 | char cmdp = param_getchar(Cmd, 0); | |
237 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_clone(); | |
238 | ||
239 | fmtlen = param_get8(Cmd, 0); | |
240 | fc = param_get32ex(Cmd, 1, 0, 10); | |
241 | cn = param_get32ex(Cmd, 2, 0, 10); | |
242 | ||
243 | if ( !fc || !cn) return usage_lf_awid_clone(); | |
244 | ||
245 | switch(fmtlen) { | |
246 | case 50: | |
247 | if ((fc & 0xFFFF) != fc) { | |
248 | fc &= 0xFFFF; | |
249 | PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc); | |
250 | } | |
251 | break; | |
252 | default: | |
253 | fmtlen = 26; | |
254 | if ((fc & 0xFF) != fc) { | |
255 | fc &= 0xFF; | |
256 | PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc); | |
257 | } | |
258 | ||
259 | if ((cn & 0xFFFF) != cn) { | |
260 | cn &= 0xFFFF; | |
261 | PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn); | |
262 | } | |
263 | break; | |
264 | } | |
265 | ||
266 | if (param_getchar(Cmd, 4) == 'Q' || param_getchar(Cmd, 4) == 'q') | |
267 | //t5555 (Q5) BITRATE = (RF-2)/2 (iceman) | |
268 | blocks[0] = T5555_MODULATION_FSK2 | T5555_INVERT_OUTPUT | 50<<T5555_BITRATE_SHIFT | 3<<T5555_MAXBLOCK_SHIFT; | |
269 | ||
270 | if ( !getAWIDBits(fmtlen, fc, cn, bs)) { | |
271 | PrintAndLog("Error with tag bitstream generation."); | |
272 | return 1; | |
273 | } | |
274 | ||
275 | blocks[1] = bytebits_to_byte(bs,32); | |
276 | blocks[2] = bytebits_to_byte(bs+32,32); | |
277 | blocks[3] = bytebits_to_byte(bs+64,32); | |
278 | ||
279 | PrintAndLog("Preparing to clone AWID %u to T55x7 with FC: %u, CN: %u", fmtlen, fc, cn); | |
280 | PrintAndLog("Blk | Data "); | |
281 | PrintAndLog("----+------------"); | |
282 | PrintAndLog(" 00 | 0x%08x", blocks[0]); | |
283 | PrintAndLog(" 01 | 0x%08x", blocks[1]); | |
284 | PrintAndLog(" 02 | 0x%08x", blocks[2]); | |
285 | PrintAndLog(" 03 | 0x%08x", blocks[3]); | |
286 | ||
287 | UsbCommand resp; | |
288 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
289 | ||
290 | for (uint8_t i=0; i<4; i++) { | |
291 | c.arg[0] = blocks[i]; | |
292 | c.arg[1] = i; | |
293 | clearCommandBuffer(); | |
294 | SendCommand(&c); | |
295 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)){ | |
296 | PrintAndLog("Error occurred, device did not respond during write operation."); | |
297 | return -1; | |
298 | } | |
299 | } | |
300 | return 0; | |
301 | } | |
302 | ||
303 | int CmdAWIDBrute(const char *Cmd){ | |
304 | ||
305 | bool errors = false; | |
306 | uint32_t fc = 0, cn = 0, delay = 1000; | |
307 | uint8_t fmtlen = 0; | |
308 | uint8_t bits[96]; | |
309 | uint8_t *bs = bits; | |
310 | size_t size = sizeof(bits); | |
311 | memset(bs, 0x00, size); | |
312 | uint8_t cmdp = 0; | |
313 | ||
314 | while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { | |
315 | switch(param_getchar(Cmd, cmdp)) { | |
316 | case 'h': | |
317 | case 'H': | |
318 | return usage_lf_awid_brute(); | |
319 | case 'f': | |
320 | case 'F': | |
321 | fc = param_get32ex(Cmd ,cmdp+1, 0, 10); | |
322 | if ( !fc ) | |
323 | errors = true; | |
324 | cmdp += 2; | |
325 | break; | |
326 | case 'd': | |
327 | case 'D': | |
328 | // delay between attemps, defaults to 1000ms. | |
329 | delay = param_get32ex(Cmd, cmdp+1, 1000, 10); | |
330 | cmdp += 2; | |
331 | break; | |
332 | case 'c': | |
333 | case 'C': | |
334 | cn = param_get32ex(Cmd, cmdp+1, 0, 10); | |
335 | // truncate cardnumber. | |
336 | cn &= 0xFFFF; | |
337 | cmdp += 2; | |
338 | break; | |
339 | case 'a': | |
340 | case 'A': | |
341 | fmtlen = param_get8(Cmd, cmdp+1); | |
342 | cmdp += 2; | |
343 | break; | |
344 | default: | |
345 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
346 | errors = true; | |
347 | break; | |
348 | } | |
349 | } | |
350 | if ( fc == 0 )errors = true; | |
351 | if ( errors ) return usage_lf_awid_brute(); | |
352 | ||
353 | // limit fc according to selected format | |
354 | switch(fmtlen) { | |
355 | case 50: | |
356 | if ((fc & 0xFFFF) != fc) { | |
357 | fc &= 0xFFFF; | |
358 | PrintAndLog("Facility-code truncated to 16-bits (AWID50): %u", fc); | |
359 | } | |
360 | break; | |
361 | default: | |
362 | if ((fc & 0xFF) != fc) { | |
363 | fc &= 0xFF; | |
364 | PrintAndLog("Facility-code truncated to 8-bits (AWID26): %u", fc); | |
365 | } | |
366 | break; | |
367 | } | |
368 | ||
369 | // start | |
370 | ||
371 | PrintAndLog("Bruteforceing AWID %d Reader", fmtlen); | |
372 | PrintAndLog("Press pm3-button to abort simulation or press key"); | |
373 | ||
374 | uint16_t up = cn; | |
375 | uint16_t down = cn; | |
376 | ||
377 | for (;;){ | |
378 | ||
379 | if (ukbhit()) return sendPing(); | |
380 | ||
381 | // Do one up | |
382 | if ( up < 0xFFFF ) | |
383 | if ( !sendTry(fmtlen, fc, up++, delay, bs, size)) return 1; | |
384 | ||
385 | // Do one down (if cardnumber is given) | |
386 | if ( cn > 1 ) | |
387 | if ( down > 0 ) | |
388 | if ( !sendTry(fmtlen, fc, --down, delay, bs, size)) return 1; | |
389 | } | |
390 | return 0; | |
391 | } | |
392 | ||
393 | static command_t CommandTable[] = { | |
394 | {"help", CmdHelp, 1, "This help"}, | |
395 | {"fskdemod", CmdAWIDDemodFSK, 0, "Realtime AWID FSK demodulator"}, | |
396 | {"sim", CmdAWIDSim, 0, "AWID tag simulator"}, | |
397 | {"clone", CmdAWIDClone, 0, "Clone AWID to T55x7"}, | |
398 | {"brute", CmdAWIDBrute, 0, "Bruteforce card number against reader"}, | |
399 | {NULL, NULL, 0, NULL} | |
400 | }; | |
401 | ||
402 | int CmdLFAWID(const char *Cmd) { | |
403 | clearCommandBuffer(); | |
404 | CmdsParse(CommandTable, Cmd); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | int CmdHelp(const char *Cmd) { | |
409 | CmdsHelp(CommandTable); | |
410 | return 0; | |
411 | } |