]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
ae380b3c348ce87e174d7d498ad829041863321b
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");
38 while (ukbhit()) getchar();
46 printf("\naborted via keyboard!\n");
51 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
53 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
54 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
55 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
56 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
57 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
60 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
61 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
62 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
63 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
64 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
74 if (isOK
!= 1) return 1;
76 // execute original function from util nonce2key
77 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
79 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
80 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
85 printf("------------------------------------------------------------------\n");
86 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
93 int CmdHF14AMfWrBl(const char *Cmd
)
97 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
98 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
103 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
104 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
108 blockNo
= param_get8(Cmd
, 0);
109 cmdp
= param_getchar(Cmd
, 1);
111 PrintAndLog("Key type must be A or B");
114 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
115 if (param_gethex(Cmd
, 2, key
, 12)) {
116 PrintAndLog("Key must include 12 HEX symbols");
119 if (param_gethex(Cmd
, 3, bldata
, 32)) {
120 PrintAndLog("Block data must include 32 HEX symbols");
123 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
124 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
126 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
127 memcpy(c
.d
.asBytes
, key
, 6);
128 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
129 clearCommandBuffer();
133 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
134 uint8_t isOK
= resp
.arg
[0] & 0xff;
135 PrintAndLog("isOk:%02x", isOK
);
137 PrintAndLog("Command execute timeout");
143 int CmdHF14AMfRdBl(const char *Cmd
)
147 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
153 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
154 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
158 blockNo
= param_get8(Cmd
, 0);
159 cmdp
= param_getchar(Cmd
, 1);
161 PrintAndLog("Key type must be A or B");
164 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
165 if (param_gethex(Cmd
, 2, key
, 12)) {
166 PrintAndLog("Key must include 12 HEX symbols");
169 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
171 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
172 memcpy(c
.d
.asBytes
, key
, 6);
173 clearCommandBuffer();
177 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
178 uint8_t isOK
= resp
.arg
[0] & 0xff;
179 uint8_t *data
= resp
.d
.asBytes
;
182 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
184 PrintAndLog("isOk:%02x", isOK
);
186 PrintAndLog("Command execute timeout");
192 int CmdHF14AMfRdSc(const char *Cmd
)
195 uint8_t sectorNo
= 0;
197 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
199 uint8_t *data
= NULL
;
203 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
204 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
208 sectorNo
= param_get8(Cmd
, 0);
210 PrintAndLog("Sector number must be less than 40");
213 cmdp
= param_getchar(Cmd
, 1);
214 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
215 PrintAndLog("Key type must be A or B");
218 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
219 if (param_gethex(Cmd
, 2, key
, 12)) {
220 PrintAndLog("Key must include 12 HEX symbols");
223 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
225 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
226 memcpy(c
.d
.asBytes
, key
, 6);
227 clearCommandBuffer();
232 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
233 isOK
= resp
.arg
[0] & 0xff;
234 data
= resp
.d
.asBytes
;
236 PrintAndLog("isOk:%02x", isOK
);
238 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
239 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
241 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
244 PrintAndLog("Command execute timeout");
250 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
255 return 32 * 4 + (sectorNo
- 32) * 16;
259 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
268 int CmdHF14AMfDump(const char *Cmd
)
270 uint8_t sectorNo
, blockNo
;
274 uint8_t rights
[40][4];
275 uint8_t carddata
[256][16];
276 uint8_t numSectors
= 16;
283 char cmdp
= param_getchar(Cmd
, 0);
285 case '0' : numSectors
= 5; break;
287 case '\0': numSectors
= 16; break;
288 case '2' : numSectors
= 32; break;
289 case '4' : numSectors
= 40; break;
290 default: numSectors
= 16;
293 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
294 PrintAndLog("Usage: hf mf dump [card memory]");
295 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
297 PrintAndLog("Samples: hf mf dump");
298 PrintAndLog(" hf mf dump 4");
302 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
303 PrintAndLog("Could not find file dumpkeys.bin");
307 // Read keys A from file
308 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
309 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
310 PrintAndLog("File reading error.");
316 // Read keys B from file
317 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
318 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
319 PrintAndLog("File reading error.");
327 PrintAndLog("|-----------------------------------------|");
328 PrintAndLog("|------ Reading sector access bits...-----|");
329 PrintAndLog("|-----------------------------------------|");
331 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
332 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
333 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
334 clearCommandBuffer();
337 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
338 uint8_t isOK
= resp
.arg
[0] & 0xff;
339 uint8_t *data
= resp
.d
.asBytes
;
341 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
342 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
343 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
344 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
346 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
347 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
348 rights
[sectorNo
][3] = 0x01;
351 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
352 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
353 rights
[sectorNo
][3] = 0x01;
357 PrintAndLog("|-----------------------------------------|");
358 PrintAndLog("|----- Dumping all blocks to file... -----|");
359 PrintAndLog("|-----------------------------------------|");
362 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
363 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
364 bool received
= false;
366 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
367 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
368 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
369 clearCommandBuffer();
371 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
372 } else { // data block. Check if it can be read with key A or key B
373 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
374 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
375 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
376 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
378 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
379 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
381 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
382 } else { // key A would work
383 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
384 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
385 clearCommandBuffer();
387 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
392 isOK
= resp
.arg
[0] & 0xff;
393 uint8_t *data
= resp
.d
.asBytes
;
394 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
395 data
[0] = (keyA
[sectorNo
][0]);
396 data
[1] = (keyA
[sectorNo
][1]);
397 data
[2] = (keyA
[sectorNo
][2]);
398 data
[3] = (keyA
[sectorNo
][3]);
399 data
[4] = (keyA
[sectorNo
][4]);
400 data
[5] = (keyA
[sectorNo
][5]);
401 data
[10] = (keyB
[sectorNo
][0]);
402 data
[11] = (keyB
[sectorNo
][1]);
403 data
[12] = (keyB
[sectorNo
][2]);
404 data
[13] = (keyB
[sectorNo
][3]);
405 data
[14] = (keyB
[sectorNo
][4]);
406 data
[15] = (keyB
[sectorNo
][5]);
409 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
410 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
412 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
418 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
425 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
426 PrintAndLog("Could not create file name dumpdata.bin");
429 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
430 fwrite(carddata
, 1, 16*numblocks
, fout
);
432 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
438 int CmdHF14AMfRestore(const char *Cmd
)
440 uint8_t sectorNo
,blockNo
;
442 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
443 uint8_t bldata
[16] = {0x00};
451 char cmdp
= param_getchar(Cmd
, 0);
453 case '0' : numSectors
= 5; break;
455 case '\0': numSectors
= 16; break;
456 case '2' : numSectors
= 32; break;
457 case '4' : numSectors
= 40; break;
458 default: numSectors
= 16;
461 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
462 PrintAndLog("Usage: hf mf restore [card memory]");
463 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
465 PrintAndLog("Samples: hf mf restore");
466 PrintAndLog(" hf mf restore 4");
470 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
471 PrintAndLog("Could not find file dumpkeys.bin");
475 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
476 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
477 PrintAndLog("File reading error (dumpkeys.bin).");
483 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
484 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
485 PrintAndLog("File reading error (dumpkeys.bin).");
493 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
494 PrintAndLog("Could not find file dumpdata.bin");
497 PrintAndLog("Restoring dumpdata.bin to card");
499 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
500 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
501 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
502 memcpy(c
.d
.asBytes
, key
, 6);
504 if (fread(bldata
, 1, 16, fdump
) == 0) {
505 PrintAndLog("File reading error (dumpdata.bin).");
510 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
511 bldata
[0] = (keyA
[sectorNo
][0]);
512 bldata
[1] = (keyA
[sectorNo
][1]);
513 bldata
[2] = (keyA
[sectorNo
][2]);
514 bldata
[3] = (keyA
[sectorNo
][3]);
515 bldata
[4] = (keyA
[sectorNo
][4]);
516 bldata
[5] = (keyA
[sectorNo
][5]);
517 bldata
[10] = (keyB
[sectorNo
][0]);
518 bldata
[11] = (keyB
[sectorNo
][1]);
519 bldata
[12] = (keyB
[sectorNo
][2]);
520 bldata
[13] = (keyB
[sectorNo
][3]);
521 bldata
[14] = (keyB
[sectorNo
][4]);
522 bldata
[15] = (keyB
[sectorNo
][5]);
525 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
527 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
528 clearCommandBuffer();
532 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
533 uint8_t isOK
= resp
.arg
[0] & 0xff;
534 PrintAndLog("isOk:%02x", isOK
);
536 PrintAndLog("Command execute timeout");
545 int CmdHF14AMfNested(const char *Cmd
)
547 int i
, j
, res
, iterations
;
548 sector
*e_sector
= NULL
;
551 uint8_t trgBlockNo
= 0;
552 uint8_t trgKeyType
= 0;
553 uint8_t SectorsCnt
= 0;
554 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
555 uint8_t keyBlock
[14*6];
557 bool transferToEml
= false;
559 bool createDumpFile
= false;
561 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
562 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
567 PrintAndLog("Usage:");
568 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
569 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
570 PrintAndLog(" <target block number> <target key A/B> [t]");
571 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
572 PrintAndLog("t - transfer keys into emulator memory");
573 PrintAndLog("d - write keys to binary file");
575 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
576 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
577 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
578 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
582 cmdp
= param_getchar(Cmd
, 0);
583 blockNo
= param_get8(Cmd
, 1);
584 ctmp
= param_getchar(Cmd
, 2);
586 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
587 PrintAndLog("Key type must be A or B");
591 if (ctmp
!= 'A' && ctmp
!= 'a')
594 if (param_gethex(Cmd
, 3, key
, 12)) {
595 PrintAndLog("Key must include 12 HEX symbols");
599 if (cmdp
== 'o' || cmdp
== 'O') {
601 trgBlockNo
= param_get8(Cmd
, 4);
602 ctmp
= param_getchar(Cmd
, 5);
603 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
604 PrintAndLog("Target key type must be A or B");
607 if (ctmp
!= 'A' && ctmp
!= 'a')
612 case '0': SectorsCnt
= 05; break;
613 case '1': SectorsCnt
= 16; break;
614 case '2': SectorsCnt
= 32; break;
615 case '4': SectorsCnt
= 40; break;
616 default: SectorsCnt
= 16;
620 ctmp
= param_getchar(Cmd
, 4);
621 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
622 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
624 ctmp
= param_getchar(Cmd
, 6);
625 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
626 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
629 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
630 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
633 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
634 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
635 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
636 default : PrintAndLog("Unknown Error.\n");
640 key64
= bytes_to_num(keyBlock
, 6);
642 PrintAndLog("Found valid key:%012"llx
, key64
);
644 // transfer key to the emulator
646 uint8_t sectortrailer
;
647 if (trgBlockNo
< 32*4) { // 4 block sector
648 sectortrailer
= (trgBlockNo
& 0x03) + 3;
649 } else { // 16 block sector
650 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
652 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
655 num_to_bytes(key64
, 6, keyBlock
);
657 num_to_bytes(key64
, 6, &keyBlock
[10]);
658 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
661 PrintAndLog("No valid key found");
664 else { // ------------------------------------ multiple sectors working
668 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
669 if (e_sector
== NULL
) return 1;
671 //test current key and additional standard keys first
672 memcpy(keyBlock
, key
, 6);
673 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
674 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
675 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
676 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
677 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
678 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
679 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
680 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
681 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
682 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
683 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
684 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
685 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
687 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
688 for (i
= 0; i
< SectorsCnt
; i
++) {
689 for (j
= 0; j
< 2; j
++) {
690 if (e_sector
[i
].foundKey
[j
]) continue;
692 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
695 e_sector
[i
].Key
[j
] = key64
;
696 e_sector
[i
].foundKey
[j
] = 1;
703 PrintAndLog("nested...");
704 bool calibrate
= true;
705 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
706 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
707 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
708 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
709 PrintAndLog("-----------------------------------------------");
710 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
713 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
714 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
715 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
716 default : PrintAndLog("Unknown Error.\n");
726 key64
= bytes_to_num(keyBlock
, 6);
728 PrintAndLog("Found valid key:%012"llx
, key64
);
729 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
730 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
736 printf("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
);
738 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
740 PrintAndLog("|---|----------------|---|----------------|---|");
741 PrintAndLog("|sec|key A |res|key B |res|");
742 PrintAndLog("|---|----------------|---|----------------|---|");
743 for (i
= 0; i
< SectorsCnt
; i
++) {
744 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
745 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
747 PrintAndLog("|---|----------------|---|----------------|---|");
749 // transfer them to the emulator
751 for (i
= 0; i
< SectorsCnt
; i
++) {
752 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
753 if (e_sector
[i
].foundKey
[0])
754 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
755 if (e_sector
[i
].foundKey
[1])
756 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
757 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
762 if (createDumpFile
) {
763 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
764 PrintAndLog("Could not create file dumpkeys.bin");
768 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
769 for(i
=0; i
<SectorsCnt
; i
++) {
770 if (e_sector
[i
].foundKey
[0]){
771 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
772 fwrite ( tempkey
, 1, 6, fkeys
);
775 fwrite ( &standart
, 1, 6, fkeys
);
778 for(i
=0; i
<SectorsCnt
; i
++) {
779 if (e_sector
[i
].foundKey
[1]){
780 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
781 fwrite ( tempkey
, 1, 6, fkeys
);
784 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};
805 ctmp
= param_getchar(Cmd
, 0);
806 if (ctmp
!= 'R' && ctmp
!= 'r' && strlen(Cmd
) < 20) {
807 PrintAndLog("Usage:");
808 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
809 PrintAndLog(" <target block number> <target key A|B> [w] [s]");
810 PrintAndLog(" or hf mf hardnested r");
812 PrintAndLog("Options: ");
813 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
814 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
815 PrintAndLog(" r: Read nonces.bin and start attack");
817 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
818 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
819 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
820 PrintAndLog(" sample4: hf mf hardnested r");
825 bool nonce_file_read
= false;
826 bool nonce_file_write
= false;
829 if (ctmp
== 'R' || ctmp
== 'r') {
831 nonce_file_read
= true;
835 blockNo
= param_get8(Cmd
, 0);
836 ctmp
= param_getchar(Cmd
, 1);
837 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
838 PrintAndLog("Key type must be A or B");
841 if (ctmp
!= 'A' && ctmp
!= 'a') {
845 if (param_gethex(Cmd
, 2, key
, 12)) {
846 PrintAndLog("Key must include 12 HEX symbols");
850 trgBlockNo
= param_get8(Cmd
, 3);
851 ctmp
= param_getchar(Cmd
, 4);
852 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
853 PrintAndLog("Target key type must be A or B");
856 if (ctmp
!= 'A' && ctmp
!= 'a') {
861 while ((ctmp
= param_getchar(Cmd
, i
))) {
862 if (ctmp
== 's' || ctmp
== 'S') {
864 } else if (ctmp
== 'w' || ctmp
== 'W') {
865 nonce_file_write
= true;
867 PrintAndLog("Possible options are w and/or s");
874 PrintAndLog("--target block no:%3d, target key type:%c, file action: %s, Slow: %s ",
877 nonce_file_write
?"write":nonce_file_read
?"read":"none",
879 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, nonce_file_read
, nonce_file_write
, slow
);
882 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
883 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
893 int CmdHF14AMfChk(const char *Cmd
)
896 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
897 PrintAndLog(" * - all sectors");
898 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
899 PrintAndLog("d - write keys to binary file");
900 PrintAndLog("t - write keys to emulator memory\n");
901 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
902 PrintAndLog(" hf mf chk *1 ? t");
903 PrintAndLog(" hf mf chk *1 ? d");
908 char filename
[FILE_PATH_SIZE
]={0};
910 uint8_t *keyBlock
= NULL
, *p
;
911 uint8_t stKeyBlock
= 20;
917 uint8_t SectorsCnt
= 1;
921 int transferToEml
= 0;
922 int createDumpFile
= 0;
924 keyBlock
= calloc(stKeyBlock
, 6);
925 if (keyBlock
== NULL
) return 1;
927 uint64_t defaultKeys
[] =
929 0xffffffffffff, // Default key (first key used by program if no user defined key)
930 0x000000000000, // Blank key
931 0xa0a1a2a3a4a5, // NFCForum MAD key
943 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
945 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
947 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
950 if (param_getchar(Cmd
, 0)=='*') {
952 switch(param_getchar(Cmd
+1, 0)) {
953 case '0': SectorsCnt
= 5; break;
954 case '1': SectorsCnt
= 16; break;
955 case '2': SectorsCnt
= 32; break;
956 case '4': SectorsCnt
= 40; break;
957 default: SectorsCnt
= 16;
961 blockNo
= param_get8(Cmd
, 0);
963 ctmp
= param_getchar(Cmd
, 1);
975 PrintAndLog("Key type must be A , B or ?");
979 ctmp
= param_getchar(Cmd
, 2);
980 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
981 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
983 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
984 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
985 if ( stKeyBlock
- keycnt
< 2) {
986 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
988 PrintAndLog("Cannot allocate memory for Keys");
994 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
995 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
996 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1000 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1001 PrintAndLog("File name too long");
1006 if ( (f
= fopen( filename
, "r")) ) {
1007 while( fgets(buf
, sizeof(buf
), f
) ){
1008 if (strlen(buf
) < 12 || buf
[11] == '\n')
1011 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1013 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1015 if (!isxdigit(buf
[0])){
1016 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1022 if ( stKeyBlock
- keycnt
< 2) {
1023 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1025 PrintAndLog("Cannot allocate memory for defKeys");
1031 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1032 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1033 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1035 memset(buf
, 0, sizeof(buf
));
1039 PrintAndLog("File: %s: not found or locked.", filename
);
1048 PrintAndLog("No key specified, trying default keys");
1049 for (;keycnt
< defaultKeysSize
; keycnt
++)
1050 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1051 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1052 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1055 // initialize storage for found keys
1056 bool validKey
[2][40];
1057 uint8_t foundKey
[2][40][6];
1058 for (uint16_t t
= 0; t
< 2; t
++) {
1059 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1060 validKey
[t
][sectorNo
] = false;
1061 for (uint16_t i
= 0; i
< 6; i
++) {
1062 foundKey
[t
][sectorNo
][i
] = 0xff;
1067 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
1069 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1070 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
1071 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
1072 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
1073 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
1074 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
1077 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
1078 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
1079 validKey
[t
][i
] = true;
1082 PrintAndLog("Command execute timeout");
1085 b
<127?(b
+=4):(b
+=16);
1089 if (transferToEml
) {
1091 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
1092 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
1093 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1094 for (uint16_t t
= 0; t
< 2; t
++) {
1095 if (validKey
[t
][sectorNo
]) {
1096 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
1099 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
1102 PrintAndLog("Found keys have been transferred to the emulator memory");
1105 if (createDumpFile
) {
1106 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1107 if (fkeys
== NULL
) {
1108 PrintAndLog("Could not create file dumpkeys.bin");
1112 for (uint16_t t
= 0; t
< 2; t
++) {
1113 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1116 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1124 int CmdHF14AMf1kSim(const char *Cmd
)
1126 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1127 uint8_t exitAfterNReads
= 0;
1130 uint8_t cmdp
= param_getchar(Cmd
, 0);
1132 if (cmdp
== 'h' || cmdp
== 'H') {
1133 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1134 PrintAndLog(" h this help");
1135 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1136 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1137 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1138 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1140 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1144 if (param_getchar(Cmd
, pnr
) == 'u') {
1145 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1147 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1148 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1149 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1151 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1156 if (param_getchar(Cmd
, pnr
) == 'n') {
1157 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1160 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1161 //Using a flag to signal interactiveness, least significant bit
1162 flags
|= FLAG_INTERACTIVE
;
1166 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1167 //Using a flag to signal interactiveness, least significant bit
1168 flags
|= FLAG_NR_AR_ATTACK
;
1170 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1171 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1172 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1173 , exitAfterNReads
, flags
,flags
);
1176 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1177 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1178 clearCommandBuffer();
1181 if(flags
& FLAG_INTERACTIVE
)
1183 PrintAndLog("Press pm3-button to abort simulation");
1190 if ( WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1191 if ( (resp
.arg
[0] & 0xffff) == CMD_SIMULATE_MIFARE_CARD
){
1192 memset(data
, 0x00, sizeof(data
));
1193 memset(key
, 0x00, sizeof(key
));
1194 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1196 memcpy(data
, resp
.d
.asBytes
, len
);
1198 uint64_t corr_uid
= 0;
1199 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) {
1200 corr_uid
= (data
[3] << 24) | (data
[2] << 16) | (data
[1] << 8) | data
[0];
1203 corr_uid
|= (uint64_t)data
[2] << 48;
1204 corr_uid
|= (uint64_t)data
[1] << 40;
1205 corr_uid
|= (uint64_t)data
[0] << 32;
1206 corr_uid
|= data
[7] << 24;
1207 corr_uid
|= data
[6] << 16;
1208 corr_uid
|= data
[5] << 8;
1209 corr_uid
|= data
[4];
1211 tryMfk32(corr_uid
, data
, key
);
1212 //tryMfk64(corr_uid, data, key);
1221 int CmdHF14AMfDbg(const char *Cmd
)
1223 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1225 PrintAndLog("Max debug mode parameter is 4 \n");
1228 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1229 PrintAndLog("Usage: hf mf dbg <debug level>");
1230 PrintAndLog(" 0 - no debug messages");
1231 PrintAndLog(" 1 - error messages");
1232 PrintAndLog(" 2 - plus information messages");
1233 PrintAndLog(" 3 - plus debug messages");
1234 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1235 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1239 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1245 int CmdHF14AMfEGet(const char *Cmd
)
1247 uint8_t blockNo
= 0;
1248 uint8_t data
[16] = {0x00};
1250 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1251 PrintAndLog("Usage: hf mf eget <block number>");
1252 PrintAndLog(" sample: hf mf eget 0 ");
1256 blockNo
= param_get8(Cmd
, 0);
1259 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1260 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1262 PrintAndLog("Command execute timeout");
1268 int CmdHF14AMfEClear(const char *Cmd
)
1270 if (param_getchar(Cmd
, 0) == 'h') {
1271 PrintAndLog("Usage: hf mf eclr");
1272 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1276 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1281 int CmdHF14AMfESet(const char *Cmd
)
1283 uint8_t memBlock
[16];
1284 uint8_t blockNo
= 0;
1286 memset(memBlock
, 0x00, sizeof(memBlock
));
1288 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1289 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1290 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1294 blockNo
= param_get8(Cmd
, 0);
1296 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1297 PrintAndLog("block data must include 32 HEX symbols");
1302 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1303 memcpy(c
.d
.asBytes
, memBlock
, 16);
1308 int CmdHF14AMfELoad(const char *Cmd
)
1311 char filename
[FILE_PATH_SIZE
];
1312 char *fnameptr
= filename
;
1313 char buf
[64] = {0x00};
1314 uint8_t buf8
[64] = {0x00};
1315 int i
, len
, blockNum
, numBlocks
;
1316 int nameParamNo
= 1;
1317 uint8_t blockWidth
= 32;
1318 char ctmp
= param_getchar(Cmd
, 0);
1320 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1321 PrintAndLog("It loads emul dump from the file `filename.eml`");
1322 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1323 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1325 PrintAndLog(" sample: hf mf eload filename");
1326 PrintAndLog(" hf mf eload 4 filename");
1331 case '0' : numBlocks
= 5*4; break;
1333 case '\0': numBlocks
= 16*4; break;
1334 case '2' : numBlocks
= 32*4; break;
1335 case '4' : numBlocks
= 256; break;
1336 case 'U' : // fall through , NTAG 215 has 135blocks a 540 bytes.
1337 case 'u' : numBlocks
= 135; blockWidth
= 8; break;
1344 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1346 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1350 sprintf(fnameptr
, ".eml");
1353 f
= fopen(filename
, "r");
1355 PrintAndLog("File %s not found or locked", filename
);
1361 memset(buf
, 0, sizeof(buf
));
1363 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1365 if (blockNum
>= numBlocks
) break;
1367 PrintAndLog("File reading error.");
1372 if (strlen(buf
) < blockWidth
){
1373 if(strlen(buf
) && feof(f
))
1375 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1380 for (i
= 0; i
< blockWidth
; i
+= 2) {
1381 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1383 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1384 PrintAndLog("Cant set emul block: %3d", blockNum
);
1391 if (blockNum
>= numBlocks
) break;
1396 if ((blockNum
!= numBlocks
)) {
1397 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1400 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1404 int CmdHF14AMfESave(const char *Cmd
)
1407 char filename
[FILE_PATH_SIZE
];
1408 char * fnameptr
= filename
;
1410 int i
, j
, len
, numBlocks
;
1411 int nameParamNo
= 1;
1413 memset(filename
, 0, sizeof(filename
));
1414 memset(buf
, 0, sizeof(buf
));
1416 char ctmp
= param_getchar(Cmd
, 0);
1418 if ( ctmp
== 'h' || ctmp
== 'H') {
1419 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1420 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1421 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1423 PrintAndLog(" sample: hf mf esave ");
1424 PrintAndLog(" hf mf esave 4");
1425 PrintAndLog(" hf mf esave 4 filename");
1430 case '0' : numBlocks
= 5*4; break;
1432 case '\0': numBlocks
= 16*4; break;
1433 case '2' : numBlocks
= 32*4; break;
1434 case '4' : numBlocks
= 256; break;
1441 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1443 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1445 // user supplied filename?
1447 // get filename (UID from memory)
1448 if (mfEmlGetMem(buf
, 0, 1)) {
1449 PrintAndLog("Can\'t get UID from block: %d", 0);
1450 len
= sprintf(fnameptr
, "dump");
1454 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1455 sprintf(fnameptr
, "%02X", buf
[j
]);
1461 // add file extension
1462 sprintf(fnameptr
, ".eml");
1465 f
= fopen(filename
, "w+");
1468 PrintAndLog("Can't open file %s ", filename
);
1473 for (i
= 0; i
< numBlocks
; i
++) {
1474 if (mfEmlGetMem(buf
, i
, 1)) {
1475 PrintAndLog("Cant get block: %d", i
);
1478 for (j
= 0; j
< 16; j
++)
1479 fprintf(f
, "%02X", buf
[j
]);
1484 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1489 int CmdHF14AMfECFill(const char *Cmd
)
1491 uint8_t keyType
= 0;
1492 uint8_t numSectors
= 16;
1494 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1495 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1496 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1498 PrintAndLog("samples: hf mf ecfill A");
1499 PrintAndLog(" hf mf ecfill A 4");
1500 PrintAndLog("Read card and transfer its data to emulator memory.");
1501 PrintAndLog("Keys must be laid in the emulator memory. \n");
1505 char ctmp
= param_getchar(Cmd
, 0);
1506 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1507 PrintAndLog("Key type must be A or B");
1510 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1512 ctmp
= param_getchar(Cmd
, 1);
1514 case '0' : numSectors
= 5; break;
1516 case '\0': numSectors
= 16; break;
1517 case '2' : numSectors
= 32; break;
1518 case '4' : numSectors
= 40; break;
1519 default: numSectors
= 16;
1522 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1523 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1528 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1533 uint64_t keyA
, keyB
;
1535 char cmdp
= param_getchar(Cmd
, 0);
1537 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1538 PrintAndLog("It prints the keys loaded in the emulator memory");
1539 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1540 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1542 PrintAndLog(" sample: hf mf ekeyprn 1");
1547 case '0' : numSectors
= 5; break;
1549 case '\0': numSectors
= 16; break;
1550 case '2' : numSectors
= 32; break;
1551 case '4' : numSectors
= 40; break;
1552 default: numSectors
= 16;
1555 PrintAndLog("|---|----------------|----------------|");
1556 PrintAndLog("|sec|key A |key B |");
1557 PrintAndLog("|---|----------------|----------------|");
1558 for (i
= 0; i
< numSectors
; i
++) {
1559 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1560 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1563 keyA
= bytes_to_num(data
, 6);
1564 keyB
= bytes_to_num(data
+ 10, 6);
1565 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1567 PrintAndLog("|---|----------------|----------------|");
1572 int CmdHF14AMfCSetUID(const char *Cmd
)
1574 uint8_t wipeCard
= 0;
1575 uint8_t uid
[8] = {0x00};
1576 uint8_t oldUid
[8] = {0x00};
1577 uint8_t atqa
[2] = {0x00};
1578 uint8_t sak
[1] = {0x00};
1579 uint8_t atqaPresent
= 1;
1584 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1585 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1586 PrintAndLog("sample: hf mf csetuid 01020304");
1587 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1588 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1589 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1593 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1594 PrintAndLog("UID must include 8 HEX symbols");
1599 ctmp
= param_getchar(Cmd
, argi
);
1600 if (ctmp
== 'w' || ctmp
== 'W') {
1606 if (param_getchar(Cmd
, argi
)) {
1607 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1608 PrintAndLog("ATQA must include 4 HEX symbols");
1612 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1613 PrintAndLog("SAK must include 2 HEX symbols");
1622 ctmp
= param_getchar(Cmd
, argi
);
1623 if (ctmp
== 'w' || ctmp
== 'W') {
1628 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1630 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1632 PrintAndLog("Can't set UID. error=%d", res
);
1636 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1637 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1641 int CmdHF14AMfCSetBlk(const char *Cmd
)
1643 uint8_t block
[16] = {0x00};
1644 uint8_t blockNo
= 0;
1645 uint8_t params
= MAGIC_SINGLE
;
1648 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1649 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1650 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1651 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1652 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1656 blockNo
= param_get8(Cmd
, 0);
1658 if (param_gethex(Cmd
, 1, block
, 32)) {
1659 PrintAndLog("block data must include 32 HEX symbols");
1663 char ctmp
= param_getchar(Cmd
, 2);
1664 if (ctmp
== 'w' || ctmp
== 'W')
1665 params
|= MAGIC_WIPE
;
1667 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
1669 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
1671 PrintAndLog("Can't write block. error=%d", res
);
1677 int CmdHF14AMfCLoad(const char *Cmd
)
1680 char filename
[FILE_PATH_SIZE
];
1681 char * fnameptr
= filename
;
1682 char buf
[64] = {0x00};
1683 uint8_t buf8
[64] = {0x00};
1684 uint8_t fillFromEmulator
= 0;
1685 int i
, len
, blockNum
, flags
=0;
1687 memset(filename
, 0, sizeof(filename
));
1689 char ctmp
= param_getchar(Cmd
, 0);
1691 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1692 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1693 PrintAndLog("or from emulator memory (option `e`)");
1694 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1695 PrintAndLog(" or: hf mf cload e ");
1696 PrintAndLog(" sample: hf mf cload filename");
1700 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1702 if (fillFromEmulator
) {
1703 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1704 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1705 PrintAndLog("Cant get block: %d", blockNum
);
1708 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1709 if (blockNum
== 1) flags
= 0; // just write
1710 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
1712 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1713 PrintAndLog("Cant set magic card block: %d", blockNum
);
1720 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1722 memcpy(filename
, Cmd
, len
);
1725 sprintf(fnameptr
, ".eml");
1728 f
= fopen(filename
, "r");
1730 PrintAndLog("File not found or locked.");
1737 memset(buf
, 0, sizeof(buf
));
1739 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1741 PrintAndLog("File reading error.");
1745 if (strlen(buf
) < 32) {
1746 if(strlen(buf
) && feof(f
))
1748 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1752 for (i
= 0; i
< 32; i
+= 2)
1753 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1755 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
1756 if (blockNum
== 1) flags
= 0; // just write
1757 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
1759 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
1760 PrintAndLog("Can't set magic card block: %d", blockNum
);
1765 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1770 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1771 PrintAndLog("File content error. There must be 64 blocks");
1774 PrintAndLog("Loaded from file: %s", filename
);
1780 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1782 uint8_t blockNo
= 0;
1784 memset(data
, 0x00, sizeof(data
));
1785 char ctmp
= param_getchar(Cmd
, 0);
1787 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1788 PrintAndLog("Usage: hf mf cgetblk <block number>");
1789 PrintAndLog("sample: hf mf cgetblk 1");
1790 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1794 blockNo
= param_get8(Cmd
, 0);
1796 PrintAndLog("--block number:%2d ", blockNo
);
1798 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
1800 PrintAndLog("Can't read block. error=%d", res
);
1804 PrintAndLog("data:%s", sprint_hex(data
, sizeof(data
)));
1808 int CmdHF14AMfCGetSc(const char *Cmd
) {
1810 uint8_t sectorNo
= 0;
1812 memset(data
, 0x00, sizeof(data
));
1813 char ctmp
= param_getchar(Cmd
, 0);
1815 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
1816 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1817 PrintAndLog("sample: hf mf cgetsc 0");
1818 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1822 sectorNo
= param_get8(Cmd
, 0);
1823 if (sectorNo
> 15) {
1824 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1828 PrintAndLog("--sector number:%d ", sectorNo
);
1829 PrintAndLog("block | data");
1831 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1832 for (i
= 0; i
< 4; i
++) {
1833 if (i
== 1) flags
= 0;
1834 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1836 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
1838 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1841 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
1846 int CmdHF14AMfCSave(const char *Cmd
) {
1849 char filename
[FILE_PATH_SIZE
];
1850 char * fnameptr
= filename
;
1851 uint8_t fillFromEmulator
= 0;
1853 int i
, j
, len
, flags
;
1855 memset(filename
, 0, sizeof(filename
));
1856 memset(buf
, 0, sizeof(buf
));
1857 char ctmp
= param_getchar(Cmd
, 0);
1859 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1860 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1861 PrintAndLog("or into emulator memory (option `e`)");
1862 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1863 PrintAndLog(" sample: hf mf esave ");
1864 PrintAndLog(" hf mf esave filename");
1865 PrintAndLog(" hf mf esave e \n");
1868 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1870 if (fillFromEmulator
) {
1871 // put into emulator
1872 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1873 for (i
= 0; i
< 16 * 4; i
++) {
1874 if (i
== 1) flags
= 0;
1875 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1877 if (mfCGetBlock(i
, buf
, flags
)) {
1878 PrintAndLog("Cant get block: %d", i
);
1882 if (mfEmlSetMem(buf
, i
, 1)) {
1883 PrintAndLog("Cant set emul block: %d", i
);
1890 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1892 // get filename based on UID
1895 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
1896 PrintAndLog("Cant get block: %d", 0);
1897 len
= sprintf(fnameptr
, "dump");
1900 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1901 sprintf(fnameptr
, "%02x", buf
[j
]);
1904 memcpy(filename
, Cmd
, len
);
1908 // add .eml extension
1909 sprintf(fnameptr
, ".eml");
1912 f
= fopen(filename
, "w+");
1915 PrintAndLog("File not found or locked.");
1920 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
1921 for (i
= 0; i
< 16 * 4; i
++) {
1922 if (i
== 1) flags
= 0;
1923 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
1925 if (mfCGetBlock(i
, buf
, flags
)) {
1926 PrintAndLog("Cant get block: %d", i
);
1929 for (j
= 0; j
< 16; j
++)
1930 fprintf(f
, "%02x", buf
[j
]);
1935 PrintAndLog("Saved to file: %s", filename
);
1940 int CmdHF14AMfSniff(const char *Cmd
){
1942 bool wantLogToFile
= 0;
1943 bool wantDecrypt
= 0;
1944 //bool wantSaveToEml = 0; TODO
1945 bool wantSaveToEmlFile
= 0;
1955 uint8_t atqa
[2] = {0x00};
1958 uint8_t *buf
= NULL
;
1959 uint16_t bufsize
= 0;
1960 uint8_t *bufPtr
= NULL
;
1962 char ctmp
= param_getchar(Cmd
, 0);
1963 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1964 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1965 PrintAndLog("You can specify:");
1966 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1967 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1968 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1969 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1970 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1971 PrintAndLog(" sample: hf mf sniff l d e");
1975 for (int i
= 0; i
< 4; i
++) {
1976 ctmp
= param_getchar(Cmd
, i
);
1977 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1978 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1979 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1980 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1983 printf("-------------------------------------------------------------------------\n");
1984 printf("Executing command. \n");
1985 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1986 printf("Press the key on pc keyboard to abort the client.\n");
1987 printf("-------------------------------------------------------------------------\n");
1989 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1990 clearCommandBuffer();
1999 printf("\naborted via keyboard!\n");
2004 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
2005 res
= resp
.arg
[0] & 0xff;
2006 uint16_t traceLen
= resp
.arg
[1];
2009 if (res
== 0) return 0; // we are done
2011 if (res
== 1) { // there is (more) data to be transferred
2012 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
2013 if (traceLen
> bufsize
) {
2015 if (buf
== NULL
) { // not yet allocated
2016 p
= malloc(traceLen
);
2017 } else { // need more memory
2018 p
= realloc(buf
, traceLen
);
2021 PrintAndLog("Cannot allocate memory for trace");
2029 memset(buf
, 0x00, traceLen
);
2031 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
2036 if (res
== 2) { // received all data, start displaying
2037 blockLen
= bufPtr
- buf
;
2040 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
2041 while (bufPtr
- buf
< blockLen
) {
2042 bufPtr
+= 6; // skip (void) timing information
2043 len
= *((uint16_t *)bufPtr
);
2051 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
2052 memcpy(uid
, bufPtr
+ 2, 7);
2053 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
2054 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
2056 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2057 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
2061 if (wantLogToFile
|| wantDecrypt
) {
2062 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
2063 AddLogCurrentDT(logHexFileName
);
2066 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
2068 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
2070 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
2072 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
2076 bufPtr
+= ((len
-1)/8+1); // ignore parity
2087 //needs nt, ar, at, Data to decrypt
2088 int CmdHf14MfDecryptBytes(const char *Cmd
){
2091 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2092 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2093 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2096 param_gethex_ex(Cmd
, 3, data
, &len
);
2099 int limit
= sizeof(data
) / 2;
2104 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2107 static command_t CommandTable
[] =
2109 {"help", CmdHelp
, 1, "This help"},
2110 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2111 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2112 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2113 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2114 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2115 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2116 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2117 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2118 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2119 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2120 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2121 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2122 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2123 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2124 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2125 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2126 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2127 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2128 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2129 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2130 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2131 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2132 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2133 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2134 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2135 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2136 {NULL
, NULL
, 0, NULL
}
2139 int CmdHFMF(const char *Cmd
)
2142 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
2143 CmdsParse(CommandTable
, Cmd
);
2147 int CmdHelp(const char *Cmd
)
2149 CmdsHelp(CommandTable
);