]> git.zerfleddert.de Git - proxmark3-svn/blob - client/mifarehost.c
Improve 'Magic' Mifare tags generation detection & hf mf c* commands magic 4k compati...
[proxmark3-svn] / client / mifarehost.c
1 // Merlok, 2011, 2012
2 // people from mifare@nethemba.com, 2010
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 // mifare commands
9 //-----------------------------------------------------------------------------
10
11 #include "mifarehost.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <pthread.h>
17
18 #include "crapto1/crapto1.h"
19 #include "proxmark3.h"
20 #include "usb_cmd.h"
21 #include "cmdmain.h"
22 #include "ui.h"
23 #include "util.h"
24 #include "iso14443crc.h"
25
26 #include "mifare.h"
27
28 // mifare tracer flags used in mfTraceDecode()
29 #define TRACE_IDLE 0x00
30 #define TRACE_AUTH1 0x01
31 #define TRACE_AUTH2 0x02
32 #define TRACE_AUTH_OK 0x03
33 #define TRACE_READ_DATA 0x04
34 #define TRACE_WRITE_OK 0x05
35 #define TRACE_WRITE_DATA 0x06
36 #define TRACE_ERROR 0xFF
37
38
39 static int compare_uint64(const void *a, const void *b) {
40 // didn't work: (the result is truncated to 32 bits)
41 //return (*(int64_t*)b - *(int64_t*)a);
42
43 // better:
44 if (*(uint64_t*)b == *(uint64_t*)a) return 0;
45 else if (*(uint64_t*)b < *(uint64_t*)a) return 1;
46 else return -1;
47 }
48
49
50 // create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned.
51 static uint32_t intersection(uint64_t *list1, uint64_t *list2)
52 {
53 if (list1 == NULL || list2 == NULL) {
54 return 0;
55 }
56 uint64_t *p1, *p2, *p3;
57 p1 = p3 = list1;
58 p2 = list2;
59
60 while ( *p1 != -1 && *p2 != -1 ) {
61 if (compare_uint64(p1, p2) == 0) {
62 *p3++ = *p1++;
63 p2++;
64 }
65 else {
66 while (compare_uint64(p1, p2) < 0) ++p1;
67 while (compare_uint64(p1, p2) > 0) ++p2;
68 }
69 }
70 *p3 = -1;
71 return p3 - list1;
72 }
73
74
75 // Darkside attack (hf mf mifare)
76 static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {
77 struct Crypto1State *states;
78 uint32_t i, pos, rr; //nr_diff;
79 uint8_t bt, ks3x[8], par[8][8];
80 uint64_t key_recovered;
81 static uint64_t *keylist;
82 rr = 0;
83
84 // Reset the last three significant bits of the reader nonce
85 nr &= 0xffffff1f;
86
87 for (pos=0; pos<8; pos++) {
88 ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;
89 bt = (par_info >> (pos*8)) & 0xff;
90 for (i=0; i<8; i++) {
91 par[7-pos][i] = (bt >> i) & 0x01;
92 }
93 }
94
95 states = lfsr_common_prefix(nr, rr, ks3x, par, (par_info == 0));
96
97 if (states == NULL) {
98 *keys = NULL;
99 return 0;
100 }
101
102 keylist = (uint64_t*)states;
103
104 for (i = 0; keylist[i]; i++) {
105 lfsr_rollback_word(states+i, uid^nt, 0);
106 crypto1_get_lfsr(states+i, &key_recovered);
107 keylist[i] = key_recovered;
108 }
109 keylist[i] = -1;
110
111 *keys = keylist;
112 return i;
113 }
114
115
116 int mfDarkside(uint64_t *key)
117 {
118 uint32_t uid = 0;
119 uint32_t nt = 0, nr = 0;
120 uint64_t par_list = 0, ks_list = 0;
121 uint64_t *keylist = NULL, *last_keylist = NULL;
122 uint32_t keycount = 0;
123 int16_t isOK = 0;
124
125 UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};
126
127 // message
128 printf("-------------------------------------------------------------------------\n");
129 printf("Executing command. Expected execution time: 25sec on average\n");
130 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
131 printf("-------------------------------------------------------------------------\n");
132
133
134 while (true) {
135 clearCommandBuffer();
136 SendCommand(&c);
137
138 //flush queue
139 while (ukbhit()) {
140 int c = getchar(); (void) c;
141 }
142
143 // wait cycle
144 while (true) {
145 printf(".");
146 fflush(stdout);
147 if (ukbhit()) {
148 return -5;
149 break;
150 }
151
152 UsbCommand resp;
153 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
154 isOK = resp.arg[0];
155 if (isOK < 0) {
156 return isOK;
157 }
158 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);
159 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);
160 par_list = bytes_to_num(resp.d.asBytes + 8, 8);
161 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);
162 nr = bytes_to_num(resp.d.asBytes + 24, 4);
163 break;
164 }
165 }
166
167 if (par_list == 0 && c.arg[0] == true) {
168 PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");
169 PrintAndLog("Attack will take a few seconds longer because we need two consecutive successful runs.");
170 }
171 c.arg[0] = false;
172
173 keycount = nonce2key(uid, nt, nr, par_list, ks_list, &keylist);
174
175 if (keycount == 0) {
176 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);
177 PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
178 continue;
179 }
180
181 qsort(keylist, keycount, sizeof(*keylist), compare_uint64);
182 keycount = intersection(last_keylist, keylist);
183 if (keycount == 0) {
184 free(last_keylist);
185 last_keylist = keylist;
186 continue;
187 }
188
189 if (keycount > 1) {
190 PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount);
191 } else {
192 PrintAndLog("Found a possible key. Trying to authenticate...\n");
193 }
194
195 *key = -1;
196 uint8_t keyBlock[USB_CMD_DATA_SIZE];
197 int max_keys = USB_CMD_DATA_SIZE/6;
198 for (int i = 0; i < keycount; i += max_keys) {
199 int size = keycount - i > max_keys ? max_keys : keycount - i;
200 for (int j = 0; j < size; j++) {
201 if (last_keylist == NULL) {
202 num_to_bytes(keylist[i*max_keys + j], 6, keyBlock);
203 } else {
204 num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock);
205 }
206 }
207 if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) {
208 break;
209 }
210 }
211
212 if (*key != -1) {
213 free(last_keylist);
214 free(keylist);
215 break;
216 } else {
217 PrintAndLog("Authentication failed. Trying again...");
218 free(last_keylist);
219 last_keylist = keylist;
220 }
221 }
222
223 return 0;
224 }
225
226
227 int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){
228
229 *key = -1;
230
231 UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};
232 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);
233 SendCommand(&c);
234
235 UsbCommand resp;
236 if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;
237 if ((resp.arg[0] & 0xff) != 0x01) return 2;
238 *key = bytes_to_num(resp.d.asBytes, 6);
239 return 0;
240 }
241
242 // Compare 16 Bits out of cryptostate
243 int Compare16Bits(const void * a, const void * b) {
244 if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;
245 else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;
246 else return -1;
247 }
248
249 typedef
250 struct {
251 union {
252 struct Crypto1State *slhead;
253 uint64_t *keyhead;
254 } head;
255 union {
256 struct Crypto1State *sltail;
257 uint64_t *keytail;
258 } tail;
259 uint32_t len;
260 uint32_t uid;
261 uint32_t blockNo;
262 uint32_t keyType;
263 uint32_t nt;
264 uint32_t ks1;
265 } StateList_t;
266
267
268 // wrapper function for multi-threaded lfsr_recovery32
269 void* nested_worker_thread(void *arg)
270 {
271 struct Crypto1State *p1;
272 StateList_t *statelist = arg;
273
274 statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);
275 for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);
276 statelist->len = p1 - statelist->head.slhead;
277 statelist->tail.sltail = --p1;
278 qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);
279
280 return statelist->head.slhead;
281 }
282
283 int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate)
284 {
285 uint16_t i;
286 uint32_t uid;
287 UsbCommand resp;
288
289 StateList_t statelists[2];
290 struct Crypto1State *p1, *p2, *p3, *p4;
291
292 // flush queue
293 WaitForResponseTimeout(CMD_ACK, NULL, 100);
294
295 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};
296 memcpy(c.d.asBytes, key, 6);
297 SendCommand(&c);
298
299 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
300 return -1;
301 }
302
303 if (resp.arg[0]) {
304 return resp.arg[0]; // error during nested
305 }
306
307 memcpy(&uid, resp.d.asBytes, 4);
308 PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);
309
310 for (i = 0; i < 2; i++) {
311 statelists[i].blockNo = resp.arg[2] & 0xff;
312 statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;
313 statelists[i].uid = uid;
314 memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);
315 memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);
316 }
317
318 // calc keys
319
320 pthread_t thread_id[2];
321
322 // create and run worker threads
323 for (i = 0; i < 2; i++) {
324 pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);
325 }
326
327 // wait for threads to terminate:
328 for (i = 0; i < 2; i++) {
329 pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);
330 }
331
332
333 // the first 16 Bits of the cryptostate already contain part of our key.
334 // Create the intersection of the two lists based on these 16 Bits and
335 // roll back the cryptostate
336 p1 = p3 = statelists[0].head.slhead;
337 p2 = p4 = statelists[1].head.slhead;
338 while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {
339 if (Compare16Bits(p1, p2) == 0) {
340 struct Crypto1State savestate, *savep = &savestate;
341 savestate = *p1;
342 while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {
343 *p3 = *p1;
344 lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);
345 p3++;
346 p1++;
347 }
348 savestate = *p2;
349 while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {
350 *p4 = *p2;
351 lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);
352 p4++;
353 p2++;
354 }
355 }
356 else {
357 while (Compare16Bits(p1, p2) == -1) p1++;
358 while (Compare16Bits(p1, p2) == 1) p2++;
359 }
360 }
361 *(uint64_t*)p3 = -1;
362 *(uint64_t*)p4 = -1;
363 statelists[0].len = p3 - statelists[0].head.slhead;
364 statelists[1].len = p4 - statelists[1].head.slhead;
365 statelists[0].tail.sltail=--p3;
366 statelists[1].tail.sltail=--p4;
367
368 // the statelists now contain possible keys. The key we are searching for must be in the
369 // intersection of both lists. Create the intersection:
370 qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64);
371 qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64);
372 statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead);
373
374 memset(resultKey, 0, 6);
375 // The list may still contain several key candidates. Test each of them with mfCheckKeys
376 for (i = 0; i < statelists[0].len; i++) {
377 uint8_t keyBlock[6];
378 uint64_t key64;
379 crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);
380 num_to_bytes(key64, 6, keyBlock);
381 key64 = 0;
382 if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {
383 num_to_bytes(key64, 6, resultKey);
384 break;
385 }
386 }
387
388 free(statelists[0].head.slhead);
389 free(statelists[1].head.slhead);
390
391 return 0;
392 }
393
394 // EMULATOR
395
396 int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {
397 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};
398 SendCommand(&c);
399
400 UsbCommand resp;
401 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;
402 memcpy(data, resp.d.asBytes, blocksCount * 16);
403 return 0;
404 }
405
406 int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {
407 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};
408 memcpy(c.d.asBytes, data, blocksCount * 16);
409 SendCommand(&c);
410 return 0;
411 }
412
413 // "MAGIC" CARD
414
415 int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {
416 uint8_t isOK = 0;
417
418 UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};
419 SendCommand(&c);
420
421 UsbCommand resp;
422 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
423 isOK = resp.arg[0] & 0xff;
424 memcpy(data, resp.d.asBytes, 16);
425 if (!isOK) return 2;
426 } else {
427 PrintAndLog("Command execute timeout");
428 return 1;
429 }
430 return 0;
431 }
432
433 int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {
434
435 uint8_t isOK = 0;
436 UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};
437 memcpy(c.d.asBytes, data, 16);
438 SendCommand(&c);
439
440 UsbCommand resp;
441 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
442 isOK = resp.arg[0] & 0xff;
443 if (uid != NULL)
444 memcpy(uid, resp.d.asBytes, 4);
445 if (!isOK)
446 return 2;
447 } else {
448 PrintAndLog("Command execute timeout");
449 return 1;
450 }
451 return 0;
452 }
453
454 int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe) {
455 uint8_t oldblock0[16] = {0x00};
456 uint8_t block0[16] = {0x00};
457 int old, gen = 0;
458
459 gen = mfCIdentify();
460
461 if (gen == 2) {
462 /* generation 1b magic card */
463 old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B);
464 } else {
465 /* generation 1a magic card by default */
466 old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER);
467 }
468
469 if (old == 0) {
470 memcpy(block0, oldblock0, 16);
471 PrintAndLog("old block 0: %s", sprint_hex(block0,16));
472 } else {
473 PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");
474 }
475
476 // fill in the new values
477 // UID
478 memcpy(block0, uid, 4);
479 // Mifare UID BCC
480 block0[4] = block0[0]^block0[1]^block0[2]^block0[3];
481 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)
482 if (sak!=NULL)
483 block0[5]=sak[0];
484 if (atqa!=NULL) {
485 block0[6]=atqa[1];
486 block0[7]=atqa[0];
487 }
488 PrintAndLog("new block 0: %s", sprint_hex(block0,16));
489
490 if (gen == 2) {
491 /* generation 1b magic card */
492 return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B);
493 } else {
494 /* generation 1a magic card by default */
495 return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);
496 }
497 }
498
499 // SNIFFER
500
501 // constants
502 static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};
503
504 // variables
505 char logHexFileName[FILE_PATH_SIZE] = {0x00};
506 static uint8_t traceCard[4096] = {0x00};
507 static char traceFileName[FILE_PATH_SIZE] = {0x00};
508 static int traceState = TRACE_IDLE;
509 static uint8_t traceCurBlock = 0;
510 static uint8_t traceCurKey = 0;
511
512 struct Crypto1State *traceCrypto1 = NULL;
513
514 struct Crypto1State *revstate;
515 uint64_t lfsr;
516 uint32_t ks2;
517 uint32_t ks3;
518
519 uint32_t uid; // serial number
520 uint32_t nt; // tag challenge
521 uint32_t nr_enc; // encrypted reader challenge
522 uint32_t ar_enc; // encrypted reader response
523 uint32_t at_enc; // encrypted tag response
524
525 int isTraceCardEmpty(void) {
526 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));
527 }
528
529 int isBlockEmpty(int blockN) {
530 for (int i = 0; i < 16; i++)
531 if (traceCard[blockN * 16 + i] != 0) return 0;
532
533 return 1;
534 }
535
536 int isBlockTrailer(int blockN) {
537 return ((blockN & 0x03) == 0x03);
538 }
539
540 int saveTraceCard(void) {
541 FILE * f;
542
543 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;
544
545 f = fopen(traceFileName, "w+");
546 if ( !f ) return 1;
547
548 for (int i = 0; i < 64; i++) { // blocks
549 for (int j = 0; j < 16; j++) // bytes
550 fprintf(f, "%02x", *(traceCard + i * 16 + j));
551 fprintf(f,"\n");
552 }
553 fclose(f);
554 return 0;
555 }
556
557 int loadTraceCard(uint8_t *tuid) {
558 FILE * f;
559 char buf[64] = {0x00};
560 uint8_t buf8[64] = {0x00};
561 int i, blockNum;
562
563 if (!isTraceCardEmpty())
564 saveTraceCard();
565
566 memset(traceCard, 0x00, 4096);
567 memcpy(traceCard, tuid + 3, 4);
568
569 FillFileNameByUID(traceFileName, tuid, ".eml", 7);
570
571 f = fopen(traceFileName, "r");
572 if (!f) return 1;
573
574 blockNum = 0;
575
576 while(!feof(f)){
577
578 memset(buf, 0, sizeof(buf));
579 if (fgets(buf, sizeof(buf), f) == NULL) {
580 PrintAndLog("File reading error.");
581 fclose(f);
582 return 2;
583 }
584
585 if (strlen(buf) < 32){
586 if (feof(f)) break;
587 PrintAndLog("File content error. Block data must include 32 HEX symbols");
588 fclose(f);
589 return 2;
590 }
591 for (i = 0; i < 32; i += 2)
592 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
593
594 memcpy(traceCard + blockNum * 16, buf8, 16);
595
596 blockNum++;
597 }
598 fclose(f);
599
600 return 0;
601 }
602
603 int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {
604
605 if (traceCrypto1)
606 crypto1_destroy(traceCrypto1);
607
608 traceCrypto1 = NULL;
609
610 if (wantSaveToEmlFile)
611 loadTraceCard(tuid);
612
613 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];
614 traceCard[5] = sak;
615 memcpy(&traceCard[6], atqa, 2);
616 traceCurBlock = 0;
617 uid = bytes_to_num(tuid + 3, 4);
618
619 traceState = TRACE_IDLE;
620
621 return 0;
622 }
623
624 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){
625 uint8_t bt = 0;
626 int i;
627
628 if (len != 1) {
629 for (i = 0; i < len; i++)
630 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];
631 } else {
632 bt = 0;
633 for (i = 0; i < 4; i++)
634 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;
635
636 data[0] = bt;
637 }
638 return;
639 }
640
641
642 int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
643 uint8_t data[64];
644
645 if (traceState == TRACE_ERROR) return 1;
646 if (len > 64) {
647 traceState = TRACE_ERROR;
648 return 1;
649 }
650
651 memcpy(data, data_src, len);
652 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {
653 mf_crypto1_decrypt(traceCrypto1, data, len, 0);
654 PrintAndLog("dec> %s", sprint_hex(data, len));
655 AddLogHex(logHexFileName, "dec> ", data, len);
656 }
657
658 switch (traceState) {
659 case TRACE_IDLE:
660 // check packet crc16!
661 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {
662 PrintAndLog("dec> CRC ERROR!!!");
663 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!");
664 traceState = TRACE_ERROR; // do not decrypt the next commands
665 return 1;
666 }
667
668 // AUTHENTICATION
669 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {
670 traceState = TRACE_AUTH1;
671 traceCurBlock = data[1];
672 traceCurKey = data[0] == 60 ? 1:0;
673 return 0;
674 }
675
676 // READ
677 if ((len ==4) && ((data[0] == 0x30))) {
678 traceState = TRACE_READ_DATA;
679 traceCurBlock = data[1];
680 return 0;
681 }
682
683 // WRITE
684 if ((len ==4) && ((data[0] == 0xA0))) {
685 traceState = TRACE_WRITE_OK;
686 traceCurBlock = data[1];
687 return 0;
688 }
689
690 // HALT
691 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {
692 traceState = TRACE_ERROR; // do not decrypt the next commands
693 return 0;
694 }
695
696 return 0;
697 break;
698
699 case TRACE_READ_DATA:
700 if (len == 18) {
701 traceState = TRACE_IDLE;
702
703 if (isBlockTrailer(traceCurBlock)) {
704 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);
705 } else {
706 memcpy(traceCard + traceCurBlock * 16, data, 16);
707 }
708 if (wantSaveToEmlFile) saveTraceCard();
709 return 0;
710 } else {
711 traceState = TRACE_ERROR;
712 return 1;
713 }
714 break;
715
716 case TRACE_WRITE_OK:
717 if ((len == 1) && (data[0] == 0x0a)) {
718 traceState = TRACE_WRITE_DATA;
719
720 return 0;
721 } else {
722 traceState = TRACE_ERROR;
723 return 1;
724 }
725 break;
726
727 case TRACE_WRITE_DATA:
728 if (len == 18) {
729 traceState = TRACE_IDLE;
730
731 memcpy(traceCard + traceCurBlock * 16, data, 16);
732 if (wantSaveToEmlFile) saveTraceCard();
733 return 0;
734 } else {
735 traceState = TRACE_ERROR;
736 return 1;
737 }
738 break;
739
740 case TRACE_AUTH1:
741 if (len == 4) {
742 traceState = TRACE_AUTH2;
743 nt = bytes_to_num(data, 4);
744 return 0;
745 } else {
746 traceState = TRACE_ERROR;
747 return 1;
748 }
749 break;
750
751 case TRACE_AUTH2:
752 if (len == 8) {
753 traceState = TRACE_AUTH_OK;
754
755 nr_enc = bytes_to_num(data, 4);
756 ar_enc = bytes_to_num(data + 4, 4);
757 return 0;
758 } else {
759 traceState = TRACE_ERROR;
760 return 1;
761 }
762 break;
763
764 case TRACE_AUTH_OK:
765 if (len ==4) {
766 traceState = TRACE_IDLE;
767
768 at_enc = bytes_to_num(data, 4);
769
770 // decode key here)
771 ks2 = ar_enc ^ prng_successor(nt, 64);
772 ks3 = at_enc ^ prng_successor(nt, 96);
773 revstate = lfsr_recovery64(ks2, ks3);
774 lfsr_rollback_word(revstate, 0, 0);
775 lfsr_rollback_word(revstate, 0, 0);
776 lfsr_rollback_word(revstate, nr_enc, 1);
777 lfsr_rollback_word(revstate, uid ^ nt, 0);
778
779 crypto1_get_lfsr(revstate, &lfsr);
780 printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));
781 AddLogUint64(logHexFileName, "key> ", lfsr);
782
783 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;
784 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);
785
786 if (traceCurKey) {
787 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);
788 } else {
789 num_to_bytes(lfsr, 6, traceCard + blockShift);
790 }
791 if (wantSaveToEmlFile) saveTraceCard();
792
793 if (traceCrypto1) {
794 crypto1_destroy(traceCrypto1);
795 }
796
797 // set cryptosystem state
798 traceCrypto1 = lfsr_recovery64(ks2, ks3);
799
800 // nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;
801
802 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr
803 crypto1_word(traceCrypto1, nt ^ uid, 0);
804 crypto1_word(traceCrypto1, ar, 1);
805 crypto1_word(traceCrypto1, 0, 0);
806 crypto1_word(traceCrypto1, 0, 0);*/
807
808 return 0;
809 } else {
810 traceState = TRACE_ERROR;
811 return 1;
812 }
813 break;
814
815 default:
816 traceState = TRACE_ERROR;
817 return 1;
818 }
819
820 return 0;
821 }
822
823 int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){
824 /*
825 uint32_t nt; // tag challenge
826 uint32_t ar_enc; // encrypted reader response
827 uint32_t at_enc; // encrypted tag response
828 */
829 if (traceCrypto1) {
830 crypto1_destroy(traceCrypto1);
831 }
832 ks2 = ar_enc ^ prng_successor(nt, 64);
833 ks3 = at_enc ^ prng_successor(nt, 96);
834 traceCrypto1 = lfsr_recovery64(ks2, ks3);
835
836 mf_crypto1_decrypt(traceCrypto1, data, len, 0);
837
838 PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );
839 crypto1_destroy(traceCrypto1);
840 return 0;
841 }
842
843 int mfCIdentify()
844 {
845 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
846 SendCommand(&c);
847
848 UsbCommand resp;
849 WaitForResponse(CMD_ACK,&resp);
850
851 iso14a_card_select_t card;
852 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
853
854 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
855
856 if(select_status != 0) {
857 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
858 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
859 c.arg[1] = 2;
860 c.arg[2] = 0;
861 memcpy(c.d.asBytes, rats, 2);
862 SendCommand(&c);
863 WaitForResponse(CMD_ACK,&resp);
864 }
865
866 c.cmd = CMD_MIFARE_CIDENT;
867 c.arg[0] = 0;
868 c.arg[1] = 0;
869 c.arg[2] = 0;
870 SendCommand(&c);
871 WaitForResponse(CMD_ACK,&resp);
872
873 uint8_t isGeneration = resp.arg[0] & 0xff;
874 switch( isGeneration ){
875 case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break;
876 case 2: PrintAndLog("Chinese magic backdoor command (GEN 1b) detected"); break;
877 default: PrintAndLog("No chinese magic backdoor command detected"); break;
878 }
879
880 // disconnect
881 c.cmd = CMD_READER_ISO_14443a;
882 c.arg[0] = 0;
883 c.arg[1] = 0;
884 c.arg[2] = 0;
885 SendCommand(&c);
886
887 return (int) isGeneration;
888 }
Impressum, Datenschutz