]>
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 //-----------------------------------------------------------------------------
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
)
147 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
151 PrintAndLog("Usage: hf mf uwrbl <block number> <block data (8 hex symbols)> <w>");
152 PrintAndLog(" sample: hf mf uwrbl 0 01020304");
156 blockNo
= param_get8(Cmd
, 0);
157 if (param_gethex(Cmd
, 1, bldata
, 8)) {
158 PrintAndLog("Block data must include 8 HEX symbols");
162 if (strchr(Cmd
,'w') != 0) {
169 PrintAndLog("Access Denied");
171 PrintAndLog("--specialblock no:%02x", blockNo
);
172 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
173 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
174 memcpy(d
.d
.asBytes
,bldata
, 4);
177 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
178 uint8_t isOK
= resp
.arg
[0] & 0xff;
179 PrintAndLog("isOk:%02x", isOK
);
181 PrintAndLog("Command execute timeout");
187 PrintAndLog("Access Denied");
189 PrintAndLog("--specialblock no:%02x", blockNo
);
190 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
191 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
192 memcpy(d
.d
.asBytes
,bldata
, 4);
195 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
196 uint8_t isOK
= resp
.arg
[0] & 0xff;
197 PrintAndLog("isOk:%02x", isOK
);
199 PrintAndLog("Command execute timeout");
205 PrintAndLog("Access Denied");
207 PrintAndLog("--specialblock no:%02x", blockNo
);
208 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
209 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
210 memcpy(c
.d
.asBytes
, bldata
, 4);
213 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
214 uint8_t isOK
= resp
.arg
[0] & 0xff;
215 PrintAndLog("isOk:%02x", isOK
);
217 PrintAndLog("Command execute timeout");
222 PrintAndLog("--specialblock no:%02x", blockNo
);
223 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
224 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
225 memcpy(d
.d
.asBytes
,bldata
, 4);
228 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
229 uint8_t isOK
= resp
.arg
[0] & 0xff;
230 PrintAndLog("isOk:%02x", isOK
);
232 PrintAndLog("Command execute timeout");
236 PrintAndLog("--block no:%02x", blockNo
);
237 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
238 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
239 memcpy(e
.d
.asBytes
,bldata
, 4);
242 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
243 uint8_t isOK
= resp
.arg
[0] & 0xff;
244 PrintAndLog("isOk:%02x", isOK
);
246 PrintAndLog("Command execute timeout");
253 int CmdHF14AMfRdBl(const char *Cmd
)
257 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
263 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
264 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
268 blockNo
= param_get8(Cmd
, 0);
269 cmdp
= param_getchar(Cmd
, 1);
271 PrintAndLog("Key type must be A or B");
274 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
275 if (param_gethex(Cmd
, 2, key
, 12)) {
276 PrintAndLog("Key must include 12 HEX symbols");
279 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo
, keyType
, sprint_hex(key
, 6));
281 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
282 memcpy(c
.d
.asBytes
, key
, 6);
286 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
287 uint8_t isOK
= resp
.arg
[0] & 0xff;
288 uint8_t * data
= resp
.d
.asBytes
;
291 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
293 PrintAndLog("isOk:%02x", isOK
);
295 PrintAndLog("Command execute timeout");
301 int CmdHF14AMfURdBl(const char *Cmd
)
306 PrintAndLog("Usage: hf mf urdbl <block number>");
307 PrintAndLog(" sample: hf mf urdbl 0");
311 blockNo
= param_get8(Cmd
, 0);
312 PrintAndLog("--block no:%02x", blockNo
);
314 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
318 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
319 uint8_t isOK
= resp
.arg
[0] & 0xff;
320 uint8_t * data
= resp
.d
.asBytes
;
323 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 4));
325 PrintAndLog("isOk:%02x", isOK
);
327 PrintAndLog("Command execute timeout");
333 int CmdHF14AMfURdCard(const char *Cmd
)
336 uint8_t sectorNo
= 0;
337 uint8_t *lockbytes_t
=NULL
;
338 uint8_t lockbytes
[2]={0,0};
339 bool bit
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
342 uint8_t * data
= NULL
;
345 PrintAndLog("Sector number must be less than 16");
348 PrintAndLog("Attempting to Read Ultralight... ");
350 UsbCommand c
= {CMD_MIFAREU_READCARD
, {sectorNo
}};
354 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
355 isOK
= resp
.arg
[0] & 0xff;
356 data
= resp
.d
.asBytes
;
358 PrintAndLog("isOk:%02x", isOK
);
360 for (i
= 0; i
< 16; i
++) {
364 lockbytes_t
=data
+(i
*4);
365 lockbytes
[0]=lockbytes_t
[2];
366 lockbytes
[1]=lockbytes_t
[3];
367 for(int j
=0; j
<16; j
++){
368 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
370 //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);
371 //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]);
372 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
375 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
378 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
381 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
384 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
387 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
390 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
393 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
396 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
399 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
402 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
405 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
408 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
411 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
414 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
419 PrintAndLog("Command1 execute timeout");
424 int CmdHF14AMfRdSc(const char *Cmd
)
427 uint8_t sectorNo
= 0;
429 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
432 uint8_t * data
= NULL
;
437 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
438 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
442 sectorNo
= param_get8(Cmd
, 0);
444 PrintAndLog("Sector number must be less than 64");
447 cmdp
= param_getchar(Cmd
, 1);
449 PrintAndLog("Key type must be A or B");
452 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
453 if (param_gethex(Cmd
, 2, key
, 12)) {
454 PrintAndLog("Key must include 12 HEX symbols");
457 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo
, keyType
, sprint_hex(key
, 6));
459 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
460 memcpy(c
.d
.asBytes
, key
, 6);
465 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
466 isOK
= resp
.arg
[0] & 0xff;
467 data
= resp
.d
.asBytes
;
469 PrintAndLog("isOk:%02x", isOK
);
471 for (i
= 0; i
< 2; i
++) {
472 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
475 PrintAndLog("Command1 execute timeout");
480 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
481 isOK
= resp
.arg
[0] & 0xff;
482 data
= resp
.d
.asBytes
;
485 for (i
= 0; i
< 2; i
++) {
486 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
489 PrintAndLog("Command2 execute timeout");
495 int CmdHF14AMfDump(const char *Cmd
)
501 uint8_t rights
[40][4];
508 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
509 PrintAndLog("Could not find file dumpkeys.bin");
513 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
514 PrintAndLog("Could not create file name dumpdata.bin");
520 for (i
=0 ; i
<16 ; i
++) {
521 if (fread( keyA
[i
], 1, 6, fin
) == 0) {
522 PrintAndLog("File reading error.");
526 for (i
=0 ; i
<16 ; i
++) {
527 if (fread( keyB
[i
], 1, 6, fin
) == 0) {
528 PrintAndLog("File reading error.");
533 // Read access rights to sectors
535 PrintAndLog("|-----------------------------------------|");
536 PrintAndLog("|------ Reading sector access bits...-----|");
537 PrintAndLog("|-----------------------------------------|");
539 for (i
= 0 ; i
< 16 ; i
++) {
540 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
541 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
544 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
545 uint8_t isOK
= resp
.arg
[0] & 0xff;
546 uint8_t *data
= resp
.d
.asBytes
;
548 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
549 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
550 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
551 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
554 PrintAndLog("Could not get access rights for block %d", i
);
558 PrintAndLog("Command execute timeout");
562 // Read blocks and print to file
564 PrintAndLog("|-----------------------------------------|");
565 PrintAndLog("|----- Dumping all blocks to file... -----|");
566 PrintAndLog("|-----------------------------------------|");
569 for (i
=0 ; i
<16 ; i
++) {
570 for (j
=0 ; j
<4 ; j
++) {
571 bool received
= false;
574 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
575 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
577 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
580 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
581 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
582 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
584 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
586 else if (rights
[i
][j
] == 7) {
587 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
590 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
591 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
593 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
598 uint8_t isOK
= resp
.arg
[0] & 0xff;
599 uint8_t *data
= resp
.d
.asBytes
;
601 data
[0] = (keyA
[i
][0]);
602 data
[1] = (keyA
[i
][1]);
603 data
[2] = (keyA
[i
][2]);
604 data
[3] = (keyA
[i
][3]);
605 data
[4] = (keyA
[i
][4]);
606 data
[5] = (keyA
[i
][5]);
607 data
[10] = (keyB
[i
][0]);
608 data
[11] = (keyB
[i
][1]);
609 data
[12] = (keyB
[i
][2]);
610 data
[13] = (keyB
[i
][3]);
611 data
[14] = (keyB
[i
][4]);
612 data
[15] = (keyB
[i
][5]);
615 fwrite ( data
, 1, 16, fout
);
616 PrintAndLog("Dumped card data into 'dumpdata.bin'");
620 PrintAndLog("Could not get access rights for block %d", i
);
624 PrintAndLog("Command execute timeout");
634 int CmdHF14AMfRestore(const char *Cmd
)
639 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
640 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
647 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
648 PrintAndLog("Could not find file dumpdata.bin");
651 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
652 PrintAndLog("Could not find file dumpkeys.bin");
656 for (i
=0 ; i
<16 ; i
++) {
657 if (fread(keyA
[i
], 1, 6, fkeys
) == 0) {
658 PrintAndLog("File reading error.");
662 for (i
=0 ; i
<16 ; i
++) {
663 if (fread(keyB
[i
], 1, 6, fkeys
) == 0) {
664 PrintAndLog("File reading error.");
669 PrintAndLog("Restoring dumpdata.bin to card");
671 for (i
=0 ; i
<16 ; i
++) {
672 for( j
=0 ; j
<4 ; j
++) {
673 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
674 memcpy(c
.d
.asBytes
, key
, 6);
676 if (fread(bldata
, 1, 16, fdump
) == 0) {
677 PrintAndLog("File reading error.");
682 bldata
[0] = (keyA
[i
][0]);
683 bldata
[1] = (keyA
[i
][1]);
684 bldata
[2] = (keyA
[i
][2]);
685 bldata
[3] = (keyA
[i
][3]);
686 bldata
[4] = (keyA
[i
][4]);
687 bldata
[5] = (keyA
[i
][5]);
688 bldata
[10] = (keyB
[i
][0]);
689 bldata
[11] = (keyB
[i
][1]);
690 bldata
[12] = (keyB
[i
][2]);
691 bldata
[13] = (keyB
[i
][3]);
692 bldata
[14] = (keyB
[i
][4]);
693 bldata
[15] = (keyB
[i
][5]);
696 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
699 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
702 if ((ch != 'y') && (ch != 'Y')){
703 PrintAndLog("Aborting !");
708 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
712 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
713 uint8_t isOK
= resp
.arg
[0] & 0xff;
714 PrintAndLog("isOk:%02x", isOK
);
716 PrintAndLog("Command execute timeout");
726 int CmdHF14AMfNested(const char *Cmd
)
728 int i
, j
, res
, iterations
;
729 sector
* e_sector
= NULL
;
732 uint8_t trgBlockNo
= 0;
733 uint8_t trgKeyType
= 0;
736 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
737 uint8_t keyBlock
[6*6];
739 int transferToEml
= 0;
741 int createDumpFile
= 0;
743 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
744 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
749 PrintAndLog("Usage:");
750 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
751 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
752 PrintAndLog(" <target block number> <target key A/B> [t]");
753 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
754 PrintAndLog("t - transfer keys into emulator memory");
755 PrintAndLog("d - write keys to binary file");
757 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
758 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
759 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
760 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
764 cmdp
= param_getchar(Cmd
, 0);
765 blockNo
= param_get8(Cmd
, 1);
766 ctmp
= param_getchar(Cmd
, 2);
768 PrintAndLog("Key type must be A or B");
771 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
772 if (param_gethex(Cmd
, 3, key
, 12)) {
773 PrintAndLog("Key must include 12 HEX symbols");
777 if (cmdp
== 'o' || cmdp
== 'O') {
779 trgBlockNo
= param_get8(Cmd
, 4);
780 ctmp
= param_getchar(Cmd
, 5);
782 PrintAndLog("Target key type must be A or B");
785 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
788 case '0': SectorsCnt
= 05; break;
789 case '1': SectorsCnt
= 16; break;
790 case '2': SectorsCnt
= 32; break;
791 case '4': SectorsCnt
= 64; break;
792 default: SectorsCnt
= 16;
796 ctmp
= param_getchar(Cmd
, 4);
797 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
798 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
800 ctmp
= param_getchar(Cmd
, 6);
801 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
802 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
804 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
806 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
809 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true)) {
810 PrintAndLog("Nested error.");
813 key64
= bytes_to_num(keyBlock
, 6);
815 PrintAndLog("Found valid key:%012"llx
, key64
);
817 // transfer key to the emulator
819 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
822 num_to_bytes(key64
, 6, keyBlock
);
824 num_to_bytes(key64
, 6, &keyBlock
[10]);
825 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
828 PrintAndLog("No valid key found");
831 else { // ------------------------------------ multiple sectors working
835 blDiff
= blockNo
% 4;
836 PrintAndLog("Block shift=%d", blDiff
);
837 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
838 if (e_sector
== NULL
) return 1;
840 //test current key 4 sectors
841 memcpy(keyBlock
, key
, 6);
842 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
843 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
844 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
845 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
846 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
848 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
849 for (i
= 0; i
< SectorsCnt
; i
++) {
850 for (j
= 0; j
< 2; j
++) {
851 if (e_sector
[i
].foundKey
[j
]) continue;
853 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
856 e_sector
[i
].Key
[j
] = key64
;
857 e_sector
[i
].foundKey
[j
] = 1;
865 PrintAndLog("nested...");
866 bool calibrate
= true;
867 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
868 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4) {
869 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
870 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
871 PrintAndLog("-----------------------------------------------");
872 if(mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, calibrate
)) {
873 PrintAndLog("Nested error.\n");
882 key64
= bytes_to_num(keyBlock
, 6);
884 PrintAndLog("Found valid key:%012"llx
, key64
);
885 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
886 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
892 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
);
894 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
896 PrintAndLog("|---|----------------|---|----------------|---|");
897 PrintAndLog("|sec|key A |res|key B |res|");
898 PrintAndLog("|---|----------------|---|----------------|---|");
899 for (i
= 0; i
< SectorsCnt
; i
++) {
900 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
901 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
903 PrintAndLog("|---|----------------|---|----------------|---|");
905 // transfer them to the emulator
907 for (i
= 0; i
< SectorsCnt
; i
++) {
908 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
909 if (e_sector
[i
].foundKey
[0])
910 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
911 if (e_sector
[i
].foundKey
[1])
912 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
913 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
918 if (createDumpFile
) {
919 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
920 PrintAndLog("Could not create file dumpkeys.bin");
924 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
925 for(i
=0; i
<16; i
++) {
926 if (e_sector
[i
].foundKey
[0]){
927 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
928 fwrite ( tempkey
, 1, 6, fkeys
);
931 fwrite ( &standart
, 1, 6, fkeys
);
934 for(i
=0; i
<16; i
++) {
935 if (e_sector
[i
].foundKey
[1]){
936 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
937 fwrite ( tempkey
, 1, 6, fkeys
);
940 fwrite ( &standart
, 1, 6, fkeys
);
953 get_trailer_block (uint32_t uiBlock
)
955 // Test if we are in the small or big sectors
956 uint32_t trailer_block
= 0;
958 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
960 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
962 return trailer_block
;
964 int CmdHF14AMfChk(const char *Cmd
)
967 char filename
[256]={0};
969 uint8_t *keyBlock
= NULL
, *p
;
970 uint8_t stKeyBlock
= 20;
976 uint8_t SectorsCnt
= 1;
980 int transferToEml
= 0;
981 int createDumpFile
= 0;
983 keyBlock
= calloc(stKeyBlock
, 6);
984 if (keyBlock
== NULL
) return 1;
986 uint64_t defaultKeys
[] =
988 0xffffffffffff, // Default key (first key used by program if no user defined key)
989 0x000000000000, // Blank key
990 0xa0a1a2a3a4a5, // NFCForum MAD key
1002 int defaultKeysSize
= (sizeof(defaultKeys
) / 7) - 1;
1004 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1006 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1009 if (strlen(Cmd
)<3) {
1010 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
1011 PrintAndLog(" * - all sectors");
1012 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
1013 // PrintAndLog("d - write keys to binary file\n");
1015 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
1016 PrintAndLog(" hf mf chk *1 ? t");
1020 if (param_getchar(Cmd
, 0)=='*') {
1022 switch(param_getchar(Cmd
+1, 0)) {
1023 case '0': SectorsCnt
= 5; break;
1024 case '1': SectorsCnt
= 16; break;
1025 case '2': SectorsCnt
= 32; break;
1026 case '4': SectorsCnt
= 40; break;
1027 default: SectorsCnt
= 16;
1031 blockNo
= param_get8(Cmd
, 0);
1033 ctmp
= param_getchar(Cmd
, 1);
1045 PrintAndLog("Key type must be A , B or ?");
1049 ctmp
= param_getchar(Cmd
, 2);
1050 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1051 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1053 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1054 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1055 if ( stKeyBlock
- keycnt
< 2) {
1056 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1058 PrintAndLog("Cannot allocate memory for Keys");
1064 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
1065 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1066 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1069 // May be a dic file
1070 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
1071 PrintAndLog("File name too long");
1076 if ( (f
= fopen( filename
, "r")) ) {
1077 while( fgets(buf
, sizeof(buf
), f
) ){
1078 if (strlen(buf
) < 12 || buf
[11] == '\n')
1081 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1083 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1085 if (!isxdigit(buf
[0])){
1086 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1092 if ( stKeyBlock
- keycnt
< 2) {
1093 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1095 PrintAndLog("Cannot allocate memory for defKeys");
1101 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1102 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1103 PrintAndLog("chk custom key[%d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1105 memset(buf
, 0, sizeof(buf
));
1108 PrintAndLog("File: %s: not found or locked.", filename
);
1117 PrintAndLog("No key specified,try default keys");
1118 for (;keycnt
< defaultKeysSize
; keycnt
++)
1119 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
1120 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1121 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1124 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
1126 for (int i
=0; i
<SectorsCnt
; ++i
) {
1127 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
1128 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1129 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1130 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1131 res
= mfCheckKeys(b
, t
, size
, &keyBlock
[6*c
], &key64
);
1134 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1135 if (transferToEml
) {
1137 mfEmlGetMem(block
, get_trailer_block(b
), 1);
1138 num_to_bytes(key64
, 6, block
+ t
*10);
1139 mfEmlSetMem(block
, get_trailer_block(b
), 1);
1143 PrintAndLog("Command execute timeout");
1146 b
<127?(b
+=4):(b
+=16);
1154 if (createDumpFile) {
1155 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
1156 PrintAndLog("Could not create file dumpkeys.bin");
1160 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
1161 for(i=0; i<16; i++) {
1162 if (e_sector[i].foundKey[0]){
1163 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
1164 fwrite ( tempkey, 1, 6, fkeys );
1167 fwrite ( &standart, 1, 6, fkeys );
1170 for(i=0; i<16; i++) {
1171 if (e_sector[i].foundKey[1]){
1172 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
1173 fwrite ( tempkey, 1, 6, fkeys );
1176 fwrite ( &standart, 1, 6, fkeys );
1185 int CmdHF14AMf1kSim(const char *Cmd
)
1187 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1188 uint8_t exitAfterNReads
= 0;
1191 if (param_getchar(Cmd
, 0) == 'h') {
1192 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1193 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1194 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1195 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1196 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1197 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
1201 if (param_getchar(Cmd
, pnr
) == 'u') {
1202 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1204 flags
|=FLAG_4B_UID_IN_DATA
; // UID from packet
1205 }else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0)
1207 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1210 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1215 if (param_getchar(Cmd
, pnr
) == 'n') {
1216 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1219 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1220 //Using a flag to signal interactiveness, least significant bit
1221 flags
|= FLAG_INTERACTIVE
;
1225 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1226 //Using a flag to signal interactiveness, least significant bit
1227 flags
|= FLAG_NR_AR_ATTACK
;
1229 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1230 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1231 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1232 , exitAfterNReads
, flags
,flags
);
1235 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1236 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1239 if(flags
& FLAG_INTERACTIVE
)
1242 PrintAndLog("Press pm3-button to abort simulation");
1243 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1244 //We're waiting only 1.5 s at a time, otherwise we get the
1245 // annoying message about "Waiting for a response... "
1252 int CmdHF14AMfDbg(const char *Cmd
)
1254 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1256 PrintAndLog("Max debud mode parameter is 4 \n");
1259 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1260 PrintAndLog("Usage: hf mf dbg <debug level>");
1261 PrintAndLog(" 0 - no debug messages");
1262 PrintAndLog(" 1 - error messages");
1263 PrintAndLog(" 2 - all messages");
1264 PrintAndLog(" 4 - extended debug mode");
1268 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1274 int CmdHF14AMfEGet(const char *Cmd
)
1276 uint8_t blockNo
= 0;
1277 uint8_t data
[3 * 16];
1280 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1281 PrintAndLog("Usage: hf mf eget <block number>");
1282 PrintAndLog(" sample: hf mf eget 0 ");
1286 blockNo
= param_get8(Cmd
, 0);
1289 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1290 for (i
= 0; i
< 3; i
++) {
1291 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1294 PrintAndLog("Command execute timeout");
1300 int CmdHF14AMfEClear(const char *Cmd
)
1302 if (param_getchar(Cmd
, 0) == 'h') {
1303 PrintAndLog("Usage: hf mf eclr");
1304 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1308 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1313 int CmdHF14AMfESet(const char *Cmd
)
1315 uint8_t memBlock
[16];
1316 uint8_t blockNo
= 0;
1318 memset(memBlock
, 0x00, sizeof(memBlock
));
1320 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1321 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1322 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1326 blockNo
= param_get8(Cmd
, 0);
1328 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1329 PrintAndLog("block data must include 32 HEX symbols");
1334 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1335 memcpy(c
.d
.asBytes
, memBlock
, 16);
1340 int CmdHF14AMfELoad(const char *Cmd
)
1344 char * fnameptr
= filename
;
1347 int i
, len
, blockNum
;
1349 memset(filename
, 0, sizeof(filename
));
1350 memset(buf
, 0, sizeof(buf
));
1352 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1353 PrintAndLog("It loads emul dump from the file `filename.eml`");
1354 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1355 PrintAndLog(" sample: hf mf eload filename");
1360 if (len
> 14) len
= 14;
1362 memcpy(filename
, Cmd
, len
);
1365 sprintf(fnameptr
, ".eml");
1368 f
= fopen(filename
, "r");
1370 PrintAndLog("File not found or locked.");
1376 memset(buf
, 0, sizeof(buf
));
1377 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1378 if(blockNum
== 16 * 4)
1382 PrintAndLog("File reading error.");
1386 if (strlen(buf
) < 32){
1387 if(strlen(buf
) && feof(f
))
1389 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1392 for (i
= 0; i
< 32; i
+= 2)
1393 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1394 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1396 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1397 PrintAndLog("Cant set emul block: %d", blockNum
);
1402 if (blockNum
>= 32 * 4 + 8 * 16) break;
1406 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1407 PrintAndLog("File content error. There must be 64 blocks");
1410 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1414 int CmdHF14AMfESave(const char *Cmd
)
1418 char * fnameptr
= filename
;
1422 memset(filename
, 0, sizeof(filename
));
1423 memset(buf
, 0, sizeof(buf
));
1425 if (param_getchar(Cmd
, 0) == 'h') {
1426 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1427 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1428 PrintAndLog(" sample: hf mf esave ");
1429 PrintAndLog(" hf mf esave filename");
1434 if (len
> 14) len
= 14;
1438 if (mfEmlGetMem(buf
, 0, 1)) {
1439 PrintAndLog("Cant get block: %d", 0);
1442 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1443 sprintf(fnameptr
, "%02x", buf
[j
]);
1445 memcpy(filename
, Cmd
, len
);
1449 sprintf(fnameptr
, ".eml");
1452 f
= fopen(filename
, "w+");
1455 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1456 if (mfEmlGetMem(buf
, i
, 1)) {
1457 PrintAndLog("Cant get block: %d", i
);
1460 for (j
= 0; j
< 16; j
++)
1461 fprintf(f
, "%02x", buf
[j
]);
1466 PrintAndLog("Saved to file: %s", filename
);
1471 int CmdHF14AMfECFill(const char *Cmd
)
1473 uint8_t keyType
= 0;
1475 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1476 PrintAndLog("Usage: hf mf efill <key A/B>");
1477 PrintAndLog("sample: hf mf efill A");
1478 PrintAndLog("Card data blocks transfers to card emulator memory.");
1479 PrintAndLog("Keys must be laid in the simulator memory. \n");
1483 char ctmp
= param_getchar(Cmd
, 0);
1485 PrintAndLog("Key type must be A or B");
1488 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1490 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1495 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1499 uint64_t keyA
, keyB
;
1501 PrintAndLog("|---|----------------|----------------|");
1502 PrintAndLog("|sec|key A |key B |");
1503 PrintAndLog("|---|----------------|----------------|");
1504 for (i
= 0; i
< 40; i
++) {
1505 b
<127?(b
+=4):(b
+=16);
1506 if (mfEmlGetMem(data
, b
, 1)) {
1507 PrintAndLog("error get block %d", b
);
1510 keyA
= bytes_to_num(data
, 6);
1511 keyB
= bytes_to_num(data
+ 10, 6);
1512 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1514 PrintAndLog("|---|----------------|----------------|");
1519 int CmdHF14AMfCSetUID(const char *Cmd
)
1521 uint8_t wipeCard
= 0;
1526 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1527 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1528 PrintAndLog("sample: hf mf csetuid 01020304 w");
1529 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1530 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1534 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1535 PrintAndLog("UID must include 8 HEX symbols");
1539 char ctmp
= param_getchar(Cmd
, 1);
1540 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1542 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1544 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1546 PrintAndLog("Can't set UID. error=%d", res
);
1550 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1554 int CmdHF14AMfCSetBlk(const char *Cmd
)
1557 uint8_t memBlock
[16];
1558 uint8_t blockNo
= 0;
1560 memset(memBlock
, 0x00, sizeof(memBlock
));
1562 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1563 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1564 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1565 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1566 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1570 blockNo
= param_get8(Cmd
, 0);
1572 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1573 PrintAndLog("block data must include 32 HEX symbols");
1577 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1579 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1581 PrintAndLog("Can't write block. error=%d", res
);
1585 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1589 int CmdHF14AMfCLoad(const char *Cmd
)
1593 char * fnameptr
= filename
;
1596 uint8_t fillFromEmulator
= 0;
1597 int i
, len
, blockNum
, flags
;
1599 memset(filename
, 0, sizeof(filename
));
1600 memset(buf
, 0, sizeof(buf
));
1602 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1603 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1604 PrintAndLog("or from emulator memory (option `e`)");
1605 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1606 PrintAndLog(" or: hf mf cload e ");
1607 PrintAndLog(" sample: hf mf cload filename");
1611 char ctmp
= param_getchar(Cmd
, 0);
1612 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1614 if (fillFromEmulator
) {
1615 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1616 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1617 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1618 PrintAndLog("Cant get block: %d", blockNum
);
1622 if (blockNum
== 2) flags
= 0;
1623 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1625 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1626 PrintAndLog("Cant set magic card block: %d", blockNum
);
1633 if (len
> 14) len
= 14;
1635 memcpy(filename
, Cmd
, len
);
1638 sprintf(fnameptr
, ".eml");
1641 f
= fopen(filename
, "r");
1643 PrintAndLog("File not found or locked.");
1648 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1650 memset(buf
, 0, sizeof(buf
));
1651 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1652 PrintAndLog("File reading error.");
1656 if (strlen(buf
) < 32){
1657 if(strlen(buf
) && feof(f
))
1659 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1662 for (i
= 0; i
< 32; i
+= 2)
1663 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1665 if (blockNum
== 2) flags
= 0;
1666 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1668 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1669 PrintAndLog("Cant set magic card block: %d", blockNum
);
1674 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1678 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1679 PrintAndLog("File content error. There must be 64 blocks");
1682 PrintAndLog("Loaded from file: %s", filename
);
1687 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1688 uint8_t memBlock
[16];
1689 uint8_t blockNo
= 0;
1691 memset(memBlock
, 0x00, sizeof(memBlock
));
1693 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1694 PrintAndLog("Usage: hf mf cgetblk <block number>");
1695 PrintAndLog("sample: hf mf cgetblk 1");
1696 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1700 blockNo
= param_get8(Cmd
, 0);
1702 PrintAndLog("--block number:%02x ", blockNo
);
1704 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1706 PrintAndLog("Can't read block. error=%d", res
);
1710 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1714 int CmdHF14AMfCGetSc(const char *Cmd
) {
1715 uint8_t memBlock
[16];
1716 uint8_t sectorNo
= 0;
1718 memset(memBlock
, 0x00, sizeof(memBlock
));
1720 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1721 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1722 PrintAndLog("sample: hf mf cgetsc 0");
1723 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1727 sectorNo
= param_get8(Cmd
, 0);
1728 if (sectorNo
> 15) {
1729 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1733 PrintAndLog("--sector number:%02x ", sectorNo
);
1735 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1736 for (i
= 0; i
< 4; i
++) {
1737 if (i
== 1) flags
= 0;
1738 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1740 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1742 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1746 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1751 int CmdHF14AMfCSave(const char *Cmd
) {
1755 char * fnameptr
= filename
;
1756 uint8_t fillFromEmulator
= 0;
1758 int i
, j
, len
, flags
;
1760 memset(filename
, 0, sizeof(filename
));
1761 memset(buf
, 0, sizeof(buf
));
1763 if (param_getchar(Cmd
, 0) == 'h') {
1764 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1765 PrintAndLog("or into emulator memory (option `e`)");
1766 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1767 PrintAndLog(" sample: hf mf esave ");
1768 PrintAndLog(" hf mf esave filename");
1769 PrintAndLog(" hf mf esave e \n");
1773 char ctmp
= param_getchar(Cmd
, 0);
1774 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1776 if (fillFromEmulator
) {
1777 // put into emulator
1778 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1779 for (i
= 0; i
< 16 * 4; i
++) {
1780 if (i
== 1) flags
= 0;
1781 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1783 if (mfCGetBlock(i
, buf
, flags
)) {
1784 PrintAndLog("Cant get block: %d", i
);
1788 if (mfEmlSetMem(buf
, i
, 1)) {
1789 PrintAndLog("Cant set emul block: %d", i
);
1796 if (len
> 14) len
= 14;
1800 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1801 PrintAndLog("Cant get block: %d", 0);
1804 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1805 sprintf(fnameptr
, "%02x", buf
[j
]);
1807 memcpy(filename
, Cmd
, len
);
1811 sprintf(fnameptr
, ".eml");
1814 f
= fopen(filename
, "w+");
1817 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1818 for (i
= 0; i
< 16 * 4; i
++) {
1819 if (i
== 1) flags
= 0;
1820 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1822 if (mfCGetBlock(i
, buf
, flags
)) {
1823 PrintAndLog("Cant get block: %d", i
);
1826 for (j
= 0; j
< 16; j
++)
1827 fprintf(f
, "%02x", buf
[j
]);
1832 PrintAndLog("Saved to file: %s", filename
);
1838 int CmdHF14AMfSniff(const char *Cmd
){
1840 bool wantLogToFile
= 0;
1841 bool wantDecrypt
= 0;
1842 //bool wantSaveToEml = 0; TODO
1843 bool wantSaveToEmlFile
= 0;
1857 uint8_t * bufPtr
= buf
;
1858 memset(buf
, 0x00, 3000);
1860 if (param_getchar(Cmd
, 0) == 'h') {
1861 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1862 PrintAndLog("You can specify:");
1863 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1864 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1865 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1866 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1867 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1868 PrintAndLog(" sample: hf mf sniff l d e");
1872 for (int i
= 0; i
< 4; i
++) {
1873 char ctmp
= param_getchar(Cmd
, i
);
1874 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1875 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1876 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1877 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1880 printf("-------------------------------------------------------------------------\n");
1881 printf("Executing command. \n");
1882 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1883 printf("Press the key on pc keyboard to abort the client.\n");
1884 printf("-------------------------------------------------------------------------\n");
1886 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1895 printf("\naborted via keyboard!\n");
1900 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1901 res
= resp
.arg
[0] & 0xff;
1905 if (res
== 0) return 0;
1909 memset(buf
, 0x00, 3000);
1911 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1916 blockLen
= bufPtr
- buf
;
1919 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1921 while (bufPtr
- buf
+ 9 < blockLen
) {
1922 isTag
= bufPtr
[3] & 0x80 ? true:false;
1924 parity
= *((uint32_t *)(bufPtr
));
1928 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1929 memcpy(uid
, bufPtr
+ 2, 7);
1930 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1933 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1934 if (wantLogToFile
) {
1935 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1936 AddLogCurrentDT(logHexFileName
);
1938 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1940 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1941 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1942 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1953 static command_t CommandTable
[] =
1955 {"help", CmdHelp
, 1, "This help"},
1956 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1957 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1958 {"urdbl", CmdHF14AMfURdBl
, 0, "Read MIFARE Ultralight block"},
1959 {"urdcard", CmdHF14AMfURdCard
, 0,"Read MIFARE Ultralight Card"},
1960 {"uwrbl", CmdHF14AMfUWrBl
, 0,"Write MIFARE Ultralight block"},
1961 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1962 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1963 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1964 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1965 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1966 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1967 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1968 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1969 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1970 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1971 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1972 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1973 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1974 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1975 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1976 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1977 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1978 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1979 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1980 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1981 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1982 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1983 {NULL
, NULL
, 0, NULL
}
1986 int CmdHFMF(const char *Cmd
)
1989 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1991 CmdsParse(CommandTable
, Cmd
);
1995 int CmdHelp(const char *Cmd
)
1997 CmdsHelp(CommandTable
);