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