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