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