]>
cvs.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:%012llx \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:%012llx", 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 fread ( keyA
[i
], 1, 6, fin
);
292 for (i
=0 ; i
<16 ; i
++) {
293 fread ( keyB
[i
], 1, 6, fin
);
296 // Read access rights to sectors
298 PrintAndLog("|-----------------------------------------|");
299 PrintAndLog("|------ Reading sector access bits...-----|");
300 PrintAndLog("|-----------------------------------------|");
302 for (i
= 0 ; i
< 16 ; i
++) {
303 UsbCommand c
= {CMD_MIFARE_READBL
, {4*i
+ 3, 0, 0}};
304 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
307 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
308 uint8_t isOK
= resp
.arg
[0] & 0xff;
309 uint8_t *data
= resp
.d
.asBytes
;
311 rights
[i
][0] = ((data
[7] & 0x10)>>4) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>2);
312 rights
[i
][1] = ((data
[7] & 0x20)>>5) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>3);
313 rights
[i
][2] = ((data
[7] & 0x40)>>6) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>4);
314 rights
[i
][3] = ((data
[7] & 0x80)>>7) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>5);
317 PrintAndLog("Could not get access rights for block %d", i
);
321 PrintAndLog("Command execute timeout");
325 // Read blocks and print to file
327 PrintAndLog("|-----------------------------------------|");
328 PrintAndLog("|----- Dumping all blocks to file... -----|");
329 PrintAndLog("|-----------------------------------------|");
332 for (i
=0 ; i
<16 ; i
++) {
333 for (j
=0 ; j
<4 ; j
++) {
334 bool received
= false;
337 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4 + j
, 0, 0}};
338 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
340 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
343 if ((rights
[i
][j
] == 6) | (rights
[i
][j
] == 5)) {
344 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 1, 0}};
345 memcpy(c
.d
.asBytes
, keyB
[i
], 6);
347 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
349 else if (rights
[i
][j
] == 7) {
350 PrintAndLog("Access rights do not allow reading of sector %d block %d",i
,j
);
353 UsbCommand c
= {CMD_MIFARE_READBL
, {i
*4+j
, 0, 0}};
354 memcpy(c
.d
.asBytes
, keyA
[i
], 6);
356 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
361 uint8_t isOK
= resp
.arg
[0] & 0xff;
362 uint8_t *data
= resp
.d
.asBytes
;
364 data
[0] = (keyA
[i
][0]);
365 data
[1] = (keyA
[i
][1]);
366 data
[2] = (keyA
[i
][2]);
367 data
[3] = (keyA
[i
][3]);
368 data
[4] = (keyA
[i
][4]);
369 data
[5] = (keyA
[i
][5]);
370 data
[10] = (keyB
[i
][0]);
371 data
[11] = (keyB
[i
][1]);
372 data
[12] = (keyB
[i
][2]);
373 data
[13] = (keyB
[i
][3]);
374 data
[14] = (keyB
[i
][4]);
375 data
[15] = (keyB
[i
][5]);
378 fwrite ( data
, 1, 16, fout
);
381 PrintAndLog("Could not get access rights for block %d", i
);
385 PrintAndLog("Command execute timeout");
396 int CmdHF14AMfRestore(const char *Cmd
)
401 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
402 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
409 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
410 PrintAndLog("Could not find file dumpdata.bin");
413 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
414 PrintAndLog("Could not find file dumpkeys.bin");
418 for (i
=0 ; i
<16 ; i
++) {
419 fread(keyA
[i
], 1, 6, fkeys
);
421 for (i
=0 ; i
<16 ; i
++) {
422 fread(keyB
[i
], 1, 6, fkeys
);
425 PrintAndLog("Restoring dumpdata.bin to card");
427 for (i
=0 ; i
<16 ; i
++) {
428 for( j
=0 ; j
<4 ; j
++) {
429 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
430 memcpy(c
.d
.asBytes
, key
, 6);
432 fread(bldata
, 1, 16, fdump
);
435 bldata
[0] = (keyA
[i
][0]);
436 bldata
[1] = (keyA
[i
][1]);
437 bldata
[2] = (keyA
[i
][2]);
438 bldata
[3] = (keyA
[i
][3]);
439 bldata
[4] = (keyA
[i
][4]);
440 bldata
[5] = (keyA
[i
][5]);
441 bldata
[10] = (keyB
[i
][0]);
442 bldata
[11] = (keyB
[i
][1]);
443 bldata
[12] = (keyB
[i
][2]);
444 bldata
[13] = (keyB
[i
][3]);
445 bldata
[14] = (keyB
[i
][4]);
446 bldata
[15] = (keyB
[i
][5]);
449 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
452 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
455 if ((ch != 'y') && (ch != 'Y')){
456 PrintAndLog("Aborting !");
461 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
465 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
466 uint8_t isOK
= resp
.arg
[0] & 0xff;
467 PrintAndLog("isOk:%02x", isOK
);
469 PrintAndLog("Command execute timeout");
479 int CmdHF14AMfNested(const char *Cmd
)
481 int i
, j
, res
, iterations
;
482 sector
* e_sector
= NULL
;
485 uint8_t trgBlockNo
= 0;
486 uint8_t trgKeyType
= 0;
489 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
490 uint8_t keyBlock
[16 * 6];
492 int transferToEml
= 0;
494 int createDumpFile
= 0;
496 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
497 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
502 PrintAndLog("Usage:");
503 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
504 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
505 PrintAndLog(" <target block number> <target key A/B> [t]");
506 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
507 PrintAndLog("t - transfer keys into emulator memory");
508 PrintAndLog("d - write keys to binary file");
510 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
511 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
512 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
513 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
517 cmdp
= param_getchar(Cmd
, 0);
518 blockNo
= param_get8(Cmd
, 1);
519 ctmp
= param_getchar(Cmd
, 2);
521 PrintAndLog("Key type must be A or B");
524 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
525 if (param_gethex(Cmd
, 3, key
, 12)) {
526 PrintAndLog("Key must include 12 HEX symbols");
530 if (cmdp
== 'o' || cmdp
== 'O') {
532 trgBlockNo
= param_get8(Cmd
, 4);
533 ctmp
= param_getchar(Cmd
, 5);
535 PrintAndLog("Target key type must be A or B");
538 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
541 case '0': SectorsCnt
= 05; break;
542 case '1': SectorsCnt
= 16; break;
543 case '2': SectorsCnt
= 32; break;
544 case '4': SectorsCnt
= 64; break;
545 default: SectorsCnt
= 16;
549 ctmp
= param_getchar(Cmd
, 4);
550 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
551 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
553 ctmp
= param_getchar(Cmd
, 6);
554 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
555 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
557 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
559 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
562 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) {
563 PrintAndLog("Nested error.");
567 for (i
= 0; i
< 16; i
++) {
568 PrintAndLog("count=%d key= %s", i
, sprint_hex(keyBlock
+ i
* 6, 6));
572 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
574 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
576 PrintAndLog("Found valid key:%012llx", key64
);
578 // transfer key to the emulator
580 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
583 num_to_bytes(key64
, 6, keyBlock
);
585 num_to_bytes(key64
, 6, &keyBlock
[10]);
586 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
589 PrintAndLog("No valid key found");
592 else { // ------------------------------------ multiple sectors working
593 blDiff
= blockNo
% 4;
594 PrintAndLog("Block shift=%d", blDiff
);
595 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
596 if (e_sector
== NULL
) return 1;
598 //test current key 4 sectors
599 memcpy(keyBlock
, key
, 6);
600 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 1 * 6));
601 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 2 * 6));
602 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 3 * 6));
603 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 4 * 6));
604 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
606 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
607 for (i
= 0; i
< SectorsCnt
; i
++) {
608 for (j
= 0; j
< 2; j
++) {
609 if (e_sector
[i
].foundKey
[j
]) continue;
611 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
614 e_sector
[i
].Key
[j
] = key64
;
615 e_sector
[i
].foundKey
[j
] = 1;
622 PrintAndLog("nested...");
623 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
624 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4)
625 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
626 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
627 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
)) continue;
631 //try keys from nested
632 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, keyBlock
, &key64
);
634 res
= mfCheckKeys(trgBlockNo
, trgKeyType
, 8, &keyBlock
[6 * 8], &key64
);
636 PrintAndLog("Found valid key:%012llx", key64
);
637 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
638 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
643 PrintAndLog("Iterations count: %d", iterations
);
645 PrintAndLog("|---|----------------|---|----------------|---|");
646 PrintAndLog("|sec|key A |res|key B |res|");
647 PrintAndLog("|---|----------------|---|----------------|---|");
648 for (i
= 0; i
< SectorsCnt
; i
++) {
649 PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i
,
650 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
652 PrintAndLog("|---|----------------|---|----------------|---|");
654 // transfer them to the emulator
656 for (i
= 0; i
< SectorsCnt
; i
++) {
657 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
658 if (e_sector
[i
].foundKey
[0])
659 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
660 if (e_sector
[i
].foundKey
[1])
661 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
662 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
667 if (createDumpFile
) {
668 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
669 PrintAndLog("Could not create file dumpkeys.bin");
673 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
674 for(i
=0; i
<16; i
++) {
675 if (e_sector
[i
].foundKey
[0]){
676 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
677 fwrite ( tempkey
, 1, 6, fkeys
);
680 fwrite ( &standart
, 1, 6, fkeys
);
683 for(i
=0; i
<16; i
++) {
684 if (e_sector
[i
].foundKey
[1]){
685 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
686 fwrite ( tempkey
, 1, 6, fkeys
);
689 fwrite ( &standart
, 1, 6, fkeys
);
702 get_trailer_block (uint32_t uiBlock
)
704 // Test if we are in the small or big sectors
705 uint32_t trailer_block
= 0;
707 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
709 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
711 return trailer_block
;
713 int CmdHF14AMfChk(const char *Cmd
)
716 char filename
[256]={0};
718 uint8_t *keyBlock
= NULL
, *p
;
719 uint8_t stKeyBlock
= 20;
725 uint8_t SectorsCnt
= 1;
729 int transferToEml
= 0;
730 int createDumpFile
= 0;
732 keyBlock
= calloc(stKeyBlock
, 6);
733 if (keyBlock
== NULL
) return 1;
735 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 0 * 6)); // Default key (first key used by program if no user defined key)
736 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 1 * 6)); // Blank key
737 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 2 * 6)); // NFCForum MAD key
738 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
739 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 4 * 6));
740 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 5 * 6));
741 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 6 * 6));
742 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 7 * 6));
743 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 8 * 6));
744 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 9 * 6));
745 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 10 * 6));
746 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 11 * 6));
747 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 12 * 6));
750 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
751 PrintAndLog(" * - all sectors");
752 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
753 // PrintAndLog("d - write keys to binary file\n");
755 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
756 PrintAndLog(" hf mf chk *1 ? t");
760 if (param_getchar(Cmd
, 0)=='*') {
762 switch(param_getchar(Cmd
+1, 0)) {
763 case '0': SectorsCnt
= 5; break;
764 case '1': SectorsCnt
= 16; break;
765 case '2': SectorsCnt
= 32; break;
766 case '4': SectorsCnt
= 40; break;
767 default: SectorsCnt
= 16;
771 blockNo
= param_get8(Cmd
, 0);
773 ctmp
= param_getchar(Cmd
, 1);
785 PrintAndLog("Key type must be A , B or ?");
789 ctmp
= param_getchar(Cmd
, 2);
790 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
791 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
793 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
794 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
795 if ( stKeyBlock
- keycnt
< 2) {
796 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
798 PrintAndLog("Cannot allocate memory for Keys");
804 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
805 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
806 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
810 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
811 PrintAndLog("File name too long");
816 if ( (f
= fopen( filename
, "r")) ) {
818 memset(buf
, 0, sizeof(buf
));
819 fgets(buf
, sizeof(buf
), f
);
821 if (strlen(buf
) < 12 || buf
[11] == '\n')
824 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
826 if( buf
[0]=='#' ) continue; //The line start with # is remcommnet,skip
828 if (!isxdigit(buf
[0])){
829 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
835 if ( stKeyBlock
- keycnt
< 2) {
836 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
838 PrintAndLog("Cannot allocate memory for defKeys");
844 memset(keyBlock
+ 6 * keycnt
, 0, 6);
845 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
846 PrintAndLog("chk custom key[%d] %012llx", keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
850 PrintAndLog("File: %s: not found or locked.", filename
);
859 PrintAndLog("No key specified,try default keys");
860 for (;keycnt
<=12; keycnt
++)
861 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
862 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
863 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
866 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
868 for (int i
=0; i
<SectorsCnt
; ++i
) {
869 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
870 int size
= keycnt
>8?8:keycnt
;
871 for (int c
= 0; c
< keycnt
; c
+=size
) {
872 size
=keycnt
-c
>8?8:keycnt
-c
;
873 res
= mfCheckKeys(b
, t
, size
, keyBlock
+6*c
, &key64
);
876 PrintAndLog("Found valid key:[%012llx]",key64
);
879 mfEmlGetMem(block
, get_trailer_block(b
), 1);
880 num_to_bytes(key64
, 6, block
+ t
*10);
881 mfEmlSetMem(block
, get_trailer_block(b
), 1);
886 printf("Not found yet, keycnt:%d\r", c
+size
);
890 PrintAndLog("Command execute timeout");
893 b
<127?(b
+=4):(b
+=16);
901 if (createDumpFile) {
902 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
903 PrintAndLog("Could not create file dumpkeys.bin");
907 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
908 for(i=0; i<16; i++) {
909 if (e_sector[i].foundKey[0]){
910 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
911 fwrite ( tempkey, 1, 6, fkeys );
914 fwrite ( &standart, 1, 6, fkeys );
917 for(i=0; i<16; i++) {
918 if (e_sector[i].foundKey[1]){
919 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
920 fwrite ( tempkey, 1, 6, fkeys );
923 fwrite ( &standart, 1, 6, fkeys );
932 int CmdHF14AMf1kSim(const char *Cmd
)
934 uint8_t uid
[4] = {0, 0, 0, 0};
936 if (param_getchar(Cmd
, 0) == 'h') {
937 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
938 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
942 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
943 PrintAndLog("UID must include 8 HEX symbols");
946 PrintAndLog(" uid:%s ", sprint_hex(uid
, 4));
948 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {0, 0, 0}};
949 memcpy(c
.d
.asBytes
, uid
, 4);
955 int CmdHF14AMfDbg(const char *Cmd
)
957 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
959 PrintAndLog("Max debud mode parameter is 4 \n");
962 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
963 PrintAndLog("Usage: hf mf dbg <debug level>");
964 PrintAndLog(" 0 - no debug messages");
965 PrintAndLog(" 1 - error messages");
966 PrintAndLog(" 2 - all messages");
967 PrintAndLog(" 4 - extended debug mode");
971 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
977 int CmdHF14AMfEGet(const char *Cmd
)
980 uint8_t data
[3 * 16];
983 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
984 PrintAndLog("Usage: hf mf eget <block number>");
985 PrintAndLog(" sample: hf mf eget 0 ");
989 blockNo
= param_get8(Cmd
, 0);
992 if (!mfEmlGetMem(data
, blockNo
, 3)) {
993 for (i
= 0; i
< 3; i
++) {
994 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
997 PrintAndLog("Command execute timeout");
1003 int CmdHF14AMfEClear(const char *Cmd
)
1005 if (param_getchar(Cmd
, 0) == 'h') {
1006 PrintAndLog("Usage: hf mf eclr");
1007 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1011 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1016 int CmdHF14AMfESet(const char *Cmd
)
1018 uint8_t memBlock
[16];
1019 uint8_t blockNo
= 0;
1021 memset(memBlock
, 0x00, sizeof(memBlock
));
1023 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1024 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1025 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1029 blockNo
= param_get8(Cmd
, 0);
1031 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1032 PrintAndLog("block data must include 32 HEX symbols");
1037 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1038 memcpy(c
.d
.asBytes
, memBlock
, 16);
1043 int CmdHF14AMfELoad(const char *Cmd
)
1047 char * fnameptr
= filename
;
1050 int i
, len
, blockNum
;
1052 memset(filename
, 0, sizeof(filename
));
1053 memset(buf
, 0, sizeof(buf
));
1055 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1056 PrintAndLog("It loads emul dump from the file `filename.eml`");
1057 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1058 PrintAndLog(" sample: hf mf eload filename");
1063 if (len
> 14) len
= 14;
1065 memcpy(filename
, Cmd
, len
);
1068 sprintf(fnameptr
, ".eml");
1071 f
= fopen(filename
, "r");
1073 PrintAndLog("File not found or locked.");
1079 memset(buf
, 0, sizeof(buf
));
1080 fgets(buf
, sizeof(buf
), f
);
1082 if (strlen(buf
) < 32){
1083 if(strlen(buf
) && feof(f
))
1085 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1088 for (i
= 0; i
< 32; i
+= 2)
1089 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1090 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1092 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1093 PrintAndLog("Cant set emul block: %d", blockNum
);
1098 if (blockNum
>= 32 * 4 + 8 * 16) break;
1102 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1103 PrintAndLog("File content error. There must be 64 blocks");
1106 PrintAndLog("Loaded from file: %s", filename
);
1110 int CmdHF14AMfESave(const char *Cmd
)
1114 char * fnameptr
= filename
;
1118 memset(filename
, 0, sizeof(filename
));
1119 memset(buf
, 0, sizeof(buf
));
1121 if (param_getchar(Cmd
, 0) == 'h') {
1122 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1123 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1124 PrintAndLog(" sample: hf mf esave ");
1125 PrintAndLog(" hf mf esave filename");
1130 if (len
> 14) len
= 14;
1134 if (mfEmlGetMem(buf
, 0, 1)) {
1135 PrintAndLog("Cant get block: %d", 0);
1138 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1139 sprintf(fnameptr
, "%02x", buf
[j
]);
1141 memcpy(filename
, Cmd
, len
);
1145 sprintf(fnameptr
, ".eml");
1148 f
= fopen(filename
, "w+");
1151 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1152 if (mfEmlGetMem(buf
, i
, 1)) {
1153 PrintAndLog("Cant get block: %d", i
);
1156 for (j
= 0; j
< 16; j
++)
1157 fprintf(f
, "%02x", buf
[j
]);
1162 PrintAndLog("Saved to file: %s", filename
);
1167 int CmdHF14AMfECFill(const char *Cmd
)
1169 uint8_t keyType
= 0;
1171 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1172 PrintAndLog("Usage: hf mf efill <key A/B>");
1173 PrintAndLog("sample: hf mf efill A");
1174 PrintAndLog("Card data blocks transfers to card emulator memory.");
1175 PrintAndLog("Keys must be laid in the simulator memory. \n");
1179 char ctmp
= param_getchar(Cmd
, 0);
1181 PrintAndLog("Key type must be A or B");
1184 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1186 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1191 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1195 uint64_t keyA
, keyB
;
1197 PrintAndLog("|---|----------------|----------------|");
1198 PrintAndLog("|sec|key A |key B |");
1199 PrintAndLog("|---|----------------|----------------|");
1200 for (i
= 0; i
< 40; i
++) {
1201 b
<127?(b
+=4):(b
+=16);
1202 if (mfEmlGetMem(data
, b
, 1)) {
1203 PrintAndLog("error get block %d", b
);
1206 keyA
= bytes_to_num(data
, 6);
1207 keyB
= bytes_to_num(data
+ 10, 6);
1208 PrintAndLog("|%03d| %012llx | %012llx |", i
, keyA
, keyB
);
1210 PrintAndLog("|---|----------------|----------------|");
1215 int CmdHF14AMfCSetUID(const char *Cmd
)
1217 uint8_t wipeCard
= 0;
1222 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1223 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1224 PrintAndLog("sample: hf mf csetuid 01020304 w");
1225 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1226 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1230 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1231 PrintAndLog("UID must include 8 HEX symbols");
1235 char ctmp
= param_getchar(Cmd
, 1);
1236 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1238 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1240 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1242 PrintAndLog("Can't set UID. error=%d", res
);
1246 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1250 int CmdHF14AMfCSetBlk(const char *Cmd
)
1253 uint8_t memBlock
[16];
1254 uint8_t blockNo
= 0;
1256 memset(memBlock
, 0x00, sizeof(memBlock
));
1258 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1259 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1260 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1261 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1262 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1266 blockNo
= param_get8(Cmd
, 0);
1268 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1269 PrintAndLog("block data must include 32 HEX symbols");
1273 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1275 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1277 PrintAndLog("Can't write block. error=%d", res
);
1281 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1285 int CmdHF14AMfCLoad(const char *Cmd
)
1289 char * fnameptr
= filename
;
1292 uint8_t fillFromEmulator
= 0;
1293 int i
, len
, blockNum
, flags
;
1295 memset(filename
, 0, sizeof(filename
));
1296 memset(buf
, 0, sizeof(buf
));
1298 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1299 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1300 PrintAndLog("or from emulator memory (option `e`)");
1301 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1302 PrintAndLog(" or: hf mf cload e ");
1303 PrintAndLog(" sample: hf mf cload filename");
1307 char ctmp
= param_getchar(Cmd
, 0);
1308 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1310 if (fillFromEmulator
) {
1311 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1312 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1313 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1314 PrintAndLog("Cant get block: %d", blockNum
);
1318 if (blockNum
== 2) flags
= 0;
1319 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1321 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1322 PrintAndLog("Cant set magic card block: %d", blockNum
);
1329 if (len
> 14) len
= 14;
1331 memcpy(filename
, Cmd
, len
);
1334 sprintf(fnameptr
, ".eml");
1337 f
= fopen(filename
, "r");
1339 PrintAndLog("File not found or locked.");
1344 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1346 memset(buf
, 0, sizeof(buf
));
1347 fgets(buf
, sizeof(buf
), f
);
1349 if (strlen(buf
) < 32){
1350 if(strlen(buf
) && feof(f
))
1352 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1355 for (i
= 0; i
< 32; i
+= 2)
1356 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1358 if (blockNum
== 2) flags
= 0;
1359 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1361 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1362 PrintAndLog("Cant set magic card block: %d", blockNum
);
1367 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1371 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1372 PrintAndLog("File content error. There must be 64 blocks");
1375 PrintAndLog("Loaded from file: %s", filename
);
1380 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1381 uint8_t memBlock
[16];
1382 uint8_t blockNo
= 0;
1384 memset(memBlock
, 0x00, sizeof(memBlock
));
1386 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1387 PrintAndLog("Usage: hf mf cgetblk <block number>");
1388 PrintAndLog("sample: hf mf cgetblk 1");
1389 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1393 blockNo
= param_get8(Cmd
, 0);
1395 PrintAndLog("--block number:%02x ", blockNo
);
1397 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1399 PrintAndLog("Can't read block. error=%d", res
);
1403 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1407 int CmdHF14AMfCGetSc(const char *Cmd
) {
1408 uint8_t memBlock
[16];
1409 uint8_t sectorNo
= 0;
1411 memset(memBlock
, 0x00, sizeof(memBlock
));
1413 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1414 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1415 PrintAndLog("sample: hf mf cgetsc 0");
1416 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1420 sectorNo
= param_get8(Cmd
, 0);
1421 if (sectorNo
> 15) {
1422 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1426 PrintAndLog("--sector number:%02x ", sectorNo
);
1428 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1429 for (i
= 0; i
< 4; i
++) {
1430 if (i
== 1) flags
= 0;
1431 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1433 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1435 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1439 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1444 int CmdHF14AMfCSave(const char *Cmd
) {
1448 char * fnameptr
= filename
;
1449 uint8_t fillFromEmulator
= 0;
1451 int i
, j
, len
, flags
;
1453 memset(filename
, 0, sizeof(filename
));
1454 memset(buf
, 0, sizeof(buf
));
1456 if (param_getchar(Cmd
, 0) == 'h') {
1457 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1458 PrintAndLog("or into emulator memory (option `e`)");
1459 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1460 PrintAndLog(" sample: hf mf esave ");
1461 PrintAndLog(" hf mf esave filename");
1462 PrintAndLog(" hf mf esave e \n");
1466 char ctmp
= param_getchar(Cmd
, 0);
1467 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1469 if (fillFromEmulator
) {
1470 // put into emulator
1471 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1472 for (i
= 0; i
< 16 * 4; i
++) {
1473 if (i
== 1) flags
= 0;
1474 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1476 if (mfCGetBlock(i
, buf
, flags
)) {
1477 PrintAndLog("Cant get block: %d", i
);
1481 if (mfEmlSetMem(buf
, i
, 1)) {
1482 PrintAndLog("Cant set emul block: %d", i
);
1489 if (len
> 14) len
= 14;
1493 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1494 PrintAndLog("Cant get block: %d", 0);
1497 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1498 sprintf(fnameptr
, "%02x", buf
[j
]);
1500 memcpy(filename
, Cmd
, len
);
1504 sprintf(fnameptr
, ".eml");
1507 f
= fopen(filename
, "w+");
1510 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1511 for (i
= 0; i
< 16 * 4; i
++) {
1512 if (i
== 1) flags
= 0;
1513 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1515 if (mfCGetBlock(i
, buf
, flags
)) {
1516 PrintAndLog("Cant get block: %d", i
);
1519 for (j
= 0; j
< 16; j
++)
1520 fprintf(f
, "%02x", buf
[j
]);
1525 PrintAndLog("Saved to file: %s", filename
);
1531 int CmdHF14AMfSniff(const char *Cmd
){
1533 bool wantLogToFile
= 0;
1534 bool wantDecrypt
= 0;
1535 //bool wantSaveToEml = 0; TODO
1536 bool wantSaveToEmlFile
= 0;
1550 uint8_t * bufPtr
= buf
;
1551 memset(buf
, 0x00, 3000);
1553 if (param_getchar(Cmd
, 0) == 'h') {
1554 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1555 PrintAndLog("You can specify:");
1556 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1557 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1558 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1559 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1560 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1561 PrintAndLog(" sample: hf mf sniff l d e");
1565 for (int i
= 0; i
< 4; i
++) {
1566 char ctmp
= param_getchar(Cmd
, i
);
1567 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1568 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1569 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1570 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1573 printf("-------------------------------------------------------------------------\n");
1574 printf("Executing command. \n");
1575 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1576 printf("Press the key on pc keyboard to abort the client.\n");
1577 printf("-------------------------------------------------------------------------\n");
1579 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1588 printf("\naborted via keyboard!\n");
1593 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1594 res
= resp
.arg
[0] & 0xff;
1598 if (res
== 0) return 0;
1602 memset(buf
, 0x00, 3000);
1604 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1609 blockLen
= bufPtr
- buf
;
1612 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1614 while (bufPtr
- buf
+ 9 < blockLen
) {
1615 isTag
= bufPtr
[3] & 0x80 ? true:false;
1617 parity
= *((uint32_t *)(bufPtr
));
1621 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1622 memcpy(uid
, bufPtr
+ 2, 7);
1623 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1626 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1627 if (wantLogToFile
) {
1628 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1629 AddLogCurrentDT(logHexFileName
);
1631 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1633 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1634 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1635 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1646 static command_t CommandTable
[] =
1648 {"help", CmdHelp
, 1, "This help"},
1649 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1650 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1651 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1652 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1653 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1654 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1655 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1656 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages. param - <used card nonce>"},
1657 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1658 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1659 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1660 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1661 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1662 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1663 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1664 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1665 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1666 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1667 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1668 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1669 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1670 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1671 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1672 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1673 {NULL
, NULL
, 0, NULL
}
1676 int CmdHFMF(const char *Cmd
)
1679 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1681 CmdsParse(CommandTable
, Cmd
);
1685 int CmdHelp(const char *Cmd
)
1687 CmdsHelp(CommandTable
);