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