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