]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhflegic.c
ADD: first try to implement crc-16 for Legic Advant *not working yet* Can't verify it.
[proxmark3-svn] / client / cmdhflegic.c
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
11 #include <stdio.h>
12 #include <string.h>
13 #include "proxmark3.h"
14 #include "data.h"
15 #include "ui.h"
16 #include "cmdparser.h"
17 #include "cmdhflegic.h"
18 #include "cmdmain.h"
19 #include "util.h"
20 #include "crc.h"
21 static int CmdHelp(const char *Cmd);
22
23 int usage_legic_calccrc8(void){
24 PrintAndLog("Calculates the legic crc8 on the input hexbytes.");
25 PrintAndLog("There must be an even number of hexsymbols as input.");
26 PrintAndLog("Usage: hf legic crc8 <hexbytes>");
27 PrintAndLog("Options :");
28 PrintAndLog(" <hexbytes> : hex bytes in a string");
29 PrintAndLog("");
30 PrintAndLog("Sample : hf legic crc8 deadbeef1122");
31 return 0;
32 }
33
34 int usage_legic_load(void){
35 PrintAndLog("It loads datasamples from the file `filename` to device memory");
36 PrintAndLog("Usage: hf legic load <file name>");
37 PrintAndLog(" sample: hf legic load filename");
38 return 0;
39 }
40
41 /*
42 * Output BigBuf and deobfuscate LEGIC RF tag data.
43 * This is based on information given in the talk held
44 * by Henryk Ploetz and Karsten Nohl at 26c3
45 */
46 int CmdLegicDecode(const char *Cmd) {
47 // Index for the bytearray.
48 int i = 0;
49 int k = 0, segmentNum;
50 int segment_len = 0;
51 int segment_flag = 0;
52 uint8_t stamp_len = 0;
53 int crc = 0;
54 int wrp = 0;
55 int wrc = 0;
56 uint8_t data_buf[1200]; // receiver buffer, should be 1024..
57 char token_type[4];
58
59 // copy data from proxmark into buffer
60 GetFromBigBuf(data_buf,sizeof(data_buf),0);
61 WaitForResponse(CMD_ACK,NULL);
62
63 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
64 crc = data_buf[4];
65 uint32_t calc_crc = CRC8Legic(data_buf, 4);
66
67 PrintAndLog("\nCDF: System Area");
68
69 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
70 data_buf[0],
71 data_buf[1],
72 data_buf[2],
73 data_buf[3],
74 data_buf[4],
75 (calc_crc == crc) ? "OK":"Fail"
76 );
77
78 switch (data_buf[5] & 0x7f) {
79 case 0x00 ... 0x2f:
80 strncpy(token_type, "IAM",sizeof(token_type));
81 break;
82 case 0x30 ... 0x6f:
83 strncpy(token_type, "SAM",sizeof(token_type));
84 break;
85 case 0x70 ... 0x7f:
86 strncpy(token_type, "GAM",sizeof(token_type));
87 break;
88 default:
89 strncpy(token_type, "???",sizeof(token_type));
90 break;
91 }
92
93 stamp_len = 0xfc - data_buf[6];
94
95 PrintAndLog("DCF: %02x %02x, Token Type=%s (OLE=%01u), Stamp len=%02u",
96 data_buf[5],
97 data_buf[6],
98 token_type,
99 (data_buf[5]&0x80)>>7,
100 stamp_len
101 );
102
103 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
104 data_buf[7]&0x0f,
105 (data_buf[7]&0x70)>>4,
106 (data_buf[7]&0x80)>>7,
107 data_buf[7],
108 data_buf[8]
109 );
110
111 PrintAndLog("Remaining Header Area");
112 PrintAndLog("%s", sprint_hex(data_buf+9, 13));
113
114 uint8_t segCrcBytes[8] = {0x00};
115 uint32_t segCalcCRC = 0;
116 uint32_t segCRC = 0;
117
118 PrintAndLog("\nADF: User Area");
119 printf("-------------------------------------\n");
120 i = 22;
121 // 64 potential segements
122 for ( segmentNum=0; segmentNum<64; segmentNum++ ) {
123 segment_len = ((data_buf[i+1]^crc)&0x0f) * 256 + (data_buf[i]^crc);
124 segment_flag = ((data_buf[i+1]^crc)&0xf0)>>4;
125
126 wrp = (data_buf[i+2]^crc);
127 wrc = ((data_buf[i+3]^crc)&0x70)>>4;
128
129 bool hasWRC = (wrc > 0);
130 bool hasWRP = (wrp > wrc);
131 int wrp_len = (wrp - wrc);
132 int remain_seg_payload_len = (segment_len - wrp - 5);
133
134 // validate segment-crc
135 segCrcBytes[0]=data_buf[0]; //uid0
136 segCrcBytes[1]=data_buf[1]; //uid1
137 segCrcBytes[2]=data_buf[2]; //uid2
138 segCrcBytes[3]=data_buf[3]; //uid3
139 segCrcBytes[4]=(data_buf[i]^crc); //hdr0
140 segCrcBytes[5]=(data_buf[i+1]^crc); //hdr1
141 segCrcBytes[6]=(data_buf[i+2]^crc); //hdr2
142 segCrcBytes[7]=(data_buf[i+3]^crc); //hdr3
143
144 segCalcCRC = CRC8Legic(segCrcBytes, 8);
145 segCRC = data_buf[i+4]^crc;
146
147 PrintAndLog("Segment %02u \nraw header=0x%02X 0x%02X 0x%02X 0x%02X \nSegment len: %u, Flag: 0x%X (valid:%01u, last:%01u), WRP: %02u, WRC: %02u, RD: %01u, CRC: 0x%02X (%s)",
148 segmentNum,
149 data_buf[i]^crc,
150 data_buf[i+1]^crc,
151 data_buf[i+2]^crc,
152 data_buf[i+3]^crc,
153 segment_len,
154 segment_flag,
155 (segment_flag & 0x4) >> 2,
156 (segment_flag & 0x8) >> 3,
157 wrp,
158 wrc,
159 ((data_buf[i+3]^crc) & 0x80) >> 7,
160 segCRC,
161 ( segCRC == segCalcCRC ) ? "OK" : "fail"
162 );
163
164 i += 5;
165
166 if ( hasWRC ) {
167 PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc);
168
169 // de-xor? if not zero, assume it needs xoring.
170 if ( data_buf[i] > 0) {
171 for ( k=i; k < wrc; ++k)
172 data_buf[k] ^= crc;
173 }
174 print_hex_break( data_buf+i, wrc, 16);
175
176 i += wrc;
177 }
178
179 if ( hasWRP ) {
180 PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len);
181
182 // de-xor? if not zero, assume it needs xoring.
183 if ( data_buf[i] > 0) {
184 for (k=i; k < wrp_len; ++k)
185 data_buf[k] ^= crc;
186 }
187
188 print_hex_break( data_buf+i, wrp_len, 16);
189
190 i += wrp_len;
191
192 // does this one work?
193 if( wrp_len == 8 )
194 PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc);
195 }
196
197 PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len);
198
199 if ( data_buf[i] > 0 ) {
200 for ( k=i; k < remain_seg_payload_len; ++k)
201 data_buf[k] ^= crc;
202 }
203
204 print_hex_break( data_buf+i, remain_seg_payload_len, 16);
205
206 i += remain_seg_payload_len;
207
208 printf("\n-------------------------------------\n");
209
210 // end with last segment
211 if (segment_flag & 0x8) return 0;
212
213 } // end for loop
214 return 0;
215 }
216
217 int CmdLegicRFRead(const char *Cmd) {
218 int byte_count=0, offset=0;
219 sscanf(Cmd, "%i %i", &offset, &byte_count);
220 if(byte_count == 0) byte_count = -1;
221 if(byte_count + offset > 1024) byte_count = 1024 - offset;
222
223 UsbCommand c= {CMD_READER_LEGIC_RF, {offset, byte_count, 0}};
224 clearCommandBuffer();
225 SendCommand(&c);
226 return 0;
227 }
228
229 int CmdLegicLoad(const char *Cmd) {
230
231 char cmdp = param_getchar(Cmd, 0);
232 if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load();
233
234 char filename[FILE_PATH_SIZE] = {0x00};
235 int len = strlen(Cmd);
236
237 if (len > FILE_PATH_SIZE) {
238 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
239 return 0;
240 }
241 memcpy(filename, Cmd, len);
242
243 FILE *f = fopen(filename, "r");
244 if(!f) {
245 PrintAndLog("couldn't open '%s'", Cmd);
246 return -1;
247 }
248
249 char line[80];
250 int offset = 0;
251 uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
252 int index = 0;
253 int totalbytes = 0;
254 while ( fgets(line, sizeof(line), f) ) {
255 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
256 (unsigned int *)&data[index],
257 (unsigned int *)&data[index + 1],
258 (unsigned int *)&data[index + 2],
259 (unsigned int *)&data[index + 3],
260 (unsigned int *)&data[index + 4],
261 (unsigned int *)&data[index + 5],
262 (unsigned int *)&data[index + 6],
263 (unsigned int *)&data[index + 7]);
264
265 if(res != 8) {
266 PrintAndLog("Error: could not read samples");
267 fclose(f);
268 return -1;
269 }
270 index += res;
271
272 if ( index == USB_CMD_DATA_SIZE ){
273 // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
274 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
275 memcpy(c.d.asBytes, data, sizeof(data));
276 clearCommandBuffer();
277 SendCommand(&c);
278 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
279 PrintAndLog("Command execute timeout");
280 fclose(f);
281 return 1;
282 }
283 offset += index;
284 totalbytes += index;
285 index = 0;
286 }
287 }
288 fclose(f);
289
290 // left over bytes?
291 if ( index != 0 ) {
292 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
293 memcpy(c.d.asBytes, data, 8);
294 clearCommandBuffer();
295 SendCommand(&c);
296 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
297 PrintAndLog("Command execute timeout");
298 return 1;
299 }
300 totalbytes += index;
301 }
302
303 PrintAndLog("loaded %u samples", totalbytes);
304 return 0;
305 }
306
307 int CmdLegicSave(const char *Cmd) {
308 int requested = 1024;
309 int offset = 0;
310 int delivered = 0;
311 char filename[FILE_PATH_SIZE];
312 uint8_t got[1024] = {0x00};
313
314 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
315
316 /* If no length given save entire legic read buffer */
317 /* round up to nearest 8 bytes so the saved data can be used with legicload */
318 if (requested == 0)
319 requested = 1024;
320
321 if (requested % 8 != 0) {
322 int remainder = requested % 8;
323 requested = requested + 8 - remainder;
324 }
325
326 if (offset + requested > sizeof(got)) {
327 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
328 return 0;
329 }
330
331 FILE *f = fopen(filename, "w");
332 if(!f) {
333 PrintAndLog("couldn't open '%s'", Cmd+1);
334 return -1;
335 }
336
337 GetFromBigBuf(got, requested, offset);
338 WaitForResponse(CMD_ACK, NULL);
339
340 for (int j = 0; j < requested; j += 8) {
341 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
342 got[j+0], got[j+1], got[j+2], got[j+3],
343 got[j+4], got[j+5], got[j+6], got[j+7]
344 );
345 delivered += 8;
346 if (delivered >= requested) break;
347 }
348
349 fclose(f);
350 PrintAndLog("saved %u samples", delivered);
351 return 0;
352 }
353
354 int CmdLegicRfSim(const char *Cmd) {
355 UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
356 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
357 clearCommandBuffer();
358 SendCommand(&c);
359 return 0;
360 }
361
362 int CmdLegicRfWrite(const char *Cmd) {
363 UsbCommand c = {CMD_WRITER_LEGIC_RF};
364 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]);
365 if(res != 2) {
366 PrintAndLog("Please specify the offset and length as two hex strings");
367 return -1;
368 }
369 clearCommandBuffer();
370 SendCommand(&c);
371 return 0;
372 }
373
374 int CmdLegicRfFill(const char *Cmd) {
375 UsbCommand cmd = {CMD_WRITER_LEGIC_RF};
376 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
377 if(res != 3) {
378 PrintAndLog("Please specify the offset, length and value as two hex strings");
379 return -1;
380 }
381
382 int i;
383 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
384 for(i = 0; i < 48; i++) {
385 c.d.asBytes[i] = cmd.arg[2];
386 }
387
388 for(i = 0; i < 22; i++) {
389 c.arg[0] = i*48;
390 SendCommand(&c);
391 WaitForResponse(CMD_ACK,NULL);
392 }
393 clearCommandBuffer();
394 SendCommand(&cmd);
395 return 0;
396 }
397
398 int CmdLegicCalcCrc8(const char *Cmd){
399
400 int len = strlen(Cmd);
401 if (len & 1 ) return usage_legic_calccrc8();
402
403 uint8_t *data = malloc(len);
404 if ( data == NULL ) return 1;
405
406 param_gethex(Cmd, 0, data, len );
407
408 uint32_t checksum = CRC8Legic(data, len/2);
409 PrintAndLog("Bytes: %s || CRC8: %X", sprint_hex(data, len/2), checksum );
410 free(data);
411 return 0;
412 }
413
414 static command_t CommandTable[] = {
415 {"help", CmdHelp, 1, "This help"},
416 {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
417 {"read", CmdLegicRFRead, 0, "[offset][length] -- read bytes from a LEGIC card"},
418 {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"},
419 {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"},
420 {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
421 {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
422 {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
423 {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"},
424 {NULL, NULL, 0, NULL}
425 };
426
427 int CmdHFLegic(const char *Cmd) {
428 clearCommandBuffer();
429 CmdsParse(CommandTable, Cmd);
430 return 0;
431 }
432
433 int CmdHelp(const char *Cmd) {
434 CmdsHelp(CommandTable);
435 return 0;
436 }
Impressum, Datenschutz