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