]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdhfmf.c
Code cleanup:
[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 <inttypes.h>\r
12#include "cmdhfmf.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
33 start:\r
34 clearCommandBuffer();\r
35 SendCommand(&c);\r
36 \r
37 //flush queue\r
38 while (ukbhit()) {\r
39 int c = getchar(); (void) c;\r
40 }\r
41 \r
42 // wait cycle\r
43 while (true) {\r
44 printf(".");\r
45 fflush(stdout);\r
46 if (ukbhit()) {\r
47 getchar();\r
48 printf("\naborted via keyboard!\n");\r
49 break;\r
50 }\r
51 \r
52 UsbCommand resp;\r
53 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r
54 isOK = resp.arg[0];\r
55 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r
56 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r
57 par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r
58 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r
59 nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
60 printf("\n\n");\r
61 switch (isOK) {\r
62 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;\r
63 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;\r
64 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;\r
65 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");\r
66 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;\r
67 default: ;\r
68 }\r
69 break;\r
70 }\r
71 } \r
72\r
73 printf("\n");\r
74 \r
75 // error\r
76 if (isOK != 1) return 1;\r
77 \r
78 // execute original function from util nonce2key\r
79 if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key)) {\r
80 isOK = 2;\r
81 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r
82 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r
83 c.arg[0] = false;\r
84 goto start;\r
85 } else {\r
86 isOK = 0;\r
87 printf("------------------------------------------------------------------\n");\r
88 PrintAndLog("Found valid key:%012" PRIx64 " \n", r_key);\r
89 }\r
90 \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 SendCommand(&c);\r
132\r
133 UsbCommand resp;\r
134 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
135 uint8_t isOK = resp.arg[0] & 0xff;\r
136 PrintAndLog("isOk:%02x", isOK);\r
137 } else {\r
138 PrintAndLog("Command execute timeout");\r
139 }\r
140\r
141 return 0;\r
142}\r
143\r
144int CmdHF14AMfRdBl(const char *Cmd)\r
145{\r
146 uint8_t blockNo = 0;\r
147 uint8_t keyType = 0;\r
148 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
149 \r
150 char cmdp = 0x00;\r
151\r
152\r
153 if (strlen(Cmd)<3) {\r
154 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r
155 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r
156 return 0;\r
157 } \r
158 \r
159 blockNo = param_get8(Cmd, 0);\r
160 cmdp = param_getchar(Cmd, 1);\r
161 if (cmdp == 0x00) {\r
162 PrintAndLog("Key type must be A or B");\r
163 return 1;\r
164 }\r
165 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
166 if (param_gethex(Cmd, 2, key, 12)) {\r
167 PrintAndLog("Key must include 12 HEX symbols");\r
168 return 1;\r
169 }\r
170 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
171 \r
172 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r
173 memcpy(c.d.asBytes, key, 6);\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 SendCommand(&c);\r
228 PrintAndLog(" ");\r
229\r
230 UsbCommand resp;\r
231 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
232 isOK = resp.arg[0] & 0xff;\r
233 data = resp.d.asBytes;\r
234\r
235 PrintAndLog("isOk:%02x", isOK);\r
236 if (isOK) {\r
237 for (i = 0; i < (sectorNo<32?3:15); i++) {\r
238 PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));\r
239 }\r
240 PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));\r
241 }\r
242 } else {\r
243 PrintAndLog("Command execute timeout");\r
244 }\r
245\r
246 return 0;\r
247}\r
248\r
249uint8_t FirstBlockOfSector(uint8_t sectorNo)\r
250{\r
251 if (sectorNo < 32) {\r
252 return sectorNo * 4;\r
253 } else {\r
254 return 32 * 4 + (sectorNo - 32) * 16;\r
255 }\r
256}\r
257\r
258uint8_t NumBlocksPerSector(uint8_t sectorNo)\r
259{\r
260 if (sectorNo < 32) {\r
261 return 4;\r
262 } else {\r
263 return 16;\r
264 }\r
265}\r
266\r
267int CmdHF14AMfDump(const char *Cmd)\r
268{\r
269 uint8_t sectorNo, blockNo;\r
270 \r
271 uint8_t keyA[40][6];\r
272 uint8_t keyB[40][6];\r
273 uint8_t rights[40][4];\r
274 uint8_t carddata[256][16];\r
275 uint8_t numSectors = 16;\r
276 \r
277 FILE *fin;\r
278 FILE *fout;\r
279 \r
280 UsbCommand resp;\r
281\r
282 char cmdp = param_getchar(Cmd, 0);\r
283 switch (cmdp) {\r
284 case '0' : numSectors = 5; break;\r
285 case '1' : \r
286 case '\0': numSectors = 16; break;\r
287 case '2' : numSectors = 32; break;\r
288 case '4' : numSectors = 40; break;\r
289 default: numSectors = 16;\r
290 } \r
291 \r
292 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r
293 PrintAndLog("Usage: hf mf dump [card memory]");\r
294 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
295 PrintAndLog("");\r
296 PrintAndLog("Samples: hf mf dump");\r
297 PrintAndLog(" hf mf dump 4");\r
298 return 0;\r
299 }\r
300 \r
301 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r
302 PrintAndLog("Could not find file dumpkeys.bin");\r
303 return 1;\r
304 }\r
305 \r
306 // Read keys A from file\r
307 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
308 size_t bytes_read = fread(keyA[sectorNo], 1, 6, fin);\r
309 if (bytes_read != 6) {\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 size_t bytes_read = fread(keyB[sectorNo], 1, 6, fin);\r
319 if (bytes_read != 6) {\r
320 PrintAndLog("File reading error.");\r
321 fclose(fin);\r
322 return 2;\r
323 }\r
324 }\r
325 \r
326 fclose(fin);\r
327\r
328 PrintAndLog("|-----------------------------------------|");\r
329 PrintAndLog("|------ Reading sector access bits...-----|");\r
330 PrintAndLog("|-----------------------------------------|");\r
331 \r
332 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
333 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};\r
334 memcpy(c.d.asBytes, keyA[sectorNo], 6);\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 SendCommand(&c);\r
370 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
371 } else { // data block. Check if it can be read with key A or key B\r
372 uint8_t data_area = sectorNo<32?blockNo:blockNo/5;\r
373 if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) { // only key B would work\r
374 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r
375 memcpy(c.d.asBytes, keyB[sectorNo], 6);\r
376 SendCommand(&c);\r
377 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
378 } else if (rights[sectorNo][data_area] == 0x07) { // no key would work\r
379 isOK = false;\r
380 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);\r
381 } else { // key A would work\r
382 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r
383 memcpy(c.d.asBytes, keyA[sectorNo], 6);\r
384 SendCommand(&c);\r
385 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
386 }\r
387 }\r
388\r
389 if (received) {\r
390 isOK = resp.arg[0] & 0xff;\r
391 uint8_t *data = resp.d.asBytes;\r
392 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.\r
393 data[0] = (keyA[sectorNo][0]);\r
394 data[1] = (keyA[sectorNo][1]);\r
395 data[2] = (keyA[sectorNo][2]);\r
396 data[3] = (keyA[sectorNo][3]);\r
397 data[4] = (keyA[sectorNo][4]);\r
398 data[5] = (keyA[sectorNo][5]);\r
399 data[10] = (keyB[sectorNo][0]);\r
400 data[11] = (keyB[sectorNo][1]);\r
401 data[12] = (keyB[sectorNo][2]);\r
402 data[13] = (keyB[sectorNo][3]);\r
403 data[14] = (keyB[sectorNo][4]);\r
404 data[15] = (keyB[sectorNo][5]);\r
405 }\r
406 if (isOK) {\r
407 memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);\r
408 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo, sectorNo);\r
409 } else {\r
410 PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);\r
411 break;\r
412 }\r
413 }\r
414 else {\r
415 isOK = false;\r
416 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo, sectorNo);\r
417 break;\r
418 }\r
419 }\r
420 }\r
421\r
422 if (isOK) {\r
423 if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r
424 PrintAndLog("Could not create file name dumpdata.bin");\r
425 return 1;\r
426 }\r
427 uint16_t numblocks = FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1);\r
428 fwrite(carddata, 1, 16*numblocks, fout);\r
429 fclose(fout);\r
430 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks, 16*numblocks);\r
431 }\r
432 \r
433 return 0;\r
434}\r
435\r
436int CmdHF14AMfRestore(const char *Cmd)\r
437{\r
438 uint8_t sectorNo,blockNo;\r
439 uint8_t keyType = 0;\r
440 uint8_t key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};\r
441 uint8_t bldata[16] = {0x00};\r
442 uint8_t keyA[40][6];\r
443 uint8_t keyB[40][6];\r
444 uint8_t numSectors;\r
445 \r
446 FILE *fdump;\r
447 FILE *fkeys;\r
448\r
449 char cmdp = param_getchar(Cmd, 0);\r
450 switch (cmdp) {\r
451 case '0' : numSectors = 5; break;\r
452 case '1' : \r
453 case '\0': numSectors = 16; break;\r
454 case '2' : numSectors = 32; break;\r
455 case '4' : numSectors = 40; break;\r
456 default: numSectors = 16;\r
457 } \r
458\r
459 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r
460 PrintAndLog("Usage: hf mf restore [card memory]");\r
461 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
462 PrintAndLog("");\r
463 PrintAndLog("Samples: hf mf restore");\r
464 PrintAndLog(" hf mf restore 4");\r
465 return 0;\r
466 }\r
467\r
468 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r
469 PrintAndLog("Could not find file dumpkeys.bin");\r
470 return 1;\r
471 }\r
472 \r
473 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
474 size_t bytes_read = fread(keyA[sectorNo], 1, 6, fkeys);\r
475 if (bytes_read != 6) {\r
476 PrintAndLog("File reading error (dumpkeys.bin).");\r
477 fclose(fkeys);\r
478 return 2;\r
479 }\r
480 }\r
481\r
482 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
483 size_t bytes_read = fread(keyB[sectorNo], 1, 6, fkeys);\r
484 if (bytes_read != 6) {\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 size_t bytes_read = fread(bldata, 1, 16, fdump);\r
505 if (bytes_read != 16) {\r
506 PrintAndLog("File reading error (dumpdata.bin).");\r
507 fclose(fdump);\r
508 return 2;\r
509 }\r
510 \r
511 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer\r
512 bldata[0] = (keyA[sectorNo][0]);\r
513 bldata[1] = (keyA[sectorNo][1]);\r
514 bldata[2] = (keyA[sectorNo][2]);\r
515 bldata[3] = (keyA[sectorNo][3]);\r
516 bldata[4] = (keyA[sectorNo][4]);\r
517 bldata[5] = (keyA[sectorNo][5]);\r
518 bldata[10] = (keyB[sectorNo][0]);\r
519 bldata[11] = (keyB[sectorNo][1]);\r
520 bldata[12] = (keyB[sectorNo][2]);\r
521 bldata[13] = (keyB[sectorNo][3]);\r
522 bldata[14] = (keyB[sectorNo][4]);\r
523 bldata[15] = (keyB[sectorNo][5]);\r
524 } \r
525 \r
526 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));\r
527 \r
528 memcpy(c.d.asBytes + 10, bldata, 16);\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" PRIx64, 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" PRIx64, 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" PRIx64 " | %d | %012" PRIx64 " | %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 CmdHF14AMfChk(const char *Cmd)\r
796{\r
797 if (strlen(Cmd)<3) {\r
798 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r
799 PrintAndLog(" * - all sectors");\r
800 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
801 PrintAndLog("d - write keys to binary file\n");\r
802 PrintAndLog("t - write keys to emulator memory");\r
803 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r
804 PrintAndLog(" hf mf chk *1 ? t");\r
805 PrintAndLog(" hf mf chk *1 ? d");\r
806 return 0;\r
807 } \r
808\r
809 FILE * f;\r
810 char filename[FILE_PATH_SIZE]={0};\r
811 char buf[13];\r
812 uint8_t *keyBlock = NULL, *p;\r
813 uint8_t stKeyBlock = 20;\r
814 \r
815 int i, res;\r
816 int keycnt = 0;\r
817 char ctmp = 0x00;\r
818 uint8_t blockNo = 0;\r
819 uint8_t SectorsCnt = 1;\r
820 uint8_t keyType = 0;\r
821 uint64_t key64 = 0;\r
822 \r
823 int transferToEml = 0;\r
824 int createDumpFile = 0;\r
825\r
826 keyBlock = calloc(stKeyBlock, 6);\r
827 if (keyBlock == NULL) return 1;\r
828\r
829 uint64_t defaultKeys[] =\r
830 {\r
831 0xffffffffffff, // Default key (first key used by program if no user defined key)\r
832 0x000000000000, // Blank key\r
833 0xa0a1a2a3a4a5, // NFCForum MAD key\r
834 0xb0b1b2b3b4b5,\r
835 0xaabbccddeeff,\r
836 0x4d3a99c351dd,\r
837 0x1a982c7e459a,\r
838 0xd3f7d3f7d3f7,\r
839 0x714c5c886e97,\r
840 0x587ee5f9350f,\r
841 0xa0478cc39091,\r
842 0x533cb6c723f6,\r
843 0x8fd0a4f256e9\r
844 };\r
845 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);\r
846\r
847 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
848 {\r
849 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
850 }\r
851 \r
852 if (param_getchar(Cmd, 0)=='*') {\r
853 blockNo = 3;\r
854 switch(param_getchar(Cmd+1, 0)) {\r
855 case '0': SectorsCnt = 5; break;\r
856 case '1': SectorsCnt = 16; break;\r
857 case '2': SectorsCnt = 32; break;\r
858 case '4': SectorsCnt = 40; break;\r
859 default: SectorsCnt = 16;\r
860 }\r
861 }\r
862 else\r
863 blockNo = param_get8(Cmd, 0);\r
864 \r
865 ctmp = param_getchar(Cmd, 1);\r
866 switch (ctmp) { \r
867 case 'a': case 'A':\r
868 keyType = !0;\r
869 break;\r
870 case 'b': case 'B':\r
871 keyType = !1;\r
872 break;\r
873 case '?':\r
874 keyType = 2;\r
875 break;\r
876 default:\r
877 PrintAndLog("Key type must be A , B or ?");\r
878 return 1;\r
879 };\r
880 \r
881 ctmp = param_getchar(Cmd, 2);\r
882 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
883 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
884 \r
885 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r
886 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r
887 if ( stKeyBlock - keycnt < 2) {\r
888 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
889 if (!p) {\r
890 PrintAndLog("Cannot allocate memory for Keys");\r
891 free(keyBlock);\r
892 return 2;\r
893 }\r
894 keyBlock = p;\r
895 }\r
896 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
897 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
898 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
899 keycnt++;\r
900 } else {\r
901 // May be a dic file\r
902 if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {\r
903 PrintAndLog("File name too long");\r
904 free(keyBlock);\r
905 return 2;\r
906 }\r
907 \r
908 if ( (f = fopen( filename , "r")) ) {\r
909 while( fgets(buf, sizeof(buf), f) ){\r
910 if (strlen(buf) < 12 || buf[11] == '\n')\r
911 continue;\r
912 \r
913 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
914 \r
915 if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r
916\r
917 if (!isxdigit(buf[0])){\r
918 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
919 continue;\r
920 }\r
921 \r
922 buf[12] = 0;\r
923\r
924 if ( stKeyBlock - keycnt < 2) {\r
925 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
926 if (!p) {\r
927 PrintAndLog("Cannot allocate memory for defKeys");\r
928 free(keyBlock);\r
929 fclose(f);\r
930 return 2;\r
931 }\r
932 keyBlock = p;\r
933 }\r
934 memset(keyBlock + 6 * keycnt, 0, 6);\r
935 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
936 PrintAndLog("chk custom key[%2d] %012" PRIx64 , keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
937 keycnt++;\r
938 memset(buf, 0, sizeof(buf));\r
939 }\r
940 fclose(f);\r
941 } else {\r
942 PrintAndLog("File: %s: not found or locked.", filename);\r
943 free(keyBlock);\r
944 return 1;\r
945 \r
946 }\r
947 }\r
948 }\r
949 \r
950 if (keycnt == 0) {\r
951 PrintAndLog("No key specified, trying default keys");\r
952 for (;keycnt < defaultKeysSize; keycnt++)\r
953 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
954 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
955 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
956 }\r
957 \r
958 // initialize storage for found keys\r
959 bool validKey[2][40];\r
960 uint8_t foundKey[2][40][6];\r
961 for (uint16_t t = 0; t < 2; t++) {\r
962 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
963 validKey[t][sectorNo] = false;\r
964 for (uint16_t i = 0; i < 6; i++) {\r
965 foundKey[t][sectorNo][i] = 0xff;\r
966 }\r
967 }\r
968 }\r
969 \r
970 for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {\r
971 int b=blockNo;\r
972 for (int i = 0; i < SectorsCnt; ++i) {\r
973 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);\r
974 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r
975 for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r
976 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r
977 res = mfCheckKeys(b, t, true, size, &keyBlock[6*c], &key64);\r
978 if (res != 1) {\r
979 if (!res) {\r
980 PrintAndLog("Found valid key:[%012" PRIx64 "]",key64);\r
981 num_to_bytes(key64, 6, foundKey[t][i]);\r
982 validKey[t][i] = true;\r
983 } \r
984 } else {\r
985 PrintAndLog("Command execute timeout");\r
986 }\r
987 }\r
988 b<127?(b+=4):(b+=16); \r
989 }\r
990 }\r
991\r
992 if (transferToEml) {\r
993 uint8_t block[16];\r
994 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
995 if (validKey[0][sectorNo] || validKey[1][sectorNo]) {\r
996 mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
997 for (uint16_t t = 0; t < 2; t++) {\r
998 if (validKey[t][sectorNo]) {\r
999 memcpy(block + t*10, foundKey[t][sectorNo], 6);\r
1000 }\r
1001 }\r
1002 mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
1003 }\r
1004 }\r
1005 PrintAndLog("Found keys have been transferred to the emulator memory");\r
1006 }\r
1007\r
1008 if (createDumpFile) {\r
1009 FILE *fkeys = fopen("dumpkeys.bin","wb");\r
1010 if (fkeys == NULL) { \r
1011 PrintAndLog("Could not create file dumpkeys.bin");\r
1012 free(keyBlock);\r
1013 return 1;\r
1014 }\r
1015 for (uint16_t t = 0; t < 2; t++) {\r
1016 fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);\r
1017 }\r
1018 fclose(fkeys);\r
1019 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");\r
1020 }\r
1021\r
1022 free(keyBlock);\r
1023 PrintAndLog("");\r
1024 return 0;\r
1025}\r
1026\r
1027void readerAttack(nonces_t ar_resp[], bool setEmulatorMem, bool doStandardAttack) {\r
1028 #define ATTACK_KEY_COUNT 8 // keep same as define in iso14443a.c -> Mifare1ksim()\r
1029 uint64_t key = 0;\r
1030 typedef struct {\r
1031 uint64_t keyA;\r
1032 uint64_t keyB;\r
1033 } st_t;\r
1034 st_t sector_trailer[ATTACK_KEY_COUNT];\r
1035 memset(sector_trailer, 0x00, sizeof(sector_trailer));\r
1036\r
1037 uint8_t stSector[ATTACK_KEY_COUNT];\r
1038 memset(stSector, 0x00, sizeof(stSector));\r
1039 uint8_t key_cnt[ATTACK_KEY_COUNT];\r
1040 memset(key_cnt, 0x00, sizeof(key_cnt));\r
1041\r
1042 for (uint8_t i = 0; i<ATTACK_KEY_COUNT; i++) {\r
1043 if (ar_resp[i].ar2 > 0) {\r
1044 //PrintAndLog("DEBUG: Trying sector %d, cuid %08x, nt %08x, ar %08x, nr %08x, ar2 %08x, nr2 %08x",ar_resp[i].sector, ar_resp[i].cuid,ar_resp[i].nonce,ar_resp[i].ar,ar_resp[i].nr,ar_resp[i].ar2,ar_resp[i].nr2);\r
1045 if (doStandardAttack && mfkey32(ar_resp[i], &key)) {\r
1046 PrintAndLog(" Found Key%s for sector %02d: [%04x%08x]", (ar_resp[i].keytype) ? "B" : "A", ar_resp[i].sector, (uint32_t) (key>>32), (uint32_t) (key &0xFFFFFFFF));\r
1047\r
1048 for (uint8_t ii = 0; ii<ATTACK_KEY_COUNT; ii++) {\r
1049 if (key_cnt[ii]==0 || stSector[ii]==ar_resp[i].sector) {\r
1050 if (ar_resp[i].keytype==0) {\r
1051 //keyA\r
1052 sector_trailer[ii].keyA = key;\r
1053 stSector[ii] = ar_resp[i].sector;\r
1054 key_cnt[ii]++;\r
1055 break;\r
1056 } else {\r
1057 //keyB\r
1058 sector_trailer[ii].keyB = key;\r
1059 stSector[ii] = ar_resp[i].sector;\r
1060 key_cnt[ii]++;\r
1061 break;\r
1062 }\r
1063 }\r
1064 }\r
1065 } else if (tryMfk32_moebius(ar_resp[i+ATTACK_KEY_COUNT], &key)) {\r
1066 uint8_t sectorNum = ar_resp[i+ATTACK_KEY_COUNT].sector;\r
1067 uint8_t keyType = ar_resp[i+ATTACK_KEY_COUNT].keytype;\r
1068\r
1069 PrintAndLog("M-Found Key%s for sector %02d: [%012" PRIx64 "]"\r
1070 , keyType ? "B" : "A"\r
1071 , sectorNum\r
1072 , key\r
1073 );\r
1074\r
1075 for (uint8_t ii = 0; ii<ATTACK_KEY_COUNT; ii++) {\r
1076 if (key_cnt[ii]==0 || stSector[ii]==sectorNum) {\r
1077 if (keyType==0) {\r
1078 //keyA\r
1079 sector_trailer[ii].keyA = key;\r
1080 stSector[ii] = sectorNum;\r
1081 key_cnt[ii]++;\r
1082 break;\r
1083 } else {\r
1084 //keyB\r
1085 sector_trailer[ii].keyB = key;\r
1086 stSector[ii] = sectorNum;\r
1087 key_cnt[ii]++;\r
1088 break;\r
1089 }\r
1090 }\r
1091 }\r
1092 continue;\r
1093 }\r
1094 }\r
1095 }\r
1096 //set emulator memory for keys\r
1097 if (setEmulatorMem) {\r
1098 for (uint8_t i = 0; i<ATTACK_KEY_COUNT; i++) {\r
1099 if (key_cnt[i]>0) {\r
1100 uint8_t memBlock[16];\r
1101 memset(memBlock, 0x00, sizeof(memBlock));\r
1102 char cmd1[36];\r
1103 memset(cmd1,0x00,sizeof(cmd1));\r
1104 snprintf(cmd1,sizeof(cmd1),"%04x%08xFF078069%04x%08x",(uint32_t) (sector_trailer[i].keyA>>32), (uint32_t) (sector_trailer[i].keyA &0xFFFFFFFF),(uint32_t) (sector_trailer[i].keyB>>32), (uint32_t) (sector_trailer[i].keyB &0xFFFFFFFF));\r
1105 PrintAndLog("Setting Emulator Memory Block %02d: [%s]",stSector[i]*4+3, cmd1);\r
1106 if (param_gethex(cmd1, 0, memBlock, 32)) {\r
1107 PrintAndLog("block data must include 32 HEX symbols");\r
1108 return;\r
1109 }\r
1110 \r
1111 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {(stSector[i]*4+3), 1, 0}};\r
1112 memcpy(c.d.asBytes, memBlock, 16);\r
1113 clearCommandBuffer();\r
1114 SendCommand(&c); \r
1115 }\r
1116 }\r
1117 }\r
1118 /*\r
1119 //un-comment to use as well moebius attack\r
1120 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {\r
1121 if (ar_resp[i].ar2 > 0) {\r
1122 if (tryMfk32_moebius(ar_resp[i], &key)) {\r
1123 PrintAndLog("M-Found Key%s for sector %02d: [%04x%08x]", (ar_resp[i].keytype) ? "B" : "A", ar_resp[i].sector, (uint32_t) (key>>32), (uint32_t) (key &0xFFFFFFFF));\r
1124 }\r
1125 }\r
1126 }*/\r
1127}\r
1128\r
1129int usage_hf14_mf1ksim(void) {\r
1130 PrintAndLog("Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x");\r
1131 PrintAndLog("options:");\r
1132 PrintAndLog(" h this help");\r
1133 PrintAndLog(" u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used");\r
1134 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r
1135 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r
1136 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r
1137 PrintAndLog(" e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)");\r
1138 PrintAndLog(" f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)");\r
1139 PrintAndLog(" r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works.");\r
1140 PrintAndLog("samples:");\r
1141 PrintAndLog(" hf mf sim u 0a0a0a0a");\r
1142 PrintAndLog(" hf mf sim u 11223344556677");\r
1143 PrintAndLog(" hf mf sim u 112233445566778899AA");\r
1144 PrintAndLog(" hf mf sim f uids.txt");\r
1145 PrintAndLog(" hf mf sim u 0a0a0a0a e");\r
1146 \r
1147 return 0;\r
1148}\r
1149\r
1150int CmdHF14AMf1kSim(const char *Cmd) {\r
1151 UsbCommand resp;\r
1152 uint8_t uid[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
1153 uint8_t exitAfterNReads = 0;\r
1154 uint8_t flags = 0;\r
1155 int uidlen = 0;\r
1156 uint8_t pnr = 0;\r
1157 bool setEmulatorMem = false;\r
1158 bool attackFromFile = false;\r
1159 FILE *f;\r
1160 char filename[FILE_PATH_SIZE];\r
1161 memset(filename, 0x00, sizeof(filename));\r
1162 int len = 0;\r
1163 char buf[64];\r
1164\r
1165 uint8_t cmdp = 0;\r
1166 bool errors = false;\r
1167\r
1168 while(param_getchar(Cmd, cmdp) != 0x00) {\r
1169 switch(param_getchar(Cmd, cmdp)) {\r
1170 case 'e':\r
1171 case 'E':\r
1172 setEmulatorMem = true;\r
1173 //implies x and i\r
1174 flags |= FLAG_INTERACTIVE;\r
1175 flags |= FLAG_NR_AR_ATTACK;\r
1176 cmdp++;\r
1177 break;\r
1178 case 'f':\r
1179 case 'F':\r
1180 len = param_getstr(Cmd, cmdp+1, filename);\r
1181 if (len < 1) {\r
1182 PrintAndLog("error no filename found");\r
1183 return 0;\r
1184 }\r
1185 attackFromFile = true;\r
1186 //implies x and i\r
1187 flags |= FLAG_INTERACTIVE;\r
1188 flags |= FLAG_NR_AR_ATTACK;\r
1189 cmdp += 2;\r
1190 break;\r
1191 case 'h':\r
1192 case 'H':\r
1193 return usage_hf14_mf1ksim();\r
1194 case 'i':\r
1195 case 'I':\r
1196 flags |= FLAG_INTERACTIVE;\r
1197 cmdp++;\r
1198 break;\r
1199 case 'n':\r
1200 case 'N':\r
1201 exitAfterNReads = param_get8(Cmd, pnr+1);\r
1202 cmdp += 2;\r
1203 break;\r
1204 case 'r':\r
1205 case 'R':\r
1206 flags |= FLAG_RANDOM_NONCE;\r
1207 cmdp++;\r
1208 break;\r
1209 case 'u':\r
1210 case 'U':\r
1211 param_gethex_ex(Cmd, cmdp+1, uid, &uidlen);\r
1212 switch(uidlen) {\r
1213 case 20: flags = FLAG_10B_UID_IN_DATA; break; //not complete\r
1214 case 14: flags = FLAG_7B_UID_IN_DATA; break;\r
1215 case 8: flags = FLAG_4B_UID_IN_DATA; break;\r
1216 default: return usage_hf14_mf1ksim();\r
1217 }\r
1218 cmdp += 2;\r
1219 break;\r
1220 case 'x':\r
1221 case 'X':\r
1222 flags |= FLAG_NR_AR_ATTACK;\r
1223 cmdp++;\r
1224 break;\r
1225 default:\r
1226 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
1227 errors = true;\r
1228 break;\r
1229 }\r
1230 if(errors) break;\r
1231 }\r
1232 //Validations\r
1233 if(errors) return usage_hf14_mf1ksim();\r
1234\r
1235 //get uid from file\r
1236 if (attackFromFile) {\r
1237 int count = 0;\r
1238 // open file\r
1239 f = fopen(filename, "r");\r
1240 if (f == NULL) {\r
1241 PrintAndLog("File %s not found or locked", filename);\r
1242 return 1;\r
1243 }\r
1244 PrintAndLog("Loading file and simulating. Press keyboard to abort");\r
1245 while(!feof(f) && !ukbhit()){\r
1246 memset(buf, 0, sizeof(buf));\r
1247 memset(uid, 0, sizeof(uid));\r
1248\r
1249 if (fgets(buf, sizeof(buf), f) == NULL) { \r
1250 if (count > 0) break;\r
1251 \r
1252 PrintAndLog("File reading error.");\r
1253 fclose(f);\r
1254 return 2;\r
1255 }\r
1256 if(!strlen(buf) && feof(f)) break;\r
1257\r
1258 uidlen = strlen(buf)-1;\r
1259 switch(uidlen) {\r
1260 case 20: flags |= FLAG_10B_UID_IN_DATA; break; //not complete\r
1261 case 14: flags |= FLAG_7B_UID_IN_DATA; break;\r
1262 case 8: flags |= FLAG_4B_UID_IN_DATA; break;\r
1263 default: \r
1264 PrintAndLog("uid in file wrong length at %d (length: %d) [%s]",count, uidlen, buf);\r
1265 fclose(f);\r
1266 return 2;\r
1267 }\r
1268\r
1269 for (uint8_t i = 0; i < uidlen; i += 2) {\r
1270 sscanf(&buf[i], "%02x", (unsigned int *)&uid[i / 2]);\r
1271 }\r
1272 \r
1273 PrintAndLog("mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort",\r
1274 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
1275 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): \r
1276 flags & FLAG_10B_UID_IN_DATA ? sprint_hex(uid,10): "N/A"\r
1277 , exitAfterNReads, flags, flags);\r
1278\r
1279 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1280 memcpy(c.d.asBytes, uid, sizeof(uid));\r
1281 clearCommandBuffer();\r
1282 SendCommand(&c);\r
1283\r
1284 while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
1285 //We're waiting only 1.5 s at a time, otherwise we get the\r
1286 // annoying message about "Waiting for a response... "\r
1287 }\r
1288 //got a response\r
1289 nonces_t ar_resp[ATTACK_KEY_COUNT*2];\r
1290 memcpy(ar_resp, resp.d.asBytes, sizeof(ar_resp));\r
1291 // We can skip the standard attack if we have RANDOM_NONCE set.\r
1292 readerAttack(ar_resp, setEmulatorMem, !(flags & FLAG_RANDOM_NONCE));\r
1293 if ((bool)resp.arg[1]) {\r
1294 PrintAndLog("Device button pressed - quitting");\r
1295 fclose(f);\r
1296 return 4;\r
1297 }\r
1298 count++;\r
1299 }\r
1300 fclose(f);\r
1301 } else { //not from file\r
1302\r
1303 PrintAndLog("mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) ",\r
1304 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
1305 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): \r
1306 flags & FLAG_10B_UID_IN_DATA ? sprint_hex(uid,10): "N/A"\r
1307 , exitAfterNReads, flags, flags);\r
1308\r
1309 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1310 memcpy(c.d.asBytes, uid, sizeof(uid));\r
1311 clearCommandBuffer();\r
1312 SendCommand(&c);\r
1313\r
1314 if(flags & FLAG_INTERACTIVE) {\r
1315 PrintAndLog("Press pm3-button to abort simulation");\r
1316 while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
1317 //We're waiting only 1.5 s at a time, otherwise we get the\r
1318 // annoying message about "Waiting for a response... "\r
1319 }\r
1320 //got a response\r
1321 if (flags & FLAG_NR_AR_ATTACK) {\r
1322 nonces_t ar_resp[ATTACK_KEY_COUNT*2];\r
1323 memcpy(ar_resp, resp.d.asBytes, sizeof(ar_resp));\r
1324 // We can skip the standard attack if we have RANDOM_NONCE set.\r
1325 readerAttack(ar_resp, setEmulatorMem, !(flags & FLAG_RANDOM_NONCE));\r
1326 }\r
1327 }\r
1328 }\r
1329\r
1330 return 0;\r
1331}\r
1332\r
1333int CmdHF14AMfDbg(const char *Cmd)\r
1334{\r
1335 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
1336 if (dbgMode > 4) {\r
1337 PrintAndLog("Max debug mode parameter is 4 \n");\r
1338 }\r
1339\r
1340 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
1341 PrintAndLog("Usage: hf mf dbg <debug level>");\r
1342 PrintAndLog(" 0 - no debug messages");\r
1343 PrintAndLog(" 1 - error messages");\r
1344 PrintAndLog(" 2 - plus information messages");\r
1345 PrintAndLog(" 3 - plus debug messages");\r
1346 PrintAndLog(" 4 - print even debug messages in timing critical functions");\r
1347 PrintAndLog(" Note: this option therefore may cause malfunction itself");\r
1348 return 0;\r
1349 } \r
1350\r
1351 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
1352 SendCommand(&c);\r
1353\r
1354 return 0;\r
1355}\r
1356\r
1357int CmdHF14AMfEGet(const char *Cmd)\r
1358{\r
1359 uint8_t blockNo = 0;\r
1360 uint8_t data[16] = {0x00};\r
1361\r
1362 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1363 PrintAndLog("Usage: hf mf eget <block number>");\r
1364 PrintAndLog(" sample: hf mf eget 0 ");\r
1365 return 0;\r
1366 } \r
1367 \r
1368 blockNo = param_get8(Cmd, 0);\r
1369\r
1370 PrintAndLog(" ");\r
1371 if (!mfEmlGetMem(data, blockNo, 1)) {\r
1372 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r
1373 } else {\r
1374 PrintAndLog("Command execute timeout");\r
1375 }\r
1376\r
1377 return 0;\r
1378}\r
1379\r
1380int CmdHF14AMfEClear(const char *Cmd)\r
1381{\r
1382 if (param_getchar(Cmd, 0) == 'h') {\r
1383 PrintAndLog("Usage: hf mf eclr");\r
1384 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1385 return 0;\r
1386 } \r
1387\r
1388 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1389 SendCommand(&c);\r
1390 return 0;\r
1391}\r
1392\r
1393\r
1394int CmdHF14AMfESet(const char *Cmd)\r
1395{\r
1396 uint8_t memBlock[16];\r
1397 uint8_t blockNo = 0;\r
1398\r
1399 memset(memBlock, 0x00, sizeof(memBlock));\r
1400\r
1401 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1402 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1403 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1404 return 0;\r
1405 } \r
1406 \r
1407 blockNo = param_get8(Cmd, 0);\r
1408 \r
1409 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1410 PrintAndLog("block data must include 32 HEX symbols");\r
1411 return 1;\r
1412 }\r
1413 \r
1414 // 1 - blocks count\r
1415 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
1416 memcpy(c.d.asBytes, memBlock, 16);\r
1417 SendCommand(&c);\r
1418 return 0;\r
1419}\r
1420\r
1421\r
1422int CmdHF14AMfELoad(const char *Cmd)\r
1423{\r
1424 FILE * f;\r
1425 char filename[FILE_PATH_SIZE];\r
1426 char *fnameptr = filename;\r
1427 char buf[64] = {0x00};\r
1428 uint8_t buf8[64] = {0x00};\r
1429 int i, len, blockNum, numBlocks;\r
1430 int nameParamNo = 1;\r
1431 \r
1432 char ctmp = param_getchar(Cmd, 0);\r
1433 \r
1434 if ( ctmp == 'h' || ctmp == 0x00) {\r
1435 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
1436 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");\r
1437 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1438 PrintAndLog("");\r
1439 PrintAndLog(" sample: hf mf eload filename");\r
1440 PrintAndLog(" hf mf eload 4 filename");\r
1441 return 0;\r
1442 } \r
1443\r
1444 switch (ctmp) {\r
1445 case '0' : numBlocks = 5*4; break;\r
1446 case '1' : \r
1447 case '\0': numBlocks = 16*4; break;\r
1448 case '2' : numBlocks = 32*4; break;\r
1449 case '4' : numBlocks = 256; break;\r
1450 default: {\r
1451 numBlocks = 16*4;\r
1452 nameParamNo = 0;\r
1453 }\r
1454 }\r
1455\r
1456 len = param_getstr(Cmd,nameParamNo,filename);\r
1457 \r
1458 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1459\r
1460 fnameptr += len;\r
1461\r
1462 sprintf(fnameptr, ".eml"); \r
1463 \r
1464 // open file\r
1465 f = fopen(filename, "r");\r
1466 if (f == NULL) {\r
1467 PrintAndLog("File %s not found or locked", filename);\r
1468 return 1;\r
1469 }\r
1470 \r
1471 blockNum = 0;\r
1472 while(!feof(f)){\r
1473 memset(buf, 0, sizeof(buf));\r
1474 \r
1475 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1476 \r
1477 if (blockNum >= numBlocks) break;\r
1478 \r
1479 PrintAndLog("File reading error.");\r
1480 fclose(f);\r
1481 return 2;\r
1482 }\r
1483 \r
1484 if (strlen(buf) < 32){\r
1485 if(strlen(buf) && feof(f))\r
1486 break;\r
1487 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1488 fclose(f);\r
1489 return 2;\r
1490 }\r
1491 \r
1492 for (i = 0; i < 32; i += 2) {\r
1493 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1494 }\r
1495 \r
1496 if (mfEmlSetMem(buf8, blockNum, 1)) {\r
1497 PrintAndLog("Cant set emul block: %3d", blockNum);\r
1498 fclose(f);\r
1499 return 3;\r
1500 }\r
1501 printf(".");\r
1502 blockNum++;\r
1503 \r
1504 if (blockNum >= numBlocks) break;\r
1505 }\r
1506 fclose(f);\r
1507 printf("\n");\r
1508 \r
1509 if ((blockNum != numBlocks)) {\r
1510 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);\r
1511 return 4;\r
1512 }\r
1513 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r
1514 return 0;\r
1515}\r
1516\r
1517\r
1518int CmdHF14AMfESave(const char *Cmd)\r
1519{\r
1520 FILE * f;\r
1521 char filename[FILE_PATH_SIZE];\r
1522 char * fnameptr = filename;\r
1523 uint8_t buf[64];\r
1524 int i, j, len, numBlocks;\r
1525 int nameParamNo = 1;\r
1526 \r
1527 memset(filename, 0, sizeof(filename));\r
1528 memset(buf, 0, sizeof(buf));\r
1529\r
1530 char ctmp = param_getchar(Cmd, 0);\r
1531 \r
1532 if ( ctmp == 'h' || ctmp == 'H') {\r
1533 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
1534 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");\r
1535 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1536 PrintAndLog("");\r
1537 PrintAndLog(" sample: hf mf esave ");\r
1538 PrintAndLog(" hf mf esave 4");\r
1539 PrintAndLog(" hf mf esave 4 filename");\r
1540 return 0;\r
1541 } \r
1542\r
1543 switch (ctmp) {\r
1544 case '0' : numBlocks = 5*4; break;\r
1545 case '1' : \r
1546 case '\0': numBlocks = 16*4; break;\r
1547 case '2' : numBlocks = 32*4; break;\r
1548 case '4' : numBlocks = 256; break;\r
1549 default: {\r
1550 numBlocks = 16*4;\r
1551 nameParamNo = 0;\r
1552 }\r
1553 }\r
1554\r
1555 len = param_getstr(Cmd,nameParamNo,filename);\r
1556 \r
1557 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1558 \r
1559 // user supplied filename?\r
1560 if (len < 1) {\r
1561 // get filename (UID from memory)\r
1562 if (mfEmlGetMem(buf, 0, 1)) {\r
1563 PrintAndLog("Can\'t get UID from block: %d", 0);\r
1564 len = sprintf(fnameptr, "dump");\r
1565 fnameptr += len;\r
1566 }\r
1567 else {\r
1568 for (j = 0; j < 7; j++, fnameptr += 2)\r
1569 sprintf(fnameptr, "%02X", buf[j]);\r
1570 }\r
1571 } else {\r
1572 fnameptr += len;\r
1573 }\r
1574\r
1575 // add file extension\r
1576 sprintf(fnameptr, ".eml"); \r
1577 \r
1578 // open file\r
1579 f = fopen(filename, "w+");\r
1580\r
1581 if ( !f ) {\r
1582 PrintAndLog("Can't open file %s ", filename);\r
1583 return 1;\r
1584 }\r
1585 \r
1586 // put hex\r
1587 for (i = 0; i < numBlocks; i++) {\r
1588 if (mfEmlGetMem(buf, i, 1)) {\r
1589 PrintAndLog("Cant get block: %d", i);\r
1590 break;\r
1591 }\r
1592 for (j = 0; j < 16; j++)\r
1593 fprintf(f, "%02X", buf[j]); \r
1594 fprintf(f,"\n");\r
1595 }\r
1596 fclose(f);\r
1597 \r
1598 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);\r
1599 \r
1600 return 0;\r
1601}\r
1602\r
1603\r
1604int CmdHF14AMfECFill(const char *Cmd)\r
1605{\r
1606 uint8_t keyType = 0;\r
1607 uint8_t numSectors = 16;\r
1608 \r
1609 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1610 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r
1611 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1612 PrintAndLog("");\r
1613 PrintAndLog("samples: hf mf ecfill A");\r
1614 PrintAndLog(" hf mf ecfill A 4");\r
1615 PrintAndLog("Read card and transfer its data to emulator memory.");\r
1616 PrintAndLog("Keys must be laid in the emulator memory. \n");\r
1617 return 0;\r
1618 } \r
1619\r
1620 char ctmp = param_getchar(Cmd, 0);\r
1621 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
1622 PrintAndLog("Key type must be A or B");\r
1623 return 1;\r
1624 }\r
1625 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1626\r
1627 ctmp = param_getchar(Cmd, 1);\r
1628 switch (ctmp) {\r
1629 case '0' : numSectors = 5; break;\r
1630 case '1' : \r
1631 case '\0': numSectors = 16; break;\r
1632 case '2' : numSectors = 32; break;\r
1633 case '4' : numSectors = 40; break;\r
1634 default: numSectors = 16;\r
1635 } \r
1636\r
1637 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r
1638 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r
1639 SendCommand(&c);\r
1640 return 0;\r
1641}\r
1642\r
1643\r
1644int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1645{\r
1646 int i;\r
1647 uint8_t numSectors;\r
1648 uint8_t data[16];\r
1649 uint64_t keyA, keyB;\r
1650 \r
1651 if (param_getchar(Cmd, 0) == 'h') {\r
1652 PrintAndLog("It prints the keys loaded in the emulator memory");\r
1653 PrintAndLog("Usage: hf mf ekeyprn [card memory]");\r
1654 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1655 PrintAndLog("");\r
1656 PrintAndLog(" sample: hf mf ekeyprn 1");\r
1657 return 0;\r
1658 } \r
1659\r
1660 char cmdp = param_getchar(Cmd, 0);\r
1661 \r
1662 switch (cmdp) {\r
1663 case '0' : numSectors = 5; break;\r
1664 case '1' : \r
1665 case '\0': numSectors = 16; break;\r
1666 case '2' : numSectors = 32; break;\r
1667 case '4' : numSectors = 40; break;\r
1668 default: numSectors = 16;\r
1669 } \r
1670 \r
1671 PrintAndLog("|---|----------------|----------------|");\r
1672 PrintAndLog("|sec|key A |key B |");\r
1673 PrintAndLog("|---|----------------|----------------|");\r
1674 for (i = 0; i < numSectors; i++) {\r
1675 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r
1676 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
1677 break;\r
1678 }\r
1679 keyA = bytes_to_num(data, 6);\r
1680 keyB = bytes_to_num(data + 10, 6);\r
1681 PrintAndLog("|%03d| %012" PRIx64 " | %012" PRIx64 " |", i, keyA, keyB);\r
1682 }\r
1683 PrintAndLog("|---|----------------|----------------|");\r
1684 \r
1685 return 0;\r
1686}\r
1687\r
1688\r
1689int CmdHF14AMfCSetUID(const char *Cmd)\r
1690{\r
1691 uint8_t wipeCard = 0;\r
1692 uint8_t uid[8] = {0x00};\r
1693 uint8_t oldUid[8] = {0x00};\r
1694 uint8_t atqa[2] = {0x00};\r
1695 uint8_t sak[1] = {0x00};\r
1696 uint8_t atqaPresent = 1;\r
1697 int res;\r
1698 char ctmp;\r
1699 int argi=0;\r
1700\r
1701 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {\r
1702 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");\r
1703 PrintAndLog("sample: hf mf csetuid 01020304");\r
1704 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");\r
1705 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");\r
1706 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");\r
1707 return 0;\r
1708 }\r
1709\r
1710 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {\r
1711 PrintAndLog("UID must include 8 HEX symbols");\r
1712 return 1;\r
1713 }\r
1714 argi++;\r
1715\r
1716 ctmp = param_getchar(Cmd, argi);\r
1717 if (ctmp == 'w' || ctmp == 'W') {\r
1718 wipeCard = 1;\r
1719 atqaPresent = 0;\r
1720 }\r
1721\r
1722 if (atqaPresent) {\r
1723 if (param_getchar(Cmd, argi)) {\r
1724 if (param_gethex(Cmd, argi, atqa, 4)) {\r
1725 PrintAndLog("ATQA must include 4 HEX symbols");\r
1726 return 1;\r
1727 }\r
1728 argi++;\r
1729 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {\r
1730 PrintAndLog("SAK must include 2 HEX symbols");\r
1731 return 1;\r
1732 }\r
1733 argi++;\r
1734 } else\r
1735 atqaPresent = 0;\r
1736 }\r
1737\r
1738 if(!wipeCard) {\r
1739 ctmp = param_getchar(Cmd, argi);\r
1740 if (ctmp == 'w' || ctmp == 'W') {\r
1741 wipeCard = 1;\r
1742 }\r
1743 }\r
1744\r
1745 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));\r
1746\r
1747 res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);\r
1748 if (res) {\r
1749 PrintAndLog("Can't set UID. error=%d", res);\r
1750 return 1;\r
1751 }\r
1752 \r
1753 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
1754 PrintAndLog("new UID:%s", sprint_hex(uid, 4));\r
1755 return 0;\r
1756}\r
1757\r
1758int CmdHF14AMfCSetBlk(const char *Cmd)\r
1759{\r
1760 uint8_t memBlock[16] = {0x00};\r
1761 uint8_t blockNo = 0;\r
1762 bool wipeCard = FALSE;\r
1763 int res;\r
1764\r
1765 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1766 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");\r
1767 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
1768 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");\r
1769 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");\r
1770 return 0;\r
1771 } \r
1772\r
1773 blockNo = param_get8(Cmd, 0);\r
1774\r
1775 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1776 PrintAndLog("block data must include 32 HEX symbols");\r
1777 return 1;\r
1778 }\r
1779\r
1780 char ctmp = param_getchar(Cmd, 2);\r
1781 wipeCard = (ctmp == 'w' || ctmp == 'W');\r
1782 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\r
1783\r
1784 res = mfCSetBlock(blockNo, memBlock, NULL, wipeCard, CSETBLOCK_SINGLE_OPER);\r
1785 if (res) {\r
1786 PrintAndLog("Can't write block. error=%d", res);\r
1787 return 1;\r
1788 }\r
1789 return 0;\r
1790}\r
1791\r
1792\r
1793int CmdHF14AMfCLoad(const char *Cmd)\r
1794{\r
1795 FILE * f;\r
1796 char filename[FILE_PATH_SIZE] = {0x00};\r
1797 char * fnameptr = filename;\r
1798 char buf[64] = {0x00};\r
1799 uint8_t buf8[64] = {0x00};\r
1800 uint8_t fillFromEmulator = 0;\r
1801 int i, len, blockNum, flags=0;\r
1802 \r
1803 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
1804 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");\r
1805 PrintAndLog("or from emulator memory (option `e`)");\r
1806 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1807 PrintAndLog(" or: hf mf cload e ");\r
1808 PrintAndLog(" sample: hf mf cload filename");\r
1809 return 0;\r
1810 } \r
1811\r
1812 char ctmp = param_getchar(Cmd, 0);\r
1813 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1814 \r
1815 if (fillFromEmulator) {\r
1816 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1817 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1818 PrintAndLog("Cant get block: %d", blockNum);\r
1819 return 2;\r
1820 }\r
1821 if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence\r
1822 if (blockNum == 1) flags = 0; // just write\r
1823 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Magic Halt and switch off field.\r
1824\r
1825 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1826 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1827 return 3;\r
1828 }\r
1829 }\r
1830 return 0;\r
1831 } else {\r
1832 len = strlen(Cmd);\r
1833 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
1834\r
1835 memcpy(filename, Cmd, len);\r
1836 fnameptr += len;\r
1837\r
1838 sprintf(fnameptr, ".eml"); \r
1839 \r
1840 // open file\r
1841 f = fopen(filename, "r");\r
1842 if (f == NULL) {\r
1843 PrintAndLog("File not found or locked.");\r
1844 return 1;\r
1845 }\r
1846 \r
1847 blockNum = 0;\r
1848 while(!feof(f)){\r
1849 \r
1850 memset(buf, 0, sizeof(buf));\r
1851 \r
1852 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1853 fclose(f);\r
1854 PrintAndLog("File reading error.");\r
1855 return 2;\r
1856 }\r
1857\r
1858 if (strlen(buf) < 32) {\r
1859 if(strlen(buf) && feof(f))\r
1860 break;\r
1861 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1862 fclose(f);\r
1863 return 2;\r
1864 }\r
1865 for (i = 0; i < 32; i += 2)\r
1866 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1867\r
1868 if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence\r
1869 if (blockNum == 1) flags = 0; // just write\r
1870 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Switch off field.\r
1871\r
1872 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1873 PrintAndLog("Can't set magic card block: %d", blockNum);\r
1874 fclose(f);\r
1875 return 3;\r
1876 }\r
1877 blockNum++;\r
1878 \r
1879 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1880 }\r
1881 fclose(f);\r
1882 \r
1883 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
1884 PrintAndLog("File content error. There must be 64 blocks");\r
1885 return 4;\r
1886 }\r
1887 PrintAndLog("Loaded from file: %s", filename);\r
1888 return 0;\r
1889 }\r
1890 return 0;\r
1891}\r
1892\r
1893int CmdHF14AMfCGetBlk(const char *Cmd) {\r
1894 uint8_t memBlock[16];\r
1895 uint8_t blockNo = 0;\r
1896 int res;\r
1897 memset(memBlock, 0x00, sizeof(memBlock));\r
1898\r
1899 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1900 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1901 PrintAndLog("sample: hf mf cgetblk 1");\r
1902 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");\r
1903 return 0;\r
1904 } \r
1905\r
1906 blockNo = param_get8(Cmd, 0);\r
1907\r
1908 PrintAndLog("--block number:%2d ", blockNo);\r
1909\r
1910 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r
1911 if (res) {\r
1912 PrintAndLog("Can't read block. error=%d", res);\r
1913 return 1;\r
1914 }\r
1915 \r
1916 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r
1917 return 0;\r
1918}\r
1919\r
1920\r
1921int CmdHF14AMfCGetSc(const char *Cmd) {\r
1922 uint8_t memBlock[16] = {0x00};\r
1923 uint8_t sectorNo = 0;\r
1924 int i, res, flags;\r
1925\r
1926 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1927 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1928 PrintAndLog("sample: hf mf cgetsc 0");\r
1929 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");\r
1930 return 0;\r
1931 } \r
1932\r
1933 sectorNo = param_get8(Cmd, 0);\r
1934 if (sectorNo > 15) {\r
1935 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1936 return 1;\r
1937 }\r
1938\r
1939 PrintAndLog("--sector number:%d ", sectorNo);\r
1940\r
1941 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1942 for (i = 0; i < 4; i++) {\r
1943 if (i == 1) flags = 0;\r
1944 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1945\r
1946 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r
1947 if (res) {\r
1948 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);\r
1949 return 1;\r
1950 }\r
1951 \r
1952 PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r
1953 }\r
1954 return 0;\r
1955}\r
1956\r
1957\r
1958int CmdHF14AMfCSave(const char *Cmd) {\r
1959\r
1960 FILE * f;\r
1961 char filename[FILE_PATH_SIZE] = {0x00};\r
1962 char * fnameptr = filename;\r
1963 uint8_t fillFromEmulator = 0;\r
1964 uint8_t buf[64] = {0x00};\r
1965 int i, j, len, flags;\r
1966 \r
1967 // memset(filename, 0, sizeof(filename));\r
1968 // memset(buf, 0, sizeof(buf));\r
1969\r
1970 if (param_getchar(Cmd, 0) == 'h') {\r
1971 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1972 PrintAndLog("or into emulator memory (option `e`)");\r
1973 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1974 PrintAndLog(" sample: hf mf esave ");\r
1975 PrintAndLog(" hf mf esave filename");\r
1976 PrintAndLog(" hf mf esave e \n");\r
1977 return 0;\r
1978 } \r
1979\r
1980 char ctmp = param_getchar(Cmd, 0);\r
1981 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1982\r
1983 if (fillFromEmulator) {\r
1984 // put into emulator\r
1985 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1986 for (i = 0; i < 16 * 4; i++) {\r
1987 if (i == 1) flags = 0;\r
1988 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1989 \r
1990 if (mfCGetBlock(i, buf, flags)) {\r
1991 PrintAndLog("Cant get block: %d", i);\r
1992 break;\r
1993 }\r
1994 \r
1995 if (mfEmlSetMem(buf, i, 1)) {\r
1996 PrintAndLog("Cant set emul block: %d", i);\r
1997 return 3;\r
1998 }\r
1999 }\r
2000 return 0;\r
2001 } else {\r
2002 len = strlen(Cmd);\r
2003 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
2004 \r
2005 if (len < 1) {\r
2006 // get filename\r
2007 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
2008 PrintAndLog("Cant get block: %d", 0);\r
2009 len = sprintf(fnameptr, "dump");\r
2010 fnameptr += len;\r
2011 }\r
2012 else {\r
2013 for (j = 0; j < 7; j++, fnameptr += 2)\r
2014 sprintf(fnameptr, "%02x", buf[j]); \r
2015 }\r
2016 } else {\r
2017 memcpy(filename, Cmd, len);\r
2018 fnameptr += len;\r
2019 }\r
2020\r
2021 sprintf(fnameptr, ".eml"); \r
2022 \r
2023 // open file\r
2024 f = fopen(filename, "w+");\r
2025\r
2026 if (f == NULL) {\r
2027 PrintAndLog("File not found or locked.");\r
2028 return 1;\r
2029 }\r
2030\r
2031 // put hex\r
2032 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
2033 for (i = 0; i < 16 * 4; i++) {\r
2034 if (i == 1) flags = 0;\r
2035 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
2036 \r
2037 if (mfCGetBlock(i, buf, flags)) {\r
2038 PrintAndLog("Cant get block: %d", i);\r
2039 break;\r
2040 }\r
2041 for (j = 0; j < 16; j++)\r
2042 fprintf(f, "%02x", buf[j]); \r
2043 fprintf(f,"\n");\r
2044 }\r
2045 fclose(f);\r
2046 \r
2047 PrintAndLog("Saved to file: %s", filename);\r
2048 \r
2049 return 0;\r
2050 }\r
2051}\r
2052\r
2053\r
2054int CmdHF14AMfSniff(const char *Cmd){\r
2055\r
2056 bool wantLogToFile = 0;\r
2057 bool wantDecrypt = 0;\r
2058 //bool wantSaveToEml = 0; TODO\r
2059 bool wantSaveToEmlFile = 0;\r
2060\r
2061 //var \r
2062 int res = 0;\r
2063 int len = 0;\r
2064 int blockLen = 0;\r
2065 int pckNum = 0;\r
2066 int num = 0;\r
2067 uint8_t uid[7];\r
2068 uint8_t uid_len;\r
2069 uint8_t atqa[2] = {0x00};\r
2070 uint8_t sak;\r
2071 bool isTag;\r
2072 uint8_t *buf = NULL;\r
2073 uint16_t bufsize = 0;\r
2074 uint8_t *bufPtr = NULL;\r
2075 \r
2076 char ctmp = param_getchar(Cmd, 0);\r
2077 if ( ctmp == 'h' || ctmp == 'H' ) {\r
2078 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r
2079 PrintAndLog("You can specify:");\r
2080 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
2081 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
2082 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
2083 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r
2084 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");\r
2085 PrintAndLog(" sample: hf mf sniff l d e");\r
2086 return 0;\r
2087 } \r
2088 \r
2089 for (int i = 0; i < 4; i++) {\r
2090 ctmp = param_getchar(Cmd, i);\r
2091 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
2092 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
2093 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
2094 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
2095 }\r
2096 \r
2097 printf("-------------------------------------------------------------------------\n");\r
2098 printf("Executing command. \n");\r
2099 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
2100 printf("Press the key on pc keyboard to abort the client.\n");\r
2101 printf("-------------------------------------------------------------------------\n");\r
2102\r
2103 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
2104 clearCommandBuffer();\r
2105 SendCommand(&c);\r
2106\r
2107 // wait cycle\r
2108 while (true) {\r
2109 printf(".");\r
2110 fflush(stdout);\r
2111 if (ukbhit()) {\r
2112 getchar();\r
2113 printf("\naborted via keyboard!\n");\r
2114 break;\r
2115 }\r
2116 \r
2117 UsbCommand resp;\r
2118 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
2119 res = resp.arg[0] & 0xff;\r
2120 uint16_t traceLen = resp.arg[1];\r
2121 len = resp.arg[2];\r
2122\r
2123 if (res == 0) { // we are done\r
2124 free(buf);\r
2125 return 0;\r
2126 }\r
2127\r
2128 if (res == 1) { // there is (more) data to be transferred\r
2129 if (pckNum == 0) { // first packet, (re)allocate necessary buffer\r
2130 if (traceLen > bufsize || buf == NULL) {\r
2131 uint8_t *p;\r
2132 if (buf == NULL) { // not yet allocated\r
2133 p = malloc(traceLen);\r
2134 } else { // need more memory\r
2135 p = realloc(buf, traceLen);\r
2136 }\r
2137 if (p == NULL) {\r
2138 PrintAndLog("Cannot allocate memory for trace");\r
2139 free(buf);\r
2140 return 2;\r
2141 }\r
2142 buf = p;\r
2143 }\r
2144 bufPtr = buf;\r
2145 bufsize = traceLen;\r
2146 memset(buf, 0x00, traceLen);\r
2147 }\r
2148 memcpy(bufPtr, resp.d.asBytes, len);\r
2149 bufPtr += len;\r
2150 pckNum++;\r
2151 }\r
2152\r
2153 if (res == 2) { // received all data, start displaying\r
2154 blockLen = bufPtr - buf;\r
2155 bufPtr = buf;\r
2156 printf(">\n");\r
2157 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
2158 while (bufPtr - buf < blockLen) {\r
2159 bufPtr += 6; // skip (void) timing information\r
2160 len = *((uint16_t *)bufPtr);\r
2161 if(len & 0x8000) {\r
2162 isTag = true;\r
2163 len &= 0x7fff;\r
2164 } else {\r
2165 isTag = false;\r
2166 }\r
2167 bufPtr += 2;\r
2168 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
2169 memcpy(uid, bufPtr + 2, 7);\r
2170 memcpy(atqa, bufPtr + 2 + 7, 2);\r
2171 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
2172 sak = bufPtr[11];\r
2173 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
2174 sprint_hex(uid + (7 - uid_len), uid_len),\r
2175 atqa[1], \r
2176 atqa[0], \r
2177 sak);\r
2178 if (wantLogToFile || wantDecrypt) {\r
2179 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r
2180 AddLogCurrentDT(logHexFileName);\r
2181 } \r
2182 if (wantDecrypt) \r
2183 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
2184 } else {\r
2185 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
2186 if (wantLogToFile) \r
2187 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
2188 if (wantDecrypt) \r
2189 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
2190 num++; \r
2191 }\r
2192 bufPtr += len;\r
2193 bufPtr += ((len-1)/8+1); // ignore parity\r
2194 }\r
2195 pckNum = 0;\r
2196 }\r
2197 } // resp not NULL\r
2198 } // while (true)\r
2199\r
2200 free(buf);\r
2201 return 0;\r
2202}\r
2203\r
2204//needs nt, ar, at, Data to decrypt\r
2205int CmdDecryptTraceCmds(const char *Cmd){\r
2206 uint8_t data[50];\r
2207 int len = 0;\r
2208 param_gethex_ex(Cmd,3,data,&len);\r
2209 return tryDecryptWord(param_get32ex(Cmd,0,0,16),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16),data,len/2);\r
2210}\r
2211\r
2212static command_t CommandTable[] =\r
2213{\r
2214 {"help", CmdHelp, 1, "This help"},\r
2215 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
2216 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
2217 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
2218 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
2219 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
2220 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
2221 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
2222 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
2223 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
2224 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
2225 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
2226 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
2227 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
2228 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
2229 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
2230 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
2231 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
2232 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
2233 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
2234 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},\r
2235 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},\r
2236 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},\r
2237 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
2238 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
2239 {"decrypt", CmdDecryptTraceCmds,1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
2240 {NULL, NULL, 0, NULL}\r
2241};\r
2242\r
2243int CmdHFMF(const char *Cmd)\r
2244{\r
2245 // flush\r
2246 WaitForResponseTimeout(CMD_ACK,NULL,100);\r
2247\r
2248 CmdsParse(CommandTable, Cmd);\r
2249 return 0;\r
2250}\r
2251\r
2252int CmdHelp(const char *Cmd)\r
2253{\r
2254 CmdsHelp(CommandTable);\r
2255 return 0;\r
2256}\r
Impressum, Datenschutz