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