]>
git.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 //-----------------------------------------------------------------------------
10 #include "../include/mifare.h"
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();
45 printf("\naborted via keyboard!\n");
50 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
51 isOK
= resp
.arg
[0] & 0xff;
52 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
53 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
54 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
55 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
56 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
58 if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
66 if (isOK
!= 1) return 1;
68 // execute original function from util nonce2key
69 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
))
72 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
74 printf("------------------------------------------------------------------\n");
75 PrintAndLog("Key found:%012"llx
" \n", r_key
);
77 num_to_bytes(r_key
, 6, keyBlock
);
78 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
81 PrintAndLog("Found valid key:%012"llx
, r_key
);
84 if (isOK
!= 2) PrintAndLog("Found invalid key. ");
85 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
93 int CmdHF14AMfWrBl(const char *Cmd
)
97 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
98 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
103 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
104 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
108 blockNo
= param_get8(Cmd
, 0);
109 cmdp
= param_getchar(Cmd
, 1);
111 PrintAndLog("Key type must be A or B");
114 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
115 if (param_gethex(Cmd
, 2, key
, 12)) {
116 PrintAndLog("Key must include 12 HEX symbols");
119 if (param_gethex(Cmd
, 3, bldata
, 32)) {
120 PrintAndLog("Block data must include 32 HEX symbols");
123 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
124 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
126 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
127 memcpy(c
.d
.asBytes
, key
, 6);
128 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
132 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
133 uint8_t isOK
= resp
.arg
[0] & 0xff;
134 PrintAndLog("isOk:%02x", isOK
);
136 PrintAndLog("Command execute timeout");
142 /* dublett finns i CMDHFMFU.C
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:%d", 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:%d", 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:%d", 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:%d", 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:%d", 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:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', 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 /* dublett finns i CMDHFMFU.C
302 int CmdHF14AMfURdBl(const char *Cmd)
307 PrintAndLog("Usage: hf mf urdbl <block number>");
308 PrintAndLog(" sample: hf mf urdbl 0");
312 blockNo = param_get8(Cmd, 0);
313 PrintAndLog("--block no:%d", blockNo);
315 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
319 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
320 uint8_t isOK = resp.arg[0] & 0xff;
321 uint8_t *data = resp.d.asBytes;
324 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4));
326 PrintAndLog("isOk:%02x", isOK);
328 PrintAndLog("Command execute timeout");
335 /* dublett finns i CMDHFMFU.C
336 int CmdHF14AMfURdCard(const char *Cmd)
339 uint8_t sectorNo = 0;
340 uint8_t *lockbytes_t=NULL;
341 uint8_t lockbytes[2]={0,0};
342 bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
345 uint8_t * data = NULL;
348 PrintAndLog("Sector number must be less than 16");
351 PrintAndLog("Attempting to Read Ultralight... ");
353 UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}};
357 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
358 isOK = resp.arg[0] & 0xff;
359 data = resp.d.asBytes;
361 PrintAndLog("isOk:%02x", isOK);
363 for (i = 0; i < 16; i++) {
367 lockbytes_t=data+(i*4);
368 lockbytes[0]=lockbytes_t[2];
369 lockbytes[1]=lockbytes_t[3];
370 for(int j=0; j<16; j++){
371 bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8));
373 //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);
374 //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]);
375 PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4));
378 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]);
381 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]);
384 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]);
387 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]);
390 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]);
393 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]);
396 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]);
399 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]);
402 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]);
405 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]);
408 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]);
411 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]);
414 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]);
417 PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4));
422 PrintAndLog("Command execute timeout");
428 int CmdHF14AMfRdSc(const char *Cmd
)
431 uint8_t sectorNo
= 0;
433 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
435 uint8_t *data
= NULL
;
439 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
440 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
444 sectorNo
= param_get8(Cmd
, 0);
446 PrintAndLog("Sector number must be less than 40");
449 cmdp
= param_getchar(Cmd
, 1);
450 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
451 PrintAndLog("Key type must be A or B");
454 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
455 if (param_gethex(Cmd
, 2, key
, 12)) {
456 PrintAndLog("Key must include 12 HEX symbols");
459 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
461 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
462 memcpy(c
.d
.asBytes
, key
, 6);
467 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
468 isOK
= resp
.arg
[0] & 0xff;
469 data
= resp
.d
.asBytes
;
471 PrintAndLog("isOk:%02x", isOK
);
473 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
474 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
476 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
479 PrintAndLog("Command execute timeout");
486 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
491 return 32 * 4 + (sectorNo
- 32) * 16;
496 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
506 int CmdHF14AMfDump(const char *Cmd
)
508 uint8_t sectorNo
, blockNo
;
512 uint8_t rights
[40][4];
513 uint8_t carddata
[256][16];
514 uint8_t numSectors
= 16;
521 int size
= GetCardSize();
522 char cmdp
= param_getchar(Cmd
, 0);
525 cmdp
= (char)(48+size
);
527 PrintAndLog("Got %d",cmdp
);
530 case '0' : numSectors
= 5; break;
532 case '\0': numSectors
= 16; break;
533 case '2' : numSectors
= 32; break;
534 case '4' : numSectors
= 40; break;
535 default: numSectors
= 16;
538 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
539 PrintAndLog("Usage: hf mf dump [card memory]");
540 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
542 PrintAndLog("Samples: hf mf dump");
543 PrintAndLog(" hf mf dump 4");
547 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
548 PrintAndLog("Could not find file dumpkeys.bin");
552 // Read keys A from file
553 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
554 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
555 PrintAndLog("File reading error.");
561 // Read keys B from file
562 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
563 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
564 PrintAndLog("File reading error.");
572 PrintAndLog("|-----------------------------------------|");
573 PrintAndLog("|------ Reading sector access bits...-----|");
574 PrintAndLog("|-----------------------------------------|");
576 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
577 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
578 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
581 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
582 uint8_t isOK
= resp
.arg
[0] & 0xff;
583 uint8_t *data
= resp
.d
.asBytes
;
585 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
586 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
587 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
588 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
590 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
591 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
592 rights
[sectorNo
][3] = 0x01;
595 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
596 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
597 rights
[sectorNo
][3] = 0x01;
601 PrintAndLog("|-----------------------------------------|");
602 PrintAndLog("|----- Dumping all blocks to file... -----|");
603 PrintAndLog("|-----------------------------------------|");
606 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
607 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
608 bool received
= false;
610 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
611 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
612 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
614 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
615 } else { // data block. Check if it can be read with key A or key B
616 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
617 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
618 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
619 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
621 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
622 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
624 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
625 } else { // key A would work
626 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
627 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
629 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
634 isOK
= resp
.arg
[0] & 0xff;
635 uint8_t *data
= resp
.d
.asBytes
;
636 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
637 data
[0] = (keyA
[sectorNo
][0]);
638 data
[1] = (keyA
[sectorNo
][1]);
639 data
[2] = (keyA
[sectorNo
][2]);
640 data
[3] = (keyA
[sectorNo
][3]);
641 data
[4] = (keyA
[sectorNo
][4]);
642 data
[5] = (keyA
[sectorNo
][5]);
643 data
[10] = (keyB
[sectorNo
][0]);
644 data
[11] = (keyB
[sectorNo
][1]);
645 data
[12] = (keyB
[sectorNo
][2]);
646 data
[13] = (keyB
[sectorNo
][3]);
647 data
[14] = (keyB
[sectorNo
][4]);
648 data
[15] = (keyB
[sectorNo
][5]);
651 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
652 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
654 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
660 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
667 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
668 PrintAndLog("Could not create file name dumpdata.bin");
671 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
672 fwrite(carddata
, 1, 16*numblocks
, fout
);
674 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
681 int CmdHF14AMfRestore(const char *Cmd
)
683 uint8_t sectorNo
,blockNo
;
685 uint8_t key
[6] = {0xFF};
686 uint8_t bldata
[16] = {0x00};
694 char cmdp
= param_getchar(Cmd
, 0);
696 case '0' : numSectors
= 5; break;
698 case '\0': numSectors
= 16; break;
699 case '2' : numSectors
= 32; break;
700 case '4' : numSectors
= 40; break;
701 default: numSectors
= 16;
704 if (cmdp
== 'h' || cmdp
== 'H') {
705 PrintAndLog("Usage: hf mf restore [card memory]");
706 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
708 PrintAndLog("Samples: hf mf restore");
709 PrintAndLog(" hf mf restore 4");
713 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
714 PrintAndLog("Could not find file dumpkeys.bin");
718 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
719 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
720 PrintAndLog("File reading error (dumpkeys.bin).");
725 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
726 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
727 PrintAndLog("File reading error (dumpkeys.bin).");
734 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
735 PrintAndLog("Could not find file dumpdata.bin");
738 PrintAndLog("Restoring dumpdata.bin to card");
740 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
741 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
742 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
743 memcpy(c
.d
.asBytes
, key
, 6);
745 if (fread(bldata
, 1, 16, fdump
) == 0) {
746 PrintAndLog("File reading error (dumpdata.bin).");
750 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
751 bldata
[0] = (keyA
[sectorNo
][0]);
752 bldata
[1] = (keyA
[sectorNo
][1]);
753 bldata
[2] = (keyA
[sectorNo
][2]);
754 bldata
[3] = (keyA
[sectorNo
][3]);
755 bldata
[4] = (keyA
[sectorNo
][4]);
756 bldata
[5] = (keyA
[sectorNo
][5]);
757 bldata
[10] = (keyB
[sectorNo
][0]);
758 bldata
[11] = (keyB
[sectorNo
][1]);
759 bldata
[12] = (keyB
[sectorNo
][2]);
760 bldata
[13] = (keyB
[sectorNo
][3]);
761 bldata
[14] = (keyB
[sectorNo
][4]);
762 bldata
[15] = (keyB
[sectorNo
][5]);
765 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
767 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
771 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
772 uint8_t isOK
= resp
.arg
[0] & 0xff;
773 PrintAndLog("isOk:%02x", isOK
);
775 PrintAndLog("Command execute timeout");
786 int CmdHF14AMfNested(const char *Cmd
)
788 int i
, j
, res
, iterations
;
789 sector
*e_sector
= NULL
;
792 uint8_t trgBlockNo
= 0;
793 uint8_t trgKeyType
= 0;
794 uint8_t SectorsCnt
= 0;
795 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
796 uint8_t keyBlock
[13*6];
798 bool transferToEml
= false;
800 bool createDumpFile
= false;
802 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
803 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
808 PrintAndLog("Usage:");
809 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
810 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
811 PrintAndLog(" <target block number> <target key A/B> [t]");
812 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
813 PrintAndLog("t - transfer keys into emulator memory");
814 PrintAndLog("d - write keys to binary file");
816 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
817 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
818 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
819 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
823 cmdp
= param_getchar(Cmd
, 0);
824 blockNo
= param_get8(Cmd
, 1);
825 ctmp
= param_getchar(Cmd
, 2);
827 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
828 PrintAndLog("Key type must be A or B");
832 if (ctmp
!= 'A' && ctmp
!= 'a')
835 if (param_gethex(Cmd
, 3, key
, 12)) {
836 PrintAndLog("Key must include 12 HEX symbols");
840 if (cmdp
== 'o' || cmdp
== 'O') {
842 trgBlockNo
= param_get8(Cmd
, 4);
843 ctmp
= param_getchar(Cmd
, 5);
844 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
845 PrintAndLog("Target key type must be A or B");
848 if (ctmp
!= 'A' && ctmp
!= 'a')
853 case '0': SectorsCnt
= 05; break;
854 case '1': SectorsCnt
= 16; break;
855 case '2': SectorsCnt
= 32; break;
856 case '4': SectorsCnt
= 40; break;
857 default: SectorsCnt
= 16;
861 ctmp
= param_getchar(Cmd
, 4);
862 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
863 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
865 ctmp
= param_getchar(Cmd
, 6);
866 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
867 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
870 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
871 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true)) {
872 PrintAndLog("Nested error.");
875 key64
= bytes_to_num(keyBlock
, 6);
877 PrintAndLog("Found valid key:%012"llx
, key64
);
879 // transfer key to the emulator
881 uint8_t sectortrailer
;
882 if (trgBlockNo
< 32*4) { // 4 block sector
883 sectortrailer
= (trgBlockNo
& 0x03) + 3;
884 } else { // 16 block sector
885 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
887 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
890 num_to_bytes(key64
, 6, keyBlock
);
892 num_to_bytes(key64
, 6, &keyBlock
[10]);
893 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
896 PrintAndLog("No valid key found");
899 else { // ------------------------------------ multiple sectors working
903 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
904 if (e_sector
== NULL
) return 1;
906 //test current key and additional standard keys first
907 memcpy(keyBlock
, key
, 6);
908 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
909 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
910 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
911 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
912 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
913 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
914 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
915 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
916 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
917 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
918 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
919 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
920 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
922 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
923 for (i
= 0; i
< SectorsCnt
; i
++) {
924 for (j
= 0; j
< 2; j
++) {
925 if (e_sector
[i
].foundKey
[j
]) continue;
927 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, 6, keyBlock
, &key64
);
930 e_sector
[i
].Key
[j
] = key64
;
931 e_sector
[i
].foundKey
[j
] = 1;
938 PrintAndLog("nested...");
939 bool calibrate
= true;
940 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
941 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
944 printf("\naborted via keyboard!\n");
949 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
950 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
951 PrintAndLog("-----------------------------------------------");
952 if(mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
)) {
953 PrintAndLog("Nested error.\n");
962 key64
= bytes_to_num(keyBlock
, 6);
964 PrintAndLog("Found valid key:%012"llx
, key64
);
965 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
966 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
972 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
);
974 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
976 PrintAndLog("|---|----------------|---|----------------|---|");
977 PrintAndLog("|sec|key A |res|key B |res|");
978 PrintAndLog("|---|----------------|---|----------------|---|");
979 for (i
= 0; i
< SectorsCnt
; i
++) {
980 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
981 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
983 PrintAndLog("|---|----------------|---|----------------|---|");
985 // transfer them to the emulator
987 for (i
= 0; i
< SectorsCnt
; i
++) {
988 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
989 if (e_sector
[i
].foundKey
[0])
990 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
991 if (e_sector
[i
].foundKey
[1])
992 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
993 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
998 if (createDumpFile
) {
999 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
1000 PrintAndLog("Could not create file dumpkeys.bin");
1004 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1005 for(i
=0; i
<SectorsCnt
; i
++) {
1006 if (e_sector
[i
].foundKey
[0]){
1007 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1008 fwrite ( tempkey
, 1, 6, fkeys
);
1011 fwrite ( &standart
, 1, 6, fkeys
);
1014 for(i
=0; i
<SectorsCnt
; i
++) {
1015 if (e_sector
[i
].foundKey
[1]){
1016 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1017 fwrite ( tempkey
, 1, 6, fkeys
);
1020 fwrite ( &standart
, 1, 6, fkeys
);
1032 int CmdHF14AMfChk(const char *Cmd
)
1034 if (strlen(Cmd
)<3) {
1035 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
1036 PrintAndLog(" * - all sectors");
1037 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
1038 PrintAndLog("d - write keys to binary file\n");
1039 PrintAndLog("t - write keys to emulator memory");
1040 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
1041 PrintAndLog(" hf mf chk *1 ? t");
1042 PrintAndLog(" hf mf chk *1 ? d");
1047 char filename
[FILE_PATH_SIZE
]={0};
1049 uint8_t *keyBlock
= NULL
, *p
;
1050 uint8_t stKeyBlock
= 20;
1055 uint8_t blockNo
= 0;
1056 uint8_t SectorsCnt
= 1;
1057 uint8_t keyType
= 0;
1060 int transferToEml
= 0;
1061 int createDumpFile
= 0;
1063 keyBlock
= calloc(stKeyBlock
, 6);
1064 if (keyBlock
== NULL
) return 1;
1066 uint64_t defaultKeys
[] =
1068 0xffffffffffff, // Default key (first key used by program if no user defined key)
1069 0x000000000000, // Blank key
1070 0xa0a1a2a3a4a5, // NFCForum MAD key
1082 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1084 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1086 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1089 if (param_getchar(Cmd
, 0)=='*') {
1091 switch(param_getchar(Cmd
+1, 0)) {
1092 case '0': SectorsCnt
= 5; break;
1093 case '1': SectorsCnt
= 16; break;
1094 case '2': SectorsCnt
= 32; break;
1095 case '4': SectorsCnt
= 40; break;
1096 default: SectorsCnt
= 16;
1100 blockNo
= param_get8(Cmd
, 0);
1102 ctmp
= param_getchar(Cmd
, 1);
1114 PrintAndLog("Key type must be A , B or ?");
1118 ctmp
= param_getchar(Cmd
, 2);
1119 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1120 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1122 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1123 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1124 if ( stKeyBlock
- keycnt
< 2) {
1125 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1127 PrintAndLog("Cannot allocate memory for Keys");
1133 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1134 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1135 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1138 // May be a dic file
1139 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1140 PrintAndLog("File name too long");
1145 if ( (f
= fopen( filename
, "r")) ) {
1146 while( fgets(buf
, sizeof(buf
), f
) ){
1147 if (strlen(buf
) < 12 || buf
[11] == '\n')
1150 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1152 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1154 if (!isxdigit(buf
[0])){
1155 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1161 if ( stKeyBlock
- keycnt
< 2) {
1162 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1164 PrintAndLog("Cannot allocate memory for defKeys");
1170 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1171 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1172 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1174 memset(buf
, 0, sizeof(buf
));
1178 PrintAndLog("File: %s: not found or locked.", filename
);
1187 PrintAndLog("No key specified, trying default keys");
1188 for (;keycnt
< defaultKeysSize
; keycnt
++)
1189 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1190 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1191 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1194 // initialize storage for found keys
1195 bool validKey
[2][40];
1196 uint8_t foundKey
[2][40][6];
1197 for (uint16_t t
= 0; t
< 2; t
++) {
1198 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1199 validKey
[t
][sectorNo
] = false;
1200 for (uint16_t i
= 0; i
< 6; i
++) {
1201 foundKey
[t
][sectorNo
][i
] = 0xff;
1206 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
1208 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1209 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
1210 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1211 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1212 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1213 res
= mfCheckKeys(b
, t
, size
, &keyBlock
[6*c
], &key64
);
1216 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1217 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
1218 validKey
[t
][i
] = true;
1221 PrintAndLog("Command execute timeout");
1224 b
<127?(b
+=4):(b
+=16);
1228 if (transferToEml
) {
1230 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1231 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1232 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1233 for (uint16_t t
= 0; t
< 2; t
++) {
1234 if (validKey
[t
][sectorNo
]) {
1235 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1238 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1241 PrintAndLog("Found keys have been transferred to the emulator memory");
1244 if (createDumpFile
) {
1245 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1246 if (fkeys
== NULL
) {
1247 PrintAndLog("Could not create file dumpkeys.bin");
1251 for (uint16_t t
= 0; t
< 2; t
++) {
1252 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1255 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1264 int CmdHF14AMf1kSim(const char *Cmd
)
1266 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1267 uint8_t exitAfterNReads
= 0;
1270 if (param_getchar(Cmd
, 0) == 'h') {
1271 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1272 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1273 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1274 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1275 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1276 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1280 if (param_getchar(Cmd
, pnr
) == 'u') {
1281 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1283 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1284 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1285 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1287 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1292 if (param_getchar(Cmd
, pnr
) == 'n') {
1293 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1296 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1297 //Using a flag to signal interactiveness, least significant bit
1298 flags
|= FLAG_INTERACTIVE
;
1302 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1303 //Using a flag to signal interactiveness, least significant bit
1304 flags
|= FLAG_NR_AR_ATTACK
;
1306 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1307 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1308 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1309 , exitAfterNReads
, flags
,flags
);
1312 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1313 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1316 if(flags
& FLAG_INTERACTIVE
)
1319 PrintAndLog("Press pm3-button to abort simulation");
1320 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1321 //We're waiting only 1.5 s at a time, otherwise we get the
1322 // annoying message about "Waiting for a response... "
1330 int CmdHF14AMfDbg(const char *Cmd
)
1332 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1334 PrintAndLog("Max debug mode parameter is 4 \n");
1337 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1338 PrintAndLog("Usage: hf mf dbg <debug level>");
1339 PrintAndLog(" 0 - no debug messages");
1340 PrintAndLog(" 1 - error messages");
1341 PrintAndLog(" 2 - plus information messages");
1342 PrintAndLog(" 3 - plus debug messages");
1343 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1344 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1348 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1354 int CmdHF14AMfEGet(const char *Cmd
)
1356 uint8_t blockNo
= 0;
1359 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1360 PrintAndLog("Usage: hf mf eget <block number>");
1361 PrintAndLog(" sample: hf mf eget 0 ");
1365 blockNo
= param_get8(Cmd
, 0);
1368 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1369 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1371 PrintAndLog("Command execute timeout");
1378 int CmdHF14AMfEClear(const char *Cmd
)
1380 if (param_getchar(Cmd
, 0) == 'h') {
1381 PrintAndLog("Usage: hf mf eclr");
1382 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1386 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1392 int CmdHF14AMfESet(const char *Cmd
)
1394 uint8_t memBlock
[16];
1395 uint8_t blockNo
= 0;
1397 memset(memBlock
, 0x00, sizeof(memBlock
));
1399 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1400 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1401 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1405 blockNo
= param_get8(Cmd
, 0);
1407 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1408 PrintAndLog("block data must include 32 HEX symbols");
1413 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1414 memcpy(c
.d
.asBytes
, memBlock
, 16);
1420 int CmdHF14AMfELoad(const char *Cmd
)
1423 char filename
[FILE_PATH_SIZE
];
1424 char *fnameptr
= filename
;
1427 int i
, len
, blockNum
, numBlocks
;
1428 int nameParamNo
= 1;
1430 memset(filename
, 0, sizeof(filename
));
1431 memset(buf
, 0, sizeof(buf
));
1433 char ctmp
= param_getchar(Cmd
, 0);
1435 if ( ctmp
== 'h' || ctmp
== 0x00) {
1436 PrintAndLog("It loads emul dump from the file `filename.eml`");
1437 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1438 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1440 PrintAndLog(" sample: hf mf eload filename");
1441 PrintAndLog(" hf mf eload 4 filename");
1446 case '0' : numBlocks
= 5*4; break;
1448 case '\0': numBlocks
= 16*4; break;
1449 case '2' : numBlocks
= 32*4; break;
1450 case '4' : numBlocks
= 256; break;
1457 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1459 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1463 sprintf(fnameptr
, ".eml");
1466 f
= fopen(filename
, "r");
1468 PrintAndLog("File %s not found or locked", filename
);
1472 // for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
1473 // for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
1477 memset(buf
, 0, sizeof(buf
));
1479 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1481 if (blockNum
>= numBlocks
) break;
1483 PrintAndLog("File reading error.");
1488 if (strlen(buf
) < 32){
1489 if(strlen(buf
) && feof(f
))
1491 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1495 for (i
= 0; i
< 32; i
+= 2) {
1496 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1499 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1500 PrintAndLog("Cant set emul block: %3d", blockNum
);
1505 if (blockNum
>= numBlocks
) break;
1509 if ((blockNum
!= numBlocks
)) {
1510 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1514 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1519 int CmdHF14AMfESave(const char *Cmd
)
1522 char filename
[FILE_PATH_SIZE
];
1523 char * fnameptr
= filename
;
1525 int i
, j
, len
, numBlocks
;
1526 int nameParamNo
= 1;
1528 memset(filename
, 0, sizeof(filename
));
1529 memset(buf
, 0, sizeof(buf
));
1531 char ctmp
= param_getchar(Cmd
, 0);
1534 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1535 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1536 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1538 PrintAndLog(" sample: hf mf esave ");
1539 PrintAndLog(" hf mf esave 4");
1540 PrintAndLog(" hf mf esave 4 filename");
1545 case '0' : numBlocks
= 5*4; break;
1547 case '\0': numBlocks
= 16*4; break;
1548 case '2' : numBlocks
= 32*4; break;
1549 case '4' : numBlocks
= 256; break;
1556 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1558 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1560 // user supplied filename?
1562 // get filename (UID from memory)
1563 if (mfEmlGetMem(buf
, 0, 1)) {
1564 PrintAndLog("Can\'t get UID from block: %d", 0);
1565 sprintf(filename
, "dump.eml");
1567 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1568 sprintf(fnameptr
, "%02X", buf
[j
]);
1573 // add file extension
1574 sprintf(fnameptr
, ".eml");
1577 f
= fopen(filename
, "w+");
1580 for (i
= 0; i
< numBlocks
; i
++) {
1581 if (mfEmlGetMem(buf
, i
, 1)) {
1582 PrintAndLog("Cant get block: %d", i
);
1585 for (j
= 0; j
< 16; j
++)
1586 fprintf(f
, "%02X", buf
[j
]);
1591 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1597 int CmdHF14AMfECFill(const char *Cmd
)
1599 uint8_t keyType
= 0;
1600 uint8_t numSectors
= 16;
1602 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1603 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1604 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1606 PrintAndLog("samples: hf mf ecfill A");
1607 PrintAndLog(" hf mf ecfill A 4");
1608 PrintAndLog("Read card and transfer its data to emulator memory.");
1609 PrintAndLog("Keys must be laid in the emulator memory. \n");
1613 char ctmp
= param_getchar(Cmd
, 0);
1614 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1615 PrintAndLog("Key type must be A or B");
1618 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1620 ctmp
= param_getchar(Cmd
, 1);
1622 case '0' : numSectors
= 5; break;
1624 case '\0': numSectors
= 16; break;
1625 case '2' : numSectors
= 32; break;
1626 case '4' : numSectors
= 40; break;
1627 default: numSectors
= 16;
1630 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1631 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1637 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1642 uint64_t keyA
, keyB
;
1644 if (param_getchar(Cmd
, 0) == 'h') {
1645 PrintAndLog("It prints the keys loaded in the emulator memory");
1646 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1647 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1649 PrintAndLog(" sample: hf mf ekeyprn 1");
1653 char cmdp
= param_getchar(Cmd
, 0);
1656 case '0' : numSectors
= 5; break;
1658 case '\0': numSectors
= 16; break;
1659 case '2' : numSectors
= 32; break;
1660 case '4' : numSectors
= 40; break;
1661 default: numSectors
= 16;
1664 PrintAndLog("|---|----------------|----------------|");
1665 PrintAndLog("|sec|key A |key B |");
1666 PrintAndLog("|---|----------------|----------------|");
1667 for (i
= 0; i
< numSectors
; i
++) {
1668 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1669 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1672 keyA
= bytes_to_num(data
, 6);
1673 keyB
= bytes_to_num(data
+ 10, 6);
1674 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1676 PrintAndLog("|---|----------------|----------------|");
1682 int CmdHF14AMfCSetUID(const char *Cmd
)
1684 uint8_t wipeCard
= 0;
1685 uint8_t uid
[8] = {0x00};
1686 uint8_t oldUid
[8] = {0x00};
1689 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1690 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1691 PrintAndLog("sample: hf mf csetuid 01020304 w");
1692 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1693 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1697 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1698 PrintAndLog("UID must include 8 HEX symbols");
1702 char ctmp
= param_getchar(Cmd
, 1);
1703 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1705 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1707 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1709 PrintAndLog("Can't set UID. error=%d", res
);
1713 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1717 int CmdHF14AMfCSetBlk(const char *Cmd
)
1720 uint8_t memBlock
[16];
1721 uint8_t blockNo
= 0;
1723 memset(memBlock
, 0x00, sizeof(memBlock
));
1725 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1726 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1727 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1728 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1729 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1733 blockNo
= param_get8(Cmd
, 0);
1735 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1736 PrintAndLog("block data must include 32 HEX symbols");
1740 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16));
1742 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1744 PrintAndLog("Can't write block. error=%d", res
);
1748 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1753 int CmdHF14AMfCLoad(const char *Cmd
)
1756 char filename
[FILE_PATH_SIZE
];
1757 char * fnameptr
= filename
;
1760 uint8_t fillFromEmulator
= 0;
1761 int i
, len
, blockNum
, flags
;
1763 memset(filename
, 0, sizeof(filename
));
1764 memset(buf
, 0, sizeof(buf
));
1766 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1767 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1768 PrintAndLog("or from emulator memory (option `e`)");
1769 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1770 PrintAndLog(" or: hf mf cload e ");
1771 PrintAndLog(" sample: hf mf cload filename");
1775 char ctmp
= param_getchar(Cmd
, 0);
1776 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1778 if (fillFromEmulator
) {
1779 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1780 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1781 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1782 PrintAndLog("Cant get block: %d", blockNum
);
1786 if (blockNum
== 2) flags
= 0;
1787 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1789 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1790 PrintAndLog("Cant set magic card block: %d", blockNum
);
1797 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1799 memcpy(filename
, Cmd
, len
);
1802 sprintf(fnameptr
, ".eml");
1805 f
= fopen(filename
, "r");
1807 PrintAndLog("File not found or locked.");
1812 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1814 memset(buf
, 0, sizeof(buf
));
1815 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1816 PrintAndLog("File reading error.");
1820 if (strlen(buf
) < 32){
1821 if(strlen(buf
) && feof(f
))
1823 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1826 for (i
= 0; i
< 32; i
+= 2)
1827 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1829 if (blockNum
== 2) flags
= 0;
1830 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1832 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1833 PrintAndLog("Can't set magic card block: %d", blockNum
);
1838 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1842 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1843 PrintAndLog("File content error. There must be 64 blocks");
1846 PrintAndLog("Loaded from file: %s", filename
);
1851 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1852 uint8_t memBlock
[16];
1853 uint8_t blockNo
= 0;
1855 memset(memBlock
, 0x00, sizeof(memBlock
));
1857 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1858 PrintAndLog("Usage: hf mf cgetblk <block number>");
1859 PrintAndLog("sample: hf mf cgetblk 1");
1860 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1864 blockNo
= param_get8(Cmd
, 0);
1866 PrintAndLog("--block number:%2d ", blockNo
);
1868 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1870 PrintAndLog("Can't read block. error=%d", res
);
1874 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1879 int CmdHF14AMfCGetSc(const char *Cmd
) {
1880 uint8_t memBlock
[16];
1881 uint8_t sectorNo
= 0;
1883 memset(memBlock
, 0x00, sizeof(memBlock
));
1885 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1886 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1887 PrintAndLog("sample: hf mf cgetsc 0");
1888 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1892 sectorNo
= param_get8(Cmd
, 0);
1893 if (sectorNo
> 15) {
1894 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1898 PrintAndLog("--sector number:%d ", sectorNo
);
1900 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1901 for (i
= 0; i
< 4; i
++) {
1902 if (i
== 1) flags
= 0;
1903 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1905 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1907 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1911 PrintAndLog("block %3d data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1917 int CmdHF14AMfCSave(const char *Cmd
) {
1920 char filename
[FILE_PATH_SIZE
];
1921 char * fnameptr
= filename
;
1922 uint8_t fillFromEmulator
= 0;
1924 int i
, j
, len
, flags
;
1926 memset(filename
, 0, sizeof(filename
));
1927 memset(buf
, 0, sizeof(buf
));
1929 if (param_getchar(Cmd
, 0) == 'h') {
1930 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1931 PrintAndLog("or into emulator memory (option `e`)");
1932 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1933 PrintAndLog(" sample: hf mf esave ");
1934 PrintAndLog(" hf mf esave filename");
1935 PrintAndLog(" hf mf esave e \n");
1939 char ctmp
= param_getchar(Cmd
, 0);
1940 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1942 if (fillFromEmulator
) {
1943 // put into emulator
1944 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1945 for (i
= 0; i
< 16 * 4; i
++) {
1946 if (i
== 1) flags
= 0;
1947 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1949 if (mfCGetBlock(i
, buf
, flags
)) {
1950 PrintAndLog("Cant get block: %d", i
);
1954 if (mfEmlSetMem(buf
, i
, 1)) {
1955 PrintAndLog("Cant set emul block: %d", i
);
1962 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1966 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1967 PrintAndLog("Cant get block: %d", 0);
1970 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1971 sprintf(fnameptr
, "%02x", buf
[j
]);
1973 memcpy(filename
, Cmd
, len
);
1977 sprintf(fnameptr
, ".eml");
1980 f
= fopen(filename
, "w+");
1983 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1984 for (i
= 0; i
< 16 * 4; i
++) {
1985 if (i
== 1) flags
= 0;
1986 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1988 if (mfCGetBlock(i
, buf
, flags
)) {
1989 PrintAndLog("Cant get block: %d", i
);
1992 for (j
= 0; j
< 16; j
++)
1993 fprintf(f
, "%02x", buf
[j
]);
1998 PrintAndLog("Saved to file: %s", filename
);
2005 int CmdHF14AMfSniff(const char *Cmd
){
2007 bool wantLogToFile
= 0;
2008 bool wantDecrypt
= 0;
2009 //bool wantSaveToEml = 0; TODO
2010 bool wantSaveToEmlFile
= 0;
2025 uint8_t * bufPtr
= buf
;
2026 memset(buf
, 0x00, 3000);
2028 if (param_getchar(Cmd
, 0) == 'h') {
2029 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
2030 PrintAndLog("You can specify:");
2031 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
2032 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
2033 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2034 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2035 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
2036 PrintAndLog(" sample: hf mf sniff l d e");
2040 for (int i
= 0; i
< 4; i
++) {
2041 char ctmp
= param_getchar(Cmd
, i
);
2042 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
2043 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
2044 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2045 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
2048 printf("-------------------------------------------------------------------------\n");
2049 printf("Executing command. \n");
2050 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2051 printf("Press the key on pc keyboard to abort the client.\n");
2052 printf("-------------------------------------------------------------------------\n");
2054 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
2055 clearCommandBuffer();
2064 printf("\naborted via keyboard!\n");
2069 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2070 res
= resp
.arg
[0] & 0xff;
2074 if (res
== 0) return 0;
2078 memset(buf
, 0x00, 3000);
2080 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2085 blockLen
= bufPtr
- buf
;
2088 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2090 while (bufPtr
- buf
+ 9 < blockLen
) {
2091 isTag
= bufPtr
[3] & 0x80 ? true:false;
2093 parity
= *((uint32_t *)(bufPtr
));
2097 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff)) {
2098 memcpy(uid
, bufPtr
+ 2, 7);
2099 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2100 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2103 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2104 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2108 if (wantLogToFile
|| wantDecrypt
) {
2109 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2110 AddLogCurrentDT(logHexFileName
);
2113 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2115 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2117 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2119 mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
2131 // Tries to identify cardsize.
2132 // Returns <num> where num is:
2134 // 0 - MINI (320bytes)
2140 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
2144 WaitForResponse(CMD_ACK
,&resp
);
2146 if(resp
.arg
[0] == 0) {
2147 PrintAndLog("iso14443a card select failed");
2151 iso14a_card_select_t
*card
= (iso14a_card_select_t
*)resp
.d
.asBytes
;
2153 PrintAndLog("Trying to detect card size.");
2157 atqa
= (card
->atqa
[1] & 0xff) << 8;
2158 atqa
+= card
->atqa
[0] & 0xff;
2161 // https://code.google.com/p/libnfc/source/browse/libnfc/target-subr.c
2163 PrintAndLog("found ATAQ: %04X SAK: %02X", atqa
, sak
);
2166 // NXP MIFARE Mini 0.3k
2167 if ( ( (atqa
& 0xff0f) == 0x0004) && (sak
== 0x09) ) return 0;
2169 // MIFARE Classic 1K
2170 if ( ((atqa
& 0xff0f) == 0x0004) && (sak
== 0x08) ) return 1;
2172 // MIFARE Classik 4K
2173 if ( ((atqa
& 0xff0f) == 0x0002) && (sak
== 0x18) ) return 4;
2175 // SmartMX with MIFARE 1K emulation
2176 if ( ((atqa
& 0xf0ff) == 0x0004) ) return 1;
2178 // SmartMX with MIFARE 4K emulation
2179 if ( ((atqa
& 0xf0ff) == 0x0002) ) return 4;
2181 // Infineon MIFARE CLASSIC 1K
2182 if ( ((atqa
& 0xffff) == 0x0004) && (sak
== 0x88) ) return 1;
2184 // MFC 4K emulated by Nokia 6212 Classic
2185 if ( ((atqa
& 0xffff) == 0x0002) && (sak
== 0x38) ) return 4;
2187 // MFC 4K emulated by Nokia 6131 NFC
2188 if ( ((atqa
& 0xffff) == 0x0008) && (sak
== 0x38) ) return 4;
2191 PrintAndLog("BEFOOO 1K %02X", (atqa
& 0xff0f));
2193 // MIFARE Plus (4 Byte UID or 4 Byte RID)
2194 // MIFARE Plus (7 Byte UID)
2196 ((atqa
& 0xffff) == 0x0002) |
2197 ((atqa
& 0xffff) == 0x0004) |
2198 ((atqa
& 0xffff) == 0x0042) |
2199 ((atqa
& 0xffff) == 0x0044)
2226 static command_t CommandTable
[] =
2228 {"help", CmdHelp
, 1, "This help"},
2229 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2230 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2231 //{"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},
2232 //{"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},
2233 //{"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},
2234 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2235 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2236 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2237 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2238 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2239 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2240 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2241 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2242 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2243 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2244 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2245 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2246 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2247 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2248 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2249 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2250 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2251 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
2252 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
2253 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
2254 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2255 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2256 {NULL
, NULL
, 0, NULL
}
2259 int CmdHFMF(const char *Cmd
)
2262 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
2264 CmdsParse(CommandTable
, Cmd
);
2268 int CmdHelp(const char *Cmd
)
2270 CmdsHelp(CommandTable
);