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