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