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