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