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