]>
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 //-----------------------------------------------------------------------------
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}};
34 while (ukbhit()) getchar();
37 printf("-------------------------------------------------------------------------\n");
38 printf("Executing command. It may take up to 30 min.\n");
39 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
40 printf("-------------------------------------------------------------------------\n");
48 printf("\naborted via keyboard!\n");
53 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
54 isOK
= resp
.arg
[0] & 0xff;
56 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
57 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
58 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
59 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
62 PrintAndLog("isOk:%02x", isOK
);
63 if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
70 if (isOK
!= 1) return 1;
72 // execute original function from util nonce2key
73 if (nonce2key(uid
, nt
, par_list
, ks_list
, &r_key
))
76 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
78 printf("------------------------------------------------------------------\n");
79 PrintAndLog("Key found:%012"llx
" \n", r_key
);
81 num_to_bytes(r_key
, 6, keyBlock
);
82 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
85 PrintAndLog("Found valid key:%012"llx
, r_key
);
88 if (isOK
!= 2) PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt
);
96 int CmdHF14AMfWrBl(const char *Cmd
)
100 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
101 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
106 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
107 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
111 blockNo
= param_get8(Cmd
, 0);
112 cmdp
= param_getchar(Cmd
, 1);
114 PrintAndLog("Key type must be A or B");
117 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
118 if (param_gethex(Cmd
, 2, key
, 12)) {
119 PrintAndLog("Key must include 12 HEX symbols");
122 if (param_gethex(Cmd
, 3, bldata
, 32)) {
123 PrintAndLog("Block data must include 32 HEX symbols");
126 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo
, keyType
, sprint_hex(key
, 6));
127 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
129 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
130 memcpy(c
.d
.asBytes
, key
, 6);
131 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
135 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
136 uint8_t isOK
= resp
.arg
[0] & 0xff;
137 PrintAndLog("isOk:%02x", isOK
);
139 PrintAndLog("Command execute timeout");
145 int CmdHF14AMfRdBl(const char *Cmd
)
149 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
155 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
156 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
160 blockNo
= param_get8(Cmd
, 0);
161 cmdp
= param_getchar(Cmd
, 1);
163 PrintAndLog("Key type must be A or B");
166 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
167 if (param_gethex(Cmd
, 2, key
, 12)) {
168 PrintAndLog("Key must include 12 HEX symbols");
171 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo
, keyType
, sprint_hex(key
, 6));
173 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
174 memcpy(c
.d
.asBytes
, key
, 6);
178 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
179 uint8_t isOK
= resp
.arg
[0] & 0xff;
180 uint8_t * data
= resp
.d
.asBytes
;
183 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
185 PrintAndLog("isOk:%02x", isOK
);
187 PrintAndLog("Command execute timeout");
193 int CmdHF14AMfRdSc(const char *Cmd
)
196 uint8_t sectorNo
= 0;
198 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
201 uint8_t * data
= NULL
;
206 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
207 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
211 sectorNo
= param_get8(Cmd
, 0);
213 PrintAndLog("Sector number must be less than 64");
216 cmdp
= param_getchar(Cmd
, 1);
218 PrintAndLog("Key type must be A or B");
221 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
222 if (param_gethex(Cmd
, 2, key
, 12)) {
223 PrintAndLog("Key must include 12 HEX symbols");
226 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo
, keyType
, sprint_hex(key
, 6));
228 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
229 memcpy(c
.d
.asBytes
, key
, 6);
234 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
235 isOK
= resp
.arg
[0] & 0xff;
236 data
= resp
.d
.asBytes
;
238 PrintAndLog("isOk:%02x", isOK
);
240 for (i
= 0; i
< 2; i
++) {
241 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
244 PrintAndLog("Command1 execute timeout");
249 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
250 isOK
= resp
.arg
[0] & 0xff;
251 data
= resp
.d
.asBytes
;
254 for (i
= 0; i
< 2; i
++) {
255 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
258 PrintAndLog("Command2 execute timeout");
264 int CmdHF14AMfDump(const char *Cmd
)
270 uint8_t rights
[40][4];
277 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
278 PrintAndLog("Could not find file dumpkeys.bin");
282 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
283 PrintAndLog("Could not create file name dumpdata.bin");
289 for (i
=0 ; i
<16 ; i
++) {
290 if (fread( keyA
[i
], 1, 6, fin
) == 0) {
291 PrintAndLog("File reading error.");
295 for (i
=0 ; i
<16 ; i
++) {
296 if (fread( keyB
[i
], 1, 6, fin
) == 0) {
297 PrintAndLog("File reading error.");
302 // Read access rights to sectors
304 PrintAndLog("|-----------------------------------------|");
305 PrintAndLog("|------ Reading sector access bits...-----|");
306 PrintAndLog("|-----------------------------------------|");
308 for (i
= 0 ; i
< 16 ; i
++) {
309 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
310 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
313 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
314 uint8_t isOK
= resp
.arg
[0] & 0xff;
315 uint8_t *data
= resp
.d
.asBytes
;
317 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
318 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
319 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
320 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
323 PrintAndLog("Could not get access rights for block %d", i
);
327 PrintAndLog("Command execute timeout");
331 // Read blocks and print to file
333 PrintAndLog("|-----------------------------------------|");
334 PrintAndLog("|----- Dumping all blocks to file... -----|");
335 PrintAndLog("|-----------------------------------------|");
338 for (i
=0 ; i
<16 ; i
++) {
339 for (j
=0 ; j
<4 ; j
++) {
340 bool received
= false;
343 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
344 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
346 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
349 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
350 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
351 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
353 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
355 else if (rights
[i
][j
] == 7) {
356 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
359 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
360 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
362 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
367 uint8_t isOK
= resp
.arg
[0] & 0xff;
368 uint8_t *data
= resp
.d
.asBytes
;
370 data
[0] = (keyA
[i
][0]);
371 data
[1] = (keyA
[i
][1]);
372 data
[2] = (keyA
[i
][2]);
373 data
[3] = (keyA
[i
][3]);
374 data
[4] = (keyA
[i
][4]);
375 data
[5] = (keyA
[i
][5]);
376 data
[10] = (keyB
[i
][0]);
377 data
[11] = (keyB
[i
][1]);
378 data
[12] = (keyB
[i
][2]);
379 data
[13] = (keyB
[i
][3]);
380 data
[14] = (keyB
[i
][4]);
381 data
[15] = (keyB
[i
][5]);
384 fwrite ( data
, 1, 16, fout
);
387 PrintAndLog("Could not get access rights for block %d", i
);
391 PrintAndLog("Command execute timeout");
402 int CmdHF14AMfRestore(const char *Cmd
)
407 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
408 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
415 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
416 PrintAndLog("Could not find file dumpdata.bin");
419 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
420 PrintAndLog("Could not find file dumpkeys.bin");
424 for (i
=0 ; i
<16 ; i
++) {
425 if (fread(keyA
[i
], 1, 6, fkeys
) == 0) {
426 PrintAndLog("File reading error.");
430 for (i
=0 ; i
<16 ; i
++) {
431 if (fread(keyB
[i
], 1, 6, fkeys
) == 0) {
432 PrintAndLog("File reading error.");
437 PrintAndLog("Restoring dumpdata.bin to card");
439 for (i
=0 ; i
<16 ; i
++) {
440 for( j
=0 ; j
<4 ; j
++) {
441 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
442 memcpy(c
.d
.asBytes
, key
, 6);
444 if (fread(bldata
, 1, 16, fdump
) == 0) {
445 PrintAndLog("File reading error.");
450 bldata
[0] = (keyA
[i
][0]);
451 bldata
[1] = (keyA
[i
][1]);
452 bldata
[2] = (keyA
[i
][2]);
453 bldata
[3] = (keyA
[i
][3]);
454 bldata
[4] = (keyA
[i
][4]);
455 bldata
[5] = (keyA
[i
][5]);
456 bldata
[10] = (keyB
[i
][0]);
457 bldata
[11] = (keyB
[i
][1]);
458 bldata
[12] = (keyB
[i
][2]);
459 bldata
[13] = (keyB
[i
][3]);
460 bldata
[14] = (keyB
[i
][4]);
461 bldata
[15] = (keyB
[i
][5]);
464 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
467 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
470 if ((ch != 'y') && (ch != 'Y')){
471 PrintAndLog("Aborting !");
476 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
480 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
481 uint8_t isOK
= resp
.arg
[0] & 0xff;
482 PrintAndLog("isOk:%02x", isOK
);
484 PrintAndLog("Command execute timeout");
494 int CmdHF14AMfNested(const char *Cmd
)
496 int i
, j
, res
, iterations
;
497 sector
* e_sector
= NULL
;
500 uint8_t trgBlockNo
= 0;
501 uint8_t trgKeyType
= 0;
504 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
505 uint8_t keyBlock
[16 * 6];
507 int transferToEml
= 0;
509 int createDumpFile
= 0;
511 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
512 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
517 PrintAndLog("Usage:");
518 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
519 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
520 PrintAndLog(" <target block number> <target key A/B> [t]");
521 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
522 PrintAndLog("t - transfer keys into emulator memory");
523 PrintAndLog("d - write keys to binary file");
525 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
526 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
527 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
528 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
532 cmdp
= param_getchar(Cmd
, 0);
533 blockNo
= param_get8(Cmd
, 1);
534 ctmp
= param_getchar(Cmd
, 2);
536 PrintAndLog("Key type must be A or B");
539 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
540 if (param_gethex(Cmd
, 3, key
, 12)) {
541 PrintAndLog("Key must include 12 HEX symbols");
545 if (cmdp
== 'o' || cmdp
== 'O') {
547 trgBlockNo
= param_get8(Cmd
, 4);
548 ctmp
= param_getchar(Cmd
, 5);
550 PrintAndLog("Target key type must be A or B");
553 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
556 case '0': SectorsCnt
= 05; break;
557 case '1': SectorsCnt
= 16; break;
558 case '2': SectorsCnt
= 32; break;
559 case '4': SectorsCnt
= 64; break;
560 default: SectorsCnt
= 16;
564 ctmp
= param_getchar(Cmd
, 4);
565 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
566 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
568 ctmp
= param_getchar(Cmd
, 6);
569 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
570 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
572 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
574 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
577 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) {
578 PrintAndLog("Nested error.");
582 for (i
= 0; i
< 16; i
++) {
583 PrintAndLog("count=%d key= %s", i
, sprint_hex(keyBlock
+ i
* 6, 6));
587 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
589 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
591 PrintAndLog("Found valid key:%012"llx
, key64
);
593 // transfer key to the emulator
595 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
598 num_to_bytes(key64
, 6, keyBlock
);
600 num_to_bytes(key64
, 6, &keyBlock
[10]);
601 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
604 PrintAndLog("No valid key found");
607 else { // ------------------------------------ multiple sectors working
608 blDiff
= blockNo
% 4;
609 PrintAndLog("Block shift=%d", blDiff
);
610 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
611 if (e_sector
== NULL
) return 1;
613 //test current key 4 sectors
614 memcpy(keyBlock
, key
, 6);
615 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 1 * 6));
616 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 2 * 6));
617 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 3 * 6));
618 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 4 * 6));
619 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
621 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
622 for (i
= 0; i
< SectorsCnt
; i
++) {
623 for (j
= 0; j
< 2; j
++) {
624 if (e_sector
[i
].foundKey
[j
]) continue;
626 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
629 e_sector
[i
].Key
[j
] = key64
;
630 e_sector
[i
].foundKey
[j
] = 1;
637 PrintAndLog("nested...");
638 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
639 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4)
640 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
641 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
642 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) continue;
646 //try keys from nested
647 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
649 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
651 PrintAndLog("Found valid key:%012"llx
, key64
);
652 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
653 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
658 PrintAndLog("Iterations count: %d", iterations
);
660 PrintAndLog("|---|----------------|---|----------------|---|");
661 PrintAndLog("|sec|key A |res|key B |res|");
662 PrintAndLog("|---|----------------|---|----------------|---|");
663 for (i
= 0; i
< SectorsCnt
; i
++) {
664 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
665 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
667 PrintAndLog("|---|----------------|---|----------------|---|");
669 // transfer them to the emulator
671 for (i
= 0; i
< SectorsCnt
; i
++) {
672 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
673 if (e_sector
[i
].foundKey
[0])
674 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
675 if (e_sector
[i
].foundKey
[1])
676 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
677 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
682 if (createDumpFile
) {
683 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
684 PrintAndLog("Could not create file dumpkeys.bin");
688 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
689 for(i
=0; i
<16; i
++) {
690 if (e_sector
[i
].foundKey
[0]){
691 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
692 fwrite ( tempkey
, 1, 6, fkeys
);
695 fwrite ( &standart
, 1, 6, fkeys
);
698 for(i
=0; i
<16; i
++) {
699 if (e_sector
[i
].foundKey
[1]){
700 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
701 fwrite ( tempkey
, 1, 6, fkeys
);
704 fwrite ( &standart
, 1, 6, fkeys
);
717 get_trailer_block (uint32_t uiBlock
)
719 // Test if we are in the small or big sectors
720 uint32_t trailer_block
= 0;
722 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
724 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
726 return trailer_block
;
728 int CmdHF14AMfChk(const char *Cmd
)
731 char filename
[256]={0};
733 uint8_t *keyBlock
= NULL
, *p
;
734 uint8_t stKeyBlock
= 20;
740 uint8_t SectorsCnt
= 1;
744 int transferToEml
= 0;
745 int createDumpFile
= 0;
747 keyBlock
= calloc(stKeyBlock
, 6);
748 if (keyBlock
== NULL
) return 1;
750 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 0 * 6)); // Default key (first key used by program if no user defined key)
751 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 1 * 6)); // Blank key
752 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 2 * 6)); // NFCForum MAD key
753 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
754 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 4 * 6));
755 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 5 * 6));
756 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 6 * 6));
757 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 7 * 6));
758 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 8 * 6));
759 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 9 * 6));
760 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 10 * 6));
761 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 11 * 6));
762 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 12 * 6));
765 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
766 PrintAndLog(" * - all sectors");
767 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
768 // PrintAndLog("d - write keys to binary file\n");
770 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
771 PrintAndLog(" hf mf chk *1 ? t");
775 if (param_getchar(Cmd
, 0)=='*') {
777 switch(param_getchar(Cmd
+1, 0)) {
778 case '0': SectorsCnt
= 5; break;
779 case '1': SectorsCnt
= 16; break;
780 case '2': SectorsCnt
= 32; break;
781 case '4': SectorsCnt
= 40; break;
782 default: SectorsCnt
= 16;
786 blockNo
= param_get8(Cmd
, 0);
788 ctmp
= param_getchar(Cmd
, 1);
800 PrintAndLog("Key type must be A , B or ?");
804 ctmp
= param_getchar(Cmd
, 2);
805 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
806 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
808 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
809 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
810 if ( stKeyBlock
- keycnt
< 2) {
811 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
813 PrintAndLog("Cannot allocate memory for Keys");
819 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
820 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
821 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
825 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
826 PrintAndLog("File name too long");
831 if ( (f
= fopen( filename
, "r")) ) {
833 memset(buf
, 0, sizeof(buf
));
834 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
835 PrintAndLog("File reading error.");
839 if (strlen(buf
) < 12 || buf
[11] == '\n')
842 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
844 if( buf
[0]=='#' ) continue; //The line start with # is remcommnet,skip
846 if (!isxdigit(buf
[0])){
847 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
853 if ( stKeyBlock
- keycnt
< 2) {
854 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
856 PrintAndLog("Cannot allocate memory for defKeys");
862 memset(keyBlock
+ 6 * keycnt
, 0, 6);
863 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
864 PrintAndLog("chk custom key[%d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
868 PrintAndLog("File: %s: not found or locked.", filename
);
877 PrintAndLog("No key specified,try default keys");
878 for (;keycnt
<=12; keycnt
++)
879 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
880 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
881 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
884 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
886 for (int i
=0; i
<SectorsCnt
; ++i
) {
887 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
888 int size
= keycnt
>8?8:keycnt
;
889 for (int c
= 0; c
< keycnt
; c
+=size
) {
890 size
=keycnt
-c
>8?8:keycnt
-c
;
891 res
= mfCheckKeys(b
, t
, size
, keyBlock
+6*c
, &key64
);
894 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
897 mfEmlGetMem(block
, get_trailer_block(b
), 1);
898 num_to_bytes(key64
, 6, block
+ t
*10);
899 mfEmlSetMem(block
, get_trailer_block(b
), 1);
904 printf("Not found yet, keycnt:%d\r", c
+size
);
908 PrintAndLog("Command execute timeout");
911 b
<127?(b
+=4):(b
+=16);
919 if (createDumpFile) {
920 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
921 PrintAndLog("Could not create file dumpkeys.bin");
925 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
926 for(i=0; i<16; i++) {
927 if (e_sector[i].foundKey[0]){
928 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
929 fwrite ( tempkey, 1, 6, fkeys );
932 fwrite ( &standart, 1, 6, fkeys );
935 for(i=0; i<16; i++) {
936 if (e_sector[i].foundKey[1]){
937 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
938 fwrite ( tempkey, 1, 6, fkeys );
941 fwrite ( &standart, 1, 6, fkeys );
950 int CmdHF14AMf1kSim(const char *Cmd
)
952 uint8_t uid
[4] = {0, 0, 0, 0};
954 if (param_getchar(Cmd
, 0) == 'h') {
955 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
956 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
960 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
961 PrintAndLog("UID must include 8 HEX symbols");
964 PrintAndLog(" uid:%s ", sprint_hex(uid
, 4));
966 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {0, 0, 0}};
967 memcpy(c
.d
.asBytes
, uid
, 4);
973 int CmdHF14AMfDbg(const char *Cmd
)
975 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
977 PrintAndLog("Max debud mode parameter is 4 \n");
980 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
981 PrintAndLog("Usage: hf mf dbg <debug level>");
982 PrintAndLog(" 0 - no debug messages");
983 PrintAndLog(" 1 - error messages");
984 PrintAndLog(" 2 - all messages");
985 PrintAndLog(" 4 - extended debug mode");
989 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
995 int CmdHF14AMfEGet(const char *Cmd
)
998 uint8_t data
[3 * 16];
1001 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1002 PrintAndLog("Usage: hf mf eget <block number>");
1003 PrintAndLog(" sample: hf mf eget 0 ");
1007 blockNo
= param_get8(Cmd
, 0);
1010 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1011 for (i
= 0; i
< 3; i
++) {
1012 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1015 PrintAndLog("Command execute timeout");
1021 int CmdHF14AMfEClear(const char *Cmd
)
1023 if (param_getchar(Cmd
, 0) == 'h') {
1024 PrintAndLog("Usage: hf mf eclr");
1025 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1029 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1034 int CmdHF14AMfESet(const char *Cmd
)
1036 uint8_t memBlock
[16];
1037 uint8_t blockNo
= 0;
1039 memset(memBlock
, 0x00, sizeof(memBlock
));
1041 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1042 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1043 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1047 blockNo
= param_get8(Cmd
, 0);
1049 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1050 PrintAndLog("block data must include 32 HEX symbols");
1055 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1056 memcpy(c
.d
.asBytes
, memBlock
, 16);
1061 int CmdHF14AMfELoad(const char *Cmd
)
1065 char * fnameptr
= filename
;
1068 int i
, len
, blockNum
;
1070 memset(filename
, 0, sizeof(filename
));
1071 memset(buf
, 0, sizeof(buf
));
1073 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1074 PrintAndLog("It loads emul dump from the file `filename.eml`");
1075 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1076 PrintAndLog(" sample: hf mf eload filename");
1081 if (len
> 14) len
= 14;
1083 memcpy(filename
, Cmd
, len
);
1086 sprintf(fnameptr
, ".eml");
1089 f
= fopen(filename
, "r");
1091 PrintAndLog("File not found or locked.");
1097 memset(buf
, 0, sizeof(buf
));
1098 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1099 PrintAndLog("File reading error.");
1103 if (strlen(buf
) < 32){
1104 if(strlen(buf
) && feof(f
))
1106 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1109 for (i
= 0; i
< 32; i
+= 2)
1110 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1111 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1113 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1114 PrintAndLog("Cant set emul block: %d", blockNum
);
1119 if (blockNum
>= 32 * 4 + 8 * 16) break;
1123 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1124 PrintAndLog("File content error. There must be 64 blocks");
1127 PrintAndLog("Loaded from file: %s", filename
);
1131 int CmdHF14AMfESave(const char *Cmd
)
1135 char * fnameptr
= filename
;
1139 memset(filename
, 0, sizeof(filename
));
1140 memset(buf
, 0, sizeof(buf
));
1142 if (param_getchar(Cmd
, 0) == 'h') {
1143 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1144 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1145 PrintAndLog(" sample: hf mf esave ");
1146 PrintAndLog(" hf mf esave filename");
1151 if (len
> 14) len
= 14;
1155 if (mfEmlGetMem(buf
, 0, 1)) {
1156 PrintAndLog("Cant get block: %d", 0);
1159 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1160 sprintf(fnameptr
, "%02x", buf
[j
]);
1162 memcpy(filename
, Cmd
, len
);
1166 sprintf(fnameptr
, ".eml");
1169 f
= fopen(filename
, "w+");
1172 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1173 if (mfEmlGetMem(buf
, i
, 1)) {
1174 PrintAndLog("Cant get block: %d", i
);
1177 for (j
= 0; j
< 16; j
++)
1178 fprintf(f
, "%02x", buf
[j
]);
1183 PrintAndLog("Saved to file: %s", filename
);
1188 int CmdHF14AMfECFill(const char *Cmd
)
1190 uint8_t keyType
= 0;
1192 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1193 PrintAndLog("Usage: hf mf efill <key A/B>");
1194 PrintAndLog("sample: hf mf efill A");
1195 PrintAndLog("Card data blocks transfers to card emulator memory.");
1196 PrintAndLog("Keys must be laid in the simulator memory. \n");
1200 char ctmp
= param_getchar(Cmd
, 0);
1202 PrintAndLog("Key type must be A or B");
1205 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1207 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1212 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1216 uint64_t keyA
, keyB
;
1218 PrintAndLog("|---|----------------|----------------|");
1219 PrintAndLog("|sec|key A |key B |");
1220 PrintAndLog("|---|----------------|----------------|");
1221 for (i
= 0; i
< 40; i
++) {
1222 b
<127?(b
+=4):(b
+=16);
1223 if (mfEmlGetMem(data
, b
, 1)) {
1224 PrintAndLog("error get block %d", b
);
1227 keyA
= bytes_to_num(data
, 6);
1228 keyB
= bytes_to_num(data
+ 10, 6);
1229 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1231 PrintAndLog("|---|----------------|----------------|");
1236 int CmdHF14AMfCSetUID(const char *Cmd
)
1238 uint8_t wipeCard
= 0;
1243 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1244 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1245 PrintAndLog("sample: hf mf csetuid 01020304 w");
1246 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1247 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1251 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1252 PrintAndLog("UID must include 8 HEX symbols");
1256 char ctmp
= param_getchar(Cmd
, 1);
1257 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1259 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1261 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1263 PrintAndLog("Can't set UID. error=%d", res
);
1267 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1271 int CmdHF14AMfCSetBlk(const char *Cmd
)
1274 uint8_t memBlock
[16];
1275 uint8_t blockNo
= 0;
1277 memset(memBlock
, 0x00, sizeof(memBlock
));
1279 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1280 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1281 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1282 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1283 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1287 blockNo
= param_get8(Cmd
, 0);
1289 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1290 PrintAndLog("block data must include 32 HEX symbols");
1294 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1296 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1298 PrintAndLog("Can't write block. error=%d", res
);
1302 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1306 int CmdHF14AMfCLoad(const char *Cmd
)
1310 char * fnameptr
= filename
;
1313 uint8_t fillFromEmulator
= 0;
1314 int i
, len
, blockNum
, flags
;
1316 memset(filename
, 0, sizeof(filename
));
1317 memset(buf
, 0, sizeof(buf
));
1319 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1320 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1321 PrintAndLog("or from emulator memory (option `e`)");
1322 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1323 PrintAndLog(" or: hf mf cload e ");
1324 PrintAndLog(" sample: hf mf cload filename");
1328 char ctmp
= param_getchar(Cmd
, 0);
1329 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1331 if (fillFromEmulator
) {
1332 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1333 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1334 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1335 PrintAndLog("Cant get block: %d", blockNum
);
1339 if (blockNum
== 2) flags
= 0;
1340 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1342 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1343 PrintAndLog("Cant set magic card block: %d", blockNum
);
1350 if (len
> 14) len
= 14;
1352 memcpy(filename
, Cmd
, len
);
1355 sprintf(fnameptr
, ".eml");
1358 f
= fopen(filename
, "r");
1360 PrintAndLog("File not found or locked.");
1365 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1367 memset(buf
, 0, sizeof(buf
));
1368 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1369 PrintAndLog("File reading error.");
1373 if (strlen(buf
) < 32){
1374 if(strlen(buf
) && feof(f
))
1376 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1379 for (i
= 0; i
< 32; i
+= 2)
1380 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1382 if (blockNum
== 2) flags
= 0;
1383 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1385 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1386 PrintAndLog("Cant set magic card block: %d", blockNum
);
1391 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1395 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1396 PrintAndLog("File content error. There must be 64 blocks");
1399 PrintAndLog("Loaded from file: %s", filename
);
1404 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1405 uint8_t memBlock
[16];
1406 uint8_t blockNo
= 0;
1408 memset(memBlock
, 0x00, sizeof(memBlock
));
1410 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1411 PrintAndLog("Usage: hf mf cgetblk <block number>");
1412 PrintAndLog("sample: hf mf cgetblk 1");
1413 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1417 blockNo
= param_get8(Cmd
, 0);
1419 PrintAndLog("--block number:%02x ", blockNo
);
1421 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1423 PrintAndLog("Can't read block. error=%d", res
);
1427 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1431 int CmdHF14AMfCGetSc(const char *Cmd
) {
1432 uint8_t memBlock
[16];
1433 uint8_t sectorNo
= 0;
1435 memset(memBlock
, 0x00, sizeof(memBlock
));
1437 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1438 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1439 PrintAndLog("sample: hf mf cgetsc 0");
1440 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1444 sectorNo
= param_get8(Cmd
, 0);
1445 if (sectorNo
> 15) {
1446 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1450 PrintAndLog("--sector number:%02x ", sectorNo
);
1452 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1453 for (i
= 0; i
< 4; i
++) {
1454 if (i
== 1) flags
= 0;
1455 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1457 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1459 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1463 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1468 int CmdHF14AMfCSave(const char *Cmd
) {
1472 char * fnameptr
= filename
;
1473 uint8_t fillFromEmulator
= 0;
1475 int i
, j
, len
, flags
;
1477 memset(filename
, 0, sizeof(filename
));
1478 memset(buf
, 0, sizeof(buf
));
1480 if (param_getchar(Cmd
, 0) == 'h') {
1481 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1482 PrintAndLog("or into emulator memory (option `e`)");
1483 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1484 PrintAndLog(" sample: hf mf esave ");
1485 PrintAndLog(" hf mf esave filename");
1486 PrintAndLog(" hf mf esave e \n");
1490 char ctmp
= param_getchar(Cmd
, 0);
1491 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1493 if (fillFromEmulator
) {
1494 // put into emulator
1495 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1496 for (i
= 0; i
< 16 * 4; i
++) {
1497 if (i
== 1) flags
= 0;
1498 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1500 if (mfCGetBlock(i
, buf
, flags
)) {
1501 PrintAndLog("Cant get block: %d", i
);
1505 if (mfEmlSetMem(buf
, i
, 1)) {
1506 PrintAndLog("Cant set emul block: %d", i
);
1513 if (len
> 14) len
= 14;
1517 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1518 PrintAndLog("Cant get block: %d", 0);
1521 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1522 sprintf(fnameptr
, "%02x", buf
[j
]);
1524 memcpy(filename
, Cmd
, len
);
1528 sprintf(fnameptr
, ".eml");
1531 f
= fopen(filename
, "w+");
1534 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1535 for (i
= 0; i
< 16 * 4; i
++) {
1536 if (i
== 1) flags
= 0;
1537 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1539 if (mfCGetBlock(i
, buf
, flags
)) {
1540 PrintAndLog("Cant get block: %d", i
);
1543 for (j
= 0; j
< 16; j
++)
1544 fprintf(f
, "%02x", buf
[j
]);
1549 PrintAndLog("Saved to file: %s", filename
);
1555 int CmdHF14AMfSniff(const char *Cmd
){
1557 bool wantLogToFile
= 0;
1558 bool wantDecrypt
= 0;
1559 //bool wantSaveToEml = 0; TODO
1560 bool wantSaveToEmlFile
= 0;
1574 uint8_t * bufPtr
= buf
;
1575 memset(buf
, 0x00, 3000);
1577 if (param_getchar(Cmd
, 0) == 'h') {
1578 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1579 PrintAndLog("You can specify:");
1580 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1581 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1582 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1583 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1584 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1585 PrintAndLog(" sample: hf mf sniff l d e");
1589 for (int i
= 0; i
< 4; i
++) {
1590 char ctmp
= param_getchar(Cmd
, i
);
1591 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1592 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1593 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1594 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1597 printf("-------------------------------------------------------------------------\n");
1598 printf("Executing command. \n");
1599 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1600 printf("Press the key on pc keyboard to abort the client.\n");
1601 printf("-------------------------------------------------------------------------\n");
1603 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1612 printf("\naborted via keyboard!\n");
1617 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1618 res
= resp
.arg
[0] & 0xff;
1622 if (res
== 0) return 0;
1626 memset(buf
, 0x00, 3000);
1628 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1633 blockLen
= bufPtr
- buf
;
1636 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1638 while (bufPtr
- buf
+ 9 < blockLen
) {
1639 isTag
= bufPtr
[3] & 0x80 ? true:false;
1641 parity
= *((uint32_t *)(bufPtr
));
1645 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1646 memcpy(uid
, bufPtr
+ 2, 7);
1647 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1650 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1651 if (wantLogToFile
) {
1652 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1653 AddLogCurrentDT(logHexFileName
);
1655 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1657 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1658 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1659 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1670 static command_t CommandTable
[] =
1672 {"help", CmdHelp
, 1, "This help"},
1673 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1674 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1675 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1676 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1677 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1678 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1679 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1680 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages. param - <used card nonce>"},
1681 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1682 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1683 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1684 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1685 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1686 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1687 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1688 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1689 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1690 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1691 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1692 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1693 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1694 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1695 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1696 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1697 {NULL
, NULL
, 0, NULL
}
1700 int CmdHFMF(const char *Cmd
)
1703 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1705 CmdsParse(CommandTable
, Cmd
);
1709 int CmdHelp(const char *Cmd
)
1711 CmdsHelp(CommandTable
);