]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhflegic.c
FIX: minor adjustments to 'lf awid bruteforce'
[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//-----------------------------------------------------------------------------
7fe9b0b7 10#include "cmdhflegic.h"
3e134b4c 11
7fe9b0b7 12static int CmdHelp(const char *Cmd);
13
3b920280 14int usage_legic_calccrc8(void){
3e134b4c 15 PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes.");
3b920280 16 PrintAndLog("There must be an even number of hexsymbols as input.");
514ddaa2 17 PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc> c <crc type>");
18 PrintAndLog("Options:");
3e134b4c 19 PrintAndLog(" b <hexbytes> : hex bytes");
20 PrintAndLog(" u <uidcrc> : MCC hexbyte");
514ddaa2 21 PrintAndLog(" c <crc type> : 8|16 bit crc size");
3b920280 22 PrintAndLog("");
514ddaa2 23 PrintAndLog("Samples:");
3e134b4c 24 PrintAndLog(" hf legic crc8 b deadbeef1122");
514ddaa2 25 PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A c 16");
3b920280 26 return 0;
27}
28
e579e768 29int usage_legic_load(void){
30 PrintAndLog("It loads datasamples from the file `filename` to device memory");
31 PrintAndLog("Usage: hf legic load <file name>");
514ddaa2 32 PrintAndLog("");
33 PrintAndLog("Samples:");
34 PrintAndLog(" hf legic load filename");
e579e768 35 return 0;
36}
37
cbdcc89a 38int usage_legic_read(void){
39 PrintAndLog("Read data from a legic tag.");
40 PrintAndLog("Usage: hf legic read <offset> <num of bytes>");
514ddaa2 41 PrintAndLog("Options:");
cbdcc89a 42 PrintAndLog(" <offset> : offset in data array to start download from");
43 PrintAndLog(" <num of bytes> : number of bytes to download");
44 PrintAndLog("");
514ddaa2 45 PrintAndLog("Samples:");
46 PrintAndLog(" hf legic read");
cbdcc89a 47 return 0;
48}
49
e8fecd72 50int usage_legic_sim(void){
51 return 0;
52}
53int usage_legic_rawwrite(void){
54 return 0;
55}
56int usage_legic_fill(void){
57 return 0;
58}
59
669c1b80 60/*
61 * Output BigBuf and deobfuscate LEGIC RF tag data.
cbdcc89a 62 * This is based on information given in the talk held
669c1b80 63 * by Henryk Ploetz and Karsten Nohl at 26c3
669c1b80 64 */
3b920280 65int CmdLegicDecode(const char *Cmd) {
e8fecd72 66
67 int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0;
68 int crc = 0, wrp = 0, wrc = 0;
60bb5ef7 69 uint8_t stamp_len = 0;
e8fecd72 70 uint8_t data_buf[1052]; // receiver buffer
71 char token_type[5] = {0,0,0,0,0};
72 int dcf = 0;
3e134b4c 73 int bIsSegmented = 0;
52cf34c1 74
cbdcc89a 75 // download EML memory, where the "legic read" command puts the data.
76 GetEMLFromBigBuf(data_buf, sizeof(data_buf), 0);
f7f844d0 77 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
78 PrintAndLog("Command execute timeout");
79 return 1;
80 }
81
52cf34c1 82 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
3b920280 83 crc = data_buf[4];
84 uint32_t calc_crc = CRC8Legic(data_buf, 4);
85
52cf34c1 86 PrintAndLog("\nCDF: System Area");
aacb96d7 87 PrintAndLog("------------------------------------------------------");
3b920280 88 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
52cf34c1 89 data_buf[0],
90 data_buf[1],
91 data_buf[2],
92 data_buf[3],
3b920280 93 data_buf[4],
e579e768 94 (calc_crc == crc) ? "OK":"Fail"
52cf34c1 95 );
669c1b80 96
3e134b4c 97
98 token_type[0] = 0;
99 dcf = ((int)data_buf[6] << 8) | (int)data_buf[5];
100
101 // New unwritten media?
102 if(dcf == 0xFFFF) {
103
104 PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)",
105 dcf,
106 data_buf[5],
107 data_buf[6]
108 );
109
110 } else if(dcf > 60000) { // Master token?
111
112 int fl = 0;
113
114 if(data_buf[6] == 0xec) {
115 strncpy(token_type, "XAM", sizeof(token_type));
116 fl = 1;
117 stamp_len = 0x0c - (data_buf[5] >> 4);
118 } else {
e8fecd72 119 switch (data_buf[5] & 0x7f) {
120 case 0x00 ... 0x2f:
121 strncpy(token_type, "IAM", sizeof(token_type));
122 fl = (0x2f - (data_buf[5] & 0x7f)) + 1;
123 break;
124 case 0x30 ... 0x6f:
125 strncpy(token_type, "SAM", sizeof(token_type));
126 fl = (0x6f - (data_buf[5] & 0x7f)) + 1;
127 break;
128 case 0x70 ... 0x7f:
129 strncpy(token_type, "GAM", sizeof(token_type));
130 fl = (0x7f - (data_buf[5] & 0x7f)) + 1;
131 break;
132 }
52cf34c1 133
e8fecd72 134 stamp_len = 0xfc - data_buf[6];
3e134b4c 135 }
52cf34c1 136
3e134b4c 137 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u",
138 dcf,
e8fecd72 139 data_buf[5],
140 data_buf[6],
141 token_type,
142 (data_buf[5] & 0x80 )>> 7,
3e134b4c 143 stamp_len,
144 fl
e8fecd72 145 );
52cf34c1 146
e8fecd72 147 } else { // Is IM(-S) type of card...
3e134b4c 148
149 if(data_buf[7] == 0x9F && data_buf[8] == 0xFF) {
150 bIsSegmented = 1;
151 strncpy(token_type, "IM-S", sizeof(token_type));
152 } else {
153 strncpy(token_type, "IM", sizeof(token_type));
154 }
155
156 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)",
157 dcf,
158 data_buf[5],
159 data_buf[6],
160 token_type,
e8fecd72 161 (data_buf[5]&0x80) >> 7
3e134b4c 162 );
163 }
164
165 // Makes no sence to show this on blank media...
166 if(dcf != 0xFFFF) {
167
168 if(bIsSegmented) {
169 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x",
e8fecd72 170 data_buf[7] & 0x0f,
171 (data_buf[7] & 0x70) >> 4,
172 (data_buf[7] & 0x80) >> 7,
173 data_buf[8]
174 );
3e134b4c 175 }
52cf34c1 176
3e134b4c 177 // Header area is only available on IM-S cards, on master tokens this data is the master token data itself
178 if(bIsSegmented || dcf > 60000) {
179 if(dcf > 60000) {
180 PrintAndLog("Master token data");
181 PrintAndLog("%s", sprint_hex(data_buf+8, 14));
182 } else {
e8fecd72 183 PrintAndLog("Remaining Header Area");
184 PrintAndLog("%s", sprint_hex(data_buf+9, 13));
3e134b4c 185 }
186 }
187 }
d7fd9084 188
e8fecd72 189 uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0};
e579e768 190 uint32_t segCalcCRC = 0;
191 uint32_t segCRC = 0;
d7fd9084 192
3e134b4c 193 // Data card?
194 if(dcf <= 60000) {
cbdcc89a 195
e8fecd72 196 PrintAndLog("\nADF: User Area");
197 PrintAndLog("------------------------------------------------------");
3e134b4c 198
199 if(bIsSegmented) {
200
201 // Data start point on segmented cards
e8fecd72 202 i = 22;
3e134b4c 203
204 // decode segments
205 for (segmentNum=1; segmentNum < 128; segmentNum++ )
206 {
26778ea7 207 segment_len = ((data_buf[i+1] ^ crc) & 0x0f) * 256 + (data_buf[i] ^ crc);
208 segment_flag = ((data_buf[i+1] ^ crc) & 0xf0) >> 4;
209 wrp = (data_buf[i+2] ^ crc);
210 wrc = ((data_buf[i+3] ^ crc) & 0x70) >> 4;
e8fecd72 211
212 bool hasWRC = (wrc > 0);
213 bool hasWRP = (wrp > wrc);
214 int wrp_len = (wrp - wrc);
215 int remain_seg_payload_len = (segment_len - wrp - 5);
e579e768 216
e8fecd72 217 // validate segment-crc
218 segCrcBytes[0]=data_buf[0]; //uid0
219 segCrcBytes[1]=data_buf[1]; //uid1
220 segCrcBytes[2]=data_buf[2]; //uid2
221 segCrcBytes[3]=data_buf[3]; //uid3
26778ea7 222 segCrcBytes[4]=(data_buf[i] ^ crc); //hdr0
223 segCrcBytes[5]=(data_buf[i+1] ^ crc); //hdr1
224 segCrcBytes[6]=(data_buf[i+2] ^ crc); //hdr2
225 segCrcBytes[7]=(data_buf[i+3] ^ crc); //hdr3
e8fecd72 226
227 segCalcCRC = CRC8Legic(segCrcBytes, 8);
26778ea7 228 segCRC = data_buf[i+4] ^ crc;
e8fecd72 229
230 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)",
231 segmentNum,
26778ea7 232 data_buf[i] ^ crc,
233 data_buf[i+1] ^ crc,
234 data_buf[i+2] ^ crc,
235 data_buf[i+3] ^ crc,
e8fecd72 236 segment_len,
237 segment_flag,
238 (segment_flag & 0x4) >> 2,
239 (segment_flag & 0x8) >> 3,
240 wrp,
241 wrc,
242 ((data_buf[i+3]^crc) & 0x80) >> 7,
243 segCRC,
244 ( segCRC == segCalcCRC ) ? "OK" : "fail"
245 );
246
247 i += 5;
669c1b80 248
e8fecd72 249 if ( hasWRC ) {
250 PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc);
251 PrintAndLog("\nrow | data");
252 PrintAndLog("-----+------------------------------------------------");
3e134b4c 253
26778ea7 254 for ( k=i; k < (i + wrc); ++k)
e8fecd72 255 data_buf[k] ^= crc;
3e134b4c 256
e8fecd72 257 print_hex_break( data_buf+i, wrc, 16);
3b920280 258
e8fecd72 259 i += wrc;
260 }
669c1b80 261
e8fecd72 262 if ( hasWRP ) {
263 PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len);
264 PrintAndLog("\nrow | data");
265 PrintAndLog("-----+------------------------------------------------");
d7fd9084 266
3e134b4c 267 for (k=i; k < (i+wrp_len); ++k)
e8fecd72 268 data_buf[k] ^= crc;
3b920280 269
e8fecd72 270 print_hex_break( data_buf+i, wrp_len, 16);
3b920280 271
e8fecd72 272 i += wrp_len;
d7fd9084 273
3e134b4c 274 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
e8fecd72 275 if( wrp_len == 8 )
276 PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc);
277 }
669c1b80 278
e8fecd72 279 PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len);
280 PrintAndLog("\nrow | data");
281 PrintAndLog("-----+------------------------------------------------");
3e134b4c 282
283 for ( k=i; k < (i+remain_seg_payload_len); ++k)
e8fecd72 284 data_buf[k] ^= crc;
3b920280 285
e8fecd72 286 print_hex_break( data_buf+i, remain_seg_payload_len, 16);
669c1b80 287
e8fecd72 288 i += remain_seg_payload_len;
d7fd9084 289
e8fecd72 290 PrintAndLog("-----+------------------------------------------------\n");
a182a680 291
e8fecd72 292 // end with last segment
293 if (segment_flag & 0x8) return 0;
52cf34c1 294
e8fecd72 295 } // end for loop
3e134b4c 296
297 } else {
298
299 // Data start point on unsegmented cards
300 i = 8;
301
e8fecd72 302 wrp = data_buf[7] & 0x0F;
26778ea7 303 wrc = (data_buf[7] & 0x70) >> 4;
3e134b4c 304
305 bool hasWRC = (wrc > 0);
306 bool hasWRP = (wrp > wrc);
307 int wrp_len = (wrp - wrc);
308 int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!?
309
310 PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u",
311 wrp,
312 wrc,
313 (data_buf[7] & 0x80) >> 7
314 );
315
316 if ( hasWRC ) {
317 PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc);
318 PrintAndLog("\nrow | data");
319 PrintAndLog("-----+------------------------------------------------");
320 print_hex_break( data_buf+i, wrc, 16);
321 i += wrc;
322 }
323
324 if ( hasWRP ) {
325 PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len);
326 PrintAndLog("\nrow | data");
327 PrintAndLog("-----+------------------------------------------------");
e8fecd72 328 print_hex_break( data_buf + i, wrp_len, 16);
3e134b4c 329 i += wrp_len;
330
331 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
332 if( wrp_len == 8 )
333 PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4], data_buf[i-3], data_buf[i-2]);
334 }
335
336 PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len);
337 PrintAndLog("\nrow | data");
338 PrintAndLog("-----+------------------------------------------------");
e8fecd72 339 print_hex_break( data_buf + i, remain_seg_payload_len, 16);
3e134b4c 340 i += remain_seg_payload_len;
341
342 PrintAndLog("-----+------------------------------------------------\n");
343 }
344 }
52cf34c1 345 return 0;
669c1b80 346}
41dab153 347
52cf34c1 348int CmdLegicRFRead(const char *Cmd) {
cbdcc89a 349
350 // params:
351 // offset in data
352 // number of bytes.
353 char cmdp = param_getchar(Cmd, 0);
354 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_read();
355
e8fecd72 356 int byte_count = 0, offset = 0;
52cf34c1 357 sscanf(Cmd, "%i %i", &offset, &byte_count);
358 if(byte_count == 0) byte_count = -1;
359 if(byte_count + offset > 1024) byte_count = 1024 - offset;
360
361 UsbCommand c= {CMD_READER_LEGIC_RF, {offset, byte_count, 0}};
362 clearCommandBuffer();
363 SendCommand(&c);
364 return 0;
41dab153 365}
3612a8a8 366
e8fecd72 367
52cf34c1 368int CmdLegicLoad(const char *Cmd) {
e8fecd72 369
370// iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :)
52cf34c1 371 char cmdp = param_getchar(Cmd, 0);
e579e768 372 if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load();
b915fda3 373
e579e768 374 char filename[FILE_PATH_SIZE] = {0x00};
375 int len = strlen(Cmd);
376
b915fda3 377 if (len > FILE_PATH_SIZE) {
378 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
379 return 0;
380 }
381 memcpy(filename, Cmd, len);
382
383 FILE *f = fopen(filename, "r");
3612a8a8 384 if(!f) {
385 PrintAndLog("couldn't open '%s'", Cmd);
386 return -1;
387 }
52cf34c1 388
389 char line[80];
810f5379 390 int offset = 0;
c6e0a2eb 391 uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
392 int index = 0;
393 int totalbytes = 0;
52cf34c1 394 while ( fgets(line, sizeof(line), f) ) {
3612a8a8 395 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
c6e0a2eb 396 (unsigned int *)&data[index],
397 (unsigned int *)&data[index + 1],
398 (unsigned int *)&data[index + 2],
399 (unsigned int *)&data[index + 3],
400 (unsigned int *)&data[index + 4],
401 (unsigned int *)&data[index + 5],
402 (unsigned int *)&data[index + 6],
403 (unsigned int *)&data[index + 7]);
e7d099dc 404
3612a8a8 405 if(res != 8) {
406 PrintAndLog("Error: could not read samples");
407 fclose(f);
408 return -1;
409 }
c6e0a2eb 410 index += res;
411
412 if ( index == USB_CMD_DATA_SIZE ){
413// PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
414 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
415 memcpy(c.d.asBytes, data, sizeof(data));
416 clearCommandBuffer();
417 SendCommand(&c);
d7fd9084 418 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
419 PrintAndLog("Command execute timeout");
420 fclose(f);
421 return 1;
422 }
c6e0a2eb 423 offset += index;
424 totalbytes += index;
425 index = 0;
426 }
3612a8a8 427 }
428 fclose(f);
c6e0a2eb 429
430 // left over bytes?
431 if ( index != 0 ) {
432 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
433 memcpy(c.d.asBytes, data, 8);
434 clearCommandBuffer();
435 SendCommand(&c);
d7fd9084 436 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
437 PrintAndLog("Command execute timeout");
438 return 1;
439 }
c6e0a2eb 440 totalbytes += index;
441 }
442
443 PrintAndLog("loaded %u samples", totalbytes);
3612a8a8 444 return 0;
445}
446
52cf34c1 447int CmdLegicSave(const char *Cmd) {
448 int requested = 1024;
449 int offset = 0;
450 int delivered = 0;
e8fecd72 451 char filename[FILE_PATH_SIZE] = {0x00};
52cf34c1 452 uint8_t got[1024] = {0x00};
453
e8fecd72 454 memset(filename, 0, FILE_PATH_SIZE);
455
52cf34c1 456 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
457
458 /* If no length given save entire legic read buffer */
459 /* round up to nearest 8 bytes so the saved data can be used with legicload */
e7d099dc 460 if (requested == 0)
52cf34c1 461 requested = 1024;
52cf34c1 462
463 if (requested % 8 != 0) {
464 int remainder = requested % 8;
465 requested = requested + 8 - remainder;
466 }
467
468 if (offset + requested > sizeof(got)) {
469 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
470 return 0;
471 }
472
d7fd9084 473 GetFromBigBuf(got, requested, offset);
f7f844d0 474 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
aacb96d7 475 PrintAndLog("Command execute timeout");
f7f844d0 476 return 1;
477 }
52cf34c1 478
aacb96d7 479 FILE *f = fopen(filename, "w");
480 if(!f) {
481 PrintAndLog("couldn't open '%s'", Cmd+1);
482 return -1;
483 }
484
52cf34c1 485 for (int j = 0; j < requested; j += 8) {
486 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
c6e0a2eb 487 got[j+0], got[j+1], got[j+2], got[j+3],
488 got[j+4], got[j+5], got[j+6], got[j+7]
52cf34c1 489 );
490 delivered += 8;
491 if (delivered >= requested) break;
492 }
493
494 fclose(f);
495 PrintAndLog("saved %u samples", delivered);
496 return 0;
3612a8a8 497}
498
f7f844d0 499//TODO: write a help text (iceman)
52cf34c1 500int CmdLegicRfSim(const char *Cmd) {
3b920280 501 UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
52cf34c1 502 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
503 clearCommandBuffer();
504 SendCommand(&c);
505 return 0;
3612a8a8 506}
507
f7f844d0 508//TODO: write a help text (iceman)
52cf34c1 509int CmdLegicRfWrite(const char *Cmd) {
e8fecd72 510 UsbCommand c = {CMD_WRITER_LEGIC_RF, {0,0,0}};
125a98a1 511 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]);
3612a8a8 512 if(res != 2) {
513 PrintAndLog("Please specify the offset and length as two hex strings");
514 return -1;
515 }
52cf34c1 516 clearCommandBuffer();
3612a8a8 517 SendCommand(&c);
518 return 0;
519}
520
3e134b4c 521//TODO: write a help text (iceman)
522int CmdLegicRfRawWrite(const char *Cmd) {
523 char answer;
524 UsbCommand c = { CMD_RAW_WRITER_LEGIC_RF, {0,0,0} };
525 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]);
526 if(res != 2) {
527 PrintAndLog("Please specify the offset and value as two hex strings");
528 return -1;
529 }
530
531 if (c.arg[0] == 0x05 || c.arg[0] == 0x06) {
532 PrintAndLog("############# DANGER !! #############");
533 PrintAndLog("# changing the DCF is irreversible #");
534 PrintAndLog("#####################################");
535 PrintAndLog("do youe really want to continue? y(es) n(o)");
6e321dd8 536 if (scanf(" %c", &answer) > 0 && (answer == 'y' || answer == 'Y')) {
3e134b4c 537 SendCommand(&c);
538 return 0;
539 }
540 return -1;
541 }
542
543 clearCommandBuffer();
544 SendCommand(&c);
545 return 0;
546}
547
548//TODO: write a help text (iceman)
52cf34c1 549int CmdLegicRfFill(const char *Cmd) {
3e134b4c 550 UsbCommand cmd = {CMD_WRITER_LEGIC_RF, {0,0,0} };
1a07fd51 551 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
3612a8a8 552 if(res != 3) {
553 PrintAndLog("Please specify the offset, length and value as two hex strings");
554 return -1;
555 }
556
557 int i;
52cf34c1 558 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
ba4ad25b 559 memset(c.d.asBytes, cmd.arg[2], 48);
3e134b4c 560
52cf34c1 561 for(i = 0; i < 22; i++) {
562 c.arg[0] = i*48;
3e134b4c 563
564 clearCommandBuffer();
52cf34c1 565 SendCommand(&c);
3e134b4c 566 WaitForResponse(CMD_ACK, NULL);
52cf34c1 567 }
568 clearCommandBuffer();
3612a8a8 569 SendCommand(&cmd);
570 return 0;
571 }
572
3b920280 573int CmdLegicCalcCrc8(const char *Cmd){
574
cc4c8fd6 575 uint8_t *data = NULL;
3e134b4c 576 uint8_t cmdp = 0, uidcrc = 0, type=0;
577 bool errors = false;
578 int len = 0;
e31a0f73 579 int bg, en;
3b920280 580
3e134b4c 581 while(param_getchar(Cmd, cmdp) != 0x00) {
582 switch(param_getchar(Cmd, cmdp)) {
583 case 'b':
584 case 'B':
e31a0f73
AG
585 // peek at length of the input string so we can
586 // figure out how many elements to malloc in "data"
587 bg=en=0;
987c5984
AG
588 if (param_getptr(Cmd, &bg, &en, cmdp+1)) {
589 errors = true;
590 break;
591 }
e31a0f73
AG
592 len = (en - bg + 1);
593
594 // check that user entered even number of characters
595 // for hex data string
596 if (len & 1) {
597 errors = true;
598 break;
599 }
600
08927081
AG
601 // it's possible for user to accidentally enter "b" parameter
602 // more than once - we have to clean previous malloc
603 if (data) free(data);
e31a0f73 604 data = malloc(len >> 1);
3e134b4c 605 if ( data == NULL ) {
606 PrintAndLog("Can't allocate memory. exiting");
607 errors = true;
608 break;
e31a0f73
AG
609 }
610
987c5984
AG
611 if (param_gethex(Cmd, cmdp+1, data, len)) {
612 errors = true;
613 break;
614 }
3e134b4c 615
616 len >>= 1;
617 cmdp += 2;
618 break;
619 case 'u':
620 case 'U':
621 uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16);
622 cmdp += 2;
623 break;
624 case 'c':
625 case 'C':
626 type = param_get8ex(Cmd, cmdp+1, 0, 10);
627 cmdp += 2;
628 break;
629 case 'h':
630 case 'H':
631 errors = true;
632 break;
633 default:
634 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
635 errors = true;
636 break;
637 }
638 if (errors) break;
639 }
640 //Validations
641 if (errors){
aeb128e2 642 if (data) free(data);
3e134b4c 643 return usage_legic_calccrc8();
644 }
645
646 switch (type){
647 case 16:
648 PrintAndLog("LEGIC CRC16: %X", CRC16Legic(data, len, uidcrc));
649 break;
650 default:
651 PrintAndLog("LEGIC CRC8: %X", CRC8Legic(data, len) );
652 break;
eb5206bd 653 }
3b920280 654
aeb128e2 655 if (data) free(data);
3b920280 656 return 0;
657}
658
52cf34c1 659static command_t CommandTable[] = {
3b920280 660 {"help", CmdHelp, 1, "This help"},
661 {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
662 {"read", CmdLegicRFRead, 0, "[offset][length] -- read bytes from a LEGIC card"},
663 {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"},
664 {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"},
665 {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
666 {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
3e134b4c 667 {"writeRaw",CmdLegicRfRawWrite, 0, "<address> <value> -- Write direct to address"},
3b920280 668 {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
669 {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"},
52cf34c1 670 {NULL, NULL, 0, NULL}
671};
672
673int CmdHFLegic(const char *Cmd) {
674 clearCommandBuffer();
675 CmdsParse(CommandTable, Cmd);
676 return 0;
677}
678
679int CmdHelp(const char *Cmd) {
680 CmdsHelp(CommandTable);
681 return 0;
682}
Impressum, Datenschutz