]>
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 //-----------------------------------------------------------------------------
12 #include "cmdhfmfhard.h"
13 #include "nonce2key/nonce2key.h"
15 static int CmdHelp(const char *Cmd
);
17 int CmdHF14AMifare(const char *Cmd
)
20 uint32_t nt
= 0, nr
= 0;
21 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
26 char cmdp
= param_getchar(Cmd
, 0);
27 if ( cmdp
== 'H' || cmdp
== 'h') {
28 PrintAndLog("Usage: hf mf mifare <block number>");
29 PrintAndLog(" sample: hf mf mifare 0");
33 blockNo
= param_get8(Cmd
, 0);
34 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, 0}};
37 printf("-------------------------------------------------------------------------\n");
38 printf("Executing command. Expected execution time: 25sec on average :-)\n");
39 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
40 printf("-------------------------------------------------------------------------\n");
61 printf("\naborted via keyboard!\n");
66 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
70 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
71 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
72 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
73 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
74 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
77 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
78 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
79 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
80 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
81 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
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...");
99 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
104 PrintAndLog("Time in darkside: %.0f ticks - %4.2f sec\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
);
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);
144 clearCommandBuffer();
148 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
149 uint8_t isOK
= resp
.arg
[0] & 0xff;
150 PrintAndLog("isOk:%02x", isOK
);
152 PrintAndLog("Command execute timeout");
158 int CmdHF14AMfRdBl(const char *Cmd
)
162 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
168 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
169 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
173 blockNo
= param_get8(Cmd
, 0);
174 cmdp
= param_getchar(Cmd
, 1);
176 PrintAndLog("Key type must be A or B");
179 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
180 if (param_gethex(Cmd
, 2, key
, 12)) {
181 PrintAndLog("Key must include 12 HEX symbols");
184 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
186 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
187 memcpy(c
.d
.asBytes
, key
, 6);
188 clearCommandBuffer();
192 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
193 uint8_t isOK
= resp
.arg
[0] & 0xff;
194 uint8_t *data
= resp
.d
.asBytes
;
197 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
199 PrintAndLog("isOk:%02x", isOK
);
201 PrintAndLog("Command execute timeout");
207 int CmdHF14AMfRdSc(const char *Cmd
)
210 uint8_t sectorNo
= 0;
212 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
214 uint8_t *data
= NULL
;
218 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
219 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
223 sectorNo
= param_get8(Cmd
, 0);
225 PrintAndLog("Sector number must be less than 40");
228 cmdp
= param_getchar(Cmd
, 1);
229 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
230 PrintAndLog("Key type must be A or B");
233 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
234 if (param_gethex(Cmd
, 2, key
, 12)) {
235 PrintAndLog("Key must include 12 HEX symbols");
238 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
240 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
241 memcpy(c
.d
.asBytes
, key
, 6);
242 clearCommandBuffer();
247 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
248 isOK
= resp
.arg
[0] & 0xff;
249 data
= resp
.d
.asBytes
;
251 PrintAndLog("isOk:%02x", isOK
);
253 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
254 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
256 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
259 PrintAndLog("Command execute timeout");
265 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
270 return 32 * 4 + (sectorNo
- 32) * 16;
274 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
283 int CmdHF14AMfDump(const char *Cmd
)
285 uint8_t sectorNo
, blockNo
;
289 uint8_t rights
[40][4];
290 uint8_t carddata
[256][16];
291 uint8_t numSectors
= 16;
298 char cmdp
= param_getchar(Cmd
, 0);
300 case '0' : numSectors
= 5; break;
302 case '\0': numSectors
= 16; break;
303 case '2' : numSectors
= 32; break;
304 case '4' : numSectors
= 40; break;
305 default: numSectors
= 16;
308 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
309 PrintAndLog("Usage: hf mf dump [card memory]");
310 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
312 PrintAndLog("Samples: hf mf dump");
313 PrintAndLog(" hf mf dump 4");
317 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
318 PrintAndLog("Could not find file dumpkeys.bin");
322 // Read keys A from file
324 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
325 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
326 if ( bytes_read
== 0) {
327 PrintAndLog("File reading error.");
333 // Read keys B from file
334 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
335 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
336 if ( bytes_read
== 0) {
337 PrintAndLog("File reading error.");
345 PrintAndLog("|-----------------------------------------|");
346 PrintAndLog("|------ Reading sector access bits...-----|");
347 PrintAndLog("|-----------------------------------------|");
349 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
350 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
351 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
352 clearCommandBuffer();
355 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
356 uint8_t isOK
= resp
.arg
[0] & 0xff;
357 uint8_t *data
= resp
.d
.asBytes
;
359 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
360 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
361 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
362 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
364 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
365 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
366 rights
[sectorNo
][3] = 0x01;
369 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
370 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
371 rights
[sectorNo
][3] = 0x01;
375 PrintAndLog("|-----------------------------------------|");
376 PrintAndLog("|----- Dumping all blocks to file... -----|");
377 PrintAndLog("|-----------------------------------------|");
380 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
381 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
382 bool received
= false;
384 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
385 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
386 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
387 clearCommandBuffer();
389 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
390 } else { // data block. Check if it can be read with key A or key B
391 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
392 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
393 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
394 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
396 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
397 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
399 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
400 } else { // key A would work
401 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
402 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
403 clearCommandBuffer();
405 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
410 isOK
= resp
.arg
[0] & 0xff;
411 uint8_t *data
= resp
.d
.asBytes
;
412 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
413 data
[0] = (keyA
[sectorNo
][0]);
414 data
[1] = (keyA
[sectorNo
][1]);
415 data
[2] = (keyA
[sectorNo
][2]);
416 data
[3] = (keyA
[sectorNo
][3]);
417 data
[4] = (keyA
[sectorNo
][4]);
418 data
[5] = (keyA
[sectorNo
][5]);
419 data
[10] = (keyB
[sectorNo
][0]);
420 data
[11] = (keyB
[sectorNo
][1]);
421 data
[12] = (keyB
[sectorNo
][2]);
422 data
[13] = (keyB
[sectorNo
][3]);
423 data
[14] = (keyB
[sectorNo
][4]);
424 data
[15] = (keyB
[sectorNo
][5]);
427 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
428 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
430 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
436 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
443 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
444 PrintAndLog("Could not create file name dumpdata.bin");
447 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
448 fwrite(carddata
, 1, 16*numblocks
, fout
);
450 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
456 int CmdHF14AMfRestore(const char *Cmd
)
458 uint8_t sectorNo
,blockNo
;
460 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
461 uint8_t bldata
[16] = {0x00};
469 char cmdp
= param_getchar(Cmd
, 0);
471 case '0' : numSectors
= 5; break;
473 case '\0': numSectors
= 16; break;
474 case '2' : numSectors
= 32; break;
475 case '4' : numSectors
= 40; break;
476 default: numSectors
= 16;
479 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
480 PrintAndLog("Usage: hf mf restore [card memory]");
481 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
483 PrintAndLog("Samples: hf mf restore");
484 PrintAndLog(" hf mf restore 4");
488 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
489 PrintAndLog("Could not find file dumpkeys.bin");
494 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
495 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
496 if ( bytes_read
== 0) {
497 PrintAndLog("File reading error (dumpkeys.bin).");
503 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
504 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
505 if ( bytes_read
== 0) {
506 PrintAndLog("File reading error (dumpkeys.bin).");
514 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
515 PrintAndLog("Could not find file dumpdata.bin");
518 PrintAndLog("Restoring dumpdata.bin to card");
520 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
521 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
522 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
523 memcpy(c
.d
.asBytes
, key
, 6);
524 bytes_read
= fread(bldata
, 1, 16, fdump
);
525 if ( bytes_read
== 0) {
526 PrintAndLog("File reading error (dumpdata.bin).");
531 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
532 bldata
[0] = (keyA
[sectorNo
][0]);
533 bldata
[1] = (keyA
[sectorNo
][1]);
534 bldata
[2] = (keyA
[sectorNo
][2]);
535 bldata
[3] = (keyA
[sectorNo
][3]);
536 bldata
[4] = (keyA
[sectorNo
][4]);
537 bldata
[5] = (keyA
[sectorNo
][5]);
538 bldata
[10] = (keyB
[sectorNo
][0]);
539 bldata
[11] = (keyB
[sectorNo
][1]);
540 bldata
[12] = (keyB
[sectorNo
][2]);
541 bldata
[13] = (keyB
[sectorNo
][3]);
542 bldata
[14] = (keyB
[sectorNo
][4]);
543 bldata
[15] = (keyB
[sectorNo
][5]);
546 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
548 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
549 clearCommandBuffer();
553 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
554 uint8_t isOK
= resp
.arg
[0] & 0xff;
555 PrintAndLog("isOk:%02x", isOK
);
557 PrintAndLog("Command execute timeout");
566 int CmdHF14AMfNested(const char *Cmd
)
568 int i
, j
, res
, iterations
;
569 sector
*e_sector
= NULL
;
572 uint8_t trgBlockNo
= 0;
573 uint8_t trgKeyType
= 0;
574 uint8_t SectorsCnt
= 0;
575 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
576 uint8_t keyBlock
[6*6];
578 bool transferToEml
= false;
580 bool createDumpFile
= false;
582 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
583 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
588 PrintAndLog("Usage:");
589 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
590 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
591 PrintAndLog(" <target block number> <target key A/B> [t]");
592 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
593 PrintAndLog("t - transfer keys into emulator memory");
594 PrintAndLog("d - write keys to binary file");
596 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
597 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
598 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
599 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
603 cmdp
= param_getchar(Cmd
, 0);
604 blockNo
= param_get8(Cmd
, 1);
605 ctmp
= param_getchar(Cmd
, 2);
607 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
608 PrintAndLog("Key type must be A or B");
612 if (ctmp
!= 'A' && ctmp
!= 'a')
615 if (param_gethex(Cmd
, 3, key
, 12)) {
616 PrintAndLog("Key must include 12 HEX symbols");
620 if (cmdp
== 'o' || cmdp
== 'O') {
622 trgBlockNo
= param_get8(Cmd
, 4);
623 ctmp
= param_getchar(Cmd
, 5);
624 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
625 PrintAndLog("Target key type must be A or B");
628 if (ctmp
!= 'A' && ctmp
!= 'a')
633 case '0': SectorsCnt
= 05; break;
634 case '1': SectorsCnt
= 16; break;
635 case '2': SectorsCnt
= 32; break;
636 case '4': SectorsCnt
= 40; break;
637 default: SectorsCnt
= 16;
641 ctmp
= param_getchar(Cmd
, 4);
642 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
643 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
645 ctmp
= param_getchar(Cmd
, 6);
646 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
647 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
650 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
652 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
653 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
654 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
655 case -4 : PrintAndLog("No valid key found"); break;
657 key64
= bytes_to_num(keyBlock
, 6);
659 // transfer key to the emulator
661 uint8_t sectortrailer
;
662 if (trgBlockNo
< 32*4) { // 4 block sector
663 sectortrailer
= (trgBlockNo
& 0x03) + 3;
664 } else { // 16 block sector
665 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
667 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
670 num_to_bytes(key64
, 6, keyBlock
);
672 num_to_bytes(key64
, 6, &keyBlock
[10]);
673 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
676 default : PrintAndLog("Unknown Error.\n");
680 else { // ------------------------------------ multiple sectors working
681 clock_t t1
= clock();
683 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
684 if (e_sector
== NULL
) return 1;
686 //test current key and additional standard keys first
687 memcpy(keyBlock
, key
, 6);
688 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
689 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
690 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
691 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
692 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
694 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
695 for (i
= 0; i
< SectorsCnt
; i
++) {
696 for (j
= 0; j
< 2; j
++) {
697 if (e_sector
[i
].foundKey
[j
]) continue;
699 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
702 e_sector
[i
].Key
[j
] = key64
;
703 e_sector
[i
].foundKey
[j
] = TRUE
;
707 clock_t t2
= clock() - t1
;
709 PrintAndLog("Time to check 6 known keys: %.0f ticks %4.2f sec", (float)t2
, ((float)t2
)/CLOCKS_PER_SEC
);
711 PrintAndLog("enter nested...");
715 bool calibrate
= true;
717 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
718 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
719 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
721 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
723 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
725 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
726 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
727 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
728 case -4 : //key not found
735 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
736 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
739 default : PrintAndLog("Unknown Error.\n");
749 PrintAndLog("Time in nested: %.0f ticks %4.2f sec (%4.2f sec per key)\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
, ((float)t1
)/iterations
/CLOCKS_PER_SEC
);
751 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
752 PrintAndLog("trying to read key B...");
753 for (i
= 0; i
< SectorsCnt
; i
++) {
754 // KEY A but not KEY B
755 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
757 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
759 PrintAndLog("Reading block %d", sectrail
);
761 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
762 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
763 clearCommandBuffer();
767 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
769 uint8_t isOK
= resp
.arg
[0] & 0xff;
772 uint8_t *data
= resp
.d
.asBytes
;
773 key64
= bytes_to_num(data
+10, 6);
775 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
776 e_sector
[i
].foundKey
[1] = TRUE
;
777 e_sector
[i
].Key
[1] = key64
;
784 printKeyTable( SectorsCnt
, e_sector
);
786 // transfer them to the emulator
788 for (i
= 0; i
< SectorsCnt
; i
++) {
789 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
790 if (e_sector
[i
].foundKey
[0])
791 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
792 if (e_sector
[i
].foundKey
[1])
793 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
794 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
799 if (createDumpFile
) {
800 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
801 PrintAndLog("Could not create file dumpkeys.bin");
805 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
806 for(i
=0; i
<SectorsCnt
; i
++) {
807 if (e_sector
[i
].foundKey
[0]){
808 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
809 fwrite ( tempkey
, 1, 6, fkeys
);
812 fwrite ( &standart
, 1, 6, fkeys
);
815 for(i
=0; i
<SectorsCnt
; i
++) {
816 if (e_sector
[i
].foundKey
[1]){
817 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
818 fwrite ( tempkey
, 1, 6, fkeys
);
821 fwrite ( &standart
, 1, 6, fkeys
);
832 int CmdHF14AMfNestedHard(const char *Cmd
)
836 uint8_t trgBlockNo
= 0;
837 uint8_t trgKeyType
= 0;
838 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
839 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
842 ctmp
= param_getchar(Cmd
, 0);
844 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) {
845 PrintAndLog("Usage:");
846 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
847 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
848 PrintAndLog(" or hf mf hardnested r [known target key]");
850 PrintAndLog("Options: ");
851 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
852 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
853 PrintAndLog(" r: Read nonces.bin and start attack");
855 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
856 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
857 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
858 PrintAndLog(" sample4: hf mf hardnested r");
860 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
861 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
865 bool know_target_key
= false;
866 bool nonce_file_read
= false;
867 bool nonce_file_write
= false;
872 if (ctmp
== 'R' || ctmp
== 'r') {
873 nonce_file_read
= true;
874 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
875 know_target_key
= true;
877 } else if (ctmp
== 'T' || ctmp
== 't') {
878 tests
= param_get32ex(Cmd
, 1, 100, 10);
880 blockNo
= param_get8(Cmd
, 0);
881 ctmp
= param_getchar(Cmd
, 1);
882 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
883 PrintAndLog("Key type must be A or B");
886 if (ctmp
!= 'A' && ctmp
!= 'a') {
890 if (param_gethex(Cmd
, 2, key
, 12)) {
891 PrintAndLog("Key must include 12 HEX symbols");
895 trgBlockNo
= param_get8(Cmd
, 3);
896 ctmp
= param_getchar(Cmd
, 4);
897 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
898 PrintAndLog("Target key type must be A or B");
901 if (ctmp
!= 'A' && ctmp
!= 'a') {
907 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
908 know_target_key
= true;
912 while ((ctmp
= param_getchar(Cmd
, i
))) {
913 if (ctmp
== 's' || ctmp
== 'S') {
915 } else if (ctmp
== 'w' || ctmp
== 'W') {
916 nonce_file_write
= true;
918 PrintAndLog("Possible options are w and/or s");
925 PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d ",
928 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
929 know_target_key
?"":" (not set)",
930 nonce_file_write
?"write":nonce_file_read
?"read":"none",
934 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
938 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
939 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
948 int CmdHF14AMfChk(const char *Cmd
)
951 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
952 PrintAndLog(" * - all sectors");
953 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
954 PrintAndLog("d - write keys to binary file");
955 PrintAndLog("t - write keys to emulator memory\n");
956 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
957 PrintAndLog(" hf mf chk *1 ? t");
958 PrintAndLog(" hf mf chk *1 ? d");
963 char filename
[FILE_PATH_SIZE
]={0};
965 uint8_t *keyBlock
= NULL
, *p
;
966 uint8_t stKeyBlock
= 20;
968 sector
*e_sector
= NULL
;
974 uint8_t SectorsCnt
= 1;
978 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
980 int transferToEml
= 0;
981 int createDumpFile
= 0;
983 keyBlock
= calloc(stKeyBlock
, 6);
984 if (keyBlock
== NULL
) return 1;
986 uint64_t defaultKeys
[] = {
987 0xffffffffffff, // Default key (first key used by program if no user defined key)
988 0x000000000000, // Blank key
989 0xa0a1a2a3a4a5, // NFCForum MAD key
1001 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1003 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1004 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1007 if (param_getchar(Cmd
, 0)=='*') {
1009 switch(param_getchar(Cmd
+1, 0)) {
1010 case '0': SectorsCnt
= 5; break;
1011 case '1': SectorsCnt
= 16; break;
1012 case '2': SectorsCnt
= 32; break;
1013 case '4': SectorsCnt
= 40; break;
1014 default: SectorsCnt
= 16;
1017 blockNo
= param_get8(Cmd
, 0);
1020 ctmp
= param_getchar(Cmd
, 1);
1032 PrintAndLog("Key type must be A , B or ?");
1037 ctmp
= param_getchar(Cmd
, 2);
1038 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1039 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1041 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1042 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1043 if ( stKeyBlock
- keycnt
< 2) {
1044 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1046 PrintAndLog("Cannot allocate memory for Keys");
1052 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1053 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1054 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1057 // May be a dic file
1058 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1059 PrintAndLog("File name too long");
1064 if ( (f
= fopen( filename
, "r")) ) {
1065 while( fgets(buf
, sizeof(buf
), f
) ){
1066 if (strlen(buf
) < 12 || buf
[11] == '\n')
1069 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1071 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1073 if (!isxdigit(buf
[0])){
1074 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1080 if ( stKeyBlock
- keycnt
< 2) {
1081 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1083 PrintAndLog("Cannot allocate memory for defKeys");
1090 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1091 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1092 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1094 memset(buf
, 0, sizeof(buf
));
1098 PrintAndLog("File: %s: not found or locked.", filename
);
1107 PrintAndLog("No key specified, trying default keys");
1108 for (;keycnt
< defaultKeysSize
; keycnt
++)
1109 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1110 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1111 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1114 // initialize storage for found keys
1115 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1116 if (e_sector
== NULL
) {
1121 uint8_t trgKeyType
= 0;
1122 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1125 clock_t t1
= clock();
1128 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
1131 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1133 // skip already found keys.
1134 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1137 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1139 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1141 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1143 //PrintAndLog("Sector:%3d Block:%3d, key type: %C -- Found key [%012"llx"]", i, b, trgKeyType ? 'B':'A', key64);
1145 e_sector
[i
].Key
[trgKeyType
] = key64
;
1146 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1149 e_sector
[i
].Key
[trgKeyType
] = 0xffffffffffff;
1150 e_sector
[i
].foundKey
[trgKeyType
] = FALSE
;
1153 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1159 printf("Time in checkkeys: %.0f ticks %1.2f sec (%1.2f sec per key)\n\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
, ((float)t1
)/keycnt
/CLOCKS_PER_SEC
);
1161 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1162 PrintAndLog("testing to read B...");
1163 for (i
= 0; i
< SectorsCnt
; i
++) {
1164 // KEY A but not KEY B
1165 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1167 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1169 PrintAndLog("Reading block %d", sectrail
);
1171 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1172 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1173 clearCommandBuffer();
1177 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1179 uint8_t isOK
= resp
.arg
[0] & 0xff;
1180 if (!isOK
) continue;
1182 uint8_t *data
= resp
.d
.asBytes
;
1183 key64
= bytes_to_num(data
+10, 6);
1185 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1186 e_sector
[i
].foundKey
[1] = 1;
1187 e_sector
[i
].Key
[1] = key64
;
1194 printKeyTable( SectorsCnt
, e_sector
);
1196 if (transferToEml
) {
1197 uint8_t block
[16] = {0x00};
1198 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1199 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1200 if (e_sector
[i
].foundKey
[0])
1201 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1202 if (e_sector
[i
].foundKey
[1])
1203 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1204 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1206 PrintAndLog("Found keys have been transferred to the emulator memory");
1209 if (createDumpFile
) {
1210 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1211 if (fkeys
== NULL
) {
1212 PrintAndLog("Could not create file dumpkeys.bin");
1217 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1219 for( i
=0; i
<SectorsCnt
; i
++) {
1220 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1221 fwrite ( tempkey
, 1, 6, fkeys
);
1223 for(i
=0; i
<SectorsCnt
; i
++) {
1224 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1225 fwrite ( tempkey
, 1, 6, fkeys
);
1228 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1237 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1238 PrintAndLog("|---|----------------|---|----------------|---|");
1239 PrintAndLog("|sec|key A |res|key B |res|");
1240 PrintAndLog("|---|----------------|---|----------------|---|");
1241 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1242 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1243 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1244 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1247 PrintAndLog("|---|----------------|---|----------------|---|");
1250 int CmdHF14AMf1kSim(const char *Cmd
)
1252 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1253 uint8_t exitAfterNReads
= 0;
1256 uint8_t cmdp
= param_getchar(Cmd
, 0);
1258 if (cmdp
== 'h' || cmdp
== 'H') {
1259 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1260 PrintAndLog(" h this help");
1261 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1262 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1263 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1264 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1266 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1270 if (param_getchar(Cmd
, pnr
) == 'u') {
1271 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1273 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1274 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1275 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1277 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1283 if (param_getchar(Cmd
, pnr
) == 'n') {
1284 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1288 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1289 //Using a flag to signal interactiveness, least significant bit
1290 flags
|= FLAG_INTERACTIVE
;
1294 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1295 //Using a flag to signal interactiveness, least significant bit
1296 flags
|= FLAG_NR_AR_ATTACK
;
1299 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1300 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1301 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1307 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1308 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1309 clearCommandBuffer();
1312 if(flags
& FLAG_INTERACTIVE
)
1318 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1321 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1323 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1325 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1327 memset(data
, 0x00, sizeof(data
));
1328 memset(key
, 0x00, sizeof(key
));
1329 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1331 memcpy(data
, resp
.d
.asBytes
, len
);
1333 uint64_t corr_uid
= 0;
1335 // this IF? what was I thinking of?
1336 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1337 corr_uid
= ((uint64_t)(data
[3] << 24)) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1338 tryMfk32(corr_uid
, data
, key
);
1340 corr_uid
|= (uint64_t)data
[2] << 48;
1341 corr_uid
|= (uint64_t)data
[1] << 40;
1342 corr_uid
|= (uint64_t)data
[0] << 32;
1343 corr_uid
|= (uint64_t)data
[7] << 24;
1344 corr_uid
|= (uint64_t)data
[6] << 16;
1345 corr_uid
|= (uint64_t)data
[5] << 8;
1346 corr_uid
|= (uint64_t)data
[4];
1347 tryMfk64(corr_uid
, data
, key
);
1355 int CmdHF14AMfDbg(const char *Cmd
)
1357 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1359 PrintAndLog("Max debug mode parameter is 4 \n");
1362 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1363 PrintAndLog("Usage: hf mf dbg <debug level>");
1364 PrintAndLog(" 0 - no debug messages");
1365 PrintAndLog(" 1 - error messages");
1366 PrintAndLog(" 2 - plus information messages");
1367 PrintAndLog(" 3 - plus debug messages");
1368 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1369 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1373 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1379 int CmdHF14AMfEGet(const char *Cmd
)
1381 uint8_t blockNo
= 0;
1382 uint8_t data
[16] = {0x00};
1384 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1385 PrintAndLog("Usage: hf mf eget <block number>");
1386 PrintAndLog(" sample: hf mf eget 0 ");
1390 blockNo
= param_get8(Cmd
, 0);
1393 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1394 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1396 PrintAndLog("Command execute timeout");
1402 int CmdHF14AMfEClear(const char *Cmd
)
1404 if (param_getchar(Cmd
, 0) == 'h') {
1405 PrintAndLog("Usage: hf mf eclr");
1406 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1410 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1415 int CmdHF14AMfESet(const char *Cmd
)
1417 uint8_t memBlock
[16];
1418 uint8_t blockNo
= 0;
1420 memset(memBlock
, 0x00, sizeof(memBlock
));
1422 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1423 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1424 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1428 blockNo
= param_get8(Cmd
, 0);
1430 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1431 PrintAndLog("block data must include 32 HEX symbols");
1436 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1437 memcpy(c
.d
.asBytes
, memBlock
, 16);
1442 int CmdHF14AMfELoad(const char *Cmd
)
1445 char filename
[FILE_PATH_SIZE
];
1446 char *fnameptr
= filename
;
1447 char buf
[64] = {0x00};
1448 uint8_t buf8
[64] = {0x00};
1449 int i
, len
, blockNum
, numBlocks
;
1450 int nameParamNo
= 1;
1451 uint8_t blockWidth
= 32;
1452 char ctmp
= param_getchar(Cmd
, 0);
1454 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1455 PrintAndLog("It loads emul dump from the file `filename.eml`");
1456 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1457 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1459 PrintAndLog(" sample: hf mf eload filename");
1460 PrintAndLog(" hf mf eload 4 filename");
1465 case '0' : numBlocks
= 5*4; break;
1467 case '\0': numBlocks
= 16*4; break;
1468 case '2' : numBlocks
= 32*4; break;
1469 case '4' : numBlocks
= 256; break;
1470 case 'U' : // fall through
1471 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1477 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1478 if (numblk2
> 0) numBlocks
= numblk2
;
1480 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1482 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1486 sprintf(fnameptr
, ".eml");
1489 f
= fopen(filename
, "r");
1491 PrintAndLog("File %s not found or locked", filename
);
1497 memset(buf
, 0, sizeof(buf
));
1499 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1501 if (blockNum
>= numBlocks
) break;
1503 PrintAndLog("File reading error.");
1508 if (strlen(buf
) < blockWidth
){
1509 if(strlen(buf
) && feof(f
))
1511 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1516 for (i
= 0; i
< blockWidth
; i
+= 2) {
1517 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1519 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1520 PrintAndLog("Cant set emul block: %3d", blockNum
);
1527 if (blockNum
>= numBlocks
) break;
1532 if ((blockNum
!= numBlocks
)) {
1533 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1536 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1540 int CmdHF14AMfESave(const char *Cmd
)
1543 char filename
[FILE_PATH_SIZE
];
1544 char * fnameptr
= filename
;
1546 int i
, j
, len
, numBlocks
;
1547 int nameParamNo
= 1;
1549 memset(filename
, 0, sizeof(filename
));
1550 memset(buf
, 0, sizeof(buf
));
1552 char ctmp
= param_getchar(Cmd
, 0);
1554 if ( ctmp
== 'h' || ctmp
== 'H') {
1555 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1556 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1557 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1559 PrintAndLog(" sample: hf mf esave ");
1560 PrintAndLog(" hf mf esave 4");
1561 PrintAndLog(" hf mf esave 4 filename");
1566 case '0' : numBlocks
= 5*4; break;
1568 case '\0': numBlocks
= 16*4; break;
1569 case '2' : numBlocks
= 32*4; break;
1570 case '4' : numBlocks
= 256; break;
1577 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1579 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1581 // user supplied filename?
1583 // get filename (UID from memory)
1584 if (mfEmlGetMem(buf
, 0, 1)) {
1585 PrintAndLog("Can\'t get UID from block: %d", 0);
1586 len
= sprintf(fnameptr
, "dump");
1590 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1591 sprintf(fnameptr
, "%02X", buf
[j
]);
1597 // add file extension
1598 sprintf(fnameptr
, ".eml");
1601 f
= fopen(filename
, "w+");
1604 PrintAndLog("Can't open file %s ", filename
);
1609 for (i
= 0; i
< numBlocks
; i
++) {
1610 if (mfEmlGetMem(buf
, i
, 1)) {
1611 PrintAndLog("Cant get block: %d", i
);
1614 for (j
= 0; j
< 16; j
++)
1615 fprintf(f
, "%02X", buf
[j
]);
1620 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1625 int CmdHF14AMfECFill(const char *Cmd
)
1627 uint8_t keyType
= 0;
1628 uint8_t numSectors
= 16;
1630 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1631 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1632 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1634 PrintAndLog("samples: hf mf ecfill A");
1635 PrintAndLog(" hf mf ecfill A 4");
1636 PrintAndLog("Read card and transfer its data to emulator memory.");
1637 PrintAndLog("Keys must be laid in the emulator memory. \n");
1641 char ctmp
= param_getchar(Cmd
, 0);
1642 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1643 PrintAndLog("Key type must be A or B");
1646 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1648 ctmp
= param_getchar(Cmd
, 1);
1650 case '0' : numSectors
= 5; break;
1652 case '\0': numSectors
= 16; break;
1653 case '2' : numSectors
= 32; break;
1654 case '4' : numSectors
= 40; break;
1655 default: numSectors
= 16;
1658 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1659 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1664 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1669 uint64_t keyA
, keyB
;
1671 char cmdp
= param_getchar(Cmd
, 0);
1673 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1674 PrintAndLog("It prints the keys loaded in the emulator memory");
1675 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1676 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1678 PrintAndLog(" sample: hf mf ekeyprn 1");
1683 case '0' : numSectors
= 5; break;
1685 case '\0': numSectors
= 16; break;
1686 case '2' : numSectors
= 32; break;
1687 case '4' : numSectors
= 40; break;
1688 default: numSectors
= 16;
1691 PrintAndLog("|---|----------------|----------------|");
1692 PrintAndLog("|sec|key A |key B |");
1693 PrintAndLog("|---|----------------|----------------|");
1694 for (i
= 0; i
< numSectors
; i
++) {
1695 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1696 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1699 keyA
= bytes_to_num(data
, 6);
1700 keyB
= bytes_to_num(data
+ 10, 6);
1701 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1703 PrintAndLog("|---|----------------|----------------|");
1708 int CmdHF14AMfCSetUID(const char *Cmd
)
1710 uint8_t wipeCard
= 0;
1711 uint8_t uid
[8] = {0x00};
1712 uint8_t oldUid
[8] = {0x00};
1713 uint8_t atqa
[2] = {0x00};
1714 uint8_t sak
[1] = {0x00};
1715 uint8_t atqaPresent
= 1;
1720 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1721 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1722 PrintAndLog("sample: hf mf csetuid 01020304");
1723 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1724 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1725 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1729 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1730 PrintAndLog("UID must include 8 HEX symbols");
1735 ctmp
= param_getchar(Cmd
, argi
);
1736 if (ctmp
== 'w' || ctmp
== 'W') {
1742 if (param_getchar(Cmd
, argi
)) {
1743 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1744 PrintAndLog("ATQA must include 4 HEX symbols");
1748 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1749 PrintAndLog("SAK must include 2 HEX symbols");
1758 ctmp
= param_getchar(Cmd
, argi
);
1759 if (ctmp
== 'w' || ctmp
== 'W') {
1764 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1766 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1768 PrintAndLog("Can't set UID. error=%d", res
);
1772 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1773 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1777 int CmdHF14AMfCSetBlk(const char *Cmd
)
1779 uint8_t block
[16] = {0x00};
1780 uint8_t blockNo
= 0;
1781 uint8_t params
= MAGIC_SINGLE
;
1784 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1785 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1786 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1787 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1788 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1792 blockNo
= param_get8(Cmd
, 0);
1794 if (param_gethex(Cmd
, 1, block
, 32)) {
1795 PrintAndLog("block data must include 32 HEX symbols");
1799 char ctmp
= param_getchar(Cmd
, 2);
1800 if (ctmp
== 'w' || ctmp
== 'W')
1801 params
|= MAGIC_WIPE
;
1803 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
1805 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
1807 PrintAndLog("Can't write block. error=%d", res
);
1813 int CmdHF14AMfCLoad(const char *Cmd
)
1816 char filename
[FILE_PATH_SIZE
];
1817 char * fnameptr
= filename
;
1818 char buf
[64] = {0x00};
1819 uint8_t buf8
[64] = {0x00};
1820 uint8_t fillFromEmulator
= 0;
1821 int i
, len
, blockNum
, flags
=0;
1823 memset(filename
, 0, sizeof(filename
));
1825 char ctmp
= param_getchar(Cmd
, 0);
1827 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1828 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1829 PrintAndLog("or from emulator memory (option `e`)");
1830 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1831 PrintAndLog(" or: hf mf cload e ");
1832 PrintAndLog(" sample: hf mf cload filename");
1836 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1838 if (fillFromEmulator
) {
1839 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1840 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1841 PrintAndLog("Cant get block: %d", blockNum
);
1844 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1845 if (blockNum
== 1) flags
= 0; // just write
1846 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
1848 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1849 PrintAndLog("Cant set magic card block: %d", blockNum
);
1856 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1858 memcpy(filename
, Cmd
, len
);
1861 sprintf(fnameptr
, ".eml");
1864 f
= fopen(filename
, "r");
1866 PrintAndLog("File not found or locked.");
1873 memset(buf
, 0, sizeof(buf
));
1875 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1877 PrintAndLog("File reading error.");
1881 if (strlen(buf
) < 32) {
1882 if(strlen(buf
) && feof(f
))
1884 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1888 for (i
= 0; i
< 32; i
+= 2)
1889 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1891 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1892 if (blockNum
== 1) flags
= 0; // just write
1893 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
1895 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1896 PrintAndLog("Can't set magic card block: %d", blockNum
);
1902 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1907 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1908 PrintAndLog("File content error. There must be 64 blocks");
1911 PrintAndLog("Loaded from file: %s", filename
);
1917 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1919 uint8_t blockNo
= 0;
1921 memset(data
, 0x00, sizeof(data
));
1922 char ctmp
= param_getchar(Cmd
, 0);
1924 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1925 PrintAndLog("Usage: hf mf cgetblk <block number>");
1926 PrintAndLog("sample: hf mf cgetblk 1");
1927 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1931 blockNo
= param_get8(Cmd
, 0);
1933 PrintAndLog("--block number:%2d ", blockNo
);
1935 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
1937 PrintAndLog("Can't read block. error=%d", res
);
1941 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
1945 int CmdHF14AMfCGetSc(const char *Cmd
) {
1947 uint8_t sectorNo
= 0;
1949 memset(data
, 0x00, sizeof(data
));
1950 char ctmp
= param_getchar(Cmd
, 0);
1952 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1953 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1954 PrintAndLog("sample: hf mf cgetsc 0");
1955 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1959 sectorNo
= param_get8(Cmd
, 0);
1960 if (sectorNo
> 15) {
1961 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1965 PrintAndLog("--sector number:%d ", sectorNo
);
1966 PrintAndLog("block | data");
1968 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1969 for (i
= 0; i
< 4; i
++) {
1970 if (i
== 1) flags
= 0;
1971 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1973 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
1975 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1978 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
1983 int CmdHF14AMfCSave(const char *Cmd
) {
1986 char filename
[FILE_PATH_SIZE
];
1987 char * fnameptr
= filename
;
1988 uint8_t fillFromEmulator
= 0;
1990 int i
, j
, len
, flags
;
1992 memset(filename
, 0, sizeof(filename
));
1993 memset(buf
, 0, sizeof(buf
));
1994 char ctmp
= param_getchar(Cmd
, 0);
1996 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1997 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1998 PrintAndLog("or into emulator memory (option `e`)");
1999 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2000 PrintAndLog(" sample: hf mf esave ");
2001 PrintAndLog(" hf mf esave filename");
2002 PrintAndLog(" hf mf esave e \n");
2005 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2007 if (fillFromEmulator
) {
2008 // put into emulator
2009 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2010 for (i
= 0; i
< 16 * 4; i
++) {
2011 if (i
== 1) flags
= 0;
2012 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2014 if (mfCGetBlock(i
, buf
, flags
)) {
2015 PrintAndLog("Cant get block: %d", i
);
2019 if (mfEmlSetMem(buf
, i
, 1)) {
2020 PrintAndLog("Cant set emul block: %d", i
);
2027 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2029 // get filename based on UID
2032 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2033 PrintAndLog("Cant get block: %d", 0);
2034 len
= sprintf(fnameptr
, "dump");
2037 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2038 sprintf(fnameptr
, "%02x", buf
[j
]);
2041 memcpy(filename
, Cmd
, len
);
2045 // add .eml extension
2046 sprintf(fnameptr
, ".eml");
2049 f
= fopen(filename
, "w+");
2052 PrintAndLog("File not found or locked.");
2057 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2058 for (i
= 0; i
< 16 * 4; i
++) {
2059 if (i
== 1) flags
= 0;
2060 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2062 if (mfCGetBlock(i
, buf
, flags
)) {
2063 PrintAndLog("Cant get block: %d", i
);
2066 for (j
= 0; j
< 16; j
++)
2067 fprintf(f
, "%02x", buf
[j
]);
2072 PrintAndLog("Saved to file: %s", filename
);
2077 int CmdHF14AMfSniff(const char *Cmd
){
2079 bool wantLogToFile
= 0;
2080 bool wantDecrypt
= 0;
2081 //bool wantSaveToEml = 0; TODO
2082 bool wantSaveToEmlFile
= 0;
2093 uint8_t atqa
[2] = {0x00};
2096 uint8_t *buf
= NULL
;
2097 uint16_t bufsize
= 0;
2098 uint8_t *bufPtr
= NULL
;
2100 char ctmp
= param_getchar(Cmd
, 0);
2101 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2102 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
2103 PrintAndLog("You can specify:");
2104 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
2105 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
2106 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2107 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2108 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
2109 PrintAndLog(" sample: hf mf sniff l d e");
2113 for (int i
= 0; i
< 4; i
++) {
2114 ctmp
= param_getchar(Cmd
, i
);
2115 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
2116 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
2117 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2118 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
2121 printf("-------------------------------------------------------------------------\n");
2122 printf("Executing command. \n");
2123 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2124 printf("Press the key on pc keyboard to abort the client.\n");
2125 printf("-------------------------------------------------------------------------\n");
2127 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
2128 clearCommandBuffer();
2136 tmpchar
= getchar();
2138 printf("\naborted via keyboard!\n");
2143 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2144 res
= resp
.arg
[0] & 0xff;
2145 uint16_t traceLen
= resp
.arg
[1];
2150 return 0; // we are done
2153 if (res
== 1) { // there is (more) data to be transferred
2154 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
2155 if (traceLen
> bufsize
) {
2157 if (buf
== NULL
) { // not yet allocated
2158 p
= malloc(traceLen
);
2159 } else { // need more memory
2160 p
= realloc(buf
, traceLen
);
2163 PrintAndLog("Cannot allocate memory for trace");
2171 memset(buf
, 0x00, traceLen
);
2173 if (bufPtr
== NULL
) {
2174 PrintAndLog("Cannot allocate memory for trace");
2178 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2183 if (res
== 2) { // received all data, start displaying
2184 blockLen
= bufPtr
- buf
;
2187 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2188 while (bufPtr
- buf
< blockLen
) {
2189 bufPtr
+= 6; // skip (void) timing information
2190 len
= *((uint16_t *)bufPtr
);
2198 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
2199 memcpy(uid
, bufPtr
+ 2, 7);
2200 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2201 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2203 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2204 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2208 if (wantLogToFile
|| wantDecrypt
) {
2209 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2210 AddLogCurrentDT(logHexFileName
);
2213 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2215 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2217 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2219 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
2223 bufPtr
+= ((len
-1)/8+1); // ignore parity
2234 //needs nt, ar, at, Data to decrypt
2235 int CmdHf14MfDecryptBytes(const char *Cmd
){
2238 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2239 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2240 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2243 param_gethex_ex(Cmd
, 3, data
, &len
);
2246 int limit
= sizeof(data
) / 2;
2251 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2254 static command_t CommandTable
[] = {
2255 {"help", CmdHelp
, 1, "This help"},
2256 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2257 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2258 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2259 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2260 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2261 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2262 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2263 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2264 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2265 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2266 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2267 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2268 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2269 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2270 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2271 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2272 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2273 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2274 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2275 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2276 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2277 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2278 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2279 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2280 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2281 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2282 {NULL
, NULL
, 0, NULL
}
2285 int CmdHFMF(const char *Cmd
) {
2287 clearCommandBuffer();
2288 //WaitForResponseTimeout(CMD_ACK,NULL,100);
2289 CmdsParse(CommandTable
, Cmd
);
2293 int CmdHelp(const char *Cmd
) {
2294 CmdsHelp(CommandTable
);