]>
Commit | Line | Data |
---|---|---|
fdd9395d OM |
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 "mifarehost.h" | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
16 | #include <pthread.h> | |
17 | ||
18 | #include "crapto1/crapto1.h" | |
19 | #include "comms.h" | |
20 | #include "usb_cmd.h" | |
21 | #include "cmdmain.h" | |
22 | #include "ui.h" | |
23 | #include "parity.h" | |
24 | #include "util.h" | |
25 | #include "iso14443crc.h" | |
5a03ea99 | 26 | #include "util_posix.h" |
fdd9395d OM |
27 | |
28 | #include "mifare.h" | |
29 | #include "mifare4.h" | |
30 | ||
31 | // mifare tracer flags used in mfTraceDecode() | |
32 | #define TRACE_IDLE 0x00 | |
33 | #define TRACE_AUTH1 0x01 | |
34 | #define TRACE_AUTH2 0x02 | |
35 | #define TRACE_AUTH_OK 0x03 | |
36 | #define TRACE_READ_DATA 0x04 | |
37 | #define TRACE_WRITE_OK 0x05 | |
38 | #define TRACE_WRITE_DATA 0x06 | |
39 | #define TRACE_ERROR 0xFF | |
40 | ||
41 | ||
42 | static int compare_uint64(const void *a, const void *b) { | |
43 | // didn't work: (the result is truncated to 32 bits) | |
44 | //return (*(int64_t*)b - *(int64_t*)a); | |
45 | ||
46 | // better: | |
47 | if (*(uint64_t*)b == *(uint64_t*)a) return 0; | |
48 | else if (*(uint64_t*)b < *(uint64_t*)a) return 1; | |
49 | else return -1; | |
50 | } | |
51 | ||
52 | ||
53 | // create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned. | |
54 | static uint32_t intersection(uint64_t *list1, uint64_t *list2) | |
55 | { | |
56 | if (list1 == NULL || list2 == NULL) { | |
57 | return 0; | |
58 | } | |
59 | uint64_t *p1, *p2, *p3; | |
60 | p1 = p3 = list1; | |
61 | p2 = list2; | |
62 | ||
63 | while ( *p1 != -1 && *p2 != -1 ) { | |
64 | if (compare_uint64(p1, p2) == 0) { | |
65 | *p3++ = *p1++; | |
66 | p2++; | |
67 | } | |
68 | else { | |
69 | while (compare_uint64(p1, p2) < 0) ++p1; | |
70 | while (compare_uint64(p1, p2) > 0) ++p2; | |
71 | } | |
72 | } | |
73 | *p3 = -1; | |
74 | return p3 - list1; | |
75 | } | |
76 | ||
77 | ||
78 | // Darkside attack (hf mf mifare) | |
79 | static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint32_t ar, uint64_t par_info, uint64_t ks_info, uint64_t **keys) { | |
80 | struct Crypto1State *states; | |
81 | uint32_t i, pos; | |
82 | uint8_t bt, ks3x[8], par[8][8]; | |
83 | uint64_t key_recovered; | |
84 | uint64_t *keylist; | |
85 | ||
86 | // Reset the last three significant bits of the reader nonce | |
87 | nr &= 0xffffff1f; | |
88 | ||
89 | for (pos=0; pos<8; pos++) { | |
90 | ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f; | |
91 | bt = (par_info >> (pos*8)) & 0xff; | |
92 | for (i=0; i<8; i++) { | |
93 | par[7-pos][i] = (bt >> i) & 0x01; | |
94 | } | |
95 | } | |
96 | ||
97 | states = lfsr_common_prefix(nr, ar, ks3x, par, (par_info == 0)); | |
98 | ||
99 | if (states == NULL) { | |
100 | *keys = NULL; | |
101 | return 0; | |
102 | } | |
103 | ||
104 | keylist = (uint64_t*)states; | |
105 | ||
106 | for (i = 0; keylist[i]; i++) { | |
107 | lfsr_rollback_word(states+i, uid^nt, 0); | |
108 | crypto1_get_lfsr(states+i, &key_recovered); | |
109 | keylist[i] = key_recovered; | |
110 | } | |
111 | keylist[i] = -1; | |
112 | ||
113 | *keys = keylist; | |
114 | return i; | |
115 | } | |
116 | ||
117 | ||
118 | int mfDarkside(uint64_t *key) | |
119 | { | |
120 | uint32_t uid = 0; | |
121 | uint32_t nt = 0, nr = 0, ar = 0; | |
122 | uint64_t par_list = 0, ks_list = 0; | |
123 | uint64_t *keylist = NULL, *last_keylist = NULL; | |
124 | uint32_t keycount = 0; | |
125 | int16_t isOK = 0; | |
126 | ||
127 | UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}}; | |
128 | ||
129 | // message | |
130 | printf("-------------------------------------------------------------------------\n"); | |
131 | printf("Executing command. Expected execution time: 25sec on average\n"); | |
132 | printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n"); | |
133 | printf("-------------------------------------------------------------------------\n"); | |
134 | ||
135 | ||
136 | while (true) { | |
137 | clearCommandBuffer(); | |
138 | SendCommand(&c); | |
139 | ||
140 | //flush queue | |
141 | while (ukbhit()) { | |
142 | int c = getchar(); (void) c; | |
143 | } | |
144 | ||
145 | // wait cycle | |
146 | while (true) { | |
147 | printf("."); | |
148 | fflush(stdout); | |
149 | if (ukbhit()) { | |
150 | return -5; | |
151 | break; | |
152 | } | |
153 | ||
154 | UsbCommand resp; | |
155 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { | |
156 | isOK = resp.arg[0]; | |
157 | if (isOK < 0) { | |
158 | return isOK; | |
159 | } | |
160 | uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4); | |
161 | nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4); | |
162 | par_list = bytes_to_num(resp.d.asBytes + 8, 8); | |
163 | ks_list = bytes_to_num(resp.d.asBytes + 16, 8); | |
164 | nr = (uint32_t)bytes_to_num(resp.d.asBytes + 24, 4); | |
165 | ar = (uint32_t)bytes_to_num(resp.d.asBytes + 28, 4); | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
170 | if (par_list == 0 && c.arg[0] == true) { | |
171 | PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication."); | |
172 | } | |
173 | c.arg[0] = false; | |
174 | ||
175 | keycount = nonce2key(uid, nt, nr, ar, par_list, ks_list, &keylist); | |
176 | ||
177 | if (keycount == 0) { | |
178 | PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); | |
179 | PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce..."); | |
180 | continue; | |
181 | } | |
182 | ||
183 | if (par_list == 0) { | |
184 | qsort(keylist, keycount, sizeof(*keylist), compare_uint64); | |
185 | keycount = intersection(last_keylist, keylist); | |
186 | if (keycount == 0) { | |
187 | free(last_keylist); | |
188 | last_keylist = keylist; | |
189 | continue; | |
190 | } | |
191 | } | |
192 | ||
193 | if (keycount > 1) { | |
194 | PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount); | |
195 | } else { | |
196 | PrintAndLog("Found a possible key. Trying to authenticate...\n"); | |
197 | } | |
198 | ||
199 | *key = -1; | |
200 | uint8_t keyBlock[USB_CMD_DATA_SIZE]; | |
201 | int max_keys = USB_CMD_DATA_SIZE/6; | |
202 | for (int i = 0; i < keycount; i += max_keys) { | |
203 | int size = keycount - i > max_keys ? max_keys : keycount - i; | |
204 | for (int j = 0; j < size; j++) { | |
205 | if (par_list == 0) { | |
206 | num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock+(j*6)); | |
207 | } else { | |
208 | num_to_bytes(keylist[i*max_keys + j], 6, keyBlock+(j*6)); | |
209 | } | |
210 | } | |
211 | if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) { | |
212 | break; | |
213 | } | |
214 | } | |
215 | ||
216 | if (*key != -1) { | |
217 | free(last_keylist); | |
218 | free(keylist); | |
219 | break; | |
220 | } else { | |
221 | PrintAndLog("Authentication failed. Trying again..."); | |
222 | free(last_keylist); | |
223 | last_keylist = keylist; | |
224 | } | |
225 | } | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | ||
231 | int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){ | |
232 | ||
233 | *key = -1; | |
234 | ||
235 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), clear_trace, keycnt}}; | |
236 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt); | |
237 | SendCommand(&c); | |
238 | ||
239 | UsbCommand resp; | |
5a03ea99 | 240 | if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) |
241 | return 1; | |
242 | ||
243 | if ((resp.arg[0] & 0xff) != 0x01) { | |
244 | if (((int)resp.arg[1]) < 0) | |
245 | return (int)resp.arg[1]; | |
246 | ||
247 | return 2; | |
248 | } | |
249 | ||
fdd9395d OM |
250 | *key = bytes_to_num(resp.d.asBytes, 6); |
251 | return 0; | |
252 | } | |
253 | ||
254 | int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){ | |
255 | ||
256 | uint8_t keyPtr = 0; | |
257 | ||
258 | if (e_sector == NULL) | |
259 | return -1; | |
260 | ||
261 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}}; | |
262 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt); | |
263 | SendCommand(&c); | |
264 | ||
265 | UsbCommand resp; | |
266 | if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth | |
267 | if ((resp.arg[0] & 0xff) != 0x01) return 2; | |
268 | ||
269 | bool foundAKey = false; | |
270 | for(int sec = 0; sec < sectorCnt; sec++){ | |
271 | for(int keyAB = 0; keyAB < 2; keyAB++){ | |
272 | keyPtr = *(resp.d.asBytes + keyAB * 40 + sec); | |
273 | if (keyPtr){ | |
274 | e_sector[sec].foundKey[keyAB] = true; | |
275 | e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6); | |
276 | foundAKey = true; | |
277 | } | |
278 | } | |
279 | } | |
280 | return foundAKey ? 0 : 3; | |
281 | } | |
282 | ||
283 | // Compare 16 Bits out of cryptostate | |
284 | int Compare16Bits(const void * a, const void * b) { | |
285 | if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0; | |
286 | else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1; | |
287 | else return -1; | |
288 | } | |
289 | ||
290 | typedef | |
291 | struct { | |
292 | union { | |
293 | struct Crypto1State *slhead; | |
294 | uint64_t *keyhead; | |
295 | } head; | |
296 | union { | |
297 | struct Crypto1State *sltail; | |
298 | uint64_t *keytail; | |
299 | } tail; | |
300 | uint32_t len; | |
301 | uint32_t uid; | |
302 | uint32_t blockNo; | |
303 | uint32_t keyType; | |
304 | uint32_t nt; | |
305 | uint32_t ks1; | |
306 | } StateList_t; | |
307 | ||
308 | ||
309 | // wrapper function for multi-threaded lfsr_recovery32 | |
310 | void | |
311 | #ifdef __has_attribute | |
312 | #if __has_attribute(force_align_arg_pointer) | |
313 | __attribute__((force_align_arg_pointer)) | |
314 | #endif | |
315 | #endif | |
316 | *nested_worker_thread(void *arg) | |
317 | { | |
318 | struct Crypto1State *p1; | |
319 | StateList_t *statelist = arg; | |
320 | ||
321 | statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid); | |
322 | for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++); | |
323 | statelist->len = p1 - statelist->head.slhead; | |
324 | statelist->tail.sltail = --p1; | |
325 | qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits); | |
326 | ||
327 | return statelist->head.slhead; | |
328 | } | |
329 | ||
330 | ||
331 | int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate) | |
332 | { | |
5a03ea99 | 333 | uint32_t i, j; |
fdd9395d OM |
334 | uint32_t uid; |
335 | UsbCommand resp; | |
336 | ||
5a03ea99 | 337 | int num_unique_nonces; |
338 | ||
fdd9395d OM |
339 | StateList_t statelists[2]; |
340 | struct Crypto1State *p1, *p2, *p3, *p4; | |
341 | ||
5a03ea99 | 342 | uint8_t *keyBlock = NULL; |
343 | uint64_t key64; | |
344 | ||
345 | int isOK = 1; | |
346 | ||
347 | uint64_t next_print_time = 0; | |
348 | uint64_t start_time; | |
349 | float brute_force_time; | |
350 | float brute_force_per_second; | |
351 | ||
fdd9395d OM |
352 | // flush queue |
353 | (void)WaitForResponseTimeout(CMD_ACK,NULL,100); | |
354 | ||
355 | UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}}; | |
356 | memcpy(c.d.asBytes, key, 6); | |
357 | SendCommand(&c); | |
358 | ||
5a03ea99 | 359 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { |
360 | // some cards can cause it to get stuck in a loop, so break out of it | |
361 | UsbCommand c = {CMD_PING}; | |
362 | SendCommand(&c); | |
363 | (void)WaitForResponseTimeout(CMD_ACK,NULL,500); | |
fdd9395d OM |
364 | return -1; |
365 | } | |
366 | ||
367 | if (resp.arg[0]) { | |
368 | return resp.arg[0]; // error during nested | |
369 | } | |
370 | ||
371 | memcpy(&uid, resp.d.asBytes, 4); | |
372 | PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8); | |
373 | ||
374 | for (i = 0; i < 2; i++) { | |
375 | statelists[i].blockNo = resp.arg[2] & 0xff; | |
376 | statelists[i].keyType = (resp.arg[2] >> 8) & 0xff; | |
377 | statelists[i].uid = uid; | |
378 | memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4); | |
379 | memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4); | |
380 | } | |
381 | ||
5a03ea99 | 382 | if (statelists[0].nt == statelists[1].nt && statelists[0].ks1 == statelists[1].ks1) |
383 | num_unique_nonces = 1; | |
384 | else | |
385 | num_unique_nonces = 2; | |
386 | ||
fdd9395d OM |
387 | // calc keys |
388 | ||
389 | pthread_t thread_id[2]; | |
390 | ||
391 | // create and run worker threads | |
392 | for (i = 0; i < 2; i++) { | |
393 | pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]); | |
394 | } | |
395 | ||
396 | // wait for threads to terminate: | |
397 | for (i = 0; i < 2; i++) { | |
398 | pthread_join(thread_id[i], (void*)&statelists[i].head.slhead); | |
399 | } | |
400 | ||
401 | ||
402 | // the first 16 Bits of the cryptostate already contain part of our key. | |
403 | // Create the intersection of the two lists based on these 16 Bits and | |
404 | // roll back the cryptostate | |
405 | p1 = p3 = statelists[0].head.slhead; | |
406 | p2 = p4 = statelists[1].head.slhead; | |
407 | while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) { | |
408 | if (Compare16Bits(p1, p2) == 0) { | |
409 | struct Crypto1State savestate, *savep = &savestate; | |
410 | savestate = *p1; | |
411 | while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) { | |
412 | *p3 = *p1; | |
413 | lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0); | |
414 | p3++; | |
415 | p1++; | |
416 | } | |
417 | savestate = *p2; | |
418 | while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) { | |
419 | *p4 = *p2; | |
420 | lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0); | |
421 | p4++; | |
422 | p2++; | |
423 | } | |
424 | } | |
425 | else { | |
426 | while (Compare16Bits(p1, p2) == -1) p1++; | |
427 | while (Compare16Bits(p1, p2) == 1) p2++; | |
428 | } | |
429 | } | |
430 | *(uint64_t*)p3 = -1; | |
431 | *(uint64_t*)p4 = -1; | |
432 | statelists[0].len = p3 - statelists[0].head.slhead; | |
433 | statelists[1].len = p4 - statelists[1].head.slhead; | |
434 | statelists[0].tail.sltail=--p3; | |
435 | statelists[1].tail.sltail=--p4; | |
436 | ||
5a03ea99 | 437 | for (i = 0; i < 2; i++) { |
438 | PrintAndLog("statelist %d: length:%d block:%02d keytype:%d nt:%08X ks1:%08X", i, statelists[i].len, statelists[i].blockNo, statelists[i].keyType, statelists[i].nt, statelists[i].ks1); | |
439 | } | |
440 | ||
fdd9395d OM |
441 | // the statelists now contain possible keys. The key we are searching for must be in the |
442 | // intersection of both lists. Create the intersection: | |
443 | qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64); | |
5a03ea99 | 444 | |
445 | if (num_unique_nonces > 1) { | |
446 | qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64); | |
447 | statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead); | |
448 | } | |
449 | else { | |
450 | PrintAndLog("Nonce 1 and 2 are the same!"); | |
451 | } | |
452 | ||
453 | if (statelists[0].len > 100) { | |
454 | PrintAndLog("We have %d keys to check. This will take a very long time!", statelists[0].len); | |
455 | PrintAndLog("Press button to abort."); | |
456 | } | |
457 | else if (statelists[0].len < 1) { | |
458 | PrintAndLog("No candidate keys to check!"); | |
459 | } | |
460 | else { | |
461 | PrintAndLog("We have %d key(s) to check.", statelists[0].len); | |
462 | } | |
463 | ||
464 | uint32_t max_keys = (statelists[0].len > (USB_CMD_DATA_SIZE / 6)) ? (USB_CMD_DATA_SIZE / 6) : statelists[0].len; | |
465 | keyBlock = calloc(max_keys, 6); | |
466 | ||
467 | if (keyBlock == NULL) { | |
468 | free(statelists[0].head.slhead); | |
469 | free(statelists[1].head.slhead); | |
470 | return -4; | |
471 | } | |
fdd9395d OM |
472 | |
473 | memset(resultKey, 0, 6); | |
5a03ea99 | 474 | start_time = msclock(); |
475 | next_print_time = start_time + 1 * 1000; | |
fdd9395d | 476 | // The list may still contain several key candidates. Test each of them with mfCheckKeys |
5a03ea99 | 477 | for (i = 0; i < statelists[0].len; i+=max_keys) { |
478 | if (next_print_time <= msclock()) { | |
479 | brute_force_per_second = ((float)i) / (((float)(msclock() - start_time)) / 1000.0); | |
480 | brute_force_time = ((float)(statelists[0].len - i)) / brute_force_per_second; | |
481 | next_print_time = msclock() + 10 * 1000; | |
482 | PrintAndLog(" %8d keys left | %5.1f keys/sec | worst case %6.1f seconds remaining", statelists[0].len - i, brute_force_per_second, brute_force_time); | |
483 | } | |
484 | ||
485 | if ((i+max_keys) >= statelists[0].len) | |
486 | max_keys = statelists[0].len - i; | |
487 | ||
488 | for (j = 0; j < max_keys; j++) { | |
489 | crypto1_get_lfsr(statelists[0].head.slhead + i + j, &key64); | |
490 | num_to_bytes(key64, 6, keyBlock+(j*6)); | |
491 | } | |
492 | ||
fdd9395d | 493 | key64 = 0; |
5a03ea99 | 494 | isOK = mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, true, max_keys, keyBlock, &key64); |
495 | ||
496 | if (isOK == 1) { // timeout | |
497 | isOK = -1; | |
498 | break; | |
499 | } | |
500 | else if (isOK < 0) { // -2 is button pressed | |
501 | break; | |
502 | } | |
503 | else if (!isOK) { | |
fdd9395d OM |
504 | num_to_bytes(key64, 6, resultKey); |
505 | break; | |
506 | } | |
507 | } | |
508 | ||
5a03ea99 | 509 | if (isOK == 0 && statelists[0].len != 1) |
510 | PrintAndLog("Key found in %0.2f seconds after checking %d keys\n", ((float)(msclock() - start_time)) / 1000.0, i+max_keys); | |
511 | ||
fdd9395d OM |
512 | free(statelists[0].head.slhead); |
513 | free(statelists[1].head.slhead); | |
5a03ea99 | 514 | free(keyBlock); |
fdd9395d | 515 | |
5a03ea99 | 516 | return isOK; |
fdd9395d OM |
517 | } |
518 | ||
519 | // MIFARE | |
520 | int mfReadSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, uint8_t *data) { | |
521 | ||
522 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}}; | |
523 | memcpy(c.d.asBytes, key, 6); | |
524 | clearCommandBuffer(); | |
525 | SendCommand(&c); | |
526 | ||
527 | UsbCommand resp; | |
528 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { | |
529 | uint8_t isOK = resp.arg[0] & 0xff; | |
530 | ||
531 | if (isOK) { | |
532 | memcpy(data, resp.d.asBytes, mfNumBlocksPerSector(sectorNo) * 16); | |
533 | return 0; | |
534 | } else { | |
535 | return 1; | |
536 | } | |
537 | } else { | |
538 | PrintAndLogEx(ERR, "Command execute timeout"); | |
539 | return 2; | |
540 | } | |
541 | ||
542 | return 0; | |
543 | } | |
544 | ||
545 | // EMULATOR | |
546 | ||
547 | int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) { | |
548 | UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}}; | |
549 | SendCommand(&c); | |
550 | ||
551 | UsbCommand resp; | |
552 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1; | |
553 | memcpy(data, resp.d.asBytes, blocksCount * 16); | |
554 | return 0; | |
555 | } | |
556 | ||
557 | int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) { | |
558 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}}; | |
559 | memcpy(c.d.asBytes, data, blocksCount * 16); | |
560 | SendCommand(&c); | |
561 | return 0; | |
562 | } | |
563 | ||
564 | // "MAGIC" CARD | |
565 | ||
566 | int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) { | |
567 | uint8_t isOK = 0; | |
568 | ||
569 | UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}}; | |
570 | SendCommand(&c); | |
571 | ||
572 | UsbCommand resp; | |
573 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
574 | isOK = resp.arg[0] & 0xff; | |
575 | memcpy(data, resp.d.asBytes, 16); | |
576 | if (!isOK) return 2; | |
577 | } else { | |
578 | PrintAndLog("Command execute timeout"); | |
579 | return 1; | |
580 | } | |
581 | return 0; | |
582 | } | |
583 | ||
584 | int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) { | |
585 | ||
586 | uint8_t isOK = 0; | |
587 | UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}}; | |
588 | memcpy(c.d.asBytes, data, 16); | |
589 | SendCommand(&c); | |
590 | ||
591 | UsbCommand resp; | |
592 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { | |
593 | isOK = resp.arg[0] & 0xff; | |
594 | if (uid != NULL) | |
595 | memcpy(uid, resp.d.asBytes, 4); | |
596 | if (!isOK) | |
597 | return 2; | |
598 | } else { | |
599 | PrintAndLog("Command execute timeout"); | |
600 | return 1; | |
601 | } | |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
606 | int mfCWipe(uint32_t numSectors, bool gen1b, bool wantWipe, bool wantFill) { | |
607 | uint8_t isOK = 0; | |
608 | uint8_t cmdParams = wantWipe + wantFill * 0x02 + gen1b * 0x04; | |
609 | UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, cmdParams, 0}}; | |
610 | SendCommand(&c); | |
611 | ||
612 | UsbCommand resp; | |
613 | WaitForResponse(CMD_ACK,&resp); | |
614 | isOK = resp.arg[0] & 0xff; | |
615 | ||
616 | return isOK; | |
617 | } | |
618 | ||
619 | int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) { | |
620 | uint8_t oldblock0[16] = {0x00}; | |
621 | uint8_t block0[16] = {0x00}; | |
622 | int gen = 0, res; | |
623 | ||
624 | gen = mfCIdentify(); | |
625 | ||
626 | /* generation 1a magic card by default */ | |
627 | uint8_t cmdParams = CSETBLOCK_SINGLE_OPER; | |
628 | if (gen == 2) { | |
629 | /* generation 1b magic card */ | |
630 | cmdParams = CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B; | |
631 | } | |
632 | ||
633 | res = mfCGetBlock(0, oldblock0, cmdParams); | |
634 | ||
635 | if (res == 0) { | |
636 | memcpy(block0, oldblock0, 16); | |
637 | PrintAndLog("old block 0: %s", sprint_hex(block0,16)); | |
638 | } else { | |
639 | PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0."); | |
640 | } | |
641 | ||
642 | // fill in the new values | |
643 | // UID | |
644 | memcpy(block0, uid, 4); | |
645 | // Mifare UID BCC | |
646 | block0[4] = block0[0] ^ block0[1] ^ block0[2] ^ block0[3]; | |
647 | // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed) | |
648 | if (sak != NULL) | |
649 | block0[5] = sak[0]; | |
650 | if (atqa != NULL) { | |
651 | block0[6] = atqa[1]; | |
652 | block0[7] = atqa[0]; | |
653 | } | |
654 | PrintAndLog("new block 0: %s", sprint_hex(block0, 16)); | |
655 | ||
656 | res = mfCSetBlock(0, block0, oldUID, false, cmdParams); | |
657 | if (res) { | |
658 | PrintAndLog("Can't set block 0. Error: %d", res); | |
659 | return res; | |
660 | } | |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
665 | int mfCIdentify() { | |
666 | UsbCommand c = {CMD_MIFARE_CIDENT, {0, 0, 0}}; | |
667 | SendCommand(&c); | |
668 | UsbCommand resp; | |
669 | WaitForResponse(CMD_ACK,&resp); | |
670 | ||
671 | uint8_t isGeneration = resp.arg[0] & 0xff; | |
672 | switch( isGeneration ){ | |
673 | case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break; | |
674 | case 2: PrintAndLog("Chinese magic backdoor command (GEN 1b) detected"); break; | |
675 | default: PrintAndLog("No chinese magic backdoor command detected"); break; | |
676 | } | |
677 | ||
678 | return (int) isGeneration; | |
679 | } | |
680 | ||
681 | ||
682 | // SNIFFER | |
683 | ||
684 | // constants | |
685 | static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00}; | |
686 | ||
687 | // variables | |
688 | char logHexFileName[FILE_PATH_SIZE] = {0x00}; | |
689 | static uint8_t traceCard[4096] = {0x00}; | |
690 | static char traceFileName[FILE_PATH_SIZE] = {0x00}; | |
691 | static int traceState = TRACE_IDLE; | |
692 | static uint8_t traceCurBlock = 0; | |
693 | static uint8_t traceCurKey = 0; | |
694 | ||
695 | struct Crypto1State *traceCrypto1 = NULL; | |
696 | ||
697 | struct Crypto1State *revstate; | |
698 | uint64_t lfsr; | |
699 | uint64_t ui64Key; | |
700 | uint32_t ks2; | |
701 | uint32_t ks3; | |
702 | ||
703 | uint32_t uid; // serial number | |
704 | uint32_t nt; // tag challenge | |
705 | uint32_t nt_enc; // encrypted tag challenge | |
706 | uint8_t nt_enc_par; // encrypted tag challenge parity | |
707 | uint32_t nr_enc; // encrypted reader challenge | |
708 | uint32_t ar_enc; // encrypted reader response | |
709 | uint8_t ar_enc_par; // encrypted reader response parity | |
710 | uint32_t at_enc; // encrypted tag response | |
711 | uint8_t at_enc_par; // encrypted tag response parity | |
712 | ||
713 | int isTraceCardEmpty(void) { | |
714 | return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0)); | |
715 | } | |
716 | ||
717 | int isBlockEmpty(int blockN) { | |
718 | for (int i = 0; i < 16; i++) | |
719 | if (traceCard[blockN * 16 + i] != 0) return 0; | |
720 | ||
721 | return 1; | |
722 | } | |
723 | ||
724 | int isBlockTrailer(int blockN) { | |
725 | return ((blockN & 0x03) == 0x03); | |
726 | } | |
727 | ||
728 | int saveTraceCard(void) { | |
729 | FILE * f; | |
730 | ||
731 | if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0; | |
732 | ||
733 | f = fopen(traceFileName, "w+"); | |
734 | if ( !f ) return 1; | |
735 | ||
736 | for (int i = 0; i < 64; i++) { // blocks | |
737 | for (int j = 0; j < 16; j++) // bytes | |
738 | fprintf(f, "%02x", *(traceCard + i * 16 + j)); | |
739 | if (i < 63) | |
740 | fprintf(f,"\n"); | |
741 | } | |
742 | fclose(f); | |
743 | return 0; | |
744 | } | |
745 | ||
746 | int loadTraceCard(uint8_t *tuid) { | |
747 | FILE * f; | |
748 | char buf[64] = {0x00}; | |
749 | uint8_t buf8[64] = {0x00}; | |
750 | int i, blockNum; | |
751 | ||
752 | if (!isTraceCardEmpty()) | |
753 | saveTraceCard(); | |
754 | ||
755 | memset(traceCard, 0x00, 4096); | |
756 | memcpy(traceCard, tuid + 3, 4); | |
757 | ||
758 | FillFileNameByUID(traceFileName, tuid, ".eml", 7); | |
759 | ||
760 | f = fopen(traceFileName, "r"); | |
761 | if (!f) return 1; | |
762 | ||
763 | blockNum = 0; | |
764 | ||
765 | while(!feof(f)){ | |
766 | ||
767 | memset(buf, 0, sizeof(buf)); | |
768 | if (fgets(buf, sizeof(buf), f) == NULL) { | |
769 | PrintAndLog("File reading error."); | |
770 | fclose(f); | |
771 | return 2; | |
772 | } | |
773 | ||
774 | if (strlen(buf) < 32){ | |
775 | if (feof(f)) break; | |
776 | PrintAndLog("File content error. Block data must include 32 HEX symbols"); | |
777 | fclose(f); | |
778 | return 2; | |
779 | } | |
780 | for (i = 0; i < 32; i += 2) | |
781 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]); | |
782 | ||
783 | memcpy(traceCard + blockNum * 16, buf8, 16); | |
784 | ||
785 | blockNum++; | |
786 | } | |
787 | fclose(f); | |
788 | ||
789 | return 0; | |
790 | } | |
791 | ||
792 | int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) { | |
793 | ||
794 | if (traceCrypto1) | |
795 | crypto1_destroy(traceCrypto1); | |
796 | ||
797 | traceCrypto1 = NULL; | |
798 | ||
799 | if (wantSaveToEmlFile) | |
800 | loadTraceCard(tuid); | |
801 | ||
802 | traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3]; | |
803 | traceCard[5] = sak; | |
804 | memcpy(&traceCard[6], atqa, 2); | |
805 | traceCurBlock = 0; | |
806 | uid = bytes_to_num(tuid + 3, 4); | |
807 | ||
808 | traceState = TRACE_IDLE; | |
809 | ||
810 | return 0; | |
811 | } | |
812 | ||
813 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){ | |
814 | uint8_t bt = 0; | |
815 | int i; | |
816 | ||
817 | if (len != 1) { | |
818 | for (i = 0; i < len; i++) | |
819 | data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i]; | |
820 | } else { | |
821 | bt = 0; | |
822 | for (i = 0; i < 4; i++) | |
823 | bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i; | |
824 | ||
825 | data[0] = bt; | |
826 | } | |
827 | return; | |
828 | } | |
829 | ||
830 | bool NTParityCheck(uint32_t ntx) { | |
831 | if ( | |
832 | (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((nt_enc_par >> 5) & 0x01) ^ (nt_enc & 0x01)) || | |
833 | (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((nt_enc_par >> 6) & 0x01) ^ (nt_enc >> 8 & 0x01)) || | |
834 | (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((nt_enc_par >> 7) & 0x01) ^ (nt_enc >> 16 & 0x01)) | |
835 | ) | |
836 | return false; | |
837 | ||
838 | uint32_t ar = prng_successor(ntx, 64); | |
839 | if ( | |
840 | (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ar_enc_par >> 5) & 0x01) ^ (ar_enc & 0x01)) || | |
841 | (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ar_enc_par >> 6) & 0x01) ^ (ar_enc >> 8 & 0x01)) || | |
842 | (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ar_enc_par >> 7) & 0x01) ^ (ar_enc >> 16 & 0x01)) | |
843 | ) | |
844 | return false; | |
845 | ||
846 | uint32_t at = prng_successor(ntx, 96); | |
847 | if ( | |
848 | (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ar_enc_par >> 4) & 0x01) ^ (at_enc >> 24 & 0x01)) || | |
849 | (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((at_enc_par >> 5) & 0x01) ^ (at_enc & 0x01)) || | |
850 | (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((at_enc_par >> 6) & 0x01) ^ (at_enc >> 8 & 0x01)) || | |
851 | (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((at_enc_par >> 7) & 0x01) ^ (at_enc >> 16 & 0x01)) | |
852 | ) | |
853 | return false; | |
854 | ||
855 | return true; | |
856 | } | |
857 | ||
858 | ||
859 | int mfTraceDecode(uint8_t *data_src, int len, uint8_t parity, bool wantSaveToEmlFile) { | |
860 | uint8_t data[64]; | |
861 | ||
862 | if (traceState == TRACE_ERROR) return 1; | |
863 | if (len > 64) { | |
864 | traceState = TRACE_ERROR; | |
865 | return 1; | |
866 | } | |
867 | ||
868 | memcpy(data, data_src, len); | |
869 | if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) { | |
870 | mf_crypto1_decrypt(traceCrypto1, data, len, 0); | |
871 | uint8_t parity[16]; | |
872 | oddparitybuf(data, len, parity); | |
873 | PrintAndLog("dec> %s [%s]", sprint_hex(data, len), printBitsPar(parity, len)); | |
874 | AddLogHex(logHexFileName, "dec> ", data, len); | |
875 | } | |
876 | ||
877 | switch (traceState) { | |
878 | case TRACE_IDLE: | |
879 | // check packet crc16! | |
880 | if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) { | |
881 | PrintAndLog("dec> CRC ERROR!!!"); | |
882 | AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); | |
883 | traceState = TRACE_ERROR; // do not decrypt the next commands | |
884 | return 1; | |
885 | } | |
886 | ||
887 | // AUTHENTICATION | |
888 | if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) { | |
889 | traceState = TRACE_AUTH1; | |
890 | traceCurBlock = data[1]; | |
891 | traceCurKey = data[0] == 60 ? 1:0; | |
892 | return 0; | |
893 | } | |
894 | ||
895 | // READ | |
896 | if ((len ==4) && ((data[0] == 0x30))) { | |
897 | traceState = TRACE_READ_DATA; | |
898 | traceCurBlock = data[1]; | |
899 | return 0; | |
900 | } | |
901 | ||
902 | // WRITE | |
903 | if ((len ==4) && ((data[0] == 0xA0))) { | |
904 | traceState = TRACE_WRITE_OK; | |
905 | traceCurBlock = data[1]; | |
906 | return 0; | |
907 | } | |
908 | ||
909 | // HALT | |
910 | if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) { | |
911 | traceState = TRACE_ERROR; // do not decrypt the next commands | |
912 | return 0; | |
913 | } | |
914 | ||
915 | return 0; | |
916 | break; | |
917 | ||
918 | case TRACE_READ_DATA: | |
919 | if (len == 18) { | |
920 | traceState = TRACE_IDLE; | |
921 | ||
922 | if (isBlockTrailer(traceCurBlock)) { | |
923 | memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4); | |
924 | } else { | |
925 | memcpy(traceCard + traceCurBlock * 16, data, 16); | |
926 | } | |
927 | if (wantSaveToEmlFile) saveTraceCard(); | |
928 | return 0; | |
929 | } else { | |
930 | traceState = TRACE_ERROR; | |
931 | return 1; | |
932 | } | |
933 | break; | |
934 | ||
935 | case TRACE_WRITE_OK: | |
936 | if ((len == 1) && (data[0] == 0x0a)) { | |
937 | traceState = TRACE_WRITE_DATA; | |
938 | ||
939 | return 0; | |
940 | } else { | |
941 | traceState = TRACE_ERROR; | |
942 | return 1; | |
943 | } | |
944 | break; | |
945 | ||
946 | case TRACE_WRITE_DATA: | |
947 | if (len == 18) { | |
948 | traceState = TRACE_IDLE; | |
949 | ||
950 | memcpy(traceCard + traceCurBlock * 16, data, 16); | |
951 | if (wantSaveToEmlFile) saveTraceCard(); | |
952 | return 0; | |
953 | } else { | |
954 | traceState = TRACE_ERROR; | |
955 | return 1; | |
956 | } | |
957 | break; | |
958 | ||
959 | case TRACE_AUTH1: | |
960 | if (len == 4) { | |
961 | traceState = TRACE_AUTH2; | |
962 | if (!traceCrypto1) { | |
963 | nt = bytes_to_num(data, 4); | |
964 | } else { | |
965 | nt_enc = bytes_to_num(data, 4); | |
966 | nt_enc_par = parity; | |
967 | } | |
968 | return 0; | |
969 | } else { | |
970 | traceState = TRACE_ERROR; | |
971 | return 1; | |
972 | } | |
973 | break; | |
974 | ||
975 | case TRACE_AUTH2: | |
976 | if (len == 8) { | |
977 | traceState = TRACE_AUTH_OK; | |
978 | ||
979 | nr_enc = bytes_to_num(data, 4); | |
980 | ar_enc = bytes_to_num(data + 4, 4); | |
981 | ar_enc_par = parity << 4; | |
982 | return 0; | |
983 | } else { | |
984 | traceState = TRACE_ERROR; | |
985 | return 1; | |
986 | } | |
987 | break; | |
988 | ||
989 | case TRACE_AUTH_OK: | |
990 | if (len ==4) { | |
991 | traceState = TRACE_IDLE; | |
992 | ||
993 | at_enc = bytes_to_num(data, 4); | |
994 | at_enc_par = parity; | |
995 | if (!traceCrypto1) { | |
996 | ||
997 | // decode key here) | |
998 | ks2 = ar_enc ^ prng_successor(nt, 64); | |
999 | ks3 = at_enc ^ prng_successor(nt, 96); | |
1000 | revstate = lfsr_recovery64(ks2, ks3); | |
1001 | lfsr_rollback_word(revstate, 0, 0); | |
1002 | lfsr_rollback_word(revstate, 0, 0); | |
1003 | lfsr_rollback_word(revstate, nr_enc, 1); | |
1004 | lfsr_rollback_word(revstate, uid ^ nt, 0); | |
1005 | ||
1006 | crypto1_get_lfsr(revstate, &lfsr); | |
1007 | crypto1_destroy(revstate); | |
1008 | ui64Key = lfsr; | |
1009 | printf("key> probable key:%x%x Prng:%s ks2:%08x ks3:%08x\n", | |
1010 | (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF), | |
1011 | validate_prng_nonce(nt) ? "WEAK": "HARDEND", | |
1012 | ks2, | |
1013 | ks3); | |
1014 | AddLogUint64(logHexFileName, "key> ", lfsr); | |
1015 | } else { | |
1016 | if (validate_prng_nonce(nt)) { | |
1017 | struct Crypto1State *pcs; | |
1018 | pcs = crypto1_create(ui64Key); | |
1019 | uint32_t nt1 = crypto1_word(pcs, nt_enc ^ uid, 1) ^ nt_enc; | |
1020 | uint32_t ar = prng_successor(nt1, 64); | |
1021 | uint32_t at = prng_successor(nt1, 96); | |
1022 | printf("key> nested auth uid: %08x nt: %08x nt_parity: %s ar: %08x at: %08x\n", uid, nt1, printBitsPar(&nt_enc_par, 4), ar, at); | |
1023 | uint32_t nr1 = crypto1_word(pcs, nr_enc, 1) ^ nr_enc; | |
1024 | uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ar_enc; | |
1025 | uint32_t at1 = crypto1_word(pcs, 0, 0) ^ at_enc; | |
1026 | crypto1_destroy(pcs); | |
1027 | printf("key> the same key test. nr1: %08x ar1: %08x at1: %08x \n", nr1, ar1, at1); | |
1028 | ||
1029 | if (NTParityCheck(nt1)) | |
1030 | printf("key> the same key test OK. key=%x%x\n", (unsigned int)((ui64Key & 0xFFFFFFFF00000000) >> 32), (unsigned int)(ui64Key & 0xFFFFFFFF)); | |
1031 | else | |
1032 | printf("key> the same key test. check nt parity error.\n"); | |
1033 | ||
1034 | uint32_t ntc = prng_successor(nt, 90); | |
1035 | uint32_t ntx = 0; | |
1036 | int ntcnt = 0; | |
1037 | for (int i = 0; i < 16383; i++) { | |
1038 | ntc = prng_successor(ntc, 1); | |
1039 | if (NTParityCheck(ntc)){ | |
1040 | if (!ntcnt) | |
1041 | ntx = ntc; | |
1042 | ntcnt++; | |
1043 | } | |
1044 | } | |
1045 | if (ntcnt) | |
1046 | printf("key> nt candidate=%08x nonce distance=%d candidates count=%d\n", ntx, nonce_distance(nt, ntx), ntcnt); | |
1047 | else | |
1048 | printf("key> don't have any nt candidate( \n"); | |
1049 | ||
1050 | nt = ntx; | |
1051 | ks2 = ar_enc ^ prng_successor(ntx, 64); | |
1052 | ks3 = at_enc ^ prng_successor(ntx, 96); | |
1053 | ||
1054 | // decode key | |
1055 | revstate = lfsr_recovery64(ks2, ks3); | |
1056 | lfsr_rollback_word(revstate, 0, 0); | |
1057 | lfsr_rollback_word(revstate, 0, 0); | |
1058 | lfsr_rollback_word(revstate, nr_enc, 1); | |
1059 | lfsr_rollback_word(revstate, uid ^ nt, 0); | |
1060 | ||
1061 | crypto1_get_lfsr(revstate, &lfsr); | |
1062 | crypto1_destroy(revstate); | |
1063 | ui64Key = lfsr; | |
1064 | printf("key> probable key:%x%x ks2:%08x ks3:%08x\n", | |
1065 | (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF), | |
1066 | ks2, | |
1067 | ks3); | |
1068 | AddLogUint64(logHexFileName, "key> ", lfsr); | |
1069 | } else { | |
1070 | printf("key> hardnested not implemented!\n"); | |
1071 | ||
1072 | crypto1_destroy(traceCrypto1); | |
1073 | ||
1074 | // not implemented | |
1075 | traceState = TRACE_ERROR; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | int blockShift = ((traceCurBlock & 0xFC) + 3) * 16; | |
1080 | if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4); | |
1081 | ||
1082 | if (traceCurKey) { | |
1083 | num_to_bytes(lfsr, 6, traceCard + blockShift + 10); | |
1084 | } else { | |
1085 | num_to_bytes(lfsr, 6, traceCard + blockShift); | |
1086 | } | |
1087 | if (wantSaveToEmlFile) saveTraceCard(); | |
1088 | ||
1089 | if (traceCrypto1) { | |
1090 | crypto1_destroy(traceCrypto1); | |
1091 | } | |
1092 | ||
1093 | // set cryptosystem state | |
1094 | traceCrypto1 = lfsr_recovery64(ks2, ks3); | |
1095 | return 0; | |
1096 | } else { | |
1097 | traceState = TRACE_ERROR; | |
1098 | return 1; | |
1099 | } | |
1100 | break; | |
1101 | ||
1102 | default: | |
1103 | traceState = TRACE_ERROR; | |
1104 | return 1; | |
1105 | } | |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | // DECODING | |
1111 | ||
1112 | int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){ | |
1113 | /* | |
1114 | uint32_t nt; // tag challenge | |
1115 | uint32_t ar_enc; // encrypted reader response | |
1116 | uint32_t at_enc; // encrypted tag response | |
1117 | */ | |
1118 | if (traceCrypto1) { | |
1119 | crypto1_destroy(traceCrypto1); | |
1120 | } | |
1121 | ks2 = ar_enc ^ prng_successor(nt, 64); | |
1122 | ks3 = at_enc ^ prng_successor(nt, 96); | |
1123 | traceCrypto1 = lfsr_recovery64(ks2, ks3); | |
1124 | ||
1125 | mf_crypto1_decrypt(traceCrypto1, data, len, 0); | |
1126 | ||
1127 | PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) ); | |
1128 | crypto1_destroy(traceCrypto1); | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | /** validate_prng_nonce | |
1133 | * Determine if nonce is deterministic. ie: Suspectable to Darkside attack. | |
1134 | * returns | |
1135 | * true = weak prng | |
1136 | * false = hardend prng | |
1137 | */ | |
1138 | bool validate_prng_nonce(uint32_t nonce) { | |
1139 | uint16_t *dist = 0; | |
1140 | uint16_t x, i; | |
1141 | ||
1142 | dist = malloc(2 << 16); | |
1143 | if(!dist) | |
1144 | return -1; | |
1145 | ||
1146 | // init prng table: | |
1147 | for (x = i = 1; i; ++i) { | |
1148 | dist[(x & 0xff) << 8 | x >> 8] = i; | |
1149 | x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15; | |
1150 | } | |
1151 | ||
1152 | uint32_t res = (65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535; | |
1153 | ||
1154 | free(dist); | |
1155 | return (res == 16); | |
1156 | } | |
1157 | ||
1158 | /* Detect Tag Prng, | |
1159 | * function performs a partial AUTH, where it tries to authenticate against block0, key A, but only collects tag nonce. | |
1160 | * the tag nonce is check to see if it has a predictable PRNG. | |
1161 | * @returns | |
1162 | * TRUE if tag uses WEAK prng (ie Now the NACK bug also needs to be present for Darkside attack) | |
1163 | * FALSE is tag uses HARDEND prng (ie hardnested attack possible, with known key) | |
1164 | */ | |
1165 | int DetectClassicPrng(void){ | |
1166 | ||
1167 | UsbCommand resp, respA; | |
1168 | uint8_t cmd[] = {0x60, 0x00}; // MIFARE_AUTH_KEYA | |
1169 | uint32_t flags = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_RATS; | |
1170 | ||
1171 | UsbCommand c = {CMD_READER_ISO_14443a, {flags, sizeof(cmd), 0}}; | |
1172 | memcpy(c.d.asBytes, cmd, sizeof(cmd)); | |
1173 | ||
1174 | clearCommandBuffer(); | |
1175 | SendCommand(&c); | |
1176 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { | |
1177 | PrintAndLog("PRNG UID: Reply timeout."); | |
1178 | return -1; | |
1179 | } | |
1180 | ||
1181 | // if select tag failed. | |
1182 | if (resp.arg[0] == 0) { | |
1183 | PrintAndLog("PRNG error: selecting tag failed, can't detect prng."); | |
1184 | return -1; | |
1185 | } | |
1186 | ||
1187 | if (!WaitForResponseTimeout(CMD_ACK, &respA, 5000)) { | |
1188 | PrintAndLog("PRNG data: Reply timeout."); | |
1189 | return -1; | |
1190 | } | |
1191 | ||
1192 | // check respA | |
1193 | if (respA.arg[0] != 4) { | |
1194 | PrintAndLog("PRNG data error: Wrong length: %d", respA.arg[0]); | |
1195 | return -1; | |
1196 | } | |
1197 | ||
1198 | uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]); | |
1199 | return validate_prng_nonce(nonce); | |
1200 | } |