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