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