]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
9b43f4118289ae109c2e8e9860ff4cf51146e3dc
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
);
16 int usage_hf14_mifare(void){
17 PrintAndLog("Usage: hf mf mifare [h] <block number>");
18 PrintAndLog("options:");
19 PrintAndLog(" h this help");
20 PrintAndLog(" <block number> (Optional) target other key A than block 0.");
21 PrintAndLog("samples:");
22 PrintAndLog(" hf mf mifare");
23 PrintAndLog(" hf mf mifare 16");
26 int usage_hf14_mf1ksim(void){
27 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14,20 hex symbols)> n <numreads> i x");
28 PrintAndLog("options:");
29 PrintAndLog(" h this help");
30 PrintAndLog(" u (Optional) UID 4,7 or 10bytes. If not specified, the UID 4b from emulator memory will be used");
31 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
32 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
33 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
34 PrintAndLog("samples:");
35 PrintAndLog(" hf mf sim u 0a0a0a0a");
36 PrintAndLog(" hf mf sim u 11223344556677");
37 PrintAndLog(" hf mf sim u 112233445566778899AA");
40 int usage_hf14_dbg(void){
41 PrintAndLog("Usage: hf mf dbg [h] <debug level>");
42 PrintAndLog("options:");
43 PrintAndLog(" h this help");
44 PrintAndLog(" <debug level> (Optional) see list for valid levels");
45 PrintAndLog(" 0 - no debug messages");
46 PrintAndLog(" 1 - error messages");
47 PrintAndLog(" 2 - plus information messages");
48 PrintAndLog(" 3 - plus debug messages");
49 PrintAndLog(" 4 - print even debug messages in timing critical functions");
50 PrintAndLog(" Note: this option therefore may cause malfunction itself");
51 PrintAndLog("samples:");
52 PrintAndLog(" hf mf dbg 3");
55 int usage_hf14_sniff(void){
56 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
57 PrintAndLog("Usage: hf mf sniff [h] [l] [d] [f]");
58 PrintAndLog("options:");
59 PrintAndLog(" h this help");
60 PrintAndLog(" l save encrypted sequence to logfile `uid.log`");
61 PrintAndLog(" d decrypt sequence and put it to log file `uid.log`");
62 // PrintAndLog(" n/a e decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
63 PrintAndLog(" f decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
64 PrintAndLog("sample:");
65 PrintAndLog(" hf mf sniff l d f");
68 int usage_hf14_nested(void){
69 PrintAndLog("Usage:");
70 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
71 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
72 PrintAndLog(" <target block number> <target key A/B> [t]");
73 PrintAndLog("options:");
74 PrintAndLog(" h this help");
75 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
76 PrintAndLog(" t transfer keys into emulator memory");
77 PrintAndLog(" d write keys to binary file");
79 PrintAndLog("samples:");
80 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF ");
81 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF t ");
82 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF d ");
83 PrintAndLog(" hf mf nested o 0 A FFFFFFFFFFFF 4 A");
86 int usage_hf14_hardnested(void){
87 PrintAndLog("Usage:");
88 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
89 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
90 PrintAndLog(" or hf mf hardnested r [known target key]");
92 PrintAndLog("options:");
93 PrintAndLog(" h this help");
94 PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
95 PrintAndLog(" s slower acquisition (required by some non standard cards)");
96 PrintAndLog(" r read nonces.bin and start attack");
97 PrintAndLog(" t tests?");
99 PrintAndLog("samples:");
100 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
101 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
102 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
103 PrintAndLog(" hf mf hardnested r");
104 PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
106 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
107 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
110 int usage_hf14_chk(void){
111 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
112 PrintAndLog("options:");
113 PrintAndLog(" h this help");
114 PrintAndLog(" * all sectors based on card memory, other values then below defaults to 1k");
115 PrintAndLog(" 0 - MINI(320 bytes)");
116 PrintAndLog(" 1 - 1K");
117 PrintAndLog(" 2 - 2K");
118 PrintAndLog(" 4 - 4K");
119 PrintAndLog(" d write keys to binary file");
120 PrintAndLog(" t write keys to emulator memory\n");
122 PrintAndLog("samples:");
123 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic -- target block 0, Key A");
124 PrintAndLog(" hf mf chk *1 ? t -- target all blocks, all keys, 1K, write to emul");
125 PrintAndLog(" hf mf chk *1 ? d -- target all blocks, all keys, 1K, write to file");
129 int CmdHF14AMifare(const char *Cmd
) {
131 uint32_t nt
= 0, nr
= 0;
132 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
137 char cmdp
= param_getchar(Cmd
, 0);
138 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
140 blockNo
= param_get8(Cmd
, 0);
141 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, 0}};
144 printf("-------------------------------------------------------------------------\n");
145 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
146 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
147 printf("-------------------------------------------------------------------------\n");
148 clock_t t1
= clock();
153 clearCommandBuffer();
169 printf("\naborted via keyboard!\n");
174 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
177 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
178 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
179 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
180 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
181 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
184 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
185 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
186 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
187 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
188 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
197 if (isOK
== -1 && par_list
== 0) {
198 if (!nonce2key_ex(uid
, nt
, nr
, ks_list
, &r_key
) ){
199 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
205 if (isOK
!= 1) return 1;
207 // execute original function from util nonce2key
208 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
210 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
211 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
215 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
220 unsigned long elapsed_time
= difftime(end
, start
);
222 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
226 int CmdHF14AMfWrBl(const char *Cmd
) {
229 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
230 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
235 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
236 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
240 blockNo
= param_get8(Cmd
, 0);
241 cmdp
= param_getchar(Cmd
, 1);
243 PrintAndLog("Key type must be A or B");
246 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
247 if (param_gethex(Cmd
, 2, key
, 12)) {
248 PrintAndLog("Key must include 12 HEX symbols");
251 if (param_gethex(Cmd
, 3, bldata
, 32)) {
252 PrintAndLog("Block data must include 32 HEX symbols");
255 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
256 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
258 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
259 memcpy(c
.d
.asBytes
, key
, 6);
260 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
261 clearCommandBuffer();
265 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
266 uint8_t isOK
= resp
.arg
[0] & 0xff;
267 PrintAndLog("isOk:%02x", isOK
);
269 PrintAndLog("Command execute timeout");
275 int CmdHF14AMfRdBl(const char *Cmd
) {
278 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
284 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
285 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
289 blockNo
= param_get8(Cmd
, 0);
290 cmdp
= param_getchar(Cmd
, 1);
292 PrintAndLog("Key type must be A or B");
295 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
296 if (param_gethex(Cmd
, 2, key
, 12)) {
297 PrintAndLog("Key must include 12 HEX symbols");
300 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
302 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
303 memcpy(c
.d
.asBytes
, key
, 6);
304 clearCommandBuffer();
308 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
309 uint8_t isOK
= resp
.arg
[0] & 0xff;
310 uint8_t *data
= resp
.d
.asBytes
;
313 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
315 PrintAndLog("isOk:%02x", isOK
);
317 PrintAndLog("Command execute timeout");
323 int CmdHF14AMfRdSc(const char *Cmd
) {
325 uint8_t sectorNo
= 0;
327 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
329 uint8_t *data
= NULL
;
333 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
334 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
338 sectorNo
= param_get8(Cmd
, 0);
340 PrintAndLog("Sector number must be less than 40");
343 cmdp
= param_getchar(Cmd
, 1);
344 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
345 PrintAndLog("Key type must be A or B");
348 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
349 if (param_gethex(Cmd
, 2, key
, 12)) {
350 PrintAndLog("Key must include 12 HEX symbols");
353 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
355 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
356 memcpy(c
.d
.asBytes
, key
, 6);
357 clearCommandBuffer();
362 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
363 isOK
= resp
.arg
[0] & 0xff;
364 data
= resp
.d
.asBytes
;
366 PrintAndLog("isOk:%02x", isOK
);
368 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
369 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
371 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
374 PrintAndLog("Command execute timeout");
380 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
384 return 32 * 4 + (sectorNo
- 32) * 16;
388 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
396 int CmdHF14AMfDump(const char *Cmd
) {
397 uint8_t sectorNo
, blockNo
;
401 uint8_t rights
[40][4];
402 uint8_t carddata
[256][16];
403 uint8_t numSectors
= 16;
410 char cmdp
= param_getchar(Cmd
, 0);
412 case '0' : numSectors
= 5; break;
414 case '\0': numSectors
= 16; break;
415 case '2' : numSectors
= 32; break;
416 case '4' : numSectors
= 40; break;
417 default: numSectors
= 16;
420 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
421 PrintAndLog("Usage: hf mf dump [card memory]");
422 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
424 PrintAndLog("Samples: hf mf dump");
425 PrintAndLog(" hf mf dump 4");
429 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
430 PrintAndLog("Could not find file dumpkeys.bin");
434 // Read keys A from file
436 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
437 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
438 if ( bytes_read
== 0) {
439 PrintAndLog("File reading error.");
445 // Read keys B from file
446 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
447 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
448 if ( bytes_read
== 0) {
449 PrintAndLog("File reading error.");
457 PrintAndLog("|-----------------------------------------|");
458 PrintAndLog("|------ Reading sector access bits...-----|");
459 PrintAndLog("|-----------------------------------------|");
461 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
462 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
463 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
464 clearCommandBuffer();
467 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
468 uint8_t isOK
= resp
.arg
[0] & 0xff;
469 uint8_t *data
= resp
.d
.asBytes
;
471 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
472 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
473 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
474 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
476 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
477 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
478 rights
[sectorNo
][3] = 0x01;
481 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
482 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
483 rights
[sectorNo
][3] = 0x01;
487 PrintAndLog("|-----------------------------------------|");
488 PrintAndLog("|----- Dumping all blocks to file... -----|");
489 PrintAndLog("|-----------------------------------------|");
492 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
493 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
494 bool received
= false;
496 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
497 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
498 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
499 clearCommandBuffer();
501 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
502 } else { // data block. Check if it can be read with key A or key B
503 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
504 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
505 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
506 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
508 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
509 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
511 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
512 } else { // key A would work
513 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
514 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
515 clearCommandBuffer();
517 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
522 isOK
= resp
.arg
[0] & 0xff;
523 uint8_t *data
= resp
.d
.asBytes
;
524 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
525 data
[0] = (keyA
[sectorNo
][0]);
526 data
[1] = (keyA
[sectorNo
][1]);
527 data
[2] = (keyA
[sectorNo
][2]);
528 data
[3] = (keyA
[sectorNo
][3]);
529 data
[4] = (keyA
[sectorNo
][4]);
530 data
[5] = (keyA
[sectorNo
][5]);
531 data
[10] = (keyB
[sectorNo
][0]);
532 data
[11] = (keyB
[sectorNo
][1]);
533 data
[12] = (keyB
[sectorNo
][2]);
534 data
[13] = (keyB
[sectorNo
][3]);
535 data
[14] = (keyB
[sectorNo
][4]);
536 data
[15] = (keyB
[sectorNo
][5]);
539 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
540 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
542 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
548 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
555 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
556 PrintAndLog("Could not create file name dumpdata.bin");
559 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
560 fwrite(carddata
, 1, 16*numblocks
, fout
);
562 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
568 int CmdHF14AMfRestore(const char *Cmd
) {
569 uint8_t sectorNo
,blockNo
;
571 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
572 uint8_t bldata
[16] = {0x00};
580 char cmdp
= param_getchar(Cmd
, 0);
582 case '0' : numSectors
= 5; break;
584 case '\0': numSectors
= 16; break;
585 case '2' : numSectors
= 32; break;
586 case '4' : numSectors
= 40; break;
587 default: numSectors
= 16;
590 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
591 PrintAndLog("Usage: hf mf restore [card memory]");
592 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
594 PrintAndLog("Samples: hf mf restore");
595 PrintAndLog(" hf mf restore 4");
599 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
600 PrintAndLog("Could not find file dumpkeys.bin");
605 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
606 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
607 if ( bytes_read
== 0) {
608 PrintAndLog("File reading error (dumpkeys.bin).");
614 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
615 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
616 if ( bytes_read
== 0) {
617 PrintAndLog("File reading error (dumpkeys.bin).");
625 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
626 PrintAndLog("Could not find file dumpdata.bin");
629 PrintAndLog("Restoring dumpdata.bin to card");
631 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
632 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
633 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
634 memcpy(c
.d
.asBytes
, key
, 6);
635 bytes_read
= fread(bldata
, 1, 16, fdump
);
636 if ( bytes_read
== 0) {
637 PrintAndLog("File reading error (dumpdata.bin).");
642 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
643 bldata
[0] = (keyA
[sectorNo
][0]);
644 bldata
[1] = (keyA
[sectorNo
][1]);
645 bldata
[2] = (keyA
[sectorNo
][2]);
646 bldata
[3] = (keyA
[sectorNo
][3]);
647 bldata
[4] = (keyA
[sectorNo
][4]);
648 bldata
[5] = (keyA
[sectorNo
][5]);
649 bldata
[10] = (keyB
[sectorNo
][0]);
650 bldata
[11] = (keyB
[sectorNo
][1]);
651 bldata
[12] = (keyB
[sectorNo
][2]);
652 bldata
[13] = (keyB
[sectorNo
][3]);
653 bldata
[14] = (keyB
[sectorNo
][4]);
654 bldata
[15] = (keyB
[sectorNo
][5]);
657 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
659 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
660 clearCommandBuffer();
664 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
665 uint8_t isOK
= resp
.arg
[0] & 0xff;
666 PrintAndLog("isOk:%02x", isOK
);
668 PrintAndLog("Command execute timeout");
677 int CmdHF14AMfNested(const char *Cmd
) {
678 int i
, j
, res
, iterations
;
679 sector
*e_sector
= NULL
;
682 uint8_t trgBlockNo
= 0;
683 uint8_t trgKeyType
= 0;
684 uint8_t SectorsCnt
= 0;
685 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
686 uint8_t keyBlock
[6*6];
688 bool transferToEml
= false;
690 bool createDumpFile
= false;
692 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
693 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
695 if (strlen(Cmd
)<3) return usage_hf14_nested();
698 cmdp
= param_getchar(Cmd
, 0);
699 blockNo
= param_get8(Cmd
, 1);
700 ctmp
= param_getchar(Cmd
, 2);
702 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
703 PrintAndLog("Key type must be A or B");
707 if (ctmp
!= 'A' && ctmp
!= 'a')
710 if (param_gethex(Cmd
, 3, key
, 12)) {
711 PrintAndLog("Key must include 12 HEX symbols");
715 if (cmdp
== 'o' || cmdp
== 'O') {
717 trgBlockNo
= param_get8(Cmd
, 4);
718 ctmp
= param_getchar(Cmd
, 5);
719 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
720 PrintAndLog("Target key type must be A or B");
723 if (ctmp
!= 'A' && ctmp
!= 'a')
728 case '0': SectorsCnt
= 05; break;
729 case '1': SectorsCnt
= 16; break;
730 case '2': SectorsCnt
= 32; break;
731 case '4': SectorsCnt
= 40; break;
732 default: SectorsCnt
= 16;
736 ctmp
= param_getchar(Cmd
, 4);
737 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
738 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
740 ctmp
= param_getchar(Cmd
, 6);
741 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
742 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
745 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
747 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
748 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
749 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
750 case -4 : PrintAndLog("No valid key found"); break;
752 key64
= bytes_to_num(keyBlock
, 6);
754 // transfer key to the emulator
756 uint8_t sectortrailer
;
757 if (trgBlockNo
< 32*4) { // 4 block sector
758 sectortrailer
= (trgBlockNo
& 0x03) + 3;
759 } else { // 16 block sector
760 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
762 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
765 num_to_bytes(key64
, 6, keyBlock
);
767 num_to_bytes(key64
, 6, &keyBlock
[10]);
768 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
771 default : PrintAndLog("Unknown Error.\n");
775 else { // ------------------------------------ multiple sectors working
776 clock_t t1
= clock();
777 unsigned long elapsed_time
;
781 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
782 if (e_sector
== NULL
) return 1;
784 //test current key and additional standard keys first
785 memcpy(keyBlock
, key
, 6);
786 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
787 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
788 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
789 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
790 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
792 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
793 for (i
= 0; i
< SectorsCnt
; i
++) {
794 for (j
= 0; j
< 2; j
++) {
795 if (e_sector
[i
].foundKey
[j
]) continue;
797 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
800 e_sector
[i
].Key
[j
] = key64
;
801 e_sector
[i
].foundKey
[j
] = TRUE
;
805 clock_t t2
= clock() - t1
;
807 elapsed_time
= difftime(end
, start
);
809 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
811 PrintAndLog("enter nested...");
815 bool calibrate
= true;
817 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
818 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
819 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
821 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
823 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
825 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
826 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
827 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
828 case -4 : //key not found
835 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
836 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
839 default : PrintAndLog("Unknown Error.\n");
849 elapsed_time
= difftime(end
, start
);
851 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
854 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
855 PrintAndLog("trying to read key B...");
856 for (i
= 0; i
< SectorsCnt
; i
++) {
857 // KEY A but not KEY B
858 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
860 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
862 PrintAndLog("Reading block %d", sectrail
);
864 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
865 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
866 clearCommandBuffer();
870 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
872 uint8_t isOK
= resp
.arg
[0] & 0xff;
875 uint8_t *data
= resp
.d
.asBytes
;
876 key64
= bytes_to_num(data
+10, 6);
878 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
879 e_sector
[i
].foundKey
[1] = TRUE
;
880 e_sector
[i
].Key
[1] = key64
;
887 printKeyTable( SectorsCnt
, e_sector
);
889 // transfer them to the emulator
891 for (i
= 0; i
< SectorsCnt
; i
++) {
892 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
893 if (e_sector
[i
].foundKey
[0])
894 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
895 if (e_sector
[i
].foundKey
[1])
896 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
897 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
902 if (createDumpFile
) {
903 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
904 PrintAndLog("Could not create file dumpkeys.bin");
908 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
909 for(i
=0; i
<SectorsCnt
; i
++) {
910 if (e_sector
[i
].foundKey
[0]){
911 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
912 fwrite ( tempkey
, 1, 6, fkeys
);
915 fwrite ( &standart
, 1, 6, fkeys
);
918 for(i
=0; i
<SectorsCnt
; i
++) {
919 if (e_sector
[i
].foundKey
[1]){
920 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
921 fwrite ( tempkey
, 1, 6, fkeys
);
924 fwrite ( &standart
, 1, 6, fkeys
);
935 int CmdHF14AMfNestedHard(const char *Cmd
) {
938 uint8_t trgBlockNo
= 0;
939 uint8_t trgKeyType
= 0;
940 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
941 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
944 ctmp
= param_getchar(Cmd
, 0);
945 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
946 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
948 bool know_target_key
= false;
949 bool nonce_file_read
= false;
950 bool nonce_file_write
= false;
954 if (ctmp
== 'R' || ctmp
== 'r') {
955 nonce_file_read
= true;
956 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
957 know_target_key
= true;
959 } else if (ctmp
== 'T' || ctmp
== 't') {
960 tests
= param_get32ex(Cmd
, 1, 100, 10);
962 blockNo
= param_get8(Cmd
, 0);
963 ctmp
= param_getchar(Cmd
, 1);
964 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
965 PrintAndLog("Key type must be A or B");
968 if (ctmp
!= 'A' && ctmp
!= 'a') {
972 if (param_gethex(Cmd
, 2, key
, 12)) {
973 PrintAndLog("Key must include 12 HEX symbols");
977 trgBlockNo
= param_get8(Cmd
, 3);
978 ctmp
= param_getchar(Cmd
, 4);
979 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
980 PrintAndLog("Target key type must be A or B");
983 if (ctmp
!= 'A' && ctmp
!= 'a') {
989 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
990 know_target_key
= true;
994 while ((ctmp
= param_getchar(Cmd
, i
))) {
995 if (ctmp
== 's' || ctmp
== 'S') {
997 } else if (ctmp
== 'w' || ctmp
== 'W') {
998 nonce_file_write
= true;
1000 PrintAndLog("Possible options are w and/or s");
1007 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 ",
1010 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1011 know_target_key
? "" : " (not set)",
1012 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1013 slow
? "Yes" : "No",
1016 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1020 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1021 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1030 int CmdHF14AMfChk(const char *Cmd
) {
1032 if (strlen(Cmd
)<3) return usage_hf14_chk();
1035 char filename
[FILE_PATH_SIZE
]={0};
1037 uint8_t *keyBlock
= NULL
, *p
;
1038 uint8_t stKeyBlock
= 20;
1040 sector
*e_sector
= NULL
;
1045 uint8_t blockNo
= 0;
1046 uint8_t SectorsCnt
= 1;
1047 uint8_t keyType
= 0;
1050 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1052 int transferToEml
= 0;
1053 int createDumpFile
= 0;
1055 keyBlock
= calloc(stKeyBlock
, 6);
1056 if (keyBlock
== NULL
) return 1;
1058 uint64_t defaultKeys
[] = {
1059 0xffffffffffff, // Default key (first key used by program if no user defined key)
1060 0x000000000000, // Blank key
1061 0xa0a1a2a3a4a5, // NFCForum MAD key
1073 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1075 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1076 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1079 if (param_getchar(Cmd
, 0)=='*') {
1081 switch(param_getchar(Cmd
+1, 0)) {
1082 case '0': SectorsCnt
= 5; break;
1083 case '1': SectorsCnt
= 16; break;
1084 case '2': SectorsCnt
= 32; break;
1085 case '4': SectorsCnt
= 40; break;
1086 default: SectorsCnt
= 16;
1089 blockNo
= param_get8(Cmd
, 0);
1092 ctmp
= param_getchar(Cmd
, 1);
1104 PrintAndLog("Key type must be A , B or ?");
1109 ctmp
= param_getchar(Cmd
, 2);
1110 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1111 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1113 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1114 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1115 if ( stKeyBlock
- keycnt
< 2) {
1116 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1118 PrintAndLog("Cannot allocate memory for Keys");
1124 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1125 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1126 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1129 // May be a dic file
1130 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1131 PrintAndLog("File name too long");
1136 if ( (f
= fopen( filename
, "r")) ) {
1137 while( fgets(buf
, sizeof(buf
), f
) ){
1138 if (strlen(buf
) < 12 || buf
[11] == '\n')
1141 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1143 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1145 if (!isxdigit(buf
[0])){
1146 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1152 if ( stKeyBlock
- keycnt
< 2) {
1153 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1155 PrintAndLog("Cannot allocate memory for defKeys");
1162 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1163 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1164 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1166 memset(buf
, 0, sizeof(buf
));
1170 PrintAndLog("File: %s: not found or locked.", filename
);
1179 PrintAndLog("No key specified, trying default keys");
1180 for (;keycnt
< defaultKeysSize
; keycnt
++)
1181 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1182 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1183 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1186 // initialize storage for found keys
1187 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1188 if (e_sector
== NULL
) {
1194 for(int i
= 0; i
< SectorsCnt
; ++i
){
1195 e_sector
[i
].Key
[0] = 0xffffffffffff;
1196 e_sector
[i
].Key
[1] = 0xffffffffffff;
1197 e_sector
[i
].foundKey
[0] = FALSE
;
1198 e_sector
[i
].foundKey
[1] = FALSE
;
1202 uint8_t trgKeyType
= 0;
1203 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1206 clock_t t1
= clock();
1211 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1214 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1216 // skip already found keys.
1217 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1219 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1222 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1224 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1226 e_sector
[i
].Key
[trgKeyType
] = key64
;
1227 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1231 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1236 unsigned long elapsed_time
= difftime(end
, start
);
1238 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1241 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1242 if ( keyType
!= 1 ) {
1243 PrintAndLog("testing to read key B...");
1244 for (i
= 0; i
< SectorsCnt
; i
++) {
1245 // KEY A but not KEY B
1246 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1248 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1250 PrintAndLog("Reading block %d", sectrail
);
1252 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1253 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1254 clearCommandBuffer();
1258 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1260 uint8_t isOK
= resp
.arg
[0] & 0xff;
1261 if (!isOK
) continue;
1263 uint8_t *data
= resp
.d
.asBytes
;
1264 key64
= bytes_to_num(data
+10, 6);
1266 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1267 e_sector
[i
].foundKey
[1] = 1;
1268 e_sector
[i
].Key
[1] = key64
;
1276 printKeyTable( SectorsCnt
, e_sector
);
1278 if (transferToEml
) {
1279 uint8_t block
[16] = {0x00};
1280 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1281 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1282 if (e_sector
[i
].foundKey
[0])
1283 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1284 if (e_sector
[i
].foundKey
[1])
1285 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1286 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1288 PrintAndLog("Found keys have been transferred to the emulator memory");
1291 if (createDumpFile
) {
1292 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1293 if (fkeys
== NULL
) {
1294 PrintAndLog("Could not create file dumpkeys.bin");
1299 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1301 for( i
=0; i
<SectorsCnt
; i
++) {
1302 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1303 fwrite ( tempkey
, 1, 6, fkeys
);
1305 for(i
=0; i
<SectorsCnt
; i
++) {
1306 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1307 fwrite ( tempkey
, 1, 6, fkeys
);
1310 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1319 int CmdHF14AMf1kSim(const char *Cmd
) {
1320 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1321 uint8_t exitAfterNReads
= 0;
1322 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1325 uint8_t cmdp
= param_getchar(Cmd
, 0);
1327 if (cmdp
== 'h' || cmdp
== 'H') return usage_hf14_mf1ksim();
1329 cmdp
= param_getchar(Cmd
, pnr
);
1330 if (cmdp
== 'u' || cmdp
== 'U') {
1331 param_gethex_ex(Cmd
, pnr
+1, uid
, &uidlen
);
1333 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1334 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1335 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1336 default: return usage_hf14_mf1ksim();
1341 cmdp
= param_getchar(Cmd
, pnr
);
1342 if (cmdp
== 'n' || cmdp
== 'N') {
1343 exitAfterNReads
= param_get8(Cmd
, pnr
+1);
1347 cmdp
= param_getchar(Cmd
, pnr
);
1348 if (cmdp
== 'i' || cmdp
== 'I' ) {
1349 flags
|= FLAG_INTERACTIVE
;
1353 cmdp
= param_getchar(Cmd
, pnr
);
1354 if (cmdp
== 'x' || cmdp
== 'X') {
1355 flags
|= FLAG_NR_AR_ATTACK
;
1358 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1359 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1364 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1365 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1366 clearCommandBuffer();
1369 if(flags
& FLAG_INTERACTIVE
) {
1373 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1375 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1377 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1378 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1380 memset(data
, 0x00, sizeof(data
));
1381 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1383 memcpy(data
, resp
.d
.asBytes
, len
);
1385 bool found
= tryMfk32(data
, &key
);
1386 found
^= tryMfk32_moebius(data
, &key
);
1393 int CmdHF14AMfSniff(const char *Cmd
){
1394 bool wantLogToFile
= FALSE
;
1395 bool wantDecrypt
= FALSE
;
1396 //bool wantSaveToEml = FALSE; TODO
1397 bool wantSaveToEmlFile
= FALSE
;
1407 uint8_t uid_len
= 0;
1408 uint8_t atqa
[2] = {0x00, 0x00};
1411 uint8_t *buf
= NULL
;
1412 uint16_t bufsize
= 0;
1413 uint8_t *bufPtr
= NULL
;
1414 uint16_t traceLen
= 0;
1416 memset(uid
, 0x00, sizeof(uid
));
1418 char ctmp
= param_getchar(Cmd
, 0);
1419 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1421 for (int i
= 0; i
< 4; i
++) {
1422 ctmp
= param_getchar(Cmd
, i
);
1423 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1424 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1425 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1426 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1429 printf("-------------------------------------------------------------------------\n");
1430 printf("Executing mifare sniffing command. \n");
1431 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1432 printf("Press the key on pc keyboard to abort the client.\n");
1433 printf("-------------------------------------------------------------------------\n");
1435 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1436 clearCommandBuffer();
1444 tmpchar
= getchar();
1446 printf("\naborted via keyboard!\n");
1451 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1452 res
= resp
.arg
[0] & 0xff;
1453 traceLen
= resp
.arg
[1];
1462 if (res
== 1) { // there is (more) data to be transferred
1463 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1464 if (traceLen
> bufsize
) {
1466 if (buf
== NULL
) // not yet allocated
1467 p
= malloc(traceLen
);
1468 else // need more memory
1469 p
= realloc(buf
, traceLen
);
1472 PrintAndLog("Cannot allocate memory for trace");
1480 memset(buf
, 0x00, traceLen
);
1482 if (bufPtr
== NULL
) {
1483 PrintAndLog("Cannot allocate memory for trace");
1487 // what happens if LEN is bigger then TRACELEN --iceman
1488 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1493 if (res
== 2) { // received all data, start displaying
1494 blockLen
= bufPtr
- buf
;
1497 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1498 while (bufPtr
- buf
< blockLen
) {
1499 bufPtr
+= 6; // skip (void) timing information
1500 len
= *((uint16_t *)bufPtr
);
1508 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1509 memcpy(uid
, bufPtr
+ 2, 10);
1510 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1511 switch (atqa
[0] & 0xC0) {
1512 case 0x80: uid_len
= 10; break;
1513 case 0x40: uid_len
= 7; break;
1514 default: uid_len
= 4; break;
1517 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1518 sprint_hex(uid
, uid_len
),
1522 if (wantLogToFile
|| wantDecrypt
) {
1523 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1524 AddLogCurrentDT(logHexFileName
);
1527 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1529 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1531 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1533 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1537 bufPtr
+= ((len
-1)/8+1); // ignore parity
1548 int CmdHF14AMfDbg(const char *Cmd
) {
1550 char ctmp
= param_getchar(Cmd
, 0);
1551 if (strlen(Cmd
) < 1 || ctmp
== 'h'|| ctmp
== 'H') return usage_hf14_dbg();
1553 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1554 if (dbgMode
> 4) return usage_hf14_dbg();
1556 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1561 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1562 PrintAndLog("|---|----------------|---|----------------|---|");
1563 PrintAndLog("|sec|key A |res|key B |res|");
1564 PrintAndLog("|---|----------------|---|----------------|---|");
1565 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1566 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1567 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1568 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1571 PrintAndLog("|---|----------------|---|----------------|---|");
1574 // EMULATOR COMMANDS
1576 int CmdHF14AMfEGet(const char *Cmd
)
1578 uint8_t blockNo
= 0;
1579 uint8_t data
[16] = {0x00};
1581 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1582 PrintAndLog("Usage: hf mf eget <block number>");
1583 PrintAndLog(" sample: hf mf eget 0 ");
1587 blockNo
= param_get8(Cmd
, 0);
1590 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1591 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1593 PrintAndLog("Command execute timeout");
1599 int CmdHF14AMfEClear(const char *Cmd
)
1601 if (param_getchar(Cmd
, 0) == 'h') {
1602 PrintAndLog("Usage: hf mf eclr");
1603 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1607 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1612 int CmdHF14AMfESet(const char *Cmd
)
1614 uint8_t memBlock
[16];
1615 uint8_t blockNo
= 0;
1617 memset(memBlock
, 0x00, sizeof(memBlock
));
1619 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1620 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1621 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1625 blockNo
= param_get8(Cmd
, 0);
1627 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1628 PrintAndLog("block data must include 32 HEX symbols");
1633 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1634 memcpy(c
.d
.asBytes
, memBlock
, 16);
1639 int CmdHF14AMfELoad(const char *Cmd
)
1642 char filename
[FILE_PATH_SIZE
];
1643 char *fnameptr
= filename
;
1644 char buf
[64] = {0x00};
1645 uint8_t buf8
[64] = {0x00};
1646 int i
, len
, blockNum
, numBlocks
;
1647 int nameParamNo
= 1;
1648 uint8_t blockWidth
= 32;
1649 char ctmp
= param_getchar(Cmd
, 0);
1651 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1652 PrintAndLog("It loads emul dump from the file `filename.eml`");
1653 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1654 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1656 PrintAndLog(" sample: hf mf eload filename");
1657 PrintAndLog(" hf mf eload 4 filename");
1662 case '0' : numBlocks
= 5*4; break;
1664 case '\0': numBlocks
= 16*4; break;
1665 case '2' : numBlocks
= 32*4; break;
1666 case '4' : numBlocks
= 256; break;
1667 case 'U' : // fall through
1668 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1674 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1675 if (numblk2
> 0) numBlocks
= numblk2
;
1677 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1679 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1683 sprintf(fnameptr
, ".eml");
1686 f
= fopen(filename
, "r");
1688 PrintAndLog("File %s not found or locked", filename
);
1694 memset(buf
, 0, sizeof(buf
));
1696 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1698 if (blockNum
>= numBlocks
) break;
1700 PrintAndLog("File reading error.");
1705 if (strlen(buf
) < blockWidth
){
1706 if(strlen(buf
) && feof(f
))
1708 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1713 for (i
= 0; i
< blockWidth
; i
+= 2) {
1714 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1716 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1717 PrintAndLog("Cant set emul block: %3d", blockNum
);
1724 if (blockNum
>= numBlocks
) break;
1729 if ((blockNum
!= numBlocks
)) {
1730 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1733 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1737 int CmdHF14AMfESave(const char *Cmd
)
1740 char filename
[FILE_PATH_SIZE
];
1741 char * fnameptr
= filename
;
1743 int i
, j
, len
, numBlocks
;
1744 int nameParamNo
= 1;
1746 memset(filename
, 0, sizeof(filename
));
1747 memset(buf
, 0, sizeof(buf
));
1749 char ctmp
= param_getchar(Cmd
, 0);
1751 if ( ctmp
== 'h' || ctmp
== 'H') {
1752 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1753 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1754 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1756 PrintAndLog(" sample: hf mf esave ");
1757 PrintAndLog(" hf mf esave 4");
1758 PrintAndLog(" hf mf esave 4 filename");
1763 case '0' : numBlocks
= 5*4; break;
1765 case '\0': numBlocks
= 16*4; break;
1766 case '2' : numBlocks
= 32*4; break;
1767 case '4' : numBlocks
= 256; break;
1774 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1776 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1778 // user supplied filename?
1780 // get filename (UID from memory)
1781 if (mfEmlGetMem(buf
, 0, 1)) {
1782 PrintAndLog("Can\'t get UID from block: %d", 0);
1783 len
= sprintf(fnameptr
, "dump");
1787 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1788 sprintf(fnameptr
, "%02X", buf
[j
]);
1794 // add file extension
1795 sprintf(fnameptr
, ".eml");
1798 f
= fopen(filename
, "w+");
1801 PrintAndLog("Can't open file %s ", filename
);
1806 for (i
= 0; i
< numBlocks
; i
++) {
1807 if (mfEmlGetMem(buf
, i
, 1)) {
1808 PrintAndLog("Cant get block: %d", i
);
1811 for (j
= 0; j
< 16; j
++)
1812 fprintf(f
, "%02X", buf
[j
]);
1817 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1822 int CmdHF14AMfECFill(const char *Cmd
)
1824 uint8_t keyType
= 0;
1825 uint8_t numSectors
= 16;
1827 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1828 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1829 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1831 PrintAndLog("samples: hf mf ecfill A");
1832 PrintAndLog(" hf mf ecfill A 4");
1833 PrintAndLog("Read card and transfer its data to emulator memory.");
1834 PrintAndLog("Keys must be laid in the emulator memory. \n");
1838 char ctmp
= param_getchar(Cmd
, 0);
1839 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1840 PrintAndLog("Key type must be A or B");
1843 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1845 ctmp
= param_getchar(Cmd
, 1);
1847 case '0' : numSectors
= 5; break;
1849 case '\0': numSectors
= 16; break;
1850 case '2' : numSectors
= 32; break;
1851 case '4' : numSectors
= 40; break;
1852 default: numSectors
= 16;
1855 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1856 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1861 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1866 uint64_t keyA
, keyB
;
1868 char cmdp
= param_getchar(Cmd
, 0);
1870 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1871 PrintAndLog("It prints the keys loaded in the emulator memory");
1872 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1873 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1875 PrintAndLog(" sample: hf mf ekeyprn 1");
1880 case '0' : numSectors
= 5; break;
1882 case '\0': numSectors
= 16; break;
1883 case '2' : numSectors
= 32; break;
1884 case '4' : numSectors
= 40; break;
1885 default: numSectors
= 16;
1888 PrintAndLog("|---|----------------|----------------|");
1889 PrintAndLog("|sec|key A |key B |");
1890 PrintAndLog("|---|----------------|----------------|");
1891 for (i
= 0; i
< numSectors
; i
++) {
1892 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1893 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1896 keyA
= bytes_to_num(data
, 6);
1897 keyB
= bytes_to_num(data
+ 10, 6);
1898 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1900 PrintAndLog("|---|----------------|----------------|");
1905 // CHINESE MAGIC COMMANDS
1907 int CmdHF14AMfCSetUID(const char *Cmd
) {
1908 uint8_t wipeCard
= 0;
1909 uint8_t uid
[8] = {0x00};
1910 uint8_t oldUid
[8] = {0x00};
1911 uint8_t atqa
[2] = {0x00};
1912 uint8_t sak
[1] = {0x00};
1913 uint8_t atqaPresent
= 1;
1918 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1919 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1920 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1922 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1924 PrintAndLog("sample: hf mf csetuid 01020304");
1925 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
1929 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1930 PrintAndLog("UID must include 8 HEX symbols");
1935 ctmp
= param_getchar(Cmd
, argi
);
1936 if (ctmp
== 'w' || ctmp
== 'W') {
1942 if (param_getchar(Cmd
, argi
)) {
1943 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1944 PrintAndLog("ATQA must include 4 HEX symbols");
1948 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1949 PrintAndLog("SAK must include 2 HEX symbols");
1958 ctmp
= param_getchar(Cmd
, argi
);
1959 if (ctmp
== 'w' || ctmp
== 'W') {
1964 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1966 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
1968 PrintAndLog("Can't set UID. error=%d", res
);
1972 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1973 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1977 int CmdHF14AMfCSetBlk(const char *Cmd
) {
1978 uint8_t block
[16] = {0x00};
1979 uint8_t blockNo
= 0;
1980 uint8_t params
= MAGIC_SINGLE
;
1983 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1984 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1985 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1986 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1987 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1991 blockNo
= param_get8(Cmd
, 0);
1993 if (param_gethex(Cmd
, 1, block
, 32)) {
1994 PrintAndLog("block data must include 32 HEX symbols");
1998 char ctmp
= param_getchar(Cmd
, 2);
1999 if (ctmp
== 'w' || ctmp
== 'W')
2000 params
|= MAGIC_WIPE
;
2002 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2004 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2006 PrintAndLog("Can't write block. error=%d", res
);
2012 int CmdHF14AMfCLoad(const char *Cmd
) {
2014 char filename
[FILE_PATH_SIZE
];
2015 char * fnameptr
= filename
;
2016 char buf
[64] = {0x00};
2017 uint8_t buf8
[64] = {0x00};
2018 uint8_t fillFromEmulator
= 0;
2019 int i
, len
, blockNum
, flags
=0;
2021 memset(filename
, 0, sizeof(filename
));
2023 char ctmp
= param_getchar(Cmd
, 0);
2025 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2026 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2027 PrintAndLog("or from emulator memory (option `e`)");
2028 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2029 PrintAndLog(" or: hf mf cload e ");
2030 PrintAndLog(" sample: hf mf cload filename");
2034 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2036 if (fillFromEmulator
) {
2037 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2038 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2039 PrintAndLog("Cant get block: %d", blockNum
);
2042 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2043 if (blockNum
== 1) flags
= 0; // just write
2044 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2046 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2047 PrintAndLog("Cant set magic card block: %d", blockNum
);
2054 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2056 memcpy(filename
, Cmd
, len
);
2059 sprintf(fnameptr
, ".eml");
2062 f
= fopen(filename
, "r");
2064 PrintAndLog("File not found or locked.");
2071 memset(buf
, 0, sizeof(buf
));
2073 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2075 PrintAndLog("File reading error.");
2079 if (strlen(buf
) < 32) {
2080 if(strlen(buf
) && feof(f
))
2082 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2086 for (i
= 0; i
< 32; i
+= 2)
2087 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2089 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2090 if (blockNum
== 1) flags
= 0; // just write
2091 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2093 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2094 PrintAndLog("Can't set magic card block: %d", blockNum
);
2100 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2105 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2106 PrintAndLog("File content error. There must be 64 blocks");
2109 PrintAndLog("Loaded from file: %s", filename
);
2115 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2117 uint8_t blockNo
= 0;
2119 memset(data
, 0x00, sizeof(data
));
2120 char ctmp
= param_getchar(Cmd
, 0);
2122 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2123 PrintAndLog("Usage: hf mf cgetblk <block number>");
2124 PrintAndLog("sample: hf mf cgetblk 1");
2125 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2129 blockNo
= param_get8(Cmd
, 0);
2131 PrintAndLog("--block number:%2d ", blockNo
);
2133 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2135 PrintAndLog("Can't read block. error=%d", res
);
2139 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2143 int CmdHF14AMfCGetSc(const char *Cmd
) {
2145 uint8_t sectorNo
= 0;
2147 memset(data
, 0x00, sizeof(data
));
2148 char ctmp
= param_getchar(Cmd
, 0);
2150 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2151 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2152 PrintAndLog("sample: hf mf cgetsc 0");
2153 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2157 sectorNo
= param_get8(Cmd
, 0);
2158 if (sectorNo
> 15) {
2159 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2163 PrintAndLog("--sector number:%d ", sectorNo
);
2164 PrintAndLog("block | data");
2166 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2167 for (i
= 0; i
< 4; i
++) {
2168 if (i
== 1) flags
= 0;
2169 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2171 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2173 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2176 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2181 int CmdHF14AMfCSave(const char *Cmd
) {
2184 char filename
[FILE_PATH_SIZE
];
2185 char * fnameptr
= filename
;
2186 uint8_t fillFromEmulator
= 0;
2188 int i
, j
, len
, flags
;
2190 memset(filename
, 0, sizeof(filename
));
2191 memset(buf
, 0, sizeof(buf
));
2192 char ctmp
= param_getchar(Cmd
, 0);
2194 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2195 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2196 PrintAndLog("or into emulator memory (option `e`)");
2197 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2198 PrintAndLog(" sample: hf mf esave ");
2199 PrintAndLog(" hf mf esave filename");
2200 PrintAndLog(" hf mf esave e \n");
2203 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2205 if (fillFromEmulator
) {
2206 // put into emulator
2207 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2208 for (i
= 0; i
< 16 * 4; i
++) {
2209 if (i
== 1) flags
= 0;
2210 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2212 if (mfCGetBlock(i
, buf
, flags
)) {
2213 PrintAndLog("Cant get block: %d", i
);
2217 if (mfEmlSetMem(buf
, i
, 1)) {
2218 PrintAndLog("Cant set emul block: %d", i
);
2225 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2227 // get filename based on UID
2230 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2231 PrintAndLog("Cant get block: %d", 0);
2232 len
= sprintf(fnameptr
, "dump");
2235 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2236 sprintf(fnameptr
, "%02x", buf
[j
]);
2239 memcpy(filename
, Cmd
, len
);
2243 // add .eml extension
2244 sprintf(fnameptr
, ".eml");
2247 f
= fopen(filename
, "w+");
2250 PrintAndLog("File not found or locked.");
2255 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2256 for (i
= 0; i
< 16 * 4; i
++) {
2257 if (i
== 1) flags
= 0;
2258 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2260 if (mfCGetBlock(i
, buf
, flags
)) {
2261 PrintAndLog("Cant get block: %d", i
);
2264 for (j
= 0; j
< 16; j
++)
2265 fprintf(f
, "%02x", buf
[j
]);
2270 PrintAndLog("Saved to file: %s", filename
);
2275 //needs nt, ar, at, Data to decrypt
2276 int CmdHf14MfDecryptBytes(const char *Cmd
){
2278 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2279 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2280 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2283 param_gethex_ex(Cmd
, 3, data
, &len
);
2286 int limit
= sizeof(data
) / 2;
2291 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2294 static command_t CommandTable
[] = {
2295 {"help", CmdHelp
, 1, "This help"},
2296 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2297 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2298 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2299 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2300 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2301 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2302 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2303 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2304 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2305 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2306 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2307 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2308 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2309 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2310 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2311 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2312 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2313 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2314 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2315 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2316 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2317 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2318 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2319 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2320 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2321 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2322 {NULL
, NULL
, 0, NULL
}
2325 int CmdHFMF(const char *Cmd
) {
2326 clearCommandBuffer();
2327 CmdsParse(CommandTable
, Cmd
);
2331 int CmdHelp(const char *Cmd
) {
2332 CmdsHelp(CommandTable
);