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