1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
15 int CmdHF14AMifare(const char *Cmd
)
18 uint32_t nt
= 0, nr
= 0;
19 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
22 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
25 printf("-------------------------------------------------------------------------\n");
26 printf("Executing command. Expected execution time: 25sec on average :-)\n");
27 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
28 printf("-------------------------------------------------------------------------\n");
36 while (ukbhit()) getchar();
44 printf("\naborted via keyboard!\n");
49 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)) {
51 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
52 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
53 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
54 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
55 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
58 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
59 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
60 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
61 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
62 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\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
);
78 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
83 printf("------------------------------------------------------------------\n");
84 PrintAndLog("Found valid key:%012"llx
" \n", r_key
);
91 int CmdHF14AMfWrBl(const char *Cmd
)
95 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
96 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
101 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
102 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
106 blockNo
= param_get8(Cmd
, 0);
107 cmdp
= param_getchar(Cmd
, 1);
109 PrintAndLog("Key type must be A or B");
112 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
113 if (param_gethex(Cmd
, 2, key
, 12)) {
114 PrintAndLog("Key must include 12 HEX symbols");
117 if (param_gethex(Cmd
, 3, bldata
, 32)) {
118 PrintAndLog("Block data must include 32 HEX symbols");
121 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
122 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
124 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
125 memcpy(c
.d
.asBytes
, key
, 6);
126 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
130 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
131 uint8_t isOK
= resp
.arg
[0] & 0xff;
132 PrintAndLog("isOk:%02x", isOK
);
134 PrintAndLog("Command execute timeout");
140 int CmdHF14AMfRdBl(const char *Cmd
)
144 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
150 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
151 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
155 blockNo
= param_get8(Cmd
, 0);
156 cmdp
= param_getchar(Cmd
, 1);
158 PrintAndLog("Key type must be A or B");
161 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
162 if (param_gethex(Cmd
, 2, key
, 12)) {
163 PrintAndLog("Key must include 12 HEX symbols");
166 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
168 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
169 memcpy(c
.d
.asBytes
, key
, 6);
173 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
174 uint8_t isOK
= resp
.arg
[0] & 0xff;
175 uint8_t *data
= resp
.d
.asBytes
;
178 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
180 PrintAndLog("isOk:%02x", isOK
);
182 PrintAndLog("Command execute timeout");
188 int CmdHF14AMfRdSc(const char *Cmd
)
191 uint8_t sectorNo
= 0;
193 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
195 uint8_t *data
= NULL
;
199 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
200 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
204 sectorNo
= param_get8(Cmd
, 0);
206 PrintAndLog("Sector number must be less than 40");
209 cmdp
= param_getchar(Cmd
, 1);
210 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
211 PrintAndLog("Key type must be A or B");
214 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
215 if (param_gethex(Cmd
, 2, key
, 12)) {
216 PrintAndLog("Key must include 12 HEX symbols");
219 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
221 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
222 memcpy(c
.d
.asBytes
, key
, 6);
227 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
228 isOK
= resp
.arg
[0] & 0xff;
229 data
= resp
.d
.asBytes
;
231 PrintAndLog("isOk:%02x", isOK
);
233 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
234 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
236 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
239 PrintAndLog("Command execute timeout");
245 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
250 return 32 * 4 + (sectorNo
- 32) * 16;
254 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
263 int CmdHF14AMfDump(const char *Cmd
)
265 uint8_t sectorNo
, blockNo
;
269 uint8_t rights
[40][4];
270 uint8_t carddata
[256][16];
271 uint8_t numSectors
= 16;
278 char cmdp
= param_getchar(Cmd
, 0);
280 case '0' : numSectors
= 5; break;
282 case '\0': numSectors
= 16; break;
283 case '2' : numSectors
= 32; break;
284 case '4' : numSectors
= 40; break;
285 default: numSectors
= 16;
288 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
289 PrintAndLog("Usage: hf mf dump [card memory]");
290 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
292 PrintAndLog("Samples: hf mf dump");
293 PrintAndLog(" hf mf dump 4");
297 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
298 PrintAndLog("Could not find file dumpkeys.bin");
302 // Read keys A from file
303 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
304 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
305 PrintAndLog("File reading error.");
311 // Read keys B from file
312 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
313 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
314 PrintAndLog("File reading error.");
322 PrintAndLog("|-----------------------------------------|");
323 PrintAndLog("|------ Reading sector access bits...-----|");
324 PrintAndLog("|-----------------------------------------|");
326 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
327 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
328 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
331 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
332 uint8_t isOK
= resp
.arg
[0] & 0xff;
333 uint8_t *data
= resp
.d
.asBytes
;
335 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
336 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
337 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
338 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
340 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
341 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
342 rights
[sectorNo
][3] = 0x01;
345 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
346 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
347 rights
[sectorNo
][3] = 0x01;
351 PrintAndLog("|-----------------------------------------|");
352 PrintAndLog("|----- Dumping all blocks to file... -----|");
353 PrintAndLog("|-----------------------------------------|");
356 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
357 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
358 bool received
= false;
360 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
361 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
362 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
364 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
365 } else { // data block. Check if it can be read with key A or key B
366 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
367 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
368 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
369 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
371 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
372 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
374 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
375 } else { // key A would work
376 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
377 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
379 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
384 isOK
= resp
.arg
[0] & 0xff;
385 uint8_t *data
= resp
.d
.asBytes
;
386 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
387 data
[0] = (keyA
[sectorNo
][0]);
388 data
[1] = (keyA
[sectorNo
][1]);
389 data
[2] = (keyA
[sectorNo
][2]);
390 data
[3] = (keyA
[sectorNo
][3]);
391 data
[4] = (keyA
[sectorNo
][4]);
392 data
[5] = (keyA
[sectorNo
][5]);
393 data
[10] = (keyB
[sectorNo
][0]);
394 data
[11] = (keyB
[sectorNo
][1]);
395 data
[12] = (keyB
[sectorNo
][2]);
396 data
[13] = (keyB
[sectorNo
][3]);
397 data
[14] = (keyB
[sectorNo
][4]);
398 data
[15] = (keyB
[sectorNo
][5]);
401 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
402 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
404 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
410 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
417 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
418 PrintAndLog("Could not create file name dumpdata.bin");
421 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
422 fwrite(carddata
, 1, 16*numblocks
, fout
);
424 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
430 int CmdHF14AMfRestore(const char *Cmd
)
432 uint8_t sectorNo
,blockNo
;
434 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
435 uint8_t bldata
[16] = {0x00};
443 char cmdp
= param_getchar(Cmd
, 0);
445 case '0' : numSectors
= 5; break;
447 case '\0': numSectors
= 16; break;
448 case '2' : numSectors
= 32; break;
449 case '4' : numSectors
= 40; break;
450 default: numSectors
= 16;
453 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
454 PrintAndLog("Usage: hf mf restore [card memory]");
455 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
457 PrintAndLog("Samples: hf mf restore");
458 PrintAndLog(" hf mf restore 4");
462 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
463 PrintAndLog("Could not find file dumpkeys.bin");
467 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
468 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
469 PrintAndLog("File reading error (dumpkeys.bin).");
476 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
477 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
478 PrintAndLog("File reading error (dumpkeys.bin).");
486 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
487 PrintAndLog("Could not find file dumpdata.bin");
490 PrintAndLog("Restoring dumpdata.bin to card");
492 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
493 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
494 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
495 memcpy(c
.d
.asBytes
, key
, 6);
497 if (fread(bldata
, 1, 16, fdump
) == 0) {
498 PrintAndLog("File reading error (dumpdata.bin).");
503 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
504 bldata
[0] = (keyA
[sectorNo
][0]);
505 bldata
[1] = (keyA
[sectorNo
][1]);
506 bldata
[2] = (keyA
[sectorNo
][2]);
507 bldata
[3] = (keyA
[sectorNo
][3]);
508 bldata
[4] = (keyA
[sectorNo
][4]);
509 bldata
[5] = (keyA
[sectorNo
][5]);
510 bldata
[10] = (keyB
[sectorNo
][0]);
511 bldata
[11] = (keyB
[sectorNo
][1]);
512 bldata
[12] = (keyB
[sectorNo
][2]);
513 bldata
[13] = (keyB
[sectorNo
][3]);
514 bldata
[14] = (keyB
[sectorNo
][4]);
515 bldata
[15] = (keyB
[sectorNo
][5]);
518 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
520 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
524 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
525 uint8_t isOK
= resp
.arg
[0] & 0xff;
526 PrintAndLog("isOk:%02x", isOK
);
528 PrintAndLog("Command execute timeout");
537 int CmdHF14AMfNested(const char *Cmd
)
539 int i
, j
, res
, iterations
;
540 sector
*e_sector
= NULL
;
543 uint8_t trgBlockNo
= 0;
544 uint8_t trgKeyType
= 0;
545 uint8_t SectorsCnt
= 0;
546 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
547 uint8_t keyBlock
[14*6];
549 bool transferToEml
= false;
551 bool createDumpFile
= false;
553 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
554 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
559 PrintAndLog("Usage:");
560 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
561 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
562 PrintAndLog(" <target block number> <target key A/B> [t]");
563 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
564 PrintAndLog("t - transfer keys into emulator memory");
565 PrintAndLog("d - write keys to binary file");
567 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
568 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
569 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
570 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
574 cmdp
= param_getchar(Cmd
, 0);
575 blockNo
= param_get8(Cmd
, 1);
576 ctmp
= param_getchar(Cmd
, 2);
578 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
579 PrintAndLog("Key type must be A or B");
583 if (ctmp
!= 'A' && ctmp
!= 'a')
586 if (param_gethex(Cmd
, 3, key
, 12)) {
587 PrintAndLog("Key must include 12 HEX symbols");
591 if (cmdp
== 'o' || cmdp
== 'O') {
593 trgBlockNo
= param_get8(Cmd
, 4);
594 ctmp
= param_getchar(Cmd
, 5);
595 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
596 PrintAndLog("Target key type must be A or B");
599 if (ctmp
!= 'A' && ctmp
!= 'a')
604 case '0': SectorsCnt
= 05; break;
605 case '1': SectorsCnt
= 16; break;
606 case '2': SectorsCnt
= 32; break;
607 case '4': SectorsCnt
= 40; break;
608 default: SectorsCnt
= 16;
612 ctmp
= param_getchar(Cmd
, 4);
613 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
614 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
616 ctmp
= param_getchar(Cmd
, 6);
617 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
618 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
621 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
622 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
625 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
626 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
627 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
628 default : PrintAndLog("Unknown Error.\n");
632 key64
= bytes_to_num(keyBlock
, 6);
634 PrintAndLog("Found valid key:%012"llx
, key64
);
636 // transfer key to the emulator
638 uint8_t sectortrailer
;
639 if (trgBlockNo
< 32*4) { // 4 block sector
640 sectortrailer
= (trgBlockNo
& 0x03) + 3;
641 } else { // 16 block sector
642 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
644 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
647 num_to_bytes(key64
, 6, keyBlock
);
649 num_to_bytes(key64
, 6, &keyBlock
[10]);
650 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
653 PrintAndLog("No valid key found");
656 else { // ------------------------------------ multiple sectors working
660 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
661 if (e_sector
== NULL
) return 1;
663 //test current key and additional standard keys first
664 memcpy(keyBlock
, key
, 6);
665 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
666 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
667 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
668 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
669 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
670 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
671 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
672 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
673 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
674 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
675 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
676 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
677 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
679 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
680 for (i
= 0; i
< SectorsCnt
; i
++) {
681 for (j
= 0; j
< 2; j
++) {
682 if (e_sector
[i
].foundKey
[j
]) continue;
684 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
687 e_sector
[i
].Key
[j
] = key64
;
688 e_sector
[i
].foundKey
[j
] = 1;
695 PrintAndLog("nested...");
696 bool calibrate
= true;
697 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
698 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
699 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
700 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
701 PrintAndLog("-----------------------------------------------");
702 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
705 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
706 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
707 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
708 default : PrintAndLog("Unknown Error.\n");
718 key64
= bytes_to_num(keyBlock
, 6);
720 PrintAndLog("Found valid key:%012"llx
, key64
);
721 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
722 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
728 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
);
730 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
732 PrintAndLog("|---|----------------|---|----------------|---|");
733 PrintAndLog("|sec|key A |res|key B |res|");
734 PrintAndLog("|---|----------------|---|----------------|---|");
735 for (i
= 0; i
< SectorsCnt
; i
++) {
736 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
737 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
739 PrintAndLog("|---|----------------|---|----------------|---|");
741 // transfer them to the emulator
743 for (i
= 0; i
< SectorsCnt
; i
++) {
744 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
745 if (e_sector
[i
].foundKey
[0])
746 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
747 if (e_sector
[i
].foundKey
[1])
748 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
749 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
754 if (createDumpFile
) {
755 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
756 PrintAndLog("Could not create file dumpkeys.bin");
760 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
761 for(i
=0; i
<SectorsCnt
; i
++) {
762 if (e_sector
[i
].foundKey
[0]){
763 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
764 fwrite ( tempkey
, 1, 6, fkeys
);
767 fwrite ( &standart
, 1, 6, fkeys
);
770 for(i
=0; i
<SectorsCnt
; i
++) {
771 if (e_sector
[i
].foundKey
[1]){
772 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
773 fwrite ( tempkey
, 1, 6, fkeys
);
776 fwrite ( &standart
, 1, 6, fkeys
);
787 int CmdHF14AMfChk(const char *Cmd
)
790 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
791 PrintAndLog(" * - all sectors");
792 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
793 PrintAndLog("d - write keys to binary file\n");
794 PrintAndLog("t - write keys to emulator memory");
795 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
796 PrintAndLog(" hf mf chk *1 ? t");
797 PrintAndLog(" hf mf chk *1 ? d");
802 char filename
[FILE_PATH_SIZE
]={0};
804 uint8_t *keyBlock
= NULL
, *p
;
805 uint8_t stKeyBlock
= 20;
811 uint8_t SectorsCnt
= 1;
815 int transferToEml
= 0;
816 int createDumpFile
= 0;
818 keyBlock
= calloc(stKeyBlock
, 6);
819 if (keyBlock
== NULL
) return 1;
821 uint64_t defaultKeys
[] =
823 0xffffffffffff, // Default key (first key used by program if no user defined key)
824 0x000000000000, // Blank key
825 0xa0a1a2a3a4a5, // NFCForum MAD key
837 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
839 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
841 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
844 if (param_getchar(Cmd
, 0)=='*') {
846 switch(param_getchar(Cmd
+1, 0)) {
847 case '0': SectorsCnt
= 5; break;
848 case '1': SectorsCnt
= 16; break;
849 case '2': SectorsCnt
= 32; break;
850 case '4': SectorsCnt
= 40; break;
851 default: SectorsCnt
= 16;
855 blockNo
= param_get8(Cmd
, 0);
857 ctmp
= param_getchar(Cmd
, 1);
869 PrintAndLog("Key type must be A , B or ?");
873 ctmp
= param_getchar(Cmd
, 2);
874 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
875 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
877 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
878 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
879 if ( stKeyBlock
- keycnt
< 2) {
880 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
882 PrintAndLog("Cannot allocate memory for Keys");
888 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
889 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
890 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
894 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
895 PrintAndLog("File name too long");
900 if ( (f
= fopen( filename
, "r")) ) {
901 while( fgets(buf
, sizeof(buf
), f
) ){
902 if (strlen(buf
) < 12 || buf
[11] == '\n')
905 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
907 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
909 if (!isxdigit(buf
[0])){
910 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
916 if ( stKeyBlock
- keycnt
< 2) {
917 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
919 PrintAndLog("Cannot allocate memory for defKeys");
925 memset(keyBlock
+ 6 * keycnt
, 0, 6);
926 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
927 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
929 memset(buf
, 0, sizeof(buf
));
933 PrintAndLog("File: %s: not found or locked.", filename
);
942 PrintAndLog("No key specified, trying default keys");
943 for (;keycnt
< defaultKeysSize
; keycnt
++)
944 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
945 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
946 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
949 // initialize storage for found keys
950 bool validKey
[2][40];
951 uint8_t foundKey
[2][40][6];
952 for (uint16_t t
= 0; t
< 2; t
++) {
953 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
954 validKey
[t
][sectorNo
] = false;
955 for (uint16_t i
= 0; i
< 6; i
++) {
956 foundKey
[t
][sectorNo
][i
] = 0xff;
961 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
963 for (int i
= 0; i
< SectorsCnt
; ++i
) {
964 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
965 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
966 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
967 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
968 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
971 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
972 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
973 validKey
[t
][i
] = true;
976 PrintAndLog("Command execute timeout");
979 b
<127?(b
+=4):(b
+=16);
985 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
986 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
987 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
988 for (uint16_t t
= 0; t
< 2; t
++) {
989 if (validKey
[t
][sectorNo
]) {
990 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
993 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
996 PrintAndLog("Found keys have been transferred to the emulator memory");
999 if (createDumpFile
) {
1000 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1001 if (fkeys
== NULL
) {
1002 PrintAndLog("Could not create file dumpkeys.bin");
1006 for (uint16_t t
= 0; t
< 2; t
++) {
1007 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1010 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1018 int CmdHF14AMf1kSim(const char *Cmd
)
1020 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1021 uint8_t exitAfterNReads
= 0;
1024 uint8_t cmdp
= param_getchar(Cmd
, 0);
1026 if (cmdp
== 'h' || cmdp
== 'H') {
1027 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1028 PrintAndLog(" h this help");
1029 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1030 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1031 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1032 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1034 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1038 if (param_getchar(Cmd
, pnr
) == 'u') {
1039 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1041 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1042 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1043 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1045 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1050 if (param_getchar(Cmd
, pnr
) == 'n') {
1051 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1054 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1055 //Using a flag to signal interactiveness, least significant bit
1056 flags
|= FLAG_INTERACTIVE
;
1060 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1061 //Using a flag to signal interactiveness, least significant bit
1062 flags
|= FLAG_NR_AR_ATTACK
;
1064 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1065 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1066 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1067 , exitAfterNReads
, flags
,flags
);
1070 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1071 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1074 if(flags
& FLAG_INTERACTIVE
)
1077 PrintAndLog("Press pm3-button to abort simulation");
1078 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1079 //We're waiting only 1.5 s at a time, otherwise we get the
1080 // annoying message about "Waiting for a response... "
1087 int CmdHF14AMfDbg(const char *Cmd
)
1089 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1091 PrintAndLog("Max debug mode parameter is 4 \n");
1094 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1095 PrintAndLog("Usage: hf mf dbg <debug level>");
1096 PrintAndLog(" 0 - no debug messages");
1097 PrintAndLog(" 1 - error messages");
1098 PrintAndLog(" 2 - plus information messages");
1099 PrintAndLog(" 3 - plus debug messages");
1100 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1101 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1105 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1111 int CmdHF14AMfEGet(const char *Cmd
)
1113 uint8_t blockNo
= 0;
1114 uint8_t data
[16] = {0x00};
1116 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1117 PrintAndLog("Usage: hf mf eget <block number>");
1118 PrintAndLog(" sample: hf mf eget 0 ");
1122 blockNo
= param_get8(Cmd
, 0);
1125 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1126 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1128 PrintAndLog("Command execute timeout");
1134 int CmdHF14AMfEClear(const char *Cmd
)
1136 if (param_getchar(Cmd
, 0) == 'h') {
1137 PrintAndLog("Usage: hf mf eclr");
1138 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1142 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1148 int CmdHF14AMfESet(const char *Cmd
)
1150 uint8_t memBlock
[16];
1151 uint8_t blockNo
= 0;
1153 memset(memBlock
, 0x00, sizeof(memBlock
));
1155 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1156 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1157 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1161 blockNo
= param_get8(Cmd
, 0);
1163 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1164 PrintAndLog("block data must include 32 HEX symbols");
1169 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1170 memcpy(c
.d
.asBytes
, memBlock
, 16);
1176 int CmdHF14AMfELoad(const char *Cmd
)
1179 char filename
[FILE_PATH_SIZE
];
1180 char *fnameptr
= filename
;
1181 char buf
[64] = {0x00};
1182 uint8_t buf8
[64] = {0x00};
1183 int i
, len
, blockNum
, numBlocks
;
1184 int nameParamNo
= 1;
1186 char ctmp
= param_getchar(Cmd
, 0);
1188 if ( ctmp
== 'h' || ctmp
== 0x00) {
1189 PrintAndLog("It loads emul dump from the file `filename.eml`");
1190 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1191 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1193 PrintAndLog(" sample: hf mf eload filename");
1194 PrintAndLog(" hf mf eload 4 filename");
1199 case '0' : numBlocks
= 5*4; break;
1201 case '\0': numBlocks
= 16*4; break;
1202 case '2' : numBlocks
= 32*4; break;
1203 case '4' : numBlocks
= 256; break;
1210 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1212 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1216 sprintf(fnameptr
, ".eml");
1219 f
= fopen(filename
, "r");
1221 PrintAndLog("File %s not found or locked", filename
);
1227 memset(buf
, 0, sizeof(buf
));
1229 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1231 if (blockNum
>= numBlocks
) break;
1233 PrintAndLog("File reading error.");
1238 if (strlen(buf
) < 32){
1239 if(strlen(buf
) && feof(f
))
1241 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1246 for (i
= 0; i
< 32; i
+= 2) {
1247 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1250 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1251 PrintAndLog("Cant set emul block: %3d", blockNum
);
1258 if (blockNum
>= numBlocks
) break;
1263 if ((blockNum
!= numBlocks
)) {
1264 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1267 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1272 int CmdHF14AMfESave(const char *Cmd
)
1275 char filename
[FILE_PATH_SIZE
];
1276 char * fnameptr
= filename
;
1278 int i
, j
, len
, numBlocks
;
1279 int nameParamNo
= 1;
1281 memset(filename
, 0, sizeof(filename
));
1282 memset(buf
, 0, sizeof(buf
));
1284 char ctmp
= param_getchar(Cmd
, 0);
1286 if ( ctmp
== 'h' || ctmp
== 'H') {
1287 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1288 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1289 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1291 PrintAndLog(" sample: hf mf esave ");
1292 PrintAndLog(" hf mf esave 4");
1293 PrintAndLog(" hf mf esave 4 filename");
1298 case '0' : numBlocks
= 5*4; break;
1300 case '\0': numBlocks
= 16*4; break;
1301 case '2' : numBlocks
= 32*4; break;
1302 case '4' : numBlocks
= 256; break;
1309 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1311 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1313 // user supplied filename?
1315 // get filename (UID from memory)
1316 if (mfEmlGetMem(buf
, 0, 1)) {
1317 PrintAndLog("Can\'t get UID from block: %d", 0);
1318 len
= sprintf(fnameptr
, "dump");
1322 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1323 sprintf(fnameptr
, "%02X", buf
[j
]);
1329 // add file extension
1330 sprintf(fnameptr
, ".eml");
1333 f
= fopen(filename
, "w+");
1336 PrintAndLog("Can't open file %s ", filename
);
1341 for (i
= 0; i
< numBlocks
; i
++) {
1342 if (mfEmlGetMem(buf
, i
, 1)) {
1343 PrintAndLog("Cant get block: %d", i
);
1346 for (j
= 0; j
< 16; j
++)
1347 fprintf(f
, "%02X", buf
[j
]);
1352 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1358 int CmdHF14AMfECFill(const char *Cmd
)
1360 uint8_t keyType
= 0;
1361 uint8_t numSectors
= 16;
1363 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1364 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1365 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1367 PrintAndLog("samples: hf mf ecfill A");
1368 PrintAndLog(" hf mf ecfill A 4");
1369 PrintAndLog("Read card and transfer its data to emulator memory.");
1370 PrintAndLog("Keys must be laid in the emulator memory. \n");
1374 char ctmp
= param_getchar(Cmd
, 0);
1375 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1376 PrintAndLog("Key type must be A or B");
1379 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1381 ctmp
= param_getchar(Cmd
, 1);
1383 case '0' : numSectors
= 5; break;
1385 case '\0': numSectors
= 16; break;
1386 case '2' : numSectors
= 32; break;
1387 case '4' : numSectors
= 40; break;
1388 default: numSectors
= 16;
1391 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1392 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1398 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1403 uint64_t keyA
, keyB
;
1405 if (param_getchar(Cmd
, 0) == 'h') {
1406 PrintAndLog("It prints the keys loaded in the emulator memory");
1407 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1408 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1410 PrintAndLog(" sample: hf mf ekeyprn 1");
1414 char cmdp
= param_getchar(Cmd
, 0);
1417 case '0' : numSectors
= 5; break;
1419 case '\0': numSectors
= 16; break;
1420 case '2' : numSectors
= 32; break;
1421 case '4' : numSectors
= 40; break;
1422 default: numSectors
= 16;
1425 PrintAndLog("|---|----------------|----------------|");
1426 PrintAndLog("|sec|key A |key B |");
1427 PrintAndLog("|---|----------------|----------------|");
1428 for (i
= 0; i
< numSectors
; i
++) {
1429 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1430 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1433 keyA
= bytes_to_num(data
, 6);
1434 keyB
= bytes_to_num(data
+ 10, 6);
1435 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1437 PrintAndLog("|---|----------------|----------------|");
1443 int CmdHF14AMfCSetUID(const char *Cmd
)
1445 uint8_t wipeCard
= 0;
1446 uint8_t uid
[8] = {0x00};
1447 uint8_t oldUid
[8] = {0x00};
1448 uint8_t atqa
[2] = {0x00};
1449 uint8_t sak
[1] = {0x00};
1450 uint8_t atqaPresent
= 1;
1455 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1456 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1457 PrintAndLog("sample: hf mf csetuid 01020304");
1458 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1459 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1460 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1464 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1465 PrintAndLog("UID must include 8 HEX symbols");
1470 ctmp
= param_getchar(Cmd
, argi
);
1471 if (ctmp
== 'w' || ctmp
== 'W') {
1477 if (param_getchar(Cmd
, argi
)) {
1478 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1479 PrintAndLog("ATQA must include 4 HEX symbols");
1483 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1484 PrintAndLog("SAK must include 2 HEX symbols");
1493 ctmp
= param_getchar(Cmd
, argi
);
1494 if (ctmp
== 'w' || ctmp
== 'W') {
1499 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1501 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1503 PrintAndLog("Can't set UID. error=%d", res
);
1507 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1508 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1512 int CmdHF14AMfCSetBlk(const char *Cmd
)
1514 uint8_t memBlock
[16] = {0x00};
1515 uint8_t blockNo
= 0;
1516 bool wipeCard
= FALSE
;
1519 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1520 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1521 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1522 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1523 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1527 blockNo
= param_get8(Cmd
, 0);
1529 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1530 PrintAndLog("block data must include 32 HEX symbols");
1534 char ctmp
= param_getchar(Cmd
, 2);
1535 wipeCard
= (ctmp
== 'w' || ctmp
== 'W');
1536 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16));
1538 res
= mfCSetBlock(blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1540 PrintAndLog("Can't write block. error=%d", res
);
1547 int CmdHF14AMfCLoad(const char *Cmd
)
1550 char filename
[FILE_PATH_SIZE
] = {0x00};
1551 char * fnameptr
= filename
;
1552 char buf
[64] = {0x00};
1553 uint8_t buf8
[64] = {0x00};
1554 uint8_t fillFromEmulator
= 0;
1555 int i
, len
, blockNum
, flags
=0;
1557 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1558 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1559 PrintAndLog("or from emulator memory (option `e`)");
1560 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1561 PrintAndLog(" or: hf mf cload e ");
1562 PrintAndLog(" sample: hf mf cload filename");
1566 char ctmp
= param_getchar(Cmd
, 0);
1567 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1569 if (fillFromEmulator
) {
1570 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1571 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1572 PrintAndLog("Cant get block: %d", blockNum
);
1575 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1576 if (blockNum
== 1) flags
= 0; // just write
1577 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1579 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1580 PrintAndLog("Cant set magic card block: %d", blockNum
);
1587 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1589 memcpy(filename
, Cmd
, len
);
1592 sprintf(fnameptr
, ".eml");
1595 f
= fopen(filename
, "r");
1597 PrintAndLog("File not found or locked.");
1604 memset(buf
, 0, sizeof(buf
));
1606 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1608 PrintAndLog("File reading error.");
1612 if (strlen(buf
) < 32) {
1613 if(strlen(buf
) && feof(f
))
1615 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1619 for (i
= 0; i
< 32; i
+= 2)
1620 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1622 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1623 if (blockNum
== 1) flags
= 0; // just write
1624 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1626 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1627 PrintAndLog("Can't set magic card block: %d", blockNum
);
1632 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1636 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1637 PrintAndLog("File content error. There must be 64 blocks");
1640 PrintAndLog("Loaded from file: %s", filename
);
1646 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1647 uint8_t memBlock
[16];
1648 uint8_t blockNo
= 0;
1650 memset(memBlock
, 0x00, sizeof(memBlock
));
1652 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1653 PrintAndLog("Usage: hf mf cgetblk <block number>");
1654 PrintAndLog("sample: hf mf cgetblk 1");
1655 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1659 blockNo
= param_get8(Cmd
, 0);
1661 PrintAndLog("--block number:%2d ", blockNo
);
1663 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1665 PrintAndLog("Can't read block. error=%d", res
);
1669 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1674 int CmdHF14AMfCGetSc(const char *Cmd
) {
1675 uint8_t memBlock
[16] = {0x00};
1676 uint8_t sectorNo
= 0;
1679 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1680 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1681 PrintAndLog("sample: hf mf cgetsc 0");
1682 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1686 sectorNo
= param_get8(Cmd
, 0);
1687 if (sectorNo
> 15) {
1688 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1692 PrintAndLog("--sector number:%d ", sectorNo
);
1694 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1695 for (i
= 0; i
< 4; i
++) {
1696 if (i
== 1) flags
= 0;
1697 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1699 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1701 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1705 PrintAndLog("block %3d data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1711 int CmdHF14AMfCSave(const char *Cmd
) {
1714 char filename
[FILE_PATH_SIZE
] = {0x00};
1715 char * fnameptr
= filename
;
1716 uint8_t fillFromEmulator
= 0;
1717 uint8_t buf
[64] = {0x00};
1718 int i
, j
, len
, flags
;
1720 // memset(filename, 0, sizeof(filename));
1721 // memset(buf, 0, sizeof(buf));
1723 if (param_getchar(Cmd
, 0) == 'h') {
1724 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1725 PrintAndLog("or into emulator memory (option `e`)");
1726 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1727 PrintAndLog(" sample: hf mf esave ");
1728 PrintAndLog(" hf mf esave filename");
1729 PrintAndLog(" hf mf esave e \n");
1733 char ctmp
= param_getchar(Cmd
, 0);
1734 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1736 if (fillFromEmulator
) {
1737 // put into emulator
1738 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1739 for (i
= 0; i
< 16 * 4; i
++) {
1740 if (i
== 1) flags
= 0;
1741 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1743 if (mfCGetBlock(i
, buf
, flags
)) {
1744 PrintAndLog("Cant get block: %d", i
);
1748 if (mfEmlSetMem(buf
, i
, 1)) {
1749 PrintAndLog("Cant set emul block: %d", i
);
1756 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1760 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1761 PrintAndLog("Cant get block: %d", 0);
1762 len
= sprintf(fnameptr
, "dump");
1766 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1767 sprintf(fnameptr
, "%02x", buf
[j
]);
1770 memcpy(filename
, Cmd
, len
);
1774 sprintf(fnameptr
, ".eml");
1777 f
= fopen(filename
, "w+");
1780 PrintAndLog("File not found or locked.");
1785 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1786 for (i
= 0; i
< 16 * 4; i
++) {
1787 if (i
== 1) flags
= 0;
1788 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1790 if (mfCGetBlock(i
, buf
, flags
)) {
1791 PrintAndLog("Cant get block: %d", i
);
1794 for (j
= 0; j
< 16; j
++)
1795 fprintf(f
, "%02x", buf
[j
]);
1800 PrintAndLog("Saved to file: %s", filename
);
1807 int CmdHF14AMfSniff(const char *Cmd
){
1809 bool wantLogToFile
= 0;
1810 bool wantDecrypt
= 0;
1811 //bool wantSaveToEml = 0; TODO
1812 bool wantSaveToEmlFile
= 0;
1822 uint8_t atqa
[2] = {0x00};
1825 uint8_t *buf
= NULL
;
1826 uint16_t bufsize
= 0;
1827 uint8_t *bufPtr
= NULL
;
1829 char ctmp
= param_getchar(Cmd
, 0);
1830 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1831 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1832 PrintAndLog("You can specify:");
1833 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1834 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1835 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1836 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1837 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1838 PrintAndLog(" sample: hf mf sniff l d e");
1842 for (int i
= 0; i
< 4; i
++) {
1843 ctmp
= param_getchar(Cmd
, i
);
1844 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1845 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1846 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1847 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1850 printf("-------------------------------------------------------------------------\n");
1851 printf("Executing command. \n");
1852 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1853 printf("Press the key on pc keyboard to abort the client.\n");
1854 printf("-------------------------------------------------------------------------\n");
1856 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1857 clearCommandBuffer();
1866 printf("\naborted via keyboard!\n");
1871 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1872 res
= resp
.arg
[0] & 0xff;
1873 uint16_t traceLen
= resp
.arg
[1];
1876 if (res
== 0) return 0; // we are done
1878 if (res
== 1) { // there is (more) data to be transferred
1879 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1880 if (traceLen
> bufsize
) {
1882 if (buf
== NULL
) { // not yet allocated
1883 p
= malloc(traceLen
);
1884 } else { // need more memory
1885 p
= realloc(buf
, traceLen
);
1888 PrintAndLog("Cannot allocate memory for trace");
1896 memset(buf
, 0x00, traceLen
);
1898 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1903 if (res
== 2) { // received all data, start displaying
1904 blockLen
= bufPtr
- buf
;
1907 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1908 while (bufPtr
- buf
< blockLen
) {
1909 bufPtr
+= 6; // skip (void) timing information
1910 len
= *((uint16_t *)bufPtr
);
1918 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
1919 memcpy(uid
, bufPtr
+ 2, 7);
1920 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1921 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
1923 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1924 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
1928 if (wantLogToFile
|| wantDecrypt
) {
1929 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
1930 AddLogCurrentDT(logHexFileName
);
1933 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1935 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1937 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1939 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1943 bufPtr
+= ((len
-1)/8+1); // ignore parity
1954 //needs nt, ar, at, Data to decrypt
1955 int CmdDecryptTraceCmds(const char *Cmd
){
1958 param_gethex_ex(Cmd
,3,data
,&len
);
1959 return tryDecryptWord(param_get32ex(Cmd
,0,0,16),param_get32ex(Cmd
,1,0,16),param_get32ex(Cmd
,2,0,16),data
,len
/2);
1962 static command_t CommandTable
[] =
1964 {"help", CmdHelp
, 1, "This help"},
1965 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1966 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1967 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1968 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1969 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1970 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1971 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1972 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1973 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1974 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1975 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1976 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1977 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1978 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1979 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1980 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1981 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1982 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1983 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1984 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
1985 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
1986 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
1987 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1988 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1989 {"decrypt", CmdDecryptTraceCmds
,1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
1990 {NULL
, NULL
, 0, NULL
}
1993 int CmdHFMF(const char *Cmd
)
1996 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1998 CmdsParse(CommandTable
, Cmd
);
2002 int CmdHelp(const char *Cmd
)
2004 CmdsHelp(CommandTable
);