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