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