]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
12 #include "cmdhfmfhard.h"
13 #include "nonce2key/nonce2key.h"
15 static int CmdHelp(const char *Cmd
);
17 int CmdHF14AMifare(const char *Cmd
)
20 uint32_t nt
= 0, nr
= 0;
21 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
24 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
27 printf("-------------------------------------------------------------------------\n");
28 printf("Executing command. Expected execution time: 25sec on average :-)\n");
29 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
30 printf("-------------------------------------------------------------------------\n");
32 time_t time1
= clock();
39 while (ukbhit()) getchar();
47 printf("\naborted via keyboard!\n");
52 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
54 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
55 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
56 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
57 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
58 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
61 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
62 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
63 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
64 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
65 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
75 if (isOK
!= 1) return 1;
77 // execute original function from util nonce2key
78 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
80 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
81 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
86 printf("------------------------------------------------------------------\n");
87 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
90 PrintAndLog("Time in darkside: %1.0f seconds", (float)(clock() - time1
)/CLOCKS_PER_SEC
);
95 int CmdHF14AMfWrBl(const char *Cmd
)
99 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
100 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
105 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
106 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
110 blockNo
= param_get8(Cmd
, 0);
111 cmdp
= param_getchar(Cmd
, 1);
113 PrintAndLog("Key type must be A or B");
116 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
117 if (param_gethex(Cmd
, 2, key
, 12)) {
118 PrintAndLog("Key must include 12 HEX symbols");
121 if (param_gethex(Cmd
, 3, bldata
, 32)) {
122 PrintAndLog("Block data must include 32 HEX symbols");
125 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
126 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
128 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
129 memcpy(c
.d
.asBytes
, key
, 6);
130 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
131 clearCommandBuffer();
135 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
136 uint8_t isOK
= resp
.arg
[0] & 0xff;
137 PrintAndLog("isOk:%02x", isOK
);
139 PrintAndLog("Command execute timeout");
145 int CmdHF14AMfRdBl(const char *Cmd
)
149 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
155 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
156 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
160 blockNo
= param_get8(Cmd
, 0);
161 cmdp
= param_getchar(Cmd
, 1);
163 PrintAndLog("Key type must be A or B");
166 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
167 if (param_gethex(Cmd
, 2, key
, 12)) {
168 PrintAndLog("Key must include 12 HEX symbols");
171 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
173 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
174 memcpy(c
.d
.asBytes
, key
, 6);
175 clearCommandBuffer();
179 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
180 uint8_t isOK
= resp
.arg
[0] & 0xff;
181 uint8_t *data
= resp
.d
.asBytes
;
184 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
186 PrintAndLog("isOk:%02x", isOK
);
188 PrintAndLog("Command execute timeout");
194 int CmdHF14AMfRdSc(const char *Cmd
)
197 uint8_t sectorNo
= 0;
199 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
201 uint8_t *data
= NULL
;
205 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
206 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
210 sectorNo
= param_get8(Cmd
, 0);
212 PrintAndLog("Sector number must be less than 40");
215 cmdp
= param_getchar(Cmd
, 1);
216 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
217 PrintAndLog("Key type must be A or B");
220 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
221 if (param_gethex(Cmd
, 2, key
, 12)) {
222 PrintAndLog("Key must include 12 HEX symbols");
225 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
227 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
228 memcpy(c
.d
.asBytes
, key
, 6);
229 clearCommandBuffer();
234 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
235 isOK
= resp
.arg
[0] & 0xff;
236 data
= resp
.d
.asBytes
;
238 PrintAndLog("isOk:%02x", isOK
);
240 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
241 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
243 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
246 PrintAndLog("Command execute timeout");
252 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
257 return 32 * 4 + (sectorNo
- 32) * 16;
261 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
270 int CmdHF14AMfDump(const char *Cmd
)
272 uint8_t sectorNo
, blockNo
;
276 uint8_t rights
[40][4];
277 uint8_t carddata
[256][16];
278 uint8_t numSectors
= 16;
285 char cmdp
= param_getchar(Cmd
, 0);
287 case '0' : numSectors
= 5; break;
289 case '\0': numSectors
= 16; break;
290 case '2' : numSectors
= 32; break;
291 case '4' : numSectors
= 40; break;
292 default: numSectors
= 16;
295 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
296 PrintAndLog("Usage: hf mf dump [card memory]");
297 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
299 PrintAndLog("Samples: hf mf dump");
300 PrintAndLog(" hf mf dump 4");
304 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
305 PrintAndLog("Could not find file dumpkeys.bin");
309 // Read keys A from file
310 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
311 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
312 PrintAndLog("File reading error.");
318 // Read keys B from file
319 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
320 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
321 PrintAndLog("File reading error.");
329 PrintAndLog("|-----------------------------------------|");
330 PrintAndLog("|------ Reading sector access bits...-----|");
331 PrintAndLog("|-----------------------------------------|");
333 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
334 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
335 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
336 clearCommandBuffer();
339 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
340 uint8_t isOK
= resp
.arg
[0] & 0xff;
341 uint8_t *data
= resp
.d
.asBytes
;
343 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
344 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
345 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
346 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
348 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
349 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
350 rights
[sectorNo
][3] = 0x01;
353 PrintAndLog("Command execute timeout when trying to read 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;
359 PrintAndLog("|-----------------------------------------|");
360 PrintAndLog("|----- Dumping all blocks to file... -----|");
361 PrintAndLog("|-----------------------------------------|");
364 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
365 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
366 bool received
= false;
368 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
369 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
370 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
371 clearCommandBuffer();
373 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
374 } else { // data block. Check if it can be read with key A or key B
375 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
376 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
377 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
378 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
380 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
381 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
383 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
384 } else { // key A would work
385 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
386 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
387 clearCommandBuffer();
389 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
394 isOK
= resp
.arg
[0] & 0xff;
395 uint8_t *data
= resp
.d
.asBytes
;
396 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
397 data
[0] = (keyA
[sectorNo
][0]);
398 data
[1] = (keyA
[sectorNo
][1]);
399 data
[2] = (keyA
[sectorNo
][2]);
400 data
[3] = (keyA
[sectorNo
][3]);
401 data
[4] = (keyA
[sectorNo
][4]);
402 data
[5] = (keyA
[sectorNo
][5]);
403 data
[10] = (keyB
[sectorNo
][0]);
404 data
[11] = (keyB
[sectorNo
][1]);
405 data
[12] = (keyB
[sectorNo
][2]);
406 data
[13] = (keyB
[sectorNo
][3]);
407 data
[14] = (keyB
[sectorNo
][4]);
408 data
[15] = (keyB
[sectorNo
][5]);
411 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
412 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
414 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
420 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
427 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
428 PrintAndLog("Could not create file name dumpdata.bin");
431 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
432 fwrite(carddata
, 1, 16*numblocks
, fout
);
434 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
440 int CmdHF14AMfRestore(const char *Cmd
)
442 uint8_t sectorNo
,blockNo
;
444 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
445 uint8_t bldata
[16] = {0x00};
453 char cmdp
= param_getchar(Cmd
, 0);
455 case '0' : numSectors
= 5; break;
457 case '\0': numSectors
= 16; break;
458 case '2' : numSectors
= 32; break;
459 case '4' : numSectors
= 40; break;
460 default: numSectors
= 16;
463 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
464 PrintAndLog("Usage: hf mf restore [card memory]");
465 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
467 PrintAndLog("Samples: hf mf restore");
468 PrintAndLog(" hf mf restore 4");
472 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
473 PrintAndLog("Could not find file dumpkeys.bin");
477 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
478 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
479 PrintAndLog("File reading error (dumpkeys.bin).");
485 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
486 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
487 PrintAndLog("File reading error (dumpkeys.bin).");
495 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
496 PrintAndLog("Could not find file dumpdata.bin");
499 PrintAndLog("Restoring dumpdata.bin to card");
501 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
502 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
503 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
504 memcpy(c
.d
.asBytes
, key
, 6);
506 if (fread(bldata
, 1, 16, fdump
) == 0) {
507 PrintAndLog("File reading error (dumpdata.bin).");
512 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
513 bldata
[0] = (keyA
[sectorNo
][0]);
514 bldata
[1] = (keyA
[sectorNo
][1]);
515 bldata
[2] = (keyA
[sectorNo
][2]);
516 bldata
[3] = (keyA
[sectorNo
][3]);
517 bldata
[4] = (keyA
[sectorNo
][4]);
518 bldata
[5] = (keyA
[sectorNo
][5]);
519 bldata
[10] = (keyB
[sectorNo
][0]);
520 bldata
[11] = (keyB
[sectorNo
][1]);
521 bldata
[12] = (keyB
[sectorNo
][2]);
522 bldata
[13] = (keyB
[sectorNo
][3]);
523 bldata
[14] = (keyB
[sectorNo
][4]);
524 bldata
[15] = (keyB
[sectorNo
][5]);
527 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
529 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
530 clearCommandBuffer();
534 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
535 uint8_t isOK
= resp
.arg
[0] & 0xff;
536 PrintAndLog("isOk:%02x", isOK
);
538 PrintAndLog("Command execute timeout");
547 int CmdHF14AMfNested(const char *Cmd
)
549 int i
, j
, res
, iterations
;
550 sector
*e_sector
= NULL
;
553 uint8_t trgBlockNo
= 0;
554 uint8_t trgKeyType
= 0;
555 uint8_t SectorsCnt
= 0;
556 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
557 uint8_t keyBlock
[14*6];
559 bool transferToEml
= false;
561 bool createDumpFile
= false;
563 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
564 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
569 PrintAndLog("Usage:");
570 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
571 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
572 PrintAndLog(" <target block number> <target key A/B> [t]");
573 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
574 PrintAndLog("t - transfer keys into emulator memory");
575 PrintAndLog("d - write keys to binary file");
577 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
578 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
579 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
580 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
584 cmdp
= param_getchar(Cmd
, 0);
585 blockNo
= param_get8(Cmd
, 1);
586 ctmp
= param_getchar(Cmd
, 2);
588 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
589 PrintAndLog("Key type must be A or B");
593 if (ctmp
!= 'A' && ctmp
!= 'a')
596 if (param_gethex(Cmd
, 3, key
, 12)) {
597 PrintAndLog("Key must include 12 HEX symbols");
601 if (cmdp
== 'o' || cmdp
== 'O') {
603 trgBlockNo
= param_get8(Cmd
, 4);
604 ctmp
= param_getchar(Cmd
, 5);
605 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
606 PrintAndLog("Target key type must be A or B");
609 if (ctmp
!= 'A' && ctmp
!= 'a')
614 case '0': SectorsCnt
= 05; break;
615 case '1': SectorsCnt
= 16; break;
616 case '2': SectorsCnt
= 32; break;
617 case '4': SectorsCnt
= 40; break;
618 default: SectorsCnt
= 16;
622 ctmp
= param_getchar(Cmd
, 4);
623 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
624 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
626 ctmp
= param_getchar(Cmd
, 6);
627 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
628 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
631 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
632 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
635 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
636 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
637 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
638 default : PrintAndLog("Unknown Error.\n");
642 key64
= bytes_to_num(keyBlock
, 6);
644 PrintAndLog("Found valid key:%012"llx
, key64
);
646 // transfer key to the emulator
648 uint8_t sectortrailer
;
649 if (trgBlockNo
< 32*4) { // 4 block sector
650 sectortrailer
= (trgBlockNo
& 0x03) + 3;
651 } else { // 16 block sector
652 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
654 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
657 num_to_bytes(key64
, 6, keyBlock
);
659 num_to_bytes(key64
, 6, &keyBlock
[10]);
660 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
663 PrintAndLog("No valid key found");
666 else { // ------------------------------------ multiple sectors working
667 clock_t time1
= clock();
669 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
670 if (e_sector
== NULL
) return 1;
672 //test current key and additional standard keys first
673 memcpy(keyBlock
, key
, 6);
674 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
675 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
676 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
677 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
678 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
679 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
680 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
681 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
682 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
683 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
684 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
685 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
686 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
688 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
689 for (i
= 0; i
< SectorsCnt
; i
++) {
690 for (j
= 0; j
< 2; j
++) {
691 if (e_sector
[i
].foundKey
[j
]) continue;
693 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
696 e_sector
[i
].Key
[j
] = key64
;
697 e_sector
[i
].foundKey
[j
] = 1;
704 PrintAndLog("nested...");
705 bool calibrate
= true;
706 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
707 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
708 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
709 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
710 PrintAndLog("-----------------------------------------------");
711 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
714 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
715 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
716 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
717 default : PrintAndLog("Unknown Error.\n");
727 key64
= bytes_to_num(keyBlock
, 6);
729 PrintAndLog("Found valid key:%012"llx
, key64
);
730 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
731 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
737 PrintAndLog("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1
)/CLOCKS_PER_SEC
, ((float)clock() - time1
)/iterations
/CLOCKS_PER_SEC
);
739 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
741 PrintAndLog("|---|----------------|---|----------------|---|");
742 PrintAndLog("|sec|key A |res|key B |res|");
743 PrintAndLog("|---|----------------|---|----------------|---|");
744 for (i
= 0; i
< SectorsCnt
; i
++) {
745 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
746 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
748 PrintAndLog("|---|----------------|---|----------------|---|");
750 // transfer them to the emulator
752 for (i
= 0; i
< SectorsCnt
; i
++) {
753 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
754 if (e_sector
[i
].foundKey
[0])
755 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
756 if (e_sector
[i
].foundKey
[1])
757 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
758 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
763 if (createDumpFile
) {
764 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
765 PrintAndLog("Could not create file dumpkeys.bin");
769 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
770 for(i
=0; i
<SectorsCnt
; i
++) {
771 if (e_sector
[i
].foundKey
[0]){
772 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
773 fwrite ( tempkey
, 1, 6, fkeys
);
776 fwrite ( &standart
, 1, 6, fkeys
);
779 for(i
=0; i
<SectorsCnt
; i
++) {
780 if (e_sector
[i
].foundKey
[1]){
781 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
782 fwrite ( tempkey
, 1, 6, fkeys
);
785 fwrite ( &standart
, 1, 6, fkeys
);
796 int CmdHF14AMfNestedHard(const char *Cmd
)
800 uint8_t trgBlockNo
= 0;
801 uint8_t trgKeyType
= 0;
802 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
803 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
806 ctmp
= param_getchar(Cmd
, 0);
808 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) {
809 PrintAndLog("Usage:");
810 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
811 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
812 PrintAndLog(" or hf mf hardnested r [known target key]");
814 PrintAndLog("Options: ");
815 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
816 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
817 PrintAndLog(" r: Read nonces.bin and start attack");
819 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
820 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
821 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
822 PrintAndLog(" sample4: hf mf hardnested r");
824 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
825 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
829 bool know_target_key
= false;
830 bool nonce_file_read
= false;
831 bool nonce_file_write
= false;
836 if (ctmp
== 'R' || ctmp
== 'r') {
837 nonce_file_read
= true;
838 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
839 know_target_key
= true;
841 } else if (ctmp
== 'T' || ctmp
== 't') {
842 tests
= param_get32ex(Cmd
, 1, 100, 10);
844 blockNo
= param_get8(Cmd
, 0);
845 ctmp
= param_getchar(Cmd
, 1);
846 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
847 PrintAndLog("Key type must be A or B");
850 if (ctmp
!= 'A' && ctmp
!= 'a') {
854 if (param_gethex(Cmd
, 2, key
, 12)) {
855 PrintAndLog("Key must include 12 HEX symbols");
859 trgBlockNo
= param_get8(Cmd
, 3);
860 ctmp
= param_getchar(Cmd
, 4);
861 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
862 PrintAndLog("Target key type must be A or B");
865 if (ctmp
!= 'A' && ctmp
!= 'a') {
871 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
872 know_target_key
= true;
876 while ((ctmp
= param_getchar(Cmd
, i
))) {
877 if (ctmp
== 's' || ctmp
== 'S') {
879 } else if (ctmp
== 'w' || ctmp
== 'W') {
880 nonce_file_write
= true;
882 PrintAndLog("Possible options are w and/or s");
889 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 ",
892 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
893 know_target_key
?"":" (not set)",
894 nonce_file_write
?"write":nonce_file_read
?"read":"none",
898 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
902 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
903 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
912 int CmdHF14AMfChk(const char *Cmd
)
915 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
916 PrintAndLog(" * - all sectors");
917 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
918 PrintAndLog("d - write keys to binary file");
919 PrintAndLog("t - write keys to emulator memory\n");
920 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
921 PrintAndLog(" hf mf chk *1 ? t");
922 PrintAndLog(" hf mf chk *1 ? d");
927 char filename
[FILE_PATH_SIZE
]={0};
929 uint8_t *keyBlock
= NULL
, *p
;
930 uint8_t stKeyBlock
= 20;
936 uint8_t SectorsCnt
= 1;
940 int transferToEml
= 0;
941 int createDumpFile
= 0;
943 keyBlock
= calloc(stKeyBlock
, 6);
944 if (keyBlock
== NULL
) return 1;
946 uint64_t defaultKeys
[] =
948 0xffffffffffff, // Default key (first key used by program if no user defined key)
949 0x000000000000, // Blank key
950 0xa0a1a2a3a4a5, // NFCForum MAD key
962 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
964 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
966 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
969 if (param_getchar(Cmd
, 0)=='*') {
971 switch(param_getchar(Cmd
+1, 0)) {
972 case '0': SectorsCnt
= 5; break;
973 case '1': SectorsCnt
= 16; break;
974 case '2': SectorsCnt
= 32; break;
975 case '4': SectorsCnt
= 40; break;
976 default: SectorsCnt
= 16;
980 blockNo
= param_get8(Cmd
, 0);
982 ctmp
= param_getchar(Cmd
, 1);
994 PrintAndLog("Key type must be A , B or ?");
999 ctmp
= param_getchar(Cmd
, 2);
1000 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1001 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1003 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1004 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1005 if ( stKeyBlock
- keycnt
< 2) {
1006 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1008 PrintAndLog("Cannot allocate memory for Keys");
1014 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1015 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1016 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1019 // May be a dic file
1020 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1021 PrintAndLog("File name too long");
1026 if ( (f
= fopen( filename
, "r")) ) {
1027 while( fgets(buf
, sizeof(buf
), f
) ){
1028 if (strlen(buf
) < 12 || buf
[11] == '\n')
1031 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1033 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1035 if (!isxdigit(buf
[0])){
1036 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1042 if ( stKeyBlock
- keycnt
< 2) {
1043 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1045 PrintAndLog("Cannot allocate memory for defKeys");
1052 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1053 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1054 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1056 memset(buf
, 0, sizeof(buf
));
1060 PrintAndLog("File: %s: not found or locked.", filename
);
1069 PrintAndLog("No key specified, trying default keys");
1070 for (;keycnt
< defaultKeysSize
; keycnt
++)
1071 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1072 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1073 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1076 // initialize storage for found keys
1077 bool validKey
[2][40];
1078 uint8_t foundKey
[2][40][6];
1079 for (uint16_t t
= 0; t
< 2; t
++) {
1080 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1081 validKey
[t
][sectorNo
] = false;
1082 for (uint16_t i
= 0; i
< 6; i
++) {
1083 foundKey
[t
][sectorNo
][i
] = 0xff;
1088 time_t time1
= clock();
1090 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
1092 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1093 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
1094 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1095 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1096 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1097 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
1100 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1101 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
1102 validKey
[t
][i
] = true;
1105 PrintAndLog("Command execute timeout");
1108 b
<127?(b
+=4):(b
+=16);
1111 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
);
1114 if (transferToEml
) {
1116 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1117 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1118 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1119 for (uint16_t t
= 0; t
< 2; t
++) {
1120 if (validKey
[t
][sectorNo
]) {
1121 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1124 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1127 PrintAndLog("Found keys have been transferred to the emulator memory");
1130 if (createDumpFile
) {
1131 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1132 if (fkeys
== NULL
) {
1133 PrintAndLog("Could not create file dumpkeys.bin");
1137 for (uint16_t t
= 0; t
< 2; t
++) {
1138 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1141 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1149 int CmdHF14AMf1kSim(const char *Cmd
)
1151 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1152 uint8_t exitAfterNReads
= 0;
1155 uint8_t cmdp
= param_getchar(Cmd
, 0);
1157 if (cmdp
== 'h' || cmdp
== 'H') {
1158 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1159 PrintAndLog(" h this help");
1160 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1161 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1162 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1163 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1165 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1169 if (param_getchar(Cmd
, pnr
) == 'u') {
1170 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1172 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1173 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1174 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1176 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1181 if (param_getchar(Cmd
, pnr
) == 'n') {
1182 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1185 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1186 //Using a flag to signal interactiveness, least significant bit
1187 flags
|= FLAG_INTERACTIVE
;
1191 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1192 //Using a flag to signal interactiveness, least significant bit
1193 flags
|= FLAG_NR_AR_ATTACK
;
1195 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1196 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1197 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1198 , exitAfterNReads
, flags
,flags
);
1201 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1202 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1203 clearCommandBuffer();
1206 if(flags
& FLAG_INTERACTIVE
)
1212 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1213 //while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1214 //We're waiting only 1.5 s at a time, otherwise we get the
1215 // annoying message about "Waiting for a response... "
1218 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) continue;
1220 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1221 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1223 memset(data
, 0x00, sizeof(data
));
1224 memset(key
, 0x00, sizeof(key
));
1225 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1227 memcpy(data
, resp
.d
.asBytes
, len
);
1229 uint64_t corr_uid
= 0;
1230 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1231 corr_uid
= (data
[3] << 24) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1232 tryMfk32(corr_uid
, data
, key
);
1234 corr_uid
|= (uint64_t)data
[2] << 48;
1235 corr_uid
|= (uint64_t)data
[1] << 40;
1236 corr_uid
|= (uint64_t)data
[0] << 32;
1237 corr_uid
|= (uint64_t)data
[7] << 24;
1238 corr_uid
|= (uint64_t)data
[6] << 16;
1239 corr_uid
|= (uint64_t)data
[5] << 8;
1240 corr_uid
|= (uint64_t)data
[4];
1241 tryMfk64(corr_uid
, data
, key
);
1250 int CmdHF14AMfDbg(const char *Cmd
)
1252 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1254 PrintAndLog("Max debug mode parameter is 4 \n");
1257 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1258 PrintAndLog("Usage: hf mf dbg <debug level>");
1259 PrintAndLog(" 0 - no debug messages");
1260 PrintAndLog(" 1 - error messages");
1261 PrintAndLog(" 2 - plus information messages");
1262 PrintAndLog(" 3 - plus debug messages");
1263 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1264 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1268 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1274 int CmdHF14AMfEGet(const char *Cmd
)
1276 uint8_t blockNo
= 0;
1277 uint8_t data
[16] = {0x00};
1279 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1280 PrintAndLog("Usage: hf mf eget <block number>");
1281 PrintAndLog(" sample: hf mf eget 0 ");
1285 blockNo
= param_get8(Cmd
, 0);
1288 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1289 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1291 PrintAndLog("Command execute timeout");
1297 int CmdHF14AMfEClear(const char *Cmd
)
1299 if (param_getchar(Cmd
, 0) == 'h') {
1300 PrintAndLog("Usage: hf mf eclr");
1301 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1305 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1310 int CmdHF14AMfESet(const char *Cmd
)
1312 uint8_t memBlock
[16];
1313 uint8_t blockNo
= 0;
1315 memset(memBlock
, 0x00, sizeof(memBlock
));
1317 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1318 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1319 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1323 blockNo
= param_get8(Cmd
, 0);
1325 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1326 PrintAndLog("block data must include 32 HEX symbols");
1331 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1332 memcpy(c
.d
.asBytes
, memBlock
, 16);
1337 int CmdHF14AMfELoad(const char *Cmd
)
1340 char filename
[FILE_PATH_SIZE
];
1341 char *fnameptr
= filename
;
1342 char buf
[64] = {0x00};
1343 uint8_t buf8
[64] = {0x00};
1344 int i
, len
, blockNum
, numBlocks
;
1345 int nameParamNo
= 1;
1346 uint8_t blockWidth
= 32;
1347 char ctmp
= param_getchar(Cmd
, 0);
1349 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1350 PrintAndLog("It loads emul dump from the file `filename.eml`");
1351 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1352 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1354 PrintAndLog(" sample: hf mf eload filename");
1355 PrintAndLog(" hf mf eload 4 filename");
1360 case '0' : numBlocks
= 5*4; break;
1362 case '\0': numBlocks
= 16*4; break;
1363 case '2' : numBlocks
= 32*4; break;
1364 case '4' : numBlocks
= 256; break;
1365 case 'U' : // fall through
1366 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1372 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1373 if (numblk2
> 0) numBlocks
= numblk2
;
1375 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1377 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1381 sprintf(fnameptr
, ".eml");
1384 f
= fopen(filename
, "r");
1386 PrintAndLog("File %s not found or locked", filename
);
1392 memset(buf
, 0, sizeof(buf
));
1394 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1396 if (blockNum
>= numBlocks
) break;
1398 PrintAndLog("File reading error.");
1403 if (strlen(buf
) < blockWidth
){
1404 if(strlen(buf
) && feof(f
))
1406 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1411 for (i
= 0; i
< blockWidth
; i
+= 2) {
1412 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1414 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1415 PrintAndLog("Cant set emul block: %3d", blockNum
);
1422 if (blockNum
>= numBlocks
) break;
1427 if ((blockNum
!= numBlocks
)) {
1428 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1431 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1435 int CmdHF14AMfESave(const char *Cmd
)
1438 char filename
[FILE_PATH_SIZE
];
1439 char * fnameptr
= filename
;
1441 int i
, j
, len
, numBlocks
;
1442 int nameParamNo
= 1;
1444 memset(filename
, 0, sizeof(filename
));
1445 memset(buf
, 0, sizeof(buf
));
1447 char ctmp
= param_getchar(Cmd
, 0);
1449 if ( ctmp
== 'h' || ctmp
== 'H') {
1450 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1451 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1452 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1454 PrintAndLog(" sample: hf mf esave ");
1455 PrintAndLog(" hf mf esave 4");
1456 PrintAndLog(" hf mf esave 4 filename");
1461 case '0' : numBlocks
= 5*4; break;
1463 case '\0': numBlocks
= 16*4; break;
1464 case '2' : numBlocks
= 32*4; break;
1465 case '4' : numBlocks
= 256; break;
1472 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1474 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1476 // user supplied filename?
1478 // get filename (UID from memory)
1479 if (mfEmlGetMem(buf
, 0, 1)) {
1480 PrintAndLog("Can\'t get UID from block: %d", 0);
1481 len
= sprintf(fnameptr
, "dump");
1485 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1486 sprintf(fnameptr
, "%02X", buf
[j
]);
1492 // add file extension
1493 sprintf(fnameptr
, ".eml");
1496 f
= fopen(filename
, "w+");
1499 PrintAndLog("Can't open file %s ", filename
);
1504 for (i
= 0; i
< numBlocks
; i
++) {
1505 if (mfEmlGetMem(buf
, i
, 1)) {
1506 PrintAndLog("Cant get block: %d", i
);
1509 for (j
= 0; j
< 16; j
++)
1510 fprintf(f
, "%02X", buf
[j
]);
1515 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1520 int CmdHF14AMfECFill(const char *Cmd
)
1522 uint8_t keyType
= 0;
1523 uint8_t numSectors
= 16;
1525 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1526 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1527 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1529 PrintAndLog("samples: hf mf ecfill A");
1530 PrintAndLog(" hf mf ecfill A 4");
1531 PrintAndLog("Read card and transfer its data to emulator memory.");
1532 PrintAndLog("Keys must be laid in the emulator memory. \n");
1536 char ctmp
= param_getchar(Cmd
, 0);
1537 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1538 PrintAndLog("Key type must be A or B");
1541 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1543 ctmp
= param_getchar(Cmd
, 1);
1545 case '0' : numSectors
= 5; break;
1547 case '\0': numSectors
= 16; break;
1548 case '2' : numSectors
= 32; break;
1549 case '4' : numSectors
= 40; break;
1550 default: numSectors
= 16;
1553 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1554 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1559 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1564 uint64_t keyA
, keyB
;
1566 char cmdp
= param_getchar(Cmd
, 0);
1568 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1569 PrintAndLog("It prints the keys loaded in the emulator memory");
1570 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1571 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1573 PrintAndLog(" sample: hf mf ekeyprn 1");
1578 case '0' : numSectors
= 5; break;
1580 case '\0': numSectors
= 16; break;
1581 case '2' : numSectors
= 32; break;
1582 case '4' : numSectors
= 40; break;
1583 default: numSectors
= 16;
1586 PrintAndLog("|---|----------------|----------------|");
1587 PrintAndLog("|sec|key A |key B |");
1588 PrintAndLog("|---|----------------|----------------|");
1589 for (i
= 0; i
< numSectors
; i
++) {
1590 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1591 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1594 keyA
= bytes_to_num(data
, 6);
1595 keyB
= bytes_to_num(data
+ 10, 6);
1596 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1598 PrintAndLog("|---|----------------|----------------|");
1603 int CmdHF14AMfCSetUID(const char *Cmd
)
1605 uint8_t wipeCard
= 0;
1606 uint8_t uid
[8] = {0x00};
1607 uint8_t oldUid
[8] = {0x00};
1608 uint8_t atqa
[2] = {0x00};
1609 uint8_t sak
[1] = {0x00};
1610 uint8_t atqaPresent
= 1;
1615 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1616 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1617 PrintAndLog("sample: hf mf csetuid 01020304");
1618 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1619 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1620 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1624 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1625 PrintAndLog("UID must include 8 HEX symbols");
1630 ctmp
= param_getchar(Cmd
, argi
);
1631 if (ctmp
== 'w' || ctmp
== 'W') {
1637 if (param_getchar(Cmd
, argi
)) {
1638 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1639 PrintAndLog("ATQA must include 4 HEX symbols");
1643 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1644 PrintAndLog("SAK must include 2 HEX symbols");
1653 ctmp
= param_getchar(Cmd
, argi
);
1654 if (ctmp
== 'w' || ctmp
== 'W') {
1659 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1661 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1663 PrintAndLog("Can't set UID. error=%d", res
);
1667 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1668 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1672 int CmdHF14AMfCSetBlk(const char *Cmd
)
1674 uint8_t block
[16] = {0x00};
1675 uint8_t blockNo
= 0;
1676 uint8_t params
= MAGIC_SINGLE
;
1679 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1680 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1681 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1682 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1683 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1687 blockNo
= param_get8(Cmd
, 0);
1689 if (param_gethex(Cmd
, 1, block
, 32)) {
1690 PrintAndLog("block data must include 32 HEX symbols");
1694 char ctmp
= param_getchar(Cmd
, 2);
1695 if (ctmp
== 'w' || ctmp
== 'W')
1696 params
|= MAGIC_WIPE
;
1698 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
1700 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
1702 PrintAndLog("Can't write block. error=%d", res
);
1708 int CmdHF14AMfCLoad(const char *Cmd
)
1711 char filename
[FILE_PATH_SIZE
];
1712 char * fnameptr
= filename
;
1713 char buf
[64] = {0x00};
1714 uint8_t buf8
[64] = {0x00};
1715 uint8_t fillFromEmulator
= 0;
1716 int i
, len
, blockNum
, flags
=0;
1718 memset(filename
, 0, sizeof(filename
));
1720 char ctmp
= param_getchar(Cmd
, 0);
1722 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1723 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1724 PrintAndLog("or from emulator memory (option `e`)");
1725 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1726 PrintAndLog(" or: hf mf cload e ");
1727 PrintAndLog(" sample: hf mf cload filename");
1731 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1733 if (fillFromEmulator
) {
1734 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1735 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1736 PrintAndLog("Cant get block: %d", blockNum
);
1739 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1740 if (blockNum
== 1) flags
= 0; // just write
1741 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
1743 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1744 PrintAndLog("Cant set magic card block: %d", blockNum
);
1751 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1753 memcpy(filename
, Cmd
, len
);
1756 sprintf(fnameptr
, ".eml");
1759 f
= fopen(filename
, "r");
1761 PrintAndLog("File not found or locked.");
1768 memset(buf
, 0, sizeof(buf
));
1770 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1772 PrintAndLog("File reading error.");
1776 if (strlen(buf
) < 32) {
1777 if(strlen(buf
) && feof(f
))
1779 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1783 for (i
= 0; i
< 32; i
+= 2)
1784 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1786 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1787 if (blockNum
== 1) flags
= 0; // just write
1788 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
1790 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1791 PrintAndLog("Can't set magic card block: %d", blockNum
);
1796 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1801 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1802 PrintAndLog("File content error. There must be 64 blocks");
1805 PrintAndLog("Loaded from file: %s", filename
);
1811 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1813 uint8_t blockNo
= 0;
1815 memset(data
, 0x00, sizeof(data
));
1816 char ctmp
= param_getchar(Cmd
, 0);
1818 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1819 PrintAndLog("Usage: hf mf cgetblk <block number>");
1820 PrintAndLog("sample: hf mf cgetblk 1");
1821 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1825 blockNo
= param_get8(Cmd
, 0);
1827 PrintAndLog("--block number:%2d ", blockNo
);
1829 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
1831 PrintAndLog("Can't read block. error=%d", res
);
1835 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
1839 int CmdHF14AMfCGetSc(const char *Cmd
) {
1841 uint8_t sectorNo
= 0;
1843 memset(data
, 0x00, sizeof(data
));
1844 char ctmp
= param_getchar(Cmd
, 0);
1846 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1847 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1848 PrintAndLog("sample: hf mf cgetsc 0");
1849 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1853 sectorNo
= param_get8(Cmd
, 0);
1854 if (sectorNo
> 15) {
1855 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1859 PrintAndLog("--sector number:%d ", sectorNo
);
1860 PrintAndLog("block | data");
1862 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1863 for (i
= 0; i
< 4; i
++) {
1864 if (i
== 1) flags
= 0;
1865 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1867 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
1869 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1872 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
1877 int CmdHF14AMfCSave(const char *Cmd
) {
1880 char filename
[FILE_PATH_SIZE
];
1881 char * fnameptr
= filename
;
1882 uint8_t fillFromEmulator
= 0;
1884 int i
, j
, len
, flags
;
1886 memset(filename
, 0, sizeof(filename
));
1887 memset(buf
, 0, sizeof(buf
));
1888 char ctmp
= param_getchar(Cmd
, 0);
1890 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1891 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1892 PrintAndLog("or into emulator memory (option `e`)");
1893 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1894 PrintAndLog(" sample: hf mf esave ");
1895 PrintAndLog(" hf mf esave filename");
1896 PrintAndLog(" hf mf esave e \n");
1899 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1901 if (fillFromEmulator
) {
1902 // put into emulator
1903 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1904 for (i
= 0; i
< 16 * 4; i
++) {
1905 if (i
== 1) flags
= 0;
1906 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1908 if (mfCGetBlock(i
, buf
, flags
)) {
1909 PrintAndLog("Cant get block: %d", i
);
1913 if (mfEmlSetMem(buf
, i
, 1)) {
1914 PrintAndLog("Cant set emul block: %d", i
);
1921 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1923 // get filename based on UID
1926 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
1927 PrintAndLog("Cant get block: %d", 0);
1928 len
= sprintf(fnameptr
, "dump");
1931 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1932 sprintf(fnameptr
, "%02x", buf
[j
]);
1935 memcpy(filename
, Cmd
, len
);
1939 // add .eml extension
1940 sprintf(fnameptr
, ".eml");
1943 f
= fopen(filename
, "w+");
1946 PrintAndLog("File not found or locked.");
1951 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1952 for (i
= 0; i
< 16 * 4; i
++) {
1953 if (i
== 1) flags
= 0;
1954 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1956 if (mfCGetBlock(i
, buf
, flags
)) {
1957 PrintAndLog("Cant get block: %d", i
);
1960 for (j
= 0; j
< 16; j
++)
1961 fprintf(f
, "%02x", buf
[j
]);
1966 PrintAndLog("Saved to file: %s", filename
);
1971 int CmdHF14AMfSniff(const char *Cmd
){
1973 bool wantLogToFile
= 0;
1974 bool wantDecrypt
= 0;
1975 //bool wantSaveToEml = 0; TODO
1976 bool wantSaveToEmlFile
= 0;
1986 uint8_t atqa
[2] = {0x00};
1989 uint8_t *buf
= NULL
;
1990 uint16_t bufsize
= 0;
1991 uint8_t *bufPtr
= NULL
;
1993 char ctmp
= param_getchar(Cmd
, 0);
1994 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1995 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1996 PrintAndLog("You can specify:");
1997 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1998 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1999 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2000 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2001 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
2002 PrintAndLog(" sample: hf mf sniff l d e");
2006 for (int i
= 0; i
< 4; i
++) {
2007 ctmp
= param_getchar(Cmd
, i
);
2008 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
2009 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
2010 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2011 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
2014 printf("-------------------------------------------------------------------------\n");
2015 printf("Executing command. \n");
2016 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2017 printf("Press the key on pc keyboard to abort the client.\n");
2018 printf("-------------------------------------------------------------------------\n");
2020 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
2021 clearCommandBuffer();
2030 printf("\naborted via keyboard!\n");
2035 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2036 res
= resp
.arg
[0] & 0xff;
2037 uint16_t traceLen
= resp
.arg
[1];
2042 return 0; // we are done
2045 if (res
== 1) { // there is (more) data to be transferred
2046 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
2047 if (traceLen
> bufsize
) {
2049 if (buf
== NULL
) { // not yet allocated
2050 p
= malloc(traceLen
);
2051 } else { // need more memory
2052 p
= realloc(buf
, traceLen
);
2055 PrintAndLog("Cannot allocate memory for trace");
2063 memset(buf
, 0x00, traceLen
);
2065 if (bufPtr
== NULL
) {
2066 PrintAndLog("Cannot allocate memory for trace");
2070 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2075 if (res
== 2) { // received all data, start displaying
2076 blockLen
= bufPtr
- buf
;
2079 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2080 while (bufPtr
- buf
< blockLen
) {
2081 bufPtr
+= 6; // skip (void) timing information
2082 len
= *((uint16_t *)bufPtr
);
2090 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
2091 memcpy(uid
, bufPtr
+ 2, 7);
2092 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2093 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2095 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2096 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2100 if (wantLogToFile
|| wantDecrypt
) {
2101 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2102 AddLogCurrentDT(logHexFileName
);
2105 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2107 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2109 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2111 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
2115 bufPtr
+= ((len
-1)/8+1); // ignore parity
2126 //needs nt, ar, at, Data to decrypt
2127 int CmdHf14MfDecryptBytes(const char *Cmd
){
2130 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2131 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2132 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2135 param_gethex_ex(Cmd
, 3, data
, &len
);
2138 int limit
= sizeof(data
) / 2;
2143 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2146 static command_t CommandTable
[] = {
2147 {"help", CmdHelp
, 1, "This help"},
2148 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2149 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2150 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2151 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2152 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2153 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2154 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2155 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2156 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2157 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2158 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2159 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2160 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2161 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2162 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2163 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2164 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2165 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2166 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2167 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2168 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2169 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2170 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2171 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2172 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2173 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2174 {NULL
, NULL
, 0, NULL
}
2177 int CmdHFMF(const char *Cmd
) {
2179 clearCommandBuffer();
2180 //WaitForResponseTimeout(CMD_ACK,NULL,100);
2181 CmdsParse(CommandTable
, Cmd
);
2185 int CmdHelp(const char *Cmd
) {
2186 CmdsHelp(CommandTable
);