]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
c98b25dac543e3ee2ffdb93fe12c8371b4ab1591
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
)
19 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
21 uint8_t keyBlock
[8] = {0};
23 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, keyBlock
, 8)) {
24 PrintAndLog("Nt must include 8 HEX symbols");
29 UsbCommand c
= {CMD_READER_MIFARE
, {(uint32_t)bytes_to_num(keyBlock
, 4), 0, 0}};
35 while (ukbhit()) getchar();
38 printf("-------------------------------------------------------------------------\n");
39 printf("Executing command. It may take up to 30 min.\n");
40 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
41 printf("-------------------------------------------------------------------------\n");
49 printf("\naborted via keyboard!\n");
54 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
55 isOK
= resp
.arg
[0] & 0xff;
57 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
58 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
59 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
60 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
63 PrintAndLog("isOk:%02x", isOK
);
64 if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
71 if (isOK
!= 1) return 1;
73 // execute original function from util nonce2key
74 if (nonce2key(uid
, nt
, par_list
, ks_list
, &r_key
))
77 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
79 printf("------------------------------------------------------------------\n");
80 PrintAndLog("Key found:%012"llx
" \n", r_key
);
82 num_to_bytes(r_key
, 6, keyBlock
);
83 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
86 PrintAndLog("Found valid key:%012"llx
, r_key
);
89 if (isOK
!= 2) PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt
);
97 int CmdHF14AMfWrBl(const char *Cmd
)
101 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
102 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
107 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
108 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
112 blockNo
= param_get8(Cmd
, 0);
113 cmdp
= param_getchar(Cmd
, 1);
115 PrintAndLog("Key type must be A or B");
118 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
119 if (param_gethex(Cmd
, 2, key
, 12)) {
120 PrintAndLog("Key must include 12 HEX symbols");
123 if (param_gethex(Cmd
, 3, bldata
, 32)) {
124 PrintAndLog("Block data must include 32 HEX symbols");
127 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo
, keyType
, sprint_hex(key
, 6));
128 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
130 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
131 memcpy(c
.d
.asBytes
, key
, 6);
132 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
136 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
137 uint8_t isOK
= resp
.arg
[0] & 0xff;
138 PrintAndLog("isOk:%02x", isOK
);
140 PrintAndLog("Command execute timeout");
146 int CmdHF14AMfRdBl(const char *Cmd
)
150 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
156 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
157 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
161 blockNo
= param_get8(Cmd
, 0);
162 cmdp
= param_getchar(Cmd
, 1);
164 PrintAndLog("Key type must be A or B");
167 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
168 if (param_gethex(Cmd
, 2, key
, 12)) {
169 PrintAndLog("Key must include 12 HEX symbols");
172 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo
, keyType
, sprint_hex(key
, 6));
174 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
175 memcpy(c
.d
.asBytes
, key
, 6);
179 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
180 uint8_t isOK
= resp
.arg
[0] & 0xff;
181 uint8_t * data
= resp
.d
.asBytes
;
184 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
186 PrintAndLog("isOk:%02x", isOK
);
188 PrintAndLog("Command execute timeout");
194 int CmdHF14AMfRdSc(const char *Cmd
)
197 uint8_t sectorNo
= 0;
199 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
202 uint8_t * data
= NULL
;
207 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
208 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
212 sectorNo
= param_get8(Cmd
, 0);
214 PrintAndLog("Sector number must be less than 64");
217 cmdp
= param_getchar(Cmd
, 1);
219 PrintAndLog("Key type must be A or B");
222 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
223 if (param_gethex(Cmd
, 2, key
, 12)) {
224 PrintAndLog("Key must include 12 HEX symbols");
227 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo
, keyType
, sprint_hex(key
, 6));
229 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
230 memcpy(c
.d
.asBytes
, key
, 6);
235 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
236 isOK
= resp
.arg
[0] & 0xff;
237 data
= resp
.d
.asBytes
;
239 PrintAndLog("isOk:%02x", isOK
);
241 for (i
= 0; i
< 2; i
++) {
242 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
245 PrintAndLog("Command1 execute timeout");
250 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
251 isOK
= resp
.arg
[0] & 0xff;
252 data
= resp
.d
.asBytes
;
255 for (i
= 0; i
< 2; i
++) {
256 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
259 PrintAndLog("Command2 execute timeout");
265 int CmdHF14AMfDump(const char *Cmd
)
271 uint8_t rights
[40][4];
278 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
279 PrintAndLog("Could not find file dumpkeys.bin");
283 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
284 PrintAndLog("Could not create file name dumpdata.bin");
290 for (i
=0 ; i
<16 ; i
++) {
291 if (fread( keyA
[i
], 1, 6, fin
) == 0) {
292 PrintAndLog("File reading error.");
296 for (i
=0 ; i
<16 ; i
++) {
297 if (fread( keyB
[i
], 1, 6, fin
) == 0) {
298 PrintAndLog("File reading error.");
303 // Read access rights to sectors
305 PrintAndLog("|-----------------------------------------|");
306 PrintAndLog("|------ Reading sector access bits...-----|");
307 PrintAndLog("|-----------------------------------------|");
309 for (i
= 0 ; i
< 16 ; i
++) {
310 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
311 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
314 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
315 uint8_t isOK
= resp
.arg
[0] & 0xff;
316 uint8_t *data
= resp
.d
.asBytes
;
318 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
319 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
320 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
321 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
324 PrintAndLog("Could not get access rights for block %d", i
);
328 PrintAndLog("Command execute timeout");
332 // Read blocks and print to file
334 PrintAndLog("|-----------------------------------------|");
335 PrintAndLog("|----- Dumping all blocks to file... -----|");
336 PrintAndLog("|-----------------------------------------|");
339 for (i
=0 ; i
<16 ; i
++) {
340 for (j
=0 ; j
<4 ; j
++) {
341 bool received
= false;
344 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
345 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
347 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
350 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
351 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
352 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
354 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
356 else if (rights
[i
][j
] == 7) {
357 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
360 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
361 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
363 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
368 uint8_t isOK
= resp
.arg
[0] & 0xff;
369 uint8_t *data
= resp
.d
.asBytes
;
371 data
[0] = (keyA
[i
][0]);
372 data
[1] = (keyA
[i
][1]);
373 data
[2] = (keyA
[i
][2]);
374 data
[3] = (keyA
[i
][3]);
375 data
[4] = (keyA
[i
][4]);
376 data
[5] = (keyA
[i
][5]);
377 data
[10] = (keyB
[i
][0]);
378 data
[11] = (keyB
[i
][1]);
379 data
[12] = (keyB
[i
][2]);
380 data
[13] = (keyB
[i
][3]);
381 data
[14] = (keyB
[i
][4]);
382 data
[15] = (keyB
[i
][5]);
385 fwrite ( data
, 1, 16, fout
);
388 PrintAndLog("Could not get access rights for block %d", i
);
392 PrintAndLog("Command execute timeout");
403 int CmdHF14AMfRestore(const char *Cmd
)
408 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
409 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
416 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
417 PrintAndLog("Could not find file dumpdata.bin");
420 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
421 PrintAndLog("Could not find file dumpkeys.bin");
425 for (i
=0 ; i
<16 ; i
++) {
426 if (fread(keyA
[i
], 1, 6, fkeys
) == 0) {
427 PrintAndLog("File reading error.");
431 for (i
=0 ; i
<16 ; i
++) {
432 if (fread(keyB
[i
], 1, 6, fkeys
) == 0) {
433 PrintAndLog("File reading error.");
438 PrintAndLog("Restoring dumpdata.bin to card");
440 for (i
=0 ; i
<16 ; i
++) {
441 for( j
=0 ; j
<4 ; j
++) {
442 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
443 memcpy(c
.d
.asBytes
, key
, 6);
445 if (fread(bldata
, 1, 16, fdump
) == 0) {
446 PrintAndLog("File reading error.");
451 bldata
[0] = (keyA
[i
][0]);
452 bldata
[1] = (keyA
[i
][1]);
453 bldata
[2] = (keyA
[i
][2]);
454 bldata
[3] = (keyA
[i
][3]);
455 bldata
[4] = (keyA
[i
][4]);
456 bldata
[5] = (keyA
[i
][5]);
457 bldata
[10] = (keyB
[i
][0]);
458 bldata
[11] = (keyB
[i
][1]);
459 bldata
[12] = (keyB
[i
][2]);
460 bldata
[13] = (keyB
[i
][3]);
461 bldata
[14] = (keyB
[i
][4]);
462 bldata
[15] = (keyB
[i
][5]);
465 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
468 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
471 if ((ch != 'y') && (ch != 'Y')){
472 PrintAndLog("Aborting !");
477 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
481 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
482 uint8_t isOK
= resp
.arg
[0] & 0xff;
483 PrintAndLog("isOk:%02x", isOK
);
485 PrintAndLog("Command execute timeout");
495 int CmdHF14AMfNested(const char *Cmd
)
497 int i
, j
, res
, iterations
;
498 sector
* e_sector
= NULL
;
501 uint8_t trgBlockNo
= 0;
502 uint8_t trgKeyType
= 0;
505 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
506 uint8_t keyBlock
[16 * 6];
508 int transferToEml
= 0;
510 int createDumpFile
= 0;
512 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
513 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
518 PrintAndLog("Usage:");
519 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
520 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
521 PrintAndLog(" <target block number> <target key A/B> [t]");
522 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
523 PrintAndLog("t - transfer keys into emulator memory");
524 PrintAndLog("d - write keys to binary file");
526 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
527 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
528 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
529 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
533 cmdp
= param_getchar(Cmd
, 0);
534 blockNo
= param_get8(Cmd
, 1);
535 ctmp
= param_getchar(Cmd
, 2);
537 PrintAndLog("Key type must be A or B");
540 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
541 if (param_gethex(Cmd
, 3, key
, 12)) {
542 PrintAndLog("Key must include 12 HEX symbols");
546 if (cmdp
== 'o' || cmdp
== 'O') {
548 trgBlockNo
= param_get8(Cmd
, 4);
549 ctmp
= param_getchar(Cmd
, 5);
551 PrintAndLog("Target key type must be A or B");
554 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
557 case '0': SectorsCnt
= 05; break;
558 case '1': SectorsCnt
= 16; break;
559 case '2': SectorsCnt
= 32; break;
560 case '4': SectorsCnt
= 64; break;
561 default: SectorsCnt
= 16;
565 ctmp
= param_getchar(Cmd
, 4);
566 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
567 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
569 ctmp
= param_getchar(Cmd
, 6);
570 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
571 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
573 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
575 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
578 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) {
579 PrintAndLog("Nested error.");
583 for (i
= 0; i
< 16; i
++) {
584 PrintAndLog("count=%d key= %s", i
, sprint_hex(keyBlock
+ i
* 6, 6));
588 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
590 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
592 PrintAndLog("Found valid key:%012"llx
, key64
);
594 // transfer key to the emulator
596 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
599 num_to_bytes(key64
, 6, keyBlock
);
601 num_to_bytes(key64
, 6, &keyBlock
[10]);
602 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
605 PrintAndLog("No valid key found");
608 else { // ------------------------------------ multiple sectors working
609 blDiff
= blockNo
% 4;
610 PrintAndLog("Block shift=%d", blDiff
);
611 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
612 if (e_sector
== NULL
) return 1;
614 //test current key 4 sectors
615 memcpy(keyBlock
, key
, 6);
616 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 1 * 6));
617 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 2 * 6));
618 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 3 * 6));
619 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 4 * 6));
620 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
622 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
623 for (i
= 0; i
< SectorsCnt
; i
++) {
624 for (j
= 0; j
< 2; j
++) {
625 if (e_sector
[i
].foundKey
[j
]) continue;
627 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
630 e_sector
[i
].Key
[j
] = key64
;
631 e_sector
[i
].foundKey
[j
] = 1;
638 PrintAndLog("nested...");
639 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
640 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4)
641 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
642 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
643 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) continue;
647 //try keys from nested
648 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
650 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
652 PrintAndLog("Found valid key:%012"llx
, key64
);
653 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
654 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
659 PrintAndLog("Iterations count: %d", iterations
);
661 PrintAndLog("|---|----------------|---|----------------|---|");
662 PrintAndLog("|sec|key A |res|key B |res|");
663 PrintAndLog("|---|----------------|---|----------------|---|");
664 for (i
= 0; i
< SectorsCnt
; i
++) {
665 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
666 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
668 PrintAndLog("|---|----------------|---|----------------|---|");
670 // transfer them to the emulator
672 for (i
= 0; i
< SectorsCnt
; i
++) {
673 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
674 if (e_sector
[i
].foundKey
[0])
675 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
676 if (e_sector
[i
].foundKey
[1])
677 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
678 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
683 if (createDumpFile
) {
684 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
685 PrintAndLog("Could not create file dumpkeys.bin");
689 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
690 for(i
=0; i
<16; i
++) {
691 if (e_sector
[i
].foundKey
[0]){
692 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
693 fwrite ( tempkey
, 1, 6, fkeys
);
696 fwrite ( &standart
, 1, 6, fkeys
);
699 for(i
=0; i
<16; i
++) {
700 if (e_sector
[i
].foundKey
[1]){
701 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
702 fwrite ( tempkey
, 1, 6, fkeys
);
705 fwrite ( &standart
, 1, 6, fkeys
);
718 get_trailer_block (uint32_t uiBlock
)
720 // Test if we are in the small or big sectors
721 uint32_t trailer_block
= 0;
723 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
725 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
727 return trailer_block
;
729 int CmdHF14AMfChk(const char *Cmd
)
732 char filename
[256]={0};
734 uint8_t *keyBlock
= NULL
, *p
;
735 uint8_t stKeyBlock
= 20;
741 uint8_t SectorsCnt
= 1;
745 int transferToEml
= 0;
746 int createDumpFile
= 0;
748 keyBlock
= calloc(stKeyBlock
, 6);
749 if (keyBlock
== NULL
) return 1;
751 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 0 * 6)); // Default key (first key used by program if no user defined key)
752 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 1 * 6)); // Blank key
753 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 2 * 6)); // NFCForum MAD key
754 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
755 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 4 * 6));
756 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 5 * 6));
757 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 6 * 6));
758 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 7 * 6));
759 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 8 * 6));
760 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 9 * 6));
761 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 10 * 6));
762 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 11 * 6));
763 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 12 * 6));
766 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
767 PrintAndLog(" * - all sectors");
768 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
769 // PrintAndLog("d - write keys to binary file\n");
771 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
772 PrintAndLog(" hf mf chk *1 ? t");
776 if (param_getchar(Cmd
, 0)=='*') {
778 switch(param_getchar(Cmd
+1, 0)) {
779 case '0': SectorsCnt
= 5; break;
780 case '1': SectorsCnt
= 16; break;
781 case '2': SectorsCnt
= 32; break;
782 case '4': SectorsCnt
= 40; break;
783 default: SectorsCnt
= 16;
787 blockNo
= param_get8(Cmd
, 0);
789 ctmp
= param_getchar(Cmd
, 1);
801 PrintAndLog("Key type must be A , B or ?");
805 ctmp
= param_getchar(Cmd
, 2);
806 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
807 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
809 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
810 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
811 if ( stKeyBlock
- keycnt
< 2) {
812 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
814 PrintAndLog("Cannot allocate memory for Keys");
820 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
821 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
822 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
826 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
827 PrintAndLog("File name too long");
832 if ( (f
= fopen( filename
, "r")) ) {
834 memset(buf
, 0, sizeof(buf
));
835 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
836 PrintAndLog("File reading error.");
840 if (strlen(buf
) < 12 || buf
[11] == '\n')
843 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
845 if( buf
[0]=='#' ) continue; //The line start with # is remcommnet,skip
847 if (!isxdigit(buf
[0])){
848 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
854 if ( stKeyBlock
- keycnt
< 2) {
855 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
857 PrintAndLog("Cannot allocate memory for defKeys");
863 memset(keyBlock
+ 6 * keycnt
, 0, 6);
864 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
865 PrintAndLog("chk custom key[%d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
869 PrintAndLog("File: %s: not found or locked.", filename
);
878 PrintAndLog("No key specified,try default keys");
879 for (;keycnt
<=12; keycnt
++)
880 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
881 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
882 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
885 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
887 for (int i
=0; i
<SectorsCnt
; ++i
) {
888 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
889 int size
= keycnt
>8?8:keycnt
;
890 for (int c
= 0; c
< keycnt
; c
+=size
) {
891 size
=keycnt
-c
>8?8:keycnt
-c
;
892 res
= mfCheckKeys(b
, t
, size
, keyBlock
+6*c
, &key64
);
895 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
898 mfEmlGetMem(block
, get_trailer_block(b
), 1);
899 num_to_bytes(key64
, 6, block
+ t
*10);
900 mfEmlSetMem(block
, get_trailer_block(b
), 1);
905 printf("Not found yet, keycnt:%d\r", c
+size
);
909 PrintAndLog("Command execute timeout");
912 b
<127?(b
+=4):(b
+=16);
920 if (createDumpFile) {
921 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
922 PrintAndLog("Could not create file dumpkeys.bin");
926 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
927 for(i=0; i<16; i++) {
928 if (e_sector[i].foundKey[0]){
929 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
930 fwrite ( tempkey, 1, 6, fkeys );
933 fwrite ( &standart, 1, 6, fkeys );
936 for(i=0; i<16; i++) {
937 if (e_sector[i].foundKey[1]){
938 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
939 fwrite ( tempkey, 1, 6, fkeys );
942 fwrite ( &standart, 1, 6, fkeys );
951 int CmdHF14AMf1kSim(const char *Cmd
)
953 uint8_t uid
[4] = {0, 0, 0, 0};
955 if (param_getchar(Cmd
, 0) == 'h') {
956 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
957 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
961 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
962 PrintAndLog("UID must include 8 HEX symbols");
965 PrintAndLog(" uid:%s ", sprint_hex(uid
, 4));
967 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {0, 0, 0}};
968 memcpy(c
.d
.asBytes
, uid
, 4);
974 int CmdHF14AMfDbg(const char *Cmd
)
976 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
978 PrintAndLog("Max debud mode parameter is 4 \n");
981 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
982 PrintAndLog("Usage: hf mf dbg <debug level>");
983 PrintAndLog(" 0 - no debug messages");
984 PrintAndLog(" 1 - error messages");
985 PrintAndLog(" 2 - all messages");
986 PrintAndLog(" 4 - extended debug mode");
990 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
996 int CmdHF14AMfEGet(const char *Cmd
)
999 uint8_t data
[3 * 16];
1002 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1003 PrintAndLog("Usage: hf mf eget <block number>");
1004 PrintAndLog(" sample: hf mf eget 0 ");
1008 blockNo
= param_get8(Cmd
, 0);
1011 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1012 for (i
= 0; i
< 3; i
++) {
1013 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1016 PrintAndLog("Command execute timeout");
1022 int CmdHF14AMfEClear(const char *Cmd
)
1024 if (param_getchar(Cmd
, 0) == 'h') {
1025 PrintAndLog("Usage: hf mf eclr");
1026 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1030 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1035 int CmdHF14AMfESet(const char *Cmd
)
1037 uint8_t memBlock
[16];
1038 uint8_t blockNo
= 0;
1040 memset(memBlock
, 0x00, sizeof(memBlock
));
1042 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1043 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1044 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1048 blockNo
= param_get8(Cmd
, 0);
1050 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1051 PrintAndLog("block data must include 32 HEX symbols");
1056 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1057 memcpy(c
.d
.asBytes
, memBlock
, 16);
1062 int CmdHF14AMfELoad(const char *Cmd
)
1066 char * fnameptr
= filename
;
1069 int i
, len
, blockNum
;
1071 memset(filename
, 0, sizeof(filename
));
1072 memset(buf
, 0, sizeof(buf
));
1074 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1075 PrintAndLog("It loads emul dump from the file `filename.eml`");
1076 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1077 PrintAndLog(" sample: hf mf eload filename");
1082 if (len
> 14) len
= 14;
1084 memcpy(filename
, Cmd
, len
);
1087 sprintf(fnameptr
, ".eml");
1090 f
= fopen(filename
, "r");
1092 PrintAndLog("File not found or locked.");
1098 memset(buf
, 0, sizeof(buf
));
1099 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1100 PrintAndLog("File reading error.");
1104 if (strlen(buf
) < 32){
1105 if(strlen(buf
) && feof(f
))
1107 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1110 for (i
= 0; i
< 32; i
+= 2)
1111 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1112 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1114 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1115 PrintAndLog("Cant set emul block: %d", blockNum
);
1120 if (blockNum
>= 32 * 4 + 8 * 16) break;
1124 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1125 PrintAndLog("File content error. There must be 64 blocks");
1128 PrintAndLog("Loaded from file: %s", filename
);
1132 int CmdHF14AMfESave(const char *Cmd
)
1136 char * fnameptr
= filename
;
1140 memset(filename
, 0, sizeof(filename
));
1141 memset(buf
, 0, sizeof(buf
));
1143 if (param_getchar(Cmd
, 0) == 'h') {
1144 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1145 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1146 PrintAndLog(" sample: hf mf esave ");
1147 PrintAndLog(" hf mf esave filename");
1152 if (len
> 14) len
= 14;
1156 if (mfEmlGetMem(buf
, 0, 1)) {
1157 PrintAndLog("Cant get block: %d", 0);
1160 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1161 sprintf(fnameptr
, "%02x", buf
[j
]);
1163 memcpy(filename
, Cmd
, len
);
1167 sprintf(fnameptr
, ".eml");
1170 f
= fopen(filename
, "w+");
1173 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1174 if (mfEmlGetMem(buf
, i
, 1)) {
1175 PrintAndLog("Cant get block: %d", i
);
1178 for (j
= 0; j
< 16; j
++)
1179 fprintf(f
, "%02x", buf
[j
]);
1184 PrintAndLog("Saved to file: %s", filename
);
1189 int CmdHF14AMfECFill(const char *Cmd
)
1191 uint8_t keyType
= 0;
1193 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1194 PrintAndLog("Usage: hf mf efill <key A/B>");
1195 PrintAndLog("sample: hf mf efill A");
1196 PrintAndLog("Card data blocks transfers to card emulator memory.");
1197 PrintAndLog("Keys must be laid in the simulator memory. \n");
1201 char ctmp
= param_getchar(Cmd
, 0);
1203 PrintAndLog("Key type must be A or B");
1206 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1208 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1213 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1217 uint64_t keyA
, keyB
;
1219 PrintAndLog("|---|----------------|----------------|");
1220 PrintAndLog("|sec|key A |key B |");
1221 PrintAndLog("|---|----------------|----------------|");
1222 for (i
= 0; i
< 40; i
++) {
1223 b
<127?(b
+=4):(b
+=16);
1224 if (mfEmlGetMem(data
, b
, 1)) {
1225 PrintAndLog("error get block %d", b
);
1228 keyA
= bytes_to_num(data
, 6);
1229 keyB
= bytes_to_num(data
+ 10, 6);
1230 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1232 PrintAndLog("|---|----------------|----------------|");
1237 int CmdHF14AMfCSetUID(const char *Cmd
)
1239 uint8_t wipeCard
= 0;
1244 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1245 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1246 PrintAndLog("sample: hf mf csetuid 01020304 w");
1247 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1248 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1252 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1253 PrintAndLog("UID must include 8 HEX symbols");
1257 char ctmp
= param_getchar(Cmd
, 1);
1258 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1260 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1262 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1264 PrintAndLog("Can't set UID. error=%d", res
);
1268 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1272 int CmdHF14AMfCSetBlk(const char *Cmd
)
1275 uint8_t memBlock
[16];
1276 uint8_t blockNo
= 0;
1278 memset(memBlock
, 0x00, sizeof(memBlock
));
1280 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1281 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1282 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1283 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1284 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1288 blockNo
= param_get8(Cmd
, 0);
1290 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1291 PrintAndLog("block data must include 32 HEX symbols");
1295 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1297 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1299 PrintAndLog("Can't write block. error=%d", res
);
1303 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1307 int CmdHF14AMfCLoad(const char *Cmd
)
1311 char * fnameptr
= filename
;
1314 uint8_t fillFromEmulator
= 0;
1315 int i
, len
, blockNum
, flags
;
1317 memset(filename
, 0, sizeof(filename
));
1318 memset(buf
, 0, sizeof(buf
));
1320 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1321 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1322 PrintAndLog("or from emulator memory (option `e`)");
1323 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1324 PrintAndLog(" or: hf mf cload e ");
1325 PrintAndLog(" sample: hf mf cload filename");
1329 char ctmp
= param_getchar(Cmd
, 0);
1330 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1332 if (fillFromEmulator
) {
1333 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1334 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1335 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1336 PrintAndLog("Cant get block: %d", blockNum
);
1340 if (blockNum
== 2) flags
= 0;
1341 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1343 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1344 PrintAndLog("Cant set magic card block: %d", blockNum
);
1351 if (len
> 14) len
= 14;
1353 memcpy(filename
, Cmd
, len
);
1356 sprintf(fnameptr
, ".eml");
1359 f
= fopen(filename
, "r");
1361 PrintAndLog("File not found or locked.");
1366 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1368 memset(buf
, 0, sizeof(buf
));
1369 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1370 PrintAndLog("File reading error.");
1374 if (strlen(buf
) < 32){
1375 if(strlen(buf
) && feof(f
))
1377 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1380 for (i
= 0; i
< 32; i
+= 2)
1381 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1383 if (blockNum
== 2) flags
= 0;
1384 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1386 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1387 PrintAndLog("Cant set magic card block: %d", blockNum
);
1392 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1396 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1397 PrintAndLog("File content error. There must be 64 blocks");
1400 PrintAndLog("Loaded from file: %s", filename
);
1405 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1406 uint8_t memBlock
[16];
1407 uint8_t blockNo
= 0;
1409 memset(memBlock
, 0x00, sizeof(memBlock
));
1411 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1412 PrintAndLog("Usage: hf mf cgetblk <block number>");
1413 PrintAndLog("sample: hf mf cgetblk 1");
1414 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1418 blockNo
= param_get8(Cmd
, 0);
1420 PrintAndLog("--block number:%02x ", blockNo
);
1422 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1424 PrintAndLog("Can't read block. error=%d", res
);
1428 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1432 int CmdHF14AMfCGetSc(const char *Cmd
) {
1433 uint8_t memBlock
[16];
1434 uint8_t sectorNo
= 0;
1436 memset(memBlock
, 0x00, sizeof(memBlock
));
1438 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1439 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1440 PrintAndLog("sample: hf mf cgetsc 0");
1441 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1445 sectorNo
= param_get8(Cmd
, 0);
1446 if (sectorNo
> 15) {
1447 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1451 PrintAndLog("--sector number:%02x ", sectorNo
);
1453 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1454 for (i
= 0; i
< 4; i
++) {
1455 if (i
== 1) flags
= 0;
1456 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1458 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1460 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1464 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1469 int CmdHF14AMfCSave(const char *Cmd
) {
1473 char * fnameptr
= filename
;
1474 uint8_t fillFromEmulator
= 0;
1476 int i
, j
, len
, flags
;
1478 memset(filename
, 0, sizeof(filename
));
1479 memset(buf
, 0, sizeof(buf
));
1481 if (param_getchar(Cmd
, 0) == 'h') {
1482 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1483 PrintAndLog("or into emulator memory (option `e`)");
1484 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1485 PrintAndLog(" sample: hf mf esave ");
1486 PrintAndLog(" hf mf esave filename");
1487 PrintAndLog(" hf mf esave e \n");
1491 char ctmp
= param_getchar(Cmd
, 0);
1492 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1494 if (fillFromEmulator
) {
1495 // put into emulator
1496 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1497 for (i
= 0; i
< 16 * 4; i
++) {
1498 if (i
== 1) flags
= 0;
1499 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1501 if (mfCGetBlock(i
, buf
, flags
)) {
1502 PrintAndLog("Cant get block: %d", i
);
1506 if (mfEmlSetMem(buf
, i
, 1)) {
1507 PrintAndLog("Cant set emul block: %d", i
);
1514 if (len
> 14) len
= 14;
1518 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1519 PrintAndLog("Cant get block: %d", 0);
1522 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1523 sprintf(fnameptr
, "%02x", buf
[j
]);
1525 memcpy(filename
, Cmd
, len
);
1529 sprintf(fnameptr
, ".eml");
1532 f
= fopen(filename
, "w+");
1535 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1536 for (i
= 0; i
< 16 * 4; i
++) {
1537 if (i
== 1) flags
= 0;
1538 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1540 if (mfCGetBlock(i
, buf
, flags
)) {
1541 PrintAndLog("Cant get block: %d", i
);
1544 for (j
= 0; j
< 16; j
++)
1545 fprintf(f
, "%02x", buf
[j
]);
1550 PrintAndLog("Saved to file: %s", filename
);
1556 int CmdHF14AMfSniff(const char *Cmd
){
1558 bool wantLogToFile
= 0;
1559 bool wantDecrypt
= 0;
1560 //bool wantSaveToEml = 0; TODO
1561 bool wantSaveToEmlFile
= 0;
1575 uint8_t * bufPtr
= buf
;
1576 memset(buf
, 0x00, 3000);
1578 if (param_getchar(Cmd
, 0) == 'h') {
1579 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1580 PrintAndLog("You can specify:");
1581 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1582 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1583 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1584 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1585 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1586 PrintAndLog(" sample: hf mf sniff l d e");
1590 for (int i
= 0; i
< 4; i
++) {
1591 char ctmp
= param_getchar(Cmd
, i
);
1592 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1593 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1594 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1595 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1598 printf("-------------------------------------------------------------------------\n");
1599 printf("Executing command. \n");
1600 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1601 printf("Press the key on pc keyboard to abort the client.\n");
1602 printf("-------------------------------------------------------------------------\n");
1604 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1613 printf("\naborted via keyboard!\n");
1618 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1619 res
= resp
.arg
[0] & 0xff;
1623 if (res
== 0) return 0;
1627 memset(buf
, 0x00, 3000);
1629 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1634 blockLen
= bufPtr
- buf
;
1637 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1639 while (bufPtr
- buf
+ 9 < blockLen
) {
1640 isTag
= bufPtr
[3] & 0x80 ? true:false;
1642 parity
= *((uint32_t *)(bufPtr
));
1646 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1647 memcpy(uid
, bufPtr
+ 2, 7);
1648 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1651 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1652 if (wantLogToFile
) {
1653 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1654 AddLogCurrentDT(logHexFileName
);
1656 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1658 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1659 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1660 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1671 static command_t CommandTable
[] =
1673 {"help", CmdHelp
, 1, "This help"},
1674 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1675 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1676 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1677 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1678 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1679 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1680 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1681 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages. param - <used card nonce>"},
1682 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1683 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1684 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1685 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1686 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1687 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1688 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1689 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1690 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1691 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1692 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1693 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1694 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1695 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1696 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1697 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1698 {NULL
, NULL
, 0, NULL
}
1701 int CmdHFMF(const char *Cmd
)
1704 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1706 CmdsParse(CommandTable
, Cmd
);
1710 int CmdHelp(const char *Cmd
)
1712 CmdsHelp(CommandTable
);