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