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