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