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