]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmf.c
CHG: minor change initializing array.
[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
1221 if (param_getchar(Cmd, pnr) == 'n') {\r
1222 exitAfterNReads = param_get8(Cmd,pnr+1);\r
1223 pnr += 2;\r
1224 }\r
1225 if (param_getchar(Cmd, pnr) == 'i' ) {\r
1226 //Using a flag to signal interactiveness, least significant bit\r
1227 flags |= FLAG_INTERACTIVE;\r
1228 pnr++;\r
1229 }\r
1230\r
1231 if (param_getchar(Cmd, pnr) == 'x' ) {\r
1232 //Using a flag to signal interactiveness, least significant bit\r
1233 flags |= FLAG_NR_AR_ATTACK;\r
1234 }\r
1235 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r
1236 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
1237 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r
1238 , exitAfterNReads, flags,flags);\r
1239\r
1240\r
baeaf579 1241 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1242 memcpy(c.d.asBytes, uid, sizeof(uid));\r
2285d9dd 1243 clearCommandBuffer();\r
baeaf579 1244 SendCommand(&c);\r
d2f487af 1245\r
baeaf579 1246 if(flags & FLAG_INTERACTIVE)\r
46cd801c 1247 { \r
46cd801c 1248 uint8_t data[40];\r
1249 uint8_t key[6];\r
1250\r
c3c241f3 1251 UsbCommand resp; \r
2b1f4228 1252 PrintAndLog("Press pm3-button or send another cmd to abort simulation");\r
1253 //while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
1254 //We're waiting only 1.5 s at a time, otherwise we get the\r
1255 // annoying message about "Waiting for a response... "\r
1256 //}\r
c3c241f3 1257 while(!ukbhit() ){\r
2b1f4228 1258 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) continue;\r
1259\r
1260 if ( !(flags & FLAG_NR_AR_ATTACK) ) break;\r
1261 if ( (resp.arg[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD ) break;\r
1262\r
c3c241f3 1263 memset(data, 0x00, sizeof(data));\r
1264 memset(key, 0x00, sizeof(key));\r
1265 int len = (resp.arg[1] > sizeof(data)) ? sizeof(data) : resp.arg[1];\r
1266 \r
1267 memcpy(data, resp.d.asBytes, len);\r
1268 \r
1269 uint64_t corr_uid = 0;\r
1270 if ( memcmp(data, "\x00\x00\x00\x00", 4) == 0 ) {\r
4750877f 1271 corr_uid = ((uint64_t)(data[3] << 24)) | (data[2] << 16) | (data[1] << 8) | data[0];\r
2b1f4228 1272 tryMfk32(corr_uid, data, key);\r
1273 } else {\r
c3c241f3 1274 corr_uid |= (uint64_t)data[2] << 48; \r
1275 corr_uid |= (uint64_t)data[1] << 40; \r
1276 corr_uid |= (uint64_t)data[0] << 32;\r
95d96ea3 1277 corr_uid |= (uint64_t)data[7] << 24;\r
1278 corr_uid |= (uint64_t)data[6] << 16;\r
1279 corr_uid |= (uint64_t)data[5] << 8;\r
1280 corr_uid |= (uint64_t)data[4];\r
2b1f4228 1281 tryMfk64(corr_uid, data, key);\r
c3c241f3 1282 }\r
c3c241f3 1283 PrintAndLog("--");\r
1284 }\r
46cd801c 1285 }\r
2b1f4228 1286 \r
baeaf579 1287 return 0;\r
9ca155ba
M
1288}\r
1289\r
1290int CmdHF14AMfDbg(const char *Cmd)\r
1291{\r
8556b852
M
1292 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
1293 if (dbgMode > 4) {\r
baeaf579 1294 PrintAndLog("Max debug mode parameter is 4 \n");\r
8556b852
M
1295 }\r
1296\r
1297 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
9ca155ba
M
1298 PrintAndLog("Usage: hf mf dbg <debug level>");\r
1299 PrintAndLog(" 0 - no debug messages");\r
1300 PrintAndLog(" 1 - error messages");\r
b03c0f2d 1301 PrintAndLog(" 2 - plus information messages");\r
1302 PrintAndLog(" 3 - plus debug messages");\r
1303 PrintAndLog(" 4 - print even debug messages in timing critical functions");\r
1304 PrintAndLog(" Note: this option therefore may cause malfunction itself");\r
9ca155ba
M
1305 return 0;\r
1306 } \r
1307\r
2285d9dd 1308 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
1309 SendCommand(&c);\r
8556b852 1310\r
2285d9dd 1311 return 0;\r
9ca155ba
M
1312}\r
1313\r
1314int CmdHF14AMfEGet(const char *Cmd)\r
1315{\r
8556b852 1316 uint8_t blockNo = 0;\r
5ee70129 1317 uint8_t data[16] = {0x00};\r
8556b852
M
1318\r
1319 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1320 PrintAndLog("Usage: hf mf eget <block number>");\r
1321 PrintAndLog(" sample: hf mf eget 0 ");\r
1322 return 0;\r
1323 } \r
1324 \r
1325 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1326\r
1327 PrintAndLog(" ");\r
baeaf579 1328 if (!mfEmlGetMem(data, blockNo, 1)) {\r
1329 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r
8556b852
M
1330 } else {\r
1331 PrintAndLog("Command execute timeout");\r
1332 }\r
1333\r
1334 return 0;\r
1335}\r
1336\r
1337int CmdHF14AMfEClear(const char *Cmd)\r
1338{\r
1339 if (param_getchar(Cmd, 0) == 'h') {\r
1340 PrintAndLog("Usage: hf mf eclr");\r
1341 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1342 return 0;\r
1343 } \r
1344\r
2285d9dd 1345 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1346 SendCommand(&c);\r
1347 return 0;\r
9ca155ba
M
1348}\r
1349\r
1350int CmdHF14AMfESet(const char *Cmd)\r
1351{\r
8556b852
M
1352 uint8_t memBlock[16];\r
1353 uint8_t blockNo = 0;\r
1354\r
1355 memset(memBlock, 0x00, sizeof(memBlock));\r
1356\r
1357 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1358 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1359 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1360 return 0;\r
1361 } \r
1362 \r
1363 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1364 \r
1365 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1366 PrintAndLog("block data must include 32 HEX symbols");\r
1367 return 1;\r
1368 }\r
1369 \r
1370 // 1 - blocks count\r
baeaf579 1371 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
8556b852 1372 memcpy(c.d.asBytes, memBlock, 16);\r
baeaf579 1373 SendCommand(&c);\r
1374 return 0;\r
9ca155ba
M
1375}\r
1376\r
1377int CmdHF14AMfELoad(const char *Cmd)\r
1378{\r
ab8b654e 1379 FILE * f;\r
b915fda3 1380 char filename[FILE_PATH_SIZE];\r
baeaf579 1381 char *fnameptr = filename;\r
5ee70129 1382 char buf[64] = {0x00};\r
1383 uint8_t buf8[64] = {0x00};\r
b915fda3 1384 int i, len, blockNum, numBlocks;\r
1385 int nameParamNo = 1;\r
0de8e387 1386 uint8_t blockWidth = 32;\r
b915fda3 1387 char ctmp = param_getchar(Cmd, 0);\r
1388 \r
2285d9dd 1389 if ( ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {\r
ab8b654e 1390 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
2b1f4228 1391 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");\r
0de8e387 1392 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");\r
b915fda3 1393 PrintAndLog("");\r
ab8b654e 1394 PrintAndLog(" sample: hf mf eload filename");\r
b915fda3 1395 PrintAndLog(" hf mf eload 4 filename");\r
ab8b654e
M
1396 return 0;\r
1397 } \r
1398\r
b915fda3 1399 switch (ctmp) {\r
1400 case '0' : numBlocks = 5*4; break;\r
1401 case '1' : \r
1402 case '\0': numBlocks = 16*4; break;\r
1403 case '2' : numBlocks = 32*4; break;\r
1404 case '4' : numBlocks = 256; break;\r
2b1f4228 1405 case 'U' : // fall through\r
1406 case 'u' : numBlocks = 255; blockWidth = 8; break;\r
b915fda3 1407 default: {\r
1408 numBlocks = 16*4;\r
1409 nameParamNo = 0;\r
1410 }\r
1411 }\r
2b1f4228 1412 uint32_t numblk2 = param_get32ex(Cmd,2,0,10);\r
1413 if (numblk2 > 0) numBlocks = numblk2; \r
b915fda3 1414\r
1415 len = param_getstr(Cmd,nameParamNo,filename);\r
1416 \r
60daed79 1417 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
ab8b654e 1418\r
d23f3f2c 1419 fnameptr += len;\r
ab8b654e
M
1420\r
1421 sprintf(fnameptr, ".eml"); \r
1422 \r
1423 // open file\r
1424 f = fopen(filename, "r");\r
1425 if (f == NULL) {\r
3fe4ff4f 1426 PrintAndLog("File %s not found or locked", filename);\r
ab8b654e
M
1427 return 1;\r
1428 }\r
1429 \r
1430 blockNum = 0;\r
1431 while(!feof(f)){\r
1432 memset(buf, 0, sizeof(buf));\r
b915fda3 1433 \r
759c16b3 1434 if (fgets(buf, sizeof(buf), f) == NULL) {\r
b915fda3 1435 \r
1436 if (blockNum >= numBlocks) break;\r
1437 \r
d2f487af 1438 PrintAndLog("File reading error.");\r
97d582a6 1439 fclose(f);\r
759c16b3 1440 return 2;\r
baeaf579 1441 }\r
b915fda3 1442 \r
0de8e387 1443 if (strlen(buf) < blockWidth){\r
aea4d766 1444 if(strlen(buf) && feof(f))\r
1445 break;\r
0de8e387 1446 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth);\r
97d582a6 1447 fclose(f);\r
ab8b654e
M
1448 return 2;\r
1449 }\r
b915fda3 1450 \r
0de8e387 1451 for (i = 0; i < blockWidth; i += 2) {\r
baeaf579 1452 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
baeaf579 1453 }\r
0de8e387 1454 if (mfEmlSetMem_xt(buf8, blockNum, 1, blockWidth/2)) {\r
baeaf579 1455 PrintAndLog("Cant set emul block: %3d", blockNum);\r
97d582a6 1456 fclose(f);\r
ab8b654e
M
1457 return 3;\r
1458 }\r
5ee70129 1459 printf(".");\r
ab8b654e
M
1460 blockNum++;\r
1461 \r
b915fda3 1462 if (blockNum >= numBlocks) break;\r
ab8b654e
M
1463 }\r
1464 fclose(f);\r
5ee70129 1465 printf("\n");\r
ab8b654e 1466 \r
b915fda3 1467 if ((blockNum != numBlocks)) {\r
1468 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);\r
ab8b654e
M
1469 return 4;\r
1470 }\r
d2f487af 1471 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r
baeaf579 1472 return 0;\r
9ca155ba
M
1473}\r
1474\r
1475int CmdHF14AMfESave(const char *Cmd)\r
1476{\r
ab8b654e 1477 FILE * f;\r
b915fda3 1478 char filename[FILE_PATH_SIZE];\r
ab8b654e
M
1479 char * fnameptr = filename;\r
1480 uint8_t buf[64];\r
b915fda3 1481 int i, j, len, numBlocks;\r
1482 int nameParamNo = 1;\r
ab8b654e
M
1483 \r
1484 memset(filename, 0, sizeof(filename));\r
1485 memset(buf, 0, sizeof(buf));\r
1486\r
b915fda3 1487 char ctmp = param_getchar(Cmd, 0);\r
1488 \r
1489 if ( ctmp == 'h' || ctmp == 'H') {\r
ab8b654e 1490 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
b915fda3 1491 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");\r
1492 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1493 PrintAndLog("");\r
ab8b654e 1494 PrintAndLog(" sample: hf mf esave ");\r
b915fda3 1495 PrintAndLog(" hf mf esave 4");\r
1496 PrintAndLog(" hf mf esave 4 filename");\r
ab8b654e
M
1497 return 0;\r
1498 } \r
1499\r
b915fda3 1500 switch (ctmp) {\r
1501 case '0' : numBlocks = 5*4; break;\r
1502 case '1' : \r
1503 case '\0': numBlocks = 16*4; break;\r
1504 case '2' : numBlocks = 32*4; break;\r
1505 case '4' : numBlocks = 256; break;\r
1506 default: {\r
1507 numBlocks = 16*4;\r
1508 nameParamNo = 0;\r
1509 }\r
1510 }\r
1511\r
1512 len = param_getstr(Cmd,nameParamNo,filename);\r
1513 \r
60daed79 1514 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
ab8b654e 1515 \r
b915fda3 1516 // user supplied filename?\r
ab8b654e 1517 if (len < 1) {\r
b915fda3 1518 // get filename (UID from memory)\r
ab8b654e 1519 if (mfEmlGetMem(buf, 0, 1)) {\r
b915fda3 1520 PrintAndLog("Can\'t get UID from block: %d", 0);\r
0b14440d 1521 len = sprintf(fnameptr, "dump");\r
d23f3f2c 1522 fnameptr += len;\r
1523 }\r
1524 else {\r
1525 for (j = 0; j < 7; j++, fnameptr += 2)\r
0b14440d 1526 sprintf(fnameptr, "%02X", buf[j]);\r
ab8b654e 1527 }\r
ab8b654e 1528 } else {\r
d23f3f2c 1529 fnameptr += len;\r
ab8b654e
M
1530 }\r
1531\r
b915fda3 1532 // add file extension\r
ab8b654e
M
1533 sprintf(fnameptr, ".eml"); \r
1534 \r
1535 // open file\r
1536 f = fopen(filename, "w+");\r
1537\r
b915fda3 1538 if ( !f ) {\r
1539 PrintAndLog("Can't open file %s ", filename);\r
1540 return 1;\r
1541 }\r
1542 \r
ab8b654e 1543 // put hex\r
b915fda3 1544 for (i = 0; i < numBlocks; i++) {\r
ab8b654e
M
1545 if (mfEmlGetMem(buf, i, 1)) {\r
1546 PrintAndLog("Cant get block: %d", i);\r
1547 break;\r
1548 }\r
1549 for (j = 0; j < 16; j++)\r
3fe4ff4f 1550 fprintf(f, "%02X", buf[j]); \r
ab8b654e
M
1551 fprintf(f,"\n");\r
1552 }\r
1553 fclose(f);\r
1554 \r
b915fda3 1555 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);\r
ab8b654e 1556 \r
9ca155ba
M
1557 return 0;\r
1558}\r
1559\r
26fdb4ab 1560int CmdHF14AMfECFill(const char *Cmd)\r
1561{\r
8556b852 1562 uint8_t keyType = 0;\r
baeaf579 1563 uint8_t numSectors = 16;\r
1564 \r
8556b852 1565 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
baeaf579 1566 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r
1567 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1568 PrintAndLog("");\r
1569 PrintAndLog("samples: hf mf ecfill A");\r
1570 PrintAndLog(" hf mf ecfill A 4");\r
1571 PrintAndLog("Read card and transfer its data to emulator memory.");\r
1572 PrintAndLog("Keys must be laid in the emulator memory. \n");\r
8556b852
M
1573 return 0;\r
1574 } \r
1575\r
1576 char ctmp = param_getchar(Cmd, 0);\r
baeaf579 1577 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
8556b852
M
1578 PrintAndLog("Key type must be A or B");\r
1579 return 1;\r
1580 }\r
1581 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1582\r
baeaf579 1583 ctmp = param_getchar(Cmd, 1);\r
1584 switch (ctmp) {\r
1585 case '0' : numSectors = 5; break;\r
1586 case '1' : \r
1587 case '\0': numSectors = 16; break;\r
1588 case '2' : numSectors = 32; break;\r
1589 case '4' : numSectors = 40; break;\r
1590 default: numSectors = 16;\r
1591 } \r
1592\r
1593 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r
1594 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r
1595 SendCommand(&c);\r
1596 return 0;\r
8556b852
M
1597}\r
1598\r
26fdb4ab 1599int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1600{\r
baeaf579 1601 int i;\r
b915fda3 1602 uint8_t numSectors;\r
8556b852
M
1603 uint8_t data[16];\r
1604 uint64_t keyA, keyB;\r
2285d9dd 1605\r
1606 char cmdp = param_getchar(Cmd, 0);\r
8556b852 1607 \r
2285d9dd 1608 if ( cmdp == 'h' || cmdp == 'H' ) {\r
b915fda3 1609 PrintAndLog("It prints the keys loaded in the emulator memory");\r
1610 PrintAndLog("Usage: hf mf ekeyprn [card memory]");\r
1611 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1612 PrintAndLog("");\r
1613 PrintAndLog(" sample: hf mf ekeyprn 1");\r
1614 return 0;\r
1615 } \r
1616\r
b915fda3 1617 switch (cmdp) {\r
1618 case '0' : numSectors = 5; break;\r
1619 case '1' : \r
1620 case '\0': numSectors = 16; break;\r
1621 case '2' : numSectors = 32; break;\r
1622 case '4' : numSectors = 40; break;\r
1623 default: numSectors = 16;\r
1624 } \r
1625 \r
8556b852
M
1626 PrintAndLog("|---|----------------|----------------|");\r
1627 PrintAndLog("|sec|key A |key B |");\r
1628 PrintAndLog("|---|----------------|----------------|");\r
b915fda3 1629 for (i = 0; i < numSectors; i++) {\r
baeaf579 1630 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r
1631 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
8556b852
M
1632 break;\r
1633 }\r
1634 keyA = bytes_to_num(data, 6);\r
1635 keyB = bytes_to_num(data + 10, 6);\r
125a98a1 1636 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r
8556b852
M
1637 }\r
1638 PrintAndLog("|---|----------------|----------------|");\r
1639 \r
1640 return 0;\r
1641}\r
1642\r
0675f200
M
1643int CmdHF14AMfCSetUID(const char *Cmd)\r
1644{\r
1645 uint8_t wipeCard = 0;\r
3fe4ff4f 1646 uint8_t uid[8] = {0x00};\r
1647 uint8_t oldUid[8] = {0x00};\r
3bba7dea
JH
1648 uint8_t atqa[2] = {0x00};\r
1649 uint8_t sak[1] = {0x00};\r
1650 uint8_t atqaPresent = 1;\r
0675f200 1651 int res;\r
3bba7dea
JH
1652 char ctmp;\r
1653 int argi=0;\r
1654\r
1655 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {\r
1656 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");\r
1657 PrintAndLog("sample: hf mf csetuid 01020304");\r
1658 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");\r
1659 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");\r
1660 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");\r
0675f200 1661 return 0;\r
3bba7dea 1662 }\r
0675f200 1663\r
3bba7dea 1664 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {\r
0675f200
M
1665 PrintAndLog("UID must include 8 HEX symbols");\r
1666 return 1;\r
1667 }\r
3bba7dea
JH
1668 argi++;\r
1669\r
1670 ctmp = param_getchar(Cmd, argi);\r
1671 if (ctmp == 'w' || ctmp == 'W') {\r
1672 wipeCard = 1;\r
1673 atqaPresent = 0;\r
1674 }\r
1675\r
1676 if (atqaPresent) {\r
1677 if (param_getchar(Cmd, argi)) {\r
1678 if (param_gethex(Cmd, argi, atqa, 4)) {\r
1679 PrintAndLog("ATQA must include 4 HEX symbols");\r
1680 return 1;\r
1681 }\r
1682 argi++;\r
1683 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {\r
1684 PrintAndLog("SAK must include 2 HEX symbols");\r
1685 return 1;\r
1686 }\r
1687 argi++;\r
1688 } else\r
1689 atqaPresent = 0;\r
1690 }\r
1691\r
1692 if(!wipeCard) {\r
1693 ctmp = param_getchar(Cmd, argi);\r
1694 if (ctmp == 'w' || ctmp == 'W') {\r
1695 wipeCard = 1;\r
1696 }\r
1697 }\r
0675f200 1698\r
e3c23565 1699 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));\r
0675f200 1700\r
3bba7dea 1701 res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);\r
0675f200
M
1702 if (res) {\r
1703 PrintAndLog("Can't set UID. error=%d", res);\r
1704 return 1;\r
1705 }\r
1706 \r
1707 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
3fe4ff4f 1708 PrintAndLog("new UID:%s", sprint_hex(uid, 4));\r
0675f200
M
1709 return 0;\r
1710}\r
1711\r
1712int CmdHF14AMfCSetBlk(const char *Cmd)\r
1713{\r
0956e0db 1714 uint8_t block[16] = {0x00};\r
f774db95 1715 uint8_t blockNo = 0;\r
c2731f37 1716 uint8_t params = MAGIC_SINGLE;\r
f774db95 1717 int res;\r
f774db95
M
1718\r
1719 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
664f6586 1720 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");\r
f774db95 1721 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
664f6586 1722 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");\r
1723 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");\r
f774db95
M
1724 return 0;\r
1725 } \r
1726\r
1727 blockNo = param_get8(Cmd, 0);\r
f774db95 1728\r
0956e0db 1729 if (param_gethex(Cmd, 1, block, 32)) {\r
f774db95
M
1730 PrintAndLog("block data must include 32 HEX symbols");\r
1731 return 1;\r
1732 }\r
1733\r
664f6586 1734 char ctmp = param_getchar(Cmd, 2);\r
c2731f37 1735 if (ctmp == 'w' || ctmp == 'W')\r
1736 params |= MAGIC_WIPE;\r
1737 \r
0956e0db 1738 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(block, 16));\r
f774db95 1739\r
0956e0db 1740 res = mfCSetBlock(blockNo, block, NULL, params);\r
f774db95 1741 if (res) {\r
664f6586 1742 PrintAndLog("Can't write block. error=%d", res);\r
1743 return 1;\r
1744 }\r
0675f200
M
1745 return 0;\r
1746}\r
1747\r
1748int CmdHF14AMfCLoad(const char *Cmd)\r
1749{\r
208a0166 1750 FILE * f;\r
c2731f37 1751 char filename[FILE_PATH_SIZE];\r
208a0166 1752 char * fnameptr = filename;\r
b915fda3 1753 char buf[64] = {0x00};\r
1754 uint8_t buf8[64] = {0x00};\r
208a0166 1755 uint8_t fillFromEmulator = 0;\r
9f7bbd24 1756 int i, len, blockNum, flags=0;\r
2285d9dd 1757\r
c2731f37 1758 memset(filename, 0, sizeof(filename));\r
1759 \r
2285d9dd 1760 char ctmp = param_getchar(Cmd, 0);\r
208a0166 1761 \r
2285d9dd 1762 if (ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {\r
e3c23565 1763 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");\r
208a0166
M
1764 PrintAndLog("or from emulator memory (option `e`)");\r
1765 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1766 PrintAndLog(" or: hf mf cload e ");\r
1767 PrintAndLog(" sample: hf mf cload filename");\r
1768 return 0;\r
1769 } \r
1770\r
208a0166
M
1771 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1772 \r
1773 if (fillFromEmulator) {\r
208a0166
M
1774 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1775 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1776 PrintAndLog("Cant get block: %d", blockNum);\r
1777 return 2;\r
1778 }\r
c2731f37 1779 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence\r
16a95d76 1780 if (blockNum == 1) flags = 0; // just write\r
c2731f37 1781 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Magic Halt and switch off field.\r
208a0166 1782\r
c2731f37 1783 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {\r
208a0166
M
1784 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1785 return 3;\r
1786 }\r
1787 }\r
1788 return 0;\r
1789 } else {\r
1790 len = strlen(Cmd);\r
60daed79 1791 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
208a0166
M
1792\r
1793 memcpy(filename, Cmd, len);\r
d23f3f2c 1794 fnameptr += len;\r
208a0166
M
1795\r
1796 sprintf(fnameptr, ".eml"); \r
1797 \r
1798 // open file\r
1799 f = fopen(filename, "r");\r
1800 if (f == NULL) {\r
1801 PrintAndLog("File not found or locked.");\r
1802 return 1;\r
1803 }\r
1804 \r
1805 blockNum = 0;\r
208a0166 1806 while(!feof(f)){\r
e3c23565 1807 \r
208a0166 1808 memset(buf, 0, sizeof(buf));\r
e3c23565 1809 \r
759c16b3 1810 if (fgets(buf, sizeof(buf), f) == NULL) {\r
e6432f05 1811 fclose(f);\r
baeaf579 1812 PrintAndLog("File reading error.");\r
1813 return 2;\r
1814 }\r
208a0166 1815\r
16a95d76 1816 if (strlen(buf) < 32) {\r
208a0166
M
1817 if(strlen(buf) && feof(f))\r
1818 break;\r
1819 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
664bb5ae 1820 fclose(f);\r
208a0166
M
1821 return 2;\r
1822 }\r
1823 for (i = 0; i < 32; i += 2)\r
1824 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1825\r
c2731f37 1826 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence\r
16a95d76 1827 if (blockNum == 1) flags = 0; // just write\r
c2731f37 1828 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Switch off field.\r
208a0166 1829\r
c2731f37 1830 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {\r
baeaf579 1831 PrintAndLog("Can't set magic card block: %d", blockNum);\r
4750877f 1832 fclose(f);\r
208a0166
M
1833 return 3;\r
1834 }\r
1835 blockNum++;\r
1836 \r
1837 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1838 }\r
1839 fclose(f);\r
1840 \r
c2731f37 1841 // 64 or 256blocks.\r
b915fda3 1842 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
208a0166
M
1843 PrintAndLog("File content error. There must be 64 blocks");\r
1844 return 4;\r
1845 }\r
1846 PrintAndLog("Loaded from file: %s", filename);\r
1847 return 0;\r
1848 }\r
e3c23565 1849 return 0;\r
0675f200
M
1850}\r
1851\r
545a1f38 1852int CmdHF14AMfCGetBlk(const char *Cmd) {\r
c2731f37 1853 uint8_t data[16];\r
545a1f38
M
1854 uint8_t blockNo = 0;\r
1855 int res;\r
c2731f37 1856 memset(data, 0x00, sizeof(data));\r
1857 char ctmp = param_getchar(Cmd, 0);\r
545a1f38 1858\r
c2731f37 1859 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {\r
545a1f38
M
1860 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1861 PrintAndLog("sample: hf mf cgetblk 1");\r
664f6586 1862 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");\r
545a1f38
M
1863 return 0;\r
1864 } \r
1865\r
1866 blockNo = param_get8(Cmd, 0);\r
545a1f38 1867\r
baeaf579 1868 PrintAndLog("--block number:%2d ", blockNo);\r
545a1f38 1869\r
c2731f37 1870 res = mfCGetBlock(blockNo, data, MAGIC_SINGLE);\r
545a1f38 1871 if (res) {\r
c2731f37 1872 PrintAndLog("Can't read block. error=%d", res);\r
1873 return 1;\r
1874 }\r
545a1f38 1875 \r
2b1f4228 1876 PrintAndLog("data: %s", sprint_hex(data, sizeof(data)));\r
545a1f38
M
1877 return 0;\r
1878}\r
1879\r
1880int CmdHF14AMfCGetSc(const char *Cmd) {\r
c2731f37 1881 uint8_t data[16];\r
545a1f38
M
1882 uint8_t sectorNo = 0;\r
1883 int i, res, flags;\r
c2731f37 1884 memset(data, 0x00, sizeof(data));\r
1885 char ctmp = param_getchar(Cmd, 0);\r
1886 \r
1887 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {\r
545a1f38
M
1888 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1889 PrintAndLog("sample: hf mf cgetsc 0");\r
664f6586 1890 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");\r
545a1f38
M
1891 return 0;\r
1892 } \r
1893\r
1894 sectorNo = param_get8(Cmd, 0);\r
1895 if (sectorNo > 15) {\r
1896 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1897 return 1;\r
1898 }\r
1899\r
baeaf579 1900 PrintAndLog("--sector number:%d ", sectorNo);\r
c2731f37 1901 PrintAndLog("block | data");\r
545a1f38 1902\r
c2731f37 1903 flags = MAGIC_INIT + MAGIC_WUPC;\r
545a1f38
M
1904 for (i = 0; i < 4; i++) {\r
1905 if (i == 1) flags = 0;\r
c2731f37 1906 if (i == 3) flags = MAGIC_HALT + MAGIC_OFF;\r
545a1f38 1907\r
c2731f37 1908 res = mfCGetBlock(sectorNo * 4 + i, data, flags);\r
545a1f38 1909 if (res) {\r
baeaf579 1910 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);\r
545a1f38 1911 return 1;\r
c2731f37 1912 } \r
1913 PrintAndLog(" %3d | %s", sectorNo * 4 + i, sprint_hex(data, sizeof(data)));\r
545a1f38
M
1914 }\r
1915 return 0;\r
1916}\r
1917\r
1918int CmdHF14AMfCSave(const char *Cmd) {\r
1919\r
1920 FILE * f;\r
c2731f37 1921 char filename[FILE_PATH_SIZE];\r
545a1f38
M
1922 char * fnameptr = filename;\r
1923 uint8_t fillFromEmulator = 0;\r
c2731f37 1924 uint8_t buf[64];\r
545a1f38
M
1925 int i, j, len, flags;\r
1926 \r
c2731f37 1927 memset(filename, 0, sizeof(filename));\r
1928 memset(buf, 0, sizeof(buf));\r
2285d9dd 1929 char ctmp = param_getchar(Cmd, 0);\r
1930 \r
1931 if ( ctmp == 'h' || ctmp == 'H' ) {\r
545a1f38
M
1932 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1933 PrintAndLog("or into emulator memory (option `e`)");\r
1934 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1935 PrintAndLog(" sample: hf mf esave ");\r
1936 PrintAndLog(" hf mf esave filename");\r
1937 PrintAndLog(" hf mf esave e \n");\r
1938 return 0;\r
1939 } \r
545a1f38
M
1940 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1941\r
1942 if (fillFromEmulator) {\r
1943 // put into emulator\r
c2731f37 1944 flags = MAGIC_INIT + MAGIC_WUPC;\r
545a1f38
M
1945 for (i = 0; i < 16 * 4; i++) {\r
1946 if (i == 1) flags = 0;\r
c2731f37 1947 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;\r
545a1f38
M
1948 \r
1949 if (mfCGetBlock(i, buf, flags)) {\r
1950 PrintAndLog("Cant get block: %d", i);\r
1951 break;\r
1952 }\r
1953 \r
1954 if (mfEmlSetMem(buf, i, 1)) {\r
1955 PrintAndLog("Cant set emul block: %d", i);\r
1956 return 3;\r
1957 }\r
1958 }\r
1959 return 0;\r
1960 } else {\r
1961 len = strlen(Cmd);\r
60daed79 1962 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
545a1f38 1963 \r
c2731f37 1964 // get filename based on UID\r
545a1f38 1965 if (len < 1) {\r
c2731f37 1966 \r
1967 if (mfCGetBlock(0, buf, MAGIC_SINGLE)) {\r
545a1f38 1968 PrintAndLog("Cant get block: %d", 0);\r
d23f3f2c 1969 len = sprintf(fnameptr, "dump");\r
1970 fnameptr += len;\r
1971 } else {\r
1972 for (j = 0; j < 7; j++, fnameptr += 2)\r
1973 sprintf(fnameptr, "%02x", buf[j]); \r
545a1f38 1974 }\r
545a1f38
M
1975 } else {\r
1976 memcpy(filename, Cmd, len);\r
1977 fnameptr += len;\r
1978 }\r
1979\r
c2731f37 1980 // add .eml extension\r
545a1f38
M
1981 sprintf(fnameptr, ".eml"); \r
1982 \r
1983 // open file\r
1984 f = fopen(filename, "w+");\r
1985\r
b915fda3 1986 if (f == NULL) {\r
1987 PrintAndLog("File not found or locked.");\r
1988 return 1;\r
1989 }\r
1990\r
545a1f38 1991 // put hex\r
c2731f37 1992 flags = MAGIC_INIT + MAGIC_WUPC;\r
545a1f38
M
1993 for (i = 0; i < 16 * 4; i++) {\r
1994 if (i == 1) flags = 0;\r
c2731f37 1995 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;\r
545a1f38
M
1996 \r
1997 if (mfCGetBlock(i, buf, flags)) {\r
1998 PrintAndLog("Cant get block: %d", i);\r
1999 break;\r
2000 }\r
2001 for (j = 0; j < 16; j++)\r
2002 fprintf(f, "%02x", buf[j]); \r
2003 fprintf(f,"\n");\r
2004 }\r
c2731f37 2005 fflush(f);\r
545a1f38 2006 fclose(f);\r
545a1f38 2007 PrintAndLog("Saved to file: %s", filename);\r
545a1f38
M
2008 return 0;\r
2009 }\r
2010}\r
2011\r
b62a5a84 2012int CmdHF14AMfSniff(const char *Cmd){\r
3fe4ff4f 2013\r
c948cbde
M
2014 bool wantLogToFile = 0;\r
2015 bool wantDecrypt = 0;\r
eede7162 2016 //bool wantSaveToEml = 0; TODO\r
55acbb2a
M
2017 bool wantSaveToEmlFile = 0;\r
2018\r
2019 //var \r
0697080e 2020 int tmpchar;\r
39864b0b
M
2021 int res = 0;\r
2022 int len = 0;\r
2023 int blockLen = 0;\r
39864b0b 2024 int pckNum = 0;\r
f71f4deb 2025 int num = 0;\r
2026 uint8_t uid[7];\r
991f13f2 2027 uint8_t uid_len;\r
5ee70129 2028 uint8_t atqa[2] = {0x00};\r
39864b0b
M
2029 uint8_t sak;\r
2030 bool isTag;\r
f71f4deb 2031 uint8_t *buf = NULL;\r
2032 uint16_t bufsize = 0;\r
2033 uint8_t *bufPtr = NULL;\r
b62a5a84 2034 \r
e3c23565 2035 char ctmp = param_getchar(Cmd, 0);\r
2036 if ( ctmp == 'h' || ctmp == 'H' ) {\r
baeaf579 2037 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r
55acbb2a
M
2038 PrintAndLog("You can specify:");\r
2039 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
2040 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
2041 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
3fe4ff4f 2042 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r
2043 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");\r
55acbb2a 2044 PrintAndLog(" sample: hf mf sniff l d e");\r
b62a5a84
M
2045 return 0;\r
2046 } \r
2047 \r
55acbb2a 2048 for (int i = 0; i < 4; i++) {\r
e3c23565 2049 ctmp = param_getchar(Cmd, i);\r
55acbb2a
M
2050 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
2051 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
eede7162 2052 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
55acbb2a
M
2053 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
2054 }\r
2055 \r
39864b0b
M
2056 printf("-------------------------------------------------------------------------\n");\r
2057 printf("Executing command. \n");\r
2058 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
2059 printf("Press the key on pc keyboard to abort the client.\n");\r
2060 printf("-------------------------------------------------------------------------\n");\r
2061\r
d714d3ef 2062 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
2063 clearCommandBuffer();\r
2064 SendCommand(&c);\r
b62a5a84 2065\r
39864b0b
M
2066 // wait cycle\r
2067 while (true) {\r
2068 printf(".");\r
2069 fflush(stdout);\r
2070 if (ukbhit()) {\r
0697080e 2071 tmpchar = getchar();\r
2072 (void)tmpchar;\r
39864b0b
M
2073 printf("\naborted via keyboard!\n");\r
2074 break;\r
2075 }\r
2076 \r
f71f4deb 2077 UsbCommand resp;\r
2078 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
902cb3c0 2079 res = resp.arg[0] & 0xff;\r
f71f4deb 2080 uint16_t traceLen = resp.arg[1];\r
2081 len = resp.arg[2];\r
2082\r
c46ea881 2083 if (res == 0) {\r
2084 free(buf);\r
2085 return 0; // we are done\r
2086 }\r
f71f4deb 2087\r
2088 if (res == 1) { // there is (more) data to be transferred\r
2089 if (pckNum == 0) { // first packet, (re)allocate necessary buffer\r
2090 if (traceLen > bufsize) {\r
2091 uint8_t *p;\r
2092 if (buf == NULL) { // not yet allocated\r
2093 p = malloc(traceLen);\r
2094 } else { // need more memory\r
2095 p = realloc(buf, traceLen);\r
2096 }\r
2097 if (p == NULL) {\r
2098 PrintAndLog("Cannot allocate memory for trace");\r
2099 free(buf);\r
2100 return 2;\r
2101 }\r
2102 buf = p;\r
2103 }\r
39864b0b 2104 bufPtr = buf;\r
f71f4deb 2105 bufsize = traceLen;\r
2106 memset(buf, 0x00, traceLen);\r
39864b0b 2107 }\r
e72d1fbb 2108 if (bufPtr == NULL) {\r
2109 PrintAndLog("Cannot allocate memory for trace");\r
2110 free(buf);\r
2111 return 2;\r
2112 }\r
902cb3c0 2113 memcpy(bufPtr, resp.d.asBytes, len);\r
39864b0b
M
2114 bufPtr += len;\r
2115 pckNum++;\r
2116 }\r
f71f4deb 2117\r
2118 if (res == 2) { // received all data, start displaying\r
39864b0b
M
2119 blockLen = bufPtr - buf;\r
2120 bufPtr = buf;\r
2121 printf(">\n");\r
2122 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
6a1f2d82 2123 while (bufPtr - buf < blockLen) {\r
f71f4deb 2124 bufPtr += 6; // skip (void) timing information\r
6a1f2d82 2125 len = *((uint16_t *)bufPtr);\r
2126 if(len & 0x8000) {\r
2127 isTag = true;\r
2128 len &= 0x7fff;\r
2129 } else {\r
2130 isTag = false;\r
2131 }\r
2132 bufPtr += 2;\r
2133 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
39864b0b
M
2134 memcpy(uid, bufPtr + 2, 7);\r
2135 memcpy(atqa, bufPtr + 2 + 7, 2);\r
991f13f2 2136 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
39864b0b 2137 sak = bufPtr[11];\r
991f13f2 2138 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
2139 sprint_hex(uid + (7 - uid_len), uid_len),\r
2140 atqa[1], \r
2141 atqa[0], \r
2142 sak);\r
d714d3ef 2143 if (wantLogToFile || wantDecrypt) {\r
991f13f2 2144 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r
55acbb2a
M
2145 AddLogCurrentDT(logHexFileName);\r
2146 } \r
3fe4ff4f 2147 if (wantDecrypt) \r
2148 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
39864b0b
M
2149 } else {\r
2150 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
3fe4ff4f 2151 if (wantLogToFile) \r
2152 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
2153 if (wantDecrypt) \r
2154 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
f71f4deb 2155 num++; \r
39864b0b
M
2156 }\r
2157 bufPtr += len;\r
6a1f2d82 2158 bufPtr += ((len-1)/8+1); // ignore parity\r
39864b0b 2159 }\r
f71f4deb 2160 pckNum = 0;\r
39864b0b 2161 }\r
3fe4ff4f 2162 } // resp not NULL\r
39864b0b 2163 } // while (true)\r
f71f4deb 2164\r
2165 free(buf);\r
d714d3ef 2166 return 0;\r
b62a5a84
M
2167}\r
2168\r
e98572a1 2169//needs nt, ar, at, Data to decrypt\r
2285d9dd 2170int CmdHf14MfDecryptBytes(const char *Cmd){\r
e98572a1 2171 uint8_t data[50];\r
3bc7b13d 2172 \r
2173 uint32_t nt = param_get32ex(Cmd,0,0,16);\r
2174 uint32_t ar_enc = param_get32ex(Cmd,1,0,16);\r
2175 uint32_t at_enc = param_get32ex(Cmd,2,0,16);\r
2176\r
e98572a1 2177 int len = 0;\r
3bc7b13d 2178 param_gethex_ex(Cmd, 3, data, &len);\r
2179 \r
2180 len /= 2; \r
2181 int limit = sizeof(data) / 2;\r
2182 \r
2183 if ( len >= limit )\r
2184 len = limit;\r
2185 \r
2186 return tryDecryptWord( nt, ar_enc, at_enc, data, len);\r
e98572a1 2187}\r
f71f4deb 2188\r
f1db8c22 2189static command_t CommandTable[] = {\r
2190 {"help", CmdHelp, 1, "This help"},\r
2191 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
2192 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
2193 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
2194 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
2195 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
2196 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
2197 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
2198 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
2199 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
c188b1b9 2200 {"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},\r
f1db8c22 2201 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
2202 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
2203 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
2204 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
2205 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
2206 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
2207 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
2208 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
2209 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
2210 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
2211 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},\r
2212 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},\r
2213 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},\r
2214 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
2215 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
2216 {"decrypt", CmdHf14MfDecryptBytes, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r
2217 {NULL, NULL, 0, NULL}\r
9ca155ba
M
2218};\r
2219\r
f1db8c22 2220int CmdHFMF(const char *Cmd) {\r
9ca155ba 2221 // flush\r
28415b5d 2222 clearCommandBuffer();\r
2223 //WaitForResponseTimeout(CMD_ACK,NULL,100);\r
c2731f37 2224 CmdsParse(CommandTable, Cmd);\r
2225 return 0;\r
9ca155ba
M
2226}\r
2227\r
f1db8c22 2228int CmdHelp(const char *Cmd) {\r
c2731f37 2229 CmdsHelp(CommandTable);\r
2230 return 0;\r
9ca155ba 2231}\r
Impressum, Datenschutz