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