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