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