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