]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
96eb800782a3fc1110eca30191d1efc741a06a05
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
15 int CmdHF14AMifare(const char *Cmd
)
18 uint32_t nt
= 0, nr
= 0;
19 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
21 uint8_t keyBlock
[8] = {0};
23 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
26 printf("-------------------------------------------------------------------------\n");
27 printf("Executing command. Expected execution time: 25sec on average :-)\n");
28 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
29 printf("-------------------------------------------------------------------------\n");
37 while (ukbhit()) getchar();
46 printf("\naborted via keyboard!\n");
51 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
52 isOK
= resp
.arg
[0] & 0xff;
53 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
54 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
55 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
56 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
57 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
59 if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
67 if (isOK
!= 1) return 1;
69 // execute original function from util nonce2key
70 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
))
73 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
75 printf("------------------------------------------------------------------\n");
76 PrintAndLog("Key found:%012"llx
" \n", r_key
);
78 num_to_bytes(r_key
, 6, keyBlock
);
79 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
82 PrintAndLog("Found valid key:%012"llx
, r_key
);
85 if (isOK
!= 2) PrintAndLog("Found invalid key. ");
86 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
94 int CmdHF14AMfWrBl(const char *Cmd
)
98 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
99 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
104 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
105 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
109 blockNo
= param_get8(Cmd
, 0);
110 cmdp
= param_getchar(Cmd
, 1);
112 PrintAndLog("Key type must be A or B");
115 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
116 if (param_gethex(Cmd
, 2, key
, 12)) {
117 PrintAndLog("Key must include 12 HEX symbols");
120 if (param_gethex(Cmd
, 3, bldata
, 32)) {
121 PrintAndLog("Block data must include 32 HEX symbols");
124 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo
, keyType
, sprint_hex(key
, 6));
125 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
127 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
128 memcpy(c
.d
.asBytes
, key
, 6);
129 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
133 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
134 uint8_t isOK
= resp
.arg
[0] & 0xff;
135 PrintAndLog("isOk:%02x", isOK
);
137 PrintAndLog("Command execute timeout");
143 int CmdHF14AMfRdBl(const char *Cmd
)
147 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
153 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
154 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
158 blockNo
= param_get8(Cmd
, 0);
159 cmdp
= param_getchar(Cmd
, 1);
161 PrintAndLog("Key type must be A or B");
164 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
165 if (param_gethex(Cmd
, 2, key
, 12)) {
166 PrintAndLog("Key must include 12 HEX symbols");
169 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo
, keyType
, sprint_hex(key
, 6));
171 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
172 memcpy(c
.d
.asBytes
, key
, 6);
176 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
177 uint8_t isOK
= resp
.arg
[0] & 0xff;
178 uint8_t * data
= resp
.d
.asBytes
;
181 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
183 PrintAndLog("isOk:%02x", isOK
);
185 PrintAndLog("Command execute timeout");
191 int CmdHF14AMfRdSc(const char *Cmd
)
194 uint8_t sectorNo
= 0;
196 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
199 uint8_t * data
= NULL
;
204 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
205 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
209 sectorNo
= param_get8(Cmd
, 0);
211 PrintAndLog("Sector number must be less than 64");
214 cmdp
= param_getchar(Cmd
, 1);
216 PrintAndLog("Key type must be A or B");
219 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
220 if (param_gethex(Cmd
, 2, key
, 12)) {
221 PrintAndLog("Key must include 12 HEX symbols");
224 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo
, keyType
, sprint_hex(key
, 6));
226 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
227 memcpy(c
.d
.asBytes
, key
, 6);
232 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
233 isOK
= resp
.arg
[0] & 0xff;
234 data
= resp
.d
.asBytes
;
236 PrintAndLog("isOk:%02x", isOK
);
238 for (i
= 0; i
< 2; i
++) {
239 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
242 PrintAndLog("Command1 execute timeout");
247 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
248 isOK
= resp
.arg
[0] & 0xff;
249 data
= resp
.d
.asBytes
;
252 for (i
= 0; i
< 2; i
++) {
253 PrintAndLog("data:%s", sprint_hex(data
+ i
* 16, 16));
256 PrintAndLog("Command2 execute timeout");
262 int CmdHF14AMfDump(const char *Cmd
)
268 uint8_t rights
[40][4];
275 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
276 PrintAndLog("Could not find file dumpkeys.bin");
280 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
281 PrintAndLog("Could not create file name dumpdata.bin");
287 for (i
=0 ; i
<16 ; i
++) {
288 if (fread( keyA
[i
], 1, 6, fin
) == 0) {
289 PrintAndLog("File reading error.");
293 for (i
=0 ; i
<16 ; i
++) {
294 if (fread( keyB
[i
], 1, 6, fin
) == 0) {
295 PrintAndLog("File reading error.");
300 // Read access rights to sectors
302 PrintAndLog("|-----------------------------------------|");
303 PrintAndLog("|------ Reading sector access bits...-----|");
304 PrintAndLog("|-----------------------------------------|");
306 for (i
= 0 ; i
< 16 ; i
++) {
307 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
308 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
311 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
312 uint8_t isOK
= resp
.arg
[0] & 0xff;
313 uint8_t *data
= resp
.d
.asBytes
;
315 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
316 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
317 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
318 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
321 PrintAndLog("Could not get access rights for block %d", i
);
325 PrintAndLog("Command execute timeout");
329 // Read blocks and print to file
331 PrintAndLog("|-----------------------------------------|");
332 PrintAndLog("|----- Dumping all blocks to file... -----|");
333 PrintAndLog("|-----------------------------------------|");
336 for (i
=0 ; i
<16 ; i
++) {
337 for (j
=0 ; j
<4 ; j
++) {
338 bool received
= false;
341 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
342 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
344 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
347 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
348 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
349 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
351 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
353 else if (rights
[i
][j
] == 7) {
354 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
357 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
358 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
360 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
365 uint8_t isOK
= resp
.arg
[0] & 0xff;
366 uint8_t *data
= resp
.d
.asBytes
;
368 data
[0] = (keyA
[i
][0]);
369 data
[1] = (keyA
[i
][1]);
370 data
[2] = (keyA
[i
][2]);
371 data
[3] = (keyA
[i
][3]);
372 data
[4] = (keyA
[i
][4]);
373 data
[5] = (keyA
[i
][5]);
374 data
[10] = (keyB
[i
][0]);
375 data
[11] = (keyB
[i
][1]);
376 data
[12] = (keyB
[i
][2]);
377 data
[13] = (keyB
[i
][3]);
378 data
[14] = (keyB
[i
][4]);
379 data
[15] = (keyB
[i
][5]);
382 fwrite ( data
, 1, 16, fout
);
385 PrintAndLog("Could not get access rights for block %d", i
);
389 PrintAndLog("Command execute timeout");
400 int CmdHF14AMfRestore(const char *Cmd
)
405 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
406 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
413 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
414 PrintAndLog("Could not find file dumpdata.bin");
417 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
418 PrintAndLog("Could not find file dumpkeys.bin");
422 for (i
=0 ; i
<16 ; i
++) {
423 if (fread(keyA
[i
], 1, 6, fkeys
) == 0) {
424 PrintAndLog("File reading error.");
428 for (i
=0 ; i
<16 ; i
++) {
429 if (fread(keyB
[i
], 1, 6, fkeys
) == 0) {
430 PrintAndLog("File reading error.");
435 PrintAndLog("Restoring dumpdata.bin to card");
437 for (i
=0 ; i
<16 ; i
++) {
438 for( j
=0 ; j
<4 ; j
++) {
439 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
440 memcpy(c
.d
.asBytes
, key
, 6);
442 if (fread(bldata
, 1, 16, fdump
) == 0) {
443 PrintAndLog("File reading error.");
448 bldata
[0] = (keyA
[i
][0]);
449 bldata
[1] = (keyA
[i
][1]);
450 bldata
[2] = (keyA
[i
][2]);
451 bldata
[3] = (keyA
[i
][3]);
452 bldata
[4] = (keyA
[i
][4]);
453 bldata
[5] = (keyA
[i
][5]);
454 bldata
[10] = (keyB
[i
][0]);
455 bldata
[11] = (keyB
[i
][1]);
456 bldata
[12] = (keyB
[i
][2]);
457 bldata
[13] = (keyB
[i
][3]);
458 bldata
[14] = (keyB
[i
][4]);
459 bldata
[15] = (keyB
[i
][5]);
462 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
465 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
468 if ((ch != 'y') && (ch != 'Y')){
469 PrintAndLog("Aborting !");
474 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
478 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
479 uint8_t isOK
= resp
.arg
[0] & 0xff;
480 PrintAndLog("isOk:%02x", isOK
);
482 PrintAndLog("Command execute timeout");
492 int CmdHF14AMfNested(const char *Cmd
)
494 int i
, j
, res
, iterations
;
495 sector
* e_sector
= NULL
;
498 uint8_t trgBlockNo
= 0;
499 uint8_t trgKeyType
= 0;
502 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
503 uint8_t keyBlock
[16 * 6];
505 int transferToEml
= 0;
507 int createDumpFile
= 0;
509 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
510 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
515 PrintAndLog("Usage:");
516 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
517 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
518 PrintAndLog(" <target block number> <target key A/B> [t]");
519 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
520 PrintAndLog("t - transfer keys into emulator memory");
521 PrintAndLog("d - write keys to binary file");
523 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
524 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
525 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
526 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
530 cmdp
= param_getchar(Cmd
, 0);
531 blockNo
= param_get8(Cmd
, 1);
532 ctmp
= param_getchar(Cmd
, 2);
534 PrintAndLog("Key type must be A or B");
537 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
538 if (param_gethex(Cmd
, 3, key
, 12)) {
539 PrintAndLog("Key must include 12 HEX symbols");
543 if (cmdp
== 'o' || cmdp
== 'O') {
545 trgBlockNo
= param_get8(Cmd
, 4);
546 ctmp
= param_getchar(Cmd
, 5);
548 PrintAndLog("Target key type must be A or B");
551 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
554 case '0': SectorsCnt
= 05; break;
555 case '1': SectorsCnt
= 16; break;
556 case '2': SectorsCnt
= 32; break;
557 case '4': SectorsCnt
= 64; break;
558 default: SectorsCnt
= 16;
562 ctmp
= param_getchar(Cmd
, 4);
563 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
564 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
566 ctmp
= param_getchar(Cmd
, 6);
567 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
568 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
570 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
572 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
575 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) {
576 PrintAndLog("Nested error.");
580 for (i
= 0; i
< 16; i
++) {
581 PrintAndLog("count=%d key= %s", i
, sprint_hex(keyBlock
+ i
* 6, 6));
585 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
587 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
589 PrintAndLog("Found valid key:%012"llx
, key64
);
591 // transfer key to the emulator
593 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
596 num_to_bytes(key64
, 6, keyBlock
);
598 num_to_bytes(key64
, 6, &keyBlock
[10]);
599 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
602 PrintAndLog("No valid key found");
605 else { // ------------------------------------ multiple sectors working
606 blDiff
= blockNo
% 4;
607 PrintAndLog("Block shift=%d", blDiff
);
608 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
609 if (e_sector
== NULL
) return 1;
611 //test current key 4 sectors
612 memcpy(keyBlock
, key
, 6);
613 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 1 * 6));
614 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 2 * 6));
615 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 3 * 6));
616 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 4 * 6));
617 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
619 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
620 for (i
= 0; i
< SectorsCnt
; i
++) {
621 for (j
= 0; j
< 2; j
++) {
622 if (e_sector
[i
].foundKey
[j
]) continue;
624 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
627 e_sector
[i
].Key
[j
] = key64
;
628 e_sector
[i
].foundKey
[j
] = 1;
635 PrintAndLog("nested...");
636 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
637 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4)
638 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
639 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
640 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) continue;
644 //try keys from nested
645 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
647 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
649 PrintAndLog("Found valid key:%012"llx
, key64
);
650 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
651 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
656 PrintAndLog("Iterations count: %d", iterations
);
658 PrintAndLog("|---|----------------|---|----------------|---|");
659 PrintAndLog("|sec|key A |res|key B |res|");
660 PrintAndLog("|---|----------------|---|----------------|---|");
661 for (i
= 0; i
< SectorsCnt
; i
++) {
662 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
663 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
665 PrintAndLog("|---|----------------|---|----------------|---|");
667 // transfer them to the emulator
669 for (i
= 0; i
< SectorsCnt
; i
++) {
670 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
671 if (e_sector
[i
].foundKey
[0])
672 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
673 if (e_sector
[i
].foundKey
[1])
674 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
675 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
680 if (createDumpFile
) {
681 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
682 PrintAndLog("Could not create file dumpkeys.bin");
686 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
687 for(i
=0; i
<16; i
++) {
688 if (e_sector
[i
].foundKey
[0]){
689 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
690 fwrite ( tempkey
, 1, 6, fkeys
);
693 fwrite ( &standart
, 1, 6, fkeys
);
696 for(i
=0; i
<16; i
++) {
697 if (e_sector
[i
].foundKey
[1]){
698 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
699 fwrite ( tempkey
, 1, 6, fkeys
);
702 fwrite ( &standart
, 1, 6, fkeys
);
715 get_trailer_block (uint32_t uiBlock
)
717 // Test if we are in the small or big sectors
718 uint32_t trailer_block
= 0;
720 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
722 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
724 return trailer_block
;
726 int CmdHF14AMfChk(const char *Cmd
)
729 char filename
[256]={0};
731 uint8_t *keyBlock
= NULL
, *p
;
732 uint8_t stKeyBlock
= 20;
738 uint8_t SectorsCnt
= 1;
742 int transferToEml
= 0;
743 int createDumpFile
= 0;
745 keyBlock
= calloc(stKeyBlock
, 6);
746 if (keyBlock
== NULL
) return 1;
748 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 0 * 6)); // Default key (first key used by program if no user defined key)
749 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 1 * 6)); // Blank key
750 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 2 * 6)); // NFCForum MAD key
751 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
752 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 4 * 6));
753 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 5 * 6));
754 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 6 * 6));
755 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 7 * 6));
756 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 8 * 6));
757 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 9 * 6));
758 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 10 * 6));
759 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 11 * 6));
760 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 12 * 6));
763 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
764 PrintAndLog(" * - all sectors");
765 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
766 // PrintAndLog("d - write keys to binary file\n");
768 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
769 PrintAndLog(" hf mf chk *1 ? t");
773 if (param_getchar(Cmd
, 0)=='*') {
775 switch(param_getchar(Cmd
+1, 0)) {
776 case '0': SectorsCnt
= 5; break;
777 case '1': SectorsCnt
= 16; break;
778 case '2': SectorsCnt
= 32; break;
779 case '4': SectorsCnt
= 40; break;
780 default: SectorsCnt
= 16;
784 blockNo
= param_get8(Cmd
, 0);
786 ctmp
= param_getchar(Cmd
, 1);
798 PrintAndLog("Key type must be A , B or ?");
802 ctmp
= param_getchar(Cmd
, 2);
803 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
804 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
806 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
807 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
808 if ( stKeyBlock
- keycnt
< 2) {
809 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
811 PrintAndLog("Cannot allocate memory for Keys");
817 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
818 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
819 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
823 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
824 PrintAndLog("File name too long");
829 if ( (f
= fopen( filename
, "r")) ) {
831 memset(buf
, 0, sizeof(buf
));
832 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
833 PrintAndLog("File reading error.");
837 if (strlen(buf
) < 12 || buf
[11] == '\n')
840 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
842 if( buf
[0]=='#' ) continue; //The line start with # is remcommnet,skip
844 if (!isxdigit(buf
[0])){
845 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
851 if ( stKeyBlock
- keycnt
< 2) {
852 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
854 PrintAndLog("Cannot allocate memory for defKeys");
860 memset(keyBlock
+ 6 * keycnt
, 0, 6);
861 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
862 PrintAndLog("chk custom key[%d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
866 PrintAndLog("File: %s: not found or locked.", filename
);
875 PrintAndLog("No key specified,try default keys");
876 for (;keycnt
<=12; keycnt
++)
877 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
878 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
879 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
882 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
884 for (int i
=0; i
<SectorsCnt
; ++i
) {
885 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
886 int size
= keycnt
>8?8:keycnt
;
887 for (int c
= 0; c
< keycnt
; c
+=size
) {
888 size
=keycnt
-c
>8?8:keycnt
-c
;
889 res
= mfCheckKeys(b
, t
, size
, keyBlock
+6*c
, &key64
);
892 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
895 mfEmlGetMem(block
, get_trailer_block(b
), 1);
896 num_to_bytes(key64
, 6, block
+ t
*10);
897 mfEmlSetMem(block
, get_trailer_block(b
), 1);
902 printf("Not found yet, keycnt:%d\r", c
+size
);
906 PrintAndLog("Command execute timeout");
909 b
<127?(b
+=4):(b
+=16);
917 if (createDumpFile) {
918 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
919 PrintAndLog("Could not create file dumpkeys.bin");
923 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
924 for(i=0; i<16; i++) {
925 if (e_sector[i].foundKey[0]){
926 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
927 fwrite ( tempkey, 1, 6, fkeys );
930 fwrite ( &standart, 1, 6, fkeys );
933 for(i=0; i<16; i++) {
934 if (e_sector[i].foundKey[1]){
935 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
936 fwrite ( tempkey, 1, 6, fkeys );
939 fwrite ( &standart, 1, 6, fkeys );
948 int CmdHF14AMf1kSim(const char *Cmd
)
950 uint8_t uid
[4] = {0, 0, 0, 0};
952 if (param_getchar(Cmd
, 0) == 'h') {
953 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
954 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
958 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
959 PrintAndLog("UID must include 8 HEX symbols");
962 PrintAndLog(" uid:%s ", sprint_hex(uid
, 4));
964 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {0, 0, 0}};
965 memcpy(c
.d
.asBytes
, uid
, 4);
971 int CmdHF14AMfDbg(const char *Cmd
)
973 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
975 PrintAndLog("Max debud mode parameter is 4 \n");
978 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
979 PrintAndLog("Usage: hf mf dbg <debug level>");
980 PrintAndLog(" 0 - no debug messages");
981 PrintAndLog(" 1 - error messages");
982 PrintAndLog(" 2 - all messages");
983 PrintAndLog(" 4 - extended debug mode");
987 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
993 int CmdHF14AMfEGet(const char *Cmd
)
996 uint8_t data
[3 * 16];
999 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1000 PrintAndLog("Usage: hf mf eget <block number>");
1001 PrintAndLog(" sample: hf mf eget 0 ");
1005 blockNo
= param_get8(Cmd
, 0);
1008 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1009 for (i
= 0; i
< 3; i
++) {
1010 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1013 PrintAndLog("Command execute timeout");
1019 int CmdHF14AMfEClear(const char *Cmd
)
1021 if (param_getchar(Cmd
, 0) == 'h') {
1022 PrintAndLog("Usage: hf mf eclr");
1023 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1027 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1032 int CmdHF14AMfESet(const char *Cmd
)
1034 uint8_t memBlock
[16];
1035 uint8_t blockNo
= 0;
1037 memset(memBlock
, 0x00, sizeof(memBlock
));
1039 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1040 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1041 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1045 blockNo
= param_get8(Cmd
, 0);
1047 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1048 PrintAndLog("block data must include 32 HEX symbols");
1053 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1054 memcpy(c
.d
.asBytes
, memBlock
, 16);
1059 int CmdHF14AMfELoad(const char *Cmd
)
1063 char * fnameptr
= filename
;
1066 int i
, len
, blockNum
;
1068 memset(filename
, 0, sizeof(filename
));
1069 memset(buf
, 0, sizeof(buf
));
1071 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1072 PrintAndLog("It loads emul dump from the file `filename.eml`");
1073 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1074 PrintAndLog(" sample: hf mf eload filename");
1079 if (len
> 14) len
= 14;
1081 memcpy(filename
, Cmd
, len
);
1084 sprintf(fnameptr
, ".eml");
1087 f
= fopen(filename
, "r");
1089 PrintAndLog("File not found or locked.");
1095 memset(buf
, 0, sizeof(buf
));
1096 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1097 PrintAndLog("File reading error.");
1101 if (strlen(buf
) < 32){
1102 if(strlen(buf
) && feof(f
))
1104 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1107 for (i
= 0; i
< 32; i
+= 2)
1108 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1109 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1111 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1112 PrintAndLog("Cant set emul block: %d", blockNum
);
1117 if (blockNum
>= 32 * 4 + 8 * 16) break;
1121 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1122 PrintAndLog("File content error. There must be 64 blocks");
1125 PrintAndLog("Loaded from file: %s", filename
);
1129 int CmdHF14AMfESave(const char *Cmd
)
1133 char * fnameptr
= filename
;
1137 memset(filename
, 0, sizeof(filename
));
1138 memset(buf
, 0, sizeof(buf
));
1140 if (param_getchar(Cmd
, 0) == 'h') {
1141 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1142 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1143 PrintAndLog(" sample: hf mf esave ");
1144 PrintAndLog(" hf mf esave filename");
1149 if (len
> 14) len
= 14;
1153 if (mfEmlGetMem(buf
, 0, 1)) {
1154 PrintAndLog("Cant get block: %d", 0);
1157 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1158 sprintf(fnameptr
, "%02x", buf
[j
]);
1160 memcpy(filename
, Cmd
, len
);
1164 sprintf(fnameptr
, ".eml");
1167 f
= fopen(filename
, "w+");
1170 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1171 if (mfEmlGetMem(buf
, i
, 1)) {
1172 PrintAndLog("Cant get block: %d", i
);
1175 for (j
= 0; j
< 16; j
++)
1176 fprintf(f
, "%02x", buf
[j
]);
1181 PrintAndLog("Saved to file: %s", filename
);
1186 int CmdHF14AMfECFill(const char *Cmd
)
1188 uint8_t keyType
= 0;
1190 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1191 PrintAndLog("Usage: hf mf efill <key A/B>");
1192 PrintAndLog("sample: hf mf efill A");
1193 PrintAndLog("Card data blocks transfers to card emulator memory.");
1194 PrintAndLog("Keys must be laid in the simulator memory. \n");
1198 char ctmp
= param_getchar(Cmd
, 0);
1200 PrintAndLog("Key type must be A or B");
1203 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1205 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1210 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1214 uint64_t keyA
, keyB
;
1216 PrintAndLog("|---|----------------|----------------|");
1217 PrintAndLog("|sec|key A |key B |");
1218 PrintAndLog("|---|----------------|----------------|");
1219 for (i
= 0; i
< 40; i
++) {
1220 b
<127?(b
+=4):(b
+=16);
1221 if (mfEmlGetMem(data
, b
, 1)) {
1222 PrintAndLog("error get block %d", b
);
1225 keyA
= bytes_to_num(data
, 6);
1226 keyB
= bytes_to_num(data
+ 10, 6);
1227 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1229 PrintAndLog("|---|----------------|----------------|");
1234 int CmdHF14AMfCSetUID(const char *Cmd
)
1236 uint8_t wipeCard
= 0;
1241 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1242 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1243 PrintAndLog("sample: hf mf csetuid 01020304 w");
1244 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1245 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1249 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1250 PrintAndLog("UID must include 8 HEX symbols");
1254 char ctmp
= param_getchar(Cmd
, 1);
1255 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1257 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1259 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1261 PrintAndLog("Can't set UID. error=%d", res
);
1265 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1269 int CmdHF14AMfCSetBlk(const char *Cmd
)
1272 uint8_t memBlock
[16];
1273 uint8_t blockNo
= 0;
1275 memset(memBlock
, 0x00, sizeof(memBlock
));
1277 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1278 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1279 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1280 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1281 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1285 blockNo
= param_get8(Cmd
, 0);
1287 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1288 PrintAndLog("block data must include 32 HEX symbols");
1292 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1294 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1296 PrintAndLog("Can't write block. error=%d", res
);
1300 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1304 int CmdHF14AMfCLoad(const char *Cmd
)
1308 char * fnameptr
= filename
;
1311 uint8_t fillFromEmulator
= 0;
1312 int i
, len
, blockNum
, flags
;
1314 memset(filename
, 0, sizeof(filename
));
1315 memset(buf
, 0, sizeof(buf
));
1317 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1318 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1319 PrintAndLog("or from emulator memory (option `e`)");
1320 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1321 PrintAndLog(" or: hf mf cload e ");
1322 PrintAndLog(" sample: hf mf cload filename");
1326 char ctmp
= param_getchar(Cmd
, 0);
1327 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1329 if (fillFromEmulator
) {
1330 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1331 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1332 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1333 PrintAndLog("Cant get block: %d", blockNum
);
1337 if (blockNum
== 2) flags
= 0;
1338 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1340 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1341 PrintAndLog("Cant set magic card block: %d", blockNum
);
1348 if (len
> 14) len
= 14;
1350 memcpy(filename
, Cmd
, len
);
1353 sprintf(fnameptr
, ".eml");
1356 f
= fopen(filename
, "r");
1358 PrintAndLog("File not found or locked.");
1363 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1365 memset(buf
, 0, sizeof(buf
));
1366 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1367 PrintAndLog("File reading error.");
1371 if (strlen(buf
) < 32){
1372 if(strlen(buf
) && feof(f
))
1374 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1377 for (i
= 0; i
< 32; i
+= 2)
1378 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1380 if (blockNum
== 2) flags
= 0;
1381 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1383 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1384 PrintAndLog("Cant set magic card block: %d", blockNum
);
1389 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1393 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1394 PrintAndLog("File content error. There must be 64 blocks");
1397 PrintAndLog("Loaded from file: %s", filename
);
1402 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1403 uint8_t memBlock
[16];
1404 uint8_t blockNo
= 0;
1406 memset(memBlock
, 0x00, sizeof(memBlock
));
1408 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1409 PrintAndLog("Usage: hf mf cgetblk <block number>");
1410 PrintAndLog("sample: hf mf cgetblk 1");
1411 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1415 blockNo
= param_get8(Cmd
, 0);
1417 PrintAndLog("--block number:%02x ", blockNo
);
1419 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1421 PrintAndLog("Can't read block. error=%d", res
);
1425 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1429 int CmdHF14AMfCGetSc(const char *Cmd
) {
1430 uint8_t memBlock
[16];
1431 uint8_t sectorNo
= 0;
1433 memset(memBlock
, 0x00, sizeof(memBlock
));
1435 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1436 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1437 PrintAndLog("sample: hf mf cgetsc 0");
1438 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1442 sectorNo
= param_get8(Cmd
, 0);
1443 if (sectorNo
> 15) {
1444 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1448 PrintAndLog("--sector number:%02x ", sectorNo
);
1450 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1451 for (i
= 0; i
< 4; i
++) {
1452 if (i
== 1) flags
= 0;
1453 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1455 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1457 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1461 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1466 int CmdHF14AMfCSave(const char *Cmd
) {
1470 char * fnameptr
= filename
;
1471 uint8_t fillFromEmulator
= 0;
1473 int i
, j
, len
, flags
;
1475 memset(filename
, 0, sizeof(filename
));
1476 memset(buf
, 0, sizeof(buf
));
1478 if (param_getchar(Cmd
, 0) == 'h') {
1479 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1480 PrintAndLog("or into emulator memory (option `e`)");
1481 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1482 PrintAndLog(" sample: hf mf esave ");
1483 PrintAndLog(" hf mf esave filename");
1484 PrintAndLog(" hf mf esave e \n");
1488 char ctmp
= param_getchar(Cmd
, 0);
1489 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1491 if (fillFromEmulator
) {
1492 // put into emulator
1493 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1494 for (i
= 0; i
< 16 * 4; i
++) {
1495 if (i
== 1) flags
= 0;
1496 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1498 if (mfCGetBlock(i
, buf
, flags
)) {
1499 PrintAndLog("Cant get block: %d", i
);
1503 if (mfEmlSetMem(buf
, i
, 1)) {
1504 PrintAndLog("Cant set emul block: %d", i
);
1511 if (len
> 14) len
= 14;
1515 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1516 PrintAndLog("Cant get block: %d", 0);
1519 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1520 sprintf(fnameptr
, "%02x", buf
[j
]);
1522 memcpy(filename
, Cmd
, len
);
1526 sprintf(fnameptr
, ".eml");
1529 f
= fopen(filename
, "w+");
1532 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1533 for (i
= 0; i
< 16 * 4; i
++) {
1534 if (i
== 1) flags
= 0;
1535 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1537 if (mfCGetBlock(i
, buf
, flags
)) {
1538 PrintAndLog("Cant get block: %d", i
);
1541 for (j
= 0; j
< 16; j
++)
1542 fprintf(f
, "%02x", buf
[j
]);
1547 PrintAndLog("Saved to file: %s", filename
);
1553 int CmdHF14AMfSniff(const char *Cmd
){
1555 bool wantLogToFile
= 0;
1556 bool wantDecrypt
= 0;
1557 //bool wantSaveToEml = 0; TODO
1558 bool wantSaveToEmlFile
= 0;
1572 uint8_t * bufPtr
= buf
;
1573 memset(buf
, 0x00, 3000);
1575 if (param_getchar(Cmd
, 0) == 'h') {
1576 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1577 PrintAndLog("You can specify:");
1578 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1579 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1580 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1581 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1582 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1583 PrintAndLog(" sample: hf mf sniff l d e");
1587 for (int i
= 0; i
< 4; i
++) {
1588 char ctmp
= param_getchar(Cmd
, i
);
1589 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1590 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1591 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1592 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1595 printf("-------------------------------------------------------------------------\n");
1596 printf("Executing command. \n");
1597 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1598 printf("Press the key on pc keyboard to abort the client.\n");
1599 printf("-------------------------------------------------------------------------\n");
1601 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1610 printf("\naborted via keyboard!\n");
1615 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1616 res
= resp
.arg
[0] & 0xff;
1620 if (res
== 0) return 0;
1624 memset(buf
, 0x00, 3000);
1626 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1631 blockLen
= bufPtr
- buf
;
1634 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1636 while (bufPtr
- buf
+ 9 < blockLen
) {
1637 isTag
= bufPtr
[3] & 0x80 ? true:false;
1639 parity
= *((uint32_t *)(bufPtr
));
1643 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1644 memcpy(uid
, bufPtr
+ 2, 7);
1645 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1648 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1649 if (wantLogToFile
) {
1650 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1651 AddLogCurrentDT(logHexFileName
);
1653 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1655 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1656 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1657 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1668 static command_t CommandTable
[] =
1670 {"help", CmdHelp
, 1, "This help"},
1671 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1672 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1673 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1674 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1675 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1676 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1677 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1678 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages. param - <used card nonce>"},
1679 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1680 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1681 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1682 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1683 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1684 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1685 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1686 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1687 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1688 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1689 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1690 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1691 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1692 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1693 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1694 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1695 {NULL
, NULL
, 0, NULL
}
1698 int CmdHFMF(const char *Cmd
)
1701 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1703 CmdsParse(CommandTable
, Cmd
);
1707 int CmdHelp(const char *Cmd
)
1709 CmdsHelp(CommandTable
);