]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
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
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
12 #include "cmdhfmfhard.h"
13 #include "nonce2key/nonce2key.h"
15 static int CmdHelp(const char *Cmd
);
16 int usage_hf14_mifare(void){
17 PrintAndLog("Usage: hf mf mifare [h] <block number>");
18 PrintAndLog("options:");
19 PrintAndLog(" h this help");
20 PrintAndLog(" <block number> (Optional) target other key A than block 0.");
21 PrintAndLog("samples:");
22 PrintAndLog(" hf mf mifare");
23 PrintAndLog(" hf mf mifare 16");
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");
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");
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");
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");
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");
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]");
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?");
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");
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");
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");
115 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
116 PrintAndLog(" d write keys to binary file");
117 PrintAndLog(" t write keys to emulator memory\n");
119 PrintAndLog("samples:");
120 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic");
121 PrintAndLog(" hf mf chk *1 ? t");
122 PrintAndLog(" hf mf chk *1 ? d");
126 int CmdHF14AMifare(const char *Cmd
) {
128 uint32_t nt
= 0, nr
= 0;
129 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
134 char cmdp
= param_getchar(Cmd
, 0);
135 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
137 blockNo
= param_get8(Cmd
, 0);
138 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, 0}};
141 printf("-------------------------------------------------------------------------\n");
142 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
143 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
144 printf("-------------------------------------------------------------------------\n");
145 clock_t t1
= clock();
150 clearCommandBuffer();
166 printf("\naborted via keyboard!\n");
171 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
174 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
175 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
176 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
177 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
178 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
181 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
182 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
183 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
184 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
185 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
194 if (isOK
== -1 && par_list
== 0) {
195 if (!nonce2key_ex(uid
, nt
, nr
, ks_list
, &r_key
) ){
196 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
202 if (isOK
!= 1) return 1;
204 // execute original function from util nonce2key
205 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
207 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
208 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
212 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
217 unsigned long elapsed_time
= difftime(end
, start
);
219 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
223 int CmdHF14AMfWrBl(const char *Cmd
) {
226 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
227 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
232 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
233 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
237 blockNo
= param_get8(Cmd
, 0);
238 cmdp
= param_getchar(Cmd
, 1);
240 PrintAndLog("Key type must be A or B");
243 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
244 if (param_gethex(Cmd
, 2, key
, 12)) {
245 PrintAndLog("Key must include 12 HEX symbols");
248 if (param_gethex(Cmd
, 3, bldata
, 32)) {
249 PrintAndLog("Block data must include 32 HEX symbols");
252 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
253 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
255 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
256 memcpy(c
.d
.asBytes
, key
, 6);
257 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
258 clearCommandBuffer();
262 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
263 uint8_t isOK
= resp
.arg
[0] & 0xff;
264 PrintAndLog("isOk:%02x", isOK
);
266 PrintAndLog("Command execute timeout");
272 int CmdHF14AMfRdBl(const char *Cmd
) {
275 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
281 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
282 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
286 blockNo
= param_get8(Cmd
, 0);
287 cmdp
= param_getchar(Cmd
, 1);
289 PrintAndLog("Key type must be A or B");
292 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
293 if (param_gethex(Cmd
, 2, key
, 12)) {
294 PrintAndLog("Key must include 12 HEX symbols");
297 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
299 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
300 memcpy(c
.d
.asBytes
, key
, 6);
301 clearCommandBuffer();
305 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
306 uint8_t isOK
= resp
.arg
[0] & 0xff;
307 uint8_t *data
= resp
.d
.asBytes
;
310 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
312 PrintAndLog("isOk:%02x", isOK
);
314 PrintAndLog("Command execute timeout");
320 int CmdHF14AMfRdSc(const char *Cmd
) {
322 uint8_t sectorNo
= 0;
324 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
326 uint8_t *data
= NULL
;
330 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
331 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
335 sectorNo
= param_get8(Cmd
, 0);
337 PrintAndLog("Sector number must be less than 40");
340 cmdp
= param_getchar(Cmd
, 1);
341 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
342 PrintAndLog("Key type must be A or B");
345 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
346 if (param_gethex(Cmd
, 2, key
, 12)) {
347 PrintAndLog("Key must include 12 HEX symbols");
350 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
352 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
353 memcpy(c
.d
.asBytes
, key
, 6);
354 clearCommandBuffer();
359 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
360 isOK
= resp
.arg
[0] & 0xff;
361 data
= resp
.d
.asBytes
;
363 PrintAndLog("isOk:%02x", isOK
);
365 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
366 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
368 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
371 PrintAndLog("Command execute timeout");
377 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
381 return 32 * 4 + (sectorNo
- 32) * 16;
385 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
393 int CmdHF14AMfDump(const char *Cmd
) {
394 uint8_t sectorNo
, blockNo
;
398 uint8_t rights
[40][4];
399 uint8_t carddata
[256][16];
400 uint8_t numSectors
= 16;
407 char cmdp
= param_getchar(Cmd
, 0);
409 case '0' : numSectors
= 5; break;
411 case '\0': numSectors
= 16; break;
412 case '2' : numSectors
= 32; break;
413 case '4' : numSectors
= 40; break;
414 default: numSectors
= 16;
417 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
418 PrintAndLog("Usage: hf mf dump [card memory]");
419 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
421 PrintAndLog("Samples: hf mf dump");
422 PrintAndLog(" hf mf dump 4");
426 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
427 PrintAndLog("Could not find file dumpkeys.bin");
431 // Read keys A from file
433 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
434 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
435 if ( bytes_read
== 0) {
436 PrintAndLog("File reading error.");
442 // Read keys B from file
443 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
444 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
445 if ( bytes_read
== 0) {
446 PrintAndLog("File reading error.");
454 PrintAndLog("|-----------------------------------------|");
455 PrintAndLog("|------ Reading sector access bits...-----|");
456 PrintAndLog("|-----------------------------------------|");
458 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
459 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
460 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
461 clearCommandBuffer();
464 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
465 uint8_t isOK
= resp
.arg
[0] & 0xff;
466 uint8_t *data
= resp
.d
.asBytes
;
468 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
469 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
470 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
471 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
473 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
474 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
475 rights
[sectorNo
][3] = 0x01;
478 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
479 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
480 rights
[sectorNo
][3] = 0x01;
484 PrintAndLog("|-----------------------------------------|");
485 PrintAndLog("|----- Dumping all blocks to file... -----|");
486 PrintAndLog("|-----------------------------------------|");
489 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
490 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
491 bool received
= false;
493 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
494 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
495 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
496 clearCommandBuffer();
498 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
499 } else { // data block. Check if it can be read with key A or key B
500 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
501 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
502 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
503 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
505 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
506 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
508 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
509 } else { // key A would work
510 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
511 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
512 clearCommandBuffer();
514 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
519 isOK
= resp
.arg
[0] & 0xff;
520 uint8_t *data
= resp
.d
.asBytes
;
521 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
522 data
[0] = (keyA
[sectorNo
][0]);
523 data
[1] = (keyA
[sectorNo
][1]);
524 data
[2] = (keyA
[sectorNo
][2]);
525 data
[3] = (keyA
[sectorNo
][3]);
526 data
[4] = (keyA
[sectorNo
][4]);
527 data
[5] = (keyA
[sectorNo
][5]);
528 data
[10] = (keyB
[sectorNo
][0]);
529 data
[11] = (keyB
[sectorNo
][1]);
530 data
[12] = (keyB
[sectorNo
][2]);
531 data
[13] = (keyB
[sectorNo
][3]);
532 data
[14] = (keyB
[sectorNo
][4]);
533 data
[15] = (keyB
[sectorNo
][5]);
536 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
537 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
539 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
545 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
552 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
553 PrintAndLog("Could not create file name dumpdata.bin");
556 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
557 fwrite(carddata
, 1, 16*numblocks
, fout
);
559 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
565 int CmdHF14AMfRestore(const char *Cmd
) {
566 uint8_t sectorNo
,blockNo
;
568 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
569 uint8_t bldata
[16] = {0x00};
577 char cmdp
= param_getchar(Cmd
, 0);
579 case '0' : numSectors
= 5; break;
581 case '\0': numSectors
= 16; break;
582 case '2' : numSectors
= 32; break;
583 case '4' : numSectors
= 40; break;
584 default: numSectors
= 16;
587 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
588 PrintAndLog("Usage: hf mf restore [card memory]");
589 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
591 PrintAndLog("Samples: hf mf restore");
592 PrintAndLog(" hf mf restore 4");
596 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
597 PrintAndLog("Could not find file dumpkeys.bin");
602 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
603 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
604 if ( bytes_read
== 0) {
605 PrintAndLog("File reading error (dumpkeys.bin).");
611 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
612 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
613 if ( bytes_read
== 0) {
614 PrintAndLog("File reading error (dumpkeys.bin).");
622 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
623 PrintAndLog("Could not find file dumpdata.bin");
626 PrintAndLog("Restoring dumpdata.bin to card");
628 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
629 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
630 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
631 memcpy(c
.d
.asBytes
, key
, 6);
632 bytes_read
= fread(bldata
, 1, 16, fdump
);
633 if ( bytes_read
== 0) {
634 PrintAndLog("File reading error (dumpdata.bin).");
639 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
640 bldata
[0] = (keyA
[sectorNo
][0]);
641 bldata
[1] = (keyA
[sectorNo
][1]);
642 bldata
[2] = (keyA
[sectorNo
][2]);
643 bldata
[3] = (keyA
[sectorNo
][3]);
644 bldata
[4] = (keyA
[sectorNo
][4]);
645 bldata
[5] = (keyA
[sectorNo
][5]);
646 bldata
[10] = (keyB
[sectorNo
][0]);
647 bldata
[11] = (keyB
[sectorNo
][1]);
648 bldata
[12] = (keyB
[sectorNo
][2]);
649 bldata
[13] = (keyB
[sectorNo
][3]);
650 bldata
[14] = (keyB
[sectorNo
][4]);
651 bldata
[15] = (keyB
[sectorNo
][5]);
654 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
656 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
657 clearCommandBuffer();
661 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
662 uint8_t isOK
= resp
.arg
[0] & 0xff;
663 PrintAndLog("isOk:%02x", isOK
);
665 PrintAndLog("Command execute timeout");
674 int CmdHF14AMfNested(const char *Cmd
) {
675 int i
, j
, res
, iterations
;
676 sector
*e_sector
= NULL
;
679 uint8_t trgBlockNo
= 0;
680 uint8_t trgKeyType
= 0;
681 uint8_t SectorsCnt
= 0;
682 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
683 uint8_t keyBlock
[6*6];
685 bool transferToEml
= false;
687 bool createDumpFile
= false;
689 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
690 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
692 if (strlen(Cmd
)<3) return usage_hf14_nested();
695 cmdp
= param_getchar(Cmd
, 0);
696 blockNo
= param_get8(Cmd
, 1);
697 ctmp
= param_getchar(Cmd
, 2);
699 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
700 PrintAndLog("Key type must be A or B");
704 if (ctmp
!= 'A' && ctmp
!= 'a')
707 if (param_gethex(Cmd
, 3, key
, 12)) {
708 PrintAndLog("Key must include 12 HEX symbols");
712 if (cmdp
== 'o' || cmdp
== 'O') {
714 trgBlockNo
= param_get8(Cmd
, 4);
715 ctmp
= param_getchar(Cmd
, 5);
716 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
717 PrintAndLog("Target key type must be A or B");
720 if (ctmp
!= 'A' && ctmp
!= 'a')
725 case '0': SectorsCnt
= 05; break;
726 case '1': SectorsCnt
= 16; break;
727 case '2': SectorsCnt
= 32; break;
728 case '4': SectorsCnt
= 40; break;
729 default: SectorsCnt
= 16;
733 ctmp
= param_getchar(Cmd
, 4);
734 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
735 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
737 ctmp
= param_getchar(Cmd
, 6);
738 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
739 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
742 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
744 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
745 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
746 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
747 case -4 : PrintAndLog("No valid key found"); break;
749 key64
= bytes_to_num(keyBlock
, 6);
751 // transfer key to the emulator
753 uint8_t sectortrailer
;
754 if (trgBlockNo
< 32*4) { // 4 block sector
755 sectortrailer
= (trgBlockNo
& 0x03) + 3;
756 } else { // 16 block sector
757 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
759 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
762 num_to_bytes(key64
, 6, keyBlock
);
764 num_to_bytes(key64
, 6, &keyBlock
[10]);
765 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
768 default : PrintAndLog("Unknown Error.\n");
772 else { // ------------------------------------ multiple sectors working
773 clock_t t1
= clock();
774 unsigned long elapsed_time
;
778 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
779 if (e_sector
== NULL
) return 1;
781 //test current key and additional standard keys first
782 memcpy(keyBlock
, key
, 6);
783 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
784 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
785 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
786 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
787 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
789 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
790 for (i
= 0; i
< SectorsCnt
; i
++) {
791 for (j
= 0; j
< 2; j
++) {
792 if (e_sector
[i
].foundKey
[j
]) continue;
794 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
797 e_sector
[i
].Key
[j
] = key64
;
798 e_sector
[i
].foundKey
[j
] = TRUE
;
802 clock_t t2
= clock() - t1
;
804 elapsed_time
= difftime(end
, start
);
806 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
808 PrintAndLog("enter nested...");
812 bool calibrate
= true;
814 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
815 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
816 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
818 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
820 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
822 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
823 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
824 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
825 case -4 : //key not found
832 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
833 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
836 default : PrintAndLog("Unknown Error.\n");
846 elapsed_time
= difftime(end
, start
);
848 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
851 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
852 PrintAndLog("trying to read key B...");
853 for (i
= 0; i
< SectorsCnt
; i
++) {
854 // KEY A but not KEY B
855 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
857 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
859 PrintAndLog("Reading block %d", sectrail
);
861 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
862 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
863 clearCommandBuffer();
867 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
869 uint8_t isOK
= resp
.arg
[0] & 0xff;
872 uint8_t *data
= resp
.d
.asBytes
;
873 key64
= bytes_to_num(data
+10, 6);
875 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
876 e_sector
[i
].foundKey
[1] = TRUE
;
877 e_sector
[i
].Key
[1] = key64
;
884 printKeyTable( SectorsCnt
, e_sector
);
886 // transfer them to the emulator
888 for (i
= 0; i
< SectorsCnt
; i
++) {
889 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
890 if (e_sector
[i
].foundKey
[0])
891 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
892 if (e_sector
[i
].foundKey
[1])
893 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
894 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
899 if (createDumpFile
) {
900 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
901 PrintAndLog("Could not create file dumpkeys.bin");
905 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
906 for(i
=0; i
<SectorsCnt
; i
++) {
907 if (e_sector
[i
].foundKey
[0]){
908 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
909 fwrite ( tempkey
, 1, 6, fkeys
);
912 fwrite ( &standart
, 1, 6, fkeys
);
915 for(i
=0; i
<SectorsCnt
; i
++) {
916 if (e_sector
[i
].foundKey
[1]){
917 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
918 fwrite ( tempkey
, 1, 6, fkeys
);
921 fwrite ( &standart
, 1, 6, fkeys
);
932 int CmdHF14AMfNestedHard(const char *Cmd
) {
935 uint8_t trgBlockNo
= 0;
936 uint8_t trgKeyType
= 0;
937 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
938 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
941 ctmp
= param_getchar(Cmd
, 0);
942 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
943 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
945 bool know_target_key
= false;
946 bool nonce_file_read
= false;
947 bool nonce_file_write
= false;
951 if (ctmp
== 'R' || ctmp
== 'r') {
952 nonce_file_read
= true;
953 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
954 know_target_key
= true;
956 } else if (ctmp
== 'T' || ctmp
== 't') {
957 tests
= param_get32ex(Cmd
, 1, 100, 10);
959 blockNo
= param_get8(Cmd
, 0);
960 ctmp
= param_getchar(Cmd
, 1);
961 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
962 PrintAndLog("Key type must be A or B");
965 if (ctmp
!= 'A' && ctmp
!= 'a') {
969 if (param_gethex(Cmd
, 2, key
, 12)) {
970 PrintAndLog("Key must include 12 HEX symbols");
974 trgBlockNo
= param_get8(Cmd
, 3);
975 ctmp
= param_getchar(Cmd
, 4);
976 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
977 PrintAndLog("Target key type must be A or B");
980 if (ctmp
!= 'A' && ctmp
!= 'a') {
986 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
987 know_target_key
= true;
991 while ((ctmp
= param_getchar(Cmd
, i
))) {
992 if (ctmp
== 's' || ctmp
== 'S') {
994 } else if (ctmp
== 'w' || ctmp
== 'W') {
995 nonce_file_write
= true;
997 PrintAndLog("Possible options are w and/or s");
1004 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 ",
1007 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1008 know_target_key
? "" : " (not set)",
1009 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1010 slow
? "Yes" : "No",
1013 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1017 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1018 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1027 int CmdHF14AMfChk(const char *Cmd
) {
1029 if (strlen(Cmd
)<3) return usage_hf14_chk();
1032 char filename
[FILE_PATH_SIZE
]={0};
1034 uint8_t *keyBlock
= NULL
, *p
;
1035 uint8_t stKeyBlock
= 20;
1037 sector
*e_sector
= NULL
;
1042 uint8_t blockNo
= 0;
1043 uint8_t SectorsCnt
= 1;
1044 uint8_t keyType
= 0;
1047 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1049 int transferToEml
= 0;
1050 int createDumpFile
= 0;
1052 keyBlock
= calloc(stKeyBlock
, 6);
1053 if (keyBlock
== NULL
) return 1;
1055 uint64_t defaultKeys
[] = {
1056 0xffffffffffff, // Default key (first key used by program if no user defined key)
1057 0x000000000000, // Blank key
1058 0xa0a1a2a3a4a5, // NFCForum MAD key
1070 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1072 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1073 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1076 if (param_getchar(Cmd
, 0)=='*') {
1078 switch(param_getchar(Cmd
+1, 0)) {
1079 case '0': SectorsCnt
= 5; break;
1080 case '1': SectorsCnt
= 16; break;
1081 case '2': SectorsCnt
= 32; break;
1082 case '4': SectorsCnt
= 40; break;
1083 default: SectorsCnt
= 16;
1086 blockNo
= param_get8(Cmd
, 0);
1089 ctmp
= param_getchar(Cmd
, 1);
1101 PrintAndLog("Key type must be A , B or ?");
1106 ctmp
= param_getchar(Cmd
, 2);
1107 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1108 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1110 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1111 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1112 if ( stKeyBlock
- keycnt
< 2) {
1113 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1115 PrintAndLog("Cannot allocate memory for Keys");
1121 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1122 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1123 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1126 // May be a dic file
1127 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1128 PrintAndLog("File name too long");
1133 if ( (f
= fopen( filename
, "r")) ) {
1134 while( fgets(buf
, sizeof(buf
), f
) ){
1135 if (strlen(buf
) < 12 || buf
[11] == '\n')
1138 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1140 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1142 if (!isxdigit(buf
[0])){
1143 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1149 if ( stKeyBlock
- keycnt
< 2) {
1150 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1152 PrintAndLog("Cannot allocate memory for defKeys");
1159 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1160 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1161 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1163 memset(buf
, 0, sizeof(buf
));
1167 PrintAndLog("File: %s: not found or locked.", filename
);
1176 PrintAndLog("No key specified, trying default keys");
1177 for (;keycnt
< defaultKeysSize
; keycnt
++)
1178 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1179 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1180 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1183 // initialize storage for found keys
1184 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1185 if (e_sector
== NULL
) {
1191 for(int i
= 0; i
< SectorsCnt
; ++i
){
1192 e_sector
[i
].Key
[0] = 0xffffffffffff;
1193 e_sector
[i
].Key
[1] = 0xffffffffffff;
1194 e_sector
[i
].foundKey
[0] = FALSE
;
1195 e_sector
[i
].foundKey
[1] = FALSE
;
1199 uint8_t trgKeyType
= 0;
1200 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1203 clock_t t1
= clock();
1208 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1211 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1213 // skip already found keys.
1214 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1216 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1218 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1220 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1222 e_sector
[i
].Key
[trgKeyType
] = key64
;
1223 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1229 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1234 unsigned long elapsed_time
= difftime(end
, start
);
1236 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1239 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1240 if ( keyType
!= 1 ) {
1241 PrintAndLog("testing to read key B...");
1242 for (i
= 0; i
< SectorsCnt
; i
++) {
1243 // KEY A but not KEY B
1244 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1246 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1248 PrintAndLog("Reading block %d", sectrail
);
1250 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1251 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1252 clearCommandBuffer();
1256 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1258 uint8_t isOK
= resp
.arg
[0] & 0xff;
1259 if (!isOK
) continue;
1261 uint8_t *data
= resp
.d
.asBytes
;
1262 key64
= bytes_to_num(data
+10, 6);
1264 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1265 e_sector
[i
].foundKey
[1] = 1;
1266 e_sector
[i
].Key
[1] = key64
;
1274 printKeyTable( SectorsCnt
, e_sector
);
1276 if (transferToEml
) {
1277 uint8_t block
[16] = {0x00};
1278 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1279 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1280 if (e_sector
[i
].foundKey
[0])
1281 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1282 if (e_sector
[i
].foundKey
[1])
1283 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1284 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1286 PrintAndLog("Found keys have been transferred to the emulator memory");
1289 if (createDumpFile
) {
1290 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1291 if (fkeys
== NULL
) {
1292 PrintAndLog("Could not create file dumpkeys.bin");
1297 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1299 for( i
=0; i
<SectorsCnt
; i
++) {
1300 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1301 fwrite ( tempkey
, 1, 6, fkeys
);
1303 for(i
=0; i
<SectorsCnt
; i
++) {
1304 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1305 fwrite ( tempkey
, 1, 6, fkeys
);
1308 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1317 int CmdHF14AMf1kSim(const char *Cmd
) {
1318 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1319 uint8_t exitAfterNReads
= 0;
1320 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1323 uint8_t cmdp
= param_getchar(Cmd
, 0);
1325 if (cmdp
== 'h' || cmdp
== 'H') return usage_hf14_mf1ksim();
1327 cmdp
= param_getchar(Cmd
, pnr
);
1328 if (cmdp
== 'u' || cmdp
== 'U') {
1329 param_gethex_ex(Cmd
, pnr
+1, uid
, &uidlen
);
1331 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1332 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1333 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1334 default: return usage_hf14_mf1ksim();
1339 cmdp
= param_getchar(Cmd
, pnr
);
1340 if (cmdp
== 'n' || cmdp
== 'N') {
1341 exitAfterNReads
= param_get8(Cmd
, pnr
+1);
1345 cmdp
= param_getchar(Cmd
, pnr
);
1346 if (cmdp
== 'i' || cmdp
== 'I' ) {
1347 flags
|= FLAG_INTERACTIVE
;
1351 cmdp
= param_getchar(Cmd
, pnr
);
1352 if (cmdp
== 'x' || cmdp
== 'X') {
1353 flags
|= FLAG_NR_AR_ATTACK
;
1356 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1357 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1362 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1363 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1364 clearCommandBuffer();
1367 if(flags
& FLAG_INTERACTIVE
) {
1371 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1373 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1375 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1376 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1378 memset(data
, 0x00, sizeof(data
));
1379 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1381 memcpy(data
, resp
.d
.asBytes
, len
);
1383 bool found
= tryMfk32(data
, &key
);
1384 found
^= tryMfk32_moebius(data
, &key
);
1391 int CmdHF14AMfSniff(const char *Cmd
){
1392 bool wantLogToFile
= FALSE
;
1393 bool wantDecrypt
= FALSE
;
1394 //bool wantSaveToEml = FALSE; TODO
1395 bool wantSaveToEmlFile
= FALSE
;
1405 uint8_t uid_len
= 0;
1406 uint8_t atqa
[2] = {0x00, 0x00};
1409 uint8_t *buf
= NULL
;
1410 uint16_t bufsize
= 0;
1411 uint8_t *bufPtr
= NULL
;
1412 uint16_t traceLen
= 0;
1414 memset(uid
, 0x00, sizeof(uid
));
1416 char ctmp
= param_getchar(Cmd
, 0);
1417 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1419 for (int i
= 0; i
< 4; i
++) {
1420 ctmp
= param_getchar(Cmd
, i
);
1421 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1422 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1423 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1424 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1427 printf("-------------------------------------------------------------------------\n");
1428 printf("Executing mifare sniffing command. \n");
1429 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1430 printf("Press the key on pc keyboard to abort the client.\n");
1431 printf("-------------------------------------------------------------------------\n");
1433 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1434 clearCommandBuffer();
1442 tmpchar
= getchar();
1444 printf("\naborted via keyboard!\n");
1449 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1450 res
= resp
.arg
[0] & 0xff;
1451 traceLen
= resp
.arg
[1];
1460 if (res
== 1) { // there is (more) data to be transferred
1461 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1462 if (traceLen
> bufsize
) {
1464 if (buf
== NULL
) // not yet allocated
1465 p
= malloc(traceLen
);
1466 else // need more memory
1467 p
= realloc(buf
, traceLen
);
1470 PrintAndLog("Cannot allocate memory for trace");
1478 memset(buf
, 0x00, traceLen
);
1480 if (bufPtr
== NULL
) {
1481 PrintAndLog("Cannot allocate memory for trace");
1485 // what happens if LEN is bigger then TRACELEN --iceman
1486 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1491 if (res
== 2) { // received all data, start displaying
1492 blockLen
= bufPtr
- buf
;
1495 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1496 while (bufPtr
- buf
< blockLen
) {
1497 bufPtr
+= 6; // skip (void) timing information
1498 len
= *((uint16_t *)bufPtr
);
1506 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1507 memcpy(uid
, bufPtr
+ 2, 10);
1508 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1509 switch (atqa
[0] & 0xC0) {
1510 case 0x80: uid_len
= 10; break;
1511 case 0x40: uid_len
= 7; break;
1512 default: uid_len
= 4; break;
1515 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1516 sprint_hex(uid
, uid_len
),
1520 if (wantLogToFile
|| wantDecrypt
) {
1521 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1522 AddLogCurrentDT(logHexFileName
);
1525 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1527 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1529 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1531 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1535 bufPtr
+= ((len
-1)/8+1); // ignore parity
1546 int CmdHF14AMfDbg(const char *Cmd
) {
1548 char ctmp
= param_getchar(Cmd
, 0);
1549 if (strlen(Cmd
) < 1 || ctmp
== 'h'|| ctmp
== 'H') return usage_hf14_dbg();
1551 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1552 if (dbgMode
> 4) return usage_hf14_dbg();
1554 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1559 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1560 PrintAndLog("|---|----------------|---|----------------|---|");
1561 PrintAndLog("|sec|key A |res|key B |res|");
1562 PrintAndLog("|---|----------------|---|----------------|---|");
1563 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1564 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1565 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1566 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1569 PrintAndLog("|---|----------------|---|----------------|---|");
1572 // EMULATOR COMMANDS
1574 int CmdHF14AMfEGet(const char *Cmd
)
1576 uint8_t blockNo
= 0;
1577 uint8_t data
[16] = {0x00};
1579 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1580 PrintAndLog("Usage: hf mf eget <block number>");
1581 PrintAndLog(" sample: hf mf eget 0 ");
1585 blockNo
= param_get8(Cmd
, 0);
1588 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1589 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1591 PrintAndLog("Command execute timeout");
1597 int CmdHF14AMfEClear(const char *Cmd
)
1599 if (param_getchar(Cmd
, 0) == 'h') {
1600 PrintAndLog("Usage: hf mf eclr");
1601 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1605 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1610 int CmdHF14AMfESet(const char *Cmd
)
1612 uint8_t memBlock
[16];
1613 uint8_t blockNo
= 0;
1615 memset(memBlock
, 0x00, sizeof(memBlock
));
1617 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1618 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1619 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1623 blockNo
= param_get8(Cmd
, 0);
1625 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1626 PrintAndLog("block data must include 32 HEX symbols");
1631 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1632 memcpy(c
.d
.asBytes
, memBlock
, 16);
1637 int CmdHF14AMfELoad(const char *Cmd
)
1640 char filename
[FILE_PATH_SIZE
];
1641 char *fnameptr
= filename
;
1642 char buf
[64] = {0x00};
1643 uint8_t buf8
[64] = {0x00};
1644 int i
, len
, blockNum
, numBlocks
;
1645 int nameParamNo
= 1;
1646 uint8_t blockWidth
= 32;
1647 char ctmp
= param_getchar(Cmd
, 0);
1649 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1650 PrintAndLog("It loads emul dump from the file `filename.eml`");
1651 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1652 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1654 PrintAndLog(" sample: hf mf eload filename");
1655 PrintAndLog(" hf mf eload 4 filename");
1660 case '0' : numBlocks
= 5*4; break;
1662 case '\0': numBlocks
= 16*4; break;
1663 case '2' : numBlocks
= 32*4; break;
1664 case '4' : numBlocks
= 256; break;
1665 case 'U' : // fall through
1666 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1672 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1673 if (numblk2
> 0) numBlocks
= numblk2
;
1675 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1677 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1681 sprintf(fnameptr
, ".eml");
1684 f
= fopen(filename
, "r");
1686 PrintAndLog("File %s not found or locked", filename
);
1692 memset(buf
, 0, sizeof(buf
));
1694 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1696 if (blockNum
>= numBlocks
) break;
1698 PrintAndLog("File reading error.");
1703 if (strlen(buf
) < blockWidth
){
1704 if(strlen(buf
) && feof(f
))
1706 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1711 for (i
= 0; i
< blockWidth
; i
+= 2) {
1712 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1714 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1715 PrintAndLog("Cant set emul block: %3d", blockNum
);
1722 if (blockNum
>= numBlocks
) break;
1727 if ((blockNum
!= numBlocks
)) {
1728 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1731 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1735 int CmdHF14AMfESave(const char *Cmd
)
1738 char filename
[FILE_PATH_SIZE
];
1739 char * fnameptr
= filename
;
1741 int i
, j
, len
, numBlocks
;
1742 int nameParamNo
= 1;
1744 memset(filename
, 0, sizeof(filename
));
1745 memset(buf
, 0, sizeof(buf
));
1747 char ctmp
= param_getchar(Cmd
, 0);
1749 if ( ctmp
== 'h' || ctmp
== 'H') {
1750 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1751 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1752 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1754 PrintAndLog(" sample: hf mf esave ");
1755 PrintAndLog(" hf mf esave 4");
1756 PrintAndLog(" hf mf esave 4 filename");
1761 case '0' : numBlocks
= 5*4; break;
1763 case '\0': numBlocks
= 16*4; break;
1764 case '2' : numBlocks
= 32*4; break;
1765 case '4' : numBlocks
= 256; break;
1772 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1774 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1776 // user supplied filename?
1778 // get filename (UID from memory)
1779 if (mfEmlGetMem(buf
, 0, 1)) {
1780 PrintAndLog("Can\'t get UID from block: %d", 0);
1781 len
= sprintf(fnameptr
, "dump");
1785 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1786 sprintf(fnameptr
, "%02X", buf
[j
]);
1792 // add file extension
1793 sprintf(fnameptr
, ".eml");
1796 f
= fopen(filename
, "w+");
1799 PrintAndLog("Can't open file %s ", filename
);
1804 for (i
= 0; i
< numBlocks
; i
++) {
1805 if (mfEmlGetMem(buf
, i
, 1)) {
1806 PrintAndLog("Cant get block: %d", i
);
1809 for (j
= 0; j
< 16; j
++)
1810 fprintf(f
, "%02X", buf
[j
]);
1815 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1820 int CmdHF14AMfECFill(const char *Cmd
)
1822 uint8_t keyType
= 0;
1823 uint8_t numSectors
= 16;
1825 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1826 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1827 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1829 PrintAndLog("samples: hf mf ecfill A");
1830 PrintAndLog(" hf mf ecfill A 4");
1831 PrintAndLog("Read card and transfer its data to emulator memory.");
1832 PrintAndLog("Keys must be laid in the emulator memory. \n");
1836 char ctmp
= param_getchar(Cmd
, 0);
1837 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1838 PrintAndLog("Key type must be A or B");
1841 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1843 ctmp
= param_getchar(Cmd
, 1);
1845 case '0' : numSectors
= 5; break;
1847 case '\0': numSectors
= 16; break;
1848 case '2' : numSectors
= 32; break;
1849 case '4' : numSectors
= 40; break;
1850 default: numSectors
= 16;
1853 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1854 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1859 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1864 uint64_t keyA
, keyB
;
1866 char cmdp
= param_getchar(Cmd
, 0);
1868 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1869 PrintAndLog("It prints the keys loaded in the emulator memory");
1870 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1871 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1873 PrintAndLog(" sample: hf mf ekeyprn 1");
1878 case '0' : numSectors
= 5; break;
1880 case '\0': numSectors
= 16; break;
1881 case '2' : numSectors
= 32; break;
1882 case '4' : numSectors
= 40; break;
1883 default: numSectors
= 16;
1886 PrintAndLog("|---|----------------|----------------|");
1887 PrintAndLog("|sec|key A |key B |");
1888 PrintAndLog("|---|----------------|----------------|");
1889 for (i
= 0; i
< numSectors
; i
++) {
1890 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1891 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1894 keyA
= bytes_to_num(data
, 6);
1895 keyB
= bytes_to_num(data
+ 10, 6);
1896 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1898 PrintAndLog("|---|----------------|----------------|");
1903 // CHINESE MAGIC COMMANDS
1905 int CmdHF14AMfCSetUID(const char *Cmd
) {
1906 uint8_t wipeCard
= 0;
1907 uint8_t uid
[8] = {0x00};
1908 uint8_t oldUid
[8] = {0x00};
1909 uint8_t atqa
[2] = {0x00};
1910 uint8_t sak
[1] = {0x00};
1911 uint8_t atqaPresent
= 1;
1916 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1917 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1918 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1920 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1922 PrintAndLog("sample: hf mf csetuid 01020304");
1923 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
1927 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1928 PrintAndLog("UID must include 8 HEX symbols");
1933 ctmp
= param_getchar(Cmd
, argi
);
1934 if (ctmp
== 'w' || ctmp
== 'W') {
1940 if (param_getchar(Cmd
, argi
)) {
1941 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1942 PrintAndLog("ATQA must include 4 HEX symbols");
1946 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1947 PrintAndLog("SAK must include 2 HEX symbols");
1956 ctmp
= param_getchar(Cmd
, argi
);
1957 if (ctmp
== 'w' || ctmp
== 'W') {
1962 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1964 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
1966 PrintAndLog("Can't set UID. error=%d", res
);
1970 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1971 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1975 int CmdHF14AMfCSetBlk(const char *Cmd
) {
1976 uint8_t block
[16] = {0x00};
1977 uint8_t blockNo
= 0;
1978 uint8_t params
= MAGIC_SINGLE
;
1981 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1982 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1983 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1984 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1985 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1989 blockNo
= param_get8(Cmd
, 0);
1991 if (param_gethex(Cmd
, 1, block
, 32)) {
1992 PrintAndLog("block data must include 32 HEX symbols");
1996 char ctmp
= param_getchar(Cmd
, 2);
1997 if (ctmp
== 'w' || ctmp
== 'W')
1998 params
|= MAGIC_WIPE
;
2000 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2002 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2004 PrintAndLog("Can't write block. error=%d", res
);
2010 int CmdHF14AMfCLoad(const char *Cmd
) {
2012 char filename
[FILE_PATH_SIZE
];
2013 char * fnameptr
= filename
;
2014 char buf
[64] = {0x00};
2015 uint8_t buf8
[64] = {0x00};
2016 uint8_t fillFromEmulator
= 0;
2017 int i
, len
, blockNum
, flags
=0;
2019 memset(filename
, 0, sizeof(filename
));
2021 char ctmp
= param_getchar(Cmd
, 0);
2023 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2024 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2025 PrintAndLog("or from emulator memory (option `e`)");
2026 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2027 PrintAndLog(" or: hf mf cload e ");
2028 PrintAndLog(" sample: hf mf cload filename");
2032 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2034 if (fillFromEmulator
) {
2035 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2036 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2037 PrintAndLog("Cant get block: %d", blockNum
);
2040 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2041 if (blockNum
== 1) flags
= 0; // just write
2042 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2044 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2045 PrintAndLog("Cant set magic card block: %d", blockNum
);
2052 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2054 memcpy(filename
, Cmd
, len
);
2057 sprintf(fnameptr
, ".eml");
2060 f
= fopen(filename
, "r");
2062 PrintAndLog("File not found or locked.");
2069 memset(buf
, 0, sizeof(buf
));
2071 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2073 PrintAndLog("File reading error.");
2077 if (strlen(buf
) < 32) {
2078 if(strlen(buf
) && feof(f
))
2080 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2084 for (i
= 0; i
< 32; i
+= 2)
2085 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2087 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2088 if (blockNum
== 1) flags
= 0; // just write
2089 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2091 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2092 PrintAndLog("Can't set magic card block: %d", blockNum
);
2098 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2103 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2104 PrintAndLog("File content error. There must be 64 blocks");
2107 PrintAndLog("Loaded from file: %s", filename
);
2113 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2115 uint8_t blockNo
= 0;
2117 memset(data
, 0x00, sizeof(data
));
2118 char ctmp
= param_getchar(Cmd
, 0);
2120 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2121 PrintAndLog("Usage: hf mf cgetblk <block number>");
2122 PrintAndLog("sample: hf mf cgetblk 1");
2123 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2127 blockNo
= param_get8(Cmd
, 0);
2129 PrintAndLog("--block number:%2d ", blockNo
);
2131 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2133 PrintAndLog("Can't read block. error=%d", res
);
2137 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2141 int CmdHF14AMfCGetSc(const char *Cmd
) {
2143 uint8_t sectorNo
= 0;
2145 memset(data
, 0x00, sizeof(data
));
2146 char ctmp
= param_getchar(Cmd
, 0);
2148 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2149 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2150 PrintAndLog("sample: hf mf cgetsc 0");
2151 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2155 sectorNo
= param_get8(Cmd
, 0);
2156 if (sectorNo
> 15) {
2157 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2161 PrintAndLog("--sector number:%d ", sectorNo
);
2162 PrintAndLog("block | data");
2164 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2165 for (i
= 0; i
< 4; i
++) {
2166 if (i
== 1) flags
= 0;
2167 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2169 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2171 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2174 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2179 int CmdHF14AMfCSave(const char *Cmd
) {
2182 char filename
[FILE_PATH_SIZE
];
2183 char * fnameptr
= filename
;
2184 uint8_t fillFromEmulator
= 0;
2186 int i
, j
, len
, flags
;
2188 memset(filename
, 0, sizeof(filename
));
2189 memset(buf
, 0, sizeof(buf
));
2190 char ctmp
= param_getchar(Cmd
, 0);
2192 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2193 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2194 PrintAndLog("or into emulator memory (option `e`)");
2195 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2196 PrintAndLog(" sample: hf mf esave ");
2197 PrintAndLog(" hf mf esave filename");
2198 PrintAndLog(" hf mf esave e \n");
2201 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2203 if (fillFromEmulator
) {
2204 // put into emulator
2205 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2206 for (i
= 0; i
< 16 * 4; i
++) {
2207 if (i
== 1) flags
= 0;
2208 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2210 if (mfCGetBlock(i
, buf
, flags
)) {
2211 PrintAndLog("Cant get block: %d", i
);
2215 if (mfEmlSetMem(buf
, i
, 1)) {
2216 PrintAndLog("Cant set emul block: %d", i
);
2223 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2225 // get filename based on UID
2228 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2229 PrintAndLog("Cant get block: %d", 0);
2230 len
= sprintf(fnameptr
, "dump");
2233 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2234 sprintf(fnameptr
, "%02x", buf
[j
]);
2237 memcpy(filename
, Cmd
, len
);
2241 // add .eml extension
2242 sprintf(fnameptr
, ".eml");
2245 f
= fopen(filename
, "w+");
2248 PrintAndLog("File not found or locked.");
2253 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2254 for (i
= 0; i
< 16 * 4; i
++) {
2255 if (i
== 1) flags
= 0;
2256 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2258 if (mfCGetBlock(i
, buf
, flags
)) {
2259 PrintAndLog("Cant get block: %d", i
);
2262 for (j
= 0; j
< 16; j
++)
2263 fprintf(f
, "%02x", buf
[j
]);
2268 PrintAndLog("Saved to file: %s", filename
);
2273 //needs nt, ar, at, Data to decrypt
2274 int CmdHf14MfDecryptBytes(const char *Cmd
){
2276 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2277 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2278 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2281 param_gethex_ex(Cmd
, 3, data
, &len
);
2284 int limit
= sizeof(data
) / 2;
2289 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2292 static command_t CommandTable
[] = {
2293 {"help", CmdHelp
, 1, "This help"},
2294 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2295 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2296 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2297 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2298 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2299 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2300 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2301 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2302 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2303 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2304 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2305 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2306 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2307 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2308 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2309 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2310 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2311 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2312 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2313 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2314 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2315 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2316 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2317 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2318 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2319 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2320 {NULL
, NULL
, 0, NULL
}
2323 int CmdHFMF(const char *Cmd
) {
2324 clearCommandBuffer();
2325 CmdsParse(CommandTable
, Cmd
);
2329 int CmdHelp(const char *Cmd
) {
2330 CmdsHelp(CommandTable
);