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