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