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