]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1d516ebc32243f678bcfa17eb6bbcd23861e2d34
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;
25 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
28 printf("-------------------------------------------------------------------------\n");
29 printf("Executing command. Expected execution time: 25sec on average :-)\n");
30 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
31 printf("-------------------------------------------------------------------------\n");
52 printf("\naborted via keyboard!\n");
57 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
59 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
60 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
61 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
62 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
63 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
66 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
67 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
68 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
69 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
70 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
80 if (isOK
!= 1) return 1;
82 // execute original function from util nonce2key
83 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
85 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
86 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
91 printf("------------------------------------------------------------------\n");
92 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
96 PrintAndLog("Time in darkside: %f ticks - %1.2f sec\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
);
101 int CmdHF14AMfWrBl(const char *Cmd
)
105 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
106 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
111 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
112 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
116 blockNo
= param_get8(Cmd
, 0);
117 cmdp
= param_getchar(Cmd
, 1);
119 PrintAndLog("Key type must be A or B");
122 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
123 if (param_gethex(Cmd
, 2, key
, 12)) {
124 PrintAndLog("Key must include 12 HEX symbols");
127 if (param_gethex(Cmd
, 3, bldata
, 32)) {
128 PrintAndLog("Block data must include 32 HEX symbols");
131 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
132 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
134 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
135 memcpy(c
.d
.asBytes
, key
, 6);
136 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
137 clearCommandBuffer();
141 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
142 uint8_t isOK
= resp
.arg
[0] & 0xff;
143 PrintAndLog("isOk:%02x", isOK
);
145 PrintAndLog("Command execute timeout");
151 int CmdHF14AMfRdBl(const char *Cmd
)
155 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
161 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
162 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
166 blockNo
= param_get8(Cmd
, 0);
167 cmdp
= param_getchar(Cmd
, 1);
169 PrintAndLog("Key type must be A or B");
172 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
173 if (param_gethex(Cmd
, 2, key
, 12)) {
174 PrintAndLog("Key must include 12 HEX symbols");
177 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
179 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
180 memcpy(c
.d
.asBytes
, key
, 6);
181 clearCommandBuffer();
185 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
186 uint8_t isOK
= resp
.arg
[0] & 0xff;
187 uint8_t *data
= resp
.d
.asBytes
;
190 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
192 PrintAndLog("isOk:%02x", isOK
);
194 PrintAndLog("Command execute timeout");
200 int CmdHF14AMfRdSc(const char *Cmd
)
203 uint8_t sectorNo
= 0;
205 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
207 uint8_t *data
= NULL
;
211 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
212 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
216 sectorNo
= param_get8(Cmd
, 0);
218 PrintAndLog("Sector number must be less than 40");
221 cmdp
= param_getchar(Cmd
, 1);
222 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
223 PrintAndLog("Key type must be A or B");
226 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
227 if (param_gethex(Cmd
, 2, key
, 12)) {
228 PrintAndLog("Key must include 12 HEX symbols");
231 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
233 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
234 memcpy(c
.d
.asBytes
, key
, 6);
235 clearCommandBuffer();
240 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
241 isOK
= resp
.arg
[0] & 0xff;
242 data
= resp
.d
.asBytes
;
244 PrintAndLog("isOk:%02x", isOK
);
246 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
247 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
249 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
252 PrintAndLog("Command execute timeout");
258 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
263 return 32 * 4 + (sectorNo
- 32) * 16;
267 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
276 int CmdHF14AMfDump(const char *Cmd
)
278 uint8_t sectorNo
, blockNo
;
282 uint8_t rights
[40][4];
283 uint8_t carddata
[256][16];
284 uint8_t numSectors
= 16;
291 char cmdp
= param_getchar(Cmd
, 0);
293 case '0' : numSectors
= 5; break;
295 case '\0': numSectors
= 16; break;
296 case '2' : numSectors
= 32; break;
297 case '4' : numSectors
= 40; break;
298 default: numSectors
= 16;
301 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
302 PrintAndLog("Usage: hf mf dump [card memory]");
303 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
305 PrintAndLog("Samples: hf mf dump");
306 PrintAndLog(" hf mf dump 4");
310 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
311 PrintAndLog("Could not find file dumpkeys.bin");
315 // Read keys A from file
317 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
318 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
319 if ( bytes_read
== 0) {
320 PrintAndLog("File reading error.");
326 // Read keys B from file
327 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
328 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
329 if ( bytes_read
== 0) {
330 PrintAndLog("File reading error.");
338 PrintAndLog("|-----------------------------------------|");
339 PrintAndLog("|------ Reading sector access bits...-----|");
340 PrintAndLog("|-----------------------------------------|");
342 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
343 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
344 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
345 clearCommandBuffer();
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);
380 clearCommandBuffer();
382 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
383 } else { // data block. Check if it can be read with key A or key B
384 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
385 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
386 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
387 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
389 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
390 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
392 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
393 } else { // key A would work
394 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
395 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
396 clearCommandBuffer();
398 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
403 isOK
= resp
.arg
[0] & 0xff;
404 uint8_t *data
= resp
.d
.asBytes
;
405 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
406 data
[0] = (keyA
[sectorNo
][0]);
407 data
[1] = (keyA
[sectorNo
][1]);
408 data
[2] = (keyA
[sectorNo
][2]);
409 data
[3] = (keyA
[sectorNo
][3]);
410 data
[4] = (keyA
[sectorNo
][4]);
411 data
[5] = (keyA
[sectorNo
][5]);
412 data
[10] = (keyB
[sectorNo
][0]);
413 data
[11] = (keyB
[sectorNo
][1]);
414 data
[12] = (keyB
[sectorNo
][2]);
415 data
[13] = (keyB
[sectorNo
][3]);
416 data
[14] = (keyB
[sectorNo
][4]);
417 data
[15] = (keyB
[sectorNo
][5]);
420 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
421 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
423 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
429 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
436 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
437 PrintAndLog("Could not create file name dumpdata.bin");
440 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
441 fwrite(carddata
, 1, 16*numblocks
, fout
);
443 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
449 int CmdHF14AMfRestore(const char *Cmd
)
451 uint8_t sectorNo
,blockNo
;
453 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
454 uint8_t bldata
[16] = {0x00};
462 char cmdp
= param_getchar(Cmd
, 0);
464 case '0' : numSectors
= 5; break;
466 case '\0': numSectors
= 16; break;
467 case '2' : numSectors
= 32; break;
468 case '4' : numSectors
= 40; break;
469 default: numSectors
= 16;
472 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
473 PrintAndLog("Usage: hf mf restore [card memory]");
474 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
476 PrintAndLog("Samples: hf mf restore");
477 PrintAndLog(" hf mf restore 4");
481 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
482 PrintAndLog("Could not find file dumpkeys.bin");
487 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
488 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
489 if ( bytes_read
== 0) {
490 PrintAndLog("File reading error (dumpkeys.bin).");
496 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
497 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
498 if ( bytes_read
== 0) {
499 PrintAndLog("File reading error (dumpkeys.bin).");
507 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
508 PrintAndLog("Could not find file dumpdata.bin");
511 PrintAndLog("Restoring dumpdata.bin to card");
513 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
514 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
515 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
516 memcpy(c
.d
.asBytes
, key
, 6);
517 bytes_read
= fread(bldata
, 1, 16, fdump
);
518 if ( bytes_read
== 0) {
519 PrintAndLog("File reading error (dumpdata.bin).");
524 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
525 bldata
[0] = (keyA
[sectorNo
][0]);
526 bldata
[1] = (keyA
[sectorNo
][1]);
527 bldata
[2] = (keyA
[sectorNo
][2]);
528 bldata
[3] = (keyA
[sectorNo
][3]);
529 bldata
[4] = (keyA
[sectorNo
][4]);
530 bldata
[5] = (keyA
[sectorNo
][5]);
531 bldata
[10] = (keyB
[sectorNo
][0]);
532 bldata
[11] = (keyB
[sectorNo
][1]);
533 bldata
[12] = (keyB
[sectorNo
][2]);
534 bldata
[13] = (keyB
[sectorNo
][3]);
535 bldata
[14] = (keyB
[sectorNo
][4]);
536 bldata
[15] = (keyB
[sectorNo
][5]);
539 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
541 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
542 clearCommandBuffer();
546 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
547 uint8_t isOK
= resp
.arg
[0] & 0xff;
548 PrintAndLog("isOk:%02x", isOK
);
550 PrintAndLog("Command execute timeout");
559 int CmdHF14AMfNested(const char *Cmd
)
561 int i
, j
, res
, iterations
;
562 sector
*e_sector
= NULL
;
565 uint8_t trgBlockNo
= 0;
566 uint8_t trgKeyType
= 0;
567 uint8_t SectorsCnt
= 0;
568 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
569 uint8_t keyBlock
[14*6];
571 bool transferToEml
= false;
573 bool createDumpFile
= false;
575 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
576 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
581 PrintAndLog("Usage:");
582 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
583 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
584 PrintAndLog(" <target block number> <target key A/B> [t]");
585 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
586 PrintAndLog("t - transfer keys into emulator memory");
587 PrintAndLog("d - write keys to binary file");
589 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
590 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
591 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
592 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
596 cmdp
= param_getchar(Cmd
, 0);
597 blockNo
= param_get8(Cmd
, 1);
598 ctmp
= param_getchar(Cmd
, 2);
600 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
601 PrintAndLog("Key type must be A or B");
605 if (ctmp
!= 'A' && ctmp
!= 'a')
608 if (param_gethex(Cmd
, 3, key
, 12)) {
609 PrintAndLog("Key must include 12 HEX symbols");
613 if (cmdp
== 'o' || cmdp
== 'O') {
615 trgBlockNo
= param_get8(Cmd
, 4);
616 ctmp
= param_getchar(Cmd
, 5);
617 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
618 PrintAndLog("Target key type must be A or B");
621 if (ctmp
!= 'A' && ctmp
!= 'a')
626 case '0': SectorsCnt
= 05; break;
627 case '1': SectorsCnt
= 16; break;
628 case '2': SectorsCnt
= 32; break;
629 case '4': SectorsCnt
= 40; break;
630 default: SectorsCnt
= 16;
634 ctmp
= param_getchar(Cmd
, 4);
635 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
636 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
638 ctmp
= param_getchar(Cmd
, 6);
639 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
640 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
643 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
644 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
647 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
648 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
649 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
650 default : PrintAndLog("Unknown Error.\n");
654 key64
= bytes_to_num(keyBlock
, 6);
656 PrintAndLog("Found valid key:%012"llx
, key64
);
658 // transfer key to the emulator
660 uint8_t sectortrailer
;
661 if (trgBlockNo
< 32*4) { // 4 block sector
662 sectortrailer
= (trgBlockNo
& 0x03) + 3;
663 } else { // 16 block sector
664 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
666 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
669 num_to_bytes(key64
, 6, keyBlock
);
671 num_to_bytes(key64
, 6, &keyBlock
[10]);
672 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
675 PrintAndLog("No valid key found");
678 else { // ------------------------------------ multiple sectors working
679 clock_t t1
= clock();
681 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
682 if (e_sector
== NULL
) return 1;
684 //test current key and additional standard keys first
685 memcpy(keyBlock
, key
, 6);
686 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
687 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
688 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
689 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
690 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
691 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
692 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
693 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
694 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
695 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
696 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
697 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
698 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
700 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
701 for (i
= 0; i
< SectorsCnt
; i
++) {
702 for (j
= 0; j
< 2; j
++) {
703 if (e_sector
[i
].foundKey
[j
]) continue;
705 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
708 e_sector
[i
].Key
[j
] = key64
;
709 e_sector
[i
].foundKey
[j
] = 1;
716 PrintAndLog("nested...");
717 bool calibrate
= true;
718 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
719 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
720 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
721 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
722 PrintAndLog("-----------------------------------------------");
723 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
726 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
727 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
728 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
729 default : PrintAndLog("Unknown Error.\n");
739 key64
= bytes_to_num(keyBlock
, 6);
741 PrintAndLog("Found valid key:%012"llx
, key64
);
742 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
743 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
749 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
750 PrintAndLog("testing to read B...");
751 for (i
= 0; i
< SectorsCnt
; i
++) {
752 // KEY A but not KEY B
753 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
755 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
757 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
758 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
759 clearCommandBuffer();
763 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
765 uint8_t isOK
= resp
.arg
[0] & 0xff;
766 uint8_t *data
= resp
.d
.asBytes
;
770 key64
= bytes_to_num(data
+10, 6);
772 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
773 e_sector
[i
].foundKey
[1] = 1;
774 e_sector
[i
].Key
[1] = key64
;
782 PrintAndLog("Time in nested: %f ticks %1.2f sec (%1.2f sec per key)\n\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
, ((float)t1
)/iterations
/CLOCKS_PER_SEC
);
785 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
787 PrintAndLog("|---|----------------|---|----------------|---|");
788 PrintAndLog("|sec|key A |res|key B |res|");
789 PrintAndLog("|---|----------------|---|----------------|---|");
790 for (i
= 0; i
< SectorsCnt
; i
++) {
791 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
793 e_sector
[i
].foundKey
[0],
795 e_sector
[i
].foundKey
[1]
798 PrintAndLog("|---|----------------|---|----------------|---|");
800 // transfer them to the emulator
802 for (i
= 0; i
< SectorsCnt
; i
++) {
803 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
804 if (e_sector
[i
].foundKey
[0])
805 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
806 if (e_sector
[i
].foundKey
[1])
807 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
808 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
813 if (createDumpFile
) {
814 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
815 PrintAndLog("Could not create file dumpkeys.bin");
819 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
820 for(i
=0; i
<SectorsCnt
; i
++) {
821 if (e_sector
[i
].foundKey
[0]){
822 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
823 fwrite ( tempkey
, 1, 6, fkeys
);
826 fwrite ( &standart
, 1, 6, fkeys
);
829 for(i
=0; i
<SectorsCnt
; i
++) {
830 if (e_sector
[i
].foundKey
[1]){
831 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
832 fwrite ( tempkey
, 1, 6, fkeys
);
835 fwrite ( &standart
, 1, 6, fkeys
);
846 int CmdHF14AMfNestedHard(const char *Cmd
)
850 uint8_t trgBlockNo
= 0;
851 uint8_t trgKeyType
= 0;
852 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
853 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
856 ctmp
= param_getchar(Cmd
, 0);
858 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) {
859 PrintAndLog("Usage:");
860 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
861 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
862 PrintAndLog(" or hf mf hardnested r [known target key]");
864 PrintAndLog("Options: ");
865 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
866 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
867 PrintAndLog(" r: Read nonces.bin and start attack");
869 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
870 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
871 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
872 PrintAndLog(" sample4: hf mf hardnested r");
874 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
875 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
879 bool know_target_key
= false;
880 bool nonce_file_read
= false;
881 bool nonce_file_write
= false;
886 if (ctmp
== 'R' || ctmp
== 'r') {
887 nonce_file_read
= true;
888 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
889 know_target_key
= true;
891 } else if (ctmp
== 'T' || ctmp
== 't') {
892 tests
= param_get32ex(Cmd
, 1, 100, 10);
894 blockNo
= param_get8(Cmd
, 0);
895 ctmp
= param_getchar(Cmd
, 1);
896 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
897 PrintAndLog("Key type must be A or B");
900 if (ctmp
!= 'A' && ctmp
!= 'a') {
904 if (param_gethex(Cmd
, 2, key
, 12)) {
905 PrintAndLog("Key must include 12 HEX symbols");
909 trgBlockNo
= param_get8(Cmd
, 3);
910 ctmp
= param_getchar(Cmd
, 4);
911 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
912 PrintAndLog("Target key type must be A or B");
915 if (ctmp
!= 'A' && ctmp
!= 'a') {
921 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
922 know_target_key
= true;
926 while ((ctmp
= param_getchar(Cmd
, i
))) {
927 if (ctmp
== 's' || ctmp
== 'S') {
929 } else if (ctmp
== 'w' || ctmp
== 'W') {
930 nonce_file_write
= true;
932 PrintAndLog("Possible options are w and/or s");
939 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 ",
942 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
943 know_target_key
?"":" (not set)",
944 nonce_file_write
?"write":nonce_file_read
?"read":"none",
948 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
952 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
953 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
962 int CmdHF14AMfChk(const char *Cmd
)
965 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
966 PrintAndLog(" * - all sectors");
967 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
968 PrintAndLog("d - write keys to binary file");
969 PrintAndLog("t - write keys to emulator memory\n");
970 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
971 PrintAndLog(" hf mf chk *1 ? t");
972 PrintAndLog(" hf mf chk *1 ? d");
977 char filename
[FILE_PATH_SIZE
]={0};
979 uint8_t *keyBlock
= NULL
, *p
;
980 uint8_t stKeyBlock
= 20;
986 uint8_t SectorsCnt
= 1;
990 int transferToEml
= 0;
991 int createDumpFile
= 0;
993 keyBlock
= calloc(stKeyBlock
, 6);
994 if (keyBlock
== NULL
) return 1;
996 uint64_t defaultKeys
[] =
998 0xffffffffffff, // Default key (first key used by program if no user defined key)
999 0x000000000000, // Blank key
1000 0xa0a1a2a3a4a5, // NFCForum MAD key
1012 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1014 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1016 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1019 if (param_getchar(Cmd
, 0)=='*') {
1021 switch(param_getchar(Cmd
+1, 0)) {
1022 case '0': SectorsCnt
= 5; break;
1023 case '1': SectorsCnt
= 16; break;
1024 case '2': SectorsCnt
= 32; break;
1025 case '4': SectorsCnt
= 40; break;
1026 default: SectorsCnt
= 16;
1030 blockNo
= param_get8(Cmd
, 0);
1032 ctmp
= param_getchar(Cmd
, 1);
1044 PrintAndLog("Key type must be A , B or ?");
1049 ctmp
= param_getchar(Cmd
, 2);
1050 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1051 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1053 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1054 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1055 if ( stKeyBlock
- keycnt
< 2) {
1056 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1058 PrintAndLog("Cannot allocate memory for Keys");
1064 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1065 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1066 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1069 // May be a dic file
1070 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1071 PrintAndLog("File name too long");
1076 if ( (f
= fopen( filename
, "r")) ) {
1077 while( fgets(buf
, sizeof(buf
), f
) ){
1078 if (strlen(buf
) < 12 || buf
[11] == '\n')
1081 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1083 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1085 if (!isxdigit(buf
[0])){
1086 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1092 if ( stKeyBlock
- keycnt
< 2) {
1093 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1095 PrintAndLog("Cannot allocate memory for defKeys");
1102 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1103 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1104 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1106 memset(buf
, 0, sizeof(buf
));
1110 PrintAndLog("File: %s: not found or locked.", filename
);
1119 PrintAndLog("No key specified, trying default keys");
1120 for (;keycnt
< defaultKeysSize
; keycnt
++)
1121 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1122 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1123 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1126 // initialize storage for found keys
1127 bool validKey
[2][40];
1128 uint8_t foundKey
[2][40][6];
1129 for (uint16_t t
= 0; t
< 2; t
++) {
1130 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1131 validKey
[t
][sectorNo
] = false;
1132 for (uint16_t i
= 0; i
< 6; i
++) {
1133 foundKey
[t
][sectorNo
][i
] = 0xff;
1138 clock_t t1
= clock();
1140 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
1142 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1143 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
1144 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1145 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1146 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1147 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
1150 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1151 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
1152 validKey
[t
][i
] = true;
1155 PrintAndLog("Command execute timeout");
1158 b
<127?(b
+=4):(b
+=16);
1163 printf("Time in checkkeys: %f ticks %1.2f sec (%1.2f sec per key)\n\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
, ((float)t1
)/keycnt
/CLOCKS_PER_SEC
);
1166 if (transferToEml
) {
1168 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1169 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1170 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1171 for (uint16_t t
= 0; t
< 2; t
++) {
1172 if (validKey
[t
][sectorNo
]) {
1173 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1176 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1179 PrintAndLog("Found keys have been transferred to the emulator memory");
1182 if (createDumpFile
) {
1183 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1184 if (fkeys
== NULL
) {
1185 PrintAndLog("Could not create file dumpkeys.bin");
1189 for (uint16_t t
= 0; t
< 2; t
++) {
1190 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1193 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1201 int CmdHF14AMf1kSim(const char *Cmd
)
1203 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1204 uint8_t exitAfterNReads
= 0;
1207 uint8_t cmdp
= param_getchar(Cmd
, 0);
1209 if (cmdp
== 'h' || cmdp
== 'H') {
1210 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1211 PrintAndLog(" h this help");
1212 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1213 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1214 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1215 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1217 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1221 if (param_getchar(Cmd
, pnr
) == 'u') {
1222 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1224 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1225 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1226 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1228 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1234 if (param_getchar(Cmd
, pnr
) == 'n') {
1235 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1239 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1240 //Using a flag to signal interactiveness, least significant bit
1241 flags
|= FLAG_INTERACTIVE
;
1245 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1246 //Using a flag to signal interactiveness, least significant bit
1247 flags
|= FLAG_NR_AR_ATTACK
;
1250 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1251 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1252 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1258 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1259 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1260 clearCommandBuffer();
1263 if(flags
& FLAG_INTERACTIVE
)
1269 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1272 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1274 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1276 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1278 memset(data
, 0x00, sizeof(data
));
1279 memset(key
, 0x00, sizeof(key
));
1280 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1282 memcpy(data
, resp
.d
.asBytes
, len
);
1284 uint64_t corr_uid
= 0;
1286 // this IF? what was I thinking of?
1287 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1288 corr_uid
= ((uint64_t)(data
[3] << 24)) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1289 tryMfk32(corr_uid
, data
, key
);
1291 corr_uid
|= (uint64_t)data
[2] << 48;
1292 corr_uid
|= (uint64_t)data
[1] << 40;
1293 corr_uid
|= (uint64_t)data
[0] << 32;
1294 corr_uid
|= (uint64_t)data
[7] << 24;
1295 corr_uid
|= (uint64_t)data
[6] << 16;
1296 corr_uid
|= (uint64_t)data
[5] << 8;
1297 corr_uid
|= (uint64_t)data
[4];
1298 tryMfk64(corr_uid
, data
, key
);
1306 int CmdHF14AMfDbg(const char *Cmd
)
1308 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1310 PrintAndLog("Max debug mode parameter is 4 \n");
1313 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1314 PrintAndLog("Usage: hf mf dbg <debug level>");
1315 PrintAndLog(" 0 - no debug messages");
1316 PrintAndLog(" 1 - error messages");
1317 PrintAndLog(" 2 - plus information messages");
1318 PrintAndLog(" 3 - plus debug messages");
1319 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1320 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1324 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1330 int CmdHF14AMfEGet(const char *Cmd
)
1332 uint8_t blockNo
= 0;
1333 uint8_t data
[16] = {0x00};
1335 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1336 PrintAndLog("Usage: hf mf eget <block number>");
1337 PrintAndLog(" sample: hf mf eget 0 ");
1341 blockNo
= param_get8(Cmd
, 0);
1344 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1345 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1347 PrintAndLog("Command execute timeout");
1353 int CmdHF14AMfEClear(const char *Cmd
)
1355 if (param_getchar(Cmd
, 0) == 'h') {
1356 PrintAndLog("Usage: hf mf eclr");
1357 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1361 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1366 int CmdHF14AMfESet(const char *Cmd
)
1368 uint8_t memBlock
[16];
1369 uint8_t blockNo
= 0;
1371 memset(memBlock
, 0x00, sizeof(memBlock
));
1373 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1374 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1375 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1379 blockNo
= param_get8(Cmd
, 0);
1381 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1382 PrintAndLog("block data must include 32 HEX symbols");
1387 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1388 memcpy(c
.d
.asBytes
, memBlock
, 16);
1393 int CmdHF14AMfELoad(const char *Cmd
)
1396 char filename
[FILE_PATH_SIZE
];
1397 char *fnameptr
= filename
;
1398 char buf
[64] = {0x00};
1399 uint8_t buf8
[64] = {0x00};
1400 int i
, len
, blockNum
, numBlocks
;
1401 int nameParamNo
= 1;
1402 uint8_t blockWidth
= 32;
1403 char ctmp
= param_getchar(Cmd
, 0);
1405 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1406 PrintAndLog("It loads emul dump from the file `filename.eml`");
1407 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1408 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1410 PrintAndLog(" sample: hf mf eload filename");
1411 PrintAndLog(" hf mf eload 4 filename");
1416 case '0' : numBlocks
= 5*4; break;
1418 case '\0': numBlocks
= 16*4; break;
1419 case '2' : numBlocks
= 32*4; break;
1420 case '4' : numBlocks
= 256; break;
1421 case 'U' : // fall through
1422 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1428 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1429 if (numblk2
> 0) numBlocks
= numblk2
;
1431 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1433 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1437 sprintf(fnameptr
, ".eml");
1440 f
= fopen(filename
, "r");
1442 PrintAndLog("File %s not found or locked", filename
);
1448 memset(buf
, 0, sizeof(buf
));
1450 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1452 if (blockNum
>= numBlocks
) break;
1454 PrintAndLog("File reading error.");
1459 if (strlen(buf
) < blockWidth
){
1460 if(strlen(buf
) && feof(f
))
1462 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1467 for (i
= 0; i
< blockWidth
; i
+= 2) {
1468 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1470 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1471 PrintAndLog("Cant set emul block: %3d", blockNum
);
1478 if (blockNum
>= numBlocks
) break;
1483 if ((blockNum
!= numBlocks
)) {
1484 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1487 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1491 int CmdHF14AMfESave(const char *Cmd
)
1494 char filename
[FILE_PATH_SIZE
];
1495 char * fnameptr
= filename
;
1497 int i
, j
, len
, numBlocks
;
1498 int nameParamNo
= 1;
1500 memset(filename
, 0, sizeof(filename
));
1501 memset(buf
, 0, sizeof(buf
));
1503 char ctmp
= param_getchar(Cmd
, 0);
1505 if ( ctmp
== 'h' || ctmp
== 'H') {
1506 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1507 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1508 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1510 PrintAndLog(" sample: hf mf esave ");
1511 PrintAndLog(" hf mf esave 4");
1512 PrintAndLog(" hf mf esave 4 filename");
1517 case '0' : numBlocks
= 5*4; break;
1519 case '\0': numBlocks
= 16*4; break;
1520 case '2' : numBlocks
= 32*4; break;
1521 case '4' : numBlocks
= 256; break;
1528 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1530 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1532 // user supplied filename?
1534 // get filename (UID from memory)
1535 if (mfEmlGetMem(buf
, 0, 1)) {
1536 PrintAndLog("Can\'t get UID from block: %d", 0);
1537 len
= sprintf(fnameptr
, "dump");
1541 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1542 sprintf(fnameptr
, "%02X", buf
[j
]);
1548 // add file extension
1549 sprintf(fnameptr
, ".eml");
1552 f
= fopen(filename
, "w+");
1555 PrintAndLog("Can't open file %s ", filename
);
1560 for (i
= 0; i
< numBlocks
; i
++) {
1561 if (mfEmlGetMem(buf
, i
, 1)) {
1562 PrintAndLog("Cant get block: %d", i
);
1565 for (j
= 0; j
< 16; j
++)
1566 fprintf(f
, "%02X", buf
[j
]);
1571 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1576 int CmdHF14AMfECFill(const char *Cmd
)
1578 uint8_t keyType
= 0;
1579 uint8_t numSectors
= 16;
1581 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1582 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1583 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1585 PrintAndLog("samples: hf mf ecfill A");
1586 PrintAndLog(" hf mf ecfill A 4");
1587 PrintAndLog("Read card and transfer its data to emulator memory.");
1588 PrintAndLog("Keys must be laid in the emulator memory. \n");
1592 char ctmp
= param_getchar(Cmd
, 0);
1593 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1594 PrintAndLog("Key type must be A or B");
1597 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1599 ctmp
= param_getchar(Cmd
, 1);
1601 case '0' : numSectors
= 5; break;
1603 case '\0': numSectors
= 16; break;
1604 case '2' : numSectors
= 32; break;
1605 case '4' : numSectors
= 40; break;
1606 default: numSectors
= 16;
1609 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1610 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1615 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1620 uint64_t keyA
, keyB
;
1622 char cmdp
= param_getchar(Cmd
, 0);
1624 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1625 PrintAndLog("It prints the keys loaded in the emulator memory");
1626 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1627 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1629 PrintAndLog(" sample: hf mf ekeyprn 1");
1634 case '0' : numSectors
= 5; break;
1636 case '\0': numSectors
= 16; break;
1637 case '2' : numSectors
= 32; break;
1638 case '4' : numSectors
= 40; break;
1639 default: numSectors
= 16;
1642 PrintAndLog("|---|----------------|----------------|");
1643 PrintAndLog("|sec|key A |key B |");
1644 PrintAndLog("|---|----------------|----------------|");
1645 for (i
= 0; i
< numSectors
; i
++) {
1646 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1647 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1650 keyA
= bytes_to_num(data
, 6);
1651 keyB
= bytes_to_num(data
+ 10, 6);
1652 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1654 PrintAndLog("|---|----------------|----------------|");
1659 int CmdHF14AMfCSetUID(const char *Cmd
)
1661 uint8_t wipeCard
= 0;
1662 uint8_t uid
[8] = {0x00};
1663 uint8_t oldUid
[8] = {0x00};
1664 uint8_t atqa
[2] = {0x00};
1665 uint8_t sak
[1] = {0x00};
1666 uint8_t atqaPresent
= 1;
1671 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1672 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1673 PrintAndLog("sample: hf mf csetuid 01020304");
1674 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1675 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1676 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1680 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1681 PrintAndLog("UID must include 8 HEX symbols");
1686 ctmp
= param_getchar(Cmd
, argi
);
1687 if (ctmp
== 'w' || ctmp
== 'W') {
1693 if (param_getchar(Cmd
, argi
)) {
1694 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1695 PrintAndLog("ATQA must include 4 HEX symbols");
1699 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1700 PrintAndLog("SAK must include 2 HEX symbols");
1709 ctmp
= param_getchar(Cmd
, argi
);
1710 if (ctmp
== 'w' || ctmp
== 'W') {
1715 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1717 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1719 PrintAndLog("Can't set UID. error=%d", res
);
1723 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1724 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1728 int CmdHF14AMfCSetBlk(const char *Cmd
)
1730 uint8_t block
[16] = {0x00};
1731 uint8_t blockNo
= 0;
1732 uint8_t params
= MAGIC_SINGLE
;
1735 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1736 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1737 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1738 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1739 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1743 blockNo
= param_get8(Cmd
, 0);
1745 if (param_gethex(Cmd
, 1, block
, 32)) {
1746 PrintAndLog("block data must include 32 HEX symbols");
1750 char ctmp
= param_getchar(Cmd
, 2);
1751 if (ctmp
== 'w' || ctmp
== 'W')
1752 params
|= MAGIC_WIPE
;
1754 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
1756 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
1758 PrintAndLog("Can't write block. error=%d", res
);
1764 int CmdHF14AMfCLoad(const char *Cmd
)
1767 char filename
[FILE_PATH_SIZE
];
1768 char * fnameptr
= filename
;
1769 char buf
[64] = {0x00};
1770 uint8_t buf8
[64] = {0x00};
1771 uint8_t fillFromEmulator
= 0;
1772 int i
, len
, blockNum
, flags
=0;
1774 memset(filename
, 0, sizeof(filename
));
1776 char ctmp
= param_getchar(Cmd
, 0);
1778 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1779 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1780 PrintAndLog("or from emulator memory (option `e`)");
1781 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1782 PrintAndLog(" or: hf mf cload e ");
1783 PrintAndLog(" sample: hf mf cload filename");
1787 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1789 if (fillFromEmulator
) {
1790 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1791 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1792 PrintAndLog("Cant get block: %d", blockNum
);
1795 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1796 if (blockNum
== 1) flags
= 0; // just write
1797 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
1799 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1800 PrintAndLog("Cant set magic card block: %d", blockNum
);
1807 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1809 memcpy(filename
, Cmd
, len
);
1812 sprintf(fnameptr
, ".eml");
1815 f
= fopen(filename
, "r");
1817 PrintAndLog("File not found or locked.");
1824 memset(buf
, 0, sizeof(buf
));
1826 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1828 PrintAndLog("File reading error.");
1832 if (strlen(buf
) < 32) {
1833 if(strlen(buf
) && feof(f
))
1835 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1839 for (i
= 0; i
< 32; i
+= 2)
1840 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1842 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1843 if (blockNum
== 1) flags
= 0; // just write
1844 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
1846 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1847 PrintAndLog("Can't set magic card block: %d", blockNum
);
1853 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1858 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1859 PrintAndLog("File content error. There must be 64 blocks");
1862 PrintAndLog("Loaded from file: %s", filename
);
1868 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1870 uint8_t blockNo
= 0;
1872 memset(data
, 0x00, sizeof(data
));
1873 char ctmp
= param_getchar(Cmd
, 0);
1875 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1876 PrintAndLog("Usage: hf mf cgetblk <block number>");
1877 PrintAndLog("sample: hf mf cgetblk 1");
1878 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1882 blockNo
= param_get8(Cmd
, 0);
1884 PrintAndLog("--block number:%2d ", blockNo
);
1886 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
1888 PrintAndLog("Can't read block. error=%d", res
);
1892 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
1896 int CmdHF14AMfCGetSc(const char *Cmd
) {
1898 uint8_t sectorNo
= 0;
1900 memset(data
, 0x00, sizeof(data
));
1901 char ctmp
= param_getchar(Cmd
, 0);
1903 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1904 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1905 PrintAndLog("sample: hf mf cgetsc 0");
1906 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1910 sectorNo
= param_get8(Cmd
, 0);
1911 if (sectorNo
> 15) {
1912 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1916 PrintAndLog("--sector number:%d ", sectorNo
);
1917 PrintAndLog("block | data");
1919 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1920 for (i
= 0; i
< 4; i
++) {
1921 if (i
== 1) flags
= 0;
1922 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1924 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
1926 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1929 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
1934 int CmdHF14AMfCSave(const char *Cmd
) {
1937 char filename
[FILE_PATH_SIZE
];
1938 char * fnameptr
= filename
;
1939 uint8_t fillFromEmulator
= 0;
1941 int i
, j
, len
, flags
;
1943 memset(filename
, 0, sizeof(filename
));
1944 memset(buf
, 0, sizeof(buf
));
1945 char ctmp
= param_getchar(Cmd
, 0);
1947 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1948 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1949 PrintAndLog("or into emulator memory (option `e`)");
1950 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1951 PrintAndLog(" sample: hf mf esave ");
1952 PrintAndLog(" hf mf esave filename");
1953 PrintAndLog(" hf mf esave e \n");
1956 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1958 if (fillFromEmulator
) {
1959 // put into emulator
1960 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1961 for (i
= 0; i
< 16 * 4; i
++) {
1962 if (i
== 1) flags
= 0;
1963 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1965 if (mfCGetBlock(i
, buf
, flags
)) {
1966 PrintAndLog("Cant get block: %d", i
);
1970 if (mfEmlSetMem(buf
, i
, 1)) {
1971 PrintAndLog("Cant set emul block: %d", i
);
1978 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1980 // get filename based on UID
1983 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
1984 PrintAndLog("Cant get block: %d", 0);
1985 len
= sprintf(fnameptr
, "dump");
1988 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1989 sprintf(fnameptr
, "%02x", buf
[j
]);
1992 memcpy(filename
, Cmd
, len
);
1996 // add .eml extension
1997 sprintf(fnameptr
, ".eml");
2000 f
= fopen(filename
, "w+");
2003 PrintAndLog("File not found or locked.");
2008 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2009 for (i
= 0; i
< 16 * 4; i
++) {
2010 if (i
== 1) flags
= 0;
2011 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2013 if (mfCGetBlock(i
, buf
, flags
)) {
2014 PrintAndLog("Cant get block: %d", i
);
2017 for (j
= 0; j
< 16; j
++)
2018 fprintf(f
, "%02x", buf
[j
]);
2023 PrintAndLog("Saved to file: %s", filename
);
2028 int CmdHF14AMfSniff(const char *Cmd
){
2030 bool wantLogToFile
= 0;
2031 bool wantDecrypt
= 0;
2032 //bool wantSaveToEml = 0; TODO
2033 bool wantSaveToEmlFile
= 0;
2044 uint8_t atqa
[2] = {0x00};
2047 uint8_t *buf
= NULL
;
2048 uint16_t bufsize
= 0;
2049 uint8_t *bufPtr
= NULL
;
2051 char ctmp
= param_getchar(Cmd
, 0);
2052 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2053 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
2054 PrintAndLog("You can specify:");
2055 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
2056 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
2057 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2058 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2059 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
2060 PrintAndLog(" sample: hf mf sniff l d e");
2064 for (int i
= 0; i
< 4; i
++) {
2065 ctmp
= param_getchar(Cmd
, i
);
2066 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
2067 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
2068 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2069 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
2072 printf("-------------------------------------------------------------------------\n");
2073 printf("Executing command. \n");
2074 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2075 printf("Press the key on pc keyboard to abort the client.\n");
2076 printf("-------------------------------------------------------------------------\n");
2078 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
2079 clearCommandBuffer();
2087 tmpchar
= getchar();
2089 printf("\naborted via keyboard!\n");
2094 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2095 res
= resp
.arg
[0] & 0xff;
2096 uint16_t traceLen
= resp
.arg
[1];
2101 return 0; // we are done
2104 if (res
== 1) { // there is (more) data to be transferred
2105 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
2106 if (traceLen
> bufsize
) {
2108 if (buf
== NULL
) { // not yet allocated
2109 p
= malloc(traceLen
);
2110 } else { // need more memory
2111 p
= realloc(buf
, traceLen
);
2114 PrintAndLog("Cannot allocate memory for trace");
2122 memset(buf
, 0x00, traceLen
);
2124 if (bufPtr
== NULL
) {
2125 PrintAndLog("Cannot allocate memory for trace");
2129 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2134 if (res
== 2) { // received all data, start displaying
2135 blockLen
= bufPtr
- buf
;
2138 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2139 while (bufPtr
- buf
< blockLen
) {
2140 bufPtr
+= 6; // skip (void) timing information
2141 len
= *((uint16_t *)bufPtr
);
2149 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
2150 memcpy(uid
, bufPtr
+ 2, 7);
2151 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2152 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2154 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2155 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2159 if (wantLogToFile
|| wantDecrypt
) {
2160 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2161 AddLogCurrentDT(logHexFileName
);
2164 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2166 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2168 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2170 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
2174 bufPtr
+= ((len
-1)/8+1); // ignore parity
2185 //needs nt, ar, at, Data to decrypt
2186 int CmdHf14MfDecryptBytes(const char *Cmd
){
2189 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2190 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2191 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2194 param_gethex_ex(Cmd
, 3, data
, &len
);
2197 int limit
= sizeof(data
) / 2;
2202 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2205 static command_t CommandTable
[] = {
2206 {"help", CmdHelp
, 1, "This help"},
2207 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2208 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2209 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2210 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2211 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2212 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2213 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2214 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2215 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2216 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2217 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2218 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2219 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2220 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2221 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2222 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2223 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2224 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2225 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2226 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2227 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2228 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2229 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2230 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2231 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2232 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2233 {NULL
, NULL
, 0, NULL
}
2236 int CmdHFMF(const char *Cmd
) {
2238 clearCommandBuffer();
2239 //WaitForResponseTimeout(CMD_ACK,NULL,100);
2240 CmdsParse(CommandTable
, Cmd
);
2244 int CmdHelp(const char *Cmd
) {
2245 CmdsHelp(CommandTable
);