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