]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
329cc1520aaecc944c1708aaa43124c21957d68a
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
);
383 PrintAndLog("Dumped card data into 'dumpdata.bin'");
387 PrintAndLog("Could not get access rights for block %d", i
);
391 PrintAndLog("Command execute timeout");
401 int CmdHF14AMfRestore(const char *Cmd
)
406 uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
407 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
414 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
415 PrintAndLog("Could not find file dumpdata.bin");
418 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
419 PrintAndLog("Could not find file dumpkeys.bin");
423 for (i
=0 ; i
<16 ; i
++) {
424 if (fread(keyA
[i
], 1, 6, fkeys
) == 0) {
425 PrintAndLog("File reading error.");
429 for (i
=0 ; i
<16 ; i
++) {
430 if (fread(keyB
[i
], 1, 6, fkeys
) == 0) {
431 PrintAndLog("File reading error.");
436 PrintAndLog("Restoring dumpdata.bin to card");
438 for (i
=0 ; i
<16 ; i
++) {
439 for( j
=0 ; j
<4 ; j
++) {
440 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {i
*4 + j
, keyType
, 0}};
441 memcpy(c
.d
.asBytes
, key
, 6);
443 if (fread(bldata
, 1, 16, fdump
) == 0) {
444 PrintAndLog("File reading error.");
449 bldata
[0] = (keyA
[i
][0]);
450 bldata
[1] = (keyA
[i
][1]);
451 bldata
[2] = (keyA
[i
][2]);
452 bldata
[3] = (keyA
[i
][3]);
453 bldata
[4] = (keyA
[i
][4]);
454 bldata
[5] = (keyA
[i
][5]);
455 bldata
[10] = (keyB
[i
][0]);
456 bldata
[11] = (keyB
[i
][1]);
457 bldata
[12] = (keyB
[i
][2]);
458 bldata
[13] = (keyB
[i
][3]);
459 bldata
[14] = (keyB
[i
][4]);
460 bldata
[15] = (keyB
[i
][5]);
463 PrintAndLog("Writing to block %2d: %s", i
*4+j
, sprint_hex(bldata
, 16));
466 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
469 if ((ch != 'y') && (ch != 'Y')){
470 PrintAndLog("Aborting !");
475 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
479 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
480 uint8_t isOK
= resp
.arg
[0] & 0xff;
481 PrintAndLog("isOk:%02x", isOK
);
483 PrintAndLog("Command execute timeout");
493 int CmdHF14AMfNested(const char *Cmd
)
495 int i
, j
, res
, iterations
;
496 sector
* e_sector
= NULL
;
499 uint8_t trgBlockNo
= 0;
500 uint8_t trgKeyType
= 0;
503 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
504 uint8_t keyBlock
[6*6];
506 int transferToEml
= 0;
508 int createDumpFile
= 0;
510 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
511 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
516 PrintAndLog("Usage:");
517 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
518 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
519 PrintAndLog(" <target block number> <target key A/B> [t]");
520 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
521 PrintAndLog("t - transfer keys into emulator memory");
522 PrintAndLog("d - write keys to binary file");
524 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
525 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
526 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
527 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
531 cmdp
= param_getchar(Cmd
, 0);
532 blockNo
= param_get8(Cmd
, 1);
533 ctmp
= param_getchar(Cmd
, 2);
535 PrintAndLog("Key type must be A or B");
538 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
539 if (param_gethex(Cmd
, 3, key
, 12)) {
540 PrintAndLog("Key must include 12 HEX symbols");
544 if (cmdp
== 'o' || cmdp
== 'O') {
546 trgBlockNo
= param_get8(Cmd
, 4);
547 ctmp
= param_getchar(Cmd
, 5);
549 PrintAndLog("Target key type must be A or B");
552 if (ctmp
!= 'A' && ctmp
!= 'a') trgKeyType
= 1;
555 case '0': SectorsCnt
= 05; break;
556 case '1': SectorsCnt
= 16; break;
557 case '2': SectorsCnt
= 32; break;
558 case '4': SectorsCnt
= 64; break;
559 default: SectorsCnt
= 16;
563 ctmp
= param_getchar(Cmd
, 4);
564 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
565 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
567 ctmp
= param_getchar(Cmd
, 6);
568 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
569 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
571 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo
, keyType
, sprint_hex(key
, 6), transferToEml
);
573 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo
, trgKeyType
);
576 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true)) {
577 PrintAndLog("Nested error.");
580 key64
= bytes_to_num(keyBlock
, 6);
582 PrintAndLog("Found valid key:%012"llx
, key64
);
584 // transfer key to the emulator
586 mfEmlGetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
589 num_to_bytes(key64
, 6, keyBlock
);
591 num_to_bytes(key64
, 6, &keyBlock
[10]);
592 mfEmlSetMem(keyBlock
, (trgBlockNo
/ 4) * 4 + 3, 1);
595 PrintAndLog("No valid key found");
598 else { // ------------------------------------ multiple sectors working
602 blDiff
= blockNo
% 4;
603 PrintAndLog("Block shift=%d", blDiff
);
604 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
605 if (e_sector
== NULL
) return 1;
607 //test current key 4 sectors
608 memcpy(keyBlock
, key
, 6);
609 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
610 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
611 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
612 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
613 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
615 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
616 for (i
= 0; i
< SectorsCnt
; i
++) {
617 for (j
= 0; j
< 2; j
++) {
618 if (e_sector
[i
].foundKey
[j
]) continue;
620 res
= mfCheckKeys(i
* 4 + blDiff
, j
, 6, keyBlock
, &key64
);
623 e_sector
[i
].Key
[j
] = key64
;
624 e_sector
[i
].foundKey
[j
] = 1;
632 PrintAndLog("nested...");
633 bool calibrate
= true;
634 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
635 for (trgBlockNo
= blDiff
; trgBlockNo
< SectorsCnt
* 4; trgBlockNo
= trgBlockNo
+ 4) {
636 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
637 if (e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
]) continue;
638 PrintAndLog("-----------------------------------------------");
639 if(mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, calibrate
)) {
640 PrintAndLog("Nested error.\n");
649 key64
= bytes_to_num(keyBlock
, 6);
651 PrintAndLog("Found valid key:%012"llx
, key64
);
652 e_sector
[trgBlockNo
/ 4].foundKey
[trgKeyType
] = 1;
653 e_sector
[trgBlockNo
/ 4].Key
[trgKeyType
] = key64
;
659 printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1
)/CLOCKS_PER_SEC
, ((float)clock() - time1
)/iterations
/CLOCKS_PER_SEC
);
661 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
663 PrintAndLog("|---|----------------|---|----------------|---|");
664 PrintAndLog("|sec|key A |res|key B |res|");
665 PrintAndLog("|---|----------------|---|----------------|---|");
666 for (i
= 0; i
< SectorsCnt
; i
++) {
667 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
668 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
670 PrintAndLog("|---|----------------|---|----------------|---|");
672 // transfer them to the emulator
674 for (i
= 0; i
< SectorsCnt
; i
++) {
675 mfEmlGetMem(keyBlock
, i
* 4 + 3, 1);
676 if (e_sector
[i
].foundKey
[0])
677 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
678 if (e_sector
[i
].foundKey
[1])
679 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
680 mfEmlSetMem(keyBlock
, i
* 4 + 3, 1);
685 if (createDumpFile
) {
686 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
687 PrintAndLog("Could not create file dumpkeys.bin");
691 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
692 for(i
=0; i
<16; i
++) {
693 if (e_sector
[i
].foundKey
[0]){
694 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
695 fwrite ( tempkey
, 1, 6, fkeys
);
698 fwrite ( &standart
, 1, 6, fkeys
);
701 for(i
=0; i
<16; i
++) {
702 if (e_sector
[i
].foundKey
[1]){
703 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
704 fwrite ( tempkey
, 1, 6, fkeys
);
707 fwrite ( &standart
, 1, 6, fkeys
);
720 get_trailer_block (uint32_t uiBlock
)
722 // Test if we are in the small or big sectors
723 uint32_t trailer_block
= 0;
725 trailer_block
= uiBlock
+ (3 - (uiBlock
% 4));
727 trailer_block
= uiBlock
+ (15 - (uiBlock
% 16));
729 return trailer_block
;
731 int CmdHF14AMfChk(const char *Cmd
)
734 char filename
[256]={0};
736 uint8_t *keyBlock
= NULL
, *p
;
737 uint8_t stKeyBlock
= 20;
743 uint8_t SectorsCnt
= 1;
747 int transferToEml
= 0;
748 int createDumpFile
= 0;
750 keyBlock
= calloc(stKeyBlock
, 6);
751 if (keyBlock
== NULL
) return 1;
753 uint64_t defaultKeys
[] =
755 0xffffffffffff, // Default key (first key used by program if no user defined key)
756 0x000000000000, // Blank key
757 0xa0a1a2a3a4a5, // NFCForum MAD key
769 int defaultKeysSize
= (sizeof(defaultKeys
) / 7) - 1;
771 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
773 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
777 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
778 PrintAndLog(" * - all sectors");
779 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
780 // PrintAndLog("d - write keys to binary file\n");
782 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
783 PrintAndLog(" hf mf chk *1 ? t");
787 if (param_getchar(Cmd
, 0)=='*') {
789 switch(param_getchar(Cmd
+1, 0)) {
790 case '0': SectorsCnt
= 5; break;
791 case '1': SectorsCnt
= 16; break;
792 case '2': SectorsCnt
= 32; break;
793 case '4': SectorsCnt
= 40; break;
794 default: SectorsCnt
= 16;
798 blockNo
= param_get8(Cmd
, 0);
800 ctmp
= param_getchar(Cmd
, 1);
812 PrintAndLog("Key type must be A , B or ?");
816 ctmp
= param_getchar(Cmd
, 2);
817 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
818 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
820 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
821 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
822 if ( stKeyBlock
- keycnt
< 2) {
823 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
825 PrintAndLog("Cannot allocate memory for Keys");
831 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
832 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
833 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
837 if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) {
838 PrintAndLog("File name too long");
843 if ( (f
= fopen( filename
, "r")) ) {
844 while( fgets(buf
, sizeof(buf
), f
) ){
845 if (strlen(buf
) < 12 || buf
[11] == '\n')
848 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
850 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
852 if (!isxdigit(buf
[0])){
853 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
859 if ( stKeyBlock
- keycnt
< 2) {
860 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
862 PrintAndLog("Cannot allocate memory for defKeys");
868 memset(keyBlock
+ 6 * keycnt
, 0, 6);
869 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
870 PrintAndLog("chk custom key[%d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
872 memset(buf
, 0, sizeof(buf
));
875 PrintAndLog("File: %s: not found or locked.", filename
);
884 PrintAndLog("No key specified,try default keys");
885 for (;keycnt
< defaultKeysSize
; keycnt
++)
886 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt
,
887 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
888 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
891 for ( int t
= !keyType
; t
< 2 ; keyType
==2?(t
++):(t
=2) ) {
893 for (int i
=0; i
<SectorsCnt
; ++i
) {
894 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i
, b
, t
?'B':'A', keycnt
);
895 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
896 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
897 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
898 res
= mfCheckKeys(b
, t
, size
, &keyBlock
[6*c
], &key64
);
901 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
904 mfEmlGetMem(block
, get_trailer_block(b
), 1);
905 num_to_bytes(key64
, 6, block
+ t
*10);
906 mfEmlSetMem(block
, get_trailer_block(b
), 1);
910 PrintAndLog("Command execute timeout");
913 b
<127?(b
+=4):(b
+=16);
921 if (createDumpFile) {
922 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
923 PrintAndLog("Could not create file dumpkeys.bin");
927 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
928 for(i=0; i<16; i++) {
929 if (e_sector[i].foundKey[0]){
930 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
931 fwrite ( tempkey, 1, 6, fkeys );
934 fwrite ( &standart, 1, 6, fkeys );
937 for(i=0; i<16; i++) {
938 if (e_sector[i].foundKey[1]){
939 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
940 fwrite ( tempkey, 1, 6, fkeys );
943 fwrite ( &standart, 1, 6, fkeys );
952 int CmdHF14AMf1kSim(const char *Cmd
)
954 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
955 uint8_t exitAfterNReads
= 0;
958 if (param_getchar(Cmd
, 0) == 'h') {
959 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
960 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
961 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
962 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
963 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
964 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
968 if (param_getchar(Cmd
, pnr
) == 'u') {
969 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
971 flags
|=FLAG_4B_UID_IN_DATA
; // UID from packet
972 }else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0)
974 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
977 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
982 if (param_getchar(Cmd
, pnr
) == 'n') {
983 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
986 if (param_getchar(Cmd
, pnr
) == 'i' ) {
987 //Using a flag to signal interactiveness, least significant bit
988 flags
|= FLAG_INTERACTIVE
;
992 if (param_getchar(Cmd
, pnr
) == 'x' ) {
993 //Using a flag to signal interactiveness, least significant bit
994 flags
|= FLAG_NR_AR_ATTACK
;
996 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
997 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
998 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
999 , exitAfterNReads
, flags
,flags
);
1002 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1003 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1006 if(flags
& FLAG_INTERACTIVE
)
1009 PrintAndLog("Press pm3-button to abort simulation");
1010 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1011 //We're waiting only 1.5 s at a time, otherwise we get the
1012 // annoying message about "Waiting for a response... "
1019 int CmdHF14AMfDbg(const char *Cmd
)
1021 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1023 PrintAndLog("Max debud mode parameter is 4 \n");
1026 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1027 PrintAndLog("Usage: hf mf dbg <debug level>");
1028 PrintAndLog(" 0 - no debug messages");
1029 PrintAndLog(" 1 - error messages");
1030 PrintAndLog(" 2 - all messages");
1031 PrintAndLog(" 4 - extended debug mode");
1035 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1041 int CmdHF14AMfEGet(const char *Cmd
)
1043 uint8_t blockNo
= 0;
1044 uint8_t data
[3 * 16];
1047 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1048 PrintAndLog("Usage: hf mf eget <block number>");
1049 PrintAndLog(" sample: hf mf eget 0 ");
1053 blockNo
= param_get8(Cmd
, 0);
1056 if (!mfEmlGetMem(data
, blockNo
, 3)) {
1057 for (i
= 0; i
< 3; i
++) {
1058 PrintAndLog("data[%d]:%s", blockNo
+ i
, sprint_hex(data
+ i
* 16, 16));
1061 PrintAndLog("Command execute timeout");
1067 int CmdHF14AMfEClear(const char *Cmd
)
1069 if (param_getchar(Cmd
, 0) == 'h') {
1070 PrintAndLog("Usage: hf mf eclr");
1071 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1075 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1080 int CmdHF14AMfESet(const char *Cmd
)
1082 uint8_t memBlock
[16];
1083 uint8_t blockNo
= 0;
1085 memset(memBlock
, 0x00, sizeof(memBlock
));
1087 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1088 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1089 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1093 blockNo
= param_get8(Cmd
, 0);
1095 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1096 PrintAndLog("block data must include 32 HEX symbols");
1101 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1102 memcpy(c
.d
.asBytes
, memBlock
, 16);
1107 int CmdHF14AMfELoad(const char *Cmd
)
1111 char * fnameptr
= filename
;
1114 int i
, len
, blockNum
;
1116 memset(filename
, 0, sizeof(filename
));
1117 memset(buf
, 0, sizeof(buf
));
1119 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1120 PrintAndLog("It loads emul dump from the file `filename.eml`");
1121 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1122 PrintAndLog(" sample: hf mf eload filename");
1127 if (len
> 14) len
= 14;
1129 memcpy(filename
, Cmd
, len
);
1132 sprintf(fnameptr
, ".eml");
1135 f
= fopen(filename
, "r");
1137 PrintAndLog("File not found or locked.");
1143 memset(buf
, 0, sizeof(buf
));
1144 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1145 if(blockNum
== 16 * 4)
1149 PrintAndLog("File reading error.");
1153 if (strlen(buf
) < 32){
1154 if(strlen(buf
) && feof(f
))
1156 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1159 for (i
= 0; i
< 32; i
+= 2)
1160 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1161 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1163 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1164 PrintAndLog("Cant set emul block: %d", blockNum
);
1169 if (blockNum
>= 32 * 4 + 8 * 16) break;
1173 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1174 PrintAndLog("File content error. There must be 64 blocks");
1177 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1181 int CmdHF14AMfESave(const char *Cmd
)
1185 char * fnameptr
= filename
;
1189 memset(filename
, 0, sizeof(filename
));
1190 memset(buf
, 0, sizeof(buf
));
1192 if (param_getchar(Cmd
, 0) == 'h') {
1193 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1194 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1195 PrintAndLog(" sample: hf mf esave ");
1196 PrintAndLog(" hf mf esave filename");
1201 if (len
> 14) len
= 14;
1205 if (mfEmlGetMem(buf
, 0, 1)) {
1206 PrintAndLog("Cant get block: %d", 0);
1209 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1210 sprintf(fnameptr
, "%02x", buf
[j
]);
1212 memcpy(filename
, Cmd
, len
);
1216 sprintf(fnameptr
, ".eml");
1219 f
= fopen(filename
, "w+");
1222 for (i
= 0; i
< 32 * 4 + 8 * 16; i
++) {
1223 if (mfEmlGetMem(buf
, i
, 1)) {
1224 PrintAndLog("Cant get block: %d", i
);
1227 for (j
= 0; j
< 16; j
++)
1228 fprintf(f
, "%02x", buf
[j
]);
1233 PrintAndLog("Saved to file: %s", filename
);
1238 int CmdHF14AMfECFill(const char *Cmd
)
1240 uint8_t keyType
= 0;
1242 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1243 PrintAndLog("Usage: hf mf efill <key A/B>");
1244 PrintAndLog("sample: hf mf efill A");
1245 PrintAndLog("Card data blocks transfers to card emulator memory.");
1246 PrintAndLog("Keys must be laid in the simulator memory. \n");
1250 char ctmp
= param_getchar(Cmd
, 0);
1252 PrintAndLog("Key type must be A or B");
1255 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1257 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {0, keyType
, 0}};
1262 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1266 uint64_t keyA
, keyB
;
1268 PrintAndLog("|---|----------------|----------------|");
1269 PrintAndLog("|sec|key A |key B |");
1270 PrintAndLog("|---|----------------|----------------|");
1271 for (i
= 0; i
< 40; i
++) {
1272 b
<127?(b
+=4):(b
+=16);
1273 if (mfEmlGetMem(data
, b
, 1)) {
1274 PrintAndLog("error get block %d", b
);
1277 keyA
= bytes_to_num(data
, 6);
1278 keyB
= bytes_to_num(data
+ 10, 6);
1279 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1281 PrintAndLog("|---|----------------|----------------|");
1286 int CmdHF14AMfCSetUID(const char *Cmd
)
1288 uint8_t wipeCard
= 0;
1293 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1294 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1295 PrintAndLog("sample: hf mf csetuid 01020304 w");
1296 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1297 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1301 if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) {
1302 PrintAndLog("UID must include 8 HEX symbols");
1306 char ctmp
= param_getchar(Cmd
, 1);
1307 if (ctmp
== 'w' || ctmp
== 'W') wipeCard
= 1;
1309 PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4));
1311 res
= mfCSetUID(uid
, oldUid
, wipeCard
);
1313 PrintAndLog("Can't set UID. error=%d", res
);
1317 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1321 int CmdHF14AMfCSetBlk(const char *Cmd
)
1324 uint8_t memBlock
[16];
1325 uint8_t blockNo
= 0;
1327 memset(memBlock
, 0x00, sizeof(memBlock
));
1329 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1330 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1331 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1332 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1333 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1337 blockNo
= param_get8(Cmd
, 0);
1339 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1340 PrintAndLog("block data must include 32 HEX symbols");
1344 PrintAndLog("--block number:%02x data:%s", blockNo
, sprint_hex(memBlock
, 16));
1346 res
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
);
1348 PrintAndLog("Can't write block. error=%d", res
);
1352 PrintAndLog("UID:%s", sprint_hex(uid
, 4));
1356 int CmdHF14AMfCLoad(const char *Cmd
)
1360 char * fnameptr
= filename
;
1363 uint8_t fillFromEmulator
= 0;
1364 int i
, len
, blockNum
, flags
;
1366 memset(filename
, 0, sizeof(filename
));
1367 memset(buf
, 0, sizeof(buf
));
1369 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1370 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1371 PrintAndLog("or from emulator memory (option `e`)");
1372 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1373 PrintAndLog(" or: hf mf cload e ");
1374 PrintAndLog(" sample: hf mf cload filename");
1378 char ctmp
= param_getchar(Cmd
, 0);
1379 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1381 if (fillFromEmulator
) {
1382 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1383 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1384 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1385 PrintAndLog("Cant get block: %d", blockNum
);
1389 if (blockNum
== 2) flags
= 0;
1390 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1392 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1393 PrintAndLog("Cant set magic card block: %d", blockNum
);
1400 if (len
> 14) len
= 14;
1402 memcpy(filename
, Cmd
, len
);
1405 sprintf(fnameptr
, ".eml");
1408 f
= fopen(filename
, "r");
1410 PrintAndLog("File not found or locked.");
1415 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1417 memset(buf
, 0, sizeof(buf
));
1418 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1419 PrintAndLog("File reading error.");
1423 if (strlen(buf
) < 32){
1424 if(strlen(buf
) && feof(f
))
1426 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1429 for (i
= 0; i
< 32; i
+= 2)
1430 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1432 if (blockNum
== 2) flags
= 0;
1433 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1435 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1436 PrintAndLog("Cant set magic card block: %d", blockNum
);
1441 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1445 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1446 PrintAndLog("File content error. There must be 64 blocks");
1449 PrintAndLog("Loaded from file: %s", filename
);
1454 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1455 uint8_t memBlock
[16];
1456 uint8_t blockNo
= 0;
1458 memset(memBlock
, 0x00, sizeof(memBlock
));
1460 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1461 PrintAndLog("Usage: hf mf cgetblk <block number>");
1462 PrintAndLog("sample: hf mf cgetblk 1");
1463 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1467 blockNo
= param_get8(Cmd
, 0);
1469 PrintAndLog("--block number:%02x ", blockNo
);
1471 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1473 PrintAndLog("Can't read block. error=%d", res
);
1477 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1481 int CmdHF14AMfCGetSc(const char *Cmd
) {
1482 uint8_t memBlock
[16];
1483 uint8_t sectorNo
= 0;
1485 memset(memBlock
, 0x00, sizeof(memBlock
));
1487 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1488 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1489 PrintAndLog("sample: hf mf cgetsc 0");
1490 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1494 sectorNo
= param_get8(Cmd
, 0);
1495 if (sectorNo
> 15) {
1496 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1500 PrintAndLog("--sector number:%02x ", sectorNo
);
1502 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1503 for (i
= 0; i
< 4; i
++) {
1504 if (i
== 1) flags
= 0;
1505 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1507 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1509 PrintAndLog("Can't read block. %02x error=%d", sectorNo
* 4 + i
, res
);
1513 PrintAndLog("block %02x data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1518 int CmdHF14AMfCSave(const char *Cmd
) {
1522 char * fnameptr
= filename
;
1523 uint8_t fillFromEmulator
= 0;
1525 int i
, j
, len
, flags
;
1527 memset(filename
, 0, sizeof(filename
));
1528 memset(buf
, 0, sizeof(buf
));
1530 if (param_getchar(Cmd
, 0) == 'h') {
1531 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1532 PrintAndLog("or into emulator memory (option `e`)");
1533 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1534 PrintAndLog(" sample: hf mf esave ");
1535 PrintAndLog(" hf mf esave filename");
1536 PrintAndLog(" hf mf esave e \n");
1540 char ctmp
= param_getchar(Cmd
, 0);
1541 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1543 if (fillFromEmulator
) {
1544 // put into emulator
1545 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1546 for (i
= 0; i
< 16 * 4; i
++) {
1547 if (i
== 1) flags
= 0;
1548 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1550 if (mfCGetBlock(i
, buf
, flags
)) {
1551 PrintAndLog("Cant get block: %d", i
);
1555 if (mfEmlSetMem(buf
, i
, 1)) {
1556 PrintAndLog("Cant set emul block: %d", i
);
1563 if (len
> 14) len
= 14;
1567 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1568 PrintAndLog("Cant get block: %d", 0);
1571 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1572 sprintf(fnameptr
, "%02x", buf
[j
]);
1574 memcpy(filename
, Cmd
, len
);
1578 sprintf(fnameptr
, ".eml");
1581 f
= fopen(filename
, "w+");
1584 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1585 for (i
= 0; i
< 16 * 4; i
++) {
1586 if (i
== 1) flags
= 0;
1587 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1589 if (mfCGetBlock(i
, buf
, flags
)) {
1590 PrintAndLog("Cant get block: %d", i
);
1593 for (j
= 0; j
< 16; j
++)
1594 fprintf(f
, "%02x", buf
[j
]);
1599 PrintAndLog("Saved to file: %s", filename
);
1605 int CmdHF14AMfSniff(const char *Cmd
){
1607 bool wantLogToFile
= 0;
1608 bool wantDecrypt
= 0;
1609 //bool wantSaveToEml = 0; TODO
1610 bool wantSaveToEmlFile
= 0;
1624 uint8_t * bufPtr
= buf
;
1625 memset(buf
, 0x00, 3000);
1627 if (param_getchar(Cmd
, 0) == 'h') {
1628 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1629 PrintAndLog("You can specify:");
1630 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1631 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1632 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1633 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1634 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1635 PrintAndLog(" sample: hf mf sniff l d e");
1639 for (int i
= 0; i
< 4; i
++) {
1640 char ctmp
= param_getchar(Cmd
, i
);
1641 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1642 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1643 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1644 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1647 printf("-------------------------------------------------------------------------\n");
1648 printf("Executing command. \n");
1649 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1650 printf("Press the key on pc keyboard to abort the client.\n");
1651 printf("-------------------------------------------------------------------------\n");
1653 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1662 printf("\naborted via keyboard!\n");
1667 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1668 res
= resp
.arg
[0] & 0xff;
1672 if (res
== 0) return 0;
1676 memset(buf
, 0x00, 3000);
1678 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1683 blockLen
= bufPtr
- buf
;
1686 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1688 while (bufPtr
- buf
+ 9 < blockLen
) {
1689 isTag
= bufPtr
[3] & 0x80 ? true:false;
1691 parity
= *((uint32_t *)(bufPtr
));
1695 if ((len
== 14) && (bufPtr
[0] = 0xff) && (bufPtr
[1] = 0xff)) {
1696 memcpy(uid
, bufPtr
+ 2, 7);
1697 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1700 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid
, 7), atqa
[0], atqa
[1], sak
);
1701 if (wantLogToFile
) {
1702 FillFileNameByUID(logHexFileName
, uid
, ".log", 7);
1703 AddLogCurrentDT(logHexFileName
);
1705 if (wantDecrypt
) mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1707 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1708 if (wantLogToFile
) AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1709 if (wantDecrypt
) mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
);
1720 static command_t CommandTable
[] =
1722 {"help", CmdHelp
, 1, "This help"},
1723 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1724 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1725 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1726 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1727 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1728 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1729 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1730 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1731 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1732 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1733 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1734 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1735 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1736 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1737 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1738 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1739 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1740 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1741 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1742 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block into magic Chinese card"},
1743 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block from magic Chinese card"},
1744 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector from magic Chinese card"},
1745 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1746 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1747 {NULL
, NULL
, 0, NULL
}
1750 int CmdHFMF(const char *Cmd
)
1753 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1755 CmdsParse(CommandTable
, Cmd
);
1759 int CmdHelp(const char *Cmd
)
1761 CmdsHelp(CommandTable
);