]>
cvs.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;
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
);
95 //printf("Time in darkside: %d ticks - %1.2f seconds\n", t, ((float)t)/CLOCKS_PER_SEC);
96 printf("Time in darkside: %Lf ticks - %1.2Lf seconds\n", (long double)t
, ((long double)t
)/CLOCKS_PER_SEC
);
100 int CmdHF14AMfWrBl(const char *Cmd
)
104 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
105 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
110 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
111 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
115 blockNo
= param_get8(Cmd
, 0);
116 cmdp
= param_getchar(Cmd
, 1);
118 PrintAndLog("Key type must be A or B");
121 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
122 if (param_gethex(Cmd
, 2, key
, 12)) {
123 PrintAndLog("Key must include 12 HEX symbols");
126 if (param_gethex(Cmd
, 3, bldata
, 32)) {
127 PrintAndLog("Block data must include 32 HEX symbols");
130 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
131 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
133 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
134 memcpy(c
.d
.asBytes
, key
, 6);
135 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
136 clearCommandBuffer();
140 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
141 uint8_t isOK
= resp
.arg
[0] & 0xff;
142 PrintAndLog("isOk:%02x", isOK
);
144 PrintAndLog("Command execute timeout");
150 int CmdHF14AMfRdBl(const char *Cmd
)
154 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
160 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
161 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
165 blockNo
= param_get8(Cmd
, 0);
166 cmdp
= param_getchar(Cmd
, 1);
168 PrintAndLog("Key type must be A or B");
171 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
172 if (param_gethex(Cmd
, 2, key
, 12)) {
173 PrintAndLog("Key must include 12 HEX symbols");
176 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
178 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
179 memcpy(c
.d
.asBytes
, key
, 6);
180 clearCommandBuffer();
184 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
185 uint8_t isOK
= resp
.arg
[0] & 0xff;
186 uint8_t *data
= resp
.d
.asBytes
;
189 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
191 PrintAndLog("isOk:%02x", isOK
);
193 PrintAndLog("Command execute timeout");
199 int CmdHF14AMfRdSc(const char *Cmd
)
202 uint8_t sectorNo
= 0;
204 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
206 uint8_t *data
= NULL
;
210 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
211 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
215 sectorNo
= param_get8(Cmd
, 0);
217 PrintAndLog("Sector number must be less than 40");
220 cmdp
= param_getchar(Cmd
, 1);
221 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
222 PrintAndLog("Key type must be A or B");
225 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
226 if (param_gethex(Cmd
, 2, key
, 12)) {
227 PrintAndLog("Key must include 12 HEX symbols");
230 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
232 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
233 memcpy(c
.d
.asBytes
, key
, 6);
234 clearCommandBuffer();
239 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
240 isOK
= resp
.arg
[0] & 0xff;
241 data
= resp
.d
.asBytes
;
243 PrintAndLog("isOk:%02x", isOK
);
245 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
246 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
248 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
251 PrintAndLog("Command execute timeout");
257 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
262 return 32 * 4 + (sectorNo
- 32) * 16;
266 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
275 int CmdHF14AMfDump(const char *Cmd
)
277 uint8_t sectorNo
, blockNo
;
281 uint8_t rights
[40][4];
282 uint8_t carddata
[256][16];
283 uint8_t numSectors
= 16;
290 char cmdp
= param_getchar(Cmd
, 0);
292 case '0' : numSectors
= 5; break;
294 case '\0': numSectors
= 16; break;
295 case '2' : numSectors
= 32; break;
296 case '4' : numSectors
= 40; break;
297 default: numSectors
= 16;
300 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
301 PrintAndLog("Usage: hf mf dump [card memory]");
302 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
304 PrintAndLog("Samples: hf mf dump");
305 PrintAndLog(" hf mf dump 4");
309 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
310 PrintAndLog("Could not find file dumpkeys.bin");
314 // Read keys A from file
316 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
317 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
318 if ( bytes_read
== 0) {
319 PrintAndLog("File reading error.");
325 // Read keys B from file
326 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
327 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
328 if ( bytes_read
== 0) {
329 PrintAndLog("File reading error.");
337 PrintAndLog("|-----------------------------------------|");
338 PrintAndLog("|------ Reading sector access bits...-----|");
339 PrintAndLog("|-----------------------------------------|");
341 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
342 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
343 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
344 clearCommandBuffer();
347 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
348 uint8_t isOK
= resp
.arg
[0] & 0xff;
349 uint8_t *data
= resp
.d
.asBytes
;
351 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
352 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
353 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
354 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
356 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
357 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
358 rights
[sectorNo
][3] = 0x01;
361 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
362 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
363 rights
[sectorNo
][3] = 0x01;
367 PrintAndLog("|-----------------------------------------|");
368 PrintAndLog("|----- Dumping all blocks to file... -----|");
369 PrintAndLog("|-----------------------------------------|");
372 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
373 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
374 bool received
= false;
376 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
377 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
378 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
379 clearCommandBuffer();
381 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
382 } else { // data block. Check if it can be read with key A or key B
383 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
384 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
385 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
386 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
388 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
389 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
391 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
392 } else { // key A would work
393 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
394 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
395 clearCommandBuffer();
397 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
402 isOK
= resp
.arg
[0] & 0xff;
403 uint8_t *data
= resp
.d
.asBytes
;
404 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
405 data
[0] = (keyA
[sectorNo
][0]);
406 data
[1] = (keyA
[sectorNo
][1]);
407 data
[2] = (keyA
[sectorNo
][2]);
408 data
[3] = (keyA
[sectorNo
][3]);
409 data
[4] = (keyA
[sectorNo
][4]);
410 data
[5] = (keyA
[sectorNo
][5]);
411 data
[10] = (keyB
[sectorNo
][0]);
412 data
[11] = (keyB
[sectorNo
][1]);
413 data
[12] = (keyB
[sectorNo
][2]);
414 data
[13] = (keyB
[sectorNo
][3]);
415 data
[14] = (keyB
[sectorNo
][4]);
416 data
[15] = (keyB
[sectorNo
][5]);
419 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
420 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
422 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
428 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
435 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
436 PrintAndLog("Could not create file name dumpdata.bin");
439 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
440 fwrite(carddata
, 1, 16*numblocks
, fout
);
442 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
448 int CmdHF14AMfRestore(const char *Cmd
)
450 uint8_t sectorNo
,blockNo
;
452 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
453 uint8_t bldata
[16] = {0x00};
461 char cmdp
= param_getchar(Cmd
, 0);
463 case '0' : numSectors
= 5; break;
465 case '\0': numSectors
= 16; break;
466 case '2' : numSectors
= 32; break;
467 case '4' : numSectors
= 40; break;
468 default: numSectors
= 16;
471 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
472 PrintAndLog("Usage: hf mf restore [card memory]");
473 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
475 PrintAndLog("Samples: hf mf restore");
476 PrintAndLog(" hf mf restore 4");
480 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
481 PrintAndLog("Could not find file dumpkeys.bin");
486 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
487 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
488 if ( bytes_read
== 0) {
489 PrintAndLog("File reading error (dumpkeys.bin).");
495 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
496 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
497 if ( bytes_read
== 0) {
498 PrintAndLog("File reading error (dumpkeys.bin).");
506 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
507 PrintAndLog("Could not find file dumpdata.bin");
510 PrintAndLog("Restoring dumpdata.bin to card");
512 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
513 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
514 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
515 memcpy(c
.d
.asBytes
, key
, 6);
517 if (fread(bldata
, 1, 16, fdump
) == 0) {
518 PrintAndLog("File reading error (dumpdata.bin).");
523 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
524 bldata
[0] = (keyA
[sectorNo
][0]);
525 bldata
[1] = (keyA
[sectorNo
][1]);
526 bldata
[2] = (keyA
[sectorNo
][2]);
527 bldata
[3] = (keyA
[sectorNo
][3]);
528 bldata
[4] = (keyA
[sectorNo
][4]);
529 bldata
[5] = (keyA
[sectorNo
][5]);
530 bldata
[10] = (keyB
[sectorNo
][0]);
531 bldata
[11] = (keyB
[sectorNo
][1]);
532 bldata
[12] = (keyB
[sectorNo
][2]);
533 bldata
[13] = (keyB
[sectorNo
][3]);
534 bldata
[14] = (keyB
[sectorNo
][4]);
535 bldata
[15] = (keyB
[sectorNo
][5]);
538 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
540 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
541 clearCommandBuffer();
545 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
546 uint8_t isOK
= resp
.arg
[0] & 0xff;
547 PrintAndLog("isOk:%02x", isOK
);
549 PrintAndLog("Command execute timeout");
558 int CmdHF14AMfNested(const char *Cmd
)
560 int i
, j
, res
, iterations
;
561 sector
*e_sector
= NULL
;
564 uint8_t trgBlockNo
= 0;
565 uint8_t trgKeyType
= 0;
566 uint8_t SectorsCnt
= 0;
567 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
568 uint8_t keyBlock
[14*6];
570 bool transferToEml
= false;
572 bool createDumpFile
= false;
574 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
575 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
580 PrintAndLog("Usage:");
581 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
582 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
583 PrintAndLog(" <target block number> <target key A/B> [t]");
584 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
585 PrintAndLog("t - transfer keys into emulator memory");
586 PrintAndLog("d - write keys to binary file");
588 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
589 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
590 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
591 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
595 cmdp
= param_getchar(Cmd
, 0);
596 blockNo
= param_get8(Cmd
, 1);
597 ctmp
= param_getchar(Cmd
, 2);
599 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
600 PrintAndLog("Key type must be A or B");
604 if (ctmp
!= 'A' && ctmp
!= 'a')
607 if (param_gethex(Cmd
, 3, key
, 12)) {
608 PrintAndLog("Key must include 12 HEX symbols");
612 if (cmdp
== 'o' || cmdp
== 'O') {
614 trgBlockNo
= param_get8(Cmd
, 4);
615 ctmp
= param_getchar(Cmd
, 5);
616 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
617 PrintAndLog("Target key type must be A or B");
620 if (ctmp
!= 'A' && ctmp
!= 'a')
625 case '0': SectorsCnt
= 05; break;
626 case '1': SectorsCnt
= 16; break;
627 case '2': SectorsCnt
= 32; break;
628 case '4': SectorsCnt
= 40; break;
629 default: SectorsCnt
= 16;
633 ctmp
= param_getchar(Cmd
, 4);
634 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
635 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
637 ctmp
= param_getchar(Cmd
, 6);
638 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
639 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
642 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
643 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
646 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
647 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
648 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
649 default : PrintAndLog("Unknown Error.\n");
653 key64
= bytes_to_num(keyBlock
, 6);
655 PrintAndLog("Found valid key:%012"llx
, key64
);
657 // transfer key to the emulator
659 uint8_t sectortrailer
;
660 if (trgBlockNo
< 32*4) { // 4 block sector
661 sectortrailer
= (trgBlockNo
& 0x03) + 3;
662 } else { // 16 block sector
663 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
665 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
668 num_to_bytes(key64
, 6, keyBlock
);
670 num_to_bytes(key64
, 6, &keyBlock
[10]);
671 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
674 PrintAndLog("No valid key found");
677 else { // ------------------------------------ multiple sectors working
678 clock_t time1
= clock();
680 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
681 if (e_sector
== NULL
) return 1;
683 //test current key and additional standard keys first
684 memcpy(keyBlock
, key
, 6);
685 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
686 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
687 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
688 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
689 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
690 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
691 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
692 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
693 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
694 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
695 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
696 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
697 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
699 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
700 for (i
= 0; i
< SectorsCnt
; i
++) {
701 for (j
= 0; j
< 2; j
++) {
702 if (e_sector
[i
].foundKey
[j
]) continue;
704 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
707 e_sector
[i
].Key
[j
] = key64
;
708 e_sector
[i
].foundKey
[j
] = 1;
715 PrintAndLog("nested...");
716 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
++) {
720 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
721 PrintAndLog("-----------------------------------------------");
722 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 default : PrintAndLog("Unknown Error.\n");
738 key64
= bytes_to_num(keyBlock
, 6);
740 PrintAndLog("Found valid key:%012"llx
, key64
);
741 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
742 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
748 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
749 PrintAndLog("testing to read B...");
750 for (i
= 0; i
< SectorsCnt
; i
++) {
751 // KEY A but not KEY B
752 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
754 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
756 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
757 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
758 clearCommandBuffer();
762 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
764 uint8_t isOK
= resp
.arg
[0] & 0xff;
765 uint8_t *data
= resp
.d
.asBytes
;
769 key64
= bytes_to_num(data
+10, 6);
771 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
772 e_sector
[i
].foundKey
[1] = 1;
773 e_sector
[i
].Key
[1] = key64
;
779 PrintAndLog("Time in nested: %1.2f (%1.2f sec per key)\n\n", ((float)clock() - time1
)/CLOCKS_PER_SEC
, ((float)clock() - time1
)/iterations
/CLOCKS_PER_SEC
);
781 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
783 PrintAndLog("|---|----------------|---|----------------|---|");
784 PrintAndLog("|sec|key A |res|key B |res|");
785 PrintAndLog("|---|----------------|---|----------------|---|");
786 for (i
= 0; i
< SectorsCnt
; i
++) {
787 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
789 e_sector
[i
].foundKey
[0],
791 e_sector
[i
].foundKey
[1]
794 PrintAndLog("|---|----------------|---|----------------|---|");
796 // transfer them to the emulator
798 for (i
= 0; i
< SectorsCnt
; i
++) {
799 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
800 if (e_sector
[i
].foundKey
[0])
801 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
802 if (e_sector
[i
].foundKey
[1])
803 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
804 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
809 if (createDumpFile
) {
810 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
811 PrintAndLog("Could not create file dumpkeys.bin");
815 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
816 for(i
=0; i
<SectorsCnt
; i
++) {
817 if (e_sector
[i
].foundKey
[0]){
818 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
819 fwrite ( tempkey
, 1, 6, fkeys
);
822 fwrite ( &standart
, 1, 6, fkeys
);
825 for(i
=0; i
<SectorsCnt
; i
++) {
826 if (e_sector
[i
].foundKey
[1]){
827 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
828 fwrite ( tempkey
, 1, 6, fkeys
);
831 fwrite ( &standart
, 1, 6, fkeys
);
842 int CmdHF14AMfNestedHard(const char *Cmd
)
846 uint8_t trgBlockNo
= 0;
847 uint8_t trgKeyType
= 0;
848 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
849 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
852 ctmp
= param_getchar(Cmd
, 0);
854 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) {
855 PrintAndLog("Usage:");
856 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
857 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
858 PrintAndLog(" or hf mf hardnested r [known target key]");
860 PrintAndLog("Options: ");
861 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
862 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
863 PrintAndLog(" r: Read nonces.bin and start attack");
865 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
866 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
867 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
868 PrintAndLog(" sample4: hf mf hardnested r");
870 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
871 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
875 bool know_target_key
= false;
876 bool nonce_file_read
= false;
877 bool nonce_file_write
= false;
882 if (ctmp
== 'R' || ctmp
== 'r') {
883 nonce_file_read
= true;
884 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
885 know_target_key
= true;
887 } else if (ctmp
== 'T' || ctmp
== 't') {
888 tests
= param_get32ex(Cmd
, 1, 100, 10);
890 blockNo
= param_get8(Cmd
, 0);
891 ctmp
= param_getchar(Cmd
, 1);
892 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
893 PrintAndLog("Key type must be A or B");
896 if (ctmp
!= 'A' && ctmp
!= 'a') {
900 if (param_gethex(Cmd
, 2, key
, 12)) {
901 PrintAndLog("Key must include 12 HEX symbols");
905 trgBlockNo
= param_get8(Cmd
, 3);
906 ctmp
= param_getchar(Cmd
, 4);
907 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
908 PrintAndLog("Target key type must be A or B");
911 if (ctmp
!= 'A' && ctmp
!= 'a') {
917 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
918 know_target_key
= true;
922 while ((ctmp
= param_getchar(Cmd
, i
))) {
923 if (ctmp
== 's' || ctmp
== 'S') {
925 } else if (ctmp
== 'w' || ctmp
== 'W') {
926 nonce_file_write
= true;
928 PrintAndLog("Possible options are w and/or s");
935 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 ",
938 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
939 know_target_key
?"":" (not set)",
940 nonce_file_write
?"write":nonce_file_read
?"read":"none",
944 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
948 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
949 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
958 int CmdHF14AMfChk(const char *Cmd
)
961 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
962 PrintAndLog(" * - all sectors");
963 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
964 PrintAndLog("d - write keys to binary file");
965 PrintAndLog("t - write keys to emulator memory\n");
966 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
967 PrintAndLog(" hf mf chk *1 ? t");
968 PrintAndLog(" hf mf chk *1 ? d");
973 char filename
[FILE_PATH_SIZE
]={0};
975 uint8_t *keyBlock
= NULL
, *p
;
976 uint8_t stKeyBlock
= 20;
982 uint8_t SectorsCnt
= 1;
986 int transferToEml
= 0;
987 int createDumpFile
= 0;
989 keyBlock
= calloc(stKeyBlock
, 6);
990 if (keyBlock
== NULL
) return 1;
992 uint64_t defaultKeys
[] =
994 0xffffffffffff, // Default key (first key used by program if no user defined key)
995 0x000000000000, // Blank key
996 0xa0a1a2a3a4a5, // NFCForum MAD key
1008 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1010 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1012 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1015 if (param_getchar(Cmd
, 0)=='*') {
1017 switch(param_getchar(Cmd
+1, 0)) {
1018 case '0': SectorsCnt
= 5; break;
1019 case '1': SectorsCnt
= 16; break;
1020 case '2': SectorsCnt
= 32; break;
1021 case '4': SectorsCnt
= 40; break;
1022 default: SectorsCnt
= 16;
1026 blockNo
= param_get8(Cmd
, 0);
1028 ctmp
= param_getchar(Cmd
, 1);
1040 PrintAndLog("Key type must be A , B or ?");
1045 ctmp
= param_getchar(Cmd
, 2);
1046 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1047 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1049 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1050 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1051 if ( stKeyBlock
- keycnt
< 2) {
1052 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1054 PrintAndLog("Cannot allocate memory for Keys");
1060 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1061 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1062 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1065 // May be a dic file
1066 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1067 PrintAndLog("File name too long");
1072 if ( (f
= fopen( filename
, "r")) ) {
1073 while( fgets(buf
, sizeof(buf
), f
) ){
1074 if (strlen(buf
) < 12 || buf
[11] == '\n')
1077 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1079 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1081 if (!isxdigit(buf
[0])){
1082 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1088 if ( stKeyBlock
- keycnt
< 2) {
1089 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1091 PrintAndLog("Cannot allocate memory for defKeys");
1098 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1099 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1100 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1102 memset(buf
, 0, sizeof(buf
));
1106 PrintAndLog("File: %s: not found or locked.", filename
);
1115 PrintAndLog("No key specified, trying default keys");
1116 for (;keycnt
< defaultKeysSize
; keycnt
++)
1117 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1118 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1119 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1122 // initialize storage for found keys
1123 bool validKey
[2][40];
1124 uint8_t foundKey
[2][40][6];
1125 for (uint16_t t
= 0; t
< 2; t
++) {
1126 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1127 validKey
[t
][sectorNo
] = false;
1128 for (uint16_t i
= 0; i
< 6; i
++) {
1129 foundKey
[t
][sectorNo
][i
] = 0xff;
1134 clock_t time1
= clock();
1136 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
1138 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1139 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
1140 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1141 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1142 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1143 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
1146 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1147 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
1148 validKey
[t
][i
] = true;
1151 PrintAndLog("Command execute timeout");
1154 b
<127?(b
+=4):(b
+=16);
1157 printf("Time in checkkeys: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1
)/CLOCKS_PER_SEC
, ((float)clock() - time1
)/keycnt
/CLOCKS_PER_SEC
);
1160 if (transferToEml
) {
1162 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1163 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1164 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1165 for (uint16_t t
= 0; t
< 2; t
++) {
1166 if (validKey
[t
][sectorNo
]) {
1167 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1170 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1173 PrintAndLog("Found keys have been transferred to the emulator memory");
1176 if (createDumpFile
) {
1177 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1178 if (fkeys
== NULL
) {
1179 PrintAndLog("Could not create file dumpkeys.bin");
1183 for (uint16_t t
= 0; t
< 2; t
++) {
1184 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1187 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1195 int CmdHF14AMf1kSim(const char *Cmd
)
1197 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1198 uint8_t exitAfterNReads
= 0;
1201 uint8_t cmdp
= param_getchar(Cmd
, 0);
1203 if (cmdp
== 'h' || cmdp
== 'H') {
1204 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1205 PrintAndLog(" h this help");
1206 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1207 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1208 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1209 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1211 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1215 if (param_getchar(Cmd
, pnr
) == 'u') {
1216 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1218 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1219 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1220 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1222 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1228 if (param_getchar(Cmd
, pnr
) == 'n') {
1229 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1233 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1234 //Using a flag to signal interactiveness, least significant bit
1235 flags
|= FLAG_INTERACTIVE
;
1239 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1240 //Using a flag to signal interactiveness, least significant bit
1241 flags
|= FLAG_NR_AR_ATTACK
;
1244 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1245 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1246 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1252 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1253 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1254 clearCommandBuffer();
1257 if(flags
& FLAG_INTERACTIVE
)
1263 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1266 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1268 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1270 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1272 memset(data
, 0x00, sizeof(data
));
1273 memset(key
, 0x00, sizeof(key
));
1274 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1276 memcpy(data
, resp
.d
.asBytes
, len
);
1278 uint64_t corr_uid
= 0;
1280 // this IF? what was I thinking of?
1281 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1282 corr_uid
= ((uint64_t)(data
[3] << 24)) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1283 tryMfk32(corr_uid
, data
, key
);
1285 corr_uid
|= (uint64_t)data
[2] << 48;
1286 corr_uid
|= (uint64_t)data
[1] << 40;
1287 corr_uid
|= (uint64_t)data
[0] << 32;
1288 corr_uid
|= (uint64_t)data
[7] << 24;
1289 corr_uid
|= (uint64_t)data
[6] << 16;
1290 corr_uid
|= (uint64_t)data
[5] << 8;
1291 corr_uid
|= (uint64_t)data
[4];
1292 tryMfk64(corr_uid
, data
, key
);
1300 int CmdHF14AMfDbg(const char *Cmd
)
1302 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1304 PrintAndLog("Max debug mode parameter is 4 \n");
1307 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1308 PrintAndLog("Usage: hf mf dbg <debug level>");
1309 PrintAndLog(" 0 - no debug messages");
1310 PrintAndLog(" 1 - error messages");
1311 PrintAndLog(" 2 - plus information messages");
1312 PrintAndLog(" 3 - plus debug messages");
1313 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1314 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1318 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1324 int CmdHF14AMfEGet(const char *Cmd
)
1326 uint8_t blockNo
= 0;
1327 uint8_t data
[16] = {0x00};
1329 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1330 PrintAndLog("Usage: hf mf eget <block number>");
1331 PrintAndLog(" sample: hf mf eget 0 ");
1335 blockNo
= param_get8(Cmd
, 0);
1338 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1339 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1341 PrintAndLog("Command execute timeout");
1347 int CmdHF14AMfEClear(const char *Cmd
)
1349 if (param_getchar(Cmd
, 0) == 'h') {
1350 PrintAndLog("Usage: hf mf eclr");
1351 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1355 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1360 int CmdHF14AMfESet(const char *Cmd
)
1362 uint8_t memBlock
[16];
1363 uint8_t blockNo
= 0;
1365 memset(memBlock
, 0x00, sizeof(memBlock
));
1367 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1368 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1369 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1373 blockNo
= param_get8(Cmd
, 0);
1375 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1376 PrintAndLog("block data must include 32 HEX symbols");
1381 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1382 memcpy(c
.d
.asBytes
, memBlock
, 16);
1387 int CmdHF14AMfELoad(const char *Cmd
)
1390 char filename
[FILE_PATH_SIZE
];
1391 char *fnameptr
= filename
;
1392 char buf
[64] = {0x00};
1393 uint8_t buf8
[64] = {0x00};
1394 int i
, len
, blockNum
, numBlocks
;
1395 int nameParamNo
= 1;
1396 uint8_t blockWidth
= 32;
1397 char ctmp
= param_getchar(Cmd
, 0);
1399 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1400 PrintAndLog("It loads emul dump from the file `filename.eml`");
1401 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1402 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1404 PrintAndLog(" sample: hf mf eload filename");
1405 PrintAndLog(" hf mf eload 4 filename");
1410 case '0' : numBlocks
= 5*4; break;
1412 case '\0': numBlocks
= 16*4; break;
1413 case '2' : numBlocks
= 32*4; break;
1414 case '4' : numBlocks
= 256; break;
1415 case 'U' : // fall through
1416 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1422 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1423 if (numblk2
> 0) numBlocks
= numblk2
;
1425 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1427 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1431 sprintf(fnameptr
, ".eml");
1434 f
= fopen(filename
, "r");
1436 PrintAndLog("File %s not found or locked", filename
);
1442 memset(buf
, 0, sizeof(buf
));
1444 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1446 if (blockNum
>= numBlocks
) break;
1448 PrintAndLog("File reading error.");
1453 if (strlen(buf
) < blockWidth
){
1454 if(strlen(buf
) && feof(f
))
1456 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1461 for (i
= 0; i
< blockWidth
; i
+= 2) {
1462 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1464 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1465 PrintAndLog("Cant set emul block: %3d", blockNum
);
1472 if (blockNum
>= numBlocks
) break;
1477 if ((blockNum
!= numBlocks
)) {
1478 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1481 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1485 int CmdHF14AMfESave(const char *Cmd
)
1488 char filename
[FILE_PATH_SIZE
];
1489 char * fnameptr
= filename
;
1491 int i
, j
, len
, numBlocks
;
1492 int nameParamNo
= 1;
1494 memset(filename
, 0, sizeof(filename
));
1495 memset(buf
, 0, sizeof(buf
));
1497 char ctmp
= param_getchar(Cmd
, 0);
1499 if ( ctmp
== 'h' || ctmp
== 'H') {
1500 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1501 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1502 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1504 PrintAndLog(" sample: hf mf esave ");
1505 PrintAndLog(" hf mf esave 4");
1506 PrintAndLog(" hf mf esave 4 filename");
1511 case '0' : numBlocks
= 5*4; break;
1513 case '\0': numBlocks
= 16*4; break;
1514 case '2' : numBlocks
= 32*4; break;
1515 case '4' : numBlocks
= 256; break;
1522 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1524 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1526 // user supplied filename?
1528 // get filename (UID from memory)
1529 if (mfEmlGetMem(buf
, 0, 1)) {
1530 PrintAndLog("Can\'t get UID from block: %d", 0);
1531 len
= sprintf(fnameptr
, "dump");
1535 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1536 sprintf(fnameptr
, "%02X", buf
[j
]);
1542 // add file extension
1543 sprintf(fnameptr
, ".eml");
1546 f
= fopen(filename
, "w+");
1549 PrintAndLog("Can't open file %s ", filename
);
1554 for (i
= 0; i
< numBlocks
; i
++) {
1555 if (mfEmlGetMem(buf
, i
, 1)) {
1556 PrintAndLog("Cant get block: %d", i
);
1559 for (j
= 0; j
< 16; j
++)
1560 fprintf(f
, "%02X", buf
[j
]);
1565 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1570 int CmdHF14AMfECFill(const char *Cmd
)
1572 uint8_t keyType
= 0;
1573 uint8_t numSectors
= 16;
1575 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1576 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1577 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1579 PrintAndLog("samples: hf mf ecfill A");
1580 PrintAndLog(" hf mf ecfill A 4");
1581 PrintAndLog("Read card and transfer its data to emulator memory.");
1582 PrintAndLog("Keys must be laid in the emulator memory. \n");
1586 char ctmp
= param_getchar(Cmd
, 0);
1587 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1588 PrintAndLog("Key type must be A or B");
1591 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1593 ctmp
= param_getchar(Cmd
, 1);
1595 case '0' : numSectors
= 5; break;
1597 case '\0': numSectors
= 16; break;
1598 case '2' : numSectors
= 32; break;
1599 case '4' : numSectors
= 40; break;
1600 default: numSectors
= 16;
1603 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1604 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1609 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1614 uint64_t keyA
, keyB
;
1616 char cmdp
= param_getchar(Cmd
, 0);
1618 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1619 PrintAndLog("It prints the keys loaded in the emulator memory");
1620 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1621 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1623 PrintAndLog(" sample: hf mf ekeyprn 1");
1628 case '0' : numSectors
= 5; break;
1630 case '\0': numSectors
= 16; break;
1631 case '2' : numSectors
= 32; break;
1632 case '4' : numSectors
= 40; break;
1633 default: numSectors
= 16;
1636 PrintAndLog("|---|----------------|----------------|");
1637 PrintAndLog("|sec|key A |key B |");
1638 PrintAndLog("|---|----------------|----------------|");
1639 for (i
= 0; i
< numSectors
; i
++) {
1640 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1641 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1644 keyA
= bytes_to_num(data
, 6);
1645 keyB
= bytes_to_num(data
+ 10, 6);
1646 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1648 PrintAndLog("|---|----------------|----------------|");
1653 int CmdHF14AMfCSetUID(const char *Cmd
)
1655 uint8_t wipeCard
= 0;
1656 uint8_t uid
[8] = {0x00};
1657 uint8_t oldUid
[8] = {0x00};
1658 uint8_t atqa
[2] = {0x00};
1659 uint8_t sak
[1] = {0x00};
1660 uint8_t atqaPresent
= 1;
1665 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1666 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1667 PrintAndLog("sample: hf mf csetuid 01020304");
1668 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1669 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1670 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1674 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1675 PrintAndLog("UID must include 8 HEX symbols");
1680 ctmp
= param_getchar(Cmd
, argi
);
1681 if (ctmp
== 'w' || ctmp
== 'W') {
1687 if (param_getchar(Cmd
, argi
)) {
1688 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1689 PrintAndLog("ATQA must include 4 HEX symbols");
1693 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1694 PrintAndLog("SAK must include 2 HEX symbols");
1703 ctmp
= param_getchar(Cmd
, argi
);
1704 if (ctmp
== 'w' || ctmp
== 'W') {
1709 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1711 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1713 PrintAndLog("Can't set UID. error=%d", res
);
1717 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1718 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1722 int CmdHF14AMfCSetBlk(const char *Cmd
)
1724 uint8_t block
[16] = {0x00};
1725 uint8_t blockNo
= 0;
1726 uint8_t params
= MAGIC_SINGLE
;
1729 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1730 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1731 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1732 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1733 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1737 blockNo
= param_get8(Cmd
, 0);
1739 if (param_gethex(Cmd
, 1, block
, 32)) {
1740 PrintAndLog("block data must include 32 HEX symbols");
1744 char ctmp
= param_getchar(Cmd
, 2);
1745 if (ctmp
== 'w' || ctmp
== 'W')
1746 params
|= MAGIC_WIPE
;
1748 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
1750 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
1752 PrintAndLog("Can't write block. error=%d", res
);
1758 int CmdHF14AMfCLoad(const char *Cmd
)
1761 char filename
[FILE_PATH_SIZE
];
1762 char * fnameptr
= filename
;
1763 char buf
[64] = {0x00};
1764 uint8_t buf8
[64] = {0x00};
1765 uint8_t fillFromEmulator
= 0;
1766 int i
, len
, blockNum
, flags
=0;
1768 memset(filename
, 0, sizeof(filename
));
1770 char ctmp
= param_getchar(Cmd
, 0);
1772 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1773 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1774 PrintAndLog("or from emulator memory (option `e`)");
1775 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1776 PrintAndLog(" or: hf mf cload e ");
1777 PrintAndLog(" sample: hf mf cload filename");
1781 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1783 if (fillFromEmulator
) {
1784 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1785 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1786 PrintAndLog("Cant get block: %d", blockNum
);
1789 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1790 if (blockNum
== 1) flags
= 0; // just write
1791 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
1793 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1794 PrintAndLog("Cant set magic card block: %d", blockNum
);
1801 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1803 memcpy(filename
, Cmd
, len
);
1806 sprintf(fnameptr
, ".eml");
1809 f
= fopen(filename
, "r");
1811 PrintAndLog("File not found or locked.");
1818 memset(buf
, 0, sizeof(buf
));
1820 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1822 PrintAndLog("File reading error.");
1826 if (strlen(buf
) < 32) {
1827 if(strlen(buf
) && feof(f
))
1829 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1833 for (i
= 0; i
< 32; i
+= 2)
1834 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1836 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1837 if (blockNum
== 1) flags
= 0; // just write
1838 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
1840 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1841 PrintAndLog("Can't set magic card block: %d", blockNum
);
1847 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1852 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1853 PrintAndLog("File content error. There must be 64 blocks");
1856 PrintAndLog("Loaded from file: %s", filename
);
1862 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1864 uint8_t blockNo
= 0;
1866 memset(data
, 0x00, sizeof(data
));
1867 char ctmp
= param_getchar(Cmd
, 0);
1869 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1870 PrintAndLog("Usage: hf mf cgetblk <block number>");
1871 PrintAndLog("sample: hf mf cgetblk 1");
1872 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1876 blockNo
= param_get8(Cmd
, 0);
1878 PrintAndLog("--block number:%2d ", blockNo
);
1880 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
1882 PrintAndLog("Can't read block. error=%d", res
);
1886 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
1890 int CmdHF14AMfCGetSc(const char *Cmd
) {
1892 uint8_t sectorNo
= 0;
1894 memset(data
, 0x00, sizeof(data
));
1895 char ctmp
= param_getchar(Cmd
, 0);
1897 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1898 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1899 PrintAndLog("sample: hf mf cgetsc 0");
1900 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1904 sectorNo
= param_get8(Cmd
, 0);
1905 if (sectorNo
> 15) {
1906 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1910 PrintAndLog("--sector number:%d ", sectorNo
);
1911 PrintAndLog("block | data");
1913 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1914 for (i
= 0; i
< 4; i
++) {
1915 if (i
== 1) flags
= 0;
1916 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1918 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
1920 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1923 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
1928 int CmdHF14AMfCSave(const char *Cmd
) {
1931 char filename
[FILE_PATH_SIZE
];
1932 char * fnameptr
= filename
;
1933 uint8_t fillFromEmulator
= 0;
1935 int i
, j
, len
, flags
;
1937 memset(filename
, 0, sizeof(filename
));
1938 memset(buf
, 0, sizeof(buf
));
1939 char ctmp
= param_getchar(Cmd
, 0);
1941 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1942 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1943 PrintAndLog("or into emulator memory (option `e`)");
1944 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1945 PrintAndLog(" sample: hf mf esave ");
1946 PrintAndLog(" hf mf esave filename");
1947 PrintAndLog(" hf mf esave e \n");
1950 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1952 if (fillFromEmulator
) {
1953 // put into emulator
1954 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1955 for (i
= 0; i
< 16 * 4; i
++) {
1956 if (i
== 1) flags
= 0;
1957 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1959 if (mfCGetBlock(i
, buf
, flags
)) {
1960 PrintAndLog("Cant get block: %d", i
);
1964 if (mfEmlSetMem(buf
, i
, 1)) {
1965 PrintAndLog("Cant set emul block: %d", i
);
1972 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1974 // get filename based on UID
1977 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
1978 PrintAndLog("Cant get block: %d", 0);
1979 len
= sprintf(fnameptr
, "dump");
1982 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1983 sprintf(fnameptr
, "%02x", buf
[j
]);
1986 memcpy(filename
, Cmd
, len
);
1990 // add .eml extension
1991 sprintf(fnameptr
, ".eml");
1994 f
= fopen(filename
, "w+");
1997 PrintAndLog("File not found or locked.");
2002 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2003 for (i
= 0; i
< 16 * 4; i
++) {
2004 if (i
== 1) flags
= 0;
2005 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2007 if (mfCGetBlock(i
, buf
, flags
)) {
2008 PrintAndLog("Cant get block: %d", i
);
2011 for (j
= 0; j
< 16; j
++)
2012 fprintf(f
, "%02x", buf
[j
]);
2017 PrintAndLog("Saved to file: %s", filename
);
2022 int CmdHF14AMfSniff(const char *Cmd
){
2024 bool wantLogToFile
= 0;
2025 bool wantDecrypt
= 0;
2026 //bool wantSaveToEml = 0; TODO
2027 bool wantSaveToEmlFile
= 0;
2038 uint8_t atqa
[2] = {0x00};
2041 uint8_t *buf
= NULL
;
2042 uint16_t bufsize
= 0;
2043 uint8_t *bufPtr
= NULL
;
2045 char ctmp
= param_getchar(Cmd
, 0);
2046 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2047 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
2048 PrintAndLog("You can specify:");
2049 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
2050 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
2051 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2052 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2053 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
2054 PrintAndLog(" sample: hf mf sniff l d e");
2058 for (int i
= 0; i
< 4; i
++) {
2059 ctmp
= param_getchar(Cmd
, i
);
2060 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
2061 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
2062 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2063 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
2066 printf("-------------------------------------------------------------------------\n");
2067 printf("Executing command. \n");
2068 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2069 printf("Press the key on pc keyboard to abort the client.\n");
2070 printf("-------------------------------------------------------------------------\n");
2072 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
2073 clearCommandBuffer();
2081 tmpchar
= getchar();
2083 printf("\naborted via keyboard!\n");
2088 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2089 res
= resp
.arg
[0] & 0xff;
2090 uint16_t traceLen
= resp
.arg
[1];
2095 return 0; // we are done
2098 if (res
== 1) { // there is (more) data to be transferred
2099 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
2100 if (traceLen
> bufsize
) {
2102 if (buf
== NULL
) { // not yet allocated
2103 p
= malloc(traceLen
);
2104 } else { // need more memory
2105 p
= realloc(buf
, traceLen
);
2108 PrintAndLog("Cannot allocate memory for trace");
2116 memset(buf
, 0x00, traceLen
);
2118 if (bufPtr
== NULL
) {
2119 PrintAndLog("Cannot allocate memory for trace");
2123 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2128 if (res
== 2) { // received all data, start displaying
2129 blockLen
= bufPtr
- buf
;
2132 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2133 while (bufPtr
- buf
< blockLen
) {
2134 bufPtr
+= 6; // skip (void) timing information
2135 len
= *((uint16_t *)bufPtr
);
2143 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
2144 memcpy(uid
, bufPtr
+ 2, 7);
2145 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2146 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2148 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2149 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2153 if (wantLogToFile
|| wantDecrypt
) {
2154 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2155 AddLogCurrentDT(logHexFileName
);
2158 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2160 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2162 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2164 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
2168 bufPtr
+= ((len
-1)/8+1); // ignore parity
2179 //needs nt, ar, at, Data to decrypt
2180 int CmdHf14MfDecryptBytes(const char *Cmd
){
2183 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2184 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2185 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2188 param_gethex_ex(Cmd
, 3, data
, &len
);
2191 int limit
= sizeof(data
) / 2;
2196 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2199 static command_t CommandTable
[] = {
2200 {"help", CmdHelp
, 1, "This help"},
2201 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2202 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2203 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2204 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2205 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2206 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2207 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2208 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2209 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2210 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2211 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2212 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2213 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2214 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2215 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2216 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2217 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2218 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2219 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2220 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2221 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2222 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2223 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2224 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2225 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2226 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2227 {NULL
, NULL
, 0, NULL
}
2230 int CmdHFMF(const char *Cmd
) {
2232 clearCommandBuffer();
2233 //WaitForResponseTimeout(CMD_ACK,NULL,100);
2234 CmdsParse(CommandTable
, Cmd
);
2238 int CmdHelp(const char *Cmd
) {
2239 CmdsHelp(CommandTable
);