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