fix bug in moebius nonce collection - now finishes
[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
c872d8c1 1019int usage_hf14_mf1ksim(void){\r
1020 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14 hex symbols)> n <numreads> i x");\r
1021 PrintAndLog("options:");\r
1022 PrintAndLog(" h this help");\r
1023 PrintAndLog(" u (Optional) UID 4,7 bytes. If not specified, the UID 4b from emulator memory will be used");\r
1024 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r
1025 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r
1026 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r
1027 PrintAndLog(" e (Optional) set keys found from 'reader attack' to emulator memory");\r
1028 PrintAndLog("samples:");\r
1029 PrintAndLog(" hf mf sim u 0a0a0a0a");\r
1030 PrintAndLog(" hf mf sim u 11223344556677");\r
1031 //PrintAndLog(" hf mf sim u 112233445566778899AA"); \r
1032 return 0;\r
1033}\r
1034\r
9ca155ba
M
1035int CmdHF14AMf1kSim(const char *Cmd)\r
1036{\r
c872d8c1 1037 #define ATTACK_KEY_COUNT 8\r
1038 uint8_t uid[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
d2f487af 1039 uint8_t exitAfterNReads = 0;\r
1040 uint8_t flags = 0;\r
c872d8c1 1041 int uidlen = 0;\r
1042 uint8_t pnr = 0;\r
1043 bool setEmulatorMem = false;\r
d2f487af 1044\r
c872d8c1 1045 char cmdp = param_getchar(Cmd, pnr);\r
e3c23565 1046 \r
c872d8c1 1047 if (cmdp == 'h' || cmdp == 'H') return usage_hf14_mf1ksim();\r
1048\r
1049 if (cmdp == 'u' || cmdp == 'U') {\r
1050 param_gethex_ex(Cmd, pnr+1, uid, &uidlen);\r
1051 switch(uidlen){\r
1052 //case 20: flags = FLAG_10B_UID_IN_DATA; break; //not complete\r
1053 case 14: flags = FLAG_7B_UID_IN_DATA; break;\r
1054 case 8: flags = FLAG_4B_UID_IN_DATA; break;\r
1055 default: return usage_hf14_mf1ksim();\r
d2f487af 1056 }\r
1057 pnr +=2;\r
1058 }\r
c872d8c1 1059\r
1060 cmdp = param_getchar(Cmd, pnr);\r
1061 if (cmdp == 'n' || cmdp == 'N') {\r
1062 exitAfterNReads = param_get8(Cmd, pnr+1);\r
d2f487af 1063 pnr += 2;\r
1064 }\r
c872d8c1 1065\r
1066 cmdp = param_getchar(Cmd, pnr);\r
1067 if (cmdp == 'i' || cmdp == 'I' ) {\r
d2f487af 1068 flags |= FLAG_INTERACTIVE;\r
1069 pnr++;\r
1070 }\r
1071\r
c872d8c1 1072 cmdp = param_getchar(Cmd, pnr);\r
1073 if (cmdp == 'x' || cmdp == 'X') {\r
d2f487af 1074 flags |= FLAG_NR_AR_ATTACK;\r
6eae192c 1075 pnr++;\r
d2f487af 1076 }\r
c872d8c1 1077\r
1078 cmdp = param_getchar(Cmd, pnr);\r
1079 if (cmdp == 'e' || cmdp == 'E') {\r
1080 setEmulatorMem = true;\r
1081 }\r
1082\r
d2f487af 1083 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r
1084 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
c872d8c1 1085 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r
d2f487af 1086 , exitAfterNReads, flags,flags);\r
1087\r
1088\r
baeaf579 1089 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1090 memcpy(c.d.asBytes, uid, sizeof(uid));\r
c872d8c1 1091 clearCommandBuffer();\r
baeaf579 1092 SendCommand(&c);\r
d2f487af 1093\r
c872d8c1 1094 if(flags & FLAG_INTERACTIVE) {\r
baeaf579 1095 UsbCommand resp;\r
1096 PrintAndLog("Press pm3-button to abort simulation");\r
1097 while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
1098 //We're waiting only 1.5 s at a time, otherwise we get the\r
1099 // annoying message about "Waiting for a response... "\r
1100 }\r
79dcb9e0 1101 //got a response\r
1102 if (flags & FLAG_NR_AR_ATTACK) {\r
c872d8c1 1103 nonces_t ar_resp[ATTACK_KEY_COUNT*2];\r
79dcb9e0 1104 uint64_t key = 0;\r
79dcb9e0 1105 memcpy (ar_resp, resp.d.asBytes, sizeof(ar_resp));\r
c872d8c1 1106 typedef struct {\r
1107 uint64_t keyA;\r
1108 uint32_t security;\r
1109 uint64_t keyB;\r
1110 } st_t;\r
1111 st_t sector_trailer[ATTACK_KEY_COUNT];\r
1112 memset(sector_trailer, 0x00, sizeof(sector_trailer));\r
1113\r
1114 uint8_t stSector[ATTACK_KEY_COUNT];\r
1115 memset(stSector, 0x00, sizeof(stSector));\r
1116 uint8_t key_cnt[ATTACK_KEY_COUNT];\r
1117 memset(key_cnt, 0x00, sizeof(key_cnt));\r
1118\r
1119 for (uint8_t i = 0; i<ATTACK_KEY_COUNT; i++) {\r
79dcb9e0 1120 if (ar_resp[i].ar2 > 0) {\r
c872d8c1 1121 //PrintAndLog("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
1122 if (mfkey32(ar_resp[i], &key)) {\r
6eae192c 1123 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
c872d8c1 1124\r
1125 for (uint8_t ii = 0; ii<ATTACK_KEY_COUNT; ii++) {\r
1126 if (key_cnt[ii]==0 || stSector[ii]==ar_resp[i].sector) {\r
1127 if (ar_resp[i].keytype==0) {\r
1128 //keyA\r
1129 sector_trailer[ii].keyA = key;\r
1130 stSector[ii] = ar_resp[i].sector;\r
1131 key_cnt[ii]++;\r
1132 break;\r
1133 } else {\r
1134 //keyB\r
1135 sector_trailer[ii].keyB = key;\r
1136 stSector[ii] = ar_resp[i].sector;\r
1137 key_cnt[ii]++;\r
1138 break;\r
1139 }\r
1140 }\r
1141 }\r
1142 }\r
79dcb9e0 1143 }\r
1144 }\r
c872d8c1 1145 //set emulator memory for keys\r
1146 if (setEmulatorMem) {\r
1147 for (uint8_t i = 0; i<ATTACK_KEY_COUNT; i++) {\r
1148 if (key_cnt[i]>0) {\r
1149 //PrintAndLog ("block %d, keyA:%04x%08x, keyb:%04x%08x",stSector[i]*4+3, (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
1150 uint8_t memBlock[16];\r
1151 memset(memBlock, 0x00, sizeof(memBlock));\r
1152 char cmd1[36];\r
1153 memset(cmd1,0x00,sizeof(cmd1));\r
1154 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
6eae192c 1155 PrintAndLog("Setting Emulator Memory Block %02d: [%s]",stSector[i]*4+3, cmd1);\r
c872d8c1 1156 if (param_gethex(cmd1, 0, memBlock, 32)) {\r
1157 PrintAndLog("block data must include 32 HEX symbols");\r
1158 return 1;\r
1159 }\r
1160 \r
1161 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {(stSector[i]*4+3), 1, 0}};\r
1162 memcpy(c.d.asBytes, memBlock, 16);\r
1163 clearCommandBuffer();\r
1164 SendCommand(&c); \r
79dcb9e0 1165 }\r
1166 }\r
79dcb9e0 1167 }\r
c872d8c1 1168 //moebius attack\r
1169 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {\r
1170 if (ar_resp[i].ar2 > 0) {\r
1171 if (tryMfk32_moebius(ar_resp[i], &key)) {\r
6eae192c 1172 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
c872d8c1 1173 }\r
79dcb9e0 1174 }\r
1175 }\r
79dcb9e0 1176 }\r
baeaf579 1177 }\r
1178 \r
1179 return 0;\r
9ca155ba
M
1180}\r
1181\r
1182int CmdHF14AMfDbg(const char *Cmd)\r
1183{\r
8556b852
M
1184 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
1185 if (dbgMode > 4) {\r
baeaf579 1186 PrintAndLog("Max debug mode parameter is 4 \n");\r
8556b852
M
1187 }\r
1188\r
1189 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
9ca155ba
M
1190 PrintAndLog("Usage: hf mf dbg <debug level>");\r
1191 PrintAndLog(" 0 - no debug messages");\r
1192 PrintAndLog(" 1 - error messages");\r
b03c0f2d 1193 PrintAndLog(" 2 - plus information messages");\r
1194 PrintAndLog(" 3 - plus debug messages");\r
1195 PrintAndLog(" 4 - print even debug messages in timing critical functions");\r
1196 PrintAndLog(" Note: this option therefore may cause malfunction itself");\r
9ca155ba
M
1197 return 0;\r
1198 } \r
1199\r
8556b852
M
1200 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
1201 SendCommand(&c);\r
1202\r
9ca155ba
M
1203 return 0;\r
1204}\r
1205\r
1206int CmdHF14AMfEGet(const char *Cmd)\r
1207{\r
8556b852 1208 uint8_t blockNo = 0;\r
5ee70129 1209 uint8_t data[16] = {0x00};\r
8556b852
M
1210\r
1211 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1212 PrintAndLog("Usage: hf mf eget <block number>");\r
1213 PrintAndLog(" sample: hf mf eget 0 ");\r
1214 return 0;\r
1215 } \r
1216 \r
1217 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1218\r
1219 PrintAndLog(" ");\r
baeaf579 1220 if (!mfEmlGetMem(data, blockNo, 1)) {\r
1221 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r
8556b852
M
1222 } else {\r
1223 PrintAndLog("Command execute timeout");\r
1224 }\r
1225\r
1226 return 0;\r
1227}\r
1228\r
1229int CmdHF14AMfEClear(const char *Cmd)\r
1230{\r
1231 if (param_getchar(Cmd, 0) == 'h') {\r
1232 PrintAndLog("Usage: hf mf eclr");\r
1233 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1234 return 0;\r
1235 } \r
1236\r
1237 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1238 SendCommand(&c);\r
9ca155ba
M
1239 return 0;\r
1240}\r
1241\r
baeaf579 1242\r
9ca155ba
M
1243int CmdHF14AMfESet(const char *Cmd)\r
1244{\r
8556b852
M
1245 uint8_t memBlock[16];\r
1246 uint8_t blockNo = 0;\r
1247\r
1248 memset(memBlock, 0x00, sizeof(memBlock));\r
1249\r
1250 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1251 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1252 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1253 return 0;\r
1254 } \r
1255 \r
1256 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1257 \r
1258 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1259 PrintAndLog("block data must include 32 HEX symbols");\r
1260 return 1;\r
1261 }\r
1262 \r
1263 // 1 - blocks count\r
baeaf579 1264 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
8556b852 1265 memcpy(c.d.asBytes, memBlock, 16);\r
baeaf579 1266 SendCommand(&c);\r
1267 return 0;\r
9ca155ba
M
1268}\r
1269\r
baeaf579 1270\r
9ca155ba
M
1271int CmdHF14AMfELoad(const char *Cmd)\r
1272{\r
ab8b654e 1273 FILE * f;\r
b915fda3 1274 char filename[FILE_PATH_SIZE];\r
baeaf579 1275 char *fnameptr = filename;\r
5ee70129 1276 char buf[64] = {0x00};\r
1277 uint8_t buf8[64] = {0x00};\r
b915fda3 1278 int i, len, blockNum, numBlocks;\r
1279 int nameParamNo = 1;\r
ab8b654e 1280 \r
b915fda3 1281 char ctmp = param_getchar(Cmd, 0);\r
1282 \r
1283 if ( ctmp == 'h' || ctmp == 0x00) {\r
ab8b654e 1284 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
b915fda3 1285 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");\r
1286 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1287 PrintAndLog("");\r
ab8b654e 1288 PrintAndLog(" sample: hf mf eload filename");\r
b915fda3 1289 PrintAndLog(" hf mf eload 4 filename");\r
ab8b654e
M
1290 return 0;\r
1291 } \r
1292\r
b915fda3 1293 switch (ctmp) {\r
1294 case '0' : numBlocks = 5*4; break;\r
1295 case '1' : \r
1296 case '\0': numBlocks = 16*4; break;\r
1297 case '2' : numBlocks = 32*4; break;\r
1298 case '4' : numBlocks = 256; break;\r
1299 default: {\r
1300 numBlocks = 16*4;\r
1301 nameParamNo = 0;\r
1302 }\r
1303 }\r
1304\r
1305 len = param_getstr(Cmd,nameParamNo,filename);\r
1306 \r
0b14440d 1307 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
ab8b654e 1308\r
0b14440d 1309 fnameptr += len;\r
ab8b654e
M
1310\r
1311 sprintf(fnameptr, ".eml"); \r
1312 \r
1313 // open file\r
1314 f = fopen(filename, "r");\r
1315 if (f == NULL) {\r
3fe4ff4f 1316 PrintAndLog("File %s not found or locked", filename);\r
ab8b654e
M
1317 return 1;\r
1318 }\r
1319 \r
1320 blockNum = 0;\r
1321 while(!feof(f)){\r
1322 memset(buf, 0, sizeof(buf));\r
b915fda3 1323 \r
759c16b3 1324 if (fgets(buf, sizeof(buf), f) == NULL) {\r
b915fda3 1325 \r
1326 if (blockNum >= numBlocks) break;\r
1327 \r
d2f487af 1328 PrintAndLog("File reading error.");\r
97d582a6 1329 fclose(f);\r
759c16b3 1330 return 2;\r
baeaf579 1331 }\r
b915fda3 1332 \r
ab8b654e 1333 if (strlen(buf) < 32){\r
aea4d766 1334 if(strlen(buf) && feof(f))\r
1335 break;\r
ab8b654e 1336 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
97d582a6 1337 fclose(f);\r
ab8b654e
M
1338 return 2;\r
1339 }\r
b915fda3 1340 \r
baeaf579 1341 for (i = 0; i < 32; i += 2) {\r
1342 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
baeaf579 1343 }\r
3fe4ff4f 1344 \r
ab8b654e 1345 if (mfEmlSetMem(buf8, blockNum, 1)) {\r
baeaf579 1346 PrintAndLog("Cant set emul block: %3d", blockNum);\r
97d582a6 1347 fclose(f);\r
ab8b654e
M
1348 return 3;\r
1349 }\r
5ee70129 1350 printf(".");\r
ab8b654e
M
1351 blockNum++;\r
1352 \r
b915fda3 1353 if (blockNum >= numBlocks) break;\r
ab8b654e
M
1354 }\r
1355 fclose(f);\r
5ee70129 1356 printf("\n");\r
ab8b654e 1357 \r
b915fda3 1358 if ((blockNum != numBlocks)) {\r
1359 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);\r
ab8b654e
M
1360 return 4;\r
1361 }\r
d2f487af 1362 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r
baeaf579 1363 return 0;\r
9ca155ba
M
1364}\r
1365\r
baeaf579 1366\r
9ca155ba
M
1367int CmdHF14AMfESave(const char *Cmd)\r
1368{\r
ab8b654e 1369 FILE * f;\r
b915fda3 1370 char filename[FILE_PATH_SIZE];\r
ab8b654e
M
1371 char * fnameptr = filename;\r
1372 uint8_t buf[64];\r
b915fda3 1373 int i, j, len, numBlocks;\r
1374 int nameParamNo = 1;\r
ab8b654e
M
1375 \r
1376 memset(filename, 0, sizeof(filename));\r
1377 memset(buf, 0, sizeof(buf));\r
1378\r
b915fda3 1379 char ctmp = param_getchar(Cmd, 0);\r
1380 \r
1381 if ( ctmp == 'h' || ctmp == 'H') {\r
ab8b654e 1382 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
b915fda3 1383 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");\r
1384 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1385 PrintAndLog("");\r
ab8b654e 1386 PrintAndLog(" sample: hf mf esave ");\r
b915fda3 1387 PrintAndLog(" hf mf esave 4");\r
1388 PrintAndLog(" hf mf esave 4 filename");\r
ab8b654e
M
1389 return 0;\r
1390 } \r
1391\r
b915fda3 1392 switch (ctmp) {\r
1393 case '0' : numBlocks = 5*4; break;\r
1394 case '1' : \r
1395 case '\0': numBlocks = 16*4; break;\r
1396 case '2' : numBlocks = 32*4; break;\r
1397 case '4' : numBlocks = 256; break;\r
1398 default: {\r
1399 numBlocks = 16*4;\r
1400 nameParamNo = 0;\r
1401 }\r
1402 }\r
1403\r
1404 len = param_getstr(Cmd,nameParamNo,filename);\r
1405 \r
0b14440d 1406 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
ab8b654e 1407 \r
b915fda3 1408 // user supplied filename?\r
ab8b654e 1409 if (len < 1) {\r
b915fda3 1410 // get filename (UID from memory)\r
ab8b654e 1411 if (mfEmlGetMem(buf, 0, 1)) {\r
b915fda3 1412 PrintAndLog("Can\'t get UID from block: %d", 0);\r
0b14440d
PL
1413 len = sprintf(fnameptr, "dump");\r
1414 fnameptr += len;\r
1415 }\r
1416 else {\r
1417 for (j = 0; j < 7; j++, fnameptr += 2)\r
1418 sprintf(fnameptr, "%02X", buf[j]);\r
ab8b654e 1419 }\r
ab8b654e 1420 } else {\r
0b14440d 1421 fnameptr += len;\r
ab8b654e
M
1422 }\r
1423\r
b915fda3 1424 // add file extension\r
ab8b654e
M
1425 sprintf(fnameptr, ".eml"); \r
1426 \r
1427 // open file\r
1428 f = fopen(filename, "w+");\r
1429\r
b915fda3 1430 if ( !f ) {\r
1431 PrintAndLog("Can't open file %s ", filename);\r
1432 return 1;\r
1433 }\r
1434 \r
ab8b654e 1435 // put hex\r
b915fda3 1436 for (i = 0; i < numBlocks; i++) {\r
ab8b654e
M
1437 if (mfEmlGetMem(buf, i, 1)) {\r
1438 PrintAndLog("Cant get block: %d", i);\r
1439 break;\r
1440 }\r
1441 for (j = 0; j < 16; j++)\r
3fe4ff4f 1442 fprintf(f, "%02X", buf[j]); \r
ab8b654e
M
1443 fprintf(f,"\n");\r
1444 }\r
1445 fclose(f);\r
1446 \r
b915fda3 1447 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);\r
ab8b654e 1448 \r
9ca155ba
M
1449 return 0;\r
1450}\r
1451\r
baeaf579 1452\r
26fdb4ab 1453int CmdHF14AMfECFill(const char *Cmd)\r
1454{\r
8556b852 1455 uint8_t keyType = 0;\r
baeaf579 1456 uint8_t numSectors = 16;\r
1457 \r
8556b852 1458 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
baeaf579 1459 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r
1460 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1461 PrintAndLog("");\r
1462 PrintAndLog("samples: hf mf ecfill A");\r
1463 PrintAndLog(" hf mf ecfill A 4");\r
1464 PrintAndLog("Read card and transfer its data to emulator memory.");\r
1465 PrintAndLog("Keys must be laid in the emulator memory. \n");\r
8556b852
M
1466 return 0;\r
1467 } \r
1468\r
1469 char ctmp = param_getchar(Cmd, 0);\r
baeaf579 1470 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
8556b852
M
1471 PrintAndLog("Key type must be A or B");\r
1472 return 1;\r
1473 }\r
1474 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1475\r
baeaf579 1476 ctmp = param_getchar(Cmd, 1);\r
1477 switch (ctmp) {\r
1478 case '0' : numSectors = 5; break;\r
1479 case '1' : \r
1480 case '\0': numSectors = 16; break;\r
1481 case '2' : numSectors = 32; break;\r
1482 case '4' : numSectors = 40; break;\r
1483 default: numSectors = 16;\r
1484 } \r
1485\r
1486 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r
1487 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r
1488 SendCommand(&c);\r
1489 return 0;\r
8556b852
M
1490}\r
1491\r
baeaf579 1492\r
26fdb4ab 1493int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1494{\r
baeaf579 1495 int i;\r
b915fda3 1496 uint8_t numSectors;\r
8556b852
M
1497 uint8_t data[16];\r
1498 uint64_t keyA, keyB;\r
1499 \r
b915fda3 1500 if (param_getchar(Cmd, 0) == 'h') {\r
1501 PrintAndLog("It prints the keys loaded in the emulator memory");\r
1502 PrintAndLog("Usage: hf mf ekeyprn [card memory]");\r
1503 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1504 PrintAndLog("");\r
1505 PrintAndLog(" sample: hf mf ekeyprn 1");\r
1506 return 0;\r
1507 } \r
1508\r
1509 char cmdp = param_getchar(Cmd, 0);\r
1510 \r
1511 switch (cmdp) {\r
1512 case '0' : numSectors = 5; break;\r
1513 case '1' : \r
1514 case '\0': numSectors = 16; break;\r
1515 case '2' : numSectors = 32; break;\r
1516 case '4' : numSectors = 40; break;\r
1517 default: numSectors = 16;\r
1518 } \r
1519 \r
8556b852
M
1520 PrintAndLog("|---|----------------|----------------|");\r
1521 PrintAndLog("|sec|key A |key B |");\r
1522 PrintAndLog("|---|----------------|----------------|");\r
b915fda3 1523 for (i = 0; i < numSectors; i++) {\r
baeaf579 1524 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r
1525 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
8556b852
M
1526 break;\r
1527 }\r
1528 keyA = bytes_to_num(data, 6);\r
1529 keyB = bytes_to_num(data + 10, 6);\r
125a98a1 1530 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r
8556b852
M
1531 }\r
1532 PrintAndLog("|---|----------------|----------------|");\r
1533 \r
1534 return 0;\r
1535}\r
1536\r
baeaf579 1537\r
0675f200
M
1538int CmdHF14AMfCSetUID(const char *Cmd)\r
1539{\r
1540 uint8_t wipeCard = 0;\r
3fe4ff4f 1541 uint8_t uid[8] = {0x00};\r
1542 uint8_t oldUid[8] = {0x00};\r
3bba7dea
JH
1543 uint8_t atqa[2] = {0x00};\r
1544 uint8_t sak[1] = {0x00};\r
1545 uint8_t atqaPresent = 1;\r
0675f200 1546 int res;\r
3bba7dea
JH
1547 char ctmp;\r
1548 int argi=0;\r
1549\r
1550 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {\r
1551 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");\r
1552 PrintAndLog("sample: hf mf csetuid 01020304");\r
1553 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");\r
1554 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");\r
1555 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");\r
0675f200 1556 return 0;\r
3bba7dea 1557 }\r
0675f200 1558\r
3bba7dea 1559 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {\r
0675f200
M
1560 PrintAndLog("UID must include 8 HEX symbols");\r
1561 return 1;\r
1562 }\r
3bba7dea
JH
1563 argi++;\r
1564\r
1565 ctmp = param_getchar(Cmd, argi);\r
1566 if (ctmp == 'w' || ctmp == 'W') {\r
1567 wipeCard = 1;\r
1568 atqaPresent = 0;\r
1569 }\r
1570\r
1571 if (atqaPresent) {\r
1572 if (param_getchar(Cmd, argi)) {\r
1573 if (param_gethex(Cmd, argi, atqa, 4)) {\r
1574 PrintAndLog("ATQA must include 4 HEX symbols");\r
1575 return 1;\r
1576 }\r
1577 argi++;\r
1578 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {\r
1579 PrintAndLog("SAK must include 2 HEX symbols");\r
1580 return 1;\r
1581 }\r
1582 argi++;\r
1583 } else\r
1584 atqaPresent = 0;\r
1585 }\r
1586\r
1587 if(!wipeCard) {\r
1588 ctmp = param_getchar(Cmd, argi);\r
1589 if (ctmp == 'w' || ctmp == 'W') {\r
1590 wipeCard = 1;\r
1591 }\r
1592 }\r
0675f200 1593\r
e3c23565 1594 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));\r
0675f200 1595\r
3bba7dea 1596 res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);\r
0675f200
M
1597 if (res) {\r
1598 PrintAndLog("Can't set UID. error=%d", res);\r
1599 return 1;\r
1600 }\r
1601 \r
1602 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
3fe4ff4f 1603 PrintAndLog("new UID:%s", sprint_hex(uid, 4));\r
0675f200
M
1604 return 0;\r
1605}\r
1606\r
1607int CmdHF14AMfCSetBlk(const char *Cmd)\r
1608{\r
5ee70129 1609 uint8_t memBlock[16] = {0x00};\r
f774db95 1610 uint8_t blockNo = 0;\r
664f6586 1611 bool wipeCard = FALSE;\r
f774db95 1612 int res;\r
f774db95
M
1613\r
1614 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
664f6586 1615 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");\r
f774db95 1616 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
664f6586 1617 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");\r
1618 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");\r
f774db95
M
1619 return 0;\r
1620 } \r
1621\r
1622 blockNo = param_get8(Cmd, 0);\r
f774db95
M
1623\r
1624 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1625 PrintAndLog("block data must include 32 HEX symbols");\r
1626 return 1;\r
1627 }\r
1628\r
664f6586 1629 char ctmp = param_getchar(Cmd, 2);\r
1630 wipeCard = (ctmp == 'w' || ctmp == 'W');\r
baeaf579 1631 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\r
f774db95 1632\r
664f6586 1633 res = mfCSetBlock(blockNo, memBlock, NULL, wipeCard, CSETBLOCK_SINGLE_OPER);\r
f774db95 1634 if (res) {\r
664f6586 1635 PrintAndLog("Can't write block. error=%d", res);\r
1636 return 1;\r
1637 }\r
0675f200
M
1638 return 0;\r
1639}\r
1640\r
baeaf579 1641\r
0675f200
M
1642int CmdHF14AMfCLoad(const char *Cmd)\r
1643{\r
208a0166 1644 FILE * f;\r
b915fda3 1645 char filename[FILE_PATH_SIZE] = {0x00};\r
208a0166 1646 char * fnameptr = filename;\r
b915fda3 1647 char buf[64] = {0x00};\r
1648 uint8_t buf8[64] = {0x00};\r
208a0166 1649 uint8_t fillFromEmulator = 0;\r
9f7bbd24 1650 int i, len, blockNum, flags=0;\r
208a0166 1651 \r
208a0166 1652 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
e3c23565 1653 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");\r
208a0166
M
1654 PrintAndLog("or from emulator memory (option `e`)");\r
1655 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1656 PrintAndLog(" or: hf mf cload e ");\r
1657 PrintAndLog(" sample: hf mf cload filename");\r
1658 return 0;\r
1659 } \r
1660\r
1661 char ctmp = param_getchar(Cmd, 0);\r
1662 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1663 \r
1664 if (fillFromEmulator) {\r
208a0166
M
1665 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1666 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1667 PrintAndLog("Cant get block: %d", blockNum);\r
1668 return 2;\r
1669 }\r
16a95d76 1670 if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence\r
1671 if (blockNum == 1) flags = 0; // just write\r
1672 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Magic Halt and switch off field.\r
208a0166
M
1673\r
1674 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1675 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1676 return 3;\r
1677 }\r
1678 }\r
1679 return 0;\r
1680 } else {\r
1681 len = strlen(Cmd);\r
292fe725 1682 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
208a0166
M
1683\r
1684 memcpy(filename, Cmd, len);\r
292fe725 1685 fnameptr += len;\r
208a0166
M
1686\r
1687 sprintf(fnameptr, ".eml"); \r
1688 \r
1689 // open file\r
1690 f = fopen(filename, "r");\r
1691 if (f == NULL) {\r
1692 PrintAndLog("File not found or locked.");\r
1693 return 1;\r
1694 }\r
1695 \r
1696 blockNum = 0;\r
208a0166 1697 while(!feof(f)){\r
e3c23565 1698 \r
208a0166 1699 memset(buf, 0, sizeof(buf));\r
e3c23565 1700 \r
759c16b3 1701 if (fgets(buf, sizeof(buf), f) == NULL) {\r
e6432f05 1702 fclose(f);\r
baeaf579 1703 PrintAndLog("File reading error.");\r
1704 return 2;\r
1705 }\r
208a0166 1706\r
16a95d76 1707 if (strlen(buf) < 32) {\r
208a0166
M
1708 if(strlen(buf) && feof(f))\r
1709 break;\r
1710 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
e6432f05 1711 fclose(f);\r
208a0166
M
1712 return 2;\r
1713 }\r
1714 for (i = 0; i < 32; i += 2)\r
1715 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1716\r
16a95d76 1717 if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence\r
1718 if (blockNum == 1) flags = 0; // just write\r
1719 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Switch off field.\r
208a0166
M
1720\r
1721 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
baeaf579 1722 PrintAndLog("Can't set magic card block: %d", blockNum);\r
208a0166
M
1723 return 3;\r
1724 }\r
1725 blockNum++;\r
1726 \r
1727 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1728 }\r
1729 fclose(f);\r
1730 \r
b915fda3 1731 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
208a0166
M
1732 PrintAndLog("File content error. There must be 64 blocks");\r
1733 return 4;\r
1734 }\r
1735 PrintAndLog("Loaded from file: %s", filename);\r
1736 return 0;\r
1737 }\r
e3c23565 1738 return 0;\r
0675f200
M
1739}\r
1740\r
545a1f38
M
1741int CmdHF14AMfCGetBlk(const char *Cmd) {\r
1742 uint8_t memBlock[16];\r
1743 uint8_t blockNo = 0;\r
1744 int res;\r
1745 memset(memBlock, 0x00, sizeof(memBlock));\r
1746\r
1747 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1748 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1749 PrintAndLog("sample: hf mf cgetblk 1");\r
664f6586 1750 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");\r
545a1f38
M
1751 return 0;\r
1752 } \r
1753\r
1754 blockNo = param_get8(Cmd, 0);\r
545a1f38 1755\r
baeaf579 1756 PrintAndLog("--block number:%2d ", blockNo);\r
545a1f38
M
1757\r
1758 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r
1759 if (res) {\r
1760 PrintAndLog("Can't read block. error=%d", res);\r
1761 return 1;\r
1762 }\r
1763 \r
1764 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r
1765 return 0;\r
1766}\r
1767\r
baeaf579 1768\r
545a1f38 1769int CmdHF14AMfCGetSc(const char *Cmd) {\r
5ee70129 1770 uint8_t memBlock[16] = {0x00};\r
545a1f38
M
1771 uint8_t sectorNo = 0;\r
1772 int i, res, flags;\r
545a1f38
M
1773\r
1774 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1775 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1776 PrintAndLog("sample: hf mf cgetsc 0");\r
664f6586 1777 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");\r
545a1f38
M
1778 return 0;\r
1779 } \r
1780\r
1781 sectorNo = param_get8(Cmd, 0);\r
1782 if (sectorNo > 15) {\r
1783 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1784 return 1;\r
1785 }\r
1786\r
baeaf579 1787 PrintAndLog("--sector number:%d ", sectorNo);\r
545a1f38
M
1788\r
1789 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1790 for (i = 0; i < 4; i++) {\r
1791 if (i == 1) flags = 0;\r
1792 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1793\r
1794 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r
1795 if (res) {\r
baeaf579 1796 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);\r
545a1f38
M
1797 return 1;\r
1798 }\r
1799 \r
baeaf579 1800 PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r
545a1f38
M
1801 }\r
1802 return 0;\r
1803}\r
1804\r
baeaf579 1805\r
545a1f38
M
1806int CmdHF14AMfCSave(const char *Cmd) {\r
1807\r
1808 FILE * f;\r
b915fda3 1809 char filename[FILE_PATH_SIZE] = {0x00};\r
545a1f38
M
1810 char * fnameptr = filename;\r
1811 uint8_t fillFromEmulator = 0;\r
b915fda3 1812 uint8_t buf[64] = {0x00};\r
545a1f38
M
1813 int i, j, len, flags;\r
1814 \r
b915fda3 1815 // memset(filename, 0, sizeof(filename));\r
1816 // memset(buf, 0, sizeof(buf));\r
545a1f38
M
1817\r
1818 if (param_getchar(Cmd, 0) == 'h') {\r
1819 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1820 PrintAndLog("or into emulator memory (option `e`)");\r
1821 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1822 PrintAndLog(" sample: hf mf esave ");\r
1823 PrintAndLog(" hf mf esave filename");\r
1824 PrintAndLog(" hf mf esave e \n");\r
1825 return 0;\r
1826 } \r
1827\r
1828 char ctmp = param_getchar(Cmd, 0);\r
1829 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1830\r
1831 if (fillFromEmulator) {\r
1832 // put into emulator\r
1833 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1834 for (i = 0; i < 16 * 4; i++) {\r
1835 if (i == 1) flags = 0;\r
1836 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1837 \r
1838 if (mfCGetBlock(i, buf, flags)) {\r
1839 PrintAndLog("Cant get block: %d", i);\r
1840 break;\r
1841 }\r
1842 \r
1843 if (mfEmlSetMem(buf, i, 1)) {\r
1844 PrintAndLog("Cant set emul block: %d", i);\r
1845 return 3;\r
1846 }\r
1847 }\r
1848 return 0;\r
1849 } else {\r
1850 len = strlen(Cmd);\r
292fe725 1851 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
545a1f38
M
1852 \r
1853 if (len < 1) {\r
1854 // get filename\r
1855 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
1856 PrintAndLog("Cant get block: %d", 0);\r
1d537ad6
PL
1857 len = sprintf(fnameptr, "dump");\r
1858 fnameptr += len;\r
1859 }\r
1860 else {\r
1861 for (j = 0; j < 7; j++, fnameptr += 2)\r
1862 sprintf(fnameptr, "%02x", buf[j]); \r
545a1f38 1863 }\r
545a1f38
M
1864 } else {\r
1865 memcpy(filename, Cmd, len);\r
1866 fnameptr += len;\r
1867 }\r
1868\r
1869 sprintf(fnameptr, ".eml"); \r
1870 \r
1871 // open file\r
1872 f = fopen(filename, "w+");\r
1873\r
b915fda3 1874 if (f == NULL) {\r
1875 PrintAndLog("File not found or locked.");\r
1876 return 1;\r
1877 }\r
1878\r
545a1f38
M
1879 // put hex\r
1880 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1881 for (i = 0; i < 16 * 4; i++) {\r
1882 if (i == 1) flags = 0;\r
1883 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1884 \r
1885 if (mfCGetBlock(i, buf, flags)) {\r
1886 PrintAndLog("Cant get block: %d", i);\r
1887 break;\r
1888 }\r
1889 for (j = 0; j < 16; j++)\r
1890 fprintf(f, "%02x", buf[j]); \r
1891 fprintf(f,"\n");\r
1892 }\r
1893 fclose(f);\r
1894 \r
1895 PrintAndLog("Saved to file: %s", filename);\r
1896 \r
1897 return 0;\r
1898 }\r
1899}\r
1900\r
baeaf579 1901\r
b62a5a84 1902int CmdHF14AMfSniff(const char *Cmd){\r
3fe4ff4f 1903\r
c948cbde
M
1904 bool wantLogToFile = 0;\r
1905 bool wantDecrypt = 0;\r
eede7162 1906 //bool wantSaveToEml = 0; TODO\r
55acbb2a
M
1907 bool wantSaveToEmlFile = 0;\r
1908\r
1909 //var \r
39864b0b
M
1910 int res = 0;\r
1911 int len = 0;\r
1912 int blockLen = 0;\r
39864b0b 1913 int pckNum = 0;\r
f71f4deb 1914 int num = 0;\r
1915 uint8_t uid[7];\r
991f13f2 1916 uint8_t uid_len;\r
5ee70129 1917 uint8_t atqa[2] = {0x00};\r
39864b0b
M
1918 uint8_t sak;\r
1919 bool isTag;\r
f71f4deb 1920 uint8_t *buf = NULL;\r
1921 uint16_t bufsize = 0;\r
1922 uint8_t *bufPtr = NULL;\r
b62a5a84 1923 \r
e3c23565 1924 char ctmp = param_getchar(Cmd, 0);\r
1925 if ( ctmp == 'h' || ctmp == 'H' ) {\r
baeaf579 1926 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r
55acbb2a
M
1927 PrintAndLog("You can specify:");\r
1928 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
1929 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
1930 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
3fe4ff4f 1931 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r
1932 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");\r
55acbb2a 1933 PrintAndLog(" sample: hf mf sniff l d e");\r
b62a5a84
M
1934 return 0;\r
1935 } \r
1936 \r
55acbb2a 1937 for (int i = 0; i < 4; i++) {\r
e3c23565 1938 ctmp = param_getchar(Cmd, i);\r
55acbb2a
M
1939 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
1940 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
eede7162 1941 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
55acbb2a
M
1942 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
1943 }\r
1944 \r
39864b0b
M
1945 printf("-------------------------------------------------------------------------\n");\r
1946 printf("Executing command. \n");\r
1947 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
1948 printf("Press the key on pc keyboard to abort the client.\n");\r
1949 printf("-------------------------------------------------------------------------\n");\r
1950\r
d714d3ef 1951 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
1952 clearCommandBuffer();\r
1953 SendCommand(&c);\r
b62a5a84 1954\r
39864b0b
M
1955 // wait cycle\r
1956 while (true) {\r
1957 printf(".");\r
1958 fflush(stdout);\r
1959 if (ukbhit()) {\r
1960 getchar();\r
1961 printf("\naborted via keyboard!\n");\r
1962 break;\r
1963 }\r
1964 \r
f71f4deb 1965 UsbCommand resp;\r
1966 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
902cb3c0 1967 res = resp.arg[0] & 0xff;\r
f71f4deb 1968 uint16_t traceLen = resp.arg[1];\r
1969 len = resp.arg[2];\r
1970\r
1971 if (res == 0) return 0; // we are done\r
1972\r
1973 if (res == 1) { // there is (more) data to be transferred\r
1974 if (pckNum == 0) { // first packet, (re)allocate necessary buffer\r
1975 if (traceLen > bufsize) {\r
1976 uint8_t *p;\r
1977 if (buf == NULL) { // not yet allocated\r
1978 p = malloc(traceLen);\r
1979 } else { // need more memory\r
1980 p = realloc(buf, traceLen);\r
1981 }\r
1982 if (p == NULL) {\r
1983 PrintAndLog("Cannot allocate memory for trace");\r
1984 free(buf);\r
1985 return 2;\r
1986 }\r
1987 buf = p;\r
1988 }\r
39864b0b 1989 bufPtr = buf;\r
f71f4deb 1990 bufsize = traceLen;\r
1991 memset(buf, 0x00, traceLen);\r
39864b0b 1992 }\r
902cb3c0 1993 memcpy(bufPtr, resp.d.asBytes, len);\r
39864b0b
M
1994 bufPtr += len;\r
1995 pckNum++;\r
1996 }\r
f71f4deb 1997\r
1998 if (res == 2) { // received all data, start displaying\r
39864b0b
M
1999 blockLen = bufPtr - buf;\r
2000 bufPtr = buf;\r
2001 printf(">\n");\r
2002 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
6a1f2d82 2003 while (bufPtr - buf < blockLen) {\r
f71f4deb 2004 bufPtr += 6; // skip (void) timing information\r
6a1f2d82 2005 len = *((uint16_t *)bufPtr);\r
2006 if(len & 0x8000) {\r
2007 isTag = true;\r
2008 len &= 0x7fff;\r
2009 } else {\r
2010 isTag = false;\r
2011 }\r
2012 bufPtr += 2;\r
2013 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
39864b0b
M
2014 memcpy(uid, bufPtr + 2, 7);\r
2015 memcpy(atqa, bufPtr + 2 + 7, 2);\r
991f13f2 2016 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
39864b0b 2017 sak = bufPtr[11];\r
991f13f2 2018 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
2019 sprint_hex(uid + (7 - uid_len), uid_len),\r
2020 atqa[1], \r
2021 atqa[0], \r
2022 sak);\r
d714d3ef 2023 if (wantLogToFile || wantDecrypt) {\r
991f13f2 2024 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r
55acbb2a
M
2025 AddLogCurrentDT(logHexFileName);\r
2026 } \r
3fe4ff4f 2027 if (wantDecrypt) \r
2028 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
39864b0b
M
2029 } else {\r
2030 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
3fe4ff4f 2031 if (wantLogToFile) \r
2032 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
2033 if (wantDecrypt) \r
2034 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
f71f4deb 2035 num++; \r
39864b0b
M
2036 }\r
2037 bufPtr += len;\r
6a1f2d82 2038 bufPtr += ((len-1)/8+1); // ignore parity\r
39864b0b 2039 }\r
f71f4deb 2040 pckNum = 0;\r
39864b0b 2041 }\r
3fe4ff4f 2042 } // resp not NULL\r
39864b0b 2043 } // while (true)\r
f71f4deb 2044\r
2045 free(buf);\r
d714d3ef 2046 return 0;\r
b62a5a84
M
2047}\r
2048\r
1a5a73ab 2049//needs nt, ar, at, Data to decrypt\r
2050int CmdDecryptTraceCmds(const char *Cmd){\r
2051 uint8_t data[50];\r
2052 int len = 0;\r
2053 param_gethex_ex(Cmd,3,data,&len);\r
2054 return tryDecryptWord(param_get32ex(Cmd,0,0,16),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16),data,len/2);\r
2055}\r
f71f4deb 2056\r
26fdb4ab 2057static command_t CommandTable[] =\r
9ca155ba 2058{\r
baeaf579 2059 {"help", CmdHelp, 1, "This help"},\r
2060 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
d2f487af 2061 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
d2f487af 2062 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
2063 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
baeaf579 2064 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
9ca155ba 2065 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
baeaf579 2066 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
7bc95e2e 2067 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
9ca155ba 2068 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
b62a5a84 2069 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
baeaf579 2070 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
545a1f38 2071 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
51969283
M
2072 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
2073 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
9ca155ba
M
2074 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
2075 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
8556b852 2076 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
baeaf579 2077 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
2078 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
b915fda3 2079 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},\r
2080 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},\r
2081 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},\r
208a0166 2082 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
545a1f38 2083 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
1a5a73ab 2084 {"decrypt", CmdDecryptTraceCmds,1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
9ca155ba
M
2085 {NULL, NULL, 0, NULL}\r
2086};\r
2087\r
2088int CmdHFMF(const char *Cmd)\r
2089{\r
2090 // flush\r
7dd1908b 2091 WaitForResponseTimeout(CMD_ACK,NULL,100);\r
9ca155ba
M
2092\r
2093 CmdsParse(CommandTable, Cmd);\r
2094 return 0;\r
2095}\r
2096\r
2097int CmdHelp(const char *Cmd)\r
2098{\r
2099 CmdsHelp(CommandTable);\r
2100 return 0;\r
2101}\r
Impressum, Datenschutz