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