]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
58d2061af30cfc7f15bd59d39217da93dfb3746e
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;
22 uint8_t keyBlock
[8] = {0};
24 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
27 printf("-------------------------------------------------------------------------\n");
28 printf("Executing command. Expected execution time: 25sec on average :-)\n");
29 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
30 printf("-------------------------------------------------------------------------\n");
38 while (ukbhit()) getchar();
46 printf("\naborted via keyboard!\n");
51 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
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);
60 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
61 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
62 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
72 if (isOK
!= 1) return 1;
74 // execute original function from util nonce2key
75 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
77 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
79 printf("------------------------------------------------------------------\n");
80 PrintAndLog("Key found:%012"llx
" \n", r_key
);
82 num_to_bytes(r_key
, 6, keyBlock
);
83 isOK
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
);
87 PrintAndLog("Found valid key:%012"llx
, r_key
);
90 if (isOK
!= 2) PrintAndLog("Found invalid key. ");
91 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
100 int CmdHF14AMfWrBl(const char *Cmd
)
104 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
105 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
110 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
111 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
115 blockNo
= param_get8(Cmd
, 0);
116 cmdp
= param_getchar(Cmd
, 1);
118 PrintAndLog("Key type must be A or B");
121 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
122 if (param_gethex(Cmd
, 2, key
, 12)) {
123 PrintAndLog("Key must include 12 HEX symbols");
126 if (param_gethex(Cmd
, 3, bldata
, 32)) {
127 PrintAndLog("Block data must include 32 HEX symbols");
130 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
131 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
133 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
134 memcpy(c
.d
.asBytes
, key
, 6);
135 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
139 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
140 uint8_t isOK
= resp
.arg
[0] & 0xff;
141 PrintAndLog("isOk:%02x", isOK
);
143 PrintAndLog("Command execute timeout");
149 int CmdHF14AMfRdBl(const char *Cmd
)
153 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
159 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
160 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
164 blockNo
= param_get8(Cmd
, 0);
165 cmdp
= param_getchar(Cmd
, 1);
167 PrintAndLog("Key type must be A or B");
170 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
171 if (param_gethex(Cmd
, 2, key
, 12)) {
172 PrintAndLog("Key must include 12 HEX symbols");
175 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
177 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
178 memcpy(c
.d
.asBytes
, key
, 6);
182 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
183 uint8_t isOK
= resp
.arg
[0] & 0xff;
184 uint8_t *data
= resp
.d
.asBytes
;
187 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
189 PrintAndLog("isOk:%02x", isOK
);
191 PrintAndLog("Command execute timeout");
197 int CmdHF14AMfRdSc(const char *Cmd
)
200 uint8_t sectorNo
= 0;
202 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
204 uint8_t *data
= NULL
;
208 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
209 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
213 sectorNo
= param_get8(Cmd
, 0);
215 PrintAndLog("Sector number must be less than 40");
218 cmdp
= param_getchar(Cmd
, 1);
219 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
220 PrintAndLog("Key type must be A or B");
223 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
224 if (param_gethex(Cmd
, 2, key
, 12)) {
225 PrintAndLog("Key must include 12 HEX symbols");
228 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
230 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
231 memcpy(c
.d
.asBytes
, key
, 6);
236 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
237 isOK
= resp
.arg
[0] & 0xff;
238 data
= resp
.d
.asBytes
;
240 PrintAndLog("isOk:%02x", isOK
);
242 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
243 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
245 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
248 PrintAndLog("Command execute timeout");
254 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
259 return 32 * 4 + (sectorNo
- 32) * 16;
263 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
272 int CmdHF14AMfDump(const char *Cmd
)
274 uint8_t sectorNo
, blockNo
;
278 uint8_t rights
[40][4];
279 uint8_t carddata
[256][16];
280 uint8_t numSectors
= 16;
287 char cmdp
= param_getchar(Cmd
, 0);
289 case '0' : numSectors
= 5; break;
291 case '\0': numSectors
= 16; break;
292 case '2' : numSectors
= 32; break;
293 case '4' : numSectors
= 40; break;
294 default: numSectors
= 16;
297 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
298 PrintAndLog("Usage: hf mf dump [card memory]");
299 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
301 PrintAndLog("Samples: hf mf dump");
302 PrintAndLog(" hf mf dump 4");
306 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
307 PrintAndLog("Could not find file dumpkeys.bin");
311 // Read keys A from file
312 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
313 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
314 PrintAndLog("File reading error.");
320 // Read keys B from file
321 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
322 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
323 PrintAndLog("File reading error.");
331 PrintAndLog("|-----------------------------------------|");
332 PrintAndLog("|------ Reading sector access bits...-----|");
333 PrintAndLog("|-----------------------------------------|");
335 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
336 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
337 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
340 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
341 uint8_t isOK
= resp
.arg
[0] & 0xff;
342 uint8_t *data
= resp
.d
.asBytes
;
344 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
345 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
346 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
347 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
349 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
350 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
351 rights
[sectorNo
][3] = 0x01;
354 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
355 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
356 rights
[sectorNo
][3] = 0x01;
360 PrintAndLog("|-----------------------------------------|");
361 PrintAndLog("|----- Dumping all blocks to file... -----|");
362 PrintAndLog("|-----------------------------------------|");
365 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
366 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
367 bool received
= false;
369 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
370 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
371 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
373 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
374 } else { // data block. Check if it can be read with key A or key B
375 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
376 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
377 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
378 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
380 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
381 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
383 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
384 } else { // key A would work
385 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
386 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
388 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
393 isOK
= resp
.arg
[0] & 0xff;
394 uint8_t *data
= resp
.d
.asBytes
;
395 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
396 data
[0] = (keyA
[sectorNo
][0]);
397 data
[1] = (keyA
[sectorNo
][1]);
398 data
[2] = (keyA
[sectorNo
][2]);
399 data
[3] = (keyA
[sectorNo
][3]);
400 data
[4] = (keyA
[sectorNo
][4]);
401 data
[5] = (keyA
[sectorNo
][5]);
402 data
[10] = (keyB
[sectorNo
][0]);
403 data
[11] = (keyB
[sectorNo
][1]);
404 data
[12] = (keyB
[sectorNo
][2]);
405 data
[13] = (keyB
[sectorNo
][3]);
406 data
[14] = (keyB
[sectorNo
][4]);
407 data
[15] = (keyB
[sectorNo
][5]);
410 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
411 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
413 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
419 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
426 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
427 PrintAndLog("Could not create file name dumpdata.bin");
430 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
431 fwrite(carddata
, 1, 16*numblocks
, fout
);
433 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
439 int CmdHF14AMfRestore(const char *Cmd
)
441 uint8_t sectorNo
,blockNo
;
443 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
444 uint8_t bldata
[16] = {0x00};
452 char cmdp
= param_getchar(Cmd
, 0);
454 case '0' : numSectors
= 5; break;
456 case '\0': numSectors
= 16; break;
457 case '2' : numSectors
= 32; break;
458 case '4' : numSectors
= 40; break;
459 default: numSectors
= 16;
462 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
463 PrintAndLog("Usage: hf mf restore [card memory]");
464 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
466 PrintAndLog("Samples: hf mf restore");
467 PrintAndLog(" hf mf restore 4");
471 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
472 PrintAndLog("Could not find file dumpkeys.bin");
476 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
477 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
478 PrintAndLog("File reading error (dumpkeys.bin).");
485 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
486 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
487 PrintAndLog("File reading error (dumpkeys.bin).");
495 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
496 PrintAndLog("Could not find file dumpdata.bin");
499 PrintAndLog("Restoring dumpdata.bin to card");
501 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
502 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
503 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
504 memcpy(c
.d
.asBytes
, key
, 6);
506 if (fread(bldata
, 1, 16, fdump
) == 0) {
507 PrintAndLog("File reading error (dumpdata.bin).");
512 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
513 bldata
[0] = (keyA
[sectorNo
][0]);
514 bldata
[1] = (keyA
[sectorNo
][1]);
515 bldata
[2] = (keyA
[sectorNo
][2]);
516 bldata
[3] = (keyA
[sectorNo
][3]);
517 bldata
[4] = (keyA
[sectorNo
][4]);
518 bldata
[5] = (keyA
[sectorNo
][5]);
519 bldata
[10] = (keyB
[sectorNo
][0]);
520 bldata
[11] = (keyB
[sectorNo
][1]);
521 bldata
[12] = (keyB
[sectorNo
][2]);
522 bldata
[13] = (keyB
[sectorNo
][3]);
523 bldata
[14] = (keyB
[sectorNo
][4]);
524 bldata
[15] = (keyB
[sectorNo
][5]);
527 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
529 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
533 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
534 uint8_t isOK
= resp
.arg
[0] & 0xff;
535 PrintAndLog("isOk:%02x", isOK
);
537 PrintAndLog("Command execute timeout");
546 int CmdHF14AMfNested(const char *Cmd
)
548 int i
, j
, res
, iterations
;
549 sector
*e_sector
= NULL
;
552 uint8_t trgBlockNo
= 0;
553 uint8_t trgKeyType
= 0;
554 uint8_t SectorsCnt
= 0;
555 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
556 uint8_t keyBlock
[14*6];
558 bool transferToEml
= false;
560 bool createDumpFile
= false;
562 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
563 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
568 PrintAndLog("Usage:");
569 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
570 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
571 PrintAndLog(" <target block number> <target key A/B> [t]");
572 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
573 PrintAndLog("t - transfer keys into emulator memory");
574 PrintAndLog("d - write keys to binary file");
576 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
577 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
578 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
579 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
583 cmdp
= param_getchar(Cmd
, 0);
584 blockNo
= param_get8(Cmd
, 1);
585 ctmp
= param_getchar(Cmd
, 2);
587 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
588 PrintAndLog("Key type must be A or B");
592 if (ctmp
!= 'A' && ctmp
!= 'a')
595 if (param_gethex(Cmd
, 3, key
, 12)) {
596 PrintAndLog("Key must include 12 HEX symbols");
600 if (cmdp
== 'o' || cmdp
== 'O') {
602 trgBlockNo
= param_get8(Cmd
, 4);
603 ctmp
= param_getchar(Cmd
, 5);
604 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
605 PrintAndLog("Target key type must be A or B");
608 if (ctmp
!= 'A' && ctmp
!= 'a')
613 case '0': SectorsCnt
= 05; break;
614 case '1': SectorsCnt
= 16; break;
615 case '2': SectorsCnt
= 32; break;
616 case '4': SectorsCnt
= 40; break;
617 default: SectorsCnt
= 16;
621 ctmp
= param_getchar(Cmd
, 4);
622 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
623 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
625 ctmp
= param_getchar(Cmd
, 6);
626 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
627 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
630 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
631 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
634 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
635 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
636 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
637 default : PrintAndLog("Unknown Error.\n");
641 key64
= bytes_to_num(keyBlock
, 6);
643 PrintAndLog("Found valid key:%012"llx
, key64
);
645 // transfer key to the emulator
647 uint8_t sectortrailer
;
648 if (trgBlockNo
< 32*4) { // 4 block sector
649 sectortrailer
= (trgBlockNo
& 0x03) + 3;
650 } else { // 16 block sector
651 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
653 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
656 num_to_bytes(key64
, 6, keyBlock
);
658 num_to_bytes(key64
, 6, &keyBlock
[10]);
659 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
662 PrintAndLog("No valid key found");
665 else { // ------------------------------------ multiple sectors working
669 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
670 if (e_sector
== NULL
) return 1;
672 //test current key and additional standard keys first
673 memcpy(keyBlock
, key
, 6);
674 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
675 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
676 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
677 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
678 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
679 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
680 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
681 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
682 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
683 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
684 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
685 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
686 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
688 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
689 for (i
= 0; i
< SectorsCnt
; i
++) {
690 for (j
= 0; j
< 2; j
++) {
691 if (e_sector
[i
].foundKey
[j
]) continue;
693 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, 6, keyBlock
, &key64
);
696 e_sector
[i
].Key
[j
] = key64
;
697 e_sector
[i
].foundKey
[j
] = 1;
704 PrintAndLog("nested...");
705 bool calibrate
= true;
706 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
707 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
708 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
709 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
710 PrintAndLog("-----------------------------------------------");
711 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
714 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
715 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
716 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
717 default : PrintAndLog("Unknown Error.\n");
727 key64
= bytes_to_num(keyBlock
, 6);
729 PrintAndLog("Found valid key:%012"llx
, key64
);
730 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
731 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
737 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
);
739 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
741 PrintAndLog("|---|----------------|---|----------------|---|");
742 PrintAndLog("|sec|key A |res|key B |res|");
743 PrintAndLog("|---|----------------|---|----------------|---|");
744 for (i
= 0; i
< SectorsCnt
; i
++) {
745 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
746 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
748 PrintAndLog("|---|----------------|---|----------------|---|");
750 // transfer them to the emulator
752 for (i
= 0; i
< SectorsCnt
; i
++) {
753 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
754 if (e_sector
[i
].foundKey
[0])
755 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
756 if (e_sector
[i
].foundKey
[1])
757 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
758 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
763 if (createDumpFile
) {
764 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
765 PrintAndLog("Could not create file dumpkeys.bin");
769 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
770 for(i
=0; i
<SectorsCnt
; i
++) {
771 if (e_sector
[i
].foundKey
[0]){
772 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
773 fwrite ( tempkey
, 1, 6, fkeys
);
776 fwrite ( &standart
, 1, 6, fkeys
);
779 for(i
=0; i
<SectorsCnt
; i
++) {
780 if (e_sector
[i
].foundKey
[1]){
781 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
782 fwrite ( tempkey
, 1, 6, fkeys
);
785 fwrite ( &standart
, 1, 6, fkeys
);
796 int CmdHF14AMfChk(const char *Cmd
)
799 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
800 PrintAndLog(" * - all sectors");
801 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
802 PrintAndLog("d - write keys to binary file");
803 PrintAndLog("t - write keys to emulator memory\n");
804 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
805 PrintAndLog(" hf mf chk *1 ? t");
806 PrintAndLog(" hf mf chk *1 ? d");
811 char filename
[FILE_PATH_SIZE
]={0};
813 uint8_t *keyBlock
= NULL
, *p
;
814 uint8_t stKeyBlock
= 20;
820 uint8_t SectorsCnt
= 1;
824 int transferToEml
= 0;
825 int createDumpFile
= 0;
827 keyBlock
= calloc(stKeyBlock
, 6);
828 if (keyBlock
== NULL
) return 1;
830 uint64_t defaultKeys
[] =
832 0xffffffffffff, // Default key (first key used by program if no user defined key)
833 0x000000000000, // Blank key
834 0xa0a1a2a3a4a5, // NFCForum MAD key
846 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
848 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
850 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
853 if (param_getchar(Cmd
, 0)=='*') {
855 switch(param_getchar(Cmd
+1, 0)) {
856 case '0': SectorsCnt
= 5; break;
857 case '1': SectorsCnt
= 16; break;
858 case '2': SectorsCnt
= 32; break;
859 case '4': SectorsCnt
= 40; break;
860 default: SectorsCnt
= 16;
864 blockNo
= param_get8(Cmd
, 0);
866 ctmp
= param_getchar(Cmd
, 1);
878 PrintAndLog("Key type must be A , B or ?");
882 ctmp
= param_getchar(Cmd
, 2);
883 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
884 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
886 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
887 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
888 if ( stKeyBlock
- keycnt
< 2) {
889 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
891 PrintAndLog("Cannot allocate memory for Keys");
897 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
898 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
899 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
903 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
904 PrintAndLog("File name too long");
909 if ( (f
= fopen( filename
, "r")) ) {
910 while( fgets(buf
, sizeof(buf
), f
) ){
911 if (strlen(buf
) < 12 || buf
[11] == '\n')
914 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
916 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
918 if (!isxdigit(buf
[0])){
919 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
925 if ( stKeyBlock
- keycnt
< 2) {
926 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
928 PrintAndLog("Cannot allocate memory for defKeys");
934 memset(keyBlock
+ 6 * keycnt
, 0, 6);
935 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
936 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
938 memset(buf
, 0, sizeof(buf
));
942 PrintAndLog("File: %s: not found or locked.", filename
);
951 PrintAndLog("No key specified, trying default keys");
952 for (;keycnt
< defaultKeysSize
; keycnt
++)
953 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
954 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
955 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
958 // initialize storage for found keys
959 bool validKey
[2][40];
960 uint8_t foundKey
[2][40][6];
961 for (uint16_t t
= 0; t
< 2; t
++) {
962 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
963 validKey
[t
][sectorNo
] = false;
964 for (uint16_t i
= 0; i
< 6; i
++) {
965 foundKey
[t
][sectorNo
][i
] = 0xff;
970 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
972 for (int i
= 0; i
< SectorsCnt
; ++i
) {
973 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
974 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
975 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
976 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
977 res
= mfCheckKeys(b
, t
, size
, &keyBlock
[6*c
], &key64
);
980 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
981 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
982 validKey
[t
][i
] = true;
985 PrintAndLog("Command execute timeout");
988 b
<127?(b
+=4):(b
+=16);
994 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
995 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
996 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
997 for (uint16_t t
= 0; t
< 2; t
++) {
998 if (validKey
[t
][sectorNo
]) {
999 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1002 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1005 PrintAndLog("Found keys have been transferred to the emulator memory");
1008 if (createDumpFile
) {
1009 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1010 if (fkeys
== NULL
) {
1011 PrintAndLog("Could not create file dumpkeys.bin");
1015 for (uint16_t t
= 0; t
< 2; t
++) {
1016 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1019 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1027 int CmdHF14AMf1kSim(const char *Cmd
)
1029 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1030 uint8_t exitAfterNReads
= 0;
1033 uint8_t cmdp
= param_getchar(Cmd
, 0);
1035 clearCommandBuffer();
1037 if (cmdp
== 'h' || cmdp
== 'H') {
1038 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1039 PrintAndLog(" h this help");
1040 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1041 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1042 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1043 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1045 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1049 if (param_getchar(Cmd
, pnr
) == 'u') {
1050 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1052 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1053 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1054 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1056 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1061 if (param_getchar(Cmd
, pnr
) == 'n') {
1062 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1065 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1066 //Using a flag to signal interactiveness, least significant bit
1067 flags
|= FLAG_INTERACTIVE
;
1071 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1072 //Using a flag to signal interactiveness, least significant bit
1073 flags
|= FLAG_NR_AR_ATTACK
;
1075 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1076 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1077 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1078 , exitAfterNReads
, flags
,flags
);
1081 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1082 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1085 if(flags
& FLAG_INTERACTIVE
)
1087 PrintAndLog("Press pm3-button to abort simulation");
1094 if ( WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1095 if ( (resp
.arg
[0] & 0xffff) == CMD_SIMULATE_MIFARE_CARD
){
1096 memset(data
, 0x00, sizeof(data
));
1097 memset(key
, 0x00, sizeof(key
));
1098 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1100 memcpy(data
, resp
.d
.asBytes
, len
);
1102 uint64_t corr_uid
= 0;
1103 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1104 corr_uid
= (data
[3] << 24) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1107 corr_uid
|= (uint64_t)data
[2] << 48;
1108 corr_uid
|= (uint64_t)data
[1] << 40;
1109 corr_uid
|= (uint64_t)data
[0] << 32;
1110 corr_uid
|= data
[7] << 24;
1111 corr_uid
|= data
[6] << 16;
1112 corr_uid
|= data
[5] << 8;
1113 corr_uid
|= data
[4];
1115 tryMfk32(corr_uid
, data
, key
);
1116 //tryMfk64(corr_uid, data, key);
1125 int CmdHF14AMfDbg(const char *Cmd
)
1127 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1129 PrintAndLog("Max debug mode parameter is 4 \n");
1132 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1133 PrintAndLog("Usage: hf mf dbg <debug level>");
1134 PrintAndLog(" 0 - no debug messages");
1135 PrintAndLog(" 1 - error messages");
1136 PrintAndLog(" 2 - plus information messages");
1137 PrintAndLog(" 3 - plus debug messages");
1138 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1139 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1143 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1149 int CmdHF14AMfEGet(const char *Cmd
)
1151 uint8_t blockNo
= 0;
1152 uint8_t data
[16] = {0x00};
1154 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1155 PrintAndLog("Usage: hf mf eget <block number>");
1156 PrintAndLog(" sample: hf mf eget 0 ");
1160 blockNo
= param_get8(Cmd
, 0);
1163 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1164 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1166 PrintAndLog("Command execute timeout");
1172 int CmdHF14AMfEClear(const char *Cmd
)
1174 if (param_getchar(Cmd
, 0) == 'h') {
1175 PrintAndLog("Usage: hf mf eclr");
1176 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1180 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1186 int CmdHF14AMfESet(const char *Cmd
)
1188 uint8_t memBlock
[16];
1189 uint8_t blockNo
= 0;
1191 memset(memBlock
, 0x00, sizeof(memBlock
));
1193 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1194 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1195 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1199 blockNo
= param_get8(Cmd
, 0);
1201 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1202 PrintAndLog("block data must include 32 HEX symbols");
1207 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1208 memcpy(c
.d
.asBytes
, memBlock
, 16);
1214 int CmdHF14AMfELoad(const char *Cmd
)
1217 char filename
[FILE_PATH_SIZE
];
1218 char *fnameptr
= filename
;
1219 char buf
[64] = {0x00};
1220 uint8_t buf8
[64] = {0x00};
1221 int i
, len
, blockNum
, numBlocks
;
1222 int nameParamNo
= 1;
1224 char ctmp
= param_getchar(Cmd
, 0);
1226 if ( ctmp
== 'h' || ctmp
== 0x00) {
1227 PrintAndLog("It loads emul dump from the file `filename.eml`");
1228 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1229 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1231 PrintAndLog(" sample: hf mf eload filename");
1232 PrintAndLog(" hf mf eload 4 filename");
1237 case '0' : numBlocks
= 5*4; break;
1239 case '\0': numBlocks
= 16*4; break;
1240 case '2' : numBlocks
= 32*4; break;
1241 case '4' : numBlocks
= 256; break;
1248 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1250 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1254 sprintf(fnameptr
, ".eml");
1257 f
= fopen(filename
, "r");
1259 PrintAndLog("File %s not found or locked", filename
);
1265 memset(buf
, 0, sizeof(buf
));
1267 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1269 if (blockNum
>= numBlocks
) break;
1271 PrintAndLog("File reading error.");
1276 if (strlen(buf
) < 32){
1277 if(strlen(buf
) && feof(f
))
1279 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1284 for (i
= 0; i
< 32; i
+= 2) {
1285 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1288 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1289 PrintAndLog("Cant set emul block: %3d", blockNum
);
1296 if (blockNum
>= numBlocks
) break;
1301 if ((blockNum
!= numBlocks
)) {
1302 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1305 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1310 int CmdHF14AMfESave(const char *Cmd
)
1313 char filename
[FILE_PATH_SIZE
];
1314 char * fnameptr
= filename
;
1316 int i
, j
, len
, numBlocks
;
1317 int nameParamNo
= 1;
1319 memset(filename
, 0, sizeof(filename
));
1320 memset(buf
, 0, sizeof(buf
));
1322 char ctmp
= param_getchar(Cmd
, 0);
1324 if ( ctmp
== 'h' || ctmp
== 'H') {
1325 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1326 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1327 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1329 PrintAndLog(" sample: hf mf esave ");
1330 PrintAndLog(" hf mf esave 4");
1331 PrintAndLog(" hf mf esave 4 filename");
1336 case '0' : numBlocks
= 5*4; break;
1338 case '\0': numBlocks
= 16*4; break;
1339 case '2' : numBlocks
= 32*4; break;
1340 case '4' : numBlocks
= 256; break;
1347 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1349 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1351 // user supplied filename?
1353 // get filename (UID from memory)
1354 if (mfEmlGetMem(buf
, 0, 1)) {
1355 PrintAndLog("Can\'t get UID from block: %d", 0);
1356 len
= sprintf(fnameptr
, "dump");
1360 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1361 sprintf(fnameptr
, "%02X", buf
[j
]);
1367 // add file extension
1368 sprintf(fnameptr
, ".eml");
1371 f
= fopen(filename
, "w+");
1374 PrintAndLog("Can't open file %s ", filename
);
1379 for (i
= 0; i
< numBlocks
; i
++) {
1380 if (mfEmlGetMem(buf
, i
, 1)) {
1381 PrintAndLog("Cant get block: %d", i
);
1384 for (j
= 0; j
< 16; j
++)
1385 fprintf(f
, "%02X", buf
[j
]);
1390 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1396 int CmdHF14AMfECFill(const char *Cmd
)
1398 uint8_t keyType
= 0;
1399 uint8_t numSectors
= 16;
1401 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1402 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1403 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1405 PrintAndLog("samples: hf mf ecfill A");
1406 PrintAndLog(" hf mf ecfill A 4");
1407 PrintAndLog("Read card and transfer its data to emulator memory.");
1408 PrintAndLog("Keys must be laid in the emulator memory. \n");
1412 char ctmp
= param_getchar(Cmd
, 0);
1413 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1414 PrintAndLog("Key type must be A or B");
1417 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1419 ctmp
= param_getchar(Cmd
, 1);
1421 case '0' : numSectors
= 5; break;
1423 case '\0': numSectors
= 16; break;
1424 case '2' : numSectors
= 32; break;
1425 case '4' : numSectors
= 40; break;
1426 default: numSectors
= 16;
1429 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1430 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1436 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1441 uint64_t keyA
, keyB
;
1443 if (param_getchar(Cmd
, 0) == 'h') {
1444 PrintAndLog("It prints the keys loaded in the emulator memory");
1445 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1446 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1448 PrintAndLog(" sample: hf mf ekeyprn 1");
1452 char cmdp
= param_getchar(Cmd
, 0);
1455 case '0' : numSectors
= 5; break;
1457 case '\0': numSectors
= 16; break;
1458 case '2' : numSectors
= 32; break;
1459 case '4' : numSectors
= 40; break;
1460 default: numSectors
= 16;
1463 PrintAndLog("|---|----------------|----------------|");
1464 PrintAndLog("|sec|key A |key B |");
1465 PrintAndLog("|---|----------------|----------------|");
1466 for (i
= 0; i
< numSectors
; i
++) {
1467 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1468 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1471 keyA
= bytes_to_num(data
, 6);
1472 keyB
= bytes_to_num(data
+ 10, 6);
1473 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1475 PrintAndLog("|---|----------------|----------------|");
1481 int CmdHF14AMfCSetUID(const char *Cmd
)
1483 uint8_t wipeCard
= 0;
1484 uint8_t uid
[8] = {0x00};
1485 uint8_t oldUid
[8] = {0x00};
1486 uint8_t atqa
[2] = {0x00};
1487 uint8_t sak
[1] = {0x00};
1488 uint8_t atqaPresent
= 1;
1493 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1494 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1495 PrintAndLog("sample: hf mf csetuid 01020304");
1496 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1497 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1498 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1502 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1503 PrintAndLog("UID must include 8 HEX symbols");
1508 ctmp
= param_getchar(Cmd
, argi
);
1509 if (ctmp
== 'w' || ctmp
== 'W') {
1515 if (param_getchar(Cmd
, argi
)) {
1516 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1517 PrintAndLog("ATQA must include 4 HEX symbols");
1521 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1522 PrintAndLog("SAK must include 2 HEX symbols");
1531 ctmp
= param_getchar(Cmd
, argi
);
1532 if (ctmp
== 'w' || ctmp
== 'W') {
1537 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1539 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1541 PrintAndLog("Can't set UID. error=%d", res
);
1545 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1546 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1550 int CmdHF14AMfCSetBlk(const char *Cmd
)
1552 uint8_t memBlock
[16] = {0x00};
1553 uint8_t blockNo
= 0;
1554 bool wipeCard
= FALSE
;
1557 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1558 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1559 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1560 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1561 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1565 blockNo
= param_get8(Cmd
, 0);
1567 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1568 PrintAndLog("block data must include 32 HEX symbols");
1572 char ctmp
= param_getchar(Cmd
, 2);
1573 wipeCard
= (ctmp
== 'w' || ctmp
== 'W');
1574 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16));
1576 res
= mfCSetBlock(blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1578 PrintAndLog("Can't write block. error=%d", res
);
1585 int CmdHF14AMfCLoad(const char *Cmd
)
1588 char filename
[FILE_PATH_SIZE
] = {0x00};
1589 char * fnameptr
= filename
;
1590 char buf
[64] = {0x00};
1591 uint8_t buf8
[64] = {0x00};
1592 uint8_t fillFromEmulator
= 0;
1593 int i
, len
, blockNum
, flags
=0;
1595 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1596 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1597 PrintAndLog("or from emulator memory (option `e`)");
1598 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1599 PrintAndLog(" or: hf mf cload e ");
1600 PrintAndLog(" sample: hf mf cload filename");
1604 char ctmp
= param_getchar(Cmd
, 0);
1605 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1607 if (fillFromEmulator
) {
1608 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1609 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1610 PrintAndLog("Cant get block: %d", blockNum
);
1613 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1614 if (blockNum
== 1) flags
= 0; // just write
1615 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1617 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1618 PrintAndLog("Cant set magic card block: %d", blockNum
);
1625 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1627 memcpy(filename
, Cmd
, len
);
1630 sprintf(fnameptr
, ".eml");
1633 f
= fopen(filename
, "r");
1635 PrintAndLog("File not found or locked.");
1642 memset(buf
, 0, sizeof(buf
));
1644 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1646 PrintAndLog("File reading error.");
1650 if (strlen(buf
) < 32) {
1651 if(strlen(buf
) && feof(f
))
1653 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1657 for (i
= 0; i
< 32; i
+= 2)
1658 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1660 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1661 if (blockNum
== 1) flags
= 0; // just write
1662 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1664 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1665 PrintAndLog("Can't set magic card block: %d", blockNum
);
1670 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1674 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1675 PrintAndLog("File content error. There must be 64 blocks");
1678 PrintAndLog("Loaded from file: %s", filename
);
1684 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1685 uint8_t memBlock
[16];
1686 uint8_t blockNo
= 0;
1688 memset(memBlock
, 0x00, sizeof(memBlock
));
1690 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1691 PrintAndLog("Usage: hf mf cgetblk <block number>");
1692 PrintAndLog("sample: hf mf cgetblk 1");
1693 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1697 blockNo
= param_get8(Cmd
, 0);
1699 PrintAndLog("--block number:%2d ", blockNo
);
1701 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1703 PrintAndLog("Can't read block. error=%d", res
);
1707 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1712 int CmdHF14AMfCGetSc(const char *Cmd
) {
1713 uint8_t memBlock
[16] = {0x00};
1714 uint8_t sectorNo
= 0;
1717 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1718 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1719 PrintAndLog("sample: hf mf cgetsc 0");
1720 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1724 sectorNo
= param_get8(Cmd
, 0);
1725 if (sectorNo
> 15) {
1726 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1730 PrintAndLog("--sector number:%d ", sectorNo
);
1732 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1733 for (i
= 0; i
< 4; i
++) {
1734 if (i
== 1) flags
= 0;
1735 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1737 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1739 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1743 PrintAndLog("block %3d data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1749 int CmdHF14AMfCSave(const char *Cmd
) {
1752 char filename
[FILE_PATH_SIZE
] = {0x00};
1753 char * fnameptr
= filename
;
1754 uint8_t fillFromEmulator
= 0;
1755 uint8_t buf
[64] = {0x00};
1756 int i
, j
, len
, flags
;
1758 // memset(filename, 0, sizeof(filename));
1759 // memset(buf, 0, sizeof(buf));
1761 if (param_getchar(Cmd
, 0) == 'h') {
1762 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1763 PrintAndLog("or into emulator memory (option `e`)");
1764 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1765 PrintAndLog(" sample: hf mf esave ");
1766 PrintAndLog(" hf mf esave filename");
1767 PrintAndLog(" hf mf esave e \n");
1771 char ctmp
= param_getchar(Cmd
, 0);
1772 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1774 if (fillFromEmulator
) {
1775 // put into emulator
1776 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1777 for (i
= 0; i
< 16 * 4; i
++) {
1778 if (i
== 1) flags
= 0;
1779 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1781 if (mfCGetBlock(i
, buf
, flags
)) {
1782 PrintAndLog("Cant get block: %d", i
);
1786 if (mfEmlSetMem(buf
, i
, 1)) {
1787 PrintAndLog("Cant set emul block: %d", i
);
1794 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1798 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1799 PrintAndLog("Cant get block: %d", 0);
1800 len
= sprintf(fnameptr
, "dump");
1803 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1804 sprintf(fnameptr
, "%02x", buf
[j
]);
1807 memcpy(filename
, Cmd
, len
);
1811 sprintf(fnameptr
, ".eml");
1814 f
= fopen(filename
, "w+");
1817 PrintAndLog("File not found or locked.");
1822 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1823 for (i
= 0; i
< 16 * 4; i
++) {
1824 if (i
== 1) flags
= 0;
1825 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1827 if (mfCGetBlock(i
, buf
, flags
)) {
1828 PrintAndLog("Cant get block: %d", i
);
1831 for (j
= 0; j
< 16; j
++)
1832 fprintf(f
, "%02x", buf
[j
]);
1837 PrintAndLog("Saved to file: %s", filename
);
1844 int CmdHF14AMfSniff(const char *Cmd
){
1846 bool wantLogToFile
= 0;
1847 bool wantDecrypt
= 0;
1848 //bool wantSaveToEml = 0; TODO
1849 bool wantSaveToEmlFile
= 0;
1859 uint8_t atqa
[2] = {0x00};
1862 uint8_t *buf
= NULL
;
1863 uint16_t bufsize
= 0;
1864 uint8_t *bufPtr
= NULL
;
1866 char ctmp
= param_getchar(Cmd
, 0);
1867 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1868 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1869 PrintAndLog("You can specify:");
1870 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1871 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1872 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1873 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1874 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1875 PrintAndLog(" sample: hf mf sniff l d e");
1879 for (int i
= 0; i
< 4; i
++) {
1880 ctmp
= param_getchar(Cmd
, i
);
1881 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1882 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1883 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1884 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1887 printf("-------------------------------------------------------------------------\n");
1888 printf("Executing command. \n");
1889 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1890 printf("Press the key on pc keyboard to abort the client.\n");
1891 printf("-------------------------------------------------------------------------\n");
1893 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1894 clearCommandBuffer();
1903 printf("\naborted via keyboard!\n");
1908 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1909 res
= resp
.arg
[0] & 0xff;
1910 uint16_t traceLen
= resp
.arg
[1];
1913 if (res
== 0) return 0; // we are done
1915 if (res
== 1) { // there is (more) data to be transferred
1916 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1917 if (traceLen
> bufsize
) {
1919 if (buf
== NULL
) { // not yet allocated
1920 p
= malloc(traceLen
);
1921 } else { // need more memory
1922 p
= realloc(buf
, traceLen
);
1925 PrintAndLog("Cannot allocate memory for trace");
1933 memset(buf
, 0x00, traceLen
);
1935 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1940 if (res
== 2) { // received all data, start displaying
1941 blockLen
= bufPtr
- buf
;
1944 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1945 while (bufPtr
- buf
< blockLen
) {
1946 bufPtr
+= 6; // skip (void) timing information
1947 len
= *((uint16_t *)bufPtr
);
1955 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
1956 memcpy(uid
, bufPtr
+ 2, 7);
1957 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1958 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
1960 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1961 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
1965 if (wantLogToFile
|| wantDecrypt
) {
1966 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
1967 AddLogCurrentDT(logHexFileName
);
1970 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1972 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1974 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1976 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1980 bufPtr
+= ((len
-1)/8+1); // ignore parity
1992 static command_t CommandTable
[] =
1994 {"help", CmdHelp
, 1, "This help"},
1995 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1996 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1997 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1998 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1999 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2000 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2001 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2002 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2003 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2004 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2005 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2006 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2007 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2008 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2009 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2010 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2011 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2012 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2013 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2014 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2015 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2016 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2017 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2018 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2019 {NULL
, NULL
, 0, NULL
}
2022 int CmdHFMF(const char *Cmd
)
2025 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
2027 CmdsParse(CommandTable
, Cmd
);
2031 int CmdHelp(const char *Cmd
)
2033 CmdsHelp(CommandTable
);