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