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