]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
ac4f1087d131bcba3cd8d21dceadbe54dbbe1876
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011 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 "proxmark3.h"
14 static int CmdHelp(const char *Cmd
);
16 int CmdHF14AMifare(const char *Cmd
)
20 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
22 uint8_t keyBlock
[6] = {0,0,0,0,0,0};
24 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, keyBlock
, 8)) {
25 PrintAndLog("Nt must include 8 HEX symbols");
30 UsbCommand c
= {CMD_READER_MIFARE
, {(uint32_t)bytes_to_num(keyBlock
, 4), 0, 0}};
35 while (ukbhit()) getchar();
38 printf("-------------------------------------------------------------------------\n");
39 printf("Executing command. It may take up to 30 min.\n");
40 printf("Press the key on proxmark3 device to abort proxmark3.\n");
41 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
42 printf("-------------------------------------------------------------------------\n");
50 printf("\naborted via keyboard!\n");
54 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 2000);
56 isOK
= resp
->arg
[0] & 0xff;
58 uid
= (uint32_t)bytes_to_num(resp
->d
.asBytes
+ 0, 4);
59 nt
= (uint32_t)bytes_to_num(resp
->d
.asBytes
+ 4, 4);
60 par_list
= bytes_to_num(resp
->d
.asBytes
+ 8, 8);
61 ks_list
= bytes_to_num(resp
->d
.asBytes
+ 16, 8);
64 PrintAndLog("isOk:%02x", isOK
);
65 if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
72 if (isOK
!= 1) return 1;
74 // execute original function from util nonce2key
75 if (nonce2key(uid
, nt
, par_list
, ks_list
, &r_key
)) return 2;
76 printf("------------------------------------------------------------------\n");
77 PrintAndLog("Key found:%012llx \n", r_key
);
79 num_to_bytes(r_key
, 6, keyBlock
);
80 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
82 PrintAndLog("Found valid key:%012llx", r_key
);
85 PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt
);
93 int CmdHF14AMfWrBl(const char *Cmd
)
97 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
98 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
103 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
104 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
108 blockNo
= param_get8(Cmd
, 0);
109 cmdp
= param_getchar(Cmd
, 1);
111 PrintAndLog("Key type must be A or B");
114 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
115 if (param_gethex(Cmd
, 2, key
, 12)) {
116 PrintAndLog("Key must include 12 HEX symbols");
119 if (param_gethex(Cmd
, 3, bldata
, 32)) {
120 PrintAndLog("Block data must include 32 HEX symbols");
123 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo
, keyType
, sprint_hex(key
, 6));
124 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
126 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
127 memcpy(c
.d
.asBytes
, key
, 6);
128 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
130 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
133 uint8_t isOK
= resp
->arg
[0] & 0xff;
135 PrintAndLog("isOk:%02x", isOK
);
137 PrintAndLog("Command execute timeout");
143 int CmdHF14AMfRdBl(const char *Cmd
)
147 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
153 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
154 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
158 blockNo
= param_get8(Cmd
, 0);
159 cmdp
= param_getchar(Cmd
, 1);
161 PrintAndLog("Key type must be A or B");
164 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
165 if (param_gethex(Cmd
, 2, key
, 12)) {
166 PrintAndLog("Key must include 12 HEX symbols");
169 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo
, keyType
, sprint_hex(key
, 6));
171 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
172 memcpy(c
.d
.asBytes
, key
, 6);
174 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
177 uint8_t isOK
= resp
->arg
[0] & 0xff;
178 uint8_t * data
= resp
->d
.asBytes
;
181 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
183 PrintAndLog("isOk:%02x", isOK
);
185 PrintAndLog("Command execute timeout");
191 int CmdHF14AMfRdSc(const char *Cmd
)
194 uint8_t sectorNo
= 0;
196 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
199 uint8_t * data
= NULL
;
204 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
205 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
209 sectorNo
= param_get8(Cmd
, 0);
211 PrintAndLog("Sector number must be less than 64");
214 cmdp
= param_getchar(Cmd
, 1);
216 PrintAndLog("Key type must be A or B");
219 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
220 if (param_gethex(Cmd
, 2, key
, 12)) {
221 PrintAndLog("Key must include 12 HEX symbols");
224 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo
, keyType
, sprint_hex(key
, 6));
226 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
227 memcpy(c
.d
.asBytes
, key
, 6);
229 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
233 isOK
= resp
->arg
[0] & 0xff;
234 data
= resp
->d
.asBytes
;
236 PrintAndLog("isOk:%02x", isOK
);
238 for (i
= 0; i
< 2; i
++) {
239 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
242 PrintAndLog("Command1 execute timeout");
246 resp
= WaitForResponseTimeout(CMD_ACK
, 500);
250 isOK
= resp
->arg
[0] & 0xff;
251 data
= resp
->d
.asBytes
;
254 for (i
= 0; i
< 2; i
++) {
255 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
258 PrintAndLog("Command2 execute timeout");
264 int CmdHF14AMfDump(const char *Cmd
)
270 uint8_t rights
[40][4];
277 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
278 PrintAndLog("Could not find file dumpkeys.bin");
282 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
283 PrintAndLog("Could not create file name dumpdata.bin");
289 for (i
=0 ; i
<16 ; i
++) {
290 fread ( keyA
[i
], 1, 6, fin
);
292 for (i
=0 ; i
<16 ; i
++) {
293 fread ( keyB
[i
], 1, 6, fin
);
296 // Read access rights to sectors
298 PrintAndLog("|-----------------------------------------|");
299 PrintAndLog("|------ Reading sector access bits...-----|");
300 PrintAndLog("|-----------------------------------------|");
302 for (i
= 0 ; i
< 16 ; i
++) {
303 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
304 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
306 resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
309 uint8_t isOK
= resp
->arg
[0] & 0xff;
310 uint8_t *data
= resp
->d
.asBytes
;
312 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
313 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
314 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
315 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
318 PrintAndLog("Could not get access rights for block %d", i
);
322 PrintAndLog("Command execute timeout");
326 // Read blocks and print to file
328 PrintAndLog("|-----------------------------------------|");
329 PrintAndLog("|----- Dumping all blocks to file... -----|");
330 PrintAndLog("|-----------------------------------------|");
332 for (i
=0 ; i
<16 ; i
++) {
333 for (j
=0 ; j
<4 ; j
++) {
335 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
336 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
338 resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
341 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
342 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
343 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
345 resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
347 else if (rights
[i
][j
] == 7) {
348 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
351 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
352 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
354 resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
359 uint8_t isOK
= resp
->arg
[0] & 0xff;
360 uint8_t *data
= resp
->d
.asBytes
;
362 data
[0] = (keyA
[i
][0]);
363 data
[1] = (keyA
[i
][1]);
364 data
[2] = (keyA
[i
][2]);
365 data
[3] = (keyA
[i
][3]);
366 data
[4] = (keyA
[i
][4]);
367 data
[5] = (keyA
[i
][5]);
368 data
[10] = (keyB
[i
][0]);
369 data
[11] = (keyB
[i
][1]);
370 data
[12] = (keyB
[i
][2]);
371 data
[13] = (keyB
[i
][3]);
372 data
[14] = (keyB
[i
][4]);
373 data
[15] = (keyB
[i
][5]);
376 fwrite ( data
, 1, 16, fout
);
379 PrintAndLog("Could not get access rights for block %d", i
);
383 PrintAndLog("Command execute timeout");
394 int CmdHF14AMfRestore(const char *Cmd
)
399 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
400 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
407 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
408 PrintAndLog("Could not find file dumpdata.bin");
411 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
412 PrintAndLog("Could not find file dumpkeys.bin");
416 for (i
=0 ; i
<16 ; i
++) {
417 fread(keyA
[i
], 1, 6, fkeys
);
419 for (i
=0 ; i
<16 ; i
++) {
420 fread(keyB
[i
], 1, 6, fkeys
);
423 PrintAndLog("Restoring dumpdata.bin to card");
425 for (i
=0 ; i
<16 ; i
++) {
426 for( j
=0 ; j
<4 ; j
++) {
427 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
428 memcpy(c
.d
.asBytes
, key
, 6);
430 fread(bldata
, 1, 16, fdump
);
433 bldata
[0] = (keyA
[i
][0]);
434 bldata
[1] = (keyA
[i
][1]);
435 bldata
[2] = (keyA
[i
][2]);
436 bldata
[3] = (keyA
[i
][3]);
437 bldata
[4] = (keyA
[i
][4]);
438 bldata
[5] = (keyA
[i
][5]);
439 bldata
[10] = (keyB
[i
][0]);
440 bldata
[11] = (keyB
[i
][1]);
441 bldata
[12] = (keyB
[i
][2]);
442 bldata
[13] = (keyB
[i
][3]);
443 bldata
[14] = (keyB
[i
][4]);
444 bldata
[15] = (keyB
[i
][5]);
447 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
450 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
453 if ((ch != 'y') && (ch != 'Y')){
454 PrintAndLog("Aborting !");
459 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
461 UsbCommand
*resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
464 uint8_t isOK
= resp
->arg
[0] & 0xff;
465 PrintAndLog("isOk:%02x", isOK
);
467 PrintAndLog("Command execute timeout");
477 int CmdHF14AMfNested(const char *Cmd
)
479 int i
, j
, res
, iterations
;
480 sector
* e_sector
= NULL
;
483 uint8_t trgBlockNo
= 0;
484 uint8_t trgKeyType
= 0;
487 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
488 uint8_t keyBlock
[16 * 6];
490 int transferToEml
= 0;
492 int createDumpFile
= 0;
494 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
495 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
500 PrintAndLog("Usage:");
501 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
502 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
503 PrintAndLog(" <target block number> <target key A/B> [t]");
504 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
505 PrintAndLog("t - transfer keys into emulator memory");
506 PrintAndLog("d - write keys to binary file");
508 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
509 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
510 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
511 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
515 cmdp
= param_getchar(Cmd
, 0);
516 blockNo
= param_get8(Cmd
, 1);
517 ctmp
= param_getchar(Cmd
, 2);
519 PrintAndLog("Key type must be A or B");
522 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
523 if (param_gethex(Cmd
, 3, key
, 12)) {
524 PrintAndLog("Key must include 12 HEX symbols");
528 if (cmdp
== 'o' || cmdp
== 'O') {
530 trgBlockNo
= param_get8(Cmd
, 4);
531 ctmp
= param_getchar(Cmd
, 5);
533 PrintAndLog("Target key type must be A or B");
536 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
539 case '0': SectorsCnt
= 05; break;
540 case '1': SectorsCnt
= 16; break;
541 case '2': SectorsCnt
= 32; break;
542 case '4': SectorsCnt
= 64; break;
543 default: SectorsCnt
= 16;
547 ctmp
= param_getchar(Cmd
, 4);
548 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
549 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
551 ctmp
= param_getchar(Cmd
, 6);
552 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
553 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
555 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
557 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
560 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) {
561 PrintAndLog("Nested error.");
565 for (i
= 0; i
< 16; i
++) {
566 PrintAndLog("count=%d key= %s", i
, sprint_hex(keyBlock
+ i
* 6, 6));
570 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
572 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
574 PrintAndLog("Found valid key:%012llx", key64
);
576 // transfer key to the emulator
578 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
581 num_to_bytes(key64
, 6, keyBlock
);
583 num_to_bytes(key64
, 6, &keyBlock
[10]);
584 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
587 PrintAndLog("No valid key found");
590 else { // ------------------------------------ multiple sectors working
591 blDiff
= blockNo
% 4;
592 PrintAndLog("Block shift=%d", blDiff
);
593 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
594 if (e_sector
== NULL
) return 1;
596 //test current key 4 sectors
597 memcpy(keyBlock
, key
, 6);
598 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 1 * 6));
599 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 2 * 6));
600 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 3 * 6));
601 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 4 * 6));
602 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
604 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
605 for (i
= 0; i
< SectorsCnt
; i
++) {
606 for (j
= 0; j
< 2; j
++) {
607 if (e_sector
[i
].foundKey
[j
]) continue;
609 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
612 e_sector
[i
].Key
[j
] = key64
;
613 e_sector
[i
].foundKey
[j
] = 1;
620 PrintAndLog("nested...");
621 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
622 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4)
623 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
624 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
625 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) continue;
629 //try keys from nested
630 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
632 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
634 PrintAndLog("Found valid key:%012llx", key64
);
635 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
636 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
641 PrintAndLog("Iterations count: %d", iterations
);
643 PrintAndLog("|---|----------------|---|----------------|---|");
644 PrintAndLog("|sec|key A |res|key B |res|");
645 PrintAndLog("|---|----------------|---|----------------|---|");
646 for (i
= 0; i
< SectorsCnt
; i
++) {
647 PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i
,
648 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
650 PrintAndLog("|---|----------------|---|----------------|---|");
652 // transfer them to the emulator
654 for (i
= 0; i
< SectorsCnt
; i
++) {
655 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
656 if (e_sector
[i
].foundKey
[0])
657 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
658 if (e_sector
[i
].foundKey
[1])
659 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
660 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
665 if (createDumpFile
) {
666 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
667 PrintAndLog("Could not create file dumpkeys.bin");
671 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
672 for(i
=0; i
<16; i
++) {
673 if (e_sector
[i
].foundKey
[0]){
674 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
675 fwrite ( tempkey
, 1, 6, fkeys
);
678 fwrite ( &standart
, 1, 6, fkeys
);
681 for(i
=0; i
<16; i
++) {
682 if (e_sector
[i
].foundKey
[1]){
683 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
684 fwrite ( tempkey
, 1, 6, fkeys
);
687 fwrite ( &standart
, 1, 6, fkeys
);
700 get_trailer_block (uint32_t uiBlock
)
702 // Test if we are in the small or big sectors
703 uint32_t trailer_block
= 0;
705 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
707 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
709 return trailer_block
;
711 int CmdHF14AMfChk(const char *Cmd
)
714 char filename
[256]={0};
715 uint8_t *keyBlock
= NULL
, *p
;
716 uint8_t stKeyBlock
= 20;
722 uint8_t SectorsCnt
= 1;
726 int transferToEml
= 0;
727 int createDumpFile
= 0;
729 keyBlock
= calloc(stKeyBlock
, 6);
730 if (keyBlock
== NULL
) return 1;
732 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 0 * 6)); // Default key (first key used by program if no user defined key)
733 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 1 * 6)); // Blank key
734 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 2 * 6)); // NFCForum MAD key
735 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
736 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 4 * 6));
737 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 5 * 6));
738 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 6 * 6));
739 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 7 * 6));
740 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 8 * 6));
741 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 9 * 6));
742 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 10 * 6));
743 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 11 * 6));
744 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 12 * 6));
747 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
748 PrintAndLog(" * - all sectors");
749 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
750 // PrintAndLog("d - write keys to binary file\n");
752 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
753 PrintAndLog(" hf mf chk *1 ? t");
757 if (param_getchar(Cmd
, 0)=='*') {
759 switch(param_getchar(Cmd
+1, 0)) {
760 case '0': SectorsCnt
= 5; break;
761 case '1': SectorsCnt
= 16; break;
762 case '2': SectorsCnt
= 32; break;
763 case '4': SectorsCnt
= 40; break;
764 default: SectorsCnt
= 16;
768 blockNo
= param_get8(Cmd
, 0);
770 ctmp
= param_getchar(Cmd
, 1);
782 PrintAndLog("Key type must be A , B or ?");
786 ctmp
= param_getchar(Cmd
, 2);
787 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
788 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
790 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
791 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
792 if ( stKeyBlock
- keycnt
< 2) {
793 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
795 PrintAndLog("Cannot allocate memory for Keys");
801 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
802 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
803 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
807 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
808 PrintAndLog("File name too long");
816 if ( (f
= fopen( filename
, "r")) ) {
817 while((read
= getline(&line
, &len
, f
)) != -1){
819 if( line
[0]=='#' || line
[0]=='\n' ) continue; //The line start with # is remcommnet,skip
821 if (read
< 12 || !isxdigit(line
[0])){
822 PrintAndLog("File content error. '%s' must include 12 HEX symbols",line
);
828 if ( stKeyBlock
- keycnt
< 2) {
829 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
831 PrintAndLog("Cannot allocate memory for defKeys");
837 memset(keyBlock
+ 6 * keycnt
, 0, 6);
838 num_to_bytes(strtoll(line
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
839 PrintAndLog("chk custom key[%d] %012llx", keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
844 PrintAndLog("File: %s: not found or locked.", filename
);
853 PrintAndLog("No key specified,try default keys");
854 for (;keycnt
<=12; keycnt
++)
855 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
856 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
857 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
860 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
862 for (int i
=0; i
<SectorsCnt
; ++i
) {
863 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
864 int size
= keycnt
>8?8:keycnt
;
865 for (int c
= 0; c
< keycnt
; c
+=size
) {
866 size
=keycnt
-c
>8?8:keycnt
-c
;
867 res
= mfCheckKeys(b
, t
, size
, keyBlock
+6*c
, &key64
);
870 PrintAndLog("Found valid key:[%012llx]",key64
);
873 mfEmlGetMem(block
, get_trailer_block(b
), 1);
874 num_to_bytes(key64
, 6, block
+ t
*10);
875 mfEmlSetMem(block
, get_trailer_block(b
), 1);
880 printf("Not found yet, keycnt:%d\r", c
+size
);
884 PrintAndLog("Command execute timeout");
887 b
<127?(b
+=4):(b
+=16);
895 if (createDumpFile) {
896 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
897 PrintAndLog("Could not create file dumpkeys.bin");
901 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
902 for(i=0; i<16; i++) {
903 if (e_sector[i].foundKey[0]){
904 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
905 fwrite ( tempkey, 1, 6, fkeys );
908 fwrite ( &standart, 1, 6, fkeys );
911 for(i=0; i<16; i++) {
912 if (e_sector[i].foundKey[1]){
913 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
914 fwrite ( tempkey, 1, 6, fkeys );
917 fwrite ( &standart, 1, 6, fkeys );
926 int CmdHF14AMf1kSim(const char *Cmd
)
928 uint8_t uid
[4] = {0, 0, 0, 0};
930 if (param_getchar(Cmd
, 0) == 'h') {
931 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
932 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
936 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
937 PrintAndLog("UID must include 8 HEX symbols");
940 PrintAndLog(" uid:%s ", sprint_hex(uid
, 4));
942 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {0, 0, 0}};
943 memcpy(c
.d
.asBytes
, uid
, 4);
949 int CmdHF14AMfDbg(const char *Cmd
)
951 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
953 PrintAndLog("Max debud mode parameter is 4 \n");
956 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
957 PrintAndLog("Usage: hf mf dbg <debug level>");
958 PrintAndLog(" 0 - no debug messages");
959 PrintAndLog(" 1 - error messages");
960 PrintAndLog(" 2 - all messages");
961 PrintAndLog(" 4 - extended debug mode");
965 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
971 int CmdHF14AMfEGet(const char *Cmd
)
974 uint8_t data
[3 * 16];
977 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
978 PrintAndLog("Usage: hf mf eget <block number>");
979 PrintAndLog(" sample: hf mf eget 0 ");
983 blockNo
= param_get8(Cmd
, 0);
984 if (blockNo
>= 32 * 4 + 8 * 16) {
985 PrintAndLog("Block number must be in [0..255] as in MIFARE classic.");
990 if (!mfEmlGetMem(data
, blockNo
, 3)) {
991 for (i
= 0; i
< 3; i
++) {
992 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
995 PrintAndLog("Command execute timeout");
1001 int CmdHF14AMfEClear(const char *Cmd
)
1003 if (param_getchar(Cmd
, 0) == 'h') {
1004 PrintAndLog("Usage: hf mf eclr");
1005 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1009 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1014 int CmdHF14AMfESet(const char *Cmd
)
1016 uint8_t memBlock
[16];
1017 uint8_t blockNo
= 0;
1019 memset(memBlock
, 0x00, sizeof(memBlock
));
1021 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1022 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1023 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1027 blockNo
= param_get8(Cmd
, 0);
1028 if (blockNo
>= 32 * 4 + 8 * 16) {
1029 PrintAndLog("Block number must be in [0..255] as in MIFARE classic.");
1033 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1034 PrintAndLog("block data must include 32 HEX symbols");
1039 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1040 memcpy(c
.d
.asBytes
, memBlock
, 16);
1045 int CmdHF14AMfELoad(const char *Cmd
)
1049 char * fnameptr
= filename
;
1052 int i
, len
, blockNum
;
1054 memset(filename
, 0, sizeof(filename
));
1055 memset(buf
, 0, sizeof(buf
));
1057 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1058 PrintAndLog("It loads emul dump from the file `filename.eml`");
1059 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1060 PrintAndLog(" sample: hf mf eload filename");
1065 if (len
> 14) len
= 14;
1067 memcpy(filename
, Cmd
, len
);
1070 sprintf(fnameptr
, ".eml");
1073 f
= fopen(filename
, "r");
1075 PrintAndLog("File not found or locked.");
1081 memset(buf
, 0, sizeof(buf
));
1082 fgets(buf
, sizeof(buf
), f
);
1084 if (strlen(buf
) < 32){
1085 if(strlen(buf
) && feof(f
))
1087 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1090 for (i
= 0; i
< 32; i
+= 2)
1091 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1092 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1094 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1095 PrintAndLog("Cant set emul block: %d", blockNum
);
1100 if (blockNum
>= 32 * 4 + 8 * 16) break;
1104 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1105 PrintAndLog("File content error. There must be 64 blocks");
1108 PrintAndLog("Loaded from file: %s", filename
);
1112 int CmdHF14AMfESave(const char *Cmd
)
1116 char * fnameptr
= filename
;
1120 memset(filename
, 0, sizeof(filename
));
1121 memset(buf
, 0, sizeof(buf
));
1123 if (param_getchar(Cmd
, 0) == 'h') {
1124 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1125 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1126 PrintAndLog(" sample: hf mf esave ");
1127 PrintAndLog(" hf mf esave filename");
1132 if (len
> 14) len
= 14;
1136 if (mfEmlGetMem(buf
, 0, 1)) {
1137 PrintAndLog("Cant get block: %d", 0);
1140 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1141 sprintf(fnameptr
, "%02x", buf
[j
]);
1143 memcpy(filename
, Cmd
, len
);
1147 sprintf(fnameptr
, ".eml");
1150 f
= fopen(filename
, "w+");
1153 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1154 if (mfEmlGetMem(buf
, i
, 1)) {
1155 PrintAndLog("Cant get block: %d", i
);
1158 for (j
= 0; j
< 16; j
++)
1159 fprintf(f
, "%02x", buf
[j
]);
1164 PrintAndLog("Saved to file: %s", filename
);
1169 int CmdHF14AMfECFill(const char *Cmd
)
1171 uint8_t keyType
= 0;
1173 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1174 PrintAndLog("Usage: hf mf efill <key A/B>");
1175 PrintAndLog("sample: hf mf efill A");
1176 PrintAndLog("Card data blocks transfers to card emulator memory.");
1177 PrintAndLog("Keys must be laid in the simulator memory. \n");
1181 char ctmp
= param_getchar(Cmd
, 0);
1183 PrintAndLog("Key type must be A or B");
1186 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1188 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1193 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1197 uint64_t keyA
, keyB
;
1199 PrintAndLog("|---|----------------|----------------|");
1200 PrintAndLog("|sec|key A |key B |");
1201 PrintAndLog("|---|----------------|----------------|");
1202 for (i
= 0; i
< 40; i
++) {
1203 b
<127?(b
+=4):(b
+=16);
1204 if (mfEmlGetMem(data
, b
, 1)) {
1205 PrintAndLog("error get block %d", b
);
1208 keyA
= bytes_to_num(data
, 6);
1209 keyB
= bytes_to_num(data
+ 10, 6);
1210 PrintAndLog("|%03d| %012llx | %012llx |", i
, keyA
, keyB
);
1212 PrintAndLog("|---|----------------|----------------|");
1217 static command_t CommandTable
[] =
1219 {"help", CmdHelp
, 1, "This help"},
1220 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1221 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1222 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1223 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1224 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1225 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1226 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1227 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages. param - <used card nonce>"},
1228 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1229 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1230 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1231 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1232 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1233 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1234 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1235 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1236 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1237 {NULL
, NULL
, 0, NULL
}
1240 int CmdHFMF(const char *Cmd
)
1243 while (WaitForResponseTimeout(CMD_ACK
, 500) != NULL
) ;
1245 CmdsParse(CommandTable
, Cmd
);
1249 int CmdHelp(const char *Cmd
)
1251 CmdsHelp(CommandTable
);