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