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