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