]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
6075db533092957b82ae9c4a789fd8d5f7a76859
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 u 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 - plus information messages");
1264 PrintAndLog(" 3 - plus debug messages");
1265 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1266 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1270 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1276 int CmdHF14AMfEGet(const char *Cmd
)
1278 uint8_t blockNo
= 0;
1279 uint8_t data
[3 * 16];
1282 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1283 PrintAndLog("Usage: hf mf eget <block number>");
1284 PrintAndLog(" sample: hf mf eget 0 ");
1288 blockNo
= param_get8(Cmd
, 0);
1291 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1292 for (i
= 0; i
< 3; i
++) {
1293 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1296 PrintAndLog("Command execute timeout");
1302 int CmdHF14AMfEClear(const char *Cmd
)
1304 if (param_getchar(Cmd
, 0) == 'h') {
1305 PrintAndLog("Usage: hf mf eclr");
1306 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1310 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1315 int CmdHF14AMfESet(const char *Cmd
)
1317 uint8_t memBlock
[16];
1318 uint8_t blockNo
= 0;
1320 memset(memBlock
, 0x00, sizeof(memBlock
));
1322 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1323 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1324 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1328 blockNo
= param_get8(Cmd
, 0);
1330 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1331 PrintAndLog("block data must include 32 HEX symbols");
1336 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1337 memcpy(c
.d
.asBytes
, memBlock
, 16);
1342 int CmdHF14AMfELoad(const char *Cmd
)
1346 char * fnameptr
= filename
;
1349 int i
, len
, blockNum
;
1351 memset(filename
, 0, sizeof(filename
));
1352 memset(buf
, 0, sizeof(buf
));
1354 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1355 PrintAndLog("It loads emul dump from the file `filename.eml`");
1356 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1357 PrintAndLog(" sample: hf mf eload filename");
1362 if (len
> 14) len
= 14;
1364 memcpy(filename
, Cmd
, len
);
1367 sprintf(fnameptr
, ".eml");
1370 f
= fopen(filename
, "r");
1372 PrintAndLog("File not found or locked.");
1378 memset(buf
, 0, sizeof(buf
));
1379 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1380 if(blockNum
== 16 * 4)
1384 PrintAndLog("File reading error.");
1388 if (strlen(buf
) < 32){
1389 if(strlen(buf
) && feof(f
))
1391 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1394 for (i
= 0; i
< 32; i
+= 2)
1395 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1396 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1398 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1399 PrintAndLog("Cant set emul block: %d", blockNum
);
1404 if (blockNum
>= 32 * 4 + 8 * 16) break;
1408 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1409 PrintAndLog("File content error. There must be 64 blocks");
1412 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1416 int CmdHF14AMfESave(const char *Cmd
)
1420 char * fnameptr
= filename
;
1424 memset(filename
, 0, sizeof(filename
));
1425 memset(buf
, 0, sizeof(buf
));
1427 if (param_getchar(Cmd
, 0) == 'h') {
1428 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1429 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1430 PrintAndLog(" sample: hf mf esave ");
1431 PrintAndLog(" hf mf esave filename");
1436 if (len
> 14) len
= 14;
1440 if (mfEmlGetMem(buf
, 0, 1)) {
1441 PrintAndLog("Cant get block: %d", 0);
1444 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1445 sprintf(fnameptr
, "%02x", buf
[j
]);
1447 memcpy(filename
, Cmd
, len
);
1451 sprintf(fnameptr
, ".eml");
1454 f
= fopen(filename
, "w+");
1457 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1458 if (mfEmlGetMem(buf
, i
, 1)) {
1459 PrintAndLog("Cant get block: %d", i
);
1462 for (j
= 0; j
< 16; j
++)
1463 fprintf(f
, "%02x", buf
[j
]);
1468 PrintAndLog("Saved to file: %s", filename
);
1473 int CmdHF14AMfECFill(const char *Cmd
)
1475 uint8_t keyType
= 0;
1477 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1478 PrintAndLog("Usage: hf mf ecfill <key A/B>");
1479 PrintAndLog("sample: hf mf ecfill A");
1480 PrintAndLog("Card data blocks transfers to card emulator memory.");
1481 PrintAndLog("Keys must be laid in the simulator memory. \n");
1485 char ctmp
= param_getchar(Cmd
, 0);
1487 PrintAndLog("Key type must be A or B");
1490 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1492 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1497 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1501 uint64_t keyA
, keyB
;
1503 PrintAndLog("|---|----------------|----------------|");
1504 PrintAndLog("|sec|key A |key B |");
1505 PrintAndLog("|---|----------------|----------------|");
1506 for (i
= 0; i
< 40; i
++) {
1507 b
<127?(b
+=4):(b
+=16);
1508 if (mfEmlGetMem(data
, b
, 1)) {
1509 PrintAndLog("error get block %d", b
);
1512 keyA
= bytes_to_num(data
, 6);
1513 keyB
= bytes_to_num(data
+ 10, 6);
1514 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1516 PrintAndLog("|---|----------------|----------------|");
1521 int CmdHF14AMfCSetUID(const char *Cmd
)
1523 uint8_t wipeCard
= 0;
1528 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1529 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1530 PrintAndLog("sample: hf mf csetuid 01020304 w");
1531 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1532 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1536 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1537 PrintAndLog("UID must include 8 HEX symbols");
1541 char ctmp
= param_getchar(Cmd
, 1);
1542 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1544 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1546 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1548 PrintAndLog("Can't set UID. error=%d", res
);
1552 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1556 int CmdHF14AMfCSetBlk(const char *Cmd
)
1559 uint8_t memBlock
[16];
1560 uint8_t blockNo
= 0;
1562 memset(memBlock
, 0x00, sizeof(memBlock
));
1564 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1565 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1566 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1567 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1568 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1572 blockNo
= param_get8(Cmd
, 0);
1574 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1575 PrintAndLog("block data must include 32 HEX symbols");
1579 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1581 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1583 PrintAndLog("Can't write block. error=%d", res
);
1587 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1591 int CmdHF14AMfCLoad(const char *Cmd
)
1595 char * fnameptr
= filename
;
1598 uint8_t fillFromEmulator
= 0;
1599 int i
, len
, blockNum
, flags
;
1601 memset(filename
, 0, sizeof(filename
));
1602 memset(buf
, 0, sizeof(buf
));
1604 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1605 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1606 PrintAndLog("or from emulator memory (option `e`)");
1607 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1608 PrintAndLog(" or: hf mf cload e ");
1609 PrintAndLog(" sample: hf mf cload filename");
1613 char ctmp
= param_getchar(Cmd
, 0);
1614 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1616 if (fillFromEmulator
) {
1617 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1618 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1619 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1620 PrintAndLog("Cant get block: %d", blockNum
);
1624 if (blockNum
== 2) flags
= 0;
1625 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1627 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1628 PrintAndLog("Cant set magic card block: %d", blockNum
);
1635 if (len
> 14) len
= 14;
1637 memcpy(filename
, Cmd
, len
);
1640 sprintf(fnameptr
, ".eml");
1643 f
= fopen(filename
, "r");
1645 PrintAndLog("File not found or locked.");
1650 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1652 memset(buf
, 0, sizeof(buf
));
1653 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1654 PrintAndLog("File reading error.");
1658 if (strlen(buf
) < 32){
1659 if(strlen(buf
) && feof(f
))
1661 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1664 for (i
= 0; i
< 32; i
+= 2)
1665 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1667 if (blockNum
== 2) flags
= 0;
1668 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1670 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1671 PrintAndLog("Cant set magic card block: %d", blockNum
);
1676 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1680 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1681 PrintAndLog("File content error. There must be 64 blocks");
1684 PrintAndLog("Loaded from file: %s", filename
);
1689 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1690 uint8_t memBlock
[16];
1691 uint8_t blockNo
= 0;
1693 memset(memBlock
, 0x00, sizeof(memBlock
));
1695 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1696 PrintAndLog("Usage: hf mf cgetblk <block number>");
1697 PrintAndLog("sample: hf mf cgetblk 1");
1698 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1702 blockNo
= param_get8(Cmd
, 0);
1704 PrintAndLog("--block number:%02x ", blockNo
);
1706 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1708 PrintAndLog("Can't read block. error=%d", res
);
1712 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1716 int CmdHF14AMfCGetSc(const char *Cmd
) {
1717 uint8_t memBlock
[16];
1718 uint8_t sectorNo
= 0;
1720 memset(memBlock
, 0x00, sizeof(memBlock
));
1722 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1723 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1724 PrintAndLog("sample: hf mf cgetsc 0");
1725 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1729 sectorNo
= param_get8(Cmd
, 0);
1730 if (sectorNo
> 15) {
1731 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1735 PrintAndLog("--sector number:%02x ", sectorNo
);
1737 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1738 for (i
= 0; i
< 4; i
++) {
1739 if (i
== 1) flags
= 0;
1740 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1742 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1744 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1748 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1753 int CmdHF14AMfCSave(const char *Cmd
) {
1757 char * fnameptr
= filename
;
1758 uint8_t fillFromEmulator
= 0;
1760 int i
, j
, len
, flags
;
1762 memset(filename
, 0, sizeof(filename
));
1763 memset(buf
, 0, sizeof(buf
));
1765 if (param_getchar(Cmd
, 0) == 'h') {
1766 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1767 PrintAndLog("or into emulator memory (option `e`)");
1768 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1769 PrintAndLog(" sample: hf mf esave ");
1770 PrintAndLog(" hf mf esave filename");
1771 PrintAndLog(" hf mf esave e \n");
1775 char ctmp
= param_getchar(Cmd
, 0);
1776 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1778 if (fillFromEmulator
) {
1779 // put into emulator
1780 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1781 for (i
= 0; i
< 16 * 4; i
++) {
1782 if (i
== 1) flags
= 0;
1783 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1785 if (mfCGetBlock(i
, buf
, flags
)) {
1786 PrintAndLog("Cant get block: %d", i
);
1790 if (mfEmlSetMem(buf
, i
, 1)) {
1791 PrintAndLog("Cant set emul block: %d", i
);
1798 if (len
> 14) len
= 14;
1802 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1803 PrintAndLog("Cant get block: %d", 0);
1806 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1807 sprintf(fnameptr
, "%02x", buf
[j
]);
1809 memcpy(filename
, Cmd
, len
);
1813 sprintf(fnameptr
, ".eml");
1816 f
= fopen(filename
, "w+");
1819 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1820 for (i
= 0; i
< 16 * 4; i
++) {
1821 if (i
== 1) flags
= 0;
1822 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1824 if (mfCGetBlock(i
, buf
, flags
)) {
1825 PrintAndLog("Cant get block: %d", i
);
1828 for (j
= 0; j
< 16; j
++)
1829 fprintf(f
, "%02x", buf
[j
]);
1834 PrintAndLog("Saved to file: %s", filename
);
1840 int CmdHF14AMfSniff(const char *Cmd
){
1842 bool wantLogToFile
= 0;
1843 bool wantDecrypt
= 0;
1844 //bool wantSaveToEml = 0; TODO
1845 bool wantSaveToEmlFile
= 0;
1860 uint8_t * bufPtr
= buf
;
1861 memset(buf
, 0x00, 3000);
1863 if (param_getchar(Cmd
, 0) == 'h') {
1864 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1865 PrintAndLog("You can specify:");
1866 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1867 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1868 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1869 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1870 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1871 PrintAndLog(" sample: hf mf sniff l d e");
1875 for (int i
= 0; i
< 4; i
++) {
1876 char ctmp
= param_getchar(Cmd
, i
);
1877 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1878 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1879 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1880 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1883 printf("-------------------------------------------------------------------------\n");
1884 printf("Executing command. \n");
1885 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1886 printf("Press the key on pc keyboard to abort the client.\n");
1887 printf("-------------------------------------------------------------------------\n");
1889 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1890 clearCommandBuffer();
1899 printf("\naborted via keyboard!\n");
1904 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1905 res
= resp
.arg
[0] & 0xff;
1909 if (res
== 0) return 0;
1913 memset(buf
, 0x00, 3000);
1915 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1920 blockLen
= bufPtr
- buf
;
1923 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1925 while (bufPtr
- buf
+ 9 < blockLen
) {
1926 isTag
= bufPtr
[3] & 0x80 ? true:false;
1928 parity
= *((uint32_t *)(bufPtr
));
1932 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff)) {
1933 memcpy(uid
, bufPtr
+ 2, 7);
1934 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1935 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
1938 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1939 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
1943 if (wantLogToFile
|| wantDecrypt
) {
1944 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
1945 AddLogCurrentDT(logHexFileName
);
1947 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1949 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1950 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1951 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1963 static command_t CommandTable
[] =
1965 {"help", CmdHelp
, 1, "This help"},
1966 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1967 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1968 {"urdbl", CmdHF14AMfURdBl
, 0, "Read MIFARE Ultralight block"},
1969 {"urdcard", CmdHF14AMfURdCard
, 0,"Read MIFARE Ultralight Card"},
1970 {"uwrbl", CmdHF14AMfUWrBl
, 0,"Write MIFARE Ultralight block"},
1971 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1972 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1973 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1974 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1975 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1976 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1977 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1978 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1979 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1980 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1981 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1982 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1983 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1984 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1985 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1986 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1987 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1988 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1989 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1990 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1991 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1992 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1993 {NULL
, NULL
, 0, NULL
}
1996 int CmdHFMF(const char *Cmd
)
1999 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
2001 CmdsParse(CommandTable
, Cmd
);
2005 int CmdHelp(const char *Cmd
)
2007 CmdsHelp(CommandTable
);