]>
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 //-----------------------------------------------------------------------------
12 #include "nonce2key/nonce2key.h"
14 static int CmdHelp(const char *Cmd
);
16 int CmdHF14AMifare(const char *Cmd
)
19 uint32_t nt
= 0, nr
= 0;
20 uint64_t par_list
= 0, ks_list
= 0, r_key
= 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 button on the proxmark3 device to abort both proxmark3 and client.\n");
29 printf("-------------------------------------------------------------------------\n");
37 while (ukbhit()) getchar();
45 printf("\naborted via keyboard!\n");
50 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
52 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
53 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
54 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
55 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
56 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
59 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
60 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
61 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
71 if (isOK
!= 1) return 1;
73 // execute original function from util nonce2key
74 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
76 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
77 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
82 printf("------------------------------------------------------------------\n");
83 PrintAndLog("Found valid key:%012"llx
" \n", r_key
);
90 int CmdHF14AMfWrBl(const char *Cmd
)
94 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
95 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
100 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
101 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
105 blockNo
= param_get8(Cmd
, 0);
106 cmdp
= param_getchar(Cmd
, 1);
108 PrintAndLog("Key type must be A or B");
111 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
112 if (param_gethex(Cmd
, 2, key
, 12)) {
113 PrintAndLog("Key must include 12 HEX symbols");
116 if (param_gethex(Cmd
, 3, bldata
, 32)) {
117 PrintAndLog("Block data must include 32 HEX symbols");
120 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
121 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
123 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
124 memcpy(c
.d
.asBytes
, key
, 6);
125 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
129 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
130 uint8_t isOK
= resp
.arg
[0] & 0xff;
131 PrintAndLog("isOk:%02x", isOK
);
133 PrintAndLog("Command execute timeout");
139 int CmdHF14AMfRdBl(const char *Cmd
)
143 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
149 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
150 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
154 blockNo
= param_get8(Cmd
, 0);
155 cmdp
= param_getchar(Cmd
, 1);
157 PrintAndLog("Key type must be A or B");
160 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
161 if (param_gethex(Cmd
, 2, key
, 12)) {
162 PrintAndLog("Key must include 12 HEX symbols");
165 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
167 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
168 memcpy(c
.d
.asBytes
, key
, 6);
172 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
173 uint8_t isOK
= resp
.arg
[0] & 0xff;
174 uint8_t *data
= resp
.d
.asBytes
;
177 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
179 PrintAndLog("isOk:%02x", isOK
);
181 PrintAndLog("Command execute timeout");
187 int CmdHF14AMfRdSc(const char *Cmd
)
190 uint8_t sectorNo
= 0;
192 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
194 uint8_t *data
= NULL
;
198 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
199 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
203 sectorNo
= param_get8(Cmd
, 0);
205 PrintAndLog("Sector number must be less than 40");
208 cmdp
= param_getchar(Cmd
, 1);
209 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
210 PrintAndLog("Key type must be A or B");
213 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
214 if (param_gethex(Cmd
, 2, key
, 12)) {
215 PrintAndLog("Key must include 12 HEX symbols");
218 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
220 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
221 memcpy(c
.d
.asBytes
, key
, 6);
226 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
227 isOK
= resp
.arg
[0] & 0xff;
228 data
= resp
.d
.asBytes
;
230 PrintAndLog("isOk:%02x", isOK
);
232 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
233 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
235 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
238 PrintAndLog("Command execute timeout");
244 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
249 return 32 * 4 + (sectorNo
- 32) * 16;
253 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
262 int CmdHF14AMfDump(const char *Cmd
)
264 uint8_t sectorNo
, blockNo
;
268 uint8_t rights
[40][4];
269 uint8_t carddata
[256][16];
270 uint8_t numSectors
= 16;
277 char cmdp
= param_getchar(Cmd
, 0);
279 case '0' : numSectors
= 5; break;
281 case '\0': numSectors
= 16; break;
282 case '2' : numSectors
= 32; break;
283 case '4' : numSectors
= 40; break;
284 default: numSectors
= 16;
287 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
288 PrintAndLog("Usage: hf mf dump [card memory]");
289 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
291 PrintAndLog("Samples: hf mf dump");
292 PrintAndLog(" hf mf dump 4");
296 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
297 PrintAndLog("Could not find file dumpkeys.bin");
301 // Read keys A from file
302 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
303 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
304 PrintAndLog("File reading error.");
310 // Read keys B from file
311 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
312 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
313 PrintAndLog("File reading error.");
321 PrintAndLog("|-----------------------------------------|");
322 PrintAndLog("|------ Reading sector access bits...-----|");
323 PrintAndLog("|-----------------------------------------|");
325 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
326 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
327 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
330 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
331 uint8_t isOK
= resp
.arg
[0] & 0xff;
332 uint8_t *data
= resp
.d
.asBytes
;
334 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
335 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
336 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
337 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
339 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
340 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
341 rights
[sectorNo
][3] = 0x01;
344 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
345 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
346 rights
[sectorNo
][3] = 0x01;
350 PrintAndLog("|-----------------------------------------|");
351 PrintAndLog("|----- Dumping all blocks to file... -----|");
352 PrintAndLog("|-----------------------------------------|");
355 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
356 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
357 bool received
= false;
359 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
360 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
361 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
363 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
364 } else { // data block. Check if it can be read with key A or key B
365 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
366 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
367 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
368 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
370 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
371 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
373 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
374 } else { // key A would work
375 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
376 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
378 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
383 isOK
= resp
.arg
[0] & 0xff;
384 uint8_t *data
= resp
.d
.asBytes
;
385 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
386 data
[0] = (keyA
[sectorNo
][0]);
387 data
[1] = (keyA
[sectorNo
][1]);
388 data
[2] = (keyA
[sectorNo
][2]);
389 data
[3] = (keyA
[sectorNo
][3]);
390 data
[4] = (keyA
[sectorNo
][4]);
391 data
[5] = (keyA
[sectorNo
][5]);
392 data
[10] = (keyB
[sectorNo
][0]);
393 data
[11] = (keyB
[sectorNo
][1]);
394 data
[12] = (keyB
[sectorNo
][2]);
395 data
[13] = (keyB
[sectorNo
][3]);
396 data
[14] = (keyB
[sectorNo
][4]);
397 data
[15] = (keyB
[sectorNo
][5]);
400 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
401 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
403 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
409 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
416 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
417 PrintAndLog("Could not create file name dumpdata.bin");
420 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
421 fwrite(carddata
, 1, 16*numblocks
, fout
);
423 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
429 int CmdHF14AMfRestore(const char *Cmd
)
431 uint8_t sectorNo
,blockNo
;
433 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
434 uint8_t bldata
[16] = {0x00};
442 char cmdp
= param_getchar(Cmd
, 0);
444 case '0' : numSectors
= 5; break;
446 case '\0': numSectors
= 16; break;
447 case '2' : numSectors
= 32; break;
448 case '4' : numSectors
= 40; break;
449 default: numSectors
= 16;
452 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
453 PrintAndLog("Usage: hf mf restore [card memory]");
454 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
456 PrintAndLog("Samples: hf mf restore");
457 PrintAndLog(" hf mf restore 4");
461 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
462 PrintAndLog("Could not find file dumpkeys.bin");
466 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
467 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
468 PrintAndLog("File reading error (dumpkeys.bin).");
475 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
476 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
477 PrintAndLog("File reading error (dumpkeys.bin).");
485 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
486 PrintAndLog("Could not find file dumpdata.bin");
489 PrintAndLog("Restoring dumpdata.bin to card");
491 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
492 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
493 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
494 memcpy(c
.d
.asBytes
, key
, 6);
496 if (fread(bldata
, 1, 16, fdump
) == 0) {
497 PrintAndLog("File reading error (dumpdata.bin).");
502 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
503 bldata
[0] = (keyA
[sectorNo
][0]);
504 bldata
[1] = (keyA
[sectorNo
][1]);
505 bldata
[2] = (keyA
[sectorNo
][2]);
506 bldata
[3] = (keyA
[sectorNo
][3]);
507 bldata
[4] = (keyA
[sectorNo
][4]);
508 bldata
[5] = (keyA
[sectorNo
][5]);
509 bldata
[10] = (keyB
[sectorNo
][0]);
510 bldata
[11] = (keyB
[sectorNo
][1]);
511 bldata
[12] = (keyB
[sectorNo
][2]);
512 bldata
[13] = (keyB
[sectorNo
][3]);
513 bldata
[14] = (keyB
[sectorNo
][4]);
514 bldata
[15] = (keyB
[sectorNo
][5]);
517 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
519 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
523 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
524 uint8_t isOK
= resp
.arg
[0] & 0xff;
525 PrintAndLog("isOk:%02x", isOK
);
527 PrintAndLog("Command execute timeout");
536 int CmdHF14AMfNested(const char *Cmd
)
538 int i
, j
, res
, iterations
;
539 sector
*e_sector
= NULL
;
542 uint8_t trgBlockNo
= 0;
543 uint8_t trgKeyType
= 0;
544 uint8_t SectorsCnt
= 0;
545 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
546 uint8_t keyBlock
[14*6];
548 bool transferToEml
= false;
550 bool createDumpFile
= false;
552 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
553 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
558 PrintAndLog("Usage:");
559 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
560 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
561 PrintAndLog(" <target block number> <target key A/B> [t]");
562 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
563 PrintAndLog("t - transfer keys into emulator memory");
564 PrintAndLog("d - write keys to binary file");
566 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
567 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
568 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
569 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
573 cmdp
= param_getchar(Cmd
, 0);
574 blockNo
= param_get8(Cmd
, 1);
575 ctmp
= param_getchar(Cmd
, 2);
577 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
578 PrintAndLog("Key type must be A or B");
582 if (ctmp
!= 'A' && ctmp
!= 'a')
585 if (param_gethex(Cmd
, 3, key
, 12)) {
586 PrintAndLog("Key must include 12 HEX symbols");
590 if (cmdp
== 'o' || cmdp
== 'O') {
592 trgBlockNo
= param_get8(Cmd
, 4);
593 ctmp
= param_getchar(Cmd
, 5);
594 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
595 PrintAndLog("Target key type must be A or B");
598 if (ctmp
!= 'A' && ctmp
!= 'a')
603 case '0': SectorsCnt
= 05; break;
604 case '1': SectorsCnt
= 16; break;
605 case '2': SectorsCnt
= 32; break;
606 case '4': SectorsCnt
= 40; break;
607 default: SectorsCnt
= 16;
611 ctmp
= param_getchar(Cmd
, 4);
612 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
613 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
615 ctmp
= param_getchar(Cmd
, 6);
616 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
617 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
620 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
621 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
624 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
625 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
626 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
627 default : PrintAndLog("Unknown Error.\n");
631 key64
= bytes_to_num(keyBlock
, 6);
633 PrintAndLog("Found valid key:%012"llx
, key64
);
635 // transfer key to the emulator
637 uint8_t sectortrailer
;
638 if (trgBlockNo
< 32*4) { // 4 block sector
639 sectortrailer
= (trgBlockNo
& 0x03) + 3;
640 } else { // 16 block sector
641 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
643 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
646 num_to_bytes(key64
, 6, keyBlock
);
648 num_to_bytes(key64
, 6, &keyBlock
[10]);
649 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
652 PrintAndLog("No valid key found");
655 else { // ------------------------------------ multiple sectors working
659 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
660 if (e_sector
== NULL
) return 1;
662 //test current key and additional standard keys first
663 memcpy(keyBlock
, key
, 6);
664 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
665 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
666 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
667 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
668 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
669 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
670 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
671 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
672 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
673 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
674 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
675 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
676 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
678 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
679 for (i
= 0; i
< SectorsCnt
; i
++) {
680 for (j
= 0; j
< 2; j
++) {
681 if (e_sector
[i
].foundKey
[j
]) continue;
683 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
686 e_sector
[i
].Key
[j
] = key64
;
687 e_sector
[i
].foundKey
[j
] = 1;
694 PrintAndLog("nested...");
695 bool calibrate
= true;
696 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
697 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
698 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
699 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
700 PrintAndLog("-----------------------------------------------");
701 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
704 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
705 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
706 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
707 default : PrintAndLog("Unknown Error.\n");
717 key64
= bytes_to_num(keyBlock
, 6);
719 PrintAndLog("Found valid key:%012"llx
, key64
);
720 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
721 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
727 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
);
729 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
731 PrintAndLog("|---|----------------|---|----------------|---|");
732 PrintAndLog("|sec|key A |res|key B |res|");
733 PrintAndLog("|---|----------------|---|----------------|---|");
734 for (i
= 0; i
< SectorsCnt
; i
++) {
735 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
736 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
738 PrintAndLog("|---|----------------|---|----------------|---|");
740 // transfer them to the emulator
742 for (i
= 0; i
< SectorsCnt
; i
++) {
743 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
744 if (e_sector
[i
].foundKey
[0])
745 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
746 if (e_sector
[i
].foundKey
[1])
747 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
748 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
753 if (createDumpFile
) {
754 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
755 PrintAndLog("Could not create file dumpkeys.bin");
759 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
760 for(i
=0; i
<SectorsCnt
; i
++) {
761 if (e_sector
[i
].foundKey
[0]){
762 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
763 fwrite ( tempkey
, 1, 6, fkeys
);
766 fwrite ( &standart
, 1, 6, fkeys
);
769 for(i
=0; i
<SectorsCnt
; i
++) {
770 if (e_sector
[i
].foundKey
[1]){
771 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
772 fwrite ( tempkey
, 1, 6, fkeys
);
775 fwrite ( &standart
, 1, 6, fkeys
);
786 int CmdHF14AMfChk(const char *Cmd
)
789 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
790 PrintAndLog(" * - all sectors");
791 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
792 PrintAndLog("d - write keys to binary file");
793 PrintAndLog("t - write keys to emulator memory\n");
794 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
795 PrintAndLog(" hf mf chk *1 ? t");
796 PrintAndLog(" hf mf chk *1 ? d");
801 char filename
[FILE_PATH_SIZE
]={0};
803 uint8_t *keyBlock
= NULL
, *p
;
804 uint8_t stKeyBlock
= 20;
810 uint8_t SectorsCnt
= 1;
814 int transferToEml
= 0;
815 int createDumpFile
= 0;
817 keyBlock
= calloc(stKeyBlock
, 6);
818 if (keyBlock
== NULL
) return 1;
820 uint64_t defaultKeys
[] =
822 0xffffffffffff, // Default key (first key used by program if no user defined key)
823 0x000000000000, // Blank key
824 0xa0a1a2a3a4a5, // NFCForum MAD key
836 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
838 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
840 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
843 if (param_getchar(Cmd
, 0)=='*') {
845 switch(param_getchar(Cmd
+1, 0)) {
846 case '0': SectorsCnt
= 5; break;
847 case '1': SectorsCnt
= 16; break;
848 case '2': SectorsCnt
= 32; break;
849 case '4': SectorsCnt
= 40; break;
850 default: SectorsCnt
= 16;
854 blockNo
= param_get8(Cmd
, 0);
856 ctmp
= param_getchar(Cmd
, 1);
868 PrintAndLog("Key type must be A , B or ?");
872 ctmp
= param_getchar(Cmd
, 2);
873 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
874 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
876 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
877 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
878 if ( stKeyBlock
- keycnt
< 2) {
879 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
881 PrintAndLog("Cannot allocate memory for Keys");
887 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
888 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
889 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
893 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
894 PrintAndLog("File name too long");
899 if ( (f
= fopen( filename
, "r")) ) {
900 while( fgets(buf
, sizeof(buf
), f
) ){
901 if (strlen(buf
) < 12 || buf
[11] == '\n')
904 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
906 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
908 if (!isxdigit(buf
[0])){
909 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
915 if ( stKeyBlock
- keycnt
< 2) {
916 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
918 PrintAndLog("Cannot allocate memory for defKeys");
924 memset(keyBlock
+ 6 * keycnt
, 0, 6);
925 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
926 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
928 memset(buf
, 0, sizeof(buf
));
932 PrintAndLog("File: %s: not found or locked.", filename
);
941 PrintAndLog("No key specified, trying default keys");
942 for (;keycnt
< defaultKeysSize
; keycnt
++)
943 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
944 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
945 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
948 // initialize storage for found keys
949 bool validKey
[2][40];
950 uint8_t foundKey
[2][40][6];
951 for (uint16_t t
= 0; t
< 2; t
++) {
952 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
953 validKey
[t
][sectorNo
] = false;
954 for (uint16_t i
= 0; i
< 6; i
++) {
955 foundKey
[t
][sectorNo
][i
] = 0xff;
960 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
962 for (int i
= 0; i
< SectorsCnt
; ++i
) {
963 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
964 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
965 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
966 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
967 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
970 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
971 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
972 validKey
[t
][i
] = true;
975 PrintAndLog("Command execute timeout");
978 b
<127?(b
+=4):(b
+=16);
984 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
985 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
986 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
987 for (uint16_t t
= 0; t
< 2; t
++) {
988 if (validKey
[t
][sectorNo
]) {
989 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
992 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
995 PrintAndLog("Found keys have been transferred to the emulator memory");
998 if (createDumpFile
) {
999 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1000 if (fkeys
== NULL
) {
1001 PrintAndLog("Could not create file dumpkeys.bin");
1005 for (uint16_t t
= 0; t
< 2; t
++) {
1006 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1009 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1017 int CmdHF14AMf1kSim(const char *Cmd
)
1019 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1020 uint8_t exitAfterNReads
= 0;
1023 uint8_t cmdp
= param_getchar(Cmd
, 0);
1025 clearCommandBuffer();
1027 if (cmdp
== 'h' || cmdp
== 'H') {
1028 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1029 PrintAndLog(" h this help");
1030 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1031 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1032 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1033 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1035 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1039 if (param_getchar(Cmd
, pnr
) == 'u') {
1040 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1042 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1043 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1044 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1046 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1051 if (param_getchar(Cmd
, pnr
) == 'n') {
1052 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1055 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1056 //Using a flag to signal interactiveness, least significant bit
1057 flags
|= FLAG_INTERACTIVE
;
1061 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1062 //Using a flag to signal interactiveness, least significant bit
1063 flags
|= FLAG_NR_AR_ATTACK
;
1065 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1066 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1067 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1068 , exitAfterNReads
, flags
,flags
);
1071 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1072 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1075 if(flags
& FLAG_INTERACTIVE
)
1077 PrintAndLog("Press pm3-button to abort simulation");
1084 if ( WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1085 if ( (resp
.arg
[0] & 0xffff) == CMD_SIMULATE_MIFARE_CARD
){
1086 memset(data
, 0x00, sizeof(data
));
1087 memset(key
, 0x00, sizeof(key
));
1088 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1090 memcpy(data
, resp
.d
.asBytes
, len
);
1092 uint64_t corr_uid
= 0;
1093 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1094 corr_uid
= (data
[3] << 24) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1097 corr_uid
|= (uint64_t)data
[2] << 48;
1098 corr_uid
|= (uint64_t)data
[1] << 40;
1099 corr_uid
|= (uint64_t)data
[0] << 32;
1100 corr_uid
|= data
[7] << 24;
1101 corr_uid
|= data
[6] << 16;
1102 corr_uid
|= data
[5] << 8;
1103 corr_uid
|= data
[4];
1105 tryMfk32(corr_uid
, data
, key
);
1106 //tryMfk64(corr_uid, data, key);
1115 int CmdHF14AMfDbg(const char *Cmd
)
1117 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1119 PrintAndLog("Max debug mode parameter is 4 \n");
1122 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1123 PrintAndLog("Usage: hf mf dbg <debug level>");
1124 PrintAndLog(" 0 - no debug messages");
1125 PrintAndLog(" 1 - error messages");
1126 PrintAndLog(" 2 - plus information messages");
1127 PrintAndLog(" 3 - plus debug messages");
1128 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1129 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1133 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1139 int CmdHF14AMfEGet(const char *Cmd
)
1141 uint8_t blockNo
= 0;
1142 uint8_t data
[16] = {0x00};
1144 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1145 PrintAndLog("Usage: hf mf eget <block number>");
1146 PrintAndLog(" sample: hf mf eget 0 ");
1150 blockNo
= param_get8(Cmd
, 0);
1153 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1154 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1156 PrintAndLog("Command execute timeout");
1162 int CmdHF14AMfEClear(const char *Cmd
)
1164 if (param_getchar(Cmd
, 0) == 'h') {
1165 PrintAndLog("Usage: hf mf eclr");
1166 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1170 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1176 int CmdHF14AMfESet(const char *Cmd
)
1178 uint8_t memBlock
[16];
1179 uint8_t blockNo
= 0;
1181 memset(memBlock
, 0x00, sizeof(memBlock
));
1183 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1184 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1185 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1189 blockNo
= param_get8(Cmd
, 0);
1191 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1192 PrintAndLog("block data must include 32 HEX symbols");
1197 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1198 memcpy(c
.d
.asBytes
, memBlock
, 16);
1204 int CmdHF14AMfELoad(const char *Cmd
)
1207 char filename
[FILE_PATH_SIZE
];
1208 char *fnameptr
= filename
;
1209 char buf
[64] = {0x00};
1210 uint8_t buf8
[64] = {0x00};
1211 int i
, len
, blockNum
, numBlocks
;
1212 int nameParamNo
= 1;
1214 char ctmp
= param_getchar(Cmd
, 0);
1216 if ( ctmp
== 'h' || ctmp
== 0x00) {
1217 PrintAndLog("It loads emul dump from the file `filename.eml`");
1218 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1219 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1221 PrintAndLog(" sample: hf mf eload filename");
1222 PrintAndLog(" hf mf eload 4 filename");
1227 case '0' : numBlocks
= 5*4; break;
1229 case '\0': numBlocks
= 16*4; break;
1230 case '2' : numBlocks
= 32*4; break;
1231 case '4' : numBlocks
= 256; break;
1238 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1240 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1244 sprintf(fnameptr
, ".eml");
1247 f
= fopen(filename
, "r");
1249 PrintAndLog("File %s not found or locked", filename
);
1255 memset(buf
, 0, sizeof(buf
));
1257 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1259 if (blockNum
>= numBlocks
) break;
1261 PrintAndLog("File reading error.");
1266 if (strlen(buf
) < 32){
1267 if(strlen(buf
) && feof(f
))
1269 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1274 for (i
= 0; i
< 32; i
+= 2) {
1275 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1278 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1279 PrintAndLog("Cant set emul block: %3d", blockNum
);
1286 if (blockNum
>= numBlocks
) break;
1291 if ((blockNum
!= numBlocks
)) {
1292 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1295 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1300 int CmdHF14AMfESave(const char *Cmd
)
1303 char filename
[FILE_PATH_SIZE
];
1304 char * fnameptr
= filename
;
1306 int i
, j
, len
, numBlocks
;
1307 int nameParamNo
= 1;
1309 memset(filename
, 0, sizeof(filename
));
1310 memset(buf
, 0, sizeof(buf
));
1312 char ctmp
= param_getchar(Cmd
, 0);
1314 if ( ctmp
== 'h' || ctmp
== 'H') {
1315 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1316 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1317 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1319 PrintAndLog(" sample: hf mf esave ");
1320 PrintAndLog(" hf mf esave 4");
1321 PrintAndLog(" hf mf esave 4 filename");
1326 case '0' : numBlocks
= 5*4; break;
1328 case '\0': numBlocks
= 16*4; break;
1329 case '2' : numBlocks
= 32*4; break;
1330 case '4' : numBlocks
= 256; break;
1337 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1339 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1341 // user supplied filename?
1343 // get filename (UID from memory)
1344 if (mfEmlGetMem(buf
, 0, 1)) {
1345 PrintAndLog("Can\'t get UID from block: %d", 0);
1346 len
= sprintf(fnameptr
, "dump");
1350 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1351 sprintf(fnameptr
, "%02X", buf
[j
]);
1357 // add file extension
1358 sprintf(fnameptr
, ".eml");
1361 f
= fopen(filename
, "w+");
1364 PrintAndLog("Can't open file %s ", filename
);
1369 for (i
= 0; i
< numBlocks
; i
++) {
1370 if (mfEmlGetMem(buf
, i
, 1)) {
1371 PrintAndLog("Cant get block: %d", i
);
1374 for (j
= 0; j
< 16; j
++)
1375 fprintf(f
, "%02X", buf
[j
]);
1380 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1386 int CmdHF14AMfECFill(const char *Cmd
)
1388 uint8_t keyType
= 0;
1389 uint8_t numSectors
= 16;
1391 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1392 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1393 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1395 PrintAndLog("samples: hf mf ecfill A");
1396 PrintAndLog(" hf mf ecfill A 4");
1397 PrintAndLog("Read card and transfer its data to emulator memory.");
1398 PrintAndLog("Keys must be laid in the emulator memory. \n");
1402 char ctmp
= param_getchar(Cmd
, 0);
1403 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1404 PrintAndLog("Key type must be A or B");
1407 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1409 ctmp
= param_getchar(Cmd
, 1);
1411 case '0' : numSectors
= 5; break;
1413 case '\0': numSectors
= 16; break;
1414 case '2' : numSectors
= 32; break;
1415 case '4' : numSectors
= 40; break;
1416 default: numSectors
= 16;
1419 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1420 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1426 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1431 uint64_t keyA
, keyB
;
1433 if (param_getchar(Cmd
, 0) == 'h') {
1434 PrintAndLog("It prints the keys loaded in the emulator memory");
1435 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1436 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1438 PrintAndLog(" sample: hf mf ekeyprn 1");
1442 char cmdp
= param_getchar(Cmd
, 0);
1445 case '0' : numSectors
= 5; break;
1447 case '\0': numSectors
= 16; break;
1448 case '2' : numSectors
= 32; break;
1449 case '4' : numSectors
= 40; break;
1450 default: numSectors
= 16;
1453 PrintAndLog("|---|----------------|----------------|");
1454 PrintAndLog("|sec|key A |key B |");
1455 PrintAndLog("|---|----------------|----------------|");
1456 for (i
= 0; i
< numSectors
; i
++) {
1457 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1458 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1461 keyA
= bytes_to_num(data
, 6);
1462 keyB
= bytes_to_num(data
+ 10, 6);
1463 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1465 PrintAndLog("|---|----------------|----------------|");
1471 int CmdHF14AMfCSetUID(const char *Cmd
)
1473 uint8_t wipeCard
= 0;
1474 uint8_t uid
[8] = {0x00};
1475 uint8_t oldUid
[8] = {0x00};
1476 uint8_t atqa
[2] = {0x00};
1477 uint8_t sak
[1] = {0x00};
1478 uint8_t atqaPresent
= 1;
1483 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1484 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1485 PrintAndLog("sample: hf mf csetuid 01020304");
1486 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1487 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1488 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1492 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1493 PrintAndLog("UID must include 8 HEX symbols");
1498 ctmp
= param_getchar(Cmd
, argi
);
1499 if (ctmp
== 'w' || ctmp
== 'W') {
1505 if (param_getchar(Cmd
, argi
)) {
1506 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1507 PrintAndLog("ATQA must include 4 HEX symbols");
1511 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1512 PrintAndLog("SAK must include 2 HEX symbols");
1521 ctmp
= param_getchar(Cmd
, argi
);
1522 if (ctmp
== 'w' || ctmp
== 'W') {
1527 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1529 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1531 PrintAndLog("Can't set UID. error=%d", res
);
1535 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1536 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1540 int CmdHF14AMfCSetBlk(const char *Cmd
)
1542 uint8_t memBlock
[16] = {0x00};
1543 uint8_t blockNo
= 0;
1544 bool wipeCard
= FALSE
;
1547 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1548 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1549 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1550 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1551 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1555 blockNo
= param_get8(Cmd
, 0);
1557 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1558 PrintAndLog("block data must include 32 HEX symbols");
1562 char ctmp
= param_getchar(Cmd
, 2);
1563 wipeCard
= (ctmp
== 'w' || ctmp
== 'W');
1564 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16));
1566 res
= mfCSetBlock(blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1568 PrintAndLog("Can't write block. error=%d", res
);
1575 int CmdHF14AMfCLoad(const char *Cmd
)
1578 char filename
[FILE_PATH_SIZE
] = {0x00};
1579 char * fnameptr
= filename
;
1580 char buf
[64] = {0x00};
1581 uint8_t buf8
[64] = {0x00};
1582 uint8_t fillFromEmulator
= 0;
1583 int i
, len
, blockNum
, flags
=0;
1585 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1586 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1587 PrintAndLog("or from emulator memory (option `e`)");
1588 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1589 PrintAndLog(" or: hf mf cload e ");
1590 PrintAndLog(" sample: hf mf cload filename");
1594 char ctmp
= param_getchar(Cmd
, 0);
1595 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1597 if (fillFromEmulator
) {
1598 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1599 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1600 PrintAndLog("Cant get block: %d", blockNum
);
1603 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1604 if (blockNum
== 1) flags
= 0; // just write
1605 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1607 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1608 PrintAndLog("Cant set magic card block: %d", blockNum
);
1615 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1617 memcpy(filename
, Cmd
, len
);
1620 sprintf(fnameptr
, ".eml");
1623 f
= fopen(filename
, "r");
1625 PrintAndLog("File not found or locked.");
1632 memset(buf
, 0, sizeof(buf
));
1634 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1636 PrintAndLog("File reading error.");
1640 if (strlen(buf
) < 32) {
1641 if(strlen(buf
) && feof(f
))
1643 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1647 for (i
= 0; i
< 32; i
+= 2)
1648 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1650 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1651 if (blockNum
== 1) flags
= 0; // just write
1652 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1654 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1655 PrintAndLog("Can't set magic card block: %d", blockNum
);
1660 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1664 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1665 PrintAndLog("File content error. There must be 64 blocks");
1668 PrintAndLog("Loaded from file: %s", filename
);
1674 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1675 uint8_t memBlock
[16];
1676 uint8_t blockNo
= 0;
1678 memset(memBlock
, 0x00, sizeof(memBlock
));
1680 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1681 PrintAndLog("Usage: hf mf cgetblk <block number>");
1682 PrintAndLog("sample: hf mf cgetblk 1");
1683 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1687 blockNo
= param_get8(Cmd
, 0);
1689 PrintAndLog("--block number:%2d ", blockNo
);
1691 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1693 PrintAndLog("Can't read block. error=%d", res
);
1697 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1702 int CmdHF14AMfCGetSc(const char *Cmd
) {
1703 uint8_t memBlock
[16] = {0x00};
1704 uint8_t sectorNo
= 0;
1707 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1708 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1709 PrintAndLog("sample: hf mf cgetsc 0");
1710 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1714 sectorNo
= param_get8(Cmd
, 0);
1715 if (sectorNo
> 15) {
1716 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1720 PrintAndLog("--sector number:%d ", sectorNo
);
1722 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1723 for (i
= 0; i
< 4; i
++) {
1724 if (i
== 1) flags
= 0;
1725 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1727 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1729 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1733 PrintAndLog("block %3d data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1739 int CmdHF14AMfCSave(const char *Cmd
) {
1742 char filename
[FILE_PATH_SIZE
] = {0x00};
1743 char * fnameptr
= filename
;
1744 uint8_t fillFromEmulator
= 0;
1745 uint8_t buf
[64] = {0x00};
1746 int i
, j
, len
, flags
;
1748 // memset(filename, 0, sizeof(filename));
1749 // memset(buf, 0, sizeof(buf));
1751 if (param_getchar(Cmd
, 0) == 'h') {
1752 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1753 PrintAndLog("or into emulator memory (option `e`)");
1754 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1755 PrintAndLog(" sample: hf mf esave ");
1756 PrintAndLog(" hf mf esave filename");
1757 PrintAndLog(" hf mf esave e \n");
1761 char ctmp
= param_getchar(Cmd
, 0);
1762 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1764 if (fillFromEmulator
) {
1765 // put into emulator
1766 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1767 for (i
= 0; i
< 16 * 4; i
++) {
1768 if (i
== 1) flags
= 0;
1769 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1771 if (mfCGetBlock(i
, buf
, flags
)) {
1772 PrintAndLog("Cant get block: %d", i
);
1776 if (mfEmlSetMem(buf
, i
, 1)) {
1777 PrintAndLog("Cant set emul block: %d", i
);
1784 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1788 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1789 PrintAndLog("Cant get block: %d", 0);
1790 len
= sprintf(fnameptr
, "dump");
1793 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1794 sprintf(fnameptr
, "%02x", buf
[j
]);
1797 memcpy(filename
, Cmd
, len
);
1801 sprintf(fnameptr
, ".eml");
1804 f
= fopen(filename
, "w+");
1807 PrintAndLog("File not found or locked.");
1812 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1813 for (i
= 0; i
< 16 * 4; i
++) {
1814 if (i
== 1) flags
= 0;
1815 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1817 if (mfCGetBlock(i
, buf
, flags
)) {
1818 PrintAndLog("Cant get block: %d", i
);
1821 for (j
= 0; j
< 16; j
++)
1822 fprintf(f
, "%02x", buf
[j
]);
1827 PrintAndLog("Saved to file: %s", filename
);
1834 int CmdHF14AMfSniff(const char *Cmd
){
1836 bool wantLogToFile
= 0;
1837 bool wantDecrypt
= 0;
1838 //bool wantSaveToEml = 0; TODO
1839 bool wantSaveToEmlFile
= 0;
1849 uint8_t atqa
[2] = {0x00};
1852 uint8_t *buf
= NULL
;
1853 uint16_t bufsize
= 0;
1854 uint8_t *bufPtr
= NULL
;
1856 char ctmp
= param_getchar(Cmd
, 0);
1857 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1858 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1859 PrintAndLog("You can specify:");
1860 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1861 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1862 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1863 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1864 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1865 PrintAndLog(" sample: hf mf sniff l d e");
1869 for (int i
= 0; i
< 4; i
++) {
1870 ctmp
= param_getchar(Cmd
, i
);
1871 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1872 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1873 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1874 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1877 printf("-------------------------------------------------------------------------\n");
1878 printf("Executing command. \n");
1879 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1880 printf("Press the key on pc keyboard to abort the client.\n");
1881 printf("-------------------------------------------------------------------------\n");
1883 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1884 clearCommandBuffer();
1893 printf("\naborted via keyboard!\n");
1898 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1899 res
= resp
.arg
[0] & 0xff;
1900 uint16_t traceLen
= resp
.arg
[1];
1903 if (res
== 0) return 0; // we are done
1905 if (res
== 1) { // there is (more) data to be transferred
1906 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1907 if (traceLen
> bufsize
) {
1909 if (buf
== NULL
) { // not yet allocated
1910 p
= malloc(traceLen
);
1911 } else { // need more memory
1912 p
= realloc(buf
, traceLen
);
1915 PrintAndLog("Cannot allocate memory for trace");
1923 memset(buf
, 0x00, traceLen
);
1925 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1930 if (res
== 2) { // received all data, start displaying
1931 blockLen
= bufPtr
- buf
;
1934 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1935 while (bufPtr
- buf
< blockLen
) {
1936 bufPtr
+= 6; // skip (void) timing information
1937 len
= *((uint16_t *)bufPtr
);
1945 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
1946 memcpy(uid
, bufPtr
+ 2, 7);
1947 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1948 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
1950 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1951 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
1955 if (wantLogToFile
|| wantDecrypt
) {
1956 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
1957 AddLogCurrentDT(logHexFileName
);
1960 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1962 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1964 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1966 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1970 bufPtr
+= ((len
-1)/8+1); // ignore parity
1982 static command_t CommandTable
[] =
1984 {"help", CmdHelp
, 1, "This help"},
1985 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1986 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1987 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1988 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1989 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1990 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1991 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1992 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1993 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1994 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1995 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1996 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1997 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1998 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1999 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2000 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2001 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2002 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2003 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2004 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2005 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2006 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2007 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2008 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2009 {NULL
, NULL
, 0, NULL
}
2012 int CmdHFMF(const char *Cmd
)
2015 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
2017 CmdsParse(CommandTable
, Cmd
);
2021 int CmdHelp(const char *Cmd
)
2023 CmdsHelp(CommandTable
);