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