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