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