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