]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdhfmf.c
FIX: #lld -> %#PRIu64" since the arguments are uin64_t
[proxmark3-svn] / client / cmdhfmf.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------\r
2// Copyright (C) 2011,2012 Merlok\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// High frequency MIFARE commands\r
9//-----------------------------------------------------------------------------\r
10\r
11#include "cmdhfmf.h"\r
12#include "cmdhfmfhard.h"\r
13#include "nonce2key/nonce2key.h"\r
14\r
15static int CmdHelp(const char *Cmd);\r
16\r
17int CmdHF14AMifare(const char *Cmd)\r
18{\r
19 uint32_t uid = 0;\r
20 uint32_t nt = 0, nr = 0;\r
21 uint64_t par_list = 0, ks_list = 0, r_key = 0;\r
22 int16_t isOK = 0;\r
23 int tmpchar; \r
24 \r
25 UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r
26\r
27 // message\r
28 printf("-------------------------------------------------------------------------\n");\r
29 printf("Executing command. Expected execution time: 25sec on average :-)\n");\r
30 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");\r
31 printf("-------------------------------------------------------------------------\n");\r
32\r
33 clock_t t1 = clock();\r
34 \r
35start:\r
36 clearCommandBuffer();\r
37 SendCommand(&c);\r
38 \r
39 //flush queue\r
40 while (ukbhit()) {\r
41 tmpchar = getchar();\r
42 (void)tmpchar;\r
43 }\r
44\r
45 // wait cycle\r
46 while (true) {\r
47 printf(".");\r
48 fflush(stdout);\r
49 if (ukbhit()) {\r
50 tmpchar = getchar();\r
51 (void)tmpchar;\r
52 printf("\naborted via keyboard!\n");\r
53 break;\r
54 }\r
55 \r
56 UsbCommand resp;\r
57 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {\r
58 isOK = resp.arg[0];\r
59 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r
60 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r
61 par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r
62 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r
63 nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
64 printf("\n\n");\r
65 switch (isOK) {\r
66 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
67 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;\r
68 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;\r
69 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");\r
70 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;\r
71 default: ;\r
72 }\r
73 break;\r
74 }\r
75 } \r
76\r
77 printf("\n");\r
78 \r
79 // error\r
80 if (isOK != 1) return 1;\r
81 \r
82 // execute original function from util nonce2key\r
83 if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key)) {\r
84 isOK = 2;\r
85 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r
86 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r
87 c.arg[0] = false;\r
88 goto start;\r
89 } else {\r
90 isOK = 0;\r
91 printf("------------------------------------------------------------------\n");\r
92 PrintAndLog("Found valid key: %012"llx" \n", r_key);\r
93 }\r
94 t1 = clock() - t1;\r
95 if ( t1 > 0 ){\r
96 PrintAndLog("Time in darkside: %f ticks - %1.2f sec\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC);\r
97 }\r
98 return 0;\r
99}\r
100\r
101int CmdHF14AMfWrBl(const char *Cmd)\r
102{\r
103 uint8_t blockNo = 0;\r
104 uint8_t keyType = 0;\r
105 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
106 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
107 \r
108 char cmdp = 0x00;\r
109\r
110 if (strlen(Cmd)<3) {\r
111 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r
112 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r
113 return 0;\r
114 } \r
115\r
116 blockNo = param_get8(Cmd, 0);\r
117 cmdp = param_getchar(Cmd, 1);\r
118 if (cmdp == 0x00) {\r
119 PrintAndLog("Key type must be A or B");\r
120 return 1;\r
121 }\r
122 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
123 if (param_gethex(Cmd, 2, key, 12)) {\r
124 PrintAndLog("Key must include 12 HEX symbols");\r
125 return 1;\r
126 }\r
127 if (param_gethex(Cmd, 3, bldata, 32)) {\r
128 PrintAndLog("Block data must include 32 HEX symbols");\r
129 return 1;\r
130 }\r
131 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
132 PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r
133 \r
134 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r
135 memcpy(c.d.asBytes, key, 6);\r
136 memcpy(c.d.asBytes + 10, bldata, 16);\r
137 clearCommandBuffer();\r
138 SendCommand(&c);\r
139\r
140 UsbCommand resp;\r
141 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
142 uint8_t isOK = resp.arg[0] & 0xff;\r
143 PrintAndLog("isOk:%02x", isOK);\r
144 } else {\r
145 PrintAndLog("Command execute timeout");\r
146 }\r
147\r
148 return 0;\r
149}\r
150\r
151int CmdHF14AMfRdBl(const char *Cmd)\r
152{\r
153 uint8_t blockNo = 0;\r
154 uint8_t keyType = 0;\r
155 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
156 \r
157 char cmdp = 0x00;\r
158\r
159\r
160 if (strlen(Cmd)<3) {\r
161 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r
162 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r
163 return 0;\r
164 } \r
165 \r
166 blockNo = param_get8(Cmd, 0);\r
167 cmdp = param_getchar(Cmd, 1);\r
168 if (cmdp == 0x00) {\r
169 PrintAndLog("Key type must be A or B");\r
170 return 1;\r
171 }\r
172 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
173 if (param_gethex(Cmd, 2, key, 12)) {\r
174 PrintAndLog("Key must include 12 HEX symbols");\r
175 return 1;\r
176 }\r
177 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
178 \r
179 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r
180 memcpy(c.d.asBytes, key, 6);\r
181 clearCommandBuffer();\r
182 SendCommand(&c);\r
183\r
184 UsbCommand resp;\r
185 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
186 uint8_t isOK = resp.arg[0] & 0xff;\r
187 uint8_t *data = resp.d.asBytes;\r
188\r
189 if (isOK)\r
190 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r
191 else\r
192 PrintAndLog("isOk:%02x", isOK);\r
193 } else {\r
194 PrintAndLog("Command execute timeout");\r
195 }\r
196\r
197 return 0;\r
198}\r
199\r
200int CmdHF14AMfRdSc(const char *Cmd)\r
201{\r
202 int i;\r
203 uint8_t sectorNo = 0;\r
204 uint8_t keyType = 0;\r
205 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
206 uint8_t isOK = 0;\r
207 uint8_t *data = NULL;\r
208 char cmdp = 0x00;\r
209\r
210 if (strlen(Cmd)<3) {\r
211 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r
212 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r
213 return 0;\r
214 } \r
215 \r
216 sectorNo = param_get8(Cmd, 0);\r
217 if (sectorNo > 39) {\r
218 PrintAndLog("Sector number must be less than 40");\r
219 return 1;\r
220 }\r
221 cmdp = param_getchar(Cmd, 1);\r
222 if (cmdp != 'a' && cmdp != 'A' && cmdp != 'b' && cmdp != 'B') {\r
223 PrintAndLog("Key type must be A or B");\r
224 return 1;\r
225 }\r
226 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
227 if (param_gethex(Cmd, 2, key, 12)) {\r
228 PrintAndLog("Key must include 12 HEX symbols");\r
229 return 1;\r
230 }\r
231 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo, keyType?'B':'A', sprint_hex(key, 6));\r
232 \r
233 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r
234 memcpy(c.d.asBytes, key, 6);\r
235 clearCommandBuffer();\r
236 SendCommand(&c);\r
237 PrintAndLog(" ");\r
238\r
239 UsbCommand resp;\r
240 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
241 isOK = resp.arg[0] & 0xff;\r
242 data = resp.d.asBytes;\r
243\r
244 PrintAndLog("isOk:%02x", isOK);\r
245 if (isOK) {\r
246 for (i = 0; i < (sectorNo<32?3:15); i++) {\r
247 PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));\r
248 }\r
249 PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));\r
250 }\r
251 } else {\r
252 PrintAndLog("Command execute timeout");\r
253 }\r
254\r
255 return 0;\r
256}\r
257\r
258uint8_t FirstBlockOfSector(uint8_t sectorNo)\r
259{\r
260 if (sectorNo < 32) {\r
261 return sectorNo * 4;\r
262 } else {\r
263 return 32 * 4 + (sectorNo - 32) * 16;\r
264 }\r
265}\r
266\r
267uint8_t NumBlocksPerSector(uint8_t sectorNo)\r
268{\r
269 if (sectorNo < 32) {\r
270 return 4;\r
271 } else {\r
272 return 16;\r
273 }\r
274}\r
275\r
276int CmdHF14AMfDump(const char *Cmd)\r
277{\r
278 uint8_t sectorNo, blockNo;\r
279 \r
280 uint8_t keyA[40][6];\r
281 uint8_t keyB[40][6];\r
282 uint8_t rights[40][4];\r
283 uint8_t carddata[256][16];\r
284 uint8_t numSectors = 16;\r
285 \r
286 FILE *fin;\r
287 FILE *fout;\r
288 \r
289 UsbCommand resp;\r
290\r
291 char cmdp = param_getchar(Cmd, 0);\r
292 switch (cmdp) {\r
293 case '0' : numSectors = 5; break;\r
294 case '1' : \r
295 case '\0': numSectors = 16; break;\r
296 case '2' : numSectors = 32; break;\r
297 case '4' : numSectors = 40; break;\r
298 default: numSectors = 16;\r
299 } \r
300 \r
301 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r
302 PrintAndLog("Usage: hf mf dump [card memory]");\r
303 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
304 PrintAndLog("");\r
305 PrintAndLog("Samples: hf mf dump");\r
306 PrintAndLog(" hf mf dump 4");\r
307 return 0;\r
308 }\r
309 \r
310 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r
311 PrintAndLog("Could not find file dumpkeys.bin");\r
312 return 1;\r
313 }\r
314 \r
315 // Read keys A from file\r
316 size_t bytes_read;\r
317 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
318 bytes_read = fread( keyA[sectorNo], 1, 6, fin );\r
319 if ( bytes_read == 0) {\r
320 PrintAndLog("File reading error.");\r
321 fclose(fin);\r
322 return 2;\r
323 }\r
324 }\r
325 \r
326 // Read keys B from file\r
327 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
328 bytes_read = fread( keyB[sectorNo], 1, 6, fin );\r
329 if ( bytes_read == 0) {\r
330 PrintAndLog("File reading error.");\r
331 fclose(fin);\r
332 return 2;\r
333 }\r
334 }\r
335 \r
336 fclose(fin);\r
337\r
338 PrintAndLog("|-----------------------------------------|");\r
339 PrintAndLog("|------ Reading sector access bits...-----|");\r
340 PrintAndLog("|-----------------------------------------|");\r
341 \r
342 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
343 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};\r
344 memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
345 clearCommandBuffer();\r
346 SendCommand(&c);\r
347\r
348 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
349 uint8_t isOK = resp.arg[0] & 0xff;\r
350 uint8_t *data = resp.d.asBytes;\r
351 if (isOK){\r
352 rights[sectorNo][0] = ((data[7] & 0x10)>>2) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>4); // C1C2C3 for data area 0\r
353 rights[sectorNo][1] = ((data[7] & 0x20)>>3) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>5); // C1C2C3 for data area 1\r
354 rights[sectorNo][2] = ((data[7] & 0x40)>>4) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>6); // C1C2C3 for data area 2\r
355 rights[sectorNo][3] = ((data[7] & 0x80)>>5) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>7); // C1C2C3 for sector trailer\r
356 } else {\r
357 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo);\r
358 rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;\r
359 rights[sectorNo][3] = 0x01;\r
360 }\r
361 } else {\r
362 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo);\r
363 rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;\r
364 rights[sectorNo][3] = 0x01;\r
365 }\r
366 }\r
367 \r
368 PrintAndLog("|-----------------------------------------|");\r
369 PrintAndLog("|----- Dumping all blocks to file... -----|");\r
370 PrintAndLog("|-----------------------------------------|");\r
371 \r
372 bool isOK = true;\r
373 for (sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
374 for (blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
375 bool received = false;\r
376 \r
377 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. At least the Access Conditions can always be read with key A. \r
378 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r
379 memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
380 clearCommandBuffer();\r
381 SendCommand(&c);\r
382 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
383 } else { // data block. Check if it can be read with key A or key B\r
384 uint8_t data_area = sectorNo<32?blockNo:blockNo/5;\r
385 if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) { // only key B would work\r
386 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r
387 memcpy(c.d.asBytes, keyB[sectorNo], 6);\r
388 SendCommand(&c);\r
389 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
390 } else if (rights[sectorNo][data_area] == 0x07) { // no key would work\r
391 isOK = false;\r
392 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);\r
393 } else { // key A would work\r
394 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r
395 memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
396 clearCommandBuffer();\r
397 SendCommand(&c);\r
398 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
399 }\r
400 }\r
401\r
402 if (received) {\r
403 isOK = resp.arg[0] & 0xff;\r
404 uint8_t *data = resp.d.asBytes;\r
405 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.\r
406 data[0] = (keyA[sectorNo][0]);\r
407 data[1] = (keyA[sectorNo][1]);\r
408 data[2] = (keyA[sectorNo][2]);\r
409 data[3] = (keyA[sectorNo][3]);\r
410 data[4] = (keyA[sectorNo][4]);\r
411 data[5] = (keyA[sectorNo][5]);\r
412 data[10] = (keyB[sectorNo][0]);\r
413 data[11] = (keyB[sectorNo][1]);\r
414 data[12] = (keyB[sectorNo][2]);\r
415 data[13] = (keyB[sectorNo][3]);\r
416 data[14] = (keyB[sectorNo][4]);\r
417 data[15] = (keyB[sectorNo][5]);\r
418 }\r
419 if (isOK) {\r
420 memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);\r
421 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo, sectorNo);\r
422 } else {\r
423 PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);\r
424 break;\r
425 }\r
426 }\r
427 else {\r
428 isOK = false;\r
429 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo, sectorNo);\r
430 break;\r
431 }\r
432 }\r
433 }\r
434\r
435 if (isOK) {\r
436 if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r
437 PrintAndLog("Could not create file name dumpdata.bin");\r
438 return 1;\r
439 }\r
440 uint16_t numblocks = FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1);\r
441 fwrite(carddata, 1, 16*numblocks, fout);\r
442 fclose(fout);\r
443 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks, 16*numblocks);\r
444 }\r
445 \r
446 return 0;\r
447}\r
448\r
449int CmdHF14AMfRestore(const char *Cmd)\r
450{\r
451 uint8_t sectorNo,blockNo;\r
452 uint8_t keyType = 0;\r
453 uint8_t key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};\r
454 uint8_t bldata[16] = {0x00};\r
455 uint8_t keyA[40][6];\r
456 uint8_t keyB[40][6];\r
457 uint8_t numSectors;\r
458 \r
459 FILE *fdump;\r
460 FILE *fkeys;\r
461\r
462 char cmdp = param_getchar(Cmd, 0);\r
463 switch (cmdp) {\r
464 case '0' : numSectors = 5; break;\r
465 case '1' : \r
466 case '\0': numSectors = 16; break;\r
467 case '2' : numSectors = 32; break;\r
468 case '4' : numSectors = 40; break;\r
469 default: numSectors = 16;\r
470 } \r
471\r
472 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r
473 PrintAndLog("Usage: hf mf restore [card memory]");\r
474 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
475 PrintAndLog("");\r
476 PrintAndLog("Samples: hf mf restore");\r
477 PrintAndLog(" hf mf restore 4");\r
478 return 0;\r
479 }\r
480\r
481 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r
482 PrintAndLog("Could not find file dumpkeys.bin");\r
483 return 1;\r
484 }\r
485 \r
486 size_t bytes_read;\r
487 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
488 bytes_read = fread( keyA[sectorNo], 1, 6, fkeys );\r
489 if ( bytes_read == 0) {\r
490 PrintAndLog("File reading error (dumpkeys.bin).");\r
491 fclose(fkeys);\r
492 return 2;\r
493 }\r
494 }\r
495\r
496 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
497 bytes_read = fread( keyB[sectorNo], 1, 6, fkeys );\r
498 if ( bytes_read == 0) {\r
499 PrintAndLog("File reading error (dumpkeys.bin).");\r
500 fclose(fkeys);\r
501 return 2;\r
502 }\r
503 }\r
504\r
505 fclose(fkeys);\r
506\r
507 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r
508 PrintAndLog("Could not find file dumpdata.bin");\r
509 return 1;\r
510 } \r
511 PrintAndLog("Restoring dumpdata.bin to card");\r
512\r
513 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
514 for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
515 UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};\r
516 memcpy(c.d.asBytes, key, 6); \r
517 bytes_read = fread(bldata, 1, 16, fdump);\r
518 if ( bytes_read == 0) {\r
519 PrintAndLog("File reading error (dumpdata.bin).");\r
520 fclose(fdump);\r
521 return 2;\r
522 }\r
523 \r
524 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer\r
525 bldata[0] = (keyA[sectorNo][0]);\r
526 bldata[1] = (keyA[sectorNo][1]);\r
527 bldata[2] = (keyA[sectorNo][2]);\r
528 bldata[3] = (keyA[sectorNo][3]);\r
529 bldata[4] = (keyA[sectorNo][4]);\r
530 bldata[5] = (keyA[sectorNo][5]);\r
531 bldata[10] = (keyB[sectorNo][0]);\r
532 bldata[11] = (keyB[sectorNo][1]);\r
533 bldata[12] = (keyB[sectorNo][2]);\r
534 bldata[13] = (keyB[sectorNo][3]);\r
535 bldata[14] = (keyB[sectorNo][4]);\r
536 bldata[15] = (keyB[sectorNo][5]);\r
537 } \r
538 \r
539 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));\r
540 \r
541 memcpy(c.d.asBytes + 10, bldata, 16);\r
542 clearCommandBuffer();\r
543 SendCommand(&c);\r
544\r
545 UsbCommand resp;\r
546 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
547 uint8_t isOK = resp.arg[0] & 0xff;\r
548 PrintAndLog("isOk:%02x", isOK);\r
549 } else {\r
550 PrintAndLog("Command execute timeout");\r
551 }\r
552 }\r
553 }\r
554 \r
555 fclose(fdump);\r
556 return 0;\r
557}\r
558\r
559int CmdHF14AMfNested(const char *Cmd)\r
560{\r
561 int i, j, res, iterations;\r
562 sector *e_sector = NULL;\r
563 uint8_t blockNo = 0;\r
564 uint8_t keyType = 0;\r
565 uint8_t trgBlockNo = 0;\r
566 uint8_t trgKeyType = 0;\r
567 uint8_t SectorsCnt = 0;\r
568 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
569 uint8_t keyBlock[14*6];\r
570 uint64_t key64 = 0;\r
571 bool transferToEml = false;\r
572 \r
573 bool createDumpFile = false;\r
574 FILE *fkeys;\r
575 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
576 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
577 \r
578 char cmdp, ctmp;\r
579\r
580 if (strlen(Cmd)<3) {\r
581 PrintAndLog("Usage:");\r
582 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r
583 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r
584 PrintAndLog(" <target block number> <target key A/B> [t]");\r
585 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
586 PrintAndLog("t - transfer keys into emulator memory");\r
587 PrintAndLog("d - write keys to binary file");\r
588 PrintAndLog(" ");\r
589 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r
590 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r
591 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r
592 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r
593 return 0;\r
594 } \r
595 \r
596 cmdp = param_getchar(Cmd, 0);\r
597 blockNo = param_get8(Cmd, 1);\r
598 ctmp = param_getchar(Cmd, 2);\r
599 \r
600 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
601 PrintAndLog("Key type must be A or B");\r
602 return 1;\r
603 }\r
604 \r
605 if (ctmp != 'A' && ctmp != 'a') \r
606 keyType = 1;\r
607 \r
608 if (param_gethex(Cmd, 3, key, 12)) {\r
609 PrintAndLog("Key must include 12 HEX symbols");\r
610 return 1;\r
611 }\r
612 \r
613 if (cmdp == 'o' || cmdp == 'O') {\r
614 cmdp = 'o';\r
615 trgBlockNo = param_get8(Cmd, 4);\r
616 ctmp = param_getchar(Cmd, 5);\r
617 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
618 PrintAndLog("Target key type must be A or B");\r
619 return 1;\r
620 }\r
621 if (ctmp != 'A' && ctmp != 'a') \r
622 trgKeyType = 1;\r
623 } else {\r
624 \r
625 switch (cmdp) {\r
626 case '0': SectorsCnt = 05; break;\r
627 case '1': SectorsCnt = 16; break;\r
628 case '2': SectorsCnt = 32; break;\r
629 case '4': SectorsCnt = 40; break;\r
630 default: SectorsCnt = 16;\r
631 }\r
632 }\r
633\r
634 ctmp = param_getchar(Cmd, 4);\r
635 if (ctmp == 't' || ctmp == 'T') transferToEml = true;\r
636 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;\r
637 \r
638 ctmp = param_getchar(Cmd, 6);\r
639 transferToEml |= (ctmp == 't' || ctmp == 'T');\r
640 transferToEml |= (ctmp == 'd' || ctmp == 'D');\r
641 \r
642 if (cmdp == 'o') {\r
643 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');\r
644 int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);\r
645 if (isOK) {\r
646 switch (isOK) {\r
647 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
648 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
649 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
650 default : PrintAndLog("Unknown Error.\n");\r
651 }\r
652 return 2;\r
653 }\r
654 key64 = bytes_to_num(keyBlock, 6);\r
655 if (key64) {\r
656 PrintAndLog("Found valid key:%012"llx, key64);\r
657\r
658 // transfer key to the emulator\r
659 if (transferToEml) {\r
660 uint8_t sectortrailer;\r
661 if (trgBlockNo < 32*4) { // 4 block sector\r
662 sectortrailer = (trgBlockNo & 0x03) + 3;\r
663 } else { // 16 block sector\r
664 sectortrailer = (trgBlockNo & 0x0f) + 15;\r
665 }\r
666 mfEmlGetMem(keyBlock, sectortrailer, 1);\r
667 \r
668 if (!trgKeyType)\r
669 num_to_bytes(key64, 6, keyBlock);\r
670 else\r
671 num_to_bytes(key64, 6, &keyBlock[10]);\r
672 mfEmlSetMem(keyBlock, sectortrailer, 1); \r
673 }\r
674 } else {\r
675 PrintAndLog("No valid key found");\r
676 }\r
677 }\r
678 else { // ------------------------------------ multiple sectors working\r
679 clock_t t1 = clock();\r
680\r
681 e_sector = calloc(SectorsCnt, sizeof(sector));\r
682 if (e_sector == NULL) return 1;\r
683 \r
684 //test current key and additional standard keys first\r
685 memcpy(keyBlock, key, 6);\r
686 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r
687 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r
688 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
689 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r
690 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r
691 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));\r
692 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));\r
693 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));\r
694 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));\r
695 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));\r
696 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));\r
697 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));\r
698 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));\r
699\r
700 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
701 for (i = 0; i < SectorsCnt; i++) {\r
702 for (j = 0; j < 2; j++) {\r
703 if (e_sector[i].foundKey[j]) continue;\r
704 \r
705 res = mfCheckKeys(FirstBlockOfSector(i), j, true, 6, keyBlock, &key64);\r
706 \r
707 if (!res) {\r
708 e_sector[i].Key[j] = key64;\r
709 e_sector[i].foundKey[j] = 1;\r
710 }\r
711 }\r
712 }\r
713 \r
714 // nested sectors\r
715 iterations = 0;\r
716 PrintAndLog("nested...");\r
717 bool calibrate = true;\r
718 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r
719 for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
720 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r
721 if (e_sector[sectorNo].foundKey[trgKeyType]) continue;\r
722 PrintAndLog("-----------------------------------------------");\r
723 int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);\r
724 if(isOK) {\r
725 switch (isOK) {\r
726 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
727 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
728 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r
729 default : PrintAndLog("Unknown Error.\n");\r
730 }\r
731 free(e_sector);\r
732 return 2;\r
733 } else {\r
734 calibrate = false;\r
735 }\r
736 \r
737 iterations++;\r
738\r
739 key64 = bytes_to_num(keyBlock, 6);\r
740 if (key64) {\r
741 PrintAndLog("Found valid key:%012"llx, key64);\r
742 e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
743 e_sector[sectorNo].Key[trgKeyType] = key64;\r
744 }\r
745 }\r
746 }\r
747 }\r
748\r
749 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?\r
750 PrintAndLog("testing to read B...");\r
751 for (i = 0; i < SectorsCnt; i++) {\r
752 // KEY A but not KEY B\r
753 if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {\r
754 \r
755 uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
756 \r
757 UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};\r
758 num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A\r
759 clearCommandBuffer();\r
760 SendCommand(&c);\r
761\r
762 UsbCommand resp;\r
763 if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;\r
764 \r
765 uint8_t isOK = resp.arg[0] & 0xff;\r
766 uint8_t *data = resp.d.asBytes;\r
767\r
768 if (isOK) {\r
769\r
770 key64 = bytes_to_num(data+10, 6);\r
771 if (key64) {\r
772 PrintAndLog("Data:%s", sprint_hex(data+10, 6));\r
773 e_sector[i].foundKey[1] = 1;\r
774 e_sector[i].Key[1] = key64;\r
775 }\r
776 }\r
777 }\r
778 }\r
779 \r
780 t1 = clock() - t1;\r
781 if ( t1 > 0 ) {\r
782 PrintAndLog("Time in nested: %f ticks %1.2f sec (%1.2f sec per key)\n\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC, ((float)t1)/iterations/CLOCKS_PER_SEC);\r
783 }\r
784 \r
785 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r
786 //print them\r
787 PrintAndLog("|---|----------------|---|----------------|---|");\r
788 PrintAndLog("|sec|key A |res|key B |res|");\r
789 PrintAndLog("|---|----------------|---|----------------|---|");\r
790 for (i = 0; i < SectorsCnt; i++) {\r
791 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r
792 e_sector[i].Key[0], \r
793 e_sector[i].foundKey[0], \r
794 e_sector[i].Key[1], \r
795 e_sector[i].foundKey[1]\r
796 );\r
797 }\r
798 PrintAndLog("|---|----------------|---|----------------|---|");\r
799 \r
800 // transfer them to the emulator\r
801 if (transferToEml) {\r
802 for (i = 0; i < SectorsCnt; i++) {\r
803 mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r
804 if (e_sector[i].foundKey[0])\r
805 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r
806 if (e_sector[i].foundKey[1])\r
807 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r
808 mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r
809 } \r
810 }\r
811 \r
812 // Create dump file\r
813 if (createDumpFile) {\r
814 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r
815 PrintAndLog("Could not create file dumpkeys.bin");\r
816 free(e_sector);\r
817 return 1;\r
818 }\r
819 PrintAndLog("Printing keys to binary file dumpkeys.bin...");\r
820 for(i=0; i<SectorsCnt; i++) {\r
821 if (e_sector[i].foundKey[0]){\r
822 num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
823 fwrite ( tempkey, 1, 6, fkeys );\r
824 }\r
825 else{\r
826 fwrite ( &standart, 1, 6, fkeys );\r
827 }\r
828 }\r
829 for(i=0; i<SectorsCnt; i++) {\r
830 if (e_sector[i].foundKey[1]){\r
831 num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
832 fwrite ( tempkey, 1, 6, fkeys );\r
833 }\r
834 else{\r
835 fwrite ( &standart, 1, 6, fkeys );\r
836 }\r
837 }\r
838 fclose(fkeys);\r
839 }\r
840 \r
841 free(e_sector);\r
842 }\r
843 return 0;\r
844}\r
845\r
846int CmdHF14AMfNestedHard(const char *Cmd)\r
847{\r
848 uint8_t blockNo = 0;\r
849 uint8_t keyType = 0;\r
850 uint8_t trgBlockNo = 0;\r
851 uint8_t trgKeyType = 0;\r
852 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
853 uint8_t trgkey[6] = {0, 0, 0, 0, 0, 0};\r
854 \r
855 char ctmp;\r
856 ctmp = param_getchar(Cmd, 0);\r
857\r
858 if (ctmp != 'R' && ctmp != 'r' && ctmp != 'T' && ctmp != 't' && strlen(Cmd) < 20) {\r
859 PrintAndLog("Usage:");\r
860 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");\r
861 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");\r
862 PrintAndLog(" or hf mf hardnested r [known target key]");\r
863 PrintAndLog(" ");\r
864 PrintAndLog("Options: ");\r
865 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");\r
866 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");\r
867 PrintAndLog(" r: Read nonces.bin and start attack");\r
868 PrintAndLog(" ");\r
869 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");\r
870 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");\r
871 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");\r
872 PrintAndLog(" sample4: hf mf hardnested r");\r
873 PrintAndLog(" ");\r
874 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");\r
875 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");\r
876 return 0;\r
877 } \r
878 \r
879 bool know_target_key = false;\r
880 bool nonce_file_read = false;\r
881 bool nonce_file_write = false;\r
882 bool slow = false;\r
883 int tests = 0;\r
884 \r
885 \r
886 if (ctmp == 'R' || ctmp == 'r') {\r
887 nonce_file_read = true;\r
888 if (!param_gethex(Cmd, 1, trgkey, 12)) {\r
889 know_target_key = true;\r
890 }\r
891 } else if (ctmp == 'T' || ctmp == 't') {\r
892 tests = param_get32ex(Cmd, 1, 100, 10);\r
893 } else {\r
894 blockNo = param_get8(Cmd, 0);\r
895 ctmp = param_getchar(Cmd, 1);\r
896 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
897 PrintAndLog("Key type must be A or B");\r
898 return 1;\r
899 }\r
900 if (ctmp != 'A' && ctmp != 'a') { \r
901 keyType = 1;\r
902 }\r
903 \r
904 if (param_gethex(Cmd, 2, key, 12)) {\r
905 PrintAndLog("Key must include 12 HEX symbols");\r
906 return 1;\r
907 }\r
908 \r
909 trgBlockNo = param_get8(Cmd, 3);\r
910 ctmp = param_getchar(Cmd, 4);\r
911 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
912 PrintAndLog("Target key type must be A or B");\r
913 return 1;\r
914 }\r
915 if (ctmp != 'A' && ctmp != 'a') {\r
916 trgKeyType = 1;\r
917 }\r
918\r
919 uint16_t i = 5;\r
920\r
921 if (!param_gethex(Cmd, 5, trgkey, 12)) {\r
922 know_target_key = true;\r
923 i++;\r
924 }\r
925\r
926 while ((ctmp = param_getchar(Cmd, i))) {\r
927 if (ctmp == 's' || ctmp == 'S') {\r
928 slow = true;\r
929 } else if (ctmp == 'w' || ctmp == 'W') {\r
930 nonce_file_write = true;\r
931 } else {\r
932 PrintAndLog("Possible options are w and/or s");\r
933 return 1;\r
934 }\r
935 i++;\r
936 }\r
937 }\r
938\r
939 PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d ", \r
940 trgBlockNo, \r
941 trgKeyType?'B':'A', \r
942 trgkey[0], trgkey[1], trgkey[2], trgkey[3], trgkey[4], trgkey[5],\r
943 know_target_key?"":" (not set)",\r
944 nonce_file_write?"write":nonce_file_read?"read":"none",\r
945 slow?"Yes":"No",\r
946 tests);\r
947\r
948 int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key?trgkey:NULL, nonce_file_read, nonce_file_write, slow, tests);\r
949\r
950 if (isOK) {\r
951 switch (isOK) {\r
952 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r
953 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
954 default : break;\r
955 }\r
956 return 2;\r
957 }\r
958\r
959 return 0;\r
960}\r
961\r
962int CmdHF14AMfChk(const char *Cmd)\r
963{\r
964 if (strlen(Cmd)<3) {\r
965 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r
966 PrintAndLog(" * - all sectors");\r
967 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
968 PrintAndLog("d - write keys to binary file");\r
969 PrintAndLog("t - write keys to emulator memory\n");\r
970 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r
971 PrintAndLog(" hf mf chk *1 ? t");\r
972 PrintAndLog(" hf mf chk *1 ? d");\r
973 return 0;\r
974 } \r
975\r
976 FILE * f;\r
977 char filename[FILE_PATH_SIZE]={0};\r
978 char buf[13];\r
979 uint8_t *keyBlock = NULL, *p;\r
980 uint8_t stKeyBlock = 20;\r
981 \r
982 int i, res;\r
983 int keycnt = 0;\r
984 char ctmp = 0x00;\r
985 uint8_t blockNo = 0;\r
986 uint8_t SectorsCnt = 1;\r
987 uint8_t keyType = 0;\r
988 uint64_t key64 = 0;\r
989 \r
990 int transferToEml = 0;\r
991 int createDumpFile = 0;\r
992\r
993 keyBlock = calloc(stKeyBlock, 6);\r
994 if (keyBlock == NULL) return 1;\r
995\r
996 uint64_t defaultKeys[] =\r
997 {\r
998 0xffffffffffff, // Default key (first key used by program if no user defined key)\r
999 0x000000000000, // Blank key\r
1000 0xa0a1a2a3a4a5, // NFCForum MAD key\r
1001 0xb0b1b2b3b4b5,\r
1002 0xaabbccddeeff,\r
1003 0x4d3a99c351dd,\r
1004 0x1a982c7e459a,\r
1005 0xd3f7d3f7d3f7,\r
1006 0x714c5c886e97,\r
1007 0x587ee5f9350f,\r
1008 0xa0478cc39091,\r
1009 0x533cb6c723f6,\r
1010 0x8fd0a4f256e9\r
1011 };\r
1012 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);\r
1013\r
1014 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
1015 {\r
1016 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
1017 }\r
1018 \r
1019 if (param_getchar(Cmd, 0)=='*') {\r
1020 blockNo = 3;\r
1021 switch(param_getchar(Cmd+1, 0)) {\r
1022 case '0': SectorsCnt = 5; break;\r
1023 case '1': SectorsCnt = 16; break;\r
1024 case '2': SectorsCnt = 32; break;\r
1025 case '4': SectorsCnt = 40; break;\r
1026 default: SectorsCnt = 16;\r
1027 }\r
1028 }\r
1029 else\r
1030 blockNo = param_get8(Cmd, 0);\r
1031 \r
1032 ctmp = param_getchar(Cmd, 1);\r
1033 switch (ctmp) { \r
1034 case 'a': case 'A':\r
1035 keyType = !0;\r
1036 break;\r
1037 case 'b': case 'B':\r
1038 keyType = !1;\r
1039 break;\r
1040 case '?':\r
1041 keyType = 2;\r
1042 break;\r
1043 default:\r
1044 PrintAndLog("Key type must be A , B or ?");\r
1045 free(keyBlock);\r
1046 return 1;\r
1047 };\r
1048 \r
1049 ctmp = param_getchar(Cmd, 2);\r
1050 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
1051 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
1052 \r
1053 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r
1054 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r
1055 if ( stKeyBlock - keycnt < 2) {\r
1056 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1057 if (!p) {\r
1058 PrintAndLog("Cannot allocate memory for Keys");\r
1059 free(keyBlock);\r
1060 return 2;\r
1061 }\r
1062 keyBlock = p;\r
1063 }\r
1064 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
1065 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
1066 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
1067 keycnt++;\r
1068 } else {\r
1069 // May be a dic file\r
1070 if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {\r
1071 PrintAndLog("File name too long");\r
1072 free(keyBlock);\r
1073 return 2;\r
1074 }\r
1075 \r
1076 if ( (f = fopen( filename , "r")) ) {\r
1077 while( fgets(buf, sizeof(buf), f) ){\r
1078 if (strlen(buf) < 12 || buf[11] == '\n')\r
1079 continue;\r
1080 \r
1081 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
1082 \r
1083 if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r
1084\r
1085 if (!isxdigit(buf[0])){\r
1086 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
1087 continue;\r
1088 }\r
1089 \r
1090 buf[12] = 0;\r
1091\r
1092 if ( stKeyBlock - keycnt < 2) {\r
1093 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1094 if (!p) {\r
1095 PrintAndLog("Cannot allocate memory for defKeys");\r
1096 free(keyBlock);\r
1097 fclose(f);\r
1098 return 2;\r
1099 }\r
1100 keyBlock = p;\r
1101 }\r
1102 memset(keyBlock + 6 * keycnt, 0, 6);\r
1103 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
1104 PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
1105 keycnt++;\r
1106 memset(buf, 0, sizeof(buf));\r
1107 }\r
1108 fclose(f);\r
1109 } else {\r
1110 PrintAndLog("File: %s: not found or locked.", filename);\r
1111 free(keyBlock);\r
1112 return 1;\r
1113 \r
1114 }\r
1115 }\r
1116 }\r
1117 \r
1118 if (keycnt == 0) {\r
1119 PrintAndLog("No key specified, trying default keys");\r
1120 for (;keycnt < defaultKeysSize; keycnt++)\r
1121 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
1122 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
1123 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
1124 }\r
1125 \r
1126 // initialize storage for found keys\r
1127 bool validKey[2][40];\r
1128 uint8_t foundKey[2][40][6];\r
1129 for (uint16_t t = 0; t < 2; t++) {\r
1130 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
1131 validKey[t][sectorNo] = false;\r
1132 for (uint16_t i = 0; i < 6; i++) {\r
1133 foundKey[t][sectorNo][i] = 0xff;\r
1134 }\r
1135 }\r
1136 }\r
1137 // time\r
1138 clock_t t1 = clock();\r
1139 \r
1140 for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {\r
1141 int b=blockNo;\r
1142 for (int i = 0; i < SectorsCnt; ++i) {\r
1143 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);\r
1144 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r
1145 for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r
1146 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r
1147 res = mfCheckKeys(b, t, true, size, &keyBlock[6*c], &key64);\r
1148 if (res != 1) {\r
1149 if (!res) {\r
1150 PrintAndLog("Found valid key:[%012"llx"]",key64);\r
1151 num_to_bytes(key64, 6, foundKey[t][i]);\r
1152 validKey[t][i] = true;\r
1153 } \r
1154 } else {\r
1155 PrintAndLog("Command execute timeout");\r
1156 }\r
1157 }\r
1158 b<127?(b+=4):(b+=16); \r
1159 }\r
1160 }\r
1161 t1 = clock() - t1;\r
1162 if ( t1 > 0 ){\r
1163 printf("Time in checkkeys: %f ticks %1.2f sec (%1.2f sec per key)\n\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC, ((float)t1)/keycnt/CLOCKS_PER_SEC);\r
1164 }\r
1165\r
1166 if (transferToEml) {\r
1167 uint8_t block[16];\r
1168 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
1169 if (validKey[0][sectorNo] || validKey[1][sectorNo]) {\r
1170 mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
1171 for (uint16_t t = 0; t < 2; t++) {\r
1172 if (validKey[t][sectorNo]) {\r
1173 memcpy(block + t*10, foundKey[t][sectorNo], 6);\r
1174 }\r
1175 }\r
1176 mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
1177 }\r
1178 }\r
1179 PrintAndLog("Found keys have been transferred to the emulator memory");\r
1180 }\r
1181\r
1182 if (createDumpFile) {\r
1183 FILE *fkeys = fopen("dumpkeys.bin","wb");\r
1184 if (fkeys == NULL) { \r
1185 PrintAndLog("Could not create file dumpkeys.bin");\r
1186 free(keyBlock);\r
1187 return 1;\r
1188 }\r
1189 for (uint16_t t = 0; t < 2; t++) {\r
1190 fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);\r
1191 }\r
1192 fclose(fkeys);\r
1193 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");\r
1194 }\r
1195\r
1196 free(keyBlock);\r
1197 PrintAndLog("");\r
1198 return 0;\r
1199}\r
1200\r
1201int CmdHF14AMf1kSim(const char *Cmd)\r
1202{\r
1203 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};\r
1204 uint8_t exitAfterNReads = 0;\r
1205 uint8_t flags = 0;\r
1206 \r
1207 uint8_t cmdp = param_getchar(Cmd, 0);\r
1208\r
1209 if (cmdp == 'h' || cmdp == 'H') {\r
1210 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");\r
1211 PrintAndLog(" h this help");\r
1212 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");\r
1213 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r
1214 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r
1215 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r
1216 PrintAndLog("");\r
1217 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");\r
1218 return 0;\r
1219 }\r
1220 uint8_t pnr = 0;\r
1221 if (param_getchar(Cmd, pnr) == 'u') {\r
1222 if(param_gethex(Cmd, pnr+1, uid, 8) == 0)\r
1223 {\r
1224 flags |= FLAG_4B_UID_IN_DATA; // UID from packet\r
1225 } else if(param_gethex(Cmd,pnr+1,uid,14) == 0) {\r
1226 flags |= FLAG_7B_UID_IN_DATA;// UID from packet\r
1227 } else {\r
1228 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");\r
1229 return 1;\r
1230 }\r
1231 pnr +=2;\r
1232 }\r
1233 \r
1234 if (param_getchar(Cmd, pnr) == 'n') {\r
1235 exitAfterNReads = param_get8(Cmd,pnr+1);\r
1236 pnr += 2;\r
1237 }\r
1238 \r
1239 if (param_getchar(Cmd, pnr) == 'i' ) {\r
1240 //Using a flag to signal interactiveness, least significant bit\r
1241 flags |= FLAG_INTERACTIVE;\r
1242 pnr++;\r
1243 }\r
1244\r
1245 if (param_getchar(Cmd, pnr) == 'x' ) {\r
1246 //Using a flag to signal interactiveness, least significant bit\r
1247 flags |= FLAG_NR_AR_ATTACK;\r
1248 }\r
1249 \r
1250 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r
1251 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
1252 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r
1253 , exitAfterNReads\r
1254 , flags\r
1255 , flags);\r
1256\r
1257\r
1258 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1259 memcpy(c.d.asBytes, uid, sizeof(uid));\r
1260 clearCommandBuffer();\r
1261 SendCommand(&c);\r
1262\r
1263 if(flags & FLAG_INTERACTIVE)\r
1264 { \r
1265 uint8_t data[40];\r
1266 uint8_t key[6];\r
1267\r
1268 UsbCommand resp; \r
1269 PrintAndLog("Press pm3-button or send another cmd to abort simulation");\r
1270\r
1271 while( !ukbhit() ){\r
1272 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) continue;\r
1273\r
1274 if ( !(flags & FLAG_NR_AR_ATTACK) ) break;\r
1275 \r
1276 if ( (resp.arg[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD ) break;\r
1277\r
1278 memset(data, 0x00, sizeof(data));\r
1279 memset(key, 0x00, sizeof(key));\r
1280 int len = (resp.arg[1] > sizeof(data)) ? sizeof(data) : resp.arg[1];\r
1281 \r
1282 memcpy(data, resp.d.asBytes, len);\r
1283 \r
1284 uint64_t corr_uid = 0;\r
1285 \r
1286 // this IF? what was I thinking of?\r
1287 if ( memcmp(data, "\x00\x00\x00\x00", 4) == 0 ) {\r
1288 corr_uid = ((uint64_t)(data[3] << 24)) | (data[2] << 16) | (data[1] << 8) | data[0];\r
1289 tryMfk32(corr_uid, data, key);\r
1290 } else {\r
1291 corr_uid |= (uint64_t)data[2] << 48; \r
1292 corr_uid |= (uint64_t)data[1] << 40; \r
1293 corr_uid |= (uint64_t)data[0] << 32;\r
1294 corr_uid |= (uint64_t)data[7] << 24;\r
1295 corr_uid |= (uint64_t)data[6] << 16;\r
1296 corr_uid |= (uint64_t)data[5] << 8;\r
1297 corr_uid |= (uint64_t)data[4];\r
1298 tryMfk64(corr_uid, data, key);\r
1299 }\r
1300 PrintAndLog("--");\r
1301 }\r
1302 }\r
1303 return 0;\r
1304}\r
1305\r
1306int CmdHF14AMfDbg(const char *Cmd)\r
1307{\r
1308 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
1309 if (dbgMode > 4) {\r
1310 PrintAndLog("Max debug mode parameter is 4 \n");\r
1311 }\r
1312\r
1313 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
1314 PrintAndLog("Usage: hf mf dbg <debug level>");\r
1315 PrintAndLog(" 0 - no debug messages");\r
1316 PrintAndLog(" 1 - error messages");\r
1317 PrintAndLog(" 2 - plus information messages");\r
1318 PrintAndLog(" 3 - plus debug messages");\r
1319 PrintAndLog(" 4 - print even debug messages in timing critical functions");\r
1320 PrintAndLog(" Note: this option therefore may cause malfunction itself");\r
1321 return 0;\r
1322 } \r
1323\r
1324 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
1325 SendCommand(&c);\r
1326\r
1327 return 0;\r
1328}\r
1329\r
1330int CmdHF14AMfEGet(const char *Cmd)\r
1331{\r
1332 uint8_t blockNo = 0;\r
1333 uint8_t data[16] = {0x00};\r
1334\r
1335 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1336 PrintAndLog("Usage: hf mf eget <block number>");\r
1337 PrintAndLog(" sample: hf mf eget 0 ");\r
1338 return 0;\r
1339 } \r
1340 \r
1341 blockNo = param_get8(Cmd, 0);\r
1342\r
1343 PrintAndLog(" ");\r
1344 if (!mfEmlGetMem(data, blockNo, 1)) {\r
1345 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r
1346 } else {\r
1347 PrintAndLog("Command execute timeout");\r
1348 }\r
1349\r
1350 return 0;\r
1351}\r
1352\r
1353int CmdHF14AMfEClear(const char *Cmd)\r
1354{\r
1355 if (param_getchar(Cmd, 0) == 'h') {\r
1356 PrintAndLog("Usage: hf mf eclr");\r
1357 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1358 return 0;\r
1359 } \r
1360\r
1361 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1362 SendCommand(&c);\r
1363 return 0;\r
1364}\r
1365\r
1366int CmdHF14AMfESet(const char *Cmd)\r
1367{\r
1368 uint8_t memBlock[16];\r
1369 uint8_t blockNo = 0;\r
1370\r
1371 memset(memBlock, 0x00, sizeof(memBlock));\r
1372\r
1373 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1374 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1375 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1376 return 0;\r
1377 } \r
1378 \r
1379 blockNo = param_get8(Cmd, 0);\r
1380 \r
1381 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1382 PrintAndLog("block data must include 32 HEX symbols");\r
1383 return 1;\r
1384 }\r
1385 \r
1386 // 1 - blocks count\r
1387 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
1388 memcpy(c.d.asBytes, memBlock, 16);\r
1389 SendCommand(&c);\r
1390 return 0;\r
1391}\r
1392\r
1393int CmdHF14AMfELoad(const char *Cmd)\r
1394{\r
1395 FILE * f;\r
1396 char filename[FILE_PATH_SIZE];\r
1397 char *fnameptr = filename;\r
1398 char buf[64] = {0x00};\r
1399 uint8_t buf8[64] = {0x00};\r
1400 int i, len, blockNum, numBlocks;\r
1401 int nameParamNo = 1;\r
1402 uint8_t blockWidth = 32;\r
1403 char ctmp = param_getchar(Cmd, 0);\r
1404 \r
1405 if ( ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {\r
1406 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
1407 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");\r
1408 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");\r
1409 PrintAndLog("");\r
1410 PrintAndLog(" sample: hf mf eload filename");\r
1411 PrintAndLog(" hf mf eload 4 filename");\r
1412 return 0;\r
1413 } \r
1414\r
1415 switch (ctmp) {\r
1416 case '0' : numBlocks = 5*4; break;\r
1417 case '1' : \r
1418 case '\0': numBlocks = 16*4; break;\r
1419 case '2' : numBlocks = 32*4; break;\r
1420 case '4' : numBlocks = 256; break;\r
1421 case 'U' : // fall through\r
1422 case 'u' : numBlocks = 255; blockWidth = 8; break;\r
1423 default: {\r
1424 numBlocks = 16*4;\r
1425 nameParamNo = 0;\r
1426 }\r
1427 }\r
1428 uint32_t numblk2 = param_get32ex(Cmd,2,0,10);\r
1429 if (numblk2 > 0) numBlocks = numblk2; \r
1430\r
1431 len = param_getstr(Cmd,nameParamNo,filename);\r
1432 \r
1433 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1434\r
1435 fnameptr += len;\r
1436\r
1437 sprintf(fnameptr, ".eml"); \r
1438 \r
1439 // open file\r
1440 f = fopen(filename, "r");\r
1441 if (f == NULL) {\r
1442 PrintAndLog("File %s not found or locked", filename);\r
1443 return 1;\r
1444 }\r
1445 \r
1446 blockNum = 0;\r
1447 while(!feof(f)){\r
1448 memset(buf, 0, sizeof(buf));\r
1449 \r
1450 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1451 \r
1452 if (blockNum >= numBlocks) break;\r
1453 \r
1454 PrintAndLog("File reading error.");\r
1455 fclose(f);\r
1456 return 2;\r
1457 }\r
1458 \r
1459 if (strlen(buf) < blockWidth){\r
1460 if(strlen(buf) && feof(f))\r
1461 break;\r
1462 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth);\r
1463 fclose(f);\r
1464 return 2;\r
1465 }\r
1466 \r
1467 for (i = 0; i < blockWidth; i += 2) {\r
1468 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1469 }\r
1470 if (mfEmlSetMem_xt(buf8, blockNum, 1, blockWidth/2)) {\r
1471 PrintAndLog("Cant set emul block: %3d", blockNum);\r
1472 fclose(f);\r
1473 return 3;\r
1474 }\r
1475 printf(".");\r
1476 blockNum++;\r
1477 \r
1478 if (blockNum >= numBlocks) break;\r
1479 }\r
1480 fclose(f);\r
1481 printf("\n");\r
1482 \r
1483 if ((blockNum != numBlocks)) {\r
1484 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);\r
1485 return 4;\r
1486 }\r
1487 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r
1488 return 0;\r
1489}\r
1490\r
1491int CmdHF14AMfESave(const char *Cmd)\r
1492{\r
1493 FILE * f;\r
1494 char filename[FILE_PATH_SIZE];\r
1495 char * fnameptr = filename;\r
1496 uint8_t buf[64];\r
1497 int i, j, len, numBlocks;\r
1498 int nameParamNo = 1;\r
1499 \r
1500 memset(filename, 0, sizeof(filename));\r
1501 memset(buf, 0, sizeof(buf));\r
1502\r
1503 char ctmp = param_getchar(Cmd, 0);\r
1504 \r
1505 if ( ctmp == 'h' || ctmp == 'H') {\r
1506 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
1507 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");\r
1508 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1509 PrintAndLog("");\r
1510 PrintAndLog(" sample: hf mf esave ");\r
1511 PrintAndLog(" hf mf esave 4");\r
1512 PrintAndLog(" hf mf esave 4 filename");\r
1513 return 0;\r
1514 } \r
1515\r
1516 switch (ctmp) {\r
1517 case '0' : numBlocks = 5*4; break;\r
1518 case '1' : \r
1519 case '\0': numBlocks = 16*4; break;\r
1520 case '2' : numBlocks = 32*4; break;\r
1521 case '4' : numBlocks = 256; break;\r
1522 default: {\r
1523 numBlocks = 16*4;\r
1524 nameParamNo = 0;\r
1525 }\r
1526 }\r
1527\r
1528 len = param_getstr(Cmd,nameParamNo,filename);\r
1529 \r
1530 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1531 \r
1532 // user supplied filename?\r
1533 if (len < 1) {\r
1534 // get filename (UID from memory)\r
1535 if (mfEmlGetMem(buf, 0, 1)) {\r
1536 PrintAndLog("Can\'t get UID from block: %d", 0);\r
1537 len = sprintf(fnameptr, "dump");\r
1538 fnameptr += len;\r
1539 }\r
1540 else {\r
1541 for (j = 0; j < 7; j++, fnameptr += 2)\r
1542 sprintf(fnameptr, "%02X", buf[j]);\r
1543 }\r
1544 } else {\r
1545 fnameptr += len;\r
1546 }\r
1547\r
1548 // add file extension\r
1549 sprintf(fnameptr, ".eml"); \r
1550 \r
1551 // open file\r
1552 f = fopen(filename, "w+");\r
1553\r
1554 if ( !f ) {\r
1555 PrintAndLog("Can't open file %s ", filename);\r
1556 return 1;\r
1557 }\r
1558 \r
1559 // put hex\r
1560 for (i = 0; i < numBlocks; i++) {\r
1561 if (mfEmlGetMem(buf, i, 1)) {\r
1562 PrintAndLog("Cant get block: %d", i);\r
1563 break;\r
1564 }\r
1565 for (j = 0; j < 16; j++)\r
1566 fprintf(f, "%02X", buf[j]); \r
1567 fprintf(f,"\n");\r
1568 }\r
1569 fclose(f);\r
1570 \r
1571 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);\r
1572 \r
1573 return 0;\r
1574}\r
1575\r
1576int CmdHF14AMfECFill(const char *Cmd)\r
1577{\r
1578 uint8_t keyType = 0;\r
1579 uint8_t numSectors = 16;\r
1580 \r
1581 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1582 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r
1583 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1584 PrintAndLog("");\r
1585 PrintAndLog("samples: hf mf ecfill A");\r
1586 PrintAndLog(" hf mf ecfill A 4");\r
1587 PrintAndLog("Read card and transfer its data to emulator memory.");\r
1588 PrintAndLog("Keys must be laid in the emulator memory. \n");\r
1589 return 0;\r
1590 } \r
1591\r
1592 char ctmp = param_getchar(Cmd, 0);\r
1593 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
1594 PrintAndLog("Key type must be A or B");\r
1595 return 1;\r
1596 }\r
1597 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1598\r
1599 ctmp = param_getchar(Cmd, 1);\r
1600 switch (ctmp) {\r
1601 case '0' : numSectors = 5; break;\r
1602 case '1' : \r
1603 case '\0': numSectors = 16; break;\r
1604 case '2' : numSectors = 32; break;\r
1605 case '4' : numSectors = 40; break;\r
1606 default: numSectors = 16;\r
1607 } \r
1608\r
1609 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r
1610 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r
1611 SendCommand(&c);\r
1612 return 0;\r
1613}\r
1614\r
1615int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1616{\r
1617 int i;\r
1618 uint8_t numSectors;\r
1619 uint8_t data[16];\r
1620 uint64_t keyA, keyB;\r
1621\r
1622 char cmdp = param_getchar(Cmd, 0);\r
1623 \r
1624 if ( cmdp == 'h' || cmdp == 'H' ) {\r
1625 PrintAndLog("It prints the keys loaded in the emulator memory");\r
1626 PrintAndLog("Usage: hf mf ekeyprn [card memory]");\r
1627 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1628 PrintAndLog("");\r
1629 PrintAndLog(" sample: hf mf ekeyprn 1");\r
1630 return 0;\r
1631 } \r
1632\r
1633 switch (cmdp) {\r
1634 case '0' : numSectors = 5; break;\r
1635 case '1' : \r
1636 case '\0': numSectors = 16; break;\r
1637 case '2' : numSectors = 32; break;\r
1638 case '4' : numSectors = 40; break;\r
1639 default: numSectors = 16;\r
1640 } \r
1641 \r
1642 PrintAndLog("|---|----------------|----------------|");\r
1643 PrintAndLog("|sec|key A |key B |");\r
1644 PrintAndLog("|---|----------------|----------------|");\r
1645 for (i = 0; i < numSectors; i++) {\r
1646 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r
1647 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
1648 break;\r
1649 }\r
1650 keyA = bytes_to_num(data, 6);\r
1651 keyB = bytes_to_num(data + 10, 6);\r
1652 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r
1653 }\r
1654 PrintAndLog("|---|----------------|----------------|");\r
1655 \r
1656 return 0;\r
1657}\r
1658\r
1659int CmdHF14AMfCSetUID(const char *Cmd)\r
1660{\r
1661 uint8_t wipeCard = 0;\r
1662 uint8_t uid[8] = {0x00};\r
1663 uint8_t oldUid[8] = {0x00};\r
1664 uint8_t atqa[2] = {0x00};\r
1665 uint8_t sak[1] = {0x00};\r
1666 uint8_t atqaPresent = 1;\r
1667 int res;\r
1668 char ctmp;\r
1669 int argi=0;\r
1670\r
1671 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {\r
1672 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");\r
1673 PrintAndLog("sample: hf mf csetuid 01020304");\r
1674 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");\r
1675 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");\r
1676 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");\r
1677 return 0;\r
1678 }\r
1679\r
1680 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {\r
1681 PrintAndLog("UID must include 8 HEX symbols");\r
1682 return 1;\r
1683 }\r
1684 argi++;\r
1685\r
1686 ctmp = param_getchar(Cmd, argi);\r
1687 if (ctmp == 'w' || ctmp == 'W') {\r
1688 wipeCard = 1;\r
1689 atqaPresent = 0;\r
1690 }\r
1691\r
1692 if (atqaPresent) {\r
1693 if (param_getchar(Cmd, argi)) {\r
1694 if (param_gethex(Cmd, argi, atqa, 4)) {\r
1695 PrintAndLog("ATQA must include 4 HEX symbols");\r
1696 return 1;\r
1697 }\r
1698 argi++;\r
1699 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {\r
1700 PrintAndLog("SAK must include 2 HEX symbols");\r
1701 return 1;\r
1702 }\r
1703 argi++;\r
1704 } else\r
1705 atqaPresent = 0;\r
1706 }\r
1707\r
1708 if(!wipeCard) {\r
1709 ctmp = param_getchar(Cmd, argi);\r
1710 if (ctmp == 'w' || ctmp == 'W') {\r
1711 wipeCard = 1;\r
1712 }\r
1713 }\r
1714\r
1715 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));\r
1716\r
1717 res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);\r
1718 if (res) {\r
1719 PrintAndLog("Can't set UID. error=%d", res);\r
1720 return 1;\r
1721 }\r
1722 \r
1723 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
1724 PrintAndLog("new UID:%s", sprint_hex(uid, 4));\r
1725 return 0;\r
1726}\r
1727\r
1728int CmdHF14AMfCSetBlk(const char *Cmd)\r
1729{\r
1730 uint8_t block[16] = {0x00};\r
1731 uint8_t blockNo = 0;\r
1732 uint8_t params = MAGIC_SINGLE;\r
1733 int res;\r
1734\r
1735 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1736 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");\r
1737 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
1738 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");\r
1739 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");\r
1740 return 0;\r
1741 } \r
1742\r
1743 blockNo = param_get8(Cmd, 0);\r
1744\r
1745 if (param_gethex(Cmd, 1, block, 32)) {\r
1746 PrintAndLog("block data must include 32 HEX symbols");\r
1747 return 1;\r
1748 }\r
1749\r
1750 char ctmp = param_getchar(Cmd, 2);\r
1751 if (ctmp == 'w' || ctmp == 'W')\r
1752 params |= MAGIC_WIPE;\r
1753 \r
1754 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(block, 16));\r
1755\r
1756 res = mfCSetBlock(blockNo, block, NULL, params);\r
1757 if (res) {\r
1758 PrintAndLog("Can't write block. error=%d", res);\r
1759 return 1;\r
1760 }\r
1761 return 0;\r
1762}\r
1763\r
1764int CmdHF14AMfCLoad(const char *Cmd)\r
1765{\r
1766 FILE * f;\r
1767 char filename[FILE_PATH_SIZE];\r
1768 char * fnameptr = filename;\r
1769 char buf[64] = {0x00};\r
1770 uint8_t buf8[64] = {0x00};\r
1771 uint8_t fillFromEmulator = 0;\r
1772 int i, len, blockNum, flags=0;\r
1773\r
1774 memset(filename, 0, sizeof(filename));\r
1775 \r
1776 char ctmp = param_getchar(Cmd, 0);\r
1777 \r
1778 if (ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {\r
1779 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");\r
1780 PrintAndLog("or from emulator memory (option `e`)");\r
1781 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1782 PrintAndLog(" or: hf mf cload e ");\r
1783 PrintAndLog(" sample: hf mf cload filename");\r
1784 return 0;\r
1785 } \r
1786\r
1787 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1788 \r
1789 if (fillFromEmulator) {\r
1790 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1791 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1792 PrintAndLog("Cant get block: %d", blockNum);\r
1793 return 2;\r
1794 }\r
1795 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence\r
1796 if (blockNum == 1) flags = 0; // just write\r
1797 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Magic Halt and switch off field.\r
1798\r
1799 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {\r
1800 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1801 return 3;\r
1802 }\r
1803 }\r
1804 return 0;\r
1805 } else {\r
1806 len = strlen(Cmd);\r
1807 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1808\r
1809 memcpy(filename, Cmd, len);\r
1810 fnameptr += len;\r
1811\r
1812 sprintf(fnameptr, ".eml"); \r
1813 \r
1814 // open file\r
1815 f = fopen(filename, "r");\r
1816 if (f == NULL) {\r
1817 PrintAndLog("File not found or locked.");\r
1818 return 1;\r
1819 }\r
1820 \r
1821 blockNum = 0;\r
1822 while(!feof(f)){\r
1823 \r
1824 memset(buf, 0, sizeof(buf));\r
1825 \r
1826 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1827 fclose(f);\r
1828 PrintAndLog("File reading error.");\r
1829 return 2;\r
1830 }\r
1831\r
1832 if (strlen(buf) < 32) {\r
1833 if(strlen(buf) && feof(f))\r
1834 break;\r
1835 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1836 fclose(f);\r
1837 return 2;\r
1838 }\r
1839 for (i = 0; i < 32; i += 2)\r
1840 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1841\r
1842 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence\r
1843 if (blockNum == 1) flags = 0; // just write\r
1844 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Switch off field.\r
1845\r
1846 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {\r
1847 PrintAndLog("Can't set magic card block: %d", blockNum);\r
1848 fclose(f);\r
1849 return 3;\r
1850 }\r
1851 blockNum++;\r
1852 \r
1853 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1854 }\r
1855 fclose(f);\r
1856 \r
1857 // 64 or 256blocks.\r
1858 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
1859 PrintAndLog("File content error. There must be 64 blocks");\r
1860 return 4;\r
1861 }\r
1862 PrintAndLog("Loaded from file: %s", filename);\r
1863 return 0;\r
1864 }\r
1865 return 0;\r
1866}\r
1867\r
1868int CmdHF14AMfCGetBlk(const char *Cmd) {\r
1869 uint8_t data[16];\r
1870 uint8_t blockNo = 0;\r
1871 int res;\r
1872 memset(data, 0x00, sizeof(data));\r
1873 char ctmp = param_getchar(Cmd, 0);\r
1874\r
1875 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {\r
1876 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1877 PrintAndLog("sample: hf mf cgetblk 1");\r
1878 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");\r
1879 return 0;\r
1880 } \r
1881\r
1882 blockNo = param_get8(Cmd, 0);\r
1883\r
1884 PrintAndLog("--block number:%2d ", blockNo);\r
1885\r
1886 res = mfCGetBlock(blockNo, data, MAGIC_SINGLE);\r
1887 if (res) {\r
1888 PrintAndLog("Can't read block. error=%d", res);\r
1889 return 1;\r
1890 }\r
1891 \r
1892 PrintAndLog("data: %s", sprint_hex(data, sizeof(data)));\r
1893 return 0;\r
1894}\r
1895\r
1896int CmdHF14AMfCGetSc(const char *Cmd) {\r
1897 uint8_t data[16];\r
1898 uint8_t sectorNo = 0;\r
1899 int i, res, flags;\r
1900 memset(data, 0x00, sizeof(data));\r
1901 char ctmp = param_getchar(Cmd, 0);\r
1902 \r
1903 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {\r
1904 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1905 PrintAndLog("sample: hf mf cgetsc 0");\r
1906 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");\r
1907 return 0;\r
1908 } \r
1909\r
1910 sectorNo = param_get8(Cmd, 0);\r
1911 if (sectorNo > 15) {\r
1912 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1913 return 1;\r
1914 }\r
1915\r
1916 PrintAndLog("--sector number:%d ", sectorNo);\r
1917 PrintAndLog("block | data");\r
1918\r
1919 flags = MAGIC_INIT + MAGIC_WUPC;\r
1920 for (i = 0; i < 4; i++) {\r
1921 if (i == 1) flags = 0;\r
1922 if (i == 3) flags = MAGIC_HALT + MAGIC_OFF;\r
1923\r
1924 res = mfCGetBlock(sectorNo * 4 + i, data, flags);\r
1925 if (res) {\r
1926 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);\r
1927 return 1;\r
1928 } \r
1929 PrintAndLog(" %3d | %s", sectorNo * 4 + i, sprint_hex(data, sizeof(data)));\r
1930 }\r
1931 return 0;\r
1932}\r
1933\r
1934int CmdHF14AMfCSave(const char *Cmd) {\r
1935\r
1936 FILE * f;\r
1937 char filename[FILE_PATH_SIZE];\r
1938 char * fnameptr = filename;\r
1939 uint8_t fillFromEmulator = 0;\r
1940 uint8_t buf[64];\r
1941 int i, j, len, flags;\r
1942 \r
1943 memset(filename, 0, sizeof(filename));\r
1944 memset(buf, 0, sizeof(buf));\r
1945 char ctmp = param_getchar(Cmd, 0);\r
1946 \r
1947 if ( ctmp == 'h' || ctmp == 'H' ) {\r
1948 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1949 PrintAndLog("or into emulator memory (option `e`)");\r
1950 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1951 PrintAndLog(" sample: hf mf esave ");\r
1952 PrintAndLog(" hf mf esave filename");\r
1953 PrintAndLog(" hf mf esave e \n");\r
1954 return 0;\r
1955 } \r
1956 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1957\r
1958 if (fillFromEmulator) {\r
1959 // put into emulator\r
1960 flags = MAGIC_INIT + MAGIC_WUPC;\r
1961 for (i = 0; i < 16 * 4; i++) {\r
1962 if (i == 1) flags = 0;\r
1963 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;\r
1964 \r
1965 if (mfCGetBlock(i, buf, flags)) {\r
1966 PrintAndLog("Cant get block: %d", i);\r
1967 break;\r
1968 }\r
1969 \r
1970 if (mfEmlSetMem(buf, i, 1)) {\r
1971 PrintAndLog("Cant set emul block: %d", i);\r
1972 return 3;\r
1973 }\r
1974 }\r
1975 return 0;\r
1976 } else {\r
1977 len = strlen(Cmd);\r
1978 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1979 \r
1980 // get filename based on UID\r
1981 if (len < 1) {\r
1982 \r
1983 if (mfCGetBlock(0, buf, MAGIC_SINGLE)) {\r
1984 PrintAndLog("Cant get block: %d", 0);\r
1985 len = sprintf(fnameptr, "dump");\r
1986 fnameptr += len;\r
1987 } else {\r
1988 for (j = 0; j < 7; j++, fnameptr += 2)\r
1989 sprintf(fnameptr, "%02x", buf[j]); \r
1990 }\r
1991 } else {\r
1992 memcpy(filename, Cmd, len);\r
1993 fnameptr += len;\r
1994 }\r
1995\r
1996 // add .eml extension\r
1997 sprintf(fnameptr, ".eml"); \r
1998 \r
1999 // open file\r
2000 f = fopen(filename, "w+");\r
2001\r
2002 if (f == NULL) {\r
2003 PrintAndLog("File not found or locked.");\r
2004 return 1;\r
2005 }\r
2006\r
2007 // put hex\r
2008 flags = MAGIC_INIT + MAGIC_WUPC;\r
2009 for (i = 0; i < 16 * 4; i++) {\r
2010 if (i == 1) flags = 0;\r
2011 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;\r
2012 \r
2013 if (mfCGetBlock(i, buf, flags)) {\r
2014 PrintAndLog("Cant get block: %d", i);\r
2015 break;\r
2016 }\r
2017 for (j = 0; j < 16; j++)\r
2018 fprintf(f, "%02x", buf[j]); \r
2019 fprintf(f,"\n");\r
2020 }\r
2021 fflush(f);\r
2022 fclose(f);\r
2023 PrintAndLog("Saved to file: %s", filename);\r
2024 return 0;\r
2025 }\r
2026}\r
2027\r
2028int CmdHF14AMfSniff(const char *Cmd){\r
2029\r
2030 bool wantLogToFile = 0;\r
2031 bool wantDecrypt = 0;\r
2032 //bool wantSaveToEml = 0; TODO\r
2033 bool wantSaveToEmlFile = 0;\r
2034\r
2035 //var \r
2036 int tmpchar;\r
2037 int res = 0;\r
2038 int len = 0;\r
2039 int blockLen = 0;\r
2040 int pckNum = 0;\r
2041 int num = 0;\r
2042 uint8_t uid[7];\r
2043 uint8_t uid_len;\r
2044 uint8_t atqa[2] = {0x00};\r
2045 uint8_t sak;\r
2046 bool isTag;\r
2047 uint8_t *buf = NULL;\r
2048 uint16_t bufsize = 0;\r
2049 uint8_t *bufPtr = NULL;\r
2050 \r
2051 char ctmp = param_getchar(Cmd, 0);\r
2052 if ( ctmp == 'h' || ctmp == 'H' ) {\r
2053 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r
2054 PrintAndLog("You can specify:");\r
2055 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
2056 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
2057 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
2058 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r
2059 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");\r
2060 PrintAndLog(" sample: hf mf sniff l d e");\r
2061 return 0;\r
2062 } \r
2063 \r
2064 for (int i = 0; i < 4; i++) {\r
2065 ctmp = param_getchar(Cmd, i);\r
2066 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
2067 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
2068 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
2069 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
2070 }\r
2071 \r
2072 printf("-------------------------------------------------------------------------\n");\r
2073 printf("Executing command. \n");\r
2074 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
2075 printf("Press the key on pc keyboard to abort the client.\n");\r
2076 printf("-------------------------------------------------------------------------\n");\r
2077\r
2078 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
2079 clearCommandBuffer();\r
2080 SendCommand(&c);\r
2081\r
2082 // wait cycle\r
2083 while (true) {\r
2084 printf(".");\r
2085 fflush(stdout);\r
2086 if (ukbhit()) {\r
2087 tmpchar = getchar();\r
2088 (void)tmpchar;\r
2089 printf("\naborted via keyboard!\n");\r
2090 break;\r
2091 }\r
2092 \r
2093 UsbCommand resp;\r
2094 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
2095 res = resp.arg[0] & 0xff;\r
2096 uint16_t traceLen = resp.arg[1];\r
2097 len = resp.arg[2];\r
2098\r
2099 if (res == 0) {\r
2100 free(buf);\r
2101 return 0; // we are done\r
2102 }\r
2103\r
2104 if (res == 1) { // there is (more) data to be transferred\r
2105 if (pckNum == 0) { // first packet, (re)allocate necessary buffer\r
2106 if (traceLen > bufsize) {\r
2107 uint8_t *p;\r
2108 if (buf == NULL) { // not yet allocated\r
2109 p = malloc(traceLen);\r
2110 } else { // need more memory\r
2111 p = realloc(buf, traceLen);\r
2112 }\r
2113 if (p == NULL) {\r
2114 PrintAndLog("Cannot allocate memory for trace");\r
2115 free(buf);\r
2116 return 2;\r
2117 }\r
2118 buf = p;\r
2119 }\r
2120 bufPtr = buf;\r
2121 bufsize = traceLen;\r
2122 memset(buf, 0x00, traceLen);\r
2123 }\r
2124 if (bufPtr == NULL) {\r
2125 PrintAndLog("Cannot allocate memory for trace");\r
2126 free(buf);\r
2127 return 2;\r
2128 }\r
2129 memcpy(bufPtr, resp.d.asBytes, len);\r
2130 bufPtr += len;\r
2131 pckNum++;\r
2132 }\r
2133\r
2134 if (res == 2) { // received all data, start displaying\r
2135 blockLen = bufPtr - buf;\r
2136 bufPtr = buf;\r
2137 printf(">\n");\r
2138 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
2139 while (bufPtr - buf < blockLen) {\r
2140 bufPtr += 6; // skip (void) timing information\r
2141 len = *((uint16_t *)bufPtr);\r
2142 if(len & 0x8000) {\r
2143 isTag = true;\r
2144 len &= 0x7fff;\r
2145 } else {\r
2146 isTag = false;\r
2147 }\r
2148 bufPtr += 2;\r
2149 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
2150 memcpy(uid, bufPtr + 2, 7);\r
2151 memcpy(atqa, bufPtr + 2 + 7, 2);\r
2152 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
2153 sak = bufPtr[11];\r
2154 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
2155 sprint_hex(uid + (7 - uid_len), uid_len),\r
2156 atqa[1], \r
2157 atqa[0], \r
2158 sak);\r
2159 if (wantLogToFile || wantDecrypt) {\r
2160 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r
2161 AddLogCurrentDT(logHexFileName);\r
2162 } \r
2163 if (wantDecrypt) \r
2164 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
2165 } else {\r
2166 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
2167 if (wantLogToFile) \r
2168 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
2169 if (wantDecrypt) \r
2170 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
2171 num++; \r
2172 }\r
2173 bufPtr += len;\r
2174 bufPtr += ((len-1)/8+1); // ignore parity\r
2175 }\r
2176 pckNum = 0;\r
2177 }\r
2178 } // resp not NULL\r
2179 } // while (true)\r
2180\r
2181 free(buf);\r
2182 return 0;\r
2183}\r
2184\r
2185//needs nt, ar, at, Data to decrypt\r
2186int CmdHf14MfDecryptBytes(const char *Cmd){\r
2187 uint8_t data[50];\r
2188 \r
2189 uint32_t nt = param_get32ex(Cmd,0,0,16);\r
2190 uint32_t ar_enc = param_get32ex(Cmd,1,0,16);\r
2191 uint32_t at_enc = param_get32ex(Cmd,2,0,16);\r
2192\r
2193 int len = 0;\r
2194 param_gethex_ex(Cmd, 3, data, &len);\r
2195 \r
2196 len /= 2; \r
2197 int limit = sizeof(data) / 2;\r
2198 \r
2199 if ( len >= limit )\r
2200 len = limit;\r
2201 \r
2202 return tryDecryptWord( nt, ar_enc, at_enc, data, len);\r
2203}\r
2204\r
2205static command_t CommandTable[] = {\r
2206 {"help", CmdHelp, 1, "This help"},\r
2207 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
2208 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
2209 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
2210 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
2211 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
2212 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
2213 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
2214 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
2215 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
2216 {"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},\r
2217 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
2218 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
2219 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
2220 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
2221 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
2222 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
2223 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
2224 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
2225 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
2226 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
2227 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},\r
2228 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},\r
2229 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},\r
2230 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
2231 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
2232 {"decrypt", CmdHf14MfDecryptBytes, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
2233 {NULL, NULL, 0, NULL}\r
2234};\r
2235\r
2236int CmdHFMF(const char *Cmd) {\r
2237 // flush\r
2238 clearCommandBuffer();\r
2239 //WaitForResponseTimeout(CMD_ACK,NULL,100);\r
2240 CmdsParse(CommandTable, Cmd);\r
2241 return 0;\r
2242}\r
2243\r
2244int CmdHelp(const char *Cmd) {\r
2245 CmdsHelp(CommandTable);\r
2246 return 0;\r
2247}\r
Impressum, Datenschutz