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