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