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