]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfawid.c
CHG: some magic generation1 tags is not following protocol and answers to the "halt...
[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 "cmdmain.h"
20 static int CmdHelp(const char *Cmd);
21
22
23 int usage_lf_awid_fskdemod(void) {
24 PrintAndLog("Enables AWID26 compatible reader mode printing details of scanned AWID26 tags.");
25 PrintAndLog("By default, values are printed and logged until the button is pressed or another USB command is issued.");
26 PrintAndLog("If the ['1'] option is provided, reader mode is exited after reading a single AWID26 card.");
27 PrintAndLog("");
28 PrintAndLog("Usage: lf awid fskdemod ['1']");
29 PrintAndLog(" Options : ");
30 PrintAndLog(" 1 : (optional) stop after reading a single card");
31 PrintAndLog("");
32 PrintAndLog(" sample : lf awid fskdemod");
33 PrintAndLog(" : lf awid fskdemod 1");
34 return 0;
35 }
36
37 int usage_lf_awid_sim(void) {
38 PrintAndLog("Enables simulation of AWID26 card with specified facility-code and card number.");
39 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
40 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
41 PrintAndLog("");
42 PrintAndLog("Usage: lf awid sim <Facility-Code> <Card-Number>");
43 PrintAndLog(" Options : ");
44 PrintAndLog(" <Facility-Code> : 8-bit value representing the AWID facility code");
45 PrintAndLog(" <Card Number> : 16-bit value representing the AWID card number");
46 PrintAndLog("");
47 PrintAndLog(" sample : lf awid sim 224 1337");
48 return 0;
49 }
50
51 int usage_lf_awid_clone(void) {
52 PrintAndLog("Enables cloning of AWID26 card with specified facility-code and card number onto T55x7.");
53 PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process.");
54 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
55 PrintAndLog("");
56 PrintAndLog("Usage: lf awid clone <Facility-Code> <Card-Number>");
57 PrintAndLog(" Options : ");
58 PrintAndLog(" <Facility-Code> : 8-bit value representing the AWID facility code");
59 PrintAndLog(" <Card Number> : 16-bit value representing the AWID card number");
60 PrintAndLog("");
61 PrintAndLog(" sample : lf awid clone 224 1337");
62 return 0;
63 }
64
65 int CmdAWIDDemodFSK(const char *Cmd)
66 {
67 int findone=0;
68 if(Cmd[0]=='1') findone=1;
69 if (Cmd[0]=='h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
70 UsbCommand c={CMD_AWID_DEMOD_FSK};
71 c.arg[0]=findone;
72 clearCommandBuffer();
73 SendCommand(&c);
74 return 0;
75 }
76
77 int getAWIDBits(unsigned int fc, unsigned int cn, uint8_t *AWIDBits)
78 {
79 int i;
80 uint32_t fcode=(fc & 0x000000FF), cnum=(cn & 0x0000FFFF), uBits=0;
81 if (fcode != fc)
82 PrintAndLog("NOTE: Facility code truncated for AWID26 format (8-bit facility code)");
83 if (cnum!=cn)
84 PrintAndLog("NOTE: Card number was truncated for AWID26 format (16-bit card number)");
85
86 AWIDBits[0] = 0x01; // 6-bit Preamble with 2 parity bits
87 AWIDBits[1] = 0x1D; // First byte from card format (26-bit) plus parity bits
88 AWIDBits[2] = 0x80; // Set the next two bits as 0b10 to finish card format
89 uBits = (fcode<<4) + (cnum>>12);
90 if (!parityTest(uBits,12,0))
91 AWIDBits[2] |= (1<<5); // If not already even parity, set bit to make even
92 uBits = AWIDBits[2]>>5;
93 if (!parityTest(uBits, 3, 1))
94 AWIDBits[2] |= (1<<4);
95 uBits = fcode>>5; // first 3 bits of facility-code
96 AWIDBits[2] += (uBits<<1);
97 if (!parityTest(uBits, 3, 1))
98 AWIDBits[2]++; // Set parity bit to make odd parity
99 uBits = (fcode & 0x1C)>>2;
100 AWIDBits[3] = 0;
101 if (!parityTest(uBits,3,1))
102 AWIDBits[3] |= (1<<4);
103 AWIDBits[3] += (uBits<<5);
104 uBits = ((fcode & 0x3)<<1) + ((cnum & 0x8000)>>15); // Grab/shift 2 LSBs from facility code and add shifted MSB from cardnum
105 if (!parityTest(uBits,3,1))
106 AWIDBits[3]++; // Set LSB for parity
107 AWIDBits[3]+= (uBits<<1);
108 uBits = (cnum & 0x7000)>>12;
109 AWIDBits[4] = uBits<<5;
110 if (!parityTest(uBits,3,1))
111 AWIDBits[4] |= (1<<4);
112 uBits = (cnum & 0x0E00)>>9;
113 AWIDBits[4] += (uBits<<1);
114 if (!parityTest(uBits,3,1))
115 AWIDBits[4]++; // Set LSB for parity
116 uBits = (cnum & 0x1C0)>>6; // Next bits from card number
117 AWIDBits[5]=(uBits<<5);
118 if (!parityTest(uBits,3,1))
119 AWIDBits[5] |= (1<<4); // Set odd parity bit as needed
120 uBits = (cnum & 0x38)>>3;
121 AWIDBits[5]+= (uBits<<1);
122 if (!parityTest(uBits,3,1))
123 AWIDBits[5]++; // Set odd parity bit as needed
124 uBits = (cnum & 0x7); // Last three bits from card number!
125 AWIDBits[6] = (uBits<<5);
126 if (!parityTest(uBits,3,1))
127 AWIDBits[6] |= (1<<4);
128 uBits = (cnum & 0x0FFF);
129 if (!parityTest(uBits,12,1))
130 AWIDBits[6] |= (1<<3);
131 else
132 AWIDBits[6]++;
133 for (i = 7; i<12; i++)
134 AWIDBits[i]=0x11;
135 return 1;
136 }
137
138 int CmdAWIDSim(const char *Cmd)
139 {
140 uint32_t fcode = 0, cnum = 0, fc=0, cn=0, i=0;
141 uint8_t *BS, BitStream[12];
142 uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
143 uint64_t arg2 = 50; // clk RF/50 invert=0
144 BS = BitStream;
145 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) {
146 return usage_lf_awid_sim();
147 }
148
149 fcode=(fc & 0x000000FF);
150 cnum=(cn & 0x0000FFFF);
151 if (fc!=fcode)
152 PrintAndLog("Facility-Code (%u) truncated to 8-bits: %u",fc,fcode);
153 if (cn!=cnum)
154 PrintAndLog("Card number (%u) truncated to 16-bits: %u",cn,cnum);
155 PrintAndLog("Emulating AWID26 -- FC: %u; CN: %u\n",fcode,cnum);
156 PrintAndLog("Press pm3-button to abort simulation or run another command");
157 // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
158 if (getAWIDBits(fc, cn, BS)) {
159 PrintAndLog("Running 'lf simfsk c 50 H 10 L 8 d %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'",
160 BS[0],BS[1],BS[2],BS[3],BS[4],BS[5],BS[6],
161 BS[7],BS[8],BS[9],BS[10],BS[11]);
162 } else
163 PrintAndLog("Error with tag bitstream generation.");
164 UsbCommand c;
165 c.cmd = CMD_FSK_SIM_TAG;
166 c.arg[0] = arg1; // fcHigh<<8 + fcLow
167 c.arg[1] = arg2; // Inversion and clk setting
168 c.arg[2] = 96; // Bitstream length: 96-bits == 12 bytes
169 for (i=0; i < 96; i++)
170 c.d.asBytes[i] = (BS[i/8] & (1<<(7-(i%8))))?1:0;
171 clearCommandBuffer();
172 SendCommand(&c);
173 return 0;
174 }
175
176 int CmdAWIDClone(const char *Cmd)
177 {
178 clearCommandBuffer();
179 uint32_t fc=0,cn=0,blocks[4] = {0x00107060, 0, 0, 0x11111111}, i=0;
180 uint8_t BitStream[12];
181 uint8_t *BS=BitStream;
182 UsbCommand c, resp;
183
184 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) {
185 return usage_lf_awid_clone();
186 }
187
188 if ((fc & 0xFF) != fc) {
189 fc &= 0xFF;
190 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
191 }
192
193 if ((cn & 0xFFFF) != cn) {
194 cn &= 0xFFFF;
195 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
196 }
197
198 if (getAWIDBits(fc,cn,BS)) {
199 PrintAndLog("Preparing to clone AWID26 to T55x7 with FC: %u, CN: %u (Raw: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x)",
200 fc,cn, BS[0],BS[1],BS[2],BS[3],BS[4],BS[5],BS[6],BS[7],BS[8],BS[9],BS[10],BS[11]);
201 blocks[1] = (BS[0]<<24) + (BS[1]<<16) + (BS[2]<<8) + (BS[3]);
202 blocks[2] = (BS[4]<<24) + (BS[5]<<16) + (BS[6]<<8) + (BS[7]);
203 PrintAndLog("Block 0: 0x%08x", blocks[0]);
204 PrintAndLog("Block 1: 0x%08x", blocks[1]);
205 PrintAndLog("Block 2: 0x%08x", blocks[2]);
206 PrintAndLog("Block 3: 0x%08x", blocks[3]);
207 for (i=0; i<4; i++) {
208 c.cmd = CMD_T55XX_WRITE_BLOCK;
209 c.arg[0] = blocks[i];
210 c.arg[1] = i;
211 c.arg[2] = 0;
212 SendCommand(&c);
213 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
214 PrintAndLog("Error occurred, device did not respond during write operation.");
215 return -1;
216 }
217 }
218 }
219 return 0;
220 }
221
222 static command_t CommandTable[] =
223 {
224 {"help", CmdHelp, 1, "This help"},
225 {"fskdemod", CmdAWIDDemodFSK, 0, "['1'] Realtime AWID FSK demodulator (option '1' for one tag only)"},
226 {"sim", CmdAWIDSim, 0, "<Facility-Code> <Card Number> -- AWID tag simulator"},
227 {"clone", CmdAWIDClone, 0, "<Facility-Code> <Card Number> -- Clone AWID to T55x7 (tag must be in range of antenna)"},
228 {NULL, NULL, 0, NULL}
229 };
230
231 int CmdLFAWID(const char *Cmd)
232 {
233 CmdsParse(CommandTable, Cmd);
234 return 0;
235 }
236
237 int CmdHelp(const char *Cmd)
238 {
239 CmdsHelp(CommandTable);
240 return 0;
241 }
Impressum, Datenschutz