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