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