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