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