]> git.zerfleddert.de Git - proxmark3-svn/blob - client/mifarehost.c
`hf mf sniff` transfered parity from arm to decoding procedure
[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 <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "mifarehost.h"
15
16 // MIFARE
17
18 int compar_int(const void * a, const void * b) {
19 return (*(uint64_t*)b - *(uint64_t*)a);
20 }
21
22 // Compare countKeys structure
23 int compar_special_int(const void * a, const void * b) {
24 return (((countKeys *)b)->count - ((countKeys *)a)->count);
25 }
26
27 countKeys * uniqsort(uint64_t * possibleKeys, uint32_t size) {
28 int i, j = 0;
29 int count = 0;
30 countKeys *our_counts;
31
32 qsort(possibleKeys, size, sizeof (uint64_t), compar_int);
33
34 our_counts = calloc(size, sizeof(countKeys));
35 if (our_counts == NULL) {
36 PrintAndLog("Memory allocation error for our_counts");
37 return NULL;
38 }
39
40 for (i = 0; i < size; i++) {
41 if (possibleKeys[i+1] == possibleKeys[i]) {
42 count++;
43 } else {
44 our_counts[j].key = possibleKeys[i];
45 our_counts[j].count = count;
46 j++;
47 count=0;
48 }
49 }
50 qsort(our_counts, j, sizeof(countKeys), compar_special_int);
51 return (our_counts);
52 }
53
54 int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t * resultKeys)
55 {
56 int i, m, len;
57 uint8_t isEOF;
58 uint32_t uid;
59 fnVector * vector = NULL;
60 countKeys *ck;
61 int lenVector = 0;
62 UsbCommand * resp = NULL;
63
64 memset(resultKeys, 0x00, 16 * 6);
65
66 // flush queue
67 while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
68
69 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo, keyType, trgBlockNo + trgKeyType * 0x100}};
70 memcpy(c.d.asBytes, key, 6);
71 SendCommand(&c);
72
73 PrintAndLog("\n");
74
75 // wait cycle
76 while (true) {
77 printf(".");
78 if (ukbhit()) {
79 getchar();
80 printf("\naborted via keyboard!\n");
81 break;
82 }
83
84 resp = WaitForResponseTimeout(CMD_ACK, 1500);
85
86 if (resp != NULL) {
87 isEOF = resp->arg[0] & 0xff;
88
89 if (isEOF) break;
90
91 len = resp->arg[1] & 0xff;
92 if (len == 0) continue;
93
94 memcpy(&uid, resp->d.asBytes, 4);
95 PrintAndLog("uid:%08x len=%d trgbl=%d trgkey=%x", uid, len, resp->arg[2] & 0xff, (resp->arg[2] >> 8) & 0xff);
96 vector = (fnVector *) realloc((void *)vector, (lenVector + len) * sizeof(fnVector) + 200);
97 if (vector == NULL) {
98 PrintAndLog("Memory allocation error for fnVector. len: %d bytes: %d", lenVector + len, (lenVector + len) * sizeof(fnVector));
99 break;
100 }
101
102 for (i = 0; i < len; i++) {
103 vector[lenVector + i].blockNo = resp->arg[2] & 0xff;
104 vector[lenVector + i].keyType = (resp->arg[2] >> 8) & 0xff;
105 vector[lenVector + i].uid = uid;
106
107 memcpy(&vector[lenVector + i].nt, (void *)(resp->d.asBytes + 8 + i * 8 + 0), 4);
108 memcpy(&vector[lenVector + i].ks1, (void *)(resp->d.asBytes + 8 + i * 8 + 4), 4);
109 }
110
111 lenVector += len;
112 }
113 }
114
115 if (!lenVector) {
116 PrintAndLog("Got 0 keys from proxmark.");
117 return 1;
118 }
119 printf("------------------------------------------------------------------\n");
120
121 // calc keys
122 struct Crypto1State* revstate = NULL;
123 struct Crypto1State* revstate_start = NULL;
124 uint64_t lfsr;
125 int kcount = 0;
126 pKeys *pk;
127
128 if ((pk = (void *) malloc(sizeof(pKeys))) == NULL) return 1;
129 memset(pk, 0x00, sizeof(pKeys));
130
131 for (m = 0; m < lenVector; m++) {
132 // And finally recover the first 32 bits of the key
133 revstate = lfsr_recovery32(vector[m].ks1, vector[m].nt ^ vector[m].uid);
134 if (revstate_start == NULL) revstate_start = revstate;
135
136 while ((revstate->odd != 0x0) || (revstate->even != 0x0)) {
137 lfsr_rollback_word(revstate, vector[m].nt ^ vector[m].uid, 0);
138 crypto1_get_lfsr(revstate, &lfsr);
139
140 // Allocate a new space for keys
141 if (((kcount % MEM_CHUNK) == 0) || (kcount >= pk->size)) {
142 pk->size += MEM_CHUNK;
143 //fprintf(stdout, "New chunk by %d, sizeof %d\n", kcount, pk->size * sizeof(uint64_t));
144 pk->possibleKeys = (uint64_t *) realloc((void *)pk->possibleKeys, pk->size * sizeof(uint64_t));
145 if (pk->possibleKeys == NULL) {
146 PrintAndLog("Memory allocation error for pk->possibleKeys");
147 return 1;
148 }
149 }
150 pk->possibleKeys[kcount] = lfsr;
151 kcount++;
152 revstate++;
153 }
154 free(revstate_start);
155 revstate_start = NULL;
156
157 }
158
159 // Truncate
160 if (kcount != 0) {
161 pk->size = --kcount;
162 if ((pk->possibleKeys = (uint64_t *) realloc((void *)pk->possibleKeys, pk->size * sizeof(uint64_t))) == NULL) {
163 PrintAndLog("Memory allocation error for pk->possibleKeys");
164 return 1;
165 }
166 }
167
168 PrintAndLog("Total keys count:%d", kcount);
169 ck = uniqsort(pk->possibleKeys, pk->size);
170
171 // fill key array
172 for (i = 0; i < 16 ; i++) {
173 num_to_bytes(ck[i].key, 6, (uint8_t*)(resultKeys + i * 6));
174 }
175
176 // finalize
177 free(pk->possibleKeys);
178 free(pk);
179 free(ck);
180 free(vector);
181
182 return 0;
183 }
184
185 int mfCheckKeys (uint8_t blockNo, uint8_t keyType, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){
186 *key = 0;
187
188 UsbCommand c = {CMD_MIFARE_CHKKEYS, {blockNo, keyType, keycnt}};
189 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);
190
191 SendCommand(&c);
192
193 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 3000);
194
195 if (resp == NULL) return 1;
196 if ((resp->arg[0] & 0xff) != 0x01) return 2;
197 *key = bytes_to_num(resp->d.asBytes, 6);
198 return 0;
199 }
200
201 // EMULATOR
202
203 int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {
204 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};
205
206 SendCommand(&c);
207
208 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
209
210 if (resp == NULL) return 1;
211 memcpy(data, resp->d.asBytes, blocksCount * 16);
212 return 0;
213 }
214
215 int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {
216 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};
217 memcpy(c.d.asBytes, data, blocksCount * 16);
218 SendCommand(&c);
219 return 0;
220 }
221
222 // "MAGIC" CARD
223
224 int mfCSetUID(uint8_t *uid, uint8_t *oldUID, int wantWipe) {
225 uint8_t block0[16];
226 memset(block0, 0, 16);
227 memcpy(block0, uid, 4);
228 block0[4] = block0[0]^block0[1]^block0[2]^block0[3]; // Mifare UID BCC
229 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7)
230 block0[5] = 0x88;
231 block0[6] = 0x04;
232 block0[7] = 0x00;
233
234 return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);
235 }
236
237 int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, int wantWipe, uint8_t params) {
238 uint8_t isOK = 0;
239
240 UsbCommand c = {CMD_MIFARE_EML_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};
241 memcpy(c.d.asBytes, data, 16);
242 SendCommand(&c);
243
244 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
245
246 if (resp != NULL) {
247 isOK = resp->arg[0] & 0xff;
248 if (uid != NULL) memcpy(uid, resp->d.asBytes, 4);
249 if (!isOK) return 2;
250 } else {
251 PrintAndLog("Command execute timeout");
252 return 1;
253 }
254 return 0;
255 }
256
257 int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {
258 uint8_t isOK = 0;
259
260 UsbCommand c = {CMD_MIFARE_EML_CGETBLOCK, {params, 0, blockNo}};
261 SendCommand(&c);
262
263 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
264
265 if (resp != NULL) {
266 isOK = resp->arg[0] & 0xff;
267 memcpy(data, resp->d.asBytes, 16);
268 if (!isOK) return 2;
269 } else {
270 PrintAndLog("Command execute timeout");
271 return 1;
272 }
273 return 0;
274 }
275
276 // SNIFFER
277
278 // constants
279 static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};
280
281 // variables
282 char logHexFileName[200] = {0x00};
283 static uint8_t traceCard[4096] = {0x00};
284 static char traceFileName[20];
285 static int traceState = TRACE_IDLE;
286 static uint8_t traceCurBlock = 0;
287 static uint8_t traceCurKey = 0;
288
289 struct Crypto1State *traceCrypto1 = NULL;
290
291 struct Crypto1State *revstate;
292 uint64_t lfsr;
293 uint32_t ks2;
294 uint32_t ks3;
295
296 uint32_t uid; // serial number
297 uint32_t nt; // tag challenge
298 uint32_t nt_par;
299 uint32_t nr_enc; // encrypted reader challenge
300 uint32_t ar_enc; // encrypted reader response
301 uint32_t nr_ar_par;
302 uint32_t at_enc; // encrypted tag response
303 uint32_t at_par;
304
305 int isTraceCardEmpty(void) {
306 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));
307 }
308
309 int isBlockEmpty(int blockN) {
310 for (int i = 0; i < 16; i++)
311 if (traceCard[blockN * 16 + i] != 0) return 0;
312
313 return 1;
314 }
315
316 int isBlockTrailer(int blockN) {
317 return ((blockN & 0x03) == 0x03);
318 }
319
320 int loadTraceCard(uint8_t *tuid) {
321 FILE * f;
322 char buf[64];
323 uint8_t buf8[64];
324 int i, blockNum;
325
326 if (!isTraceCardEmpty()) saveTraceCard();
327 memset(traceCard, 0x00, 4096);
328 memcpy(traceCard, tuid + 3, 4);
329 FillFileNameByUID(traceFileName, tuid, ".eml", 7);
330
331 f = fopen(traceFileName, "r");
332 if (!f) return 1;
333
334 blockNum = 0;
335 while(!feof(f)){
336 memset(buf, 0, sizeof(buf));
337 fgets(buf, sizeof(buf), f);
338
339 if (strlen(buf) < 32){
340 if (feof(f)) break;
341 PrintAndLog("File content error. Block data must include 32 HEX symbols");
342 return 2;
343 }
344 for (i = 0; i < 32; i += 2)
345 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
346
347 memcpy(traceCard + blockNum * 16, buf8, 16);
348
349 blockNum++;
350 }
351 fclose(f);
352
353 return 0;
354 }
355
356 int saveTraceCard(void) {
357 FILE * f;
358
359 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;
360
361 f = fopen(traceFileName, "w+");
362 for (int i = 0; i < 64; i++) { // blocks
363 for (int j = 0; j < 16; j++) // bytes
364 fprintf(f, "%02x", *(traceCard + i * 16 + j));
365 fprintf(f,"\n");
366 }
367 fclose(f);
368
369 return 0;
370 }
371
372 int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {
373
374 if (traceCrypto1) crypto1_destroy(traceCrypto1);
375 traceCrypto1 = NULL;
376
377 if (wantSaveToEmlFile) loadTraceCard(tuid);
378 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];
379 traceCard[5] = sak;
380 memcpy(&traceCard[6], atqa, 2);
381 traceCurBlock = 0;
382 uid = bytes_to_num(tuid + 3, 4);
383
384 traceState = TRACE_IDLE;
385
386 return 0;
387 }
388
389 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){
390 uint8_t bt = 0;
391 int i;
392
393 if (len != 1) {
394 for (i = 0; i < len; i++)
395 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];
396 } else {
397 bt = 0;
398 for (i = 0; i < 4; i++)
399 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;
400
401 data[0] = bt;
402 }
403 return;
404 }
405
406
407 int mfTraceDecode(uint8_t *data_src, int len, uint32_t parity, bool wantSaveToEmlFile) {
408 uint8_t data[64];
409
410 if (traceState == TRACE_ERROR) return 1;
411 if (len > 64) {
412 traceState = TRACE_ERROR;
413 return 1;
414 }
415
416 memcpy(data, data_src, len);
417 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {
418 mf_crypto1_decrypt(traceCrypto1, data, len, 0);
419 PrintAndLog("dec> %s", sprint_hex(data, len));
420 AddLogHex(logHexFileName, "dec> ", data, len);
421 }
422
423 switch (traceState) {
424 case TRACE_IDLE:
425 // check packet crc16!
426 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {
427 PrintAndLog("dec> CRC ERROR!!!");
428 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!");
429 traceState = TRACE_ERROR; // do not decrypt the next commands
430 return 1;
431 }
432
433 // AUTHENTICATION
434 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {
435 traceState = TRACE_AUTH1;
436 traceCurBlock = data[1];
437 traceCurKey = data[0] == 60 ? 1:0;
438 return 0;
439 }
440
441 // READ
442 if ((len ==4) && ((data[0] == 0x30))) {
443 traceState = TRACE_READ_DATA;
444 traceCurBlock = data[1];
445 return 0;
446 }
447
448 // WRITE
449 if ((len ==4) && ((data[0] == 0xA0))) {
450 traceState = TRACE_WRITE_OK;
451 traceCurBlock = data[1];
452 return 0;
453 }
454
455 // HALT
456 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {
457 traceState = TRACE_ERROR; // do not decrypt the next commands
458 return 0;
459 }
460
461 return 0;
462 break;
463
464 case TRACE_READ_DATA:
465 if (len == 18) {
466 traceState = TRACE_IDLE;
467
468 if (isBlockTrailer(traceCurBlock)) {
469 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);
470 } else {
471 memcpy(traceCard + traceCurBlock * 16, data, 16);
472 }
473 if (wantSaveToEmlFile) saveTraceCard();
474 return 0;
475 } else {
476 traceState = TRACE_ERROR;
477 return 1;
478 }
479 break;
480
481 case TRACE_WRITE_OK:
482 if ((len == 1) && (data[0] = 0x0a)) {
483 traceState = TRACE_WRITE_DATA;
484
485 return 0;
486 } else {
487 traceState = TRACE_ERROR;
488 return 1;
489 }
490 break;
491
492 case TRACE_WRITE_DATA:
493 if (len == 18) {
494 traceState = TRACE_IDLE;
495
496 memcpy(traceCard + traceCurBlock * 16, data, 16);
497 if (wantSaveToEmlFile) saveTraceCard();
498 return 0;
499 } else {
500 traceState = TRACE_ERROR;
501 return 1;
502 }
503 break;
504
505 case TRACE_AUTH1:
506 if (len == 4) {
507 traceState = TRACE_AUTH2;
508
509 nt = bytes_to_num(data, 4);
510 nt_par = parity;
511 return 0;
512 } else {
513 traceState = TRACE_ERROR;
514 return 1;
515 }
516 break;
517
518 case TRACE_AUTH2:
519 if (len == 8) {
520 traceState = TRACE_AUTH_OK;
521
522 nr_enc = bytes_to_num(data, 4);
523 ar_enc = bytes_to_num(data + 4, 4);
524 nr_ar_par = parity;
525 return 0;
526 } else {
527 traceState = TRACE_ERROR;
528 return 1;
529 }
530 break;
531
532 case TRACE_AUTH_OK:
533 if (len ==4) {
534 traceState = TRACE_IDLE;
535
536 at_enc = bytes_to_num(data, 4);
537 at_par = parity;
538
539 // decode key here)
540 if (!traceCrypto1) {
541 ks2 = ar_enc ^ prng_successor(nt, 64);
542 ks3 = at_enc ^ prng_successor(nt, 96);
543 revstate = lfsr_recovery64(ks2, ks3);
544 lfsr_rollback_word(revstate, 0, 0);
545 lfsr_rollback_word(revstate, 0, 0);
546 lfsr_rollback_word(revstate, nr_enc, 1);
547 lfsr_rollback_word(revstate, uid ^ nt, 0);
548 }else{
549 ks2 = ar_enc ^ prng_successor(nt, 64);
550 ks3 = at_enc ^ prng_successor(nt, 96);
551 revstate = lfsr_recovery64(ks2, ks3);
552 lfsr_rollback_word(revstate, 0, 0);
553 lfsr_rollback_word(revstate, 0, 0);
554 lfsr_rollback_word(revstate, nr_enc, 1);
555 lfsr_rollback_word(revstate, uid ^ nt, 0);
556 }
557 crypto1_get_lfsr(revstate, &lfsr);
558 printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));
559 AddLogUint64(logHexFileName, "key> ", lfsr);
560
561 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;
562 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);
563
564 if (traceCurKey) {
565 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);
566 } else {
567 num_to_bytes(lfsr, 6, traceCard + blockShift);
568 }
569 if (wantSaveToEmlFile) saveTraceCard();
570
571 if (traceCrypto1) {
572 crypto1_destroy(traceCrypto1);
573 }
574
575 // set cryptosystem state
576 traceCrypto1 = lfsr_recovery64(ks2, ks3);
577
578 // nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;
579
580 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr
581 crypto1_word(traceCrypto1, nt ^ uid, 0);
582 crypto1_word(traceCrypto1, ar, 1);
583 crypto1_word(traceCrypto1, 0, 0);
584 crypto1_word(traceCrypto1, 0, 0);*/
585
586 return 0;
587 } else {
588 traceState = TRACE_ERROR;
589 return 1;
590 }
591 break;
592
593 default:
594 traceState = TRACE_ERROR;
595 return 1;
596 }
597
598 return 0;
599 }
Impressum, Datenschutz