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