]>
Commit | Line | Data |
---|---|---|
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 "mifarehost.h"\r | |
12 | \r | |
13 | #include <stdio.h>\r | |
14 | #include <stdlib.h>\r | |
15 | #include <string.h>\r | |
16 | #include <pthread.h>\r | |
17 | \r | |
18 | #include "crapto1/crapto1.h"\r | |
19 | #include "proxmark3.h"\r | |
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 | |
25 | \r | |
26 | #include "mifare.h"\r | |
27 | \r | |
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 | |
38 | \r | |
39 | static 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 | |
51 | static 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 | |
57 | p1 = p3 = list1;\r | |
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 | |
76 | static 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 | |
103 | \r | |
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 | |
116 | int 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 | |
133 | \r | |
134 | while (true) {\r | |
135 | clearCommandBuffer();\r | |
136 | SendCommand(&c);\r | |
137 | \r | |
138 | //flush queue\r | |
139 | while (ukbhit()) {\r | |
140 | int c = getchar(); (void) c;\r | |
141 | }\r | |
142 | \r | |
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 | |
151 | \r | |
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 | |
165 | }\r | |
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 | |
176 | PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);\r | |
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 | |
193 | }\r | |
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 | |
210 | }\r | |
211 | \r | |
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 | |
222 | \r | |
223 | return 0;\r | |
224 | }\r | |
225 | \r | |
226 | \r | |
227 | int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r | |
228 | \r | |
229 | *key = -1;\r | |
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 | |
242 | int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){\r | |
243 | \r | |
244 | uint8_t keyPtr = 0;\r | |
245 | \r | |
246 | if (e_sector == NULL)\r | |
247 | return -1;\r | |
248 | \r | |
249 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}}; \r | |
250 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r | |
251 | SendCommand(&c);\r | |
252 | \r | |
253 | UsbCommand resp;\r | |
254 | if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth\r | |
255 | if ((resp.arg[0] & 0xff) != 0x01) return 2;\r | |
256 | \r | |
257 | bool foundAKey = false;\r | |
258 | for(int sec = 0; sec < sectorCnt; sec++){\r | |
259 | for(int keyAB = 0; keyAB < 2; keyAB++){\r | |
260 | keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);\r | |
261 | if (keyPtr){\r | |
262 | e_sector[sec].foundKey[keyAB] = true;\r | |
263 | e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);\r | |
264 | foundAKey = true;\r | |
265 | }\r | |
266 | }\r | |
267 | }\r | |
268 | return foundAKey ? 0 : 3;\r | |
269 | }\r | |
270 | \r | |
271 | // Compare 16 Bits out of cryptostate\r | |
272 | int Compare16Bits(const void * a, const void * b) {\r | |
273 | if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r | |
274 | else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r | |
275 | else return -1;\r | |
276 | }\r | |
277 | \r | |
278 | typedef\r | |
279 | struct {\r | |
280 | union {\r | |
281 | struct Crypto1State *slhead;\r | |
282 | uint64_t *keyhead;\r | |
283 | } head;\r | |
284 | union {\r | |
285 | struct Crypto1State *sltail;\r | |
286 | uint64_t *keytail;\r | |
287 | } tail;\r | |
288 | uint32_t len;\r | |
289 | uint32_t uid;\r | |
290 | uint32_t blockNo;\r | |
291 | uint32_t keyType;\r | |
292 | uint32_t nt;\r | |
293 | uint32_t ks1;\r | |
294 | } StateList_t;\r | |
295 | \r | |
296 | \r | |
297 | // wrapper function for multi-threaded lfsr_recovery32\r | |
298 | void* nested_worker_thread(void *arg)\r | |
299 | {\r | |
300 | struct Crypto1State *p1;\r | |
301 | StateList_t *statelist = arg;\r | |
302 | \r | |
303 | statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r | |
304 | for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r | |
305 | statelist->len = p1 - statelist->head.slhead;\r | |
306 | statelist->tail.sltail = --p1;\r | |
307 | qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r | |
308 | \r | |
309 | return statelist->head.slhead;\r | |
310 | }\r | |
311 | \r | |
312 | int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate)\r | |
313 | {\r | |
314 | uint16_t i;\r | |
315 | uint32_t uid;\r | |
316 | UsbCommand resp;\r | |
317 | \r | |
318 | StateList_t statelists[2];\r | |
319 | struct Crypto1State *p1, *p2, *p3, *p4;\r | |
320 | \r | |
321 | // flush queue\r | |
322 | WaitForResponseTimeout(CMD_ACK, NULL, 100);\r | |
323 | \r | |
324 | UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r | |
325 | memcpy(c.d.asBytes, key, 6);\r | |
326 | SendCommand(&c);\r | |
327 | \r | |
328 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r | |
329 | return -1;\r | |
330 | }\r | |
331 | \r | |
332 | if (resp.arg[0]) {\r | |
333 | return resp.arg[0]; // error during nested\r | |
334 | }\r | |
335 | \r | |
336 | memcpy(&uid, resp.d.asBytes, 4);\r | |
337 | PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);\r | |
338 | \r | |
339 | for (i = 0; i < 2; i++) {\r | |
340 | statelists[i].blockNo = resp.arg[2] & 0xff;\r | |
341 | statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r | |
342 | statelists[i].uid = uid;\r | |
343 | memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r | |
344 | memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r | |
345 | }\r | |
346 | \r | |
347 | // calc keys\r | |
348 | \r | |
349 | pthread_t thread_id[2];\r | |
350 | \r | |
351 | // create and run worker threads\r | |
352 | for (i = 0; i < 2; i++) {\r | |
353 | pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r | |
354 | }\r | |
355 | \r | |
356 | // wait for threads to terminate:\r | |
357 | for (i = 0; i < 2; i++) {\r | |
358 | pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r | |
359 | }\r | |
360 | \r | |
361 | \r | |
362 | // the first 16 Bits of the cryptostate already contain part of our key.\r | |
363 | // Create the intersection of the two lists based on these 16 Bits and\r | |
364 | // roll back the cryptostate\r | |
365 | p1 = p3 = statelists[0].head.slhead;\r | |
366 | p2 = p4 = statelists[1].head.slhead;\r | |
367 | while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r | |
368 | if (Compare16Bits(p1, p2) == 0) {\r | |
369 | struct Crypto1State savestate, *savep = &savestate;\r | |
370 | savestate = *p1;\r | |
371 | while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r | |
372 | *p3 = *p1;\r | |
373 | lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r | |
374 | p3++;\r | |
375 | p1++;\r | |
376 | }\r | |
377 | savestate = *p2;\r | |
378 | while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r | |
379 | *p4 = *p2;\r | |
380 | lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r | |
381 | p4++;\r | |
382 | p2++;\r | |
383 | }\r | |
384 | }\r | |
385 | else {\r | |
386 | while (Compare16Bits(p1, p2) == -1) p1++;\r | |
387 | while (Compare16Bits(p1, p2) == 1) p2++;\r | |
388 | }\r | |
389 | }\r | |
390 | *(uint64_t*)p3 = -1;\r | |
391 | *(uint64_t*)p4 = -1;\r | |
392 | statelists[0].len = p3 - statelists[0].head.slhead;\r | |
393 | statelists[1].len = p4 - statelists[1].head.slhead;\r | |
394 | statelists[0].tail.sltail=--p3;\r | |
395 | statelists[1].tail.sltail=--p4;\r | |
396 | \r | |
397 | // the statelists now contain possible keys. The key we are searching for must be in the\r | |
398 | // intersection of both lists. Create the intersection:\r | |
399 | qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64);\r | |
400 | qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64);\r | |
401 | statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead);\r | |
402 | \r | |
403 | memset(resultKey, 0, 6);\r | |
404 | // The list may still contain several key candidates. Test each of them with mfCheckKeys\r | |
405 | for (i = 0; i < statelists[0].len; i++) {\r | |
406 | uint8_t keyBlock[6];\r | |
407 | uint64_t key64;\r | |
408 | crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r | |
409 | num_to_bytes(key64, 6, keyBlock);\r | |
410 | key64 = 0;\r | |
411 | if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r | |
412 | num_to_bytes(key64, 6, resultKey);\r | |
413 | break;\r | |
414 | }\r | |
415 | }\r | |
416 | \r | |
417 | free(statelists[0].head.slhead);\r | |
418 | free(statelists[1].head.slhead);\r | |
419 | \r | |
420 | return 0;\r | |
421 | }\r | |
422 | \r | |
423 | // EMULATOR\r | |
424 | \r | |
425 | int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
426 | UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r | |
427 | SendCommand(&c);\r | |
428 | \r | |
429 | UsbCommand resp;\r | |
430 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r | |
431 | memcpy(data, resp.d.asBytes, blocksCount * 16);\r | |
432 | return 0;\r | |
433 | }\r | |
434 | \r | |
435 | int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
436 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};\r | |
437 | memcpy(c.d.asBytes, data, blocksCount * 16);\r | |
438 | SendCommand(&c);\r | |
439 | return 0;\r | |
440 | }\r | |
441 | \r | |
442 | // "MAGIC" CARD\r | |
443 | \r | |
444 | int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r | |
445 | uint8_t isOK = 0;\r | |
446 | \r | |
447 | UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r | |
448 | SendCommand(&c);\r | |
449 | \r | |
450 | UsbCommand resp;\r | |
451 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
452 | isOK = resp.arg[0] & 0xff;\r | |
453 | memcpy(data, resp.d.asBytes, 16);\r | |
454 | if (!isOK) return 2;\r | |
455 | } else {\r | |
456 | PrintAndLog("Command execute timeout");\r | |
457 | return 1;\r | |
458 | }\r | |
459 | return 0;\r | |
460 | }\r | |
461 | \r | |
462 | int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {\r | |
463 | \r | |
464 | uint8_t isOK = 0;\r | |
465 | UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};\r | |
466 | memcpy(c.d.asBytes, data, 16);\r | |
467 | SendCommand(&c);\r | |
468 | \r | |
469 | UsbCommand resp;\r | |
470 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r | |
471 | isOK = resp.arg[0] & 0xff;\r | |
472 | if (uid != NULL)\r | |
473 | memcpy(uid, resp.d.asBytes, 4);\r | |
474 | if (!isOK)\r | |
475 | return 2;\r | |
476 | } else {\r | |
477 | PrintAndLog("Command execute timeout");\r | |
478 | return 1;\r | |
479 | }\r | |
480 | \r | |
481 | return 0;\r | |
482 | }\r | |
483 | \r | |
484 | int mfCWipe(uint32_t numSectors, bool gen1b, bool wantWipe, bool wantFill) {\r | |
485 | uint8_t isOK = 0;\r | |
486 | uint8_t cmdParams = wantWipe + wantFill * 0x02 + gen1b * 0x04;\r | |
487 | UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, cmdParams, 0}};\r | |
488 | SendCommand(&c);\r | |
489 | \r | |
490 | UsbCommand resp;\r | |
491 | WaitForResponse(CMD_ACK,&resp);\r | |
492 | isOK = resp.arg[0] & 0xff;\r | |
493 | \r | |
494 | return isOK;\r | |
495 | }\r | |
496 | \r | |
497 | int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) {\r | |
498 | uint8_t oldblock0[16] = {0x00};\r | |
499 | uint8_t block0[16] = {0x00};\r | |
500 | int gen = 0, res;\r | |
501 | \r | |
502 | gen = mfCIdentify();\r | |
503 | \r | |
504 | /* generation 1a magic card by default */\r | |
505 | uint8_t cmdParams = CSETBLOCK_SINGLE_OPER;\r | |
506 | if (gen == 2) {\r | |
507 | /* generation 1b magic card */\r | |
508 | cmdParams = CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B;\r | |
509 | }\r | |
510 | \r | |
511 | res = mfCGetBlock(0, oldblock0, cmdParams);\r | |
512 | \r | |
513 | if (res == 0) {\r | |
514 | memcpy(block0, oldblock0, 16);\r | |
515 | PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r | |
516 | } else {\r | |
517 | PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r | |
518 | }\r | |
519 | \r | |
520 | // fill in the new values\r | |
521 | // UID\r | |
522 | memcpy(block0, uid, 4);\r | |
523 | // Mifare UID BCC\r | |
524 | block0[4] = block0[0] ^ block0[1] ^ block0[2] ^ block0[3];\r | |
525 | // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r | |
526 | if (sak != NULL)\r | |
527 | block0[5] = sak[0];\r | |
528 | if (atqa != NULL) {\r | |
529 | block0[6] = atqa[1];\r | |
530 | block0[7] = atqa[0];\r | |
531 | }\r | |
532 | PrintAndLog("new block 0: %s", sprint_hex(block0, 16));\r | |
533 | \r | |
534 | res = mfCSetBlock(0, block0, oldUID, false, cmdParams);\r | |
535 | if (res) {\r | |
536 | PrintAndLog("Can't set block 0. Error: %d", res);\r | |
537 | return res;\r | |
538 | }\r | |
539 | \r | |
540 | return 0;\r | |
541 | }\r | |
542 | \r | |
543 | int mfCIdentify() {\r | |
544 | UsbCommand c = {CMD_MIFARE_CIDENT, {0, 0, 0}};\r | |
545 | SendCommand(&c);\r | |
546 | UsbCommand resp;\r | |
547 | WaitForResponse(CMD_ACK,&resp);\r | |
548 | \r | |
549 | uint8_t isGeneration = resp.arg[0] & 0xff;\r | |
550 | switch( isGeneration ){\r | |
551 | case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break;\r | |
552 | case 2: PrintAndLog("Chinese magic backdoor command (GEN 1b) detected"); break;\r | |
553 | default: PrintAndLog("No chinese magic backdoor command detected"); break;\r | |
554 | }\r | |
555 | \r | |
556 | return (int) isGeneration;\r | |
557 | }\r | |
558 | \r | |
559 | \r | |
560 | // SNIFFER\r | |
561 | \r | |
562 | // constants\r | |
563 | static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r | |
564 | \r | |
565 | // variables\r | |
566 | char logHexFileName[FILE_PATH_SIZE] = {0x00};\r | |
567 | static uint8_t traceCard[4096] = {0x00};\r | |
568 | static char traceFileName[FILE_PATH_SIZE] = {0x00};\r | |
569 | static int traceState = TRACE_IDLE;\r | |
570 | static uint8_t traceCurBlock = 0;\r | |
571 | static uint8_t traceCurKey = 0;\r | |
572 | \r | |
573 | struct Crypto1State *traceCrypto1 = NULL;\r | |
574 | \r | |
575 | struct Crypto1State *revstate;\r | |
576 | uint64_t lfsr;\r | |
577 | uint32_t ks2;\r | |
578 | uint32_t ks3;\r | |
579 | \r | |
580 | uint32_t uid; // serial number\r | |
581 | uint32_t nt; // tag challenge\r | |
582 | uint32_t nr_enc; // encrypted reader challenge\r | |
583 | uint32_t ar_enc; // encrypted reader response\r | |
584 | uint32_t at_enc; // encrypted tag response\r | |
585 | \r | |
586 | int isTraceCardEmpty(void) {\r | |
587 | return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r | |
588 | }\r | |
589 | \r | |
590 | int isBlockEmpty(int blockN) {\r | |
591 | for (int i = 0; i < 16; i++)\r | |
592 | if (traceCard[blockN * 16 + i] != 0) return 0;\r | |
593 | \r | |
594 | return 1;\r | |
595 | }\r | |
596 | \r | |
597 | int isBlockTrailer(int blockN) {\r | |
598 | return ((blockN & 0x03) == 0x03);\r | |
599 | }\r | |
600 | \r | |
601 | int saveTraceCard(void) {\r | |
602 | FILE * f;\r | |
603 | \r | |
604 | if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r | |
605 | \r | |
606 | f = fopen(traceFileName, "w+");\r | |
607 | if ( !f ) return 1;\r | |
608 | \r | |
609 | for (int i = 0; i < 64; i++) { // blocks\r | |
610 | for (int j = 0; j < 16; j++) // bytes\r | |
611 | fprintf(f, "%02x", *(traceCard + i * 16 + j));\r | |
612 | fprintf(f,"\n");\r | |
613 | }\r | |
614 | fclose(f);\r | |
615 | return 0;\r | |
616 | }\r | |
617 | \r | |
618 | int loadTraceCard(uint8_t *tuid) {\r | |
619 | FILE * f;\r | |
620 | char buf[64] = {0x00};\r | |
621 | uint8_t buf8[64] = {0x00};\r | |
622 | int i, blockNum;\r | |
623 | \r | |
624 | if (!isTraceCardEmpty())\r | |
625 | saveTraceCard();\r | |
626 | \r | |
627 | memset(traceCard, 0x00, 4096);\r | |
628 | memcpy(traceCard, tuid + 3, 4);\r | |
629 | \r | |
630 | FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r | |
631 | \r | |
632 | f = fopen(traceFileName, "r");\r | |
633 | if (!f) return 1;\r | |
634 | \r | |
635 | blockNum = 0;\r | |
636 | \r | |
637 | while(!feof(f)){\r | |
638 | \r | |
639 | memset(buf, 0, sizeof(buf));\r | |
640 | if (fgets(buf, sizeof(buf), f) == NULL) {\r | |
641 | PrintAndLog("File reading error.");\r | |
642 | fclose(f);\r | |
643 | return 2;\r | |
644 | }\r | |
645 | \r | |
646 | if (strlen(buf) < 32){\r | |
647 | if (feof(f)) break;\r | |
648 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
649 | fclose(f);\r | |
650 | return 2;\r | |
651 | }\r | |
652 | for (i = 0; i < 32; i += 2)\r | |
653 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
654 | \r | |
655 | memcpy(traceCard + blockNum * 16, buf8, 16);\r | |
656 | \r | |
657 | blockNum++;\r | |
658 | }\r | |
659 | fclose(f);\r | |
660 | \r | |
661 | return 0;\r | |
662 | }\r | |
663 | \r | |
664 | int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r | |
665 | \r | |
666 | if (traceCrypto1)\r | |
667 | crypto1_destroy(traceCrypto1);\r | |
668 | \r | |
669 | traceCrypto1 = NULL;\r | |
670 | \r | |
671 | if (wantSaveToEmlFile)\r | |
672 | loadTraceCard(tuid);\r | |
673 | \r | |
674 | traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r | |
675 | traceCard[5] = sak;\r | |
676 | memcpy(&traceCard[6], atqa, 2);\r | |
677 | traceCurBlock = 0;\r | |
678 | uid = bytes_to_num(tuid + 3, 4);\r | |
679 | \r | |
680 | traceState = TRACE_IDLE;\r | |
681 | \r | |
682 | return 0;\r | |
683 | }\r | |
684 | \r | |
685 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r | |
686 | uint8_t bt = 0;\r | |
687 | int i;\r | |
688 | \r | |
689 | if (len != 1) {\r | |
690 | for (i = 0; i < len; i++)\r | |
691 | data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r | |
692 | } else {\r | |
693 | bt = 0;\r | |
694 | for (i = 0; i < 4; i++)\r | |
695 | bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r | |
696 | \r | |
697 | data[0] = bt;\r | |
698 | }\r | |
699 | return;\r | |
700 | }\r | |
701 | \r | |
702 | \r | |
703 | int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r | |
704 | uint8_t data[64];\r | |
705 | \r | |
706 | if (traceState == TRACE_ERROR) return 1;\r | |
707 | if (len > 64) {\r | |
708 | traceState = TRACE_ERROR;\r | |
709 | return 1;\r | |
710 | }\r | |
711 | \r | |
712 | memcpy(data, data_src, len);\r | |
713 | if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r | |
714 | mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r | |
715 | PrintAndLog("dec> %s", sprint_hex(data, len));\r | |
716 | AddLogHex(logHexFileName, "dec> ", data, len);\r | |
717 | }\r | |
718 | \r | |
719 | switch (traceState) {\r | |
720 | case TRACE_IDLE:\r | |
721 | // check packet crc16!\r | |
722 | if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r | |
723 | PrintAndLog("dec> CRC ERROR!!!");\r | |
724 | AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!");\r | |
725 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
726 | return 1;\r | |
727 | }\r | |
728 | \r | |
729 | // AUTHENTICATION\r | |
730 | if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r | |
731 | traceState = TRACE_AUTH1;\r | |
732 | traceCurBlock = data[1];\r | |
733 | traceCurKey = data[0] == 60 ? 1:0;\r | |
734 | return 0;\r | |
735 | }\r | |
736 | \r | |
737 | // READ\r | |
738 | if ((len ==4) && ((data[0] == 0x30))) {\r | |
739 | traceState = TRACE_READ_DATA;\r | |
740 | traceCurBlock = data[1];\r | |
741 | return 0;\r | |
742 | }\r | |
743 | \r | |
744 | // WRITE\r | |
745 | if ((len ==4) && ((data[0] == 0xA0))) {\r | |
746 | traceState = TRACE_WRITE_OK;\r | |
747 | traceCurBlock = data[1];\r | |
748 | return 0;\r | |
749 | }\r | |
750 | \r | |
751 | // HALT\r | |
752 | if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r | |
753 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
754 | return 0;\r | |
755 | }\r | |
756 | \r | |
757 | return 0;\r | |
758 | break;\r | |
759 | \r | |
760 | case TRACE_READ_DATA:\r | |
761 | if (len == 18) {\r | |
762 | traceState = TRACE_IDLE;\r | |
763 | \r | |
764 | if (isBlockTrailer(traceCurBlock)) {\r | |
765 | memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r | |
766 | } else {\r | |
767 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
768 | }\r | |
769 | if (wantSaveToEmlFile) saveTraceCard();\r | |
770 | return 0;\r | |
771 | } else {\r | |
772 | traceState = TRACE_ERROR;\r | |
773 | return 1;\r | |
774 | }\r | |
775 | break;\r | |
776 | \r | |
777 | case TRACE_WRITE_OK:\r | |
778 | if ((len == 1) && (data[0] == 0x0a)) {\r | |
779 | traceState = TRACE_WRITE_DATA;\r | |
780 | \r | |
781 | return 0;\r | |
782 | } else {\r | |
783 | traceState = TRACE_ERROR;\r | |
784 | return 1;\r | |
785 | }\r | |
786 | break;\r | |
787 | \r | |
788 | case TRACE_WRITE_DATA:\r | |
789 | if (len == 18) {\r | |
790 | traceState = TRACE_IDLE;\r | |
791 | \r | |
792 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
793 | if (wantSaveToEmlFile) saveTraceCard();\r | |
794 | return 0;\r | |
795 | } else {\r | |
796 | traceState = TRACE_ERROR;\r | |
797 | return 1;\r | |
798 | }\r | |
799 | break;\r | |
800 | \r | |
801 | case TRACE_AUTH1:\r | |
802 | if (len == 4) {\r | |
803 | traceState = TRACE_AUTH2;\r | |
804 | nt = bytes_to_num(data, 4);\r | |
805 | return 0;\r | |
806 | } else {\r | |
807 | traceState = TRACE_ERROR;\r | |
808 | return 1;\r | |
809 | }\r | |
810 | break;\r | |
811 | \r | |
812 | case TRACE_AUTH2:\r | |
813 | if (len == 8) {\r | |
814 | traceState = TRACE_AUTH_OK;\r | |
815 | \r | |
816 | nr_enc = bytes_to_num(data, 4);\r | |
817 | ar_enc = bytes_to_num(data + 4, 4);\r | |
818 | return 0;\r | |
819 | } else {\r | |
820 | traceState = TRACE_ERROR;\r | |
821 | return 1;\r | |
822 | }\r | |
823 | break;\r | |
824 | \r | |
825 | case TRACE_AUTH_OK:\r | |
826 | if (len ==4) {\r | |
827 | traceState = TRACE_IDLE;\r | |
828 | \r | |
829 | at_enc = bytes_to_num(data, 4);\r | |
830 | \r | |
831 | // decode key here)\r | |
832 | ks2 = ar_enc ^ prng_successor(nt, 64);\r | |
833 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
834 | revstate = lfsr_recovery64(ks2, ks3);\r | |
835 | lfsr_rollback_word(revstate, 0, 0);\r | |
836 | lfsr_rollback_word(revstate, 0, 0);\r | |
837 | lfsr_rollback_word(revstate, nr_enc, 1);\r | |
838 | lfsr_rollback_word(revstate, uid ^ nt, 0);\r | |
839 | \r | |
840 | crypto1_get_lfsr(revstate, &lfsr);\r | |
841 | printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r | |
842 | AddLogUint64(logHexFileName, "key> ", lfsr);\r | |
843 | \r | |
844 | int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r | |
845 | if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r | |
846 | \r | |
847 | if (traceCurKey) {\r | |
848 | num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r | |
849 | } else {\r | |
850 | num_to_bytes(lfsr, 6, traceCard + blockShift);\r | |
851 | }\r | |
852 | if (wantSaveToEmlFile) saveTraceCard();\r | |
853 | \r | |
854 | if (traceCrypto1) {\r | |
855 | crypto1_destroy(traceCrypto1);\r | |
856 | }\r | |
857 | \r | |
858 | // set cryptosystem state\r | |
859 | traceCrypto1 = lfsr_recovery64(ks2, ks3);\r | |
860 | \r | |
861 | // nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r | |
862 | \r | |
863 | /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r | |
864 | crypto1_word(traceCrypto1, nt ^ uid, 0);\r | |
865 | crypto1_word(traceCrypto1, ar, 1);\r | |
866 | crypto1_word(traceCrypto1, 0, 0);\r | |
867 | crypto1_word(traceCrypto1, 0, 0);*/\r | |
868 | \r | |
869 | return 0;\r | |
870 | } else {\r | |
871 | traceState = TRACE_ERROR;\r | |
872 | return 1;\r | |
873 | }\r | |
874 | break;\r | |
875 | \r | |
876 | default:\r | |
877 | traceState = TRACE_ERROR;\r | |
878 | return 1;\r | |
879 | }\r | |
880 | \r | |
881 | return 0;\r | |
882 | }\r | |
883 | \r | |
884 | // DECODING\r | |
885 | \r | |
886 | int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r | |
887 | /*\r | |
888 | uint32_t nt; // tag challenge\r | |
889 | uint32_t ar_enc; // encrypted reader response\r | |
890 | uint32_t at_enc; // encrypted tag response\r | |
891 | */\r | |
892 | if (traceCrypto1) {\r | |
893 | crypto1_destroy(traceCrypto1);\r | |
894 | }\r | |
895 | ks2 = ar_enc ^ prng_successor(nt, 64);\r | |
896 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
897 | traceCrypto1 = lfsr_recovery64(ks2, ks3);\r | |
898 | \r | |
899 | mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r | |
900 | \r | |
901 | PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r | |
902 | crypto1_destroy(traceCrypto1);\r | |
903 | return 0;\r | |
904 | }\r | |
905 | \r |