]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
14 int usage_hf14_mifare(void){
15 PrintAndLog("Usage: hf mf mifare [h] <block number> <A|B>");
16 PrintAndLog("options:");
17 PrintAndLog(" h this help");
18 PrintAndLog(" <block number> (Optional) target other block");
19 PrintAndLog(" <A|B> (optional) target key type");
20 PrintAndLog("samples:");
21 PrintAndLog(" hf mf mifare");
22 PrintAndLog(" hf mf mifare 16");
23 PrintAndLog(" hf mf mifare 16 B");
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(" e (Optional) Fill simulator keys from what we crack");
35 PrintAndLog(" v (Optional) Show maths used for cracking reader. Useful for debugging.");
36 PrintAndLog("samples:");
37 PrintAndLog(" hf mf sim u 0a0a0a0a");
38 PrintAndLog(" hf mf sim u 11223344556677");
39 PrintAndLog(" hf mf sim u 112233445566778899AA");
42 int usage_hf14_dbg(void){
43 PrintAndLog("Usage: hf mf dbg [h] <debug level>");
44 PrintAndLog("options:");
45 PrintAndLog(" h this help");
46 PrintAndLog(" <debug level> (Optional) see list for valid levels");
47 PrintAndLog(" 0 - no debug messages");
48 PrintAndLog(" 1 - error messages");
49 PrintAndLog(" 2 - plus information messages");
50 PrintAndLog(" 3 - plus debug messages");
51 PrintAndLog(" 4 - print even debug messages in timing critical functions");
52 PrintAndLog(" Note: this option therefore may cause malfunction itself");
53 PrintAndLog("samples:");
54 PrintAndLog(" hf mf dbg 3");
57 int usage_hf14_sniff(void){
58 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
59 PrintAndLog("Usage: hf mf sniff [h] [l] [d] [f]");
60 PrintAndLog("options:");
61 PrintAndLog(" h this help");
62 PrintAndLog(" l save encrypted sequence to logfile `uid.log`");
63 PrintAndLog(" d decrypt sequence and put it to log file `uid.log`");
64 // PrintAndLog(" n/a e decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
65 PrintAndLog(" f decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
66 PrintAndLog("sample:");
67 PrintAndLog(" hf mf sniff l d f");
70 int usage_hf14_nested(void){
71 PrintAndLog("Usage:");
72 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
73 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
74 PrintAndLog(" <target block number> <target key A/B> [t]");
75 PrintAndLog("options:");
76 PrintAndLog(" h this help");
77 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
78 PrintAndLog(" t transfer keys into emulator memory");
79 PrintAndLog(" d write keys to binary file");
81 PrintAndLog("samples:");
82 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF ");
83 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF t ");
84 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF d ");
85 PrintAndLog(" hf mf nested o 0 A FFFFFFFFFFFF 4 A");
88 int usage_hf14_hardnested(void){
89 PrintAndLog("Usage:");
90 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
91 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
92 PrintAndLog(" or hf mf hardnested r [known target key]");
94 PrintAndLog("options:");
95 PrintAndLog(" h this help");
96 PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
97 PrintAndLog(" s slower acquisition (required by some non standard cards)");
98 PrintAndLog(" r read nonces.bin and start attack");
99 PrintAndLog(" t tests?");
101 PrintAndLog("samples:");
102 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
103 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
104 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
105 PrintAndLog(" hf mf hardnested r");
106 PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
108 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
109 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
112 int usage_hf14_chk(void){
113 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
114 PrintAndLog("options:");
115 PrintAndLog(" h this help");
116 PrintAndLog(" * all sectors based on card memory, other values then below defaults to 1k");
117 PrintAndLog(" 0 - MINI(320 bytes)");
118 PrintAndLog(" 1 - 1K");
119 PrintAndLog(" 2 - 2K");
120 PrintAndLog(" 4 - 4K");
121 PrintAndLog(" d write keys to binary file");
122 PrintAndLog(" t write keys to emulator memory\n");
124 PrintAndLog("samples:");
125 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic -- target block 0, Key A");
126 PrintAndLog(" hf mf chk *1 ? t -- target all blocks, all keys, 1K, write to emul");
127 PrintAndLog(" hf mf chk *1 ? d -- target all blocks, all keys, 1K, write to file");
130 int usage_hf14_keybrute(void){
131 PrintAndLog("J_Run's 2nd phase of multiple sector nested authentication key recovery");
132 PrintAndLog("You have a known 4 last bytes of a key recovered with mf_nonce_brute tool.");
133 PrintAndLog("First 2 bytes of key will be bruteforced");
135 PrintAndLog("Usage: hf mf keybrute [h] <block number> <A|B> <key>");
136 PrintAndLog("options:");
137 PrintAndLog(" h this help");
138 PrintAndLog(" <block number> target block number");
139 PrintAndLog(" <A|B> target key type");
140 PrintAndLog(" <key> candidate key from mf_nonce_brute tool");
141 PrintAndLog("samples:");
142 PrintAndLog(" hf mf keybrute 1 A 000011223344");
146 int CmdHF14AMifare(const char *Cmd
) {
148 uint32_t nt
= 0, nr
= 0;
149 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
152 uint8_t blockNo
= 0, keytype
= MIFARE_AUTH_KEYA
;
154 char cmdp
= param_getchar(Cmd
, 0);
155 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
157 blockNo
= param_get8(Cmd
, 0);
159 cmdp
= param_getchar(Cmd
, 1);
160 if (cmdp
== 'B' || cmdp
== 'b')
161 keytype
= MIFARE_AUTH_KEYB
;
163 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, keytype
}};
166 printf("-------------------------------------------------------------------------\n");
167 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
168 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
169 printf("-------------------------------------------------------------------------\n");
170 clock_t t1
= clock();
175 clearCommandBuffer();
191 printf("\naborted via keyboard!\n");
196 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
199 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
200 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
201 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
202 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
203 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
206 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
207 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
208 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
209 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
210 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
218 if (isOK
!= 1) return 1;
220 if (par_list
== 0 && ks_list
!= 0) {
221 // this special attack when parities is zero, uses checkkeys. Which now with block/keytype option also needs.
222 // but it uses 0|1 instead of 0x60|0x61...
223 if (nonce2key_ex(blockNo
, keytype
- 0x60 , uid
, nt
, nr
, ks_list
, &r_key
) ){
224 PrintAndLog("Trying again with a different reader nonce...");
228 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
233 // execute original function from util nonce2key
234 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
236 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
237 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
242 // nonce2key found a candidate key. Lets verify it.
243 uint8_t keyblock
[] = {0,0,0,0,0,0};
244 num_to_bytes(r_key
, 6, keyblock
);
246 int res
= mfCheckKeys(blockNo
, keytype
- 0x60 , false, 1, keyblock
, &key64
);
248 PrintAndLog("Candidate Key found (%012"llx
") - Test authentication failed. [%d] Restarting darkside attack", r_key
, res
);
251 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
256 unsigned long elapsed_time
= difftime(end
, start
);
258 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
262 int CmdHF14AMfWrBl(const char *Cmd
) {
265 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
266 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
271 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
272 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
276 blockNo
= param_get8(Cmd
, 0);
277 cmdp
= param_getchar(Cmd
, 1);
279 PrintAndLog("Key type must be A or B");
282 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
283 if (param_gethex(Cmd
, 2, key
, 12)) {
284 PrintAndLog("Key must include 12 HEX symbols");
287 if (param_gethex(Cmd
, 3, bldata
, 32)) {
288 PrintAndLog("Block data must include 32 HEX symbols");
291 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
292 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
294 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
295 memcpy(c
.d
.asBytes
, key
, 6);
296 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
297 clearCommandBuffer();
301 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
302 uint8_t isOK
= resp
.arg
[0] & 0xff;
303 PrintAndLog("isOk:%02x", isOK
);
305 PrintAndLog("Command execute timeout");
311 int CmdHF14AMfRdBl(const char *Cmd
) {
314 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
320 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
321 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
325 blockNo
= param_get8(Cmd
, 0);
326 cmdp
= param_getchar(Cmd
, 1);
328 PrintAndLog("Key type must be A or B");
331 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
332 if (param_gethex(Cmd
, 2, key
, 12)) {
333 PrintAndLog("Key must include 12 HEX symbols");
336 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
338 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
339 memcpy(c
.d
.asBytes
, key
, 6);
340 clearCommandBuffer();
344 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
345 uint8_t isOK
= resp
.arg
[0] & 0xff;
346 uint8_t *data
= resp
.d
.asBytes
;
349 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
351 PrintAndLog("isOk:%02x", isOK
);
353 PrintAndLog("Command execute timeout");
359 int CmdHF14AMfRdSc(const char *Cmd
) {
361 uint8_t sectorNo
= 0;
363 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
365 uint8_t *data
= NULL
;
369 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
370 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
374 sectorNo
= param_get8(Cmd
, 0);
376 PrintAndLog("Sector number must be less than 40");
379 cmdp
= param_getchar(Cmd
, 1);
380 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
381 PrintAndLog("Key type must be A or B");
384 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
385 if (param_gethex(Cmd
, 2, key
, 12)) {
386 PrintAndLog("Key must include 12 HEX symbols");
389 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
391 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
392 memcpy(c
.d
.asBytes
, key
, 6);
393 clearCommandBuffer();
398 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
399 isOK
= resp
.arg
[0] & 0xff;
400 data
= resp
.d
.asBytes
;
402 PrintAndLog("isOk:%02x", isOK
);
404 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
405 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
407 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
410 PrintAndLog("Command execute timeout");
416 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
420 return 32 * 4 + (sectorNo
- 32) * 16;
424 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
432 int CmdHF14AMfDump(const char *Cmd
) {
433 uint8_t sectorNo
, blockNo
;
437 uint8_t rights
[40][4];
438 uint8_t carddata
[256][16];
439 uint8_t numSectors
= 16;
446 char cmdp
= param_getchar(Cmd
, 0);
448 case '0' : numSectors
= 5; break;
450 case '\0': numSectors
= 16; break;
451 case '2' : numSectors
= 32; break;
452 case '4' : numSectors
= 40; break;
453 default: numSectors
= 16;
456 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
457 PrintAndLog("Usage: hf mf dump [card memory]");
458 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
460 PrintAndLog("Samples: hf mf dump");
461 PrintAndLog(" hf mf dump 4");
465 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
466 PrintAndLog("Could not find file dumpkeys.bin");
470 // Read keys A from file
472 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
473 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
474 if ( bytes_read
== 0) {
475 PrintAndLog("File reading error.");
482 // Read keys B from file
483 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
484 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
485 if ( bytes_read
== 0) {
486 PrintAndLog("File reading error.");
496 PrintAndLog("|-----------------------------------------|");
497 PrintAndLog("|------ Reading sector access bits...-----|");
498 PrintAndLog("|-----------------------------------------|");
500 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
501 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
502 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
503 clearCommandBuffer();
506 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
507 uint8_t isOK
= resp
.arg
[0] & 0xff;
508 uint8_t *data
= resp
.d
.asBytes
;
510 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
511 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
512 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
513 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
515 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
516 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
517 rights
[sectorNo
][3] = 0x01;
520 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
521 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
522 rights
[sectorNo
][3] = 0x01;
526 PrintAndLog("|-----------------------------------------|");
527 PrintAndLog("|----- Dumping all blocks to file... -----|");
528 PrintAndLog("|-----------------------------------------|");
531 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
532 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
533 bool received
= false;
535 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
536 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
537 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
538 clearCommandBuffer();
540 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
541 } else { // data block. Check if it can be read with key A or key B
542 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
543 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
544 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
545 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
547 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
548 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
550 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
551 } else { // key A would work
552 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
553 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
554 clearCommandBuffer();
556 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
561 isOK
= resp
.arg
[0] & 0xff;
562 uint8_t *data
= resp
.d
.asBytes
;
563 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
564 data
[0] = (keyA
[sectorNo
][0]);
565 data
[1] = (keyA
[sectorNo
][1]);
566 data
[2] = (keyA
[sectorNo
][2]);
567 data
[3] = (keyA
[sectorNo
][3]);
568 data
[4] = (keyA
[sectorNo
][4]);
569 data
[5] = (keyA
[sectorNo
][5]);
570 data
[10] = (keyB
[sectorNo
][0]);
571 data
[11] = (keyB
[sectorNo
][1]);
572 data
[12] = (keyB
[sectorNo
][2]);
573 data
[13] = (keyB
[sectorNo
][3]);
574 data
[14] = (keyB
[sectorNo
][4]);
575 data
[15] = (keyB
[sectorNo
][5]);
578 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
579 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
581 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
587 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
594 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
595 PrintAndLog("Could not create file name dumpdata.bin");
598 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
599 fwrite(carddata
, 1, 16*numblocks
, fout
);
602 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
608 int CmdHF14AMfRestore(const char *Cmd
) {
609 uint8_t sectorNo
,blockNo
;
611 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
612 uint8_t bldata
[16] = {0x00};
620 char cmdp
= param_getchar(Cmd
, 0);
622 case '0' : numSectors
= 5; break;
624 case '\0': numSectors
= 16; break;
625 case '2' : numSectors
= 32; break;
626 case '4' : numSectors
= 40; break;
627 default: numSectors
= 16;
630 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
631 PrintAndLog("Usage: hf mf restore [card memory]");
632 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
634 PrintAndLog("Samples: hf mf restore");
635 PrintAndLog(" hf mf restore 4");
639 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
640 PrintAndLog("Could not find file dumpkeys.bin");
645 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
646 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
647 if ( bytes_read
== 0) {
648 PrintAndLog("File reading error (dumpkeys.bin).");
655 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
656 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
657 if ( bytes_read
== 0) {
658 PrintAndLog("File reading error (dumpkeys.bin).");
667 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
668 PrintAndLog("Could not find file dumpdata.bin");
671 PrintAndLog("Restoring dumpdata.bin to card");
673 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
674 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
675 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
676 memcpy(c
.d
.asBytes
, key
, 6);
677 bytes_read
= fread(bldata
, 1, 16, fdump
);
678 if ( bytes_read
== 0) {
679 PrintAndLog("File reading error (dumpdata.bin).");
685 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
686 bldata
[0] = (keyA
[sectorNo
][0]);
687 bldata
[1] = (keyA
[sectorNo
][1]);
688 bldata
[2] = (keyA
[sectorNo
][2]);
689 bldata
[3] = (keyA
[sectorNo
][3]);
690 bldata
[4] = (keyA
[sectorNo
][4]);
691 bldata
[5] = (keyA
[sectorNo
][5]);
692 bldata
[10] = (keyB
[sectorNo
][0]);
693 bldata
[11] = (keyB
[sectorNo
][1]);
694 bldata
[12] = (keyB
[sectorNo
][2]);
695 bldata
[13] = (keyB
[sectorNo
][3]);
696 bldata
[14] = (keyB
[sectorNo
][4]);
697 bldata
[15] = (keyB
[sectorNo
][5]);
700 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
702 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
703 clearCommandBuffer();
707 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
708 uint8_t isOK
= resp
.arg
[0] & 0xff;
709 PrintAndLog("isOk:%02x", isOK
);
711 PrintAndLog("Command execute timeout");
721 int CmdHF14AMfNested(const char *Cmd
) {
722 int i
, j
, res
, iterations
;
723 sector
*e_sector
= NULL
;
726 uint8_t trgBlockNo
= 0;
727 uint8_t trgKeyType
= 0;
728 uint8_t SectorsCnt
= 0;
729 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
730 uint8_t keyBlock
[6*6];
732 bool transferToEml
= false;
734 bool createDumpFile
= false;
736 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
737 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
739 if (strlen(Cmd
)<3) return usage_hf14_nested();
742 cmdp
= param_getchar(Cmd
, 0);
743 blockNo
= param_get8(Cmd
, 1);
744 ctmp
= param_getchar(Cmd
, 2);
746 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
747 PrintAndLog("Key type must be A or B");
751 if (ctmp
!= 'A' && ctmp
!= 'a')
754 if (param_gethex(Cmd
, 3, key
, 12)) {
755 PrintAndLog("Key must include 12 HEX symbols");
759 if (cmdp
== 'o' || cmdp
== 'O') {
761 trgBlockNo
= param_get8(Cmd
, 4);
762 ctmp
= param_getchar(Cmd
, 5);
763 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
764 PrintAndLog("Target key type must be A or B");
767 if (ctmp
!= 'A' && ctmp
!= 'a')
772 case '0': SectorsCnt
= 05; break;
773 case '1': SectorsCnt
= 16; break;
774 case '2': SectorsCnt
= 32; break;
775 case '4': SectorsCnt
= 40; break;
776 default: SectorsCnt
= 16;
780 ctmp
= param_getchar(Cmd
, 4);
781 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
782 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
784 ctmp
= param_getchar(Cmd
, 6);
785 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
786 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
789 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
791 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
792 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
793 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
794 case -4 : PrintAndLog("No valid key found"); break;
796 key64
= bytes_to_num(keyBlock
, 6);
798 // transfer key to the emulator
800 uint8_t sectortrailer
;
801 if (trgBlockNo
< 32*4) { // 4 block sector
802 sectortrailer
= (trgBlockNo
& 0x03) + 3;
803 } else { // 16 block sector
804 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
806 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
809 num_to_bytes(key64
, 6, keyBlock
);
811 num_to_bytes(key64
, 6, &keyBlock
[10]);
812 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
815 default : PrintAndLog("Unknown Error.\n");
819 else { // ------------------------------------ multiple sectors working
820 clock_t t1
= clock();
821 unsigned long elapsed_time
;
825 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
826 if (e_sector
== NULL
) return 1;
828 //test current key and additional standard keys first
829 memcpy(keyBlock
, key
, 6);
830 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
831 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
832 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
833 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
834 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
836 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
837 for (i
= 0; i
< SectorsCnt
; i
++) {
838 for (j
= 0; j
< 2; j
++) {
839 if (e_sector
[i
].foundKey
[j
]) continue;
841 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
844 e_sector
[i
].Key
[j
] = key64
;
845 e_sector
[i
].foundKey
[j
] = TRUE
;
849 clock_t t2
= clock() - t1
;
851 elapsed_time
= difftime(end
, start
);
853 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
855 PrintAndLog("enter nested...");
859 bool calibrate
= true;
861 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
862 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
863 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
865 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
867 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
869 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
870 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
871 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
872 case -4 : //key not found
879 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
880 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
883 default : PrintAndLog("Unknown Error.\n");
893 elapsed_time
= difftime(end
, start
);
895 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
898 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
899 PrintAndLog("trying to read key B...");
900 for (i
= 0; i
< SectorsCnt
; i
++) {
901 // KEY A but not KEY B
902 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
904 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
906 PrintAndLog("Reading block %d", sectrail
);
908 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
909 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
910 clearCommandBuffer();
914 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
916 uint8_t isOK
= resp
.arg
[0] & 0xff;
919 uint8_t *data
= resp
.d
.asBytes
;
920 key64
= bytes_to_num(data
+10, 6);
922 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
923 e_sector
[i
].foundKey
[1] = TRUE
;
924 e_sector
[i
].Key
[1] = key64
;
931 printKeyTable( SectorsCnt
, e_sector
);
933 // transfer them to the emulator
935 for (i
= 0; i
< SectorsCnt
; i
++) {
936 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
937 if (e_sector
[i
].foundKey
[0])
938 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
939 if (e_sector
[i
].foundKey
[1])
940 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
941 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
946 if (createDumpFile
) {
947 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
948 PrintAndLog("Could not create file dumpkeys.bin");
952 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
953 for(i
=0; i
<SectorsCnt
; i
++) {
954 if (e_sector
[i
].foundKey
[0]){
955 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
956 fwrite ( tempkey
, 1, 6, fkeys
);
959 fwrite ( &standart
, 1, 6, fkeys
);
962 for(i
=0; i
<SectorsCnt
; i
++) {
963 if (e_sector
[i
].foundKey
[1]){
964 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
965 fwrite ( tempkey
, 1, 6, fkeys
);
968 fwrite ( &standart
, 1, 6, fkeys
);
979 int CmdHF14AMfNestedHard(const char *Cmd
) {
982 uint8_t trgBlockNo
= 0;
983 uint8_t trgKeyType
= 0;
984 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
985 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
988 ctmp
= param_getchar(Cmd
, 0);
989 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
990 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
992 bool know_target_key
= false;
993 bool nonce_file_read
= false;
994 bool nonce_file_write
= false;
998 if (ctmp
== 'R' || ctmp
== 'r') {
999 nonce_file_read
= true;
1000 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
1001 know_target_key
= true;
1003 } else if (ctmp
== 'T' || ctmp
== 't') {
1004 tests
= param_get32ex(Cmd
, 1, 100, 10);
1006 blockNo
= param_get8(Cmd
, 0);
1007 ctmp
= param_getchar(Cmd
, 1);
1008 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1009 PrintAndLog("Key type must be A or B");
1012 if (ctmp
!= 'A' && ctmp
!= 'a') {
1016 if (param_gethex(Cmd
, 2, key
, 12)) {
1017 PrintAndLog("Key must include 12 HEX symbols");
1021 trgBlockNo
= param_get8(Cmd
, 3);
1022 ctmp
= param_getchar(Cmd
, 4);
1023 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1024 PrintAndLog("Target key type must be A or B");
1027 if (ctmp
!= 'A' && ctmp
!= 'a') {
1033 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
1034 know_target_key
= true;
1038 while ((ctmp
= param_getchar(Cmd
, i
))) {
1039 if (ctmp
== 's' || ctmp
== 'S') {
1041 } else if (ctmp
== 'w' || ctmp
== 'W') {
1042 nonce_file_write
= true;
1044 PrintAndLog("Possible options are w and/or s");
1051 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 ",
1054 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1055 know_target_key
? "" : " (not set)",
1056 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1057 slow
? "Yes" : "No",
1060 uint64_t foundkey
= 0;
1061 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
, &foundkey
);
1065 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1066 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1075 int CmdHF14AMfChk(const char *Cmd
) {
1077 if (strlen(Cmd
)<3) return usage_hf14_chk();
1080 char filename
[FILE_PATH_SIZE
]={0};
1082 uint8_t *keyBlock
= NULL
, *p
;
1083 uint8_t stKeyBlock
= 20;
1085 sector
*e_sector
= NULL
;
1090 uint8_t blockNo
= 0;
1091 uint8_t SectorsCnt
= 1;
1092 uint8_t keyType
= 0;
1095 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1097 int transferToEml
= 0;
1098 int createDumpFile
= 0;
1100 keyBlock
= calloc(stKeyBlock
, 6);
1101 if (keyBlock
== NULL
) return 1;
1103 uint64_t defaultKeys
[] = {
1104 0xffffffffffff, // Default key (first key used by program if no user defined key)
1105 0x000000000000, // Blank key
1106 0xa0a1a2a3a4a5, // NFCForum MAD key
1118 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1120 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1121 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1124 if (param_getchar(Cmd
, 0)=='*') {
1126 switch(param_getchar(Cmd
+1, 0)) {
1127 case '0': SectorsCnt
= 5; break;
1128 case '1': SectorsCnt
= 16; break;
1129 case '2': SectorsCnt
= 32; break;
1130 case '4': SectorsCnt
= 40; break;
1131 default: SectorsCnt
= 16;
1134 blockNo
= param_get8(Cmd
, 0);
1137 ctmp
= param_getchar(Cmd
, 1);
1149 PrintAndLog("Key type must be A , B or ?");
1154 ctmp
= param_getchar(Cmd
, 2);
1155 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1156 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1158 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1159 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1160 if ( stKeyBlock
- keycnt
< 2) {
1161 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1163 PrintAndLog("Cannot allocate memory for Keys");
1169 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1170 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1171 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1174 // May be a dic file
1175 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1176 PrintAndLog("File name too long");
1181 if ( (f
= fopen( filename
, "r")) ) {
1182 while( fgets(buf
, sizeof(buf
), f
) ){
1183 if (strlen(buf
) < 12 || buf
[11] == '\n')
1186 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1188 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1190 if (!isxdigit(buf
[0])){
1191 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1197 if ( stKeyBlock
- keycnt
< 2) {
1198 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1200 PrintAndLog("Cannot allocate memory for defKeys");
1207 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1208 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1209 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1211 memset(buf
, 0, sizeof(buf
));
1215 PrintAndLog("File: %s: not found or locked.", filename
);
1224 PrintAndLog("No key specified, trying default keys");
1225 for (;keycnt
< defaultKeysSize
; keycnt
++)
1226 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1227 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1228 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1231 // initialize storage for found keys
1232 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1233 if (e_sector
== NULL
) {
1239 for(int i
= 0; i
< SectorsCnt
; ++i
){
1240 e_sector
[i
].Key
[0] = 0xffffffffffff;
1241 e_sector
[i
].Key
[1] = 0xffffffffffff;
1242 e_sector
[i
].foundKey
[0] = FALSE
;
1243 e_sector
[i
].foundKey
[1] = FALSE
;
1247 uint8_t trgKeyType
= 0;
1248 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1251 clock_t t1
= clock();
1256 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1259 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1261 // skip already found keys.
1262 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1264 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1267 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1269 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1271 e_sector
[i
].Key
[trgKeyType
] = key64
;
1272 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1276 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1281 unsigned long elapsed_time
= difftime(end
, start
);
1283 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1286 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1287 if ( keyType
!= 1 ) {
1288 PrintAndLog("testing to read key B...");
1289 for (i
= 0; i
< SectorsCnt
; i
++) {
1290 // KEY A but not KEY B
1291 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1293 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1295 PrintAndLog("Reading block %d", sectrail
);
1297 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1298 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1299 clearCommandBuffer();
1303 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1305 uint8_t isOK
= resp
.arg
[0] & 0xff;
1306 if (!isOK
) continue;
1308 uint8_t *data
= resp
.d
.asBytes
;
1309 key64
= bytes_to_num(data
+10, 6);
1311 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1312 e_sector
[i
].foundKey
[1] = 1;
1313 e_sector
[i
].Key
[1] = key64
;
1321 printKeyTable( SectorsCnt
, e_sector
);
1323 if (transferToEml
) {
1324 uint8_t block
[16] = {0x00};
1325 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1326 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1327 if (e_sector
[i
].foundKey
[0])
1328 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1329 if (e_sector
[i
].foundKey
[1])
1330 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1331 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1333 PrintAndLog("Found keys have been transferred to the emulator memory");
1336 if (createDumpFile
) {
1337 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1338 if (fkeys
== NULL
) {
1339 PrintAndLog("Could not create file dumpkeys.bin");
1344 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1346 for( i
=0; i
<SectorsCnt
; i
++) {
1347 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1348 fwrite ( tempkey
, 1, 6, fkeys
);
1350 for(i
=0; i
<SectorsCnt
; i
++) {
1351 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1352 fwrite ( tempkey
, 1, 6, fkeys
);
1355 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1363 #define ATTACK_KEY_COUNT 8
1364 sector
*k_sector
= NULL
;
1365 uint8_t k_sectorsCount
= 16;
1366 void readerAttack(nonces_t data
[], bool setEmulatorMem
, bool verbose
) {
1368 // initialize storage for found keys
1369 if (k_sector
== NULL
)
1370 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1371 if (k_sector
== NULL
)
1377 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1378 k_sector
[i
].Key
[0] = 0xffffffffffff;
1379 k_sector
[i
].Key
[1] = 0xffffffffffff;
1380 k_sector
[i
].foundKey
[0] = FALSE
;
1381 k_sector
[i
].foundKey
[1] = FALSE
;
1384 printf("enter reader attack\n");
1385 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1386 if (data
[i
].ar2
> 0) {
1388 // We can probably skip this, mfkey32v2 is more reliable.
1389 #ifdef HFMF_TRYMFK32
1390 if (tryMfk32(data
[i
], &key
, verbose
)) {
1391 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1392 , (data
[i
].keytype
) ? "B" : "A"
1397 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1398 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1400 //set emulator memory for keys
1401 if (setEmulatorMem
) {
1402 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1403 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1404 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1405 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1406 , ((data
[i
].sector
)*4) + 3
1407 , sprint_hex( memBlock
, sizeof(memBlock
))
1409 mfEmlSetMem( memBlock
, ((data
[i
].sector
)*4) + 3, 1);
1415 if (tryMfk32_moebius(data
[i
+ATTACK_KEY_COUNT
], &key
, verbose
)) {
1416 uint8_t sectorNum
= data
[i
+ATTACK_KEY_COUNT
].sector
;
1417 uint8_t keyType
= data
[i
+ATTACK_KEY_COUNT
].keytype
;
1419 PrintAndLog("M-Found Key%s for sector %02d: [%012"llx
"]"
1420 , keyType
? "B" : "A"
1425 k_sector
[sectorNum
].Key
[keyType
] = key
;
1426 k_sector
[sectorNum
].foundKey
[keyType
] = TRUE
;
1428 //set emulator memory for keys
1429 if (setEmulatorMem
) {
1430 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1431 num_to_bytes( k_sector
[sectorNum
].Key
[0], 6, memBlock
);
1432 num_to_bytes( k_sector
[sectorNum
].Key
[1], 6, memBlock
+10);
1433 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1435 , sprint_hex( memBlock
, sizeof(memBlock
))
1437 mfEmlSetMem( memBlock
, (sectorNum
*4) + 3, 1);
1446 int CmdHF14AMf1kSim(const char *Cmd
) {
1448 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1449 uint8_t exitAfterNReads
= 0;
1450 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1452 bool setEmulatorMem
= false;
1454 bool errors
= false;
1456 // If set to true, we should show our workings when doing NR_AR_ATTACK.
1457 bool verbose
= false;
1459 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1460 switch(param_getchar(Cmd
, cmdp
)) {
1463 setEmulatorMem
= true;
1468 return usage_hf14_mf1ksim();
1471 flags
|= FLAG_INTERACTIVE
;
1476 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1481 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1483 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1484 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1485 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1486 default: return usage_hf14_mf1ksim();
1497 flags
|= FLAG_NR_AR_ATTACK
;
1501 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1508 if(errors
) return usage_hf14_mf1ksim();
1510 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1511 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1516 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1517 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1518 clearCommandBuffer();
1521 if(flags
& FLAG_INTERACTIVE
) {
1522 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1524 nonces_t data
[ATTACK_KEY_COUNT
*2];
1528 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1530 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1531 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1533 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1534 readerAttack(data
, setEmulatorMem
, verbose
);
1537 if (k_sector
!= NULL
) {
1538 printKeyTable(k_sectorsCount
, k_sector
);
1546 int CmdHF14AMfSniff(const char *Cmd
){
1547 bool wantLogToFile
= FALSE
;
1548 bool wantDecrypt
= FALSE
;
1549 //bool wantSaveToEml = FALSE; TODO
1550 bool wantSaveToEmlFile
= FALSE
;
1560 uint8_t uid_len
= 0;
1561 uint8_t atqa
[2] = {0x00, 0x00};
1564 uint8_t *buf
= NULL
;
1565 uint16_t bufsize
= 0;
1566 uint8_t *bufPtr
= NULL
;
1567 uint16_t traceLen
= 0;
1569 memset(uid
, 0x00, sizeof(uid
));
1571 char ctmp
= param_getchar(Cmd
, 0);
1572 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1574 for (int i
= 0; i
< 4; i
++) {
1575 ctmp
= param_getchar(Cmd
, i
);
1576 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1577 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1578 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1579 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1582 printf("-------------------------------------------------------------------------\n");
1583 printf("Executing mifare sniffing command. \n");
1584 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1585 printf("Press the key on pc keyboard to abort the client.\n");
1586 printf("-------------------------------------------------------------------------\n");
1588 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1589 clearCommandBuffer();
1597 tmpchar
= getchar();
1599 printf("\naborted via keyboard!\n");
1604 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1605 res
= resp
.arg
[0] & 0xff;
1606 traceLen
= resp
.arg
[1];
1615 if (res
== 1) { // there is (more) data to be transferred
1616 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1617 if (traceLen
> bufsize
) {
1619 if (buf
== NULL
) // not yet allocated
1620 p
= malloc(traceLen
);
1621 else // need more memory
1622 p
= realloc(buf
, traceLen
);
1625 PrintAndLog("Cannot allocate memory for trace");
1633 memset(buf
, 0x00, traceLen
);
1635 if (bufPtr
== NULL
) {
1636 PrintAndLog("Cannot allocate memory for trace");
1640 // what happens if LEN is bigger then TRACELEN --iceman
1641 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1646 if (res
== 2) { // received all data, start displaying
1647 blockLen
= bufPtr
- buf
;
1650 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1651 while (bufPtr
- buf
< blockLen
) {
1652 bufPtr
+= 6; // skip (void) timing information
1653 len
= *((uint16_t *)bufPtr
);
1661 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1662 memcpy(uid
, bufPtr
+ 2, 10);
1663 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1664 switch (atqa
[0] & 0xC0) {
1665 case 0x80: uid_len
= 10; break;
1666 case 0x40: uid_len
= 7; break;
1667 default: uid_len
= 4; break;
1670 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1671 sprint_hex(uid
, uid_len
),
1675 if (wantLogToFile
|| wantDecrypt
) {
1676 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1677 AddLogCurrentDT(logHexFileName
);
1680 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1682 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1684 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1686 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1690 bufPtr
+= ((len
-1)/8+1); // ignore parity
1701 int CmdHF14AMfDbg(const char *Cmd
) {
1703 char ctmp
= param_getchar(Cmd
, 0);
1704 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1706 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1707 if (dbgMode
> 4) return usage_hf14_dbg();
1709 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1714 int CmdHF14AMfKeyBrute(const char *Cmd
) {
1716 uint8_t blockNo
= 0, keytype
= 0;
1717 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
1718 uint64_t foundkey
= 0;
1720 char cmdp
= param_getchar(Cmd
, 0);
1721 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_keybrute();
1724 blockNo
= param_get8(Cmd
, 0);
1727 cmdp
= param_getchar(Cmd
, 1);
1728 if (cmdp
== 'B' || cmdp
== 'b') keytype
= 1;
1731 if (param_gethex(Cmd
, 2, key
, 12)) return usage_hf14_keybrute();
1733 clock_t t1
= clock();
1737 if (mfKeyBrute( blockNo
, keytype
, key
, &foundkey
))
1738 PrintAndLog("Found valid key: %012"llx
" \n", foundkey
);
1740 PrintAndLog("Key not found");
1744 unsigned long elapsed_time
= difftime(end
, start
);
1746 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1751 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1752 PrintAndLog("|---|----------------|---|----------------|---|");
1753 PrintAndLog("|sec|key A |res|key B |res|");
1754 PrintAndLog("|---|----------------|---|----------------|---|");
1755 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1756 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1757 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1758 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1761 PrintAndLog("|---|----------------|---|----------------|---|");
1764 // EMULATOR COMMANDS
1765 int CmdHF14AMfEGet(const char *Cmd
)
1767 uint8_t blockNo
= 0;
1768 uint8_t data
[16] = {0x00};
1770 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1771 PrintAndLog("Usage: hf mf eget <block number>");
1772 PrintAndLog(" sample: hf mf eget 0 ");
1776 blockNo
= param_get8(Cmd
, 0);
1779 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1780 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1782 PrintAndLog("Command execute timeout");
1788 int CmdHF14AMfEClear(const char *Cmd
)
1790 if (param_getchar(Cmd
, 0) == 'h') {
1791 PrintAndLog("Usage: hf mf eclr");
1792 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1796 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1801 int CmdHF14AMfESet(const char *Cmd
)
1803 uint8_t memBlock
[16];
1804 uint8_t blockNo
= 0;
1806 memset(memBlock
, 0x00, sizeof(memBlock
));
1808 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1809 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1810 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1814 blockNo
= param_get8(Cmd
, 0);
1816 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1817 PrintAndLog("block data must include 32 HEX symbols");
1822 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1823 memcpy(c
.d
.asBytes
, memBlock
, 16);
1828 int CmdHF14AMfELoad(const char *Cmd
)
1831 char filename
[FILE_PATH_SIZE
];
1832 char *fnameptr
= filename
;
1833 char buf
[64] = {0x00};
1834 uint8_t buf8
[64] = {0x00};
1835 int i
, len
, blockNum
, numBlocks
;
1836 int nameParamNo
= 1;
1837 uint8_t blockWidth
= 32;
1838 char ctmp
= param_getchar(Cmd
, 0);
1840 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1841 PrintAndLog("It loads emul dump from the file `filename.eml`");
1842 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1843 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1845 PrintAndLog(" sample: hf mf eload filename");
1846 PrintAndLog(" hf mf eload 4 filename");
1851 case '0' : numBlocks
= 5*4; break;
1853 case '\0': numBlocks
= 16*4; break;
1854 case '2' : numBlocks
= 32*4; break;
1855 case '4' : numBlocks
= 256; break;
1856 case 'U' : // fall through
1857 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1863 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1864 if (numblk2
> 0) numBlocks
= numblk2
;
1866 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1868 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1872 sprintf(fnameptr
, ".eml");
1875 f
= fopen(filename
, "r");
1877 PrintAndLog("File %s not found or locked", filename
);
1883 memset(buf
, 0, sizeof(buf
));
1885 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1887 if (blockNum
>= numBlocks
) break;
1889 PrintAndLog("File reading error.");
1894 if (strlen(buf
) < blockWidth
){
1895 if(strlen(buf
) && feof(f
))
1897 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1902 for (i
= 0; i
< blockWidth
; i
+= 2) {
1903 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1905 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1906 PrintAndLog("Cant set emul block: %3d", blockNum
);
1913 if (blockNum
>= numBlocks
) break;
1918 if ((blockNum
!= numBlocks
)) {
1919 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1922 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1926 int CmdHF14AMfESave(const char *Cmd
)
1929 char filename
[FILE_PATH_SIZE
];
1930 char * fnameptr
= filename
;
1932 int i
, j
, len
, numBlocks
;
1933 int nameParamNo
= 1;
1935 memset(filename
, 0, sizeof(filename
));
1936 memset(buf
, 0, sizeof(buf
));
1938 char ctmp
= param_getchar(Cmd
, 0);
1940 if ( ctmp
== 'h' || ctmp
== 'H') {
1941 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1942 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1943 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1945 PrintAndLog(" sample: hf mf esave ");
1946 PrintAndLog(" hf mf esave 4");
1947 PrintAndLog(" hf mf esave 4 filename");
1952 case '0' : numBlocks
= 5*4; break;
1954 case '\0': numBlocks
= 16*4; break;
1955 case '2' : numBlocks
= 32*4; break;
1956 case '4' : numBlocks
= 256; break;
1963 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1965 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1967 // user supplied filename?
1969 // get filename (UID from memory)
1970 if (mfEmlGetMem(buf
, 0, 1)) {
1971 PrintAndLog("Can\'t get UID from block: %d", 0);
1972 len
= sprintf(fnameptr
, "dump");
1976 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1977 sprintf(fnameptr
, "%02X", buf
[j
]);
1983 // add file extension
1984 sprintf(fnameptr
, ".eml");
1987 f
= fopen(filename
, "w+");
1990 PrintAndLog("Can't open file %s ", filename
);
1995 for (i
= 0; i
< numBlocks
; i
++) {
1996 if (mfEmlGetMem(buf
, i
, 1)) {
1997 PrintAndLog("Cant get block: %d", i
);
2000 for (j
= 0; j
< 16; j
++)
2001 fprintf(f
, "%02X", buf
[j
]);
2006 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
2011 int CmdHF14AMfECFill(const char *Cmd
)
2013 uint8_t keyType
= 0;
2014 uint8_t numSectors
= 16;
2016 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2017 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
2018 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2020 PrintAndLog("samples: hf mf ecfill A");
2021 PrintAndLog(" hf mf ecfill A 4");
2022 PrintAndLog("Read card and transfer its data to emulator memory.");
2023 PrintAndLog("Keys must be laid in the emulator memory. \n");
2027 char ctmp
= param_getchar(Cmd
, 0);
2028 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
2029 PrintAndLog("Key type must be A or B");
2032 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
2034 ctmp
= param_getchar(Cmd
, 1);
2036 case '0' : numSectors
= 5; break;
2038 case '\0': numSectors
= 16; break;
2039 case '2' : numSectors
= 32; break;
2040 case '4' : numSectors
= 40; break;
2041 default: numSectors
= 16;
2044 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
2045 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
2050 int CmdHF14AMfEKeyPrn(const char *Cmd
)
2055 uint64_t keyA
, keyB
;
2057 char cmdp
= param_getchar(Cmd
, 0);
2059 if ( cmdp
== 'h' || cmdp
== 'H' ) {
2060 PrintAndLog("It prints the keys loaded in the emulator memory");
2061 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2062 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2064 PrintAndLog(" sample: hf mf ekeyprn 1");
2069 case '0' : numSectors
= 5; break;
2071 case '\0': numSectors
= 16; break;
2072 case '2' : numSectors
= 32; break;
2073 case '4' : numSectors
= 40; break;
2074 default: numSectors
= 16;
2077 PrintAndLog("|---|----------------|----------------|");
2078 PrintAndLog("|sec|key A |key B |");
2079 PrintAndLog("|---|----------------|----------------|");
2080 for (i
= 0; i
< numSectors
; i
++) {
2081 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
2082 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
2085 keyA
= bytes_to_num(data
, 6);
2086 keyB
= bytes_to_num(data
+ 10, 6);
2087 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
2089 PrintAndLog("|---|----------------|----------------|");
2094 // CHINESE MAGIC COMMANDS
2096 int CmdHF14AMfCSetUID(const char *Cmd
) {
2097 uint8_t wipeCard
= 0;
2098 uint8_t uid
[8] = {0x00};
2099 uint8_t oldUid
[8] = {0x00};
2100 uint8_t atqa
[2] = {0x00};
2101 uint8_t sak
[1] = {0x00};
2102 uint8_t atqaPresent
= 1;
2107 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
2108 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2109 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2111 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2113 PrintAndLog("sample: hf mf csetuid 01020304");
2114 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2118 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2119 PrintAndLog("UID must include 8 HEX symbols");
2124 ctmp
= param_getchar(Cmd
, argi
);
2125 if (ctmp
== 'w' || ctmp
== 'W') {
2131 if (param_getchar(Cmd
, argi
)) {
2132 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2133 PrintAndLog("ATQA must include 4 HEX symbols");
2137 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2138 PrintAndLog("SAK must include 2 HEX symbols");
2147 ctmp
= param_getchar(Cmd
, argi
);
2148 if (ctmp
== 'w' || ctmp
== 'W') {
2153 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2155 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2157 PrintAndLog("Can't set UID. error=%d", res
);
2161 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2162 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2166 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2167 uint8_t block
[16] = {0x00};
2168 uint8_t blockNo
= 0;
2169 uint8_t params
= MAGIC_SINGLE
;
2172 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2173 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2174 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2175 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2176 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2180 blockNo
= param_get8(Cmd
, 0);
2182 if (param_gethex(Cmd
, 1, block
, 32)) {
2183 PrintAndLog("block data must include 32 HEX symbols");
2187 char ctmp
= param_getchar(Cmd
, 2);
2188 if (ctmp
== 'w' || ctmp
== 'W')
2189 params
|= MAGIC_WIPE
;
2191 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2193 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2195 PrintAndLog("Can't write block. error=%d", res
);
2201 int CmdHF14AMfCLoad(const char *Cmd
) {
2203 char filename
[FILE_PATH_SIZE
];
2204 char * fnameptr
= filename
;
2205 char buf
[64] = {0x00};
2206 uint8_t buf8
[64] = {0x00};
2207 uint8_t fillFromEmulator
= 0;
2208 int i
, len
, blockNum
, flags
=0;
2210 memset(filename
, 0, sizeof(filename
));
2212 char ctmp
= param_getchar(Cmd
, 0);
2214 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2215 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2216 PrintAndLog("or from emulator memory (option `e`)");
2217 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2218 PrintAndLog(" or: hf mf cload e ");
2219 PrintAndLog(" sample: hf mf cload filename");
2223 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2225 if (fillFromEmulator
) {
2226 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2227 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2228 PrintAndLog("Cant get block: %d", blockNum
);
2231 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2232 if (blockNum
== 1) flags
= 0; // just write
2233 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2235 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2236 PrintAndLog("Cant set magic card block: %d", blockNum
);
2243 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2245 memcpy(filename
, Cmd
, len
);
2248 sprintf(fnameptr
, ".eml");
2251 f
= fopen(filename
, "r");
2253 PrintAndLog("File not found or locked.");
2260 memset(buf
, 0, sizeof(buf
));
2262 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2264 PrintAndLog("File reading error.");
2268 if (strlen(buf
) < 32) {
2269 if(strlen(buf
) && feof(f
))
2271 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2275 for (i
= 0; i
< 32; i
+= 2)
2276 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2278 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2279 if (blockNum
== 1) flags
= 0; // just write
2280 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2282 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2283 PrintAndLog("Can't set magic card block: %d", blockNum
);
2289 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2294 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2295 PrintAndLog("File content error. There must be 64 blocks");
2298 PrintAndLog("Loaded from file: %s", filename
);
2304 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2306 uint8_t blockNo
= 0;
2308 memset(data
, 0x00, sizeof(data
));
2309 char ctmp
= param_getchar(Cmd
, 0);
2311 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2312 PrintAndLog("Usage: hf mf cgetblk <block number>");
2313 PrintAndLog("sample: hf mf cgetblk 1");
2314 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2318 blockNo
= param_get8(Cmd
, 0);
2320 PrintAndLog("--block number:%2d ", blockNo
);
2322 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2324 PrintAndLog("Can't read block. error=%d", res
);
2328 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2332 int CmdHF14AMfCGetSc(const char *Cmd
) {
2334 uint8_t sectorNo
= 0;
2336 memset(data
, 0x00, sizeof(data
));
2337 char ctmp
= param_getchar(Cmd
, 0);
2339 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2340 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2341 PrintAndLog("sample: hf mf cgetsc 0");
2342 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2346 sectorNo
= param_get8(Cmd
, 0);
2347 if (sectorNo
> 15) {
2348 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2352 PrintAndLog("--sector number:%d ", sectorNo
);
2353 PrintAndLog("block | data");
2355 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2356 for (i
= 0; i
< 4; i
++) {
2357 if (i
== 1) flags
= 0;
2358 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2360 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2362 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2365 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2370 int CmdHF14AMfCSave(const char *Cmd
) {
2373 char filename
[FILE_PATH_SIZE
];
2374 char * fnameptr
= filename
;
2375 uint8_t fillFromEmulator
= 0;
2377 int i
, j
, len
, flags
;
2379 memset(filename
, 0, sizeof(filename
));
2380 memset(buf
, 0, sizeof(buf
));
2381 char ctmp
= param_getchar(Cmd
, 0);
2383 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2384 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2385 PrintAndLog("or into emulator memory (option `e`)");
2386 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2387 PrintAndLog(" sample: hf mf esave ");
2388 PrintAndLog(" hf mf esave filename");
2389 PrintAndLog(" hf mf esave e \n");
2392 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2394 if (fillFromEmulator
) {
2395 // put into emulator
2396 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2397 for (i
= 0; i
< 16 * 4; i
++) {
2398 if (i
== 1) flags
= 0;
2399 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2401 if (mfCGetBlock(i
, buf
, flags
)) {
2402 PrintAndLog("Cant get block: %d", i
);
2406 if (mfEmlSetMem(buf
, i
, 1)) {
2407 PrintAndLog("Cant set emul block: %d", i
);
2414 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2416 // get filename based on UID
2419 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2420 PrintAndLog("Cant get block: %d", 0);
2421 len
= sprintf(fnameptr
, "dump");
2424 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2425 sprintf(fnameptr
, "%02x", buf
[j
]);
2428 memcpy(filename
, Cmd
, len
);
2432 // add .eml extension
2433 sprintf(fnameptr
, ".eml");
2436 f
= fopen(filename
, "w+");
2439 PrintAndLog("File not found or locked.");
2444 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2445 for (i
= 0; i
< 16 * 4; i
++) {
2446 if (i
== 1) flags
= 0;
2447 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2449 if (mfCGetBlock(i
, buf
, flags
)) {
2450 PrintAndLog("Cant get block: %d", i
);
2453 for (j
= 0; j
< 16; j
++)
2454 fprintf(f
, "%02x", buf
[j
]);
2459 PrintAndLog("Saved to file: %s", filename
);
2464 //needs nt, ar, at, Data to decrypt
2465 int CmdHf14MfDecryptBytes(const char *Cmd
){
2467 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2468 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2469 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2472 param_gethex_ex(Cmd
, 3, data
, &len
);
2475 int limit
= sizeof(data
) / 2;
2480 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2483 static command_t CommandTable
[] = {
2484 {"help", CmdHelp
, 1, "This help"},
2485 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2486 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2487 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2488 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2489 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2490 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2491 {"chk", CmdHF14AMfChk
, 0, "Check keys"},
2492 {"mifare", CmdHF14AMifare
, 0, "Darkside attack. read parity error messages."},
2493 {"nested", CmdHF14AMfNested
, 0, "Nested attack. Test nested authentication"},
2494 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2495 {"keybrute", CmdHF14AMfKeyBrute
, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2496 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2497 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2498 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2499 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2500 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2501 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2502 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2503 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2504 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2505 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2506 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2507 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2508 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2509 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2510 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2511 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2512 {NULL
, NULL
, 0, NULL
}
2515 int CmdHFMF(const char *Cmd
) {
2516 clearCommandBuffer();
2517 CmdsParse(CommandTable
, Cmd
);
2521 int CmdHelp(const char *Cmd
) {
2522 CmdsHelp(CommandTable
);