]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
8d78b0e6f8665daa2fa2625ea5772001427428e4
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 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
15 int CmdHF14AMifare(const char *Cmd
)
18 uint32_t nt
= 0, nr
= 0;
19 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
21 uint8_t keyBlock
[8] = {0};
23 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
26 printf("-------------------------------------------------------------------------\n");
27 printf("Executing command. Expected execution time: 25sec on average :-)\n");
28 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
29 printf("-------------------------------------------------------------------------\n");
37 while (ukbhit()) getchar();
46 printf("\naborted via keyboard!\n");
51 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
52 isOK
= resp
.arg
[0] & 0xff;
53 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
54 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
55 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
56 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
57 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
59 if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
67 if (isOK
!= 1) return 1;
69 // execute original function from util nonce2key
70 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
))
73 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
75 printf("------------------------------------------------------------------\n");
76 PrintAndLog("Key found:%012"llx
" \n", r_key
);
78 num_to_bytes(r_key
, 6, keyBlock
);
79 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
82 PrintAndLog("Found valid key:%012"llx
, r_key
);
85 if (isOK
!= 2) PrintAndLog("Found invalid key. ");
86 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
94 int CmdHF14AMfWrBl(const char *Cmd
)
98 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
99 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
104 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
105 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
109 blockNo
= param_get8(Cmd
, 0);
110 cmdp
= param_getchar(Cmd
, 1);
112 PrintAndLog("Key type must be A or B");
115 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
116 if (param_gethex(Cmd
, 2, key
, 12)) {
117 PrintAndLog("Key must include 12 HEX symbols");
120 if (param_gethex(Cmd
, 3, bldata
, 32)) {
121 PrintAndLog("Block data must include 32 HEX symbols");
124 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo
, keyType
, sprint_hex(key
, 6));
125 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
127 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
128 memcpy(c
.d
.asBytes
, key
, 6);
129 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
133 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
134 uint8_t isOK
= resp
.arg
[0] & 0xff;
135 PrintAndLog("isOk:%02x", isOK
);
137 PrintAndLog("Command execute timeout");
143 int CmdHF14AMfUWrBl(const char *Cmd
)
146 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
150 PrintAndLog("Usage: hf mf uwrbl <block number> <block data (8 hex symbols)>");
151 PrintAndLog(" sample: hf mf uwrbl 0 01020304");
155 blockNo
= param_get8(Cmd
, 0);
156 if (param_gethex(Cmd
, 1, bldata
, 8)) {
157 PrintAndLog("Block data must include 8 HEX symbols");
164 PrintAndLog("Access Denied");
167 PrintAndLog("Access Denied");
170 PrintAndLog("--specialblock no:%02x", blockNo
);
171 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
172 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
173 memcpy(c
.d
.asBytes
, bldata
, 4);
176 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
177 uint8_t isOK
= resp
.arg
[0] & 0xff;
178 PrintAndLog("isOk:%02x", isOK
);
180 PrintAndLog("Command execute timeout");
184 PrintAndLog("--specialblock no:%02x", blockNo
);
185 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
186 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
187 memcpy(d
.d
.asBytes
,bldata
, 4);
190 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
191 uint8_t isOK
= resp
.arg
[0] & 0xff;
192 PrintAndLog("isOk:%02x", isOK
);
194 PrintAndLog("Command execute timeout");
198 PrintAndLog("--block no:%02x", blockNo
);
199 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
200 //UsbCommand e = {CMD_MIFAREU_WRITEBL_COMPAT, {blockNo}};
201 //memcpy(e.d.asBytes,bldata, 16);
202 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
203 memcpy(e
.d
.asBytes
,bldata
, 4);
206 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
207 uint8_t isOK
= resp
.arg
[0] & 0xff;
208 PrintAndLog("isOk:%02x", isOK
);
210 PrintAndLog("Command execute timeout");
217 int CmdHF14AMfRdBl(const char *Cmd
)
221 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
227 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
228 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
232 blockNo
= param_get8(Cmd
, 0);
233 cmdp
= param_getchar(Cmd
, 1);
235 PrintAndLog("Key type must be A or B");
238 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
239 if (param_gethex(Cmd
, 2, key
, 12)) {
240 PrintAndLog("Key must include 12 HEX symbols");
243 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo
, keyType
, sprint_hex(key
, 6));
245 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
246 memcpy(c
.d
.asBytes
, key
, 6);
250 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
251 uint8_t isOK
= resp
.arg
[0] & 0xff;
252 uint8_t * data
= resp
.d
.asBytes
;
255 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
257 PrintAndLog("isOk:%02x", isOK
);
259 PrintAndLog("Command execute timeout");
265 int CmdHF14AMfURdBl(const char *Cmd
)
270 PrintAndLog("Usage: hf mf urdbl <block number>");
271 PrintAndLog(" sample: hf mf urdbl 0");
275 blockNo
= param_get8(Cmd
, 0);
276 PrintAndLog("--block no:%02x", blockNo
);
278 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
282 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
283 uint8_t isOK
= resp
.arg
[0] & 0xff;
284 uint8_t * data
= resp
.d
.asBytes
;
287 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 4));
289 PrintAndLog("isOk:%02x", isOK
);
291 PrintAndLog("Command execute timeout");
297 int CmdHF14AMfURdCard(const char *Cmd
)
300 uint8_t sectorNo
= 0;
301 uint8_t *lockbytes_t
=NULL
;
302 uint8_t lockbytes
[2]={0,0};
303 bool bit
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
306 uint8_t * data
= NULL
;
309 PrintAndLog("Sector number must be less than 16");
312 PrintAndLog("Attempting to Read Ultralight... ");
314 UsbCommand c
= {CMD_MIFAREU_READCARD
, {sectorNo
}};
318 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
319 isOK
= resp
.arg
[0] & 0xff;
320 data
= resp
.d
.asBytes
;
322 PrintAndLog("isOk:%02x", isOK
);
324 for (i
= 0; i
< 16; i
++) {
328 lockbytes_t
=data
+(i
*4);
329 lockbytes
[0]=lockbytes_t
[2];
330 lockbytes
[1]=lockbytes_t
[3];
331 for(int j
=0; j
<16; j
++){
332 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
334 //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);
335 //PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]);
336 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
339 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
342 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
345 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
348 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
351 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
354 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
357 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
360 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
363 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
366 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
369 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
372 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
375 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
378 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
383 PrintAndLog("Command1 execute timeout");
388 int CmdHF14AMfRdSc(const char *Cmd
)
391 uint8_t sectorNo
= 0;
393 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
396 uint8_t * data
= NULL
;
401 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
402 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
406 sectorNo
= param_get8(Cmd
, 0);
408 PrintAndLog("Sector number must be less than 64");
411 cmdp
= param_getchar(Cmd
, 1);
413 PrintAndLog("Key type must be A or B");
416 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
417 if (param_gethex(Cmd
, 2, key
, 12)) {
418 PrintAndLog("Key must include 12 HEX symbols");
421 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo
, keyType
, sprint_hex(key
, 6));
423 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
424 memcpy(c
.d
.asBytes
, key
, 6);
429 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
430 isOK
= resp
.arg
[0] & 0xff;
431 data
= resp
.d
.asBytes
;
433 PrintAndLog("isOk:%02x", isOK
);
435 for (i
= 0; i
< 2; i
++) {
436 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
439 PrintAndLog("Command1 execute timeout");
444 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
445 isOK
= resp
.arg
[0] & 0xff;
446 data
= resp
.d
.asBytes
;
449 for (i
= 0; i
< 2; i
++) {
450 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
453 PrintAndLog("Command2 execute timeout");
459 int CmdHF14AMfDump(const char *Cmd
)
465 uint8_t rights
[40][4];
472 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
473 PrintAndLog("Could not find file dumpkeys.bin");
477 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
478 PrintAndLog("Could not create file name dumpdata.bin");
484 for (i
=0 ; i
<16 ; i
++) {
485 if (fread( keyA
[i
], 1, 6, fin
) == 0) {
486 PrintAndLog("File reading error.");
490 for (i
=0 ; i
<16 ; i
++) {
491 if (fread( keyB
[i
], 1, 6, fin
) == 0) {
492 PrintAndLog("File reading error.");
497 // Read access rights to sectors
499 PrintAndLog("|-----------------------------------------|");
500 PrintAndLog("|------ Reading sector access bits...-----|");
501 PrintAndLog("|-----------------------------------------|");
503 for (i
= 0 ; i
< 16 ; i
++) {
504 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
505 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
508 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
509 uint8_t isOK
= resp
.arg
[0] & 0xff;
510 uint8_t *data
= resp
.d
.asBytes
;
512 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
513 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
514 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
515 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
518 PrintAndLog("Could not get access rights for block %d", i
);
522 PrintAndLog("Command execute timeout");
526 // Read blocks and print to file
528 PrintAndLog("|-----------------------------------------|");
529 PrintAndLog("|----- Dumping all blocks to file... -----|");
530 PrintAndLog("|-----------------------------------------|");
533 for (i
=0 ; i
<16 ; i
++) {
534 for (j
=0 ; j
<4 ; j
++) {
535 bool received
= false;
538 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
539 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
541 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
544 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
545 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
546 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
548 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
550 else if (rights
[i
][j
] == 7) {
551 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
554 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
555 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
557 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
562 uint8_t isOK
= resp
.arg
[0] & 0xff;
563 uint8_t *data
= resp
.d
.asBytes
;
565 data
[0] = (keyA
[i
][0]);
566 data
[1] = (keyA
[i
][1]);
567 data
[2] = (keyA
[i
][2]);
568 data
[3] = (keyA
[i
][3]);
569 data
[4] = (keyA
[i
][4]);
570 data
[5] = (keyA
[i
][5]);
571 data
[10] = (keyB
[i
][0]);
572 data
[11] = (keyB
[i
][1]);
573 data
[12] = (keyB
[i
][2]);
574 data
[13] = (keyB
[i
][3]);
575 data
[14] = (keyB
[i
][4]);
576 data
[15] = (keyB
[i
][5]);
579 fwrite ( data
, 1, 16, fout
);
580 PrintAndLog("Dumped card data into 'dumpdata.bin'");
584 PrintAndLog("Could not get access rights for block %d", i
);
588 PrintAndLog("Command execute timeout");
598 int CmdHF14AMfRestore(const char *Cmd
)
603 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
604 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
611 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
612 PrintAndLog("Could not find file dumpdata.bin");
615 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
616 PrintAndLog("Could not find file dumpkeys.bin");
620 for (i
=0 ; i
<16 ; i
++) {
621 if (fread(keyA
[i
], 1, 6, fkeys
) == 0) {
622 PrintAndLog("File reading error.");
626 for (i
=0 ; i
<16 ; i
++) {
627 if (fread(keyB
[i
], 1, 6, fkeys
) == 0) {
628 PrintAndLog("File reading error.");
633 PrintAndLog("Restoring dumpdata.bin to card");
635 for (i
=0 ; i
<16 ; i
++) {
636 for( j
=0 ; j
<4 ; j
++) {
637 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
638 memcpy(c
.d
.asBytes
, key
, 6);
640 if (fread(bldata
, 1, 16, fdump
) == 0) {
641 PrintAndLog("File reading error.");
646 bldata
[0] = (keyA
[i
][0]);
647 bldata
[1] = (keyA
[i
][1]);
648 bldata
[2] = (keyA
[i
][2]);
649 bldata
[3] = (keyA
[i
][3]);
650 bldata
[4] = (keyA
[i
][4]);
651 bldata
[5] = (keyA
[i
][5]);
652 bldata
[10] = (keyB
[i
][0]);
653 bldata
[11] = (keyB
[i
][1]);
654 bldata
[12] = (keyB
[i
][2]);
655 bldata
[13] = (keyB
[i
][3]);
656 bldata
[14] = (keyB
[i
][4]);
657 bldata
[15] = (keyB
[i
][5]);
660 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
663 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
666 if ((ch != 'y') && (ch != 'Y')){
667 PrintAndLog("Aborting !");
672 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
676 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
677 uint8_t isOK
= resp
.arg
[0] & 0xff;
678 PrintAndLog("isOk:%02x", isOK
);
680 PrintAndLog("Command execute timeout");
690 int CmdHF14AMfNested(const char *Cmd
)
692 int i
, j
, res
, iterations
;
693 sector
* e_sector
= NULL
;
696 uint8_t trgBlockNo
= 0;
697 uint8_t trgKeyType
= 0;
700 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
701 uint8_t keyBlock
[6*6];
703 int transferToEml
= 0;
705 int createDumpFile
= 0;
707 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
708 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
713 PrintAndLog("Usage:");
714 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
715 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
716 PrintAndLog(" <target block number> <target key A/B> [t]");
717 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
718 PrintAndLog("t - transfer keys into emulator memory");
719 PrintAndLog("d - write keys to binary file");
721 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
722 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
723 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
724 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
728 cmdp
= param_getchar(Cmd
, 0);
729 blockNo
= param_get8(Cmd
, 1);
730 ctmp
= param_getchar(Cmd
, 2);
732 PrintAndLog("Key type must be A or B");
735 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
736 if (param_gethex(Cmd
, 3, key
, 12)) {
737 PrintAndLog("Key must include 12 HEX symbols");
741 if (cmdp
== 'o' || cmdp
== 'O') {
743 trgBlockNo
= param_get8(Cmd
, 4);
744 ctmp
= param_getchar(Cmd
, 5);
746 PrintAndLog("Target key type must be A or B");
749 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
752 case '0': SectorsCnt
= 05; break;
753 case '1': SectorsCnt
= 16; break;
754 case '2': SectorsCnt
= 32; break;
755 case '4': SectorsCnt
= 64; break;
756 default: SectorsCnt
= 16;
760 ctmp
= param_getchar(Cmd
, 4);
761 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
762 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
764 ctmp
= param_getchar(Cmd
, 6);
765 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
766 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
768 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
770 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
773 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true)) {
774 PrintAndLog("Nested error.");
777 key64
= bytes_to_num(keyBlock
, 6);
779 PrintAndLog("Found valid key:%012"llx
, key64
);
781 // transfer key to the emulator
783 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
786 num_to_bytes(key64
, 6, keyBlock
);
788 num_to_bytes(key64
, 6, &keyBlock
[10]);
789 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
792 PrintAndLog("No valid key found");
795 else { // ------------------------------------ multiple sectors working
799 blDiff
= blockNo
% 4;
800 PrintAndLog("Block shift=%d", blDiff
);
801 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
802 if (e_sector
== NULL
) return 1;
804 //test current key 4 sectors
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));
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;
817 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
820 e_sector
[i
].Key
[j
] = key64
;
821 e_sector
[i
].foundKey
[j
] = 1;
829 PrintAndLog("nested...");
830 bool calibrate
= true;
831 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
832 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4) {
833 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
834 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
835 PrintAndLog("-----------------------------------------------");
836 if(mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, calibrate
)) {
837 PrintAndLog("Nested error.\n");
846 key64
= bytes_to_num(keyBlock
, 6);
848 PrintAndLog("Found valid key:%012"llx
, key64
);
849 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
850 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
856 printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1
)/CLOCKS_PER_SEC
, ((float)clock() - time1
)/iterations
/CLOCKS_PER_SEC
);
858 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
860 PrintAndLog("|---|----------------|---|----------------|---|");
861 PrintAndLog("|sec|key A |res|key B |res|");
862 PrintAndLog("|---|----------------|---|----------------|---|");
863 for (i
= 0; i
< SectorsCnt
; i
++) {
864 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
865 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
867 PrintAndLog("|---|----------------|---|----------------|---|");
869 // transfer them to the emulator
871 for (i
= 0; i
< SectorsCnt
; i
++) {
872 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
873 if (e_sector
[i
].foundKey
[0])
874 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
875 if (e_sector
[i
].foundKey
[1])
876 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
877 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
882 if (createDumpFile
) {
883 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
884 PrintAndLog("Could not create file dumpkeys.bin");
888 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
889 for(i
=0; i
<16; i
++) {
890 if (e_sector
[i
].foundKey
[0]){
891 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
892 fwrite ( tempkey
, 1, 6, fkeys
);
895 fwrite ( &standart
, 1, 6, fkeys
);
898 for(i
=0; i
<16; i
++) {
899 if (e_sector
[i
].foundKey
[1]){
900 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
901 fwrite ( tempkey
, 1, 6, fkeys
);
904 fwrite ( &standart
, 1, 6, fkeys
);
917 get_trailer_block (uint32_t uiBlock
)
919 // Test if we are in the small or big sectors
920 uint32_t trailer_block
= 0;
922 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
924 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
926 return trailer_block
;
928 int CmdHF14AMfChk(const char *Cmd
)
931 char filename
[256]={0};
933 uint8_t *keyBlock
= NULL
, *p
;
934 uint8_t stKeyBlock
= 20;
940 uint8_t SectorsCnt
= 1;
944 int transferToEml
= 0;
945 int createDumpFile
= 0;
947 keyBlock
= calloc(stKeyBlock
, 6);
948 if (keyBlock
== NULL
) return 1;
950 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 0 * 6)); // Default key (first key used by program if no user defined key)
951 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 1 * 6)); // Blank key
952 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 2 * 6)); // NFCForum MAD key
953 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
954 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 4 * 6));
955 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 5 * 6));
956 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 6 * 6));
957 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 7 * 6));
958 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 8 * 6));
959 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 9 * 6));
960 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 10 * 6));
961 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 11 * 6));
962 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 12 * 6));
965 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
966 PrintAndLog(" * - all sectors");
967 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
968 // PrintAndLog("d - write keys to binary file\n");
970 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
971 PrintAndLog(" hf mf chk *1 ? t");
975 if (param_getchar(Cmd
, 0)=='*') {
977 switch(param_getchar(Cmd
+1, 0)) {
978 case '0': SectorsCnt
= 5; break;
979 case '1': SectorsCnt
= 16; break;
980 case '2': SectorsCnt
= 32; break;
981 case '4': SectorsCnt
= 40; break;
982 default: SectorsCnt
= 16;
986 blockNo
= param_get8(Cmd
, 0);
988 ctmp
= param_getchar(Cmd
, 1);
1000 PrintAndLog("Key type must be A , B or ?");
1004 ctmp
= param_getchar(Cmd
, 2);
1005 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1006 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1008 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1009 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1010 if ( stKeyBlock
- keycnt
< 2) {
1011 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1013 PrintAndLog("Cannot allocate memory for Keys");
1019 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
1020 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1021 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1024 // May be a dic file
1025 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
1026 PrintAndLog("File name too long");
1031 if ( (f
= fopen( filename
, "r")) ) {
1033 memset(buf
, 0, sizeof(buf
));
1034 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1035 PrintAndLog("File reading error.");
1039 if (strlen(buf
) < 12 || buf
[11] == '\n')
1042 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1044 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1046 if (!isxdigit(buf
[0])){
1047 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1053 if ( stKeyBlock
- keycnt
< 2) {
1054 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1056 PrintAndLog("Cannot allocate memory for defKeys");
1062 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1063 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1064 PrintAndLog("chk custom key[%d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1068 PrintAndLog("File: %s: not found or locked.", filename
);
1077 PrintAndLog("No key specified,try default keys");
1078 for (;keycnt
<=12; keycnt
++)
1079 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
1080 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1081 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1084 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
1086 for (int i
=0; i
<SectorsCnt
; ++i
) {
1087 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
1088 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1089 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1090 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1091 res
= mfCheckKeys(b
, t
, size
, &keyBlock
[6*c
], &key64
);
1094 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1095 if (transferToEml
) {
1097 mfEmlGetMem(block
, get_trailer_block(b
), 1);
1098 num_to_bytes(key64
, 6, block
+ t
*10);
1099 mfEmlSetMem(block
, get_trailer_block(b
), 1);
1103 PrintAndLog("Command execute timeout");
1106 b
<127?(b
+=4):(b
+=16);
1114 if (createDumpFile) {
1115 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
1116 PrintAndLog("Could not create file dumpkeys.bin");
1120 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
1121 for(i=0; i<16; i++) {
1122 if (e_sector[i].foundKey[0]){
1123 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
1124 fwrite ( tempkey, 1, 6, fkeys );
1127 fwrite ( &standart, 1, 6, fkeys );
1130 for(i=0; i<16; i++) {
1131 if (e_sector[i].foundKey[1]){
1132 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
1133 fwrite ( tempkey, 1, 6, fkeys );
1136 fwrite ( &standart, 1, 6, fkeys );
1145 int CmdHF14AMf1kSim(const char *Cmd
)
1147 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1148 uint8_t exitAfterNReads
= 0;
1151 if (param_getchar(Cmd
, 0) == 'h') {
1152 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1153 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1154 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1155 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1156 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1157 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
1161 if (param_getchar(Cmd
, pnr
) == 'u') {
1162 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1164 flags
|=FLAG_4B_UID_IN_DATA
; // UID from packet
1165 }else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0)
1167 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1170 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1175 if (param_getchar(Cmd
, pnr
) == 'n') {
1176 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1179 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1180 //Using a flag to signal interactiveness, least significant bit
1181 flags
|= FLAG_INTERACTIVE
;
1185 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1186 //Using a flag to signal interactiveness, least significant bit
1187 flags
|= FLAG_NR_AR_ATTACK
;
1189 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1190 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1191 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1192 , exitAfterNReads
, flags
,flags
);
1195 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1196 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1199 if(flags
& FLAG_INTERACTIVE
)
1202 PrintAndLog("Press pm3-button to abort simulation");
1203 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1204 //We're waiting only 1.5 s at a time, otherwise we get the
1205 // annoying message about "Waiting for a response... "
1212 int CmdHF14AMfDbg(const char *Cmd
)
1214 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1216 PrintAndLog("Max debud mode parameter is 4 \n");
1219 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1220 PrintAndLog("Usage: hf mf dbg <debug level>");
1221 PrintAndLog(" 0 - no debug messages");
1222 PrintAndLog(" 1 - error messages");
1223 PrintAndLog(" 2 - all messages");
1224 PrintAndLog(" 4 - extended debug mode");
1228 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1234 int CmdHF14AMfEGet(const char *Cmd
)
1236 uint8_t blockNo
= 0;
1237 uint8_t data
[3 * 16];
1240 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1241 PrintAndLog("Usage: hf mf eget <block number>");
1242 PrintAndLog(" sample: hf mf eget 0 ");
1246 blockNo
= param_get8(Cmd
, 0);
1249 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1250 for (i
= 0; i
< 3; i
++) {
1251 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1254 PrintAndLog("Command execute timeout");
1260 int CmdHF14AMfEClear(const char *Cmd
)
1262 if (param_getchar(Cmd
, 0) == 'h') {
1263 PrintAndLog("Usage: hf mf eclr");
1264 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1268 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1273 int CmdHF14AMfESet(const char *Cmd
)
1275 uint8_t memBlock
[16];
1276 uint8_t blockNo
= 0;
1278 memset(memBlock
, 0x00, sizeof(memBlock
));
1280 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1281 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1282 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1286 blockNo
= param_get8(Cmd
, 0);
1288 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1289 PrintAndLog("block data must include 32 HEX symbols");
1294 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1295 memcpy(c
.d
.asBytes
, memBlock
, 16);
1300 int CmdHF14AMfELoad(const char *Cmd
)
1304 char * fnameptr
= filename
;
1307 int i
, len
, blockNum
;
1309 memset(filename
, 0, sizeof(filename
));
1310 memset(buf
, 0, sizeof(buf
));
1312 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1313 PrintAndLog("It loads emul dump from the file `filename.eml`");
1314 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1315 PrintAndLog(" sample: hf mf eload filename");
1320 if (len
> 14) len
= 14;
1322 memcpy(filename
, Cmd
, len
);
1325 sprintf(fnameptr
, ".eml");
1328 f
= fopen(filename
, "r");
1330 PrintAndLog("File not found or locked.");
1336 memset(buf
, 0, sizeof(buf
));
1337 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1338 if(blockNum
== 16 * 4)
1342 PrintAndLog("File reading error.");
1346 if (strlen(buf
) < 32){
1347 if(strlen(buf
) && feof(f
))
1349 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1352 for (i
= 0; i
< 32; i
+= 2)
1353 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1354 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1356 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1357 PrintAndLog("Cant set emul block: %d", blockNum
);
1362 if (blockNum
>= 32 * 4 + 8 * 16) break;
1366 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1367 PrintAndLog("File content error. There must be 64 blocks");
1370 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1374 int CmdHF14AMfESave(const char *Cmd
)
1378 char * fnameptr
= filename
;
1382 memset(filename
, 0, sizeof(filename
));
1383 memset(buf
, 0, sizeof(buf
));
1385 if (param_getchar(Cmd
, 0) == 'h') {
1386 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1387 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1388 PrintAndLog(" sample: hf mf esave ");
1389 PrintAndLog(" hf mf esave filename");
1394 if (len
> 14) len
= 14;
1398 if (mfEmlGetMem(buf
, 0, 1)) {
1399 PrintAndLog("Cant get block: %d", 0);
1402 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1403 sprintf(fnameptr
, "%02x", buf
[j
]);
1405 memcpy(filename
, Cmd
, len
);
1409 sprintf(fnameptr
, ".eml");
1412 f
= fopen(filename
, "w+");
1415 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1416 if (mfEmlGetMem(buf
, i
, 1)) {
1417 PrintAndLog("Cant get block: %d", i
);
1420 for (j
= 0; j
< 16; j
++)
1421 fprintf(f
, "%02x", buf
[j
]);
1426 PrintAndLog("Saved to file: %s", filename
);
1431 int CmdHF14AMfECFill(const char *Cmd
)
1433 uint8_t keyType
= 0;
1435 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1436 PrintAndLog("Usage: hf mf efill <key A/B>");
1437 PrintAndLog("sample: hf mf efill A");
1438 PrintAndLog("Card data blocks transfers to card emulator memory.");
1439 PrintAndLog("Keys must be laid in the simulator memory. \n");
1443 char ctmp
= param_getchar(Cmd
, 0);
1445 PrintAndLog("Key type must be A or B");
1448 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1450 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1455 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1459 uint64_t keyA
, keyB
;
1461 PrintAndLog("|---|----------------|----------------|");
1462 PrintAndLog("|sec|key A |key B |");
1463 PrintAndLog("|---|----------------|----------------|");
1464 for (i
= 0; i
< 40; i
++) {
1465 b
<127?(b
+=4):(b
+=16);
1466 if (mfEmlGetMem(data
, b
, 1)) {
1467 PrintAndLog("error get block %d", b
);
1470 keyA
= bytes_to_num(data
, 6);
1471 keyB
= bytes_to_num(data
+ 10, 6);
1472 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1474 PrintAndLog("|---|----------------|----------------|");
1479 int CmdHF14AMfCSetUID(const char *Cmd
)
1481 uint8_t wipeCard
= 0;
1486 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1487 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1488 PrintAndLog("sample: hf mf csetuid 01020304 w");
1489 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1490 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1494 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1495 PrintAndLog("UID must include 8 HEX symbols");
1499 char ctmp
= param_getchar(Cmd
, 1);
1500 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1502 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1504 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1506 PrintAndLog("Can't set UID. error=%d", res
);
1510 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1514 int CmdHF14AMfCSetBlk(const char *Cmd
)
1517 uint8_t memBlock
[16];
1518 uint8_t blockNo
= 0;
1520 memset(memBlock
, 0x00, sizeof(memBlock
));
1522 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1523 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1524 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1525 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1526 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1530 blockNo
= param_get8(Cmd
, 0);
1532 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1533 PrintAndLog("block data must include 32 HEX symbols");
1537 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1539 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1541 PrintAndLog("Can't write block. error=%d", res
);
1545 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1549 int CmdHF14AMfCLoad(const char *Cmd
)
1553 char * fnameptr
= filename
;
1556 uint8_t fillFromEmulator
= 0;
1557 int i
, len
, blockNum
, flags
;
1559 memset(filename
, 0, sizeof(filename
));
1560 memset(buf
, 0, sizeof(buf
));
1562 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1563 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1564 PrintAndLog("or from emulator memory (option `e`)");
1565 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1566 PrintAndLog(" or: hf mf cload e ");
1567 PrintAndLog(" sample: hf mf cload filename");
1571 char ctmp
= param_getchar(Cmd
, 0);
1572 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1574 if (fillFromEmulator
) {
1575 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1576 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1577 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1578 PrintAndLog("Cant get block: %d", blockNum
);
1582 if (blockNum
== 2) flags
= 0;
1583 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1585 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1586 PrintAndLog("Cant set magic card block: %d", blockNum
);
1593 if (len
> 14) len
= 14;
1595 memcpy(filename
, Cmd
, len
);
1598 sprintf(fnameptr
, ".eml");
1601 f
= fopen(filename
, "r");
1603 PrintAndLog("File not found or locked.");
1608 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1610 memset(buf
, 0, sizeof(buf
));
1611 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1612 PrintAndLog("File reading error.");
1616 if (strlen(buf
) < 32){
1617 if(strlen(buf
) && feof(f
))
1619 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1622 for (i
= 0; i
< 32; i
+= 2)
1623 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1625 if (blockNum
== 2) flags
= 0;
1626 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1628 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1629 PrintAndLog("Cant set magic card block: %d", blockNum
);
1634 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1638 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1639 PrintAndLog("File content error. There must be 64 blocks");
1642 PrintAndLog("Loaded from file: %s", filename
);
1647 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1648 uint8_t memBlock
[16];
1649 uint8_t blockNo
= 0;
1651 memset(memBlock
, 0x00, sizeof(memBlock
));
1653 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1654 PrintAndLog("Usage: hf mf cgetblk <block number>");
1655 PrintAndLog("sample: hf mf cgetblk 1");
1656 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1660 blockNo
= param_get8(Cmd
, 0);
1662 PrintAndLog("--block number:%02x ", blockNo
);
1664 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1666 PrintAndLog("Can't read block. error=%d", res
);
1670 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1674 int CmdHF14AMfCGetSc(const char *Cmd
) {
1675 uint8_t memBlock
[16];
1676 uint8_t sectorNo
= 0;
1678 memset(memBlock
, 0x00, sizeof(memBlock
));
1680 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1681 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1682 PrintAndLog("sample: hf mf cgetsc 0");
1683 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1687 sectorNo
= param_get8(Cmd
, 0);
1688 if (sectorNo
> 15) {
1689 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1693 PrintAndLog("--sector number:%02x ", sectorNo
);
1695 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1696 for (i
= 0; i
< 4; i
++) {
1697 if (i
== 1) flags
= 0;
1698 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1700 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1702 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1706 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1711 int CmdHF14AMfCSave(const char *Cmd
) {
1715 char * fnameptr
= filename
;
1716 uint8_t fillFromEmulator
= 0;
1718 int i
, j
, len
, flags
;
1720 memset(filename
, 0, sizeof(filename
));
1721 memset(buf
, 0, sizeof(buf
));
1723 if (param_getchar(Cmd
, 0) == 'h') {
1724 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1725 PrintAndLog("or into emulator memory (option `e`)");
1726 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1727 PrintAndLog(" sample: hf mf esave ");
1728 PrintAndLog(" hf mf esave filename");
1729 PrintAndLog(" hf mf esave e \n");
1733 char ctmp
= param_getchar(Cmd
, 0);
1734 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1736 if (fillFromEmulator
) {
1737 // put into emulator
1738 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1739 for (i
= 0; i
< 16 * 4; i
++) {
1740 if (i
== 1) flags
= 0;
1741 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1743 if (mfCGetBlock(i
, buf
, flags
)) {
1744 PrintAndLog("Cant get block: %d", i
);
1748 if (mfEmlSetMem(buf
, i
, 1)) {
1749 PrintAndLog("Cant set emul block: %d", i
);
1756 if (len
> 14) len
= 14;
1760 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1761 PrintAndLog("Cant get block: %d", 0);
1764 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1765 sprintf(fnameptr
, "%02x", buf
[j
]);
1767 memcpy(filename
, Cmd
, len
);
1771 sprintf(fnameptr
, ".eml");
1774 f
= fopen(filename
, "w+");
1777 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1778 for (i
= 0; i
< 16 * 4; i
++) {
1779 if (i
== 1) flags
= 0;
1780 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1782 if (mfCGetBlock(i
, buf
, flags
)) {
1783 PrintAndLog("Cant get block: %d", i
);
1786 for (j
= 0; j
< 16; j
++)
1787 fprintf(f
, "%02x", buf
[j
]);
1792 PrintAndLog("Saved to file: %s", filename
);
1798 int CmdHF14AMfSniff(const char *Cmd
){
1800 bool wantLogToFile
= 0;
1801 bool wantDecrypt
= 0;
1802 //bool wantSaveToEml = 0; TODO
1803 bool wantSaveToEmlFile
= 0;
1817 uint8_t * bufPtr
= buf
;
1818 memset(buf
, 0x00, 3000);
1820 if (param_getchar(Cmd
, 0) == 'h') {
1821 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1822 PrintAndLog("You can specify:");
1823 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1824 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1825 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1826 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1827 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1828 PrintAndLog(" sample: hf mf sniff l d e");
1832 for (int i
= 0; i
< 4; i
++) {
1833 char ctmp
= param_getchar(Cmd
, i
);
1834 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1835 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1836 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1837 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1840 printf("-------------------------------------------------------------------------\n");
1841 printf("Executing command. \n");
1842 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1843 printf("Press the key on pc keyboard to abort the client.\n");
1844 printf("-------------------------------------------------------------------------\n");
1846 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1855 printf("\naborted via keyboard!\n");
1860 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1861 res
= resp
.arg
[0] & 0xff;
1865 if (res
== 0) return 0;
1869 memset(buf
, 0x00, 3000);
1871 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1876 blockLen
= bufPtr
- buf
;
1879 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1881 while (bufPtr
- buf
+ 9 < blockLen
) {
1882 isTag
= bufPtr
[3] & 0x80 ? true:false;
1884 parity
= *((uint32_t *)(bufPtr
));
1888 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1889 memcpy(uid
, bufPtr
+ 2, 7);
1890 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1893 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1894 if (wantLogToFile
) {
1895 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1896 AddLogCurrentDT(logHexFileName
);
1898 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1900 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1901 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1902 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1913 static command_t CommandTable
[] =
1915 {"help", CmdHelp
, 1, "This help"},
1916 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1917 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1918 {"urdbl", CmdHF14AMfURdBl
, 0, "Read MIFARE Ultralight block"},
1919 {"urdcard", CmdHF14AMfURdCard
, 0,"Read MIFARE Ultralight Card"},
1920 {"uwrbl", CmdHF14AMfUWrBl
, 0,"Write MIFARE Ultralight block"},
1921 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1922 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1923 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1924 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1925 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1926 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1927 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1928 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1929 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1930 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1931 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1932 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1933 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1934 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1935 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1936 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1937 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1938 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1939 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1940 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1941 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1942 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1943 {NULL
, NULL
, 0, NULL
}
1946 int CmdHFMF(const char *Cmd
)
1949 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1951 CmdsParse(CommandTable
, Cmd
);
1955 int CmdHelp(const char *Cmd
)
1957 CmdsHelp(CommandTable
);