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