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