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