]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfawid.c
CHG: Remake the "hf mf chkkeys" It now prints the found keys like "hf mf nested...
[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) {
a126332a 27 PrintAndLog("Enables AWID26 compatible reader mode printing details of scanned AWID26 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 AWID26 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 : lf awid fskdemod");
36 PrintAndLog(" : lf awid fskdemod 1");
37 return 0;
db25599d 38}
39
40int usage_lf_awid_sim(void) {
a126332a 41 PrintAndLog("Enables simulation of AWID26 card with specified facility-code and card number.");
42 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
43 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
44 PrintAndLog("");
45 PrintAndLog("Usage: lf awid sim <Facility-Code> <Card-Number>");
46 PrintAndLog("Options :");
47 PrintAndLog(" <Facility-Code> : 8-bit value AWID facility code");
48 PrintAndLog(" <Card Number> : 16-bit value AWID card number");
49 PrintAndLog("");
50 PrintAndLog("Sample : lf awid sim 224 1337");
51 return 0;
db25599d 52}
53
54int usage_lf_awid_clone(void) {
a126332a 55 PrintAndLog("Enables cloning of AWID26 card with specified facility-code and card number onto T55x7.");
56 PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process.");
57 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
58 PrintAndLog("");
59 PrintAndLog("Usage: lf awid clone <Facility-Code> <Card-Number>");
60 PrintAndLog("Options :");
508b37ba 61 PrintAndLog(" <Facility-Code> : 8-bit value AWID facility code");
a126332a 62 PrintAndLog(" <Card Number> : 16-bit value AWID card number");
63 PrintAndLog(" Q5 : optional - clone to Q5 (T5555) instead of T55x7 chip");
64 PrintAndLog("");
65 PrintAndLog("Sample : lf awid clone 224 1337");
66 return 0;
db25599d 67}
68
508b37ba 69int usage_lf_awid_brute(void){
978920b9 70 PrintAndLog("Enables bruteforce of AWID26 reader with specified facility-code.");
508b37ba 71 PrintAndLog("Per AWID26 format, the facility-code (FC) is 8-bit and the card number is 16-bit.");
978920b9 72 PrintAndLog("This is a incremental attack against reader.");
508b37ba 73 PrintAndLog("");
74 PrintAndLog("Usage: lf awid brute <Facility-Code>");
75 PrintAndLog("Options :");
76 PrintAndLog(" <Facility-Code> : 8-bit value AWID facility code");
77 PrintAndLog("");
78 PrintAndLog("Sample : lf awid brute 224");
79 return 0;
80}
81
52f2df61 82int CmdAWIDDemodFSK(const char *Cmd) {
a126332a 83 int findone = 0;
84 if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
85 if (Cmd[0] == '1') findone = 1;
86
87 UsbCommand c = {CMD_AWID_DEMOD_FSK, {findone, 0, 0}};
88 clearCommandBuffer();
89 SendCommand(&c);
90 return 0;
db25599d 91}
92
a126332a 93//refactored by marshmellow
94int getAWIDBits(uint32_t fc, uint32_t cn, uint8_t *AWIDBits) {
95 uint8_t pre[66];
96 memset(pre, 0, sizeof(pre));
97 AWIDBits[7]=1;
98 num_to_bytebits(26, 8, pre);
52f2df61 99
a126332a 100 uint8_t wiegand[24];
101 num_to_bytebits(fc, 8, wiegand);
102 num_to_bytebits(cn, 16, wiegand+8);
52f2df61 103
a126332a 104 wiegand_add_parity(pre+8, wiegand, 24);
a126332a 105 size_t bitLen = addParity(pre, AWIDBits+8, 66, 4, 1);
fcb1cdba 106
a126332a 107 if (bitLen != 88) return 0;
52f2df61 108 return 1;
db25599d 109}
110
a126332a 111int CmdAWIDSim(const char *Cmd) {
fcb1cdba 112 uint32_t fcode = 0, cnum = 0, fc = 0, cn = 0;
a126332a 113 uint8_t bits[96];
114 uint8_t *bs = bits;
115 size_t size = sizeof(bits);
fcb1cdba 116 memset(bs, 0x00, size);
52f2df61 117
fcb1cdba 118 uint64_t arg1 = ( 10 << 8 ) + 8; // fcHigh = 10, fcLow = 8
52f2df61 119 uint64_t arg2 = 50; // clk RF/50 invert=0
120
121 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_awid_sim();
122
123 fcode = (fc & 0x000000FF);
124 cnum = (cn & 0x0000FFFF);
125
fcb1cdba 126 if (fc != fcode) PrintAndLog("Facility-Code (%u) truncated to 8-bits: %u", fc, fcode);
127 if (cn != cnum) PrintAndLog("Card number (%u) truncated to 16-bits: %u", cn, cnum);
52f2df61 128
129 PrintAndLog("Emulating AWID26 -- FC: %u; CN: %u\n", fcode, cnum);
130 PrintAndLog("Press pm3-button to abort simulation or run another command");
131
132 if (!getAWIDBits(fc, cn, bs)) {
133 PrintAndLog("Error with tag bitstream generation.");
134 return 1;
135 }
136 // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
52f2df61 137 // arg1 --- fcHigh<<8 + fcLow
138 // arg2 --- Inversion and clk setting
139 // 96 --- Bitstream length: 96-bits == 12 bytes
a126332a 140 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
141 memcpy(c.d.asBytes, bs, size);
52f2df61 142 clearCommandBuffer();
143 SendCommand(&c);
144 return 0;
db25599d 145}
146
a126332a 147int CmdAWIDClone(const char *Cmd) {
148 uint32_t blocks[4] = {T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 3<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0};
149 uint32_t fc=0,cn=0;
150 uint8_t bits[96];
52f2df61 151 uint8_t *bs=bits;
a126332a 152 memset(bs,0,sizeof(bits));
52f2df61 153
154 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_awid_clone();
f3cfe428 155
a126332a 156 if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
157 blocks[0] = T5555_MODULATION_FSK2 | T5555_INVERT_OUTPUT | 50<<T5555_BITRATE_SHIFT | 3<<T5555_MAXBLOCK_SHIFT;
158
f3cfe428 159 if ((fc & 0xFF) != fc) {
160 fc &= 0xFF;
161 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
162 }
163
164 if ((cn & 0xFFFF) != cn) {
165 cn &= 0xFFFF;
166 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
167 }
52f2df61 168
169 if ( !getAWIDBits(fc, cn, bs)) {
170 PrintAndLog("Error with tag bitstream generation.");
171 return 1;
172 }
173
a126332a 174 blocks[1] = bytebits_to_byte(bs,32);
175 blocks[2] = bytebits_to_byte(bs+32,32);
176 blocks[3] = bytebits_to_byte(bs+64,32);
52f2df61 177
a126332a 178 PrintAndLog("Preparing to clone AWID26 to T55x7 with FC: %u, CN: %u",
179 fc, cn);
52f2df61 180 PrintAndLog("Blk | Data ");
181 PrintAndLog("----+------------");
182 PrintAndLog(" 00 | 0x%08x", blocks[0]);
183 PrintAndLog(" 01 | 0x%08x", blocks[1]);
184 PrintAndLog(" 02 | 0x%08x", blocks[2]);
185 PrintAndLog(" 03 | 0x%08x", blocks[3]);
186
187 UsbCommand resp;
188 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
f3cfe428 189
a126332a 190 for (uint8_t i=0; i<4; i++) {
52f2df61 191 c.arg[0] = blocks[i];
192 c.arg[1] = i;
193 clearCommandBuffer();
194 SendCommand(&c);
195 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
196 PrintAndLog("Error occurred, device did not respond during write operation.");
197 return -1;
f3cfe428 198 }
199 }
200 return 0;
db25599d 201}
202
508b37ba 203int CmdAWIDBrute(const char *Cmd){
204
205 uint8_t fc = 0x00;
206 uint8_t bits[96];
207 uint8_t *bs = bits;
208 size_t size = sizeof(bits);
209 memset(bs, 0x00, size);
210
211 char cmdp = param_getchar(Cmd, 0);
212 if (strlen(Cmd) > 3 || strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_brute();
213
214 fc = param_get8(Cmd, 0);
215 if ( fc == 0) return usage_lf_awid_brute();
216
978920b9 217 PrintAndLog("Bruteforceing AWID26 Reader");
508b37ba 218 PrintAndLog("Press pm3-button to abort simulation or run another command");
219
220 uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
221 uint64_t arg2 = 50; // clk RF/50 invert=0
222 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
223
224 for ( uint16_t cn = 1; cn < 0xFFFF; ++cn){
225 if (ukbhit()) {
226 PrintAndLog("aborted via keyboard!");
227 c.cmd = CMD_PING;
228 c.arg[0] = 0x00;
229 c.arg[1] = 0x00;
230 c.arg[2] = 0x00;
231 clearCommandBuffer();
232 SendCommand(&c);
233 return 1;
234 }
235
236 (void)getAWIDBits(fc, cn, bs);
237 memcpy(c.d.asBytes, bs, size);
238 clearCommandBuffer();
239 SendCommand(&c);
240
241 PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
242 // pause
243 sleep(1);
244 }
245 return 0;
246}
247
a126332a 248static command_t CommandTable[] = {
f3cfe428 249 {"help", CmdHelp, 1, "This help"},
250 {"fskdemod", CmdAWIDDemodFSK, 0, "['1'] Realtime AWID FSK demodulator (option '1' for one tag only)"},
251 {"sim", CmdAWIDSim, 0, "<Facility-Code> <Card Number> -- AWID tag simulator"},
a126332a 252 {"clone", CmdAWIDClone, 0, "<Facility-Code> <Card Number> <Q5> -- Clone AWID to T55x7"},
508b37ba 253 {"brute", CmdAWIDBrute, 0, "<Facility-Code> -- bruteforce card number"},
f3cfe428 254 {NULL, NULL, 0, NULL}
db25599d 255};
256
a126332a 257int CmdLFAWID(const char *Cmd) {
f3cfe428 258 CmdsParse(CommandTable, Cmd);
259 return 0;
db25599d 260}
261
a126332a 262int CmdHelp(const char *Cmd) {
f3cfe428 263 CmdsHelp(CommandTable);
264 return 0;
db25599d 265}
Impressum, Datenschutz