]>
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
315 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
316 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
317 PrintAndLog("File reading error.");
323 // Read keys B from file
324 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
325 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
326 PrintAndLog("File reading error.");
334 PrintAndLog("|-----------------------------------------|");
335 PrintAndLog("|------ Reading sector access bits...-----|");
336 PrintAndLog("|-----------------------------------------|");
338 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
339 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
340 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
341 clearCommandBuffer();
344 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
345 uint8_t isOK
= resp
.arg
[0] & 0xff;
346 uint8_t *data
= resp
.d
.asBytes
;
348 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
349 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
350 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
351 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
353 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
354 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
355 rights
[sectorNo
][3] = 0x01;
358 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
359 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
360 rights
[sectorNo
][3] = 0x01;
364 PrintAndLog("|-----------------------------------------|");
365 PrintAndLog("|----- Dumping all blocks to file... -----|");
366 PrintAndLog("|-----------------------------------------|");
369 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
370 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
371 bool received
= false;
373 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
374 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
375 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
376 clearCommandBuffer();
378 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
379 } else { // data block. Check if it can be read with key A or key B
380 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
381 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
382 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
383 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
385 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
386 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
388 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
389 } else { // key A would work
390 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
391 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
392 clearCommandBuffer();
394 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
399 isOK
= resp
.arg
[0] & 0xff;
400 uint8_t *data
= resp
.d
.asBytes
;
401 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
402 data
[0] = (keyA
[sectorNo
][0]);
403 data
[1] = (keyA
[sectorNo
][1]);
404 data
[2] = (keyA
[sectorNo
][2]);
405 data
[3] = (keyA
[sectorNo
][3]);
406 data
[4] = (keyA
[sectorNo
][4]);
407 data
[5] = (keyA
[sectorNo
][5]);
408 data
[10] = (keyB
[sectorNo
][0]);
409 data
[11] = (keyB
[sectorNo
][1]);
410 data
[12] = (keyB
[sectorNo
][2]);
411 data
[13] = (keyB
[sectorNo
][3]);
412 data
[14] = (keyB
[sectorNo
][4]);
413 data
[15] = (keyB
[sectorNo
][5]);
416 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
417 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
419 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
425 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
432 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
433 PrintAndLog("Could not create file name dumpdata.bin");
436 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
437 fwrite(carddata
, 1, 16*numblocks
, fout
);
439 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
445 int CmdHF14AMfRestore(const char *Cmd
)
447 uint8_t sectorNo
,blockNo
;
449 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
450 uint8_t bldata
[16] = {0x00};
458 char cmdp
= param_getchar(Cmd
, 0);
460 case '0' : numSectors
= 5; break;
462 case '\0': numSectors
= 16; break;
463 case '2' : numSectors
= 32; break;
464 case '4' : numSectors
= 40; break;
465 default: numSectors
= 16;
468 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
469 PrintAndLog("Usage: hf mf restore [card memory]");
470 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
472 PrintAndLog("Samples: hf mf restore");
473 PrintAndLog(" hf mf restore 4");
477 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
478 PrintAndLog("Could not find file dumpkeys.bin");
482 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
483 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
484 PrintAndLog("File reading error (dumpkeys.bin).");
490 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
491 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
492 PrintAndLog("File reading error (dumpkeys.bin).");
500 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
501 PrintAndLog("Could not find file dumpdata.bin");
504 PrintAndLog("Restoring dumpdata.bin to card");
506 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
507 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
508 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
509 memcpy(c
.d
.asBytes
, key
, 6);
511 if (fread(bldata
, 1, 16, fdump
) == 0) {
512 PrintAndLog("File reading error (dumpdata.bin).");
517 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
518 bldata
[0] = (keyA
[sectorNo
][0]);
519 bldata
[1] = (keyA
[sectorNo
][1]);
520 bldata
[2] = (keyA
[sectorNo
][2]);
521 bldata
[3] = (keyA
[sectorNo
][3]);
522 bldata
[4] = (keyA
[sectorNo
][4]);
523 bldata
[5] = (keyA
[sectorNo
][5]);
524 bldata
[10] = (keyB
[sectorNo
][0]);
525 bldata
[11] = (keyB
[sectorNo
][1]);
526 bldata
[12] = (keyB
[sectorNo
][2]);
527 bldata
[13] = (keyB
[sectorNo
][3]);
528 bldata
[14] = (keyB
[sectorNo
][4]);
529 bldata
[15] = (keyB
[sectorNo
][5]);
532 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
534 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
535 clearCommandBuffer();
539 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
540 uint8_t isOK
= resp
.arg
[0] & 0xff;
541 PrintAndLog("isOk:%02x", isOK
);
543 PrintAndLog("Command execute timeout");
552 int CmdHF14AMfNested(const char *Cmd
)
554 int i
, j
, res
, iterations
;
555 sector
*e_sector
= NULL
;
558 uint8_t trgBlockNo
= 0;
559 uint8_t trgKeyType
= 0;
560 uint8_t SectorsCnt
= 0;
561 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
562 uint8_t keyBlock
[14*6];
564 bool transferToEml
= false;
566 bool createDumpFile
= false;
568 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
569 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
574 PrintAndLog("Usage:");
575 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
576 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
577 PrintAndLog(" <target block number> <target key A/B> [t]");
578 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
579 PrintAndLog("t - transfer keys into emulator memory");
580 PrintAndLog("d - write keys to binary file");
582 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
583 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
584 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
585 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
589 cmdp
= param_getchar(Cmd
, 0);
590 blockNo
= param_get8(Cmd
, 1);
591 ctmp
= param_getchar(Cmd
, 2);
593 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
594 PrintAndLog("Key type must be A or B");
598 if (ctmp
!= 'A' && ctmp
!= 'a')
601 if (param_gethex(Cmd
, 3, key
, 12)) {
602 PrintAndLog("Key must include 12 HEX symbols");
606 if (cmdp
== 'o' || cmdp
== 'O') {
608 trgBlockNo
= param_get8(Cmd
, 4);
609 ctmp
= param_getchar(Cmd
, 5);
610 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
611 PrintAndLog("Target key type must be A or B");
614 if (ctmp
!= 'A' && ctmp
!= 'a')
619 case '0': SectorsCnt
= 05; break;
620 case '1': SectorsCnt
= 16; break;
621 case '2': SectorsCnt
= 32; break;
622 case '4': SectorsCnt
= 40; break;
623 default: SectorsCnt
= 16;
627 ctmp
= param_getchar(Cmd
, 4);
628 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
629 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
631 ctmp
= param_getchar(Cmd
, 6);
632 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
633 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
636 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
637 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
640 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
641 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
642 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
643 default : PrintAndLog("Unknown Error.\n");
647 key64
= bytes_to_num(keyBlock
, 6);
649 PrintAndLog("Found valid key:%012"llx
, key64
);
651 // transfer key to the emulator
653 uint8_t sectortrailer
;
654 if (trgBlockNo
< 32*4) { // 4 block sector
655 sectortrailer
= (trgBlockNo
& 0x03) + 3;
656 } else { // 16 block sector
657 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
659 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
662 num_to_bytes(key64
, 6, keyBlock
);
664 num_to_bytes(key64
, 6, &keyBlock
[10]);
665 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
668 PrintAndLog("No valid key found");
671 else { // ------------------------------------ multiple sectors working
672 clock_t time1
= clock();
674 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
675 if (e_sector
== NULL
) return 1;
677 //test current key and additional standard keys first
678 memcpy(keyBlock
, key
, 6);
679 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
680 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
681 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
682 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
683 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
684 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
685 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
686 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
687 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
688 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
689 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
690 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
691 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
693 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
694 for (i
= 0; i
< SectorsCnt
; i
++) {
695 for (j
= 0; j
< 2; j
++) {
696 if (e_sector
[i
].foundKey
[j
]) continue;
698 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
701 e_sector
[i
].Key
[j
] = key64
;
702 e_sector
[i
].foundKey
[j
] = 1;
709 PrintAndLog("nested...");
710 bool calibrate
= true;
711 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
712 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
713 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
714 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
715 PrintAndLog("-----------------------------------------------");
716 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
719 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
720 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
721 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
722 default : PrintAndLog("Unknown Error.\n");
732 key64
= bytes_to_num(keyBlock
, 6);
734 PrintAndLog("Found valid key:%012"llx
, key64
);
735 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
736 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
742 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
743 PrintAndLog("testing to read B...");
744 for (i
= 0; i
< SectorsCnt
; i
++) {
745 // KEY A but not KEY B
746 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
748 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
750 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
751 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
752 clearCommandBuffer();
756 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
758 uint8_t isOK
= resp
.arg
[0] & 0xff;
759 uint8_t *data
= resp
.d
.asBytes
;
763 key64
= bytes_to_num(data
+10, 6);
765 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
766 e_sector
[i
].foundKey
[1] = 1;
767 e_sector
[i
].Key
[1] = key64
;
773 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
);
775 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
777 PrintAndLog("|---|----------------|---|----------------|---|");
778 PrintAndLog("|sec|key A |res|key B |res|");
779 PrintAndLog("|---|----------------|---|----------------|---|");
780 for (i
= 0; i
< SectorsCnt
; i
++) {
781 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
783 e_sector
[i
].foundKey
[0],
785 e_sector
[i
].foundKey
[1]
788 PrintAndLog("|---|----------------|---|----------------|---|");
790 // transfer them to the emulator
792 for (i
= 0; i
< SectorsCnt
; i
++) {
793 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
794 if (e_sector
[i
].foundKey
[0])
795 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
796 if (e_sector
[i
].foundKey
[1])
797 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
798 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
803 if (createDumpFile
) {
804 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
805 PrintAndLog("Could not create file dumpkeys.bin");
809 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
810 for(i
=0; i
<SectorsCnt
; i
++) {
811 if (e_sector
[i
].foundKey
[0]){
812 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
813 fwrite ( tempkey
, 1, 6, fkeys
);
816 fwrite ( &standart
, 1, 6, fkeys
);
819 for(i
=0; i
<SectorsCnt
; i
++) {
820 if (e_sector
[i
].foundKey
[1]){
821 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
822 fwrite ( tempkey
, 1, 6, fkeys
);
825 fwrite ( &standart
, 1, 6, fkeys
);
836 int CmdHF14AMfNestedHard(const char *Cmd
)
840 uint8_t trgBlockNo
= 0;
841 uint8_t trgKeyType
= 0;
842 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
843 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
846 ctmp
= param_getchar(Cmd
, 0);
848 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) {
849 PrintAndLog("Usage:");
850 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
851 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
852 PrintAndLog(" or hf mf hardnested r [known target key]");
854 PrintAndLog("Options: ");
855 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
856 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
857 PrintAndLog(" r: Read nonces.bin and start attack");
859 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
860 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
861 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
862 PrintAndLog(" sample4: hf mf hardnested r");
864 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
865 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
869 bool know_target_key
= false;
870 bool nonce_file_read
= false;
871 bool nonce_file_write
= false;
876 if (ctmp
== 'R' || ctmp
== 'r') {
877 nonce_file_read
= true;
878 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
879 know_target_key
= true;
881 } else if (ctmp
== 'T' || ctmp
== 't') {
882 tests
= param_get32ex(Cmd
, 1, 100, 10);
884 blockNo
= param_get8(Cmd
, 0);
885 ctmp
= param_getchar(Cmd
, 1);
886 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
887 PrintAndLog("Key type must be A or B");
890 if (ctmp
!= 'A' && ctmp
!= 'a') {
894 if (param_gethex(Cmd
, 2, key
, 12)) {
895 PrintAndLog("Key must include 12 HEX symbols");
899 trgBlockNo
= param_get8(Cmd
, 3);
900 ctmp
= param_getchar(Cmd
, 4);
901 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
902 PrintAndLog("Target key type must be A or B");
905 if (ctmp
!= 'A' && ctmp
!= 'a') {
911 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
912 know_target_key
= true;
916 while ((ctmp
= param_getchar(Cmd
, i
))) {
917 if (ctmp
== 's' || ctmp
== 'S') {
919 } else if (ctmp
== 'w' || ctmp
== 'W') {
920 nonce_file_write
= true;
922 PrintAndLog("Possible options are w and/or s");
929 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 ",
932 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
933 know_target_key
?"":" (not set)",
934 nonce_file_write
?"write":nonce_file_read
?"read":"none",
938 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
942 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
943 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
952 int CmdHF14AMfChk(const char *Cmd
)
955 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
956 PrintAndLog(" * - all sectors");
957 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
958 PrintAndLog("d - write keys to binary file");
959 PrintAndLog("t - write keys to emulator memory\n");
960 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
961 PrintAndLog(" hf mf chk *1 ? t");
962 PrintAndLog(" hf mf chk *1 ? d");
967 char filename
[FILE_PATH_SIZE
]={0};
969 uint8_t *keyBlock
= NULL
, *p
;
970 uint8_t stKeyBlock
= 20;
976 uint8_t SectorsCnt
= 1;
980 int transferToEml
= 0;
981 int createDumpFile
= 0;
983 keyBlock
= calloc(stKeyBlock
, 6);
984 if (keyBlock
== NULL
) return 1;
986 uint64_t defaultKeys
[] =
988 0xffffffffffff, // Default key (first key used by program if no user defined key)
989 0x000000000000, // Blank key
990 0xa0a1a2a3a4a5, // NFCForum MAD key
1002 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1004 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1006 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1009 if (param_getchar(Cmd
, 0)=='*') {
1011 switch(param_getchar(Cmd
+1, 0)) {
1012 case '0': SectorsCnt
= 5; break;
1013 case '1': SectorsCnt
= 16; break;
1014 case '2': SectorsCnt
= 32; break;
1015 case '4': SectorsCnt
= 40; break;
1016 default: SectorsCnt
= 16;
1020 blockNo
= param_get8(Cmd
, 0);
1022 ctmp
= param_getchar(Cmd
, 1);
1034 PrintAndLog("Key type must be A , B or ?");
1039 ctmp
= param_getchar(Cmd
, 2);
1040 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1041 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1043 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1044 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1045 if ( stKeyBlock
- keycnt
< 2) {
1046 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1048 PrintAndLog("Cannot allocate memory for Keys");
1054 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1055 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1056 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1059 // May be a dic file
1060 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1061 PrintAndLog("File name too long");
1066 if ( (f
= fopen( filename
, "r")) ) {
1067 while( fgets(buf
, sizeof(buf
), f
) ){
1068 if (strlen(buf
) < 12 || buf
[11] == '\n')
1071 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1073 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1075 if (!isxdigit(buf
[0])){
1076 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1082 if ( stKeyBlock
- keycnt
< 2) {
1083 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1085 PrintAndLog("Cannot allocate memory for defKeys");
1092 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1093 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1094 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1096 memset(buf
, 0, sizeof(buf
));
1100 PrintAndLog("File: %s: not found or locked.", filename
);
1109 PrintAndLog("No key specified, trying default keys");
1110 for (;keycnt
< defaultKeysSize
; keycnt
++)
1111 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1112 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1113 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1116 // initialize storage for found keys
1117 bool validKey
[2][40];
1118 uint8_t foundKey
[2][40][6];
1119 for (uint16_t t
= 0; t
< 2; t
++) {
1120 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1121 validKey
[t
][sectorNo
] = false;
1122 for (uint16_t i
= 0; i
< 6; i
++) {
1123 foundKey
[t
][sectorNo
][i
] = 0xff;
1128 clock_t time1
= clock();
1130 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
1132 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1133 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
1134 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1135 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1136 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1137 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
1140 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1141 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
1142 validKey
[t
][i
] = true;
1145 PrintAndLog("Command execute timeout");
1148 b
<127?(b
+=4):(b
+=16);
1151 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
);
1154 if (transferToEml
) {
1156 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1157 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1158 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1159 for (uint16_t t
= 0; t
< 2; t
++) {
1160 if (validKey
[t
][sectorNo
]) {
1161 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1164 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1167 PrintAndLog("Found keys have been transferred to the emulator memory");
1170 if (createDumpFile
) {
1171 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1172 if (fkeys
== NULL
) {
1173 PrintAndLog("Could not create file dumpkeys.bin");
1177 for (uint16_t t
= 0; t
< 2; t
++) {
1178 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1181 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1189 int CmdHF14AMf1kSim(const char *Cmd
)
1191 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1192 uint8_t exitAfterNReads
= 0;
1195 uint8_t cmdp
= param_getchar(Cmd
, 0);
1197 if (cmdp
== 'h' || cmdp
== 'H') {
1198 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1199 PrintAndLog(" h this help");
1200 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1201 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1202 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1203 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1205 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1209 if (param_getchar(Cmd
, pnr
) == 'u') {
1210 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1212 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1213 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1214 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1216 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1222 if (param_getchar(Cmd
, pnr
) == 'n') {
1223 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1227 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1228 //Using a flag to signal interactiveness, least significant bit
1229 flags
|= FLAG_INTERACTIVE
;
1233 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1234 //Using a flag to signal interactiveness, least significant bit
1235 flags
|= FLAG_NR_AR_ATTACK
;
1238 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1239 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1240 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1246 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1247 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1248 clearCommandBuffer();
1251 if(flags
& FLAG_INTERACTIVE
)
1257 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1260 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1262 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1264 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1266 memset(data
, 0x00, sizeof(data
));
1267 memset(key
, 0x00, sizeof(key
));
1268 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1270 memcpy(data
, resp
.d
.asBytes
, len
);
1272 uint64_t corr_uid
= 0;
1274 // this IF? what was I thinking of?
1275 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1276 corr_uid
= ((uint64_t)(data
[3] << 24)) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1277 tryMfk32(corr_uid
, data
, key
);
1279 corr_uid
|= (uint64_t)data
[2] << 48;
1280 corr_uid
|= (uint64_t)data
[1] << 40;
1281 corr_uid
|= (uint64_t)data
[0] << 32;
1282 corr_uid
|= (uint64_t)data
[7] << 24;
1283 corr_uid
|= (uint64_t)data
[6] << 16;
1284 corr_uid
|= (uint64_t)data
[5] << 8;
1285 corr_uid
|= (uint64_t)data
[4];
1286 tryMfk64(corr_uid
, data
, key
);
1294 int CmdHF14AMfDbg(const char *Cmd
)
1296 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1298 PrintAndLog("Max debug mode parameter is 4 \n");
1301 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1302 PrintAndLog("Usage: hf mf dbg <debug level>");
1303 PrintAndLog(" 0 - no debug messages");
1304 PrintAndLog(" 1 - error messages");
1305 PrintAndLog(" 2 - plus information messages");
1306 PrintAndLog(" 3 - plus debug messages");
1307 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1308 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1312 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1318 int CmdHF14AMfEGet(const char *Cmd
)
1320 uint8_t blockNo
= 0;
1321 uint8_t data
[16] = {0x00};
1323 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1324 PrintAndLog("Usage: hf mf eget <block number>");
1325 PrintAndLog(" sample: hf mf eget 0 ");
1329 blockNo
= param_get8(Cmd
, 0);
1332 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1333 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1335 PrintAndLog("Command execute timeout");
1341 int CmdHF14AMfEClear(const char *Cmd
)
1343 if (param_getchar(Cmd
, 0) == 'h') {
1344 PrintAndLog("Usage: hf mf eclr");
1345 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1349 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1354 int CmdHF14AMfESet(const char *Cmd
)
1356 uint8_t memBlock
[16];
1357 uint8_t blockNo
= 0;
1359 memset(memBlock
, 0x00, sizeof(memBlock
));
1361 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1362 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1363 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1367 blockNo
= param_get8(Cmd
, 0);
1369 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1370 PrintAndLog("block data must include 32 HEX symbols");
1375 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1376 memcpy(c
.d
.asBytes
, memBlock
, 16);
1381 int CmdHF14AMfELoad(const char *Cmd
)
1384 char filename
[FILE_PATH_SIZE
];
1385 char *fnameptr
= filename
;
1386 char buf
[64] = {0x00};
1387 uint8_t buf8
[64] = {0x00};
1388 int i
, len
, blockNum
, numBlocks
;
1389 int nameParamNo
= 1;
1390 uint8_t blockWidth
= 32;
1391 char ctmp
= param_getchar(Cmd
, 0);
1393 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1394 PrintAndLog("It loads emul dump from the file `filename.eml`");
1395 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1396 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1398 PrintAndLog(" sample: hf mf eload filename");
1399 PrintAndLog(" hf mf eload 4 filename");
1404 case '0' : numBlocks
= 5*4; break;
1406 case '\0': numBlocks
= 16*4; break;
1407 case '2' : numBlocks
= 32*4; break;
1408 case '4' : numBlocks
= 256; break;
1409 case 'U' : // fall through
1410 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1416 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1417 if (numblk2
> 0) numBlocks
= numblk2
;
1419 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1421 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1425 sprintf(fnameptr
, ".eml");
1428 f
= fopen(filename
, "r");
1430 PrintAndLog("File %s not found or locked", filename
);
1436 memset(buf
, 0, sizeof(buf
));
1438 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1440 if (blockNum
>= numBlocks
) break;
1442 PrintAndLog("File reading error.");
1447 if (strlen(buf
) < blockWidth
){
1448 if(strlen(buf
) && feof(f
))
1450 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1455 for (i
= 0; i
< blockWidth
; i
+= 2) {
1456 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1458 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1459 PrintAndLog("Cant set emul block: %3d", blockNum
);
1466 if (blockNum
>= numBlocks
) break;
1471 if ((blockNum
!= numBlocks
)) {
1472 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1475 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1479 int CmdHF14AMfESave(const char *Cmd
)
1482 char filename
[FILE_PATH_SIZE
];
1483 char * fnameptr
= filename
;
1485 int i
, j
, len
, numBlocks
;
1486 int nameParamNo
= 1;
1488 memset(filename
, 0, sizeof(filename
));
1489 memset(buf
, 0, sizeof(buf
));
1491 char ctmp
= param_getchar(Cmd
, 0);
1493 if ( ctmp
== 'h' || ctmp
== 'H') {
1494 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1495 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1496 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1498 PrintAndLog(" sample: hf mf esave ");
1499 PrintAndLog(" hf mf esave 4");
1500 PrintAndLog(" hf mf esave 4 filename");
1505 case '0' : numBlocks
= 5*4; break;
1507 case '\0': numBlocks
= 16*4; break;
1508 case '2' : numBlocks
= 32*4; break;
1509 case '4' : numBlocks
= 256; break;
1516 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1518 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1520 // user supplied filename?
1522 // get filename (UID from memory)
1523 if (mfEmlGetMem(buf
, 0, 1)) {
1524 PrintAndLog("Can\'t get UID from block: %d", 0);
1525 len
= sprintf(fnameptr
, "dump");
1529 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1530 sprintf(fnameptr
, "%02X", buf
[j
]);
1536 // add file extension
1537 sprintf(fnameptr
, ".eml");
1540 f
= fopen(filename
, "w+");
1543 PrintAndLog("Can't open file %s ", filename
);
1548 for (i
= 0; i
< numBlocks
; i
++) {
1549 if (mfEmlGetMem(buf
, i
, 1)) {
1550 PrintAndLog("Cant get block: %d", i
);
1553 for (j
= 0; j
< 16; j
++)
1554 fprintf(f
, "%02X", buf
[j
]);
1559 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1564 int CmdHF14AMfECFill(const char *Cmd
)
1566 uint8_t keyType
= 0;
1567 uint8_t numSectors
= 16;
1569 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1570 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1571 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1573 PrintAndLog("samples: hf mf ecfill A");
1574 PrintAndLog(" hf mf ecfill A 4");
1575 PrintAndLog("Read card and transfer its data to emulator memory.");
1576 PrintAndLog("Keys must be laid in the emulator memory. \n");
1580 char ctmp
= param_getchar(Cmd
, 0);
1581 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1582 PrintAndLog("Key type must be A or B");
1585 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1587 ctmp
= param_getchar(Cmd
, 1);
1589 case '0' : numSectors
= 5; break;
1591 case '\0': numSectors
= 16; break;
1592 case '2' : numSectors
= 32; break;
1593 case '4' : numSectors
= 40; break;
1594 default: numSectors
= 16;
1597 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1598 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1603 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1608 uint64_t keyA
, keyB
;
1610 char cmdp
= param_getchar(Cmd
, 0);
1612 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1613 PrintAndLog("It prints the keys loaded in the emulator memory");
1614 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1615 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1617 PrintAndLog(" sample: hf mf ekeyprn 1");
1622 case '0' : numSectors
= 5; break;
1624 case '\0': numSectors
= 16; break;
1625 case '2' : numSectors
= 32; break;
1626 case '4' : numSectors
= 40; break;
1627 default: numSectors
= 16;
1630 PrintAndLog("|---|----------------|----------------|");
1631 PrintAndLog("|sec|key A |key B |");
1632 PrintAndLog("|---|----------------|----------------|");
1633 for (i
= 0; i
< numSectors
; i
++) {
1634 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1635 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1638 keyA
= bytes_to_num(data
, 6);
1639 keyB
= bytes_to_num(data
+ 10, 6);
1640 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1642 PrintAndLog("|---|----------------|----------------|");
1647 int CmdHF14AMfCSetUID(const char *Cmd
)
1649 uint8_t wipeCard
= 0;
1650 uint8_t uid
[8] = {0x00};
1651 uint8_t oldUid
[8] = {0x00};
1652 uint8_t atqa
[2] = {0x00};
1653 uint8_t sak
[1] = {0x00};
1654 uint8_t atqaPresent
= 1;
1659 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1660 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1661 PrintAndLog("sample: hf mf csetuid 01020304");
1662 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1663 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1664 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1668 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1669 PrintAndLog("UID must include 8 HEX symbols");
1674 ctmp
= param_getchar(Cmd
, argi
);
1675 if (ctmp
== 'w' || ctmp
== 'W') {
1681 if (param_getchar(Cmd
, argi
)) {
1682 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1683 PrintAndLog("ATQA must include 4 HEX symbols");
1687 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1688 PrintAndLog("SAK must include 2 HEX symbols");
1697 ctmp
= param_getchar(Cmd
, argi
);
1698 if (ctmp
== 'w' || ctmp
== 'W') {
1703 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1705 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1707 PrintAndLog("Can't set UID. error=%d", res
);
1711 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1712 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1716 int CmdHF14AMfCSetBlk(const char *Cmd
)
1718 uint8_t block
[16] = {0x00};
1719 uint8_t blockNo
= 0;
1720 uint8_t params
= MAGIC_SINGLE
;
1723 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1724 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1725 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1726 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1727 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1731 blockNo
= param_get8(Cmd
, 0);
1733 if (param_gethex(Cmd
, 1, block
, 32)) {
1734 PrintAndLog("block data must include 32 HEX symbols");
1738 char ctmp
= param_getchar(Cmd
, 2);
1739 if (ctmp
== 'w' || ctmp
== 'W')
1740 params
|= MAGIC_WIPE
;
1742 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
1744 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
1746 PrintAndLog("Can't write block. error=%d", res
);
1752 int CmdHF14AMfCLoad(const char *Cmd
)
1755 char filename
[FILE_PATH_SIZE
];
1756 char * fnameptr
= filename
;
1757 char buf
[64] = {0x00};
1758 uint8_t buf8
[64] = {0x00};
1759 uint8_t fillFromEmulator
= 0;
1760 int i
, len
, blockNum
, flags
=0;
1762 memset(filename
, 0, sizeof(filename
));
1764 char ctmp
= param_getchar(Cmd
, 0);
1766 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1767 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1768 PrintAndLog("or from emulator memory (option `e`)");
1769 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1770 PrintAndLog(" or: hf mf cload e ");
1771 PrintAndLog(" sample: hf mf cload filename");
1775 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1777 if (fillFromEmulator
) {
1778 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1779 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1780 PrintAndLog("Cant get block: %d", blockNum
);
1783 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1784 if (blockNum
== 1) flags
= 0; // just write
1785 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
1787 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1788 PrintAndLog("Cant set magic card block: %d", blockNum
);
1795 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1797 memcpy(filename
, Cmd
, len
);
1800 sprintf(fnameptr
, ".eml");
1803 f
= fopen(filename
, "r");
1805 PrintAndLog("File not found or locked.");
1812 memset(buf
, 0, sizeof(buf
));
1814 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1816 PrintAndLog("File reading error.");
1820 if (strlen(buf
) < 32) {
1821 if(strlen(buf
) && feof(f
))
1823 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1827 for (i
= 0; i
< 32; i
+= 2)
1828 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1830 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1831 if (blockNum
== 1) flags
= 0; // just write
1832 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
1834 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1835 PrintAndLog("Can't set magic card block: %d", blockNum
);
1841 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1846 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1847 PrintAndLog("File content error. There must be 64 blocks");
1850 PrintAndLog("Loaded from file: %s", filename
);
1856 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1858 uint8_t blockNo
= 0;
1860 memset(data
, 0x00, sizeof(data
));
1861 char ctmp
= param_getchar(Cmd
, 0);
1863 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1864 PrintAndLog("Usage: hf mf cgetblk <block number>");
1865 PrintAndLog("sample: hf mf cgetblk 1");
1866 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1870 blockNo
= param_get8(Cmd
, 0);
1872 PrintAndLog("--block number:%2d ", blockNo
);
1874 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
1876 PrintAndLog("Can't read block. error=%d", res
);
1880 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
1884 int CmdHF14AMfCGetSc(const char *Cmd
) {
1886 uint8_t sectorNo
= 0;
1888 memset(data
, 0x00, sizeof(data
));
1889 char ctmp
= param_getchar(Cmd
, 0);
1891 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1892 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1893 PrintAndLog("sample: hf mf cgetsc 0");
1894 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1898 sectorNo
= param_get8(Cmd
, 0);
1899 if (sectorNo
> 15) {
1900 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1904 PrintAndLog("--sector number:%d ", sectorNo
);
1905 PrintAndLog("block | data");
1907 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1908 for (i
= 0; i
< 4; i
++) {
1909 if (i
== 1) flags
= 0;
1910 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1912 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
1914 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1917 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
1922 int CmdHF14AMfCSave(const char *Cmd
) {
1925 char filename
[FILE_PATH_SIZE
];
1926 char * fnameptr
= filename
;
1927 uint8_t fillFromEmulator
= 0;
1929 int i
, j
, len
, flags
;
1931 memset(filename
, 0, sizeof(filename
));
1932 memset(buf
, 0, sizeof(buf
));
1933 char ctmp
= param_getchar(Cmd
, 0);
1935 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1936 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1937 PrintAndLog("or into emulator memory (option `e`)");
1938 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1939 PrintAndLog(" sample: hf mf esave ");
1940 PrintAndLog(" hf mf esave filename");
1941 PrintAndLog(" hf mf esave e \n");
1944 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1946 if (fillFromEmulator
) {
1947 // put into emulator
1948 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1949 for (i
= 0; i
< 16 * 4; i
++) {
1950 if (i
== 1) flags
= 0;
1951 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1953 if (mfCGetBlock(i
, buf
, flags
)) {
1954 PrintAndLog("Cant get block: %d", i
);
1958 if (mfEmlSetMem(buf
, i
, 1)) {
1959 PrintAndLog("Cant set emul block: %d", i
);
1966 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1968 // get filename based on UID
1971 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
1972 PrintAndLog("Cant get block: %d", 0);
1973 len
= sprintf(fnameptr
, "dump");
1976 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1977 sprintf(fnameptr
, "%02x", buf
[j
]);
1980 memcpy(filename
, Cmd
, len
);
1984 // add .eml extension
1985 sprintf(fnameptr
, ".eml");
1988 f
= fopen(filename
, "w+");
1991 PrintAndLog("File not found or locked.");
1996 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1997 for (i
= 0; i
< 16 * 4; i
++) {
1998 if (i
== 1) flags
= 0;
1999 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2001 if (mfCGetBlock(i
, buf
, flags
)) {
2002 PrintAndLog("Cant get block: %d", i
);
2005 for (j
= 0; j
< 16; j
++)
2006 fprintf(f
, "%02x", buf
[j
]);
2011 PrintAndLog("Saved to file: %s", filename
);
2016 int CmdHF14AMfSniff(const char *Cmd
){
2018 bool wantLogToFile
= 0;
2019 bool wantDecrypt
= 0;
2020 //bool wantSaveToEml = 0; TODO
2021 bool wantSaveToEmlFile
= 0;
2032 uint8_t atqa
[2] = {0x00};
2035 uint8_t *buf
= NULL
;
2036 uint16_t bufsize
= 0;
2037 uint8_t *bufPtr
= NULL
;
2039 char ctmp
= param_getchar(Cmd
, 0);
2040 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2041 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
2042 PrintAndLog("You can specify:");
2043 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
2044 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
2045 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2046 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2047 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
2048 PrintAndLog(" sample: hf mf sniff l d e");
2052 for (int i
= 0; i
< 4; i
++) {
2053 ctmp
= param_getchar(Cmd
, i
);
2054 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
2055 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
2056 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2057 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
2060 printf("-------------------------------------------------------------------------\n");
2061 printf("Executing command. \n");
2062 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2063 printf("Press the key on pc keyboard to abort the client.\n");
2064 printf("-------------------------------------------------------------------------\n");
2066 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
2067 clearCommandBuffer();
2075 tmpchar
= getchar();
2077 printf("\naborted via keyboard!\n");
2082 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2083 res
= resp
.arg
[0] & 0xff;
2084 uint16_t traceLen
= resp
.arg
[1];
2089 return 0; // we are done
2092 if (res
== 1) { // there is (more) data to be transferred
2093 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
2094 if (traceLen
> bufsize
) {
2096 if (buf
== NULL
) { // not yet allocated
2097 p
= malloc(traceLen
);
2098 } else { // need more memory
2099 p
= realloc(buf
, traceLen
);
2102 PrintAndLog("Cannot allocate memory for trace");
2110 memset(buf
, 0x00, traceLen
);
2112 if (bufPtr
== NULL
) {
2113 PrintAndLog("Cannot allocate memory for trace");
2117 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2122 if (res
== 2) { // received all data, start displaying
2123 blockLen
= bufPtr
- buf
;
2126 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2127 while (bufPtr
- buf
< blockLen
) {
2128 bufPtr
+= 6; // skip (void) timing information
2129 len
= *((uint16_t *)bufPtr
);
2137 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
2138 memcpy(uid
, bufPtr
+ 2, 7);
2139 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2140 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2142 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2143 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2147 if (wantLogToFile
|| wantDecrypt
) {
2148 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2149 AddLogCurrentDT(logHexFileName
);
2152 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2154 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2156 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2158 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
2162 bufPtr
+= ((len
-1)/8+1); // ignore parity
2173 //needs nt, ar, at, Data to decrypt
2174 int CmdHf14MfDecryptBytes(const char *Cmd
){
2177 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2178 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2179 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2182 param_gethex_ex(Cmd
, 3, data
, &len
);
2185 int limit
= sizeof(data
) / 2;
2190 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2193 static command_t CommandTable
[] = {
2194 {"help", CmdHelp
, 1, "This help"},
2195 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2196 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2197 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2198 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2199 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2200 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2201 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2202 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2203 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2204 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2205 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2206 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2207 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2208 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2209 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2210 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2211 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2212 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2213 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2214 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2215 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2216 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2217 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2218 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2219 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2220 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2221 {NULL
, NULL
, 0, NULL
}
2224 int CmdHFMF(const char *Cmd
) {
2226 clearCommandBuffer();
2227 //WaitForResponseTimeout(CMD_ACK,NULL,100);
2228 CmdsParse(CommandTable
, Cmd
);
2232 int CmdHelp(const char *Cmd
) {
2233 CmdsHelp(CommandTable
);