]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfawid.c
ADD: added the possiblity to use AWID formatlength of 26 and 50.
[proxmark3-svn] / client / cmdlfawid.c
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 [1]");
32 PrintAndLog("Options :");
33 PrintAndLog(" 1 : (optional) stop after reading a single card");
34 PrintAndLog("");
35 PrintAndLog("Samples");
36 PrintAndLog(" lf awid fskdemod");
37 PrintAndLog(" lf awid fskdemod 1");
38 return 0;
39 }
40
41 int usage_lf_awid_sim(void) {
42 PrintAndLog("Enables simulation of AWID card with specified facility-code and card number.");
43 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
44 PrintAndLog("");
45 PrintAndLog("Usage: lf awid sim <format> <facility-code> <card-number>");
46 PrintAndLog("Options :");
47 PrintAndLog(" <format> : format length 26|50");
48 PrintAndLog(" <facility-code> : 8|16bit value facility code");
49 PrintAndLog(" <card number> : 16|32-bit value card number");
50 PrintAndLog("");
51 PrintAndLog("Samples");
52 PrintAndLog(" lf awid sim 26 224 1337");
53 PrintAndLog(" lf awid sim 50 2001 13371337");
54 return 0;
55 }
56
57 int usage_lf_awid_clone(void) {
58 PrintAndLog("Enables cloning of AWID card with specified facility-code and card number onto T55x7.");
59 PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process.");
60 PrintAndLog("");
61 PrintAndLog("Usage: lf awid clone <format> <facility-code> <card-number>");
62 PrintAndLog("Options :");
63 PrintAndLog(" <format> : format length 26|50");
64 PrintAndLog(" <facility-code> : 8|16bit value facility code");
65 PrintAndLog(" <card number> : 16|32-bit value card number");
66 PrintAndLog(" Q5 : optional - clone to Q5 (T5555) instead of T55x7 chip");
67 PrintAndLog("");
68 PrintAndLog("Samples");
69 PrintAndLog(" lf awid clone 26 224 1337");
70 PrintAndLog(" lf awid clone 50 2001 13371337");
71 return 0;
72 }
73
74 int usage_lf_awid_brute(void){
75 PrintAndLog("Enables bruteforce of AWID reader with specified facility-code.");
76 PrintAndLog("This is a incremental attack against reader.");
77 PrintAndLog("");
78 PrintAndLog("Usage: lf awid brute <format> <facility-code>");
79 PrintAndLog("Options :");
80 PrintAndLog(" <format> : format length 26|50");
81 PrintAndLog(" <facility-code> : 8|16bit value facility code");
82 PrintAndLog("");
83 PrintAndLog("Samples");
84 PrintAndLog(" lf awid brute 26 224");
85 PrintAndLog(" lf awid brute 50 2001");
86 return 0;
87 }
88
89 int CmdAWIDDemodFSK(const char *Cmd) {
90 int findone = 0;
91 if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
92 if (Cmd[0] == '1') findone = 1;
93
94 UsbCommand c = {CMD_AWID_DEMOD_FSK, {findone, 0, 0}};
95 clearCommandBuffer();
96 SendCommand(&c);
97 return 0;
98 }
99
100 //refactored by marshmellow
101 int getAWIDBits(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint8_t *bits) {
102
103 // the return bits, preamble 0000 0001
104 bits[7] = 1;
105
106 uint8_t pre[66];
107 memset(pre, 0, sizeof(pre));
108
109 // add formatlength
110 num_to_bytebits(fmtlen, 8, pre);
111
112 // add facilitycode, cardnumber and wiegand parity bits
113 if ( fmtlen == 26 ) {
114 uint8_t wiegand[24];
115 num_to_bytebits(fc, 8, wiegand);
116 num_to_bytebits(cn, 16, wiegand+8);
117 wiegand_add_parity(pre+8, wiegand, sizeof(wiegand));
118 } else {
119 uint8_t wiegand[48];
120 num_to_bytebits(fc, 16, wiegand);
121 num_to_bytebits(cn, 32, wiegand+16);
122 wiegand_add_parity(pre+8, wiegand, sizeof(wiegand));
123 }
124
125 // add AWID 4bit parity
126 size_t bitLen = addParity(pre, bits+8, 66, 4, 1);
127
128 if (bitLen != 88) return 0;
129 return 1;
130 }
131
132 int CmdAWIDSim(const char *Cmd) {
133 uint32_t fc = 0, cn = 0;
134 uint8_t fmtlen = 0;
135 uint8_t bits[96];
136 uint8_t *bs = bits;
137 size_t size = sizeof(bits);
138 memset(bs, 0x00, size);
139
140 uint64_t arg1 = ( 10 << 8 ) + 8; // fcHigh = 10, fcLow = 8
141 uint64_t arg2 = 50; // clk RF/50 invert=0
142
143 char cmdp = param_getchar(Cmd, 0);
144 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_sim();
145
146 fmtlen = param_get8(Cmd, 0);
147 fc = param_get32ex(Cmd, 1, 0, 10);
148 cn = param_get32ex(Cmd, 2, 0, 10);
149 if ( !fc || !cn) return usage_lf_awid_sim();
150
151 switch(fmtlen) {
152 case 26:
153 if ((fc & 0xFF) != fc) {
154 fc &= 0xFF;
155 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
156 }
157
158 if ((cn & 0xFFFF) != cn) {
159 cn &= 0xFFFF;
160 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
161 }
162 break;
163 case 50:
164 if ((fc & 0xFFFF) != fc) {
165 fc &= 0xFFFF;
166 PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc);
167 }
168 break;
169 default: break;
170 }
171
172 PrintAndLog("Emulating AWID %u -- FC: %u; CN: %u\n", fmtlen, fc, cn);
173 PrintAndLog("Press pm3-button to abort simulation or run another command");
174
175 if (!getAWIDBits(fmtlen, fc, cn, bs)) {
176 PrintAndLog("Error with tag bitstream generation.");
177 return 1;
178 }
179 // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
180 // arg1 --- fcHigh<<8 + fcLow
181 // arg2 --- Inversion and clk setting
182 // 96 --- Bitstream length: 96-bits == 12 bytes
183 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
184 memcpy(c.d.asBytes, bs, size);
185 clearCommandBuffer();
186 SendCommand(&c);
187 return 0;
188 }
189
190 int CmdAWIDClone(const char *Cmd) {
191 uint32_t blocks[4] = {T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 3<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0};
192 uint32_t fc = 0, cn = 0;
193 uint8_t fmtlen = 0;
194 uint8_t bits[96];
195 uint8_t *bs=bits;
196 memset(bs,0,sizeof(bits));
197
198 char cmdp = param_getchar(Cmd, 0);
199 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_clone();
200
201 fmtlen = param_get8(Cmd, 0);
202 fc = param_get32ex(Cmd, 1, 0, 10);
203 cn = param_get32ex(Cmd, 2, 0, 10);
204
205 if ( !fc || !cn) return usage_lf_awid_clone();
206
207 switch(fmtlen) {
208 case 50:
209 if ((fc & 0xFFFF) != fc) {
210 fc &= 0xFFFF;
211 PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc);
212 }
213 break;
214 default:
215 fmtlen = 26;
216 if ((fc & 0xFF) != fc) {
217 fc &= 0xFF;
218 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
219 }
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 if (param_getchar(Cmd, 4) == 'Q' || param_getchar(Cmd, 4) == 'q')
229 blocks[0] = T5555_MODULATION_FSK2 | T5555_INVERT_OUTPUT | 50<<T5555_BITRATE_SHIFT | 3<<T5555_MAXBLOCK_SHIFT;
230
231 if ( !getAWIDBits(fmtlen, fc, cn, bs)) {
232 PrintAndLog("Error with tag bitstream generation.");
233 return 1;
234 }
235
236 blocks[1] = bytebits_to_byte(bs,32);
237 blocks[2] = bytebits_to_byte(bs+32,32);
238 blocks[3] = bytebits_to_byte(bs+64,32);
239
240 PrintAndLog("Preparing to clone AWID %u to T55x7 with FC: %u, CN: %u", fmtlen, fc, cn);
241 PrintAndLog("Blk | Data ");
242 PrintAndLog("----+------------");
243 PrintAndLog(" 00 | 0x%08x", blocks[0]);
244 PrintAndLog(" 01 | 0x%08x", blocks[1]);
245 PrintAndLog(" 02 | 0x%08x", blocks[2]);
246 PrintAndLog(" 03 | 0x%08x", blocks[3]);
247
248 UsbCommand resp;
249 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
250
251 for (uint8_t i=0; i<4; i++) {
252 c.arg[0] = blocks[i];
253 c.arg[1] = i;
254 clearCommandBuffer();
255 SendCommand(&c);
256 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
257 PrintAndLog("Error occurred, device did not respond during write operation.");
258 return -1;
259 }
260 }
261 return 0;
262 }
263
264 int CmdAWIDBrute(const char *Cmd){
265
266 uint32_t fc = 0x00;
267 uint8_t fmtlen = 0;
268 uint8_t bits[96];
269 uint8_t *bs = bits;
270 size_t size = sizeof(bits);
271 memset(bs, 0x00, size);
272
273 char cmdp = param_getchar(Cmd, 0);
274 if (strlen(Cmd) > 4 || strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_brute();
275
276 fmtlen = param_get8(Cmd, 0);
277 fc = param_get32ex(Cmd, 1, 0, 10);
278 if ( !fc ) return usage_lf_awid_brute();
279
280 switch(fmtlen) {
281 case 50:
282 if ((fc & 0xFFFF) != fc) {
283 fc &= 0xFFFF;
284 PrintAndLog("Facility-Code Truncated to 16-bits (AWID50): %u", fc);
285 }
286 break;
287 default:
288 if ((fc & 0xFF) != fc) {
289 fc &= 0xFF;
290 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
291 }
292 break;
293 }
294
295 PrintAndLog("Bruteforceing AWID %d Reader", fmtlen);
296 PrintAndLog("Press pm3-button to abort simulation or run another command");
297
298 uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
299 uint64_t arg2 = 50; // clk RF/50 invert=0
300 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
301
302 for ( uint16_t cn = 1; cn < 0xFFFF; ++cn){
303 if (ukbhit()) {
304 PrintAndLog("aborted via keyboard!");
305 c.cmd = CMD_PING;
306 c.arg[0] = 0x00;
307 c.arg[1] = 0x00;
308 c.arg[2] = 0x00;
309 clearCommandBuffer();
310 SendCommand(&c);
311 return 1;
312 }
313
314 (void)getAWIDBits(fmtlen, fc, cn, bs);
315 memcpy(c.d.asBytes, bs, size);
316 clearCommandBuffer();
317 SendCommand(&c);
318
319 PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
320 // pause
321 sleep(1);
322 }
323 return 0;
324 }
325
326 static command_t CommandTable[] = {
327 {"help", CmdHelp, 1, "This help"},
328 {"fskdemod", CmdAWIDDemodFSK, 0, "['1'] Realtime AWID FSK demodulator (option '1' for one tag only)"},
329 {"sim", CmdAWIDSim, 0, "<Facility-Code> <Card Number> -- AWID tag simulator"},
330 {"clone", CmdAWIDClone, 0, "<Facility-Code> <Card Number> <Q5> -- Clone AWID to T55x7"},
331 {"brute", CmdAWIDBrute, 0, "<Facility-Code> -- bruteforce card number"},
332 {NULL, NULL, 0, NULL}
333 };
334
335 int CmdLFAWID(const char *Cmd) {
336 clearCommandBuffer();
337 CmdsParse(CommandTable, Cmd);
338 return 0;
339 }
340
341 int CmdHelp(const char *Cmd) {
342 CmdsHelp(CommandTable);
343 return 0;
344 }
Impressum, Datenschutz