]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
15 int CmdHF14AMifare(const char *Cmd
)
18 uint32_t nt
= 0, nr
= 0;
19 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
22 uint8_t blockNo
= 0, keyType
= 0;
26 PrintAndLog("Usage: hf mf mifare <block number> <key A/B>");
27 PrintAndLog(" sample: hf mf mi 0 A");
31 blockNo
= param_get8(Cmd
, 0);
32 cmdp
= param_getchar(Cmd
, 1);
34 PrintAndLog("Key type must be A or B");
37 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
39 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, keyType
}};
42 printf("-------------------------------------------------------------------------\n");
43 printf("Executing command. Expected execution time: 25sec on average :-)\n");
44 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
45 printf("-------------------------------------------------------------------------\n");
53 while (ukbhit()) getchar();
61 printf("\naborted via keyboard!\n");
66 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)) {
68 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
69 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
70 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
71 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
72 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
75 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
76 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
77 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
78 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
79 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
89 if (isOK
!= 1) return 1;
91 // execute original function from util nonce2key
92 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
94 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
95 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
100 printf("------------------------------------------------------------------\n");
101 PrintAndLog("Found valid key:%012"llx
" \n", r_key
);
108 int CmdHF14AMfWrBl(const char *Cmd
)
112 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
113 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
118 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
119 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
123 blockNo
= param_get8(Cmd
, 0);
124 cmdp
= param_getchar(Cmd
, 1);
126 PrintAndLog("Key type must be A or B");
129 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
130 if (param_gethex(Cmd
, 2, key
, 12)) {
131 PrintAndLog("Key must include 12 HEX symbols");
134 if (param_gethex(Cmd
, 3, bldata
, 32)) {
135 PrintAndLog("Block data must include 32 HEX symbols");
138 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
139 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
141 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
142 memcpy(c
.d
.asBytes
, key
, 6);
143 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
147 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
148 uint8_t isOK
= resp
.arg
[0] & 0xff;
149 PrintAndLog("isOk:%02x", isOK
);
151 PrintAndLog("Command execute timeout");
157 int CmdHF14AMfRdBl(const char *Cmd
)
161 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
167 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
168 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
172 blockNo
= param_get8(Cmd
, 0);
173 cmdp
= param_getchar(Cmd
, 1);
175 PrintAndLog("Key type must be A or B");
178 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
179 if (param_gethex(Cmd
, 2, key
, 12)) {
180 PrintAndLog("Key must include 12 HEX symbols");
183 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
185 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
186 memcpy(c
.d
.asBytes
, key
, 6);
190 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
191 uint8_t isOK
= resp
.arg
[0] & 0xff;
192 uint8_t *data
= resp
.d
.asBytes
;
195 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
197 PrintAndLog("isOk:%02x", isOK
);
199 PrintAndLog("Command execute timeout");
205 int CmdHF14AMfRdSc(const char *Cmd
)
208 uint8_t sectorNo
= 0;
210 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
212 uint8_t *data
= NULL
;
216 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
217 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
221 sectorNo
= param_get8(Cmd
, 0);
223 PrintAndLog("Sector number must be less than 40");
226 cmdp
= param_getchar(Cmd
, 1);
227 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
228 PrintAndLog("Key type must be A or B");
231 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
232 if (param_gethex(Cmd
, 2, key
, 12)) {
233 PrintAndLog("Key must include 12 HEX symbols");
236 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
238 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
239 memcpy(c
.d
.asBytes
, key
, 6);
244 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
245 isOK
= resp
.arg
[0] & 0xff;
246 data
= resp
.d
.asBytes
;
248 PrintAndLog("isOk:%02x", isOK
);
250 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
251 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
253 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
256 PrintAndLog("Command execute timeout");
262 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
267 return 32 * 4 + (sectorNo
- 32) * 16;
271 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
280 int CmdHF14AMfDump(const char *Cmd
)
282 uint8_t sectorNo
, blockNo
;
286 uint8_t rights
[40][4];
287 uint8_t carddata
[256][16];
288 uint8_t numSectors
= 16;
295 char cmdp
= param_getchar(Cmd
, 0);
297 case '0' : numSectors
= 5; break;
299 case '\0': numSectors
= 16; break;
300 case '2' : numSectors
= 32; break;
301 case '4' : numSectors
= 40; break;
302 default: numSectors
= 16;
305 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
306 PrintAndLog("Usage: hf mf dump [card memory]");
307 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
309 PrintAndLog("Samples: hf mf dump");
310 PrintAndLog(" hf mf dump 4");
314 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
315 PrintAndLog("Could not find file dumpkeys.bin");
319 // Read keys A from file
320 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
321 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
322 PrintAndLog("File reading error.");
328 // Read keys B from file
329 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
330 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
331 PrintAndLog("File reading error.");
339 PrintAndLog("|-----------------------------------------|");
340 PrintAndLog("|------ Reading sector access bits...-----|");
341 PrintAndLog("|-----------------------------------------|");
343 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
344 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
345 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
348 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
349 uint8_t isOK
= resp
.arg
[0] & 0xff;
350 uint8_t *data
= resp
.d
.asBytes
;
352 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
353 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
354 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
355 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
357 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
358 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
359 rights
[sectorNo
][3] = 0x01;
362 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
363 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
364 rights
[sectorNo
][3] = 0x01;
368 PrintAndLog("|-----------------------------------------|");
369 PrintAndLog("|----- Dumping all blocks to file... -----|");
370 PrintAndLog("|-----------------------------------------|");
373 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
374 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
375 bool received
= false;
377 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
378 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
379 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
381 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
382 } else { // data block. Check if it can be read with key A or key B
383 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
384 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
385 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
386 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
388 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
389 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
391 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
392 } else { // key A would work
393 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
394 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
396 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
401 isOK
= resp
.arg
[0] & 0xff;
402 uint8_t *data
= resp
.d
.asBytes
;
403 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
404 data
[0] = (keyA
[sectorNo
][0]);
405 data
[1] = (keyA
[sectorNo
][1]);
406 data
[2] = (keyA
[sectorNo
][2]);
407 data
[3] = (keyA
[sectorNo
][3]);
408 data
[4] = (keyA
[sectorNo
][4]);
409 data
[5] = (keyA
[sectorNo
][5]);
410 data
[10] = (keyB
[sectorNo
][0]);
411 data
[11] = (keyB
[sectorNo
][1]);
412 data
[12] = (keyB
[sectorNo
][2]);
413 data
[13] = (keyB
[sectorNo
][3]);
414 data
[14] = (keyB
[sectorNo
][4]);
415 data
[15] = (keyB
[sectorNo
][5]);
418 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
419 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
421 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
427 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
434 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
435 PrintAndLog("Could not create file name dumpdata.bin");
438 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
439 fwrite(carddata
, 1, 16*numblocks
, fout
);
441 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
447 int CmdHF14AMfRestore(const char *Cmd
)
449 uint8_t sectorNo
,blockNo
;
451 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
452 uint8_t bldata
[16] = {0x00};
460 char cmdp
= param_getchar(Cmd
, 0);
462 case '0' : numSectors
= 5; break;
464 case '\0': numSectors
= 16; break;
465 case '2' : numSectors
= 32; break;
466 case '4' : numSectors
= 40; break;
467 default: numSectors
= 16;
470 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
471 PrintAndLog("Usage: hf mf restore [card memory]");
472 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
474 PrintAndLog("Samples: hf mf restore");
475 PrintAndLog(" hf mf restore 4");
479 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
480 PrintAndLog("Could not find file dumpkeys.bin");
484 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
485 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
486 PrintAndLog("File reading error (dumpkeys.bin).");
493 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
494 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
495 PrintAndLog("File reading error (dumpkeys.bin).");
503 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
504 PrintAndLog("Could not find file dumpdata.bin");
507 PrintAndLog("Restoring dumpdata.bin to card");
509 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
510 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
511 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
512 memcpy(c
.d
.asBytes
, key
, 6);
514 if (fread(bldata
, 1, 16, fdump
) == 0) {
515 PrintAndLog("File reading error (dumpdata.bin).");
520 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
521 bldata
[0] = (keyA
[sectorNo
][0]);
522 bldata
[1] = (keyA
[sectorNo
][1]);
523 bldata
[2] = (keyA
[sectorNo
][2]);
524 bldata
[3] = (keyA
[sectorNo
][3]);
525 bldata
[4] = (keyA
[sectorNo
][4]);
526 bldata
[5] = (keyA
[sectorNo
][5]);
527 bldata
[10] = (keyB
[sectorNo
][0]);
528 bldata
[11] = (keyB
[sectorNo
][1]);
529 bldata
[12] = (keyB
[sectorNo
][2]);
530 bldata
[13] = (keyB
[sectorNo
][3]);
531 bldata
[14] = (keyB
[sectorNo
][4]);
532 bldata
[15] = (keyB
[sectorNo
][5]);
535 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
537 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
541 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
542 uint8_t isOK
= resp
.arg
[0] & 0xff;
543 PrintAndLog("isOk:%02x", isOK
);
545 PrintAndLog("Command execute timeout");
554 int CmdHF14AMfNested(const char *Cmd
)
556 int i
, j
, res
, iterations
;
557 sector
*e_sector
= NULL
;
560 uint8_t trgBlockNo
= 0;
561 uint8_t trgKeyType
= 0;
562 uint8_t SectorsCnt
= 0;
563 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
564 uint8_t keyBlock
[14*6];
566 bool transferToEml
= false;
568 bool createDumpFile
= false;
570 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
571 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
576 PrintAndLog("Usage:");
577 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
578 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
579 PrintAndLog(" <target block number> <target key A/B> [t]");
580 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
581 PrintAndLog("t - transfer keys into emulator memory");
582 PrintAndLog("d - write keys to binary file");
584 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
585 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
586 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
587 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
591 cmdp
= param_getchar(Cmd
, 0);
592 blockNo
= param_get8(Cmd
, 1);
593 ctmp
= param_getchar(Cmd
, 2);
595 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
596 PrintAndLog("Key type must be A or B");
600 if (ctmp
!= 'A' && ctmp
!= 'a')
603 if (param_gethex(Cmd
, 3, key
, 12)) {
604 PrintAndLog("Key must include 12 HEX symbols");
608 if (cmdp
== 'o' || cmdp
== 'O') {
610 trgBlockNo
= param_get8(Cmd
, 4);
611 ctmp
= param_getchar(Cmd
, 5);
612 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
613 PrintAndLog("Target key type must be A or B");
616 if (ctmp
!= 'A' && ctmp
!= 'a')
621 case '0': SectorsCnt
= 05; break;
622 case '1': SectorsCnt
= 16; break;
623 case '2': SectorsCnt
= 32; break;
624 case '4': SectorsCnt
= 40; break;
625 default: SectorsCnt
= 16;
629 ctmp
= param_getchar(Cmd
, 4);
630 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
631 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
633 ctmp
= param_getchar(Cmd
, 6);
634 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
635 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
638 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
639 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
642 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
643 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
644 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
645 default : PrintAndLog("Unknown Error.\n");
649 key64
= bytes_to_num(keyBlock
, 6);
651 PrintAndLog("Found valid key:%012"llx
, key64
);
653 // transfer key to the emulator
655 uint8_t sectortrailer
;
656 if (trgBlockNo
< 32*4) { // 4 block sector
657 sectortrailer
= (trgBlockNo
& 0x03) + 3;
658 } else { // 16 block sector
659 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
661 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
664 num_to_bytes(key64
, 6, keyBlock
);
666 num_to_bytes(key64
, 6, &keyBlock
[10]);
667 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
670 PrintAndLog("No valid key found");
673 else { // ------------------------------------ multiple sectors working
677 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
678 if (e_sector
== NULL
) return 1;
680 //test current key and additional standard keys first
681 memcpy(keyBlock
, key
, 6);
682 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
683 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
684 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
685 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
686 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
687 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
688 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
689 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
690 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
691 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
692 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
693 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
694 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
696 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
697 for (i
= 0; i
< SectorsCnt
; i
++) {
698 for (j
= 0; j
< 2; j
++) {
699 if (e_sector
[i
].foundKey
[j
]) continue;
701 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
704 e_sector
[i
].Key
[j
] = key64
;
705 e_sector
[i
].foundKey
[j
] = 1;
712 PrintAndLog("nested...");
713 bool calibrate
= true;
714 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
715 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
716 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
717 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
718 PrintAndLog("-----------------------------------------------");
719 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
722 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
723 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
724 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
725 default : PrintAndLog("Unknown Error.\n");
735 key64
= bytes_to_num(keyBlock
, 6);
737 PrintAndLog("Found valid key:%012"llx
, key64
);
738 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
739 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
745 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
);
747 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
749 PrintAndLog("|---|----------------|---|----------------|---|");
750 PrintAndLog("|sec|key A |res|key B |res|");
751 PrintAndLog("|---|----------------|---|----------------|---|");
752 for (i
= 0; i
< SectorsCnt
; i
++) {
753 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
754 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
756 PrintAndLog("|---|----------------|---|----------------|---|");
758 // transfer them to the emulator
760 for (i
= 0; i
< SectorsCnt
; i
++) {
761 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
762 if (e_sector
[i
].foundKey
[0])
763 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
764 if (e_sector
[i
].foundKey
[1])
765 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
766 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
771 if (createDumpFile
) {
772 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
773 PrintAndLog("Could not create file dumpkeys.bin");
777 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
778 for(i
=0; i
<SectorsCnt
; i
++) {
779 if (e_sector
[i
].foundKey
[0]){
780 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
781 fwrite ( tempkey
, 1, 6, fkeys
);
784 fwrite ( &standart
, 1, 6, fkeys
);
787 for(i
=0; i
<SectorsCnt
; i
++) {
788 if (e_sector
[i
].foundKey
[1]){
789 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
790 fwrite ( tempkey
, 1, 6, fkeys
);
793 fwrite ( &standart
, 1, 6, fkeys
);
804 int CmdHF14AMfChk(const char *Cmd
)
807 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
808 PrintAndLog(" * - all sectors");
809 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
810 PrintAndLog("d - write keys to binary file\n");
811 PrintAndLog("t - write keys to emulator memory");
812 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
813 PrintAndLog(" hf mf chk *1 ? t");
814 PrintAndLog(" hf mf chk *1 ? d");
819 char filename
[FILE_PATH_SIZE
]={0};
821 uint8_t *keyBlock
= NULL
, *p
;
822 uint8_t stKeyBlock
= 20;
828 uint8_t SectorsCnt
= 1;
832 int transferToEml
= 0;
833 int createDumpFile
= 0;
835 keyBlock
= calloc(stKeyBlock
, 6);
836 if (keyBlock
== NULL
) return 1;
838 uint64_t defaultKeys
[] =
840 0xffffffffffff, // Default key (first key used by program if no user defined key)
841 0x000000000000, // Blank key
842 0xa0a1a2a3a4a5, // NFCForum MAD key
854 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
856 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
858 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
861 if (param_getchar(Cmd
, 0)=='*') {
863 switch(param_getchar(Cmd
+1, 0)) {
864 case '0': SectorsCnt
= 5; break;
865 case '1': SectorsCnt
= 16; break;
866 case '2': SectorsCnt
= 32; break;
867 case '4': SectorsCnt
= 40; break;
868 default: SectorsCnt
= 16;
872 blockNo
= param_get8(Cmd
, 0);
874 ctmp
= param_getchar(Cmd
, 1);
886 PrintAndLog("Key type must be A , B or ?");
890 ctmp
= param_getchar(Cmd
, 2);
891 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
892 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
894 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
895 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
896 if ( stKeyBlock
- keycnt
< 2) {
897 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
899 PrintAndLog("Cannot allocate memory for Keys");
905 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
906 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
907 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
911 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
912 PrintAndLog("File name too long");
917 if ( (f
= fopen( filename
, "r")) ) {
918 while( fgets(buf
, sizeof(buf
), f
) ){
919 if (strlen(buf
) < 12 || buf
[11] == '\n')
922 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
924 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
926 if (!isxdigit(buf
[0])){
927 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
933 if ( stKeyBlock
- keycnt
< 2) {
934 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
936 PrintAndLog("Cannot allocate memory for defKeys");
942 memset(keyBlock
+ 6 * keycnt
, 0, 6);
943 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
944 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
946 memset(buf
, 0, sizeof(buf
));
950 PrintAndLog("File: %s: not found or locked.", filename
);
959 PrintAndLog("No key specified, trying default keys");
960 for (;keycnt
< defaultKeysSize
; keycnt
++)
961 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
962 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
963 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
966 // initialize storage for found keys
967 bool validKey
[2][40];
968 uint8_t foundKey
[2][40][6];
969 for (uint16_t t
= 0; t
< 2; t
++) {
970 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
971 validKey
[t
][sectorNo
] = false;
972 for (uint16_t i
= 0; i
< 6; i
++) {
973 foundKey
[t
][sectorNo
][i
] = 0xff;
978 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
980 for (int i
= 0; i
< SectorsCnt
; ++i
) {
981 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
982 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
983 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
984 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
985 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
988 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
989 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
990 validKey
[t
][i
] = true;
993 PrintAndLog("Command execute timeout");
996 b
<127?(b
+=4):(b
+=16);
1000 if (transferToEml
) {
1002 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1003 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1004 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1005 for (uint16_t t
= 0; t
< 2; t
++) {
1006 if (validKey
[t
][sectorNo
]) {
1007 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1010 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1013 PrintAndLog("Found keys have been transferred to the emulator memory");
1016 if (createDumpFile
) {
1017 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1018 if (fkeys
== NULL
) {
1019 PrintAndLog("Could not create file dumpkeys.bin");
1023 for (uint16_t t
= 0; t
< 2; t
++) {
1024 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1027 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1035 int CmdHF14AMf1kSim(const char *Cmd
)
1037 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1038 uint8_t exitAfterNReads
= 0;
1041 uint8_t cmdp
= param_getchar(Cmd
, 0);
1043 if (cmdp
== 'h' || cmdp
== 'H') {
1044 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1045 PrintAndLog(" h this help");
1046 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1047 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1048 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1049 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1051 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1055 if (param_getchar(Cmd
, pnr
) == 'u') {
1056 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1058 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1059 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1060 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1062 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1067 if (param_getchar(Cmd
, pnr
) == 'n') {
1068 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1071 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1072 //Using a flag to signal interactiveness, least significant bit
1073 flags
|= FLAG_INTERACTIVE
;
1077 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1078 //Using a flag to signal interactiveness, least significant bit
1079 flags
|= FLAG_NR_AR_ATTACK
;
1081 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1082 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1083 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1084 , exitAfterNReads
, flags
,flags
);
1087 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1088 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1091 if(flags
& FLAG_INTERACTIVE
)
1094 PrintAndLog("Press pm3-button to abort simulation");
1095 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1096 //We're waiting only 1.5 s at a time, otherwise we get the
1097 // annoying message about "Waiting for a response... "
1104 int CmdHF14AMfDbg(const char *Cmd
)
1106 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1108 PrintAndLog("Max debug mode parameter is 4 \n");
1111 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1112 PrintAndLog("Usage: hf mf dbg <debug level>");
1113 PrintAndLog(" 0 - no debug messages");
1114 PrintAndLog(" 1 - error messages");
1115 PrintAndLog(" 2 - plus information messages");
1116 PrintAndLog(" 3 - plus debug messages");
1117 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1118 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1122 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1128 int CmdHF14AMfEGet(const char *Cmd
)
1130 uint8_t blockNo
= 0;
1131 uint8_t data
[16] = {0x00};
1133 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1134 PrintAndLog("Usage: hf mf eget <block number>");
1135 PrintAndLog(" sample: hf mf eget 0 ");
1139 blockNo
= param_get8(Cmd
, 0);
1142 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1143 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1145 PrintAndLog("Command execute timeout");
1151 int CmdHF14AMfEClear(const char *Cmd
)
1153 if (param_getchar(Cmd
, 0) == 'h') {
1154 PrintAndLog("Usage: hf mf eclr");
1155 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1159 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1165 int CmdHF14AMfESet(const char *Cmd
)
1167 uint8_t memBlock
[16];
1168 uint8_t blockNo
= 0;
1170 memset(memBlock
, 0x00, sizeof(memBlock
));
1172 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1173 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1174 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1178 blockNo
= param_get8(Cmd
, 0);
1180 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1181 PrintAndLog("block data must include 32 HEX symbols");
1186 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1187 memcpy(c
.d
.asBytes
, memBlock
, 16);
1193 int CmdHF14AMfELoad(const char *Cmd
)
1196 char filename
[FILE_PATH_SIZE
];
1197 char *fnameptr
= filename
;
1198 char buf
[64] = {0x00};
1199 uint8_t buf8
[64] = {0x00};
1200 int i
, len
, blockNum
, numBlocks
;
1201 int nameParamNo
= 1;
1203 char ctmp
= param_getchar(Cmd
, 0);
1205 if ( ctmp
== 'h' || ctmp
== 0x00) {
1206 PrintAndLog("It loads emul dump from the file `filename.eml`");
1207 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1208 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1210 PrintAndLog(" sample: hf mf eload filename");
1211 PrintAndLog(" hf mf eload 4 filename");
1216 case '0' : numBlocks
= 5*4; break;
1218 case '\0': numBlocks
= 16*4; break;
1219 case '2' : numBlocks
= 32*4; break;
1220 case '4' : numBlocks
= 256; break;
1227 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1229 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1233 sprintf(fnameptr
, ".eml");
1236 f
= fopen(filename
, "r");
1238 PrintAndLog("File %s not found or locked", filename
);
1244 memset(buf
, 0, sizeof(buf
));
1246 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1248 if (blockNum
>= numBlocks
) break;
1250 PrintAndLog("File reading error.");
1255 if (strlen(buf
) < 32){
1256 if(strlen(buf
) && feof(f
))
1258 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1263 for (i
= 0; i
< 32; i
+= 2) {
1264 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1267 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1268 PrintAndLog("Cant set emul block: %3d", blockNum
);
1275 if (blockNum
>= numBlocks
) break;
1280 if ((blockNum
!= numBlocks
)) {
1281 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1284 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1289 int CmdHF14AMfESave(const char *Cmd
)
1292 char filename
[FILE_PATH_SIZE
];
1293 char * fnameptr
= filename
;
1295 int i
, j
, len
, numBlocks
;
1296 int nameParamNo
= 1;
1298 memset(filename
, 0, sizeof(filename
));
1299 memset(buf
, 0, sizeof(buf
));
1301 char ctmp
= param_getchar(Cmd
, 0);
1303 if ( ctmp
== 'h' || ctmp
== 'H') {
1304 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1305 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1306 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1308 PrintAndLog(" sample: hf mf esave ");
1309 PrintAndLog(" hf mf esave 4");
1310 PrintAndLog(" hf mf esave 4 filename");
1315 case '0' : numBlocks
= 5*4; break;
1317 case '\0': numBlocks
= 16*4; break;
1318 case '2' : numBlocks
= 32*4; break;
1319 case '4' : numBlocks
= 256; break;
1326 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1328 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1330 // user supplied filename?
1332 // get filename (UID from memory)
1333 if (mfEmlGetMem(buf
, 0, 1)) {
1334 PrintAndLog("Can\'t get UID from block: %d", 0);
1335 len
= sprintf(fnameptr
, "dump");
1339 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1340 sprintf(fnameptr
, "%02X", buf
[j
]);
1346 // add file extension
1347 sprintf(fnameptr
, ".eml");
1350 f
= fopen(filename
, "w+");
1353 PrintAndLog("Can't open file %s ", filename
);
1358 for (i
= 0; i
< numBlocks
; i
++) {
1359 if (mfEmlGetMem(buf
, i
, 1)) {
1360 PrintAndLog("Cant get block: %d", i
);
1363 for (j
= 0; j
< 16; j
++)
1364 fprintf(f
, "%02X", buf
[j
]);
1369 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1375 int CmdHF14AMfECFill(const char *Cmd
)
1377 uint8_t keyType
= 0;
1378 uint8_t numSectors
= 16;
1380 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1381 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1382 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1384 PrintAndLog("samples: hf mf ecfill A");
1385 PrintAndLog(" hf mf ecfill A 4");
1386 PrintAndLog("Read card and transfer its data to emulator memory.");
1387 PrintAndLog("Keys must be laid in the emulator memory. \n");
1391 char ctmp
= param_getchar(Cmd
, 0);
1392 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1393 PrintAndLog("Key type must be A or B");
1396 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1398 ctmp
= param_getchar(Cmd
, 1);
1400 case '0' : numSectors
= 5; break;
1402 case '\0': numSectors
= 16; break;
1403 case '2' : numSectors
= 32; break;
1404 case '4' : numSectors
= 40; break;
1405 default: numSectors
= 16;
1408 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1409 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1415 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1420 uint64_t keyA
, keyB
;
1422 if (param_getchar(Cmd
, 0) == 'h') {
1423 PrintAndLog("It prints the keys loaded in the emulator memory");
1424 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1425 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1427 PrintAndLog(" sample: hf mf ekeyprn 1");
1431 char cmdp
= param_getchar(Cmd
, 0);
1434 case '0' : numSectors
= 5; break;
1436 case '\0': numSectors
= 16; break;
1437 case '2' : numSectors
= 32; break;
1438 case '4' : numSectors
= 40; break;
1439 default: numSectors
= 16;
1442 PrintAndLog("|---|----------------|----------------|");
1443 PrintAndLog("|sec|key A |key B |");
1444 PrintAndLog("|---|----------------|----------------|");
1445 for (i
= 0; i
< numSectors
; i
++) {
1446 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1447 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1450 keyA
= bytes_to_num(data
, 6);
1451 keyB
= bytes_to_num(data
+ 10, 6);
1452 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1454 PrintAndLog("|---|----------------|----------------|");
1460 int CmdHF14AMfCSetUID(const char *Cmd
)
1462 uint8_t wipeCard
= 0;
1463 uint8_t uid
[8] = {0x00};
1464 uint8_t oldUid
[8] = {0x00};
1465 uint8_t atqa
[2] = {0x00};
1466 uint8_t sak
[1] = {0x00};
1467 uint8_t atqaPresent
= 1;
1472 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1473 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1474 PrintAndLog("sample: hf mf csetuid 01020304");
1475 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1476 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1477 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1481 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1482 PrintAndLog("UID must include 8 HEX symbols");
1487 ctmp
= param_getchar(Cmd
, argi
);
1488 if (ctmp
== 'w' || ctmp
== 'W') {
1494 if (param_getchar(Cmd
, argi
)) {
1495 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1496 PrintAndLog("ATQA must include 4 HEX symbols");
1500 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1501 PrintAndLog("SAK must include 2 HEX symbols");
1510 ctmp
= param_getchar(Cmd
, argi
);
1511 if (ctmp
== 'w' || ctmp
== 'W') {
1516 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1518 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1520 PrintAndLog("Can't set UID. error=%d", res
);
1524 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1525 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1529 int CmdHF14AMfCSetBlk(const char *Cmd
)
1531 uint8_t memBlock
[16] = {0x00};
1532 uint8_t blockNo
= 0;
1533 bool wipeCard
= FALSE
;
1536 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1537 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1538 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1539 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1540 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1544 blockNo
= param_get8(Cmd
, 0);
1546 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1547 PrintAndLog("block data must include 32 HEX symbols");
1551 char ctmp
= param_getchar(Cmd
, 2);
1552 wipeCard
= (ctmp
== 'w' || ctmp
== 'W');
1553 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16));
1555 res
= mfCSetBlock(blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1557 PrintAndLog("Can't write block. error=%d", res
);
1564 int CmdHF14AMfCLoad(const char *Cmd
)
1567 char filename
[FILE_PATH_SIZE
] = {0x00};
1568 char * fnameptr
= filename
;
1569 char buf
[64] = {0x00};
1570 uint8_t buf8
[64] = {0x00};
1571 uint8_t fillFromEmulator
= 0;
1572 int i
, len
, blockNum
, flags
=0;
1574 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1575 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1576 PrintAndLog("or from emulator memory (option `e`)");
1577 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1578 PrintAndLog(" or: hf mf cload e ");
1579 PrintAndLog(" sample: hf mf cload filename");
1583 char ctmp
= param_getchar(Cmd
, 0);
1584 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1586 if (fillFromEmulator
) {
1587 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1588 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1589 PrintAndLog("Cant get block: %d", blockNum
);
1592 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1593 if (blockNum
== 1) flags
= 0; // just write
1594 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1596 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1597 PrintAndLog("Cant set magic card block: %d", blockNum
);
1604 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1606 memcpy(filename
, Cmd
, len
);
1609 sprintf(fnameptr
, ".eml");
1612 f
= fopen(filename
, "r");
1614 PrintAndLog("File not found or locked.");
1621 memset(buf
, 0, sizeof(buf
));
1623 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1625 PrintAndLog("File reading error.");
1629 if (strlen(buf
) < 32) {
1630 if(strlen(buf
) && feof(f
))
1632 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1636 for (i
= 0; i
< 32; i
+= 2)
1637 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1639 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1640 if (blockNum
== 1) flags
= 0; // just write
1641 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1643 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1644 PrintAndLog("Can't set magic card block: %d", blockNum
);
1649 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1653 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1654 PrintAndLog("File content error. There must be 64 blocks");
1657 PrintAndLog("Loaded from file: %s", filename
);
1663 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1664 uint8_t memBlock
[16];
1665 uint8_t blockNo
= 0;
1667 memset(memBlock
, 0x00, sizeof(memBlock
));
1669 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1670 PrintAndLog("Usage: hf mf cgetblk <block number>");
1671 PrintAndLog("sample: hf mf cgetblk 1");
1672 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1676 blockNo
= param_get8(Cmd
, 0);
1678 PrintAndLog("--block number:%2d ", blockNo
);
1680 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1682 PrintAndLog("Can't read block. error=%d", res
);
1686 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1691 int CmdHF14AMfCGetSc(const char *Cmd
) {
1692 uint8_t memBlock
[16] = {0x00};
1693 uint8_t sectorNo
= 0;
1696 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1697 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1698 PrintAndLog("sample: hf mf cgetsc 0");
1699 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1703 sectorNo
= param_get8(Cmd
, 0);
1704 if (sectorNo
> 15) {
1705 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1709 PrintAndLog("--sector number:%d ", sectorNo
);
1711 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1712 for (i
= 0; i
< 4; i
++) {
1713 if (i
== 1) flags
= 0;
1714 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1716 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1718 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1722 PrintAndLog("block %3d data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1728 int CmdHF14AMfCSave(const char *Cmd
) {
1731 char filename
[FILE_PATH_SIZE
] = {0x00};
1732 char * fnameptr
= filename
;
1733 uint8_t fillFromEmulator
= 0;
1734 uint8_t buf
[64] = {0x00};
1735 int i
, j
, len
, flags
;
1737 // memset(filename, 0, sizeof(filename));
1738 // memset(buf, 0, sizeof(buf));
1740 if (param_getchar(Cmd
, 0) == 'h') {
1741 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1742 PrintAndLog("or into emulator memory (option `e`)");
1743 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1744 PrintAndLog(" sample: hf mf esave ");
1745 PrintAndLog(" hf mf esave filename");
1746 PrintAndLog(" hf mf esave e \n");
1750 char ctmp
= param_getchar(Cmd
, 0);
1751 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1753 if (fillFromEmulator
) {
1754 // put into emulator
1755 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1756 for (i
= 0; i
< 16 * 4; i
++) {
1757 if (i
== 1) flags
= 0;
1758 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1760 if (mfCGetBlock(i
, buf
, flags
)) {
1761 PrintAndLog("Cant get block: %d", i
);
1765 if (mfEmlSetMem(buf
, i
, 1)) {
1766 PrintAndLog("Cant set emul block: %d", i
);
1773 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1777 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1778 PrintAndLog("Cant get block: %d", 0);
1779 len
= sprintf(fnameptr
, "dump");
1783 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1784 sprintf(fnameptr
, "%02x", buf
[j
]);
1787 memcpy(filename
, Cmd
, len
);
1791 sprintf(fnameptr
, ".eml");
1794 f
= fopen(filename
, "w+");
1797 PrintAndLog("File not found or locked.");
1802 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1803 for (i
= 0; i
< 16 * 4; i
++) {
1804 if (i
== 1) flags
= 0;
1805 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1807 if (mfCGetBlock(i
, buf
, flags
)) {
1808 PrintAndLog("Cant get block: %d", i
);
1811 for (j
= 0; j
< 16; j
++)
1812 fprintf(f
, "%02x", buf
[j
]);
1817 PrintAndLog("Saved to file: %s", filename
);
1824 int CmdHF14AMfSniff(const char *Cmd
){
1826 bool wantLogToFile
= 0;
1827 bool wantDecrypt
= 0;
1828 //bool wantSaveToEml = 0; TODO
1829 bool wantSaveToEmlFile
= 0;
1839 uint8_t atqa
[2] = {0x00};
1842 uint8_t *buf
= NULL
;
1843 uint16_t bufsize
= 0;
1844 uint8_t *bufPtr
= NULL
;
1846 char ctmp
= param_getchar(Cmd
, 0);
1847 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1848 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1849 PrintAndLog("You can specify:");
1850 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1851 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1852 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1853 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1854 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1855 PrintAndLog(" sample: hf mf sniff l d e");
1859 for (int i
= 0; i
< 4; i
++) {
1860 ctmp
= param_getchar(Cmd
, i
);
1861 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1862 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1863 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1864 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1867 printf("-------------------------------------------------------------------------\n");
1868 printf("Executing command. \n");
1869 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1870 printf("Press the key on pc keyboard to abort the client.\n");
1871 printf("-------------------------------------------------------------------------\n");
1873 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1874 clearCommandBuffer();
1883 printf("\naborted via keyboard!\n");
1888 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1889 res
= resp
.arg
[0] & 0xff;
1890 uint16_t traceLen
= resp
.arg
[1];
1893 if (res
== 0) return 0; // we are done
1895 if (res
== 1) { // there is (more) data to be transferred
1896 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1897 if (traceLen
> bufsize
) {
1899 if (buf
== NULL
) { // not yet allocated
1900 p
= malloc(traceLen
);
1901 } else { // need more memory
1902 p
= realloc(buf
, traceLen
);
1905 PrintAndLog("Cannot allocate memory for trace");
1913 memset(buf
, 0x00, traceLen
);
1915 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1920 if (res
== 2) { // received all data, start displaying
1921 blockLen
= bufPtr
- buf
;
1924 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1925 while (bufPtr
- buf
< blockLen
) {
1926 bufPtr
+= 6; // skip (void) timing information
1927 len
= *((uint16_t *)bufPtr
);
1935 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
1936 memcpy(uid
, bufPtr
+ 2, 7);
1937 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1938 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
1940 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1941 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
1945 if (wantLogToFile
|| wantDecrypt
) {
1946 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
1947 AddLogCurrentDT(logHexFileName
);
1950 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1952 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1954 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1956 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1960 bufPtr
+= ((len
-1)/8+1); // ignore parity
1971 //needs nt, ar, at, Data to decrypt
1972 int CmdDecryptTraceCmds(const char *Cmd
){
1975 param_gethex_ex(Cmd
,3,data
,&len
);
1976 return tryDecryptWord(param_get32ex(Cmd
,0,0,16),param_get32ex(Cmd
,1,0,16),param_get32ex(Cmd
,2,0,16),data
,len
/2);
1979 static command_t CommandTable
[] =
1981 {"help", CmdHelp
, 1, "This help"},
1982 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1983 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1984 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1985 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1986 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1987 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1988 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1989 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1990 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1991 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1992 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1993 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1994 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1995 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1996 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1997 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1998 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1999 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2000 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2001 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2002 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2003 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2004 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2005 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2006 {"decrypt", CmdDecryptTraceCmds
,1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2007 {NULL
, NULL
, 0, NULL
}
2010 int CmdHFMF(const char *Cmd
)
2013 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
2015 CmdsParse(CommandTable
, Cmd
);
2019 int CmdHelp(const char *Cmd
)
2021 CmdsHelp(CommandTable
);