]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhflegic.c
BUG: fixed a bug with the initialization of IV in cmdhfmfu.c CmdHF14AMfucAuth
[proxmark3-svn] / client / cmdhflegic.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency Legic commands
9//-----------------------------------------------------------------------------
10
8e220a91 11#include <stdio.h>
12#include <string.h>
902cb3c0 13#include "proxmark3.h"
41dab153 14#include "data.h"
15#include "ui.h"
7fe9b0b7 16#include "cmdparser.h"
17#include "cmdhflegic.h"
669c1b80 18#include "cmdmain.h"
7fe9b0b7 19
20static int CmdHelp(const char *Cmd);
21
7fe9b0b7 22static command_t CommandTable[] =
23{
24 {"help", CmdHelp, 1, "This help"},
669c1b80 25 {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
41dab153 26 {"reader", CmdLegicRFRead, 0, "[offset [length]] -- read bytes from a LEGIC card"},
3612a8a8 27 {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"},
28 {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"},
29 {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
30 {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
31 {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
7fe9b0b7 32 {NULL, NULL, 0, NULL}
33};
34
35int CmdHFLegic(const char *Cmd)
36{
37 CmdsParse(CommandTable, Cmd);
38 return 0;
39}
40
41int CmdHelp(const char *Cmd)
42{
43 CmdsHelp(CommandTable);
44 return 0;
45}
669c1b80 46
47/*
48 * Output BigBuf and deobfuscate LEGIC RF tag data.
49 * This is based on information given in the talk held
50 * by Henryk Ploetz and Karsten Nohl at 26c3
669c1b80 51 */
52int CmdLegicDecode(const char *Cmd)
53{
4961e292 54 int i, j, k, n;
669c1b80 55 int segment_len = 0;
56 int segment_flag = 0;
57 int stamp_len = 0;
58 int crc = 0;
59 int wrp = 0;
60 int wrc = 0;
4961e292 61 uint8_t data_buf[1024]; // receiver buffer
669c1b80 62 char out_string[3076]; // just use big buffer - bad practice
63 char token_type[4];
669c1b80 64
65 // copy data from proxmark into buffer
4961e292 66 GetFromBigBuf(data_buf,sizeof(data_buf),0);
67 WaitForResponse(CMD_ACK,NULL);
669c1b80 68
69 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
70
71 PrintAndLog("\nCDF: System Area");
72
73 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x",
74 data_buf[0],
75 data_buf[1],
76 data_buf[2],
77 data_buf[3],
78 data_buf[4]
79 );
80
81 crc = data_buf[4];
82
83 switch (data_buf[5]&0x7f) {
84 case 0x00 ... 0x2f:
85 strncpy(token_type, "IAM",sizeof(token_type));
86 break;
87 case 0x30 ... 0x6f:
88 strcpy(token_type, "SAM");
89 break;
90 case 0x70 ... 0x7f:
91 strcpy(token_type, "GAM");
92 break;
93 default:
94 strcpy(token_type, "???");
95 break;
96 }
97
98 stamp_len = 0xfc - data_buf[6];
99
100 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
101 data_buf[5],
102 data_buf[6],
103 token_type,
104 (data_buf[5]&0x80)>>7,
105 stamp_len
106 );
107
108 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
109 data_buf[7]&0x0f,
110 (data_buf[7]&0x70)>>4,
111 (data_buf[7]&0x80)>>7,
112 data_buf[7],
113 data_buf[8]
114 );
115
116 PrintAndLog("Remaining Header Area");
117
118 PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
119 data_buf[9],
120 data_buf[10],
121 data_buf[11],
122 data_buf[12],
123 data_buf[13],
124 data_buf[14],
125 data_buf[15],
126 data_buf[16],
127 data_buf[17],
128 data_buf[18],
129 data_buf[19],
130 data_buf[20],
131 data_buf[21]
132 );
133
134 PrintAndLog("\nADF: User Area");
135
41dab153 136 i = 22;
669c1b80 137 for (n=0; n<64; n++) {
138 segment_len = ((data_buf[i+1]^crc)&0x0f) * 256 + (data_buf[i]^crc);
139 segment_flag = ((data_buf[i+1]^crc)&0xf0)>>4;
140
141 wrp = (data_buf[i+2]^crc);
142 wrc = ((data_buf[i+3]^crc)&0x70)>>4;
143
144 PrintAndLog("Segment %02u: raw header=%02x %02x %02x %02x, flag=%01x (valid=%01u, last=%01u), len=%04u, WRP=%02u, WRC=%02u, RD=%01u, CRC=%02x",
145 n,
146 data_buf[i]^crc,
147 data_buf[i+1]^crc,
148 data_buf[i+2]^crc,
149 data_buf[i+3]^crc,
150 segment_flag,
151 (segment_flag&0x4)>>2,
152 (segment_flag&0x8)>>3,
153 segment_len,
154 wrp,
155 wrc,
156 ((data_buf[i+3]^crc)&0x80)>>7,
157 (data_buf[i+4]^crc)
158 );
159
160 i+=5;
161
162 if (wrc>0) {
163 PrintAndLog("WRC protected area:");
31332265 164 for (k=0, j=0; k < wrc && j<(sizeof(out_string)-3); k++, i++, j += 3) {
669c1b80 165 sprintf(&out_string[j], "%02x", (data_buf[i]^crc));
166 out_string[j+2] = ' ';
167 };
168
169 out_string[j] = '\0';
170
171 PrintAndLog("%s", out_string);
172 }
173
174 if (wrp>wrc) {
175 PrintAndLog("Remaining write protected area:");
176
31332265 177 for (k=0, j=0; k < (wrp-wrc) && j<(sizeof(out_string)-3); k++, i++, j += 3) {
669c1b80 178 sprintf(&out_string[j], "%02x", (data_buf[i]^crc));
179 out_string[j+2] = ' ';
180 };
181
182 out_string[j] = '\0';
183
184 PrintAndLog("%s", out_string);
185 if((wrp-wrc) == 8) {
186 sprintf(out_string,"Card ID: %2X%02X%02X",data_buf[i-4]^crc,data_buf[i-3]^crc,data_buf[i-2]^crc);
187 PrintAndLog("%s", out_string);
188 }
189 }
190
191 PrintAndLog("Remaining segment payload:");
31332265 192 for (k=0, j=0; k < (segment_len - wrp - 5) && j<(sizeof(out_string)-3); k++, i++, j += 3) {
669c1b80 193 sprintf(&out_string[j], "%02x", (data_buf[i]^crc));
194 out_string[j+2] = ' ';
195 };
196
197 out_string[j] = '\0';
198
199 PrintAndLog("%s", out_string);
200
201 // end with last segment
202 if (segment_flag & 0x8)
203 return 0;
204 };
205 return 0;
206}
41dab153 207
208int CmdLegicRFRead(const char *Cmd)
209{
210 int byte_count=0,offset=0;
211 sscanf(Cmd, "%i %i", &offset, &byte_count);
a2b1414f 212 if(byte_count == 0) byte_count = -1;
213 if(byte_count + offset > 1024) byte_count = 1024 - offset;
41dab153 214 UsbCommand c={CMD_READER_LEGIC_RF, {offset, byte_count, 0}};
215 SendCommand(&c);
216 return 0;
217}
3612a8a8 218
219int CmdLegicLoad(const char *Cmd)
220{
221 FILE *f = fopen(Cmd, "r");
222 if(!f) {
223 PrintAndLog("couldn't open '%s'", Cmd);
224 return -1;
225 }
226 char line[80]; int offset = 0; unsigned int data[8];
227 while(fgets(line, sizeof(line), f)) {
228 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
229 &data[0], &data[1], &data[2], &data[3],
230 &data[4], &data[5], &data[6], &data[7]);
231 if(res != 8) {
232 PrintAndLog("Error: could not read samples");
233 fclose(f);
234 return -1;
235 }
236 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
237 int j; for(j = 0; j < 8; j++) {
238 c.d.asBytes[j] = data[j];
239 }
240 SendCommand(&c);
902cb3c0 241 WaitForResponse(CMD_ACK, NULL);
3612a8a8 242 offset += 8;
243 }
244 fclose(f);
245 PrintAndLog("loaded %u samples", offset);
246 return 0;
247}
248
249int CmdLegicSave(const char *Cmd)
250{
3612a8a8 251 int requested = 1024;
252 int offset = 0;
4961e292 253 int delivered = 0;
3612a8a8 254 char filename[1024];
4961e292 255 uint8_t got[1024];
256
3612a8a8 257 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
3612a8a8 258
4961e292 259 /* If no length given save entire legic read buffer */
260 /* round up to nearest 8 bytes so the saved data can be used with legicload */
3612a8a8 261 if (requested == 0) {
4961e292 262 requested = 1024;
3612a8a8 263 }
4961e292 264 if (requested % 8 != 0) {
265 int remainder = requested % 8;
266 requested = requested + 8 - remainder;
267 }
268
269 if (offset + requested > sizeof(got)) {
270 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
271 return 0;
272 }
273
3612a8a8 274 FILE *f = fopen(filename, "w");
275 if(!f) {
276 PrintAndLog("couldn't open '%s'", Cmd+1);
277 return -1;
278 }
279
4961e292 280 GetFromBigBuf(got,requested,offset);
281 WaitForResponse(CMD_ACK,NULL);
282
283 for (int j = 0; j < requested; j += 8) {
284 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
285 got[j+0],
286 got[j+1],
287 got[j+2],
288 got[j+3],
289 got[j+4],
290 got[j+5],
291 got[j+6],
292 got[j+7]
293 );
294 delivered += 8;
3612a8a8 295 if (delivered >= requested)
296 break;
297 }
298
299 fclose(f);
300 PrintAndLog("saved %u samples", delivered);
301 return 0;
302}
303
304int CmdLegicRfSim(const char *Cmd)
305{
306 UsbCommand c={CMD_SIMULATE_TAG_LEGIC_RF};
307 c.arg[0] = 6;
308 c.arg[1] = 3;
309 c.arg[2] = 0;
1a07fd51 310 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
3612a8a8 311 SendCommand(&c);
312 return 0;
313}
314
315int CmdLegicRfWrite(const char *Cmd)
316{
317 UsbCommand c={CMD_WRITER_LEGIC_RF};
125a98a1 318 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]);
3612a8a8 319 if(res != 2) {
320 PrintAndLog("Please specify the offset and length as two hex strings");
321 return -1;
322 }
323 SendCommand(&c);
324 return 0;
325}
326
327int CmdLegicRfFill(const char *Cmd)
328{
329 UsbCommand cmd ={CMD_WRITER_LEGIC_RF};
1a07fd51 330 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
3612a8a8 331 if(res != 3) {
332 PrintAndLog("Please specify the offset, length and value as two hex strings");
333 return -1;
334 }
335
336 int i;
337 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
338 for(i = 0; i < 48; i++) {
339 c.d.asBytes[i] = cmd.arg[2];
340 }
341 for(i = 0; i < 22; i++) {
342 c.arg[0] = i*48;
343 SendCommand(&c);
902cb3c0 344 WaitForResponse(CMD_ACK,NULL);
3612a8a8 345 }
346 SendCommand(&cmd);
347 return 0;
348 }
349
Impressum, Datenschutz