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