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