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