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