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