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