]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhflegic.c
CHG: revert legiccrc8 to old algo.
[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 #include "cmdhflegic.h"
11
12 static int CmdHelp(const char *Cmd);
13
14 #define MAX_LENGTH 1024
15
16 int usage_legic_calccrc8(void){
17 PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes.");
18 PrintAndLog("There must be an even number of hexsymbols as input.");
19 PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc> c <crc type>");
20 PrintAndLog("Options:");
21 PrintAndLog(" h : this help");
22 PrintAndLog(" b <hexbytes> : hex bytes");
23 PrintAndLog(" u <uidcrc> : MCC hexbyte");
24 PrintAndLog(" c <crc type> : 8|16 bit crc size");
25 PrintAndLog("");
26 PrintAndLog("Samples:");
27 PrintAndLog(" hf legic crc8 b deadbeef1122");
28 PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A c 16");
29 return 0;
30 }
31 int usage_legic_load(void){
32 PrintAndLog("It loads datasamples from the file `filename` to device memory");
33 PrintAndLog("Usage: hf legic load [h] <file name>");
34 PrintAndLog("Options:");
35 PrintAndLog(" h : this help");
36 PrintAndLog(" <filename> : Name of file to load");
37 PrintAndLog("");
38 PrintAndLog("Samples:");
39 PrintAndLog(" hf legic load filename");
40 return 0;
41 }
42 int usage_legic_read(void){
43 PrintAndLog("Read data from a legic tag.");
44 PrintAndLog("Usage: hf legic read [h] <offset> <length> <IV>");
45 PrintAndLog("Options:");
46 PrintAndLog(" h : this help");
47 PrintAndLog(" <offset> : offset in data array to start download from");
48 PrintAndLog(" <length> : number of bytes to download");
49 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
50 PrintAndLog("");
51 PrintAndLog("Samples:");
52 PrintAndLog(" hf legic read");
53 PrintAndLog(" hf legic read 10 4");
54 return 0;
55 }
56 int usage_legic_sim(void){
57 PrintAndLog("Missing help text.");
58 return 0;
59 }
60 int usage_legic_write(void){
61 PrintAndLog(" Write sample buffer to a legic tag. (use after load or read)");
62 PrintAndLog("Usage: hf legic write [h] <offset> <length> <IV>");
63 PrintAndLog("Options:");
64 PrintAndLog(" h : this help");
65 PrintAndLog(" <offset> : offset in data array to start writing from");
66 PrintAndLog(" <length> : number of bytes to write");
67 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
68 PrintAndLog("");
69 PrintAndLog("Samples:");
70 PrintAndLog(" hf legic write");
71 PrintAndLog(" hf legic write 10 4");
72 return 0;
73 }
74 int usage_legic_rawwrite(void){
75 PrintAndLog("Write raw data direct to a specific address on legic tag.");
76 PrintAndLog("Usage: hf legic writeraw [h] <address> <value> <IV>");
77 PrintAndLog("Options:");
78 PrintAndLog(" h : this help");
79 PrintAndLog(" <address> : address to write to");
80 PrintAndLog(" <value> : value to write");
81 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
82 PrintAndLog("");
83 PrintAndLog("Samples:");
84 PrintAndLog(" hf legic writeraw");
85 PrintAndLog(" hf legic writeraw 10 4");
86 return 0;
87 }
88 int usage_legic_fill(void){
89 PrintAndLog("Missing help text.");
90 return 0;
91 }
92 int usage_legic_info(void){
93 PrintAndLog("Read info from a legic tag.");
94 PrintAndLog("Usage: hf legic info [h]");
95 PrintAndLog("Options:");
96 PrintAndLog(" h : this help");
97 PrintAndLog("");
98 PrintAndLog("Samples:");
99 PrintAndLog(" hf legic info");
100 return 0;
101 }
102 /*
103 * Output BigBuf and deobfuscate LEGIC RF tag data.
104 * This is based on information given in the talk held
105 * by Henryk Ploetz and Karsten Nohl at 26c3
106 */
107 int CmdLegicDecode(const char *Cmd) {
108
109 int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0;
110 int crc = 0, wrp = 0, wrc = 0;
111 uint8_t stamp_len = 0;
112 uint8_t data[1024]; // receiver buffer
113 char token_type[5] = {0,0,0,0,0};
114 int dcf = 0;
115 int bIsSegmented = 0;
116
117 // copy data from device
118 GetEMLFromBigBuf(data, sizeof(data), 0);
119 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
120 PrintAndLog("Command execute timeout");
121 return 1;
122 }
123
124 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
125 crc = data[4];
126 uint32_t calc_crc = CRC8Legic(data, 4);
127
128 PrintAndLog("\nCDF: System Area");
129 PrintAndLog("------------------------------------------------------");
130 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
131 data[0],
132 data[1],
133 data[2],
134 data[3],
135 data[4],
136 (calc_crc == crc) ? "OK":"Fail"
137 );
138
139
140 token_type[0] = 0;
141 dcf = ((int)data[6] << 8) | (int)data[5];
142
143 // New unwritten media?
144 if(dcf == 0xFFFF) {
145
146 PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)",
147 dcf,
148 data[5],
149 data[6]
150 );
151
152 } else if(dcf > 60000) { // Master token?
153
154 int fl = 0;
155
156 if(data[6] == 0xec) {
157 strncpy(token_type, "XAM", sizeof(token_type));
158 fl = 1;
159 stamp_len = 0x0c - (data[5] >> 4);
160 } else {
161 switch (data[5] & 0x7f) {
162 case 0x00 ... 0x2f:
163 strncpy(token_type, "IAM", sizeof(token_type));
164 fl = (0x2f - (data[5] & 0x7f)) + 1;
165 break;
166 case 0x30 ... 0x6f:
167 strncpy(token_type, "SAM", sizeof(token_type));
168 fl = (0x6f - (data[5] & 0x7f)) + 1;
169 break;
170 case 0x70 ... 0x7f:
171 strncpy(token_type, "GAM", sizeof(token_type));
172 fl = (0x7f - (data[5] & 0x7f)) + 1;
173 break;
174 }
175
176 stamp_len = 0xfc - data[6];
177 }
178
179 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u",
180 dcf,
181 data[5],
182 data[6],
183 token_type,
184 (data[5] & 0x80 )>> 7,
185 stamp_len,
186 fl
187 );
188
189 } else { // Is IM(-S) type of card...
190
191 if(data[7] == 0x9F && data[8] == 0xFF) {
192 bIsSegmented = 1;
193 strncpy(token_type, "IM-S", sizeof(token_type));
194 } else {
195 strncpy(token_type, "IM", sizeof(token_type));
196 }
197
198 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)",
199 dcf,
200 data[5],
201 data[6],
202 token_type,
203 (data[5]&0x80) >> 7
204 );
205 }
206
207 // Makes no sence to show this on blank media...
208 if(dcf != 0xFFFF) {
209
210 if(bIsSegmented) {
211 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x",
212 data[7] & 0x0f,
213 (data[7] & 0x70) >> 4,
214 (data[7] & 0x80) >> 7,
215 data[8]
216 );
217 }
218
219 // Header area is only available on IM-S cards, on master tokens this data is the master token data itself
220 if(bIsSegmented || dcf > 60000) {
221 if(dcf > 60000) {
222 PrintAndLog("Master token data");
223 PrintAndLog("%s", sprint_hex(data+8, 14));
224 } else {
225 PrintAndLog("Remaining Header Area");
226 PrintAndLog("%s", sprint_hex(data+9, 13));
227 }
228 }
229 }
230
231 uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0};
232 uint32_t segCalcCRC = 0;
233 uint32_t segCRC = 0;
234
235 // Data card?
236 if(dcf <= 60000) {
237
238 PrintAndLog("\nADF: User Area");
239 PrintAndLog("------------------------------------------------------");
240
241 if(bIsSegmented) {
242
243 // Data start point on segmented cards
244 i = 22;
245
246 // decode segments
247 for (segmentNum=1; segmentNum < 128; segmentNum++ )
248 {
249 segment_len = ((data[i+1] ^ crc) & 0x0f) * 256 + (data[i] ^ crc);
250 segment_flag = ((data[i+1] ^ crc) & 0xf0) >> 4;
251 wrp = (data[i+2] ^ crc);
252 wrc = ((data[i+3] ^ crc) & 0x70) >> 4;
253
254 bool hasWRC = (wrc > 0);
255 bool hasWRP = (wrp > wrc);
256 int wrp_len = (wrp - wrc);
257 int remain_seg_payload_len = (segment_len - wrp - 5);
258
259 // validate segment-crc
260 segCrcBytes[0]=data[0]; //uid0
261 segCrcBytes[1]=data[1]; //uid1
262 segCrcBytes[2]=data[2]; //uid2
263 segCrcBytes[3]=data[3]; //uid3
264 segCrcBytes[4]=(data[i] ^ crc); //hdr0
265 segCrcBytes[5]=(data[i+1] ^ crc); //hdr1
266 segCrcBytes[6]=(data[i+2] ^ crc); //hdr2
267 segCrcBytes[7]=(data[i+3] ^ crc); //hdr3
268
269 segCalcCRC = CRC8Legic(segCrcBytes, 8);
270 segCRC = data[i+4] ^ crc;
271
272 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)",
273 segmentNum,
274 data[i] ^ crc,
275 data[i+1] ^ crc,
276 data[i+2] ^ crc,
277 data[i+3] ^ crc,
278 segment_len,
279 segment_flag,
280 (segment_flag & 0x4) >> 2,
281 (segment_flag & 0x8) >> 3,
282 wrp,
283 wrc,
284 ((data[i+3]^crc) & 0x80) >> 7,
285 segCRC,
286 ( segCRC == segCalcCRC ) ? "OK" : "fail"
287 );
288
289 i += 5;
290
291 if ( hasWRC ) {
292 PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc);
293 PrintAndLog("\nrow | data");
294 PrintAndLog("-----+------------------------------------------------");
295
296 for ( k=i; k < (i + wrc); ++k)
297 data[k] ^= crc;
298
299 print_hex_break( data+i, wrc, 16);
300
301 i += wrc;
302 }
303
304 if ( hasWRP ) {
305 PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len);
306 PrintAndLog("\nrow | data");
307 PrintAndLog("-----+------------------------------------------------");
308
309 for (k=i; k < (i+wrp_len); ++k)
310 data[k] ^= crc;
311
312 print_hex_break( data+i, wrp_len, 16);
313
314 i += wrp_len;
315
316 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
317 if( wrp_len == 8 )
318 PrintAndLog("Card ID: %2X%02X%02X", data[i-4]^crc, data[i-3]^crc, data[i-2]^crc);
319 }
320
321 PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len);
322 PrintAndLog("\nrow | data");
323 PrintAndLog("-----+------------------------------------------------");
324
325 for ( k=i; k < (i+remain_seg_payload_len); ++k)
326 data[k] ^= crc;
327
328 print_hex_break( data+i, remain_seg_payload_len, 16);
329
330 i += remain_seg_payload_len;
331
332 PrintAndLog("-----+------------------------------------------------\n");
333
334 // end with last segment
335 if (segment_flag & 0x8) return 0;
336
337 } // end for loop
338
339 } else {
340
341 // Data start point on unsegmented cards
342 i = 8;
343
344 wrp = data[7] & 0x0F;
345 wrc = (data[7] & 0x70) >> 4;
346
347 bool hasWRC = (wrc > 0);
348 bool hasWRP = (wrp > wrc);
349 int wrp_len = (wrp - wrc);
350 int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!?
351
352 PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u",
353 wrp,
354 wrc,
355 (data[7] & 0x80) >> 7
356 );
357
358 if ( hasWRC ) {
359 PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc);
360 PrintAndLog("\nrow | data");
361 PrintAndLog("-----+------------------------------------------------");
362 print_hex_break( data+i, wrc, 16);
363 i += wrc;
364 }
365
366 if ( hasWRP ) {
367 PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len);
368 PrintAndLog("\nrow | data");
369 PrintAndLog("-----+------------------------------------------------");
370 print_hex_break( data + i, wrp_len, 16);
371 i += wrp_len;
372
373 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
374 if( wrp_len == 8 )
375 PrintAndLog("Card ID: %2X%02X%02X", data[i-4], data[i-3], data[i-2]);
376 }
377
378 PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len);
379 PrintAndLog("\nrow | data");
380 PrintAndLog("-----+------------------------------------------------");
381 print_hex_break( data + i, remain_seg_payload_len, 16);
382 i += remain_seg_payload_len;
383
384 PrintAndLog("-----+------------------------------------------------\n");
385 }
386 }
387 return 0;
388 }
389
390 int CmdLegicRFRead(const char *Cmd) {
391
392 // params:
393 // offset in data memory
394 // number of bytes to read
395 char cmdp = param_getchar(Cmd, 0);
396 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_read();
397
398 uint32_t offset = 0, len = 0, IV = 1;
399 sscanf(Cmd, "%x %x %x", &offset, &len, &IV);
400
401 // OUT-OF-BOUNDS check
402 if ( len + offset > MAX_LENGTH ) {
403 len = MAX_LENGTH - offset;
404 PrintAndLog("Out-of-bound, shorten len to %d", len);
405 }
406
407 if ( (IV & 0x7F) != IV ){
408 IV &= 0x7F;
409 PrintAndLog("Truncating IV to 7bits");
410 }
411
412 if ( (IV & 1) == 0 ){
413 IV |= 0x01;
414 PrintAndLog("LSB of IV must be SET");
415 }
416
417 //PrintAndLog("Using IV: 0x%02x", IV);
418
419 UsbCommand c = {CMD_READER_LEGIC_RF, {offset, len, IV}};
420 clearCommandBuffer();
421 SendCommand(&c);
422 UsbCommand resp;
423 if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
424 uint8_t isOK = resp.arg[0] & 0xFF;
425 uint16_t len = resp.arg[1] & 0x3FF;
426 if ( isOK ) {
427
428 uint8_t *data = malloc(len);
429 if ( !data ){
430 PrintAndLog("Cannot allocate memory");
431 return 2;
432 }
433 // copy data from device
434 GetEMLFromBigBuf(data, len, 0);
435 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
436 PrintAndLog("Command execute timeout");
437 if ( data )
438 free(data);
439 return 1;
440 }
441
442 PrintAndLog("\nData");
443 PrintAndLog("-----------------------------");
444 print_hex_break( data, len, 32);
445 } else {
446 PrintAndLog("failed reading tag");
447 }
448 } else {
449 PrintAndLog("command execution time out");
450 return 1;
451 }
452 return 0;
453 }
454
455 int CmdLegicLoad(const char *Cmd) {
456
457 // iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :)
458 char cmdp = param_getchar(Cmd, 0);
459 if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load();
460
461 char filename[FILE_PATH_SIZE] = {0x00};
462 int len = strlen(Cmd);
463
464 if (len > FILE_PATH_SIZE) {
465 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
466 return 0;
467 }
468 memcpy(filename, Cmd, len);
469
470 FILE *f = fopen(filename, "r");
471 if(!f) {
472 PrintAndLog("couldn't open '%s'", Cmd);
473 return -1;
474 }
475
476 char line[80];
477 int offset = 0;
478 uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
479 int index = 0;
480 int totalbytes = 0;
481 while ( fgets(line, sizeof(line), f) ) {
482 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
483 (unsigned int *)&data[index],
484 (unsigned int *)&data[index + 1],
485 (unsigned int *)&data[index + 2],
486 (unsigned int *)&data[index + 3],
487 (unsigned int *)&data[index + 4],
488 (unsigned int *)&data[index + 5],
489 (unsigned int *)&data[index + 6],
490 (unsigned int *)&data[index + 7]);
491
492 if(res != 8) {
493 PrintAndLog("Error: could not read samples");
494 fclose(f);
495 return -1;
496 }
497 index += res;
498
499 if ( index == USB_CMD_DATA_SIZE ){
500 // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
501 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
502 memcpy(c.d.asBytes, data, sizeof(data));
503 clearCommandBuffer();
504 SendCommand(&c);
505 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
506 PrintAndLog("Command execute timeout");
507 fclose(f);
508 return 1;
509 }
510 offset += index;
511 totalbytes += index;
512 index = 0;
513 }
514 }
515 fclose(f);
516
517 // left over bytes?
518 if ( index != 0 ) {
519 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
520 memcpy(c.d.asBytes, data, 8);
521 clearCommandBuffer();
522 SendCommand(&c);
523 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
524 PrintAndLog("Command execute timeout");
525 return 1;
526 }
527 totalbytes += index;
528 }
529
530 PrintAndLog("loaded %u samples", totalbytes);
531 return 0;
532 }
533
534 int CmdLegicSave(const char *Cmd) {
535 int requested = 1024;
536 int offset = 0;
537 int delivered = 0;
538 char filename[FILE_PATH_SIZE] = {0x00};
539 uint8_t got[1024] = {0x00};
540
541 memset(filename, 0, FILE_PATH_SIZE);
542
543 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
544
545 /* If no length given save entire legic read buffer */
546 /* round up to nearest 8 bytes so the saved data can be used with legicload */
547 if (requested == 0)
548 requested = 1024;
549
550 if (requested % 8 != 0) {
551 int remainder = requested % 8;
552 requested = requested + 8 - remainder;
553 }
554
555 if (offset + requested > sizeof(got)) {
556 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
557 return 0;
558 }
559
560 GetFromBigBuf(got, requested, offset);
561 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
562 PrintAndLog("Command execute timeout");
563 return 1;
564 }
565
566 FILE *f = fopen(filename, "w");
567 if(!f) {
568 PrintAndLog("couldn't open '%s'", Cmd+1);
569 return -1;
570 }
571
572 for (int j = 0; j < requested; j += 8) {
573 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
574 got[j+0], got[j+1], got[j+2], got[j+3],
575 got[j+4], got[j+5], got[j+6], got[j+7]
576 );
577 delivered += 8;
578 if (delivered >= requested) break;
579 }
580
581 fclose(f);
582 PrintAndLog("saved %u samples", delivered);
583 return 0;
584 }
585
586 //TODO: write a help text (iceman)
587 int CmdLegicRfSim(const char *Cmd) {
588 UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
589 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
590 clearCommandBuffer();
591 SendCommand(&c);
592 return 0;
593 }
594
595 int CmdLegicRfWrite(const char *Cmd) {
596
597 // params:
598 // offset - in tag memory
599 // length - num of bytes to be written
600 // IV - initialisation vector
601
602 char cmdp = param_getchar(Cmd, 0);
603 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_write();
604
605 uint32_t offset = 0, len = 0, IV = 0;
606
607 UsbCommand c = {CMD_WRITER_LEGIC_RF, {0,0,0}};
608 int res = sscanf(Cmd, "%x %x %x", &offset, &len, &IV);
609 if(res < 2) {
610 PrintAndLog("Please specify the offset and length as two hex strings and, optionally, the IV also as an hex string");
611 return -1;
612 }
613
614 // OUT-OF-BOUNDS check
615 if(len + offset > MAX_LENGTH) len = MAX_LENGTH - offset;
616
617 if ( (IV & 0x7F) != IV ){
618 IV &= 0x7F;
619 PrintAndLog("Truncating IV to 7bits");
620 }
621 if ( (IV & 1) == 0 ){
622 IV |= 0x01; // IV must be odd
623 PrintAndLog("LSB of IV must be SET");
624 }
625
626 PrintAndLog("Current IV: 0x%02x", IV);
627
628 c.arg[0] = offset;
629 c.arg[1] = len;
630 c.arg[2] = IV;
631
632 clearCommandBuffer();
633 SendCommand(&c);
634 return 0;
635 }
636
637 int CmdLegicRfRawWrite(const char *Cmd) {
638
639 char cmdp = param_getchar(Cmd, 0);
640 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_rawwrite();
641
642 uint32_t address = 0, data = 0, IV = 0;
643 char answer;
644
645 UsbCommand c = { CMD_RAW_WRITER_LEGIC_RF, {0,0,0} };
646 int res = sscanf(Cmd, "%x %x %x", &address, &data, &IV);
647 if(res < 2)
648 return usage_legic_rawwrite();
649
650 // OUT-OF-BOUNDS check
651 if(address > MAX_LENGTH)
652 return usage_legic_rawwrite();
653
654 if ( (IV & 0x7F) != IV ){
655 IV &= 0x7F;
656 PrintAndLog("Truncating IV to 7bits");
657 }
658 if ( (IV & 1) == 0 ){
659 IV |= 0x01; // IV must be odd
660 PrintAndLog("LSB of IV must be SET");
661 }
662 PrintAndLog("Current IV: 0x%02x", IV);
663
664 c.arg[0] = address;
665 c.arg[1] = data;
666 c.arg[2] = IV;
667
668 if (c.arg[0] == 0x05 || c.arg[0] == 0x06) {
669 PrintAndLog("############# DANGER !! #############");
670 PrintAndLog("# changing the DCF is irreversible #");
671 PrintAndLog("#####################################");
672 PrintAndLog("do youe really want to continue? y(es) n(o)");
673 if (scanf(" %c", &answer) > 0 && (answer == 'y' || answer == 'Y')) {
674 SendCommand(&c);
675 return 0;
676 }
677 return -1;
678 }
679
680 clearCommandBuffer();
681 SendCommand(&c);
682 return 0;
683 }
684
685 //TODO: write a help text (iceman)
686 int CmdLegicRfFill(const char *Cmd) {
687 UsbCommand cmd = {CMD_WRITER_LEGIC_RF, {0,0,0} };
688 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
689 if(res != 3) {
690 PrintAndLog("Please specify the offset, length and value as two hex strings");
691 return -1;
692 }
693
694 int i;
695 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
696 memset(c.d.asBytes, cmd.arg[2], 48);
697
698 for(i = 0; i < 22; i++) {
699 c.arg[0] = i*48;
700
701 clearCommandBuffer();
702 SendCommand(&c);
703 WaitForResponse(CMD_ACK, NULL);
704 }
705 clearCommandBuffer();
706 SendCommand(&cmd);
707 return 0;
708 }
709
710 void static calc4(uint8_t *cmd, uint8_t len){
711 crc_t crc;
712 //crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, TRUE, TRUE);
713 crc_init(&crc, 4, 0x19 >> 1, 0x5, 0);
714
715 crc_clear(&crc);
716 crc_update(&crc, 1, 1); /* CMD_READ */
717 crc_update(&crc, cmd[0], 8);
718 crc_update(&crc, cmd[1], 8);
719 printf("crc4 %X\n", reflect(crc_finish(&crc), 4) ) ;
720
721 crc_clear(&crc);
722 crc_update(&crc, 1, 1); /* CMD_READ */
723 crc_update(&crc, cmd[0], 8);
724 crc_update(&crc, cmd[1], 8);
725 printf("crc4 %X\n", crc_finish(&crc) ) ;
726
727 printf("---- old ---\n");
728 crc_update2(&crc, 1, 1); /* CMD_READ */
729 crc_update2(&crc, cmd[0], 8);
730 crc_update2(&crc, cmd[1], 8);
731 printf("crc4 %X \n", reflect(crc_finish(&crc), 4) ) ;
732
733
734 crc_clear(&crc);
735 crc_update2(&crc, 1, 1); /* CMD_READ */
736 crc_update2(&crc, cmd[0], 8);
737 crc_update2(&crc, cmd[1], 8);
738 printf("crc4 %X\n", crc_finish(&crc) ) ;
739 }
740
741 int CmdLegicCalcCrc8(const char *Cmd){
742
743 uint8_t *data = NULL;
744 uint8_t cmdp = 0, uidcrc = 0, type=0;
745 bool errors = false;
746 int len = 0;
747 int bg, en;
748
749 while(param_getchar(Cmd, cmdp) != 0x00) {
750 switch(param_getchar(Cmd, cmdp)) {
751 case 'b':
752 case 'B':
753 // peek at length of the input string so we can
754 // figure out how many elements to malloc in "data"
755 bg=en=0;
756 if (param_getptr(Cmd, &bg, &en, cmdp+1)) {
757 errors = true;
758 break;
759 }
760 len = (en - bg + 1);
761
762 // check that user entered even number of characters
763 // for hex data string
764 if (len & 1) {
765 errors = true;
766 break;
767 }
768
769 // it's possible for user to accidentally enter "b" parameter
770 // more than once - we have to clean previous malloc
771 if (data) free(data);
772 data = malloc(len >> 1);
773 if ( data == NULL ) {
774 PrintAndLog("Can't allocate memory. exiting");
775 errors = true;
776 break;
777 }
778
779 if (param_gethex(Cmd, cmdp+1, data, len)) {
780 errors = true;
781 break;
782 }
783
784 len >>= 1;
785 cmdp += 2;
786 break;
787 case 'u':
788 case 'U':
789 uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16);
790 cmdp += 2;
791 break;
792 case 'c':
793 case 'C':
794 type = param_get8ex(Cmd, cmdp+1, 0, 10);
795 cmdp += 2;
796 break;
797 case 'h':
798 case 'H':
799 errors = true;
800 break;
801 default:
802 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
803 errors = true;
804 break;
805 }
806 if (errors) break;
807 }
808 //Validations
809 if (errors){
810 if (data) free(data);
811 return usage_legic_calccrc8();
812 }
813
814 switch (type){
815 case 16:
816 PrintAndLog("Legic crc16: %X", CRC16Legic(data, len, uidcrc));
817 break;
818 case 4:
819 calc4(data, 0);
820 break;
821 default:
822 PrintAndLog("Legic crc8: %X", CRC8Legic(data, len) );
823 break;
824 }
825
826 if (data) free(data);
827 return 0;
828 }
829
830 int HFLegicInfo(const char *Cmd, bool verbose) {
831
832 char cmdp = param_getchar(Cmd, 0);
833 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_info();
834
835 UsbCommand c = {CMD_LEGIC_INFO, {0,0,0}};
836 clearCommandBuffer();
837 SendCommand(&c);
838 UsbCommand resp;
839 if (!WaitForResponseTimeout(CMD_ACK, &resp, 500)) {
840 if ( verbose ) PrintAndLog("command execution time out");
841 return 1;
842 }
843
844 uint8_t isOK = resp.arg[0] & 0xFF;
845 if ( !isOK ) {
846 if ( verbose ) PrintAndLog("legic card select failed");
847 return 1;
848 }
849
850 legic_card_select_t card;
851 memcpy(&card, (legic_card_select_t *)resp.d.asBytes, sizeof(legic_card_select_t));
852
853 PrintAndLog(" UID : %s", sprint_hex(card.uid, sizeof(card.uid)));
854 switch(card.cardsize) {
855 case 22:
856 case 256:
857 case 1024:
858 PrintAndLog(" TYPE : MIM%d card (%d bytes)", card.cardsize, card.cardsize); break;
859 default: {
860 PrintAndLog("Unknown card format: %d", card.cardsize);
861 return 1;
862 }
863 }
864 return 0;
865 }
866 int CmdLegicInfo(const char *Cmd){
867 return HFLegicInfo(Cmd, TRUE);
868 }
869
870 static command_t CommandTable[] = {
871 {"help", CmdHelp, 1, "This help"},
872 {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
873 {"read", CmdLegicRFRead, 0, "[offset][length] <iv> -- read bytes from a LEGIC card"},
874 {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"},
875 {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"},
876 {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
877 {"write", CmdLegicRfWrite,0, "<offset> <length> <iv> -- Write sample buffer (user after load or read)"},
878 {"writeraw",CmdLegicRfRawWrite, 0, "<address> <value> <iv> -- Write direct to address"},
879 {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
880 {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"},
881 {"info", CmdLegicInfo, 1, "Information"},
882 {NULL, NULL, 0, NULL}
883 };
884
885 int CmdHFLegic(const char *Cmd) {
886 clearCommandBuffer();
887 CmdsParse(CommandTable, Cmd);
888 return 0;
889 }
890
891 int CmdHelp(const char *Cmd) {
892 CmdsHelp(CommandTable);
893 return 0;
894 }
Impressum, Datenschutz