]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfawid.c
FIX: fixes the broken build,...
[proxmark3-svn] / client / cmdlfawid.c
CommitLineData
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 14static int CmdHelp(const char *Cmd);
15
db25599d 16int 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
32int 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
49int 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 67int 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
f121b478 87static int sendPing(void){
88 UsbCommand ping = {CMD_PING, {1, 2, 3}};
89 SendCommand(&ping);
90 SendCommand(&ping);
91 SendCommand(&ping);
92
93 clearCommandBuffer();
94 UsbCommand resp;
95 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
96 return 0;
97 }
98 return 1;
99}
100
05442fa6 101static bool sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bs, size_t bs_len){
10c4231e 102
103 PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
104 if ( !getAWIDBits(fmtlen, fc, cn, bs)) {
105 PrintAndLog("Error with tag bitstream generation.");
106 return FALSE;
107 }
108
109 uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
110 uint64_t arg2 = 50; // clk RF/50 invert=0
111 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, bs_len}};
112 memcpy(c.d.asBytes, bs, bs_len);
113 clearCommandBuffer();
114 SendCommand(&c);
f121b478 115
10c4231e 116 msleep(delay);
f121b478 117 sendPing();
10c4231e 118 return TRUE;
119}
f121b478 120
10c4231e 121
52f2df61 122int CmdAWIDDemodFSK(const char *Cmd) {
a126332a 123 int findone = 0;
124 if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
125 if (Cmd[0] == '1') findone = 1;
126
127 UsbCommand c = {CMD_AWID_DEMOD_FSK, {findone, 0, 0}};
128 clearCommandBuffer();
129 SendCommand(&c);
130 return 0;
db25599d 131}
132
a126332a 133//refactored by marshmellow
8fd9bfb0 134int getAWIDBits(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint8_t *bits) {
135
136 // the return bits, preamble 0000 0001
137 bits[7] = 1;
138
a126332a 139 uint8_t pre[66];
140 memset(pre, 0, sizeof(pre));
52f2df61 141
8fd9bfb0 142 // add formatlength
143 num_to_bytebits(fmtlen, 8, pre);
144
145 // add facilitycode, cardnumber and wiegand parity bits
146 if ( fmtlen == 26 ) {
147 uint8_t wiegand[24];
148 num_to_bytebits(fc, 8, wiegand);
149 num_to_bytebits(cn, 16, wiegand+8);
150 wiegand_add_parity(pre+8, wiegand, sizeof(wiegand));
151 } else {
152 uint8_t wiegand[48];
153 num_to_bytebits(fc, 16, wiegand);
154 num_to_bytebits(cn, 32, wiegand+16);
155 wiegand_add_parity(pre+8, wiegand, sizeof(wiegand));
156 }
157
158 // add AWID 4bit parity
159 size_t bitLen = addParity(pre, bits+8, 66, 4, 1);
fcb1cdba 160
a126332a 161 if (bitLen != 88) return 0;
52f2df61 162 return 1;
db25599d 163}
164
a126332a 165int CmdAWIDSim(const char *Cmd) {
8fd9bfb0 166 uint32_t fc = 0, cn = 0;
167 uint8_t fmtlen = 0;
a126332a 168 uint8_t bits[96];
169 uint8_t *bs = bits;
170 size_t size = sizeof(bits);
fcb1cdba 171 memset(bs, 0x00, size);
52f2df61 172
fcb1cdba 173 uint64_t arg1 = ( 10 << 8 ) + 8; // fcHigh = 10, fcLow = 8
52f2df61 174 uint64_t arg2 = 50; // clk RF/50 invert=0
175
8fd9bfb0 176 char cmdp = param_getchar(Cmd, 0);
177 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_sim();
52f2df61 178
8fd9bfb0 179 fmtlen = param_get8(Cmd, 0);
180 fc = param_get32ex(Cmd, 1, 0, 10);
181 cn = param_get32ex(Cmd, 2, 0, 10);
182 if ( !fc || !cn) return usage_lf_awid_sim();
52f2df61 183
8fd9bfb0 184 switch(fmtlen) {
185 case 26:
186 if ((fc & 0xFF) != fc) {
187 fc &= 0xFF;
188 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
189 }
190
191 if ((cn & 0xFFFF) != cn) {
192 cn &= 0xFFFF;
193 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
194 }
195 break;
196 case 50:
197 if ((fc & 0xFFFF) != fc) {
198 fc &= 0xFFFF;
199 PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc);
200 }
201 break;
202 default: break;
203 }
204
205 PrintAndLog("Emulating AWID %u -- FC: %u; CN: %u\n", fmtlen, fc, cn);
52f2df61 206 PrintAndLog("Press pm3-button to abort simulation or run another command");
207
8fd9bfb0 208 if (!getAWIDBits(fmtlen, fc, cn, bs)) {
52f2df61 209 PrintAndLog("Error with tag bitstream generation.");
210 return 1;
211 }
212 // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
52f2df61 213 // arg1 --- fcHigh<<8 + fcLow
214 // arg2 --- Inversion and clk setting
215 // 96 --- Bitstream length: 96-bits == 12 bytes
a126332a 216 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
217 memcpy(c.d.asBytes, bs, size);
52f2df61 218 clearCommandBuffer();
219 SendCommand(&c);
220 return 0;
db25599d 221}
222
a126332a 223int CmdAWIDClone(const char *Cmd) {
224 uint32_t blocks[4] = {T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 3<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0};
8fd9bfb0 225 uint32_t fc = 0, cn = 0;
226 uint8_t fmtlen = 0;
a126332a 227 uint8_t bits[96];
52f2df61 228 uint8_t *bs=bits;
a126332a 229 memset(bs,0,sizeof(bits));
52f2df61 230
8fd9bfb0 231 char cmdp = param_getchar(Cmd, 0);
232 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_clone();
f3cfe428 233
8fd9bfb0 234 fmtlen = param_get8(Cmd, 0);
235 fc = param_get32ex(Cmd, 1, 0, 10);
236 cn = param_get32ex(Cmd, 2, 0, 10);
a126332a 237
8fd9bfb0 238 if ( !fc || !cn) return usage_lf_awid_clone();
239
240 switch(fmtlen) {
241 case 50:
242 if ((fc & 0xFFFF) != fc) {
243 fc &= 0xFFFF;
244 PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc);
245 }
246 break;
247 default:
248 fmtlen = 26;
249 if ((fc & 0xFF) != fc) {
250 fc &= 0xFF;
251 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
252 }
f3cfe428 253
8fd9bfb0 254 if ((cn & 0xFFFF) != cn) {
255 cn &= 0xFFFF;
256 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
257 }
258 break;
f3cfe428 259 }
52f2df61 260
8fd9bfb0 261 if (param_getchar(Cmd, 4) == 'Q' || param_getchar(Cmd, 4) == 'q')
890ae3dd 262 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
8fd9bfb0 263 blocks[0] = T5555_MODULATION_FSK2 | T5555_INVERT_OUTPUT | 50<<T5555_BITRATE_SHIFT | 3<<T5555_MAXBLOCK_SHIFT;
264
265 if ( !getAWIDBits(fmtlen, fc, cn, bs)) {
52f2df61 266 PrintAndLog("Error with tag bitstream generation.");
267 return 1;
268 }
269
a126332a 270 blocks[1] = bytebits_to_byte(bs,32);
271 blocks[2] = bytebits_to_byte(bs+32,32);
272 blocks[3] = bytebits_to_byte(bs+64,32);
52f2df61 273
8fd9bfb0 274 PrintAndLog("Preparing to clone AWID %u to T55x7 with FC: %u, CN: %u", fmtlen, fc, cn);
52f2df61 275 PrintAndLog("Blk | Data ");
276 PrintAndLog("----+------------");
277 PrintAndLog(" 00 | 0x%08x", blocks[0]);
278 PrintAndLog(" 01 | 0x%08x", blocks[1]);
279 PrintAndLog(" 02 | 0x%08x", blocks[2]);
280 PrintAndLog(" 03 | 0x%08x", blocks[3]);
281
282 UsbCommand resp;
283 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
f3cfe428 284
a126332a 285 for (uint8_t i=0; i<4; i++) {
52f2df61 286 c.arg[0] = blocks[i];
287 c.arg[1] = i;
288 clearCommandBuffer();
289 SendCommand(&c);
26922aef 290 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)){
52f2df61 291 PrintAndLog("Error occurred, device did not respond during write operation.");
292 return -1;
f3cfe428 293 }
294 }
295 return 0;
db25599d 296}
297
508b37ba 298int CmdAWIDBrute(const char *Cmd){
299
10c4231e 300 bool errors = false;
301 uint32_t fc = 0, cn = 0, delay = 1000;
8fd9bfb0 302 uint8_t fmtlen = 0;
508b37ba 303 uint8_t bits[96];
304 uint8_t *bs = bits;
305 size_t size = sizeof(bits);
306 memset(bs, 0x00, size);
10c4231e 307 uint8_t cmdp = 0;
934dfd72 308
10c4231e 309 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
310 switch(param_getchar(Cmd, cmdp)) {
311 case 'h':
312 case 'H':
313 return usage_lf_awid_brute();
314 case 'f':
315 case 'F':
316 fc = param_get32ex(Cmd ,cmdp+1, 0, 10);
317 if ( !fc )
318 errors = true;
319 cmdp += 2;
320 break;
321 case 'd':
322 case 'D':
323 // delay between attemps, defaults to 1000ms.
324 delay = param_get32ex(Cmd, cmdp+1, 1000, 10);
325 cmdp += 2;
326 break;
327 case 'c':
328 case 'C':
329 cn = param_get32ex(Cmd, cmdp+1, 0, 10);
330 // truncate cardnumber.
331 cn &= 0xFFFF;
332 cmdp += 2;
333 break;
334 case 'a':
335 case 'A':
336 fmtlen = param_get8(Cmd, cmdp+1);
337 cmdp += 2;
338 break;
339 default:
340 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
341 errors = true;
342 break;
343 }
344 }
345 if ( fc == 0 )errors = true;
346 if ( errors ) return usage_lf_awid_brute();
347
348 // limit fc according to selected format
8fd9bfb0 349 switch(fmtlen) {
350 case 50:
351 if ((fc & 0xFFFF) != fc) {
352 fc &= 0xFFFF;
10c4231e 353 PrintAndLog("Facility-code truncated to 16-bits (AWID50): %u", fc);
8fd9bfb0 354 }
355 break;
356 default:
357 if ((fc & 0xFF) != fc) {
358 fc &= 0xFF;
10c4231e 359 PrintAndLog("Facility-code truncated to 8-bits (AWID26): %u", fc);
8fd9bfb0 360 }
361 break;
362 }
508b37ba 363
10c4231e 364 // start
365
8fd9bfb0 366 PrintAndLog("Bruteforceing AWID %d Reader", fmtlen);
934dfd72 367 PrintAndLog("Press pm3-button to abort simulation or press key");
508b37ba 368
10c4231e 369 uint16_t up = cn;
370 uint16_t down = cn;
371
372 for (;;){
373
f121b478 374 if ( offline ) {
375 printf("Device offline\n");
376 return 2;
377 }
378
10c4231e 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;
508b37ba 384
10c4231e 385 // Do one down (if cardnumber is given)
386 if ( cn > 1 )
56f1aaa2 387 if ( down > 1 )
10c4231e 388 if ( !sendTry(fmtlen, fc, --down, delay, bs, size)) return 1;
508b37ba 389 }
390 return 0;
391}
392
a126332a 393static command_t CommandTable[] = {
f3cfe428 394 {"help", CmdHelp, 1, "This help"},
10c4231e 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"},
f3cfe428 399 {NULL, NULL, 0, NULL}
db25599d 400};
401
a126332a 402int CmdLFAWID(const char *Cmd) {
4c36581b 403 clearCommandBuffer();
f3cfe428 404 CmdsParse(CommandTable, Cmd);
405 return 0;
db25599d 406}
407
a126332a 408int CmdHelp(const char *Cmd) {
f3cfe428 409 CmdsHelp(CommandTable);
410 return 0;
db25599d 411}
Impressum, Datenschutz