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