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