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