]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
5465c4d4ca1a6abdb53cc268e4eb6b3e6d60e4c7
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("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");
128 int usage_hf14_keybrute(void){
129 PrintAndLog("J_Run's 2nd phase of multiple sector nested authentication key recovery");
130 PrintAndLog("You have a known 4 last bytes of a key recovered with mf_nonce_brute tool.");
131 PrintAndLog("First 2 bytes of key will be bruteforced");
133 PrintAndLog("Usage: hf mf keybrute [h] <block number> <A|B> <key>");
134 PrintAndLog("options:");
135 PrintAndLog(" h this help");
136 PrintAndLog(" <block number> target block number");
137 PrintAndLog(" <A|B> target key type");
138 PrintAndLog(" <key> candidate key from mf_nonce_brute tool");
139 PrintAndLog("samples:");
140 PrintAndLog(" hf mf keybrute 1 A 000011223344");
144 int CmdHF14AMifare(const char *Cmd
) {
146 uint32_t nt
= 0, nr
= 0;
147 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
150 uint8_t blockNo
= 0, keytype
= MIFARE_AUTH_KEYA
;
152 char cmdp
= param_getchar(Cmd
, 0);
153 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
155 blockNo
= param_get8(Cmd
, 0);
157 cmdp
= param_getchar(Cmd
, 1);
158 if (cmdp
== 'B' || cmdp
== 'b')
159 keytype
= MIFARE_AUTH_KEYB
;
161 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, keytype
}};
164 printf("-------------------------------------------------------------------------\n");
165 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
166 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
167 printf("-------------------------------------------------------------------------\n");
168 clock_t t1
= clock();
173 clearCommandBuffer();
189 printf("\naborted via keyboard!\n");
194 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
197 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
198 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
199 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
200 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
201 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
204 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
205 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
206 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
207 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
208 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
217 if (isOK
== -4 && par_list
== 0) {
218 // this special attack when parities is zero, uses checkkeys. Which now with block/keytype option also needs.
219 // but it uses 0|1 instead of 0x60|0x61...
220 if (!nonce2key_ex(blockNo
, keytype
- 0x60 , uid
, nt
, nr
, ks_list
, &r_key
) ){
221 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
227 if (isOK
!= 1) return 1;
229 // execute original function from util nonce2key
230 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
232 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
233 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
237 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
242 unsigned long elapsed_time
= difftime(end
, start
);
244 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
248 int CmdHF14AMfWrBl(const char *Cmd
) {
251 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
252 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
257 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
258 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
262 blockNo
= param_get8(Cmd
, 0);
263 cmdp
= param_getchar(Cmd
, 1);
265 PrintAndLog("Key type must be A or B");
268 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
269 if (param_gethex(Cmd
, 2, key
, 12)) {
270 PrintAndLog("Key must include 12 HEX symbols");
273 if (param_gethex(Cmd
, 3, bldata
, 32)) {
274 PrintAndLog("Block data must include 32 HEX symbols");
277 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
278 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
280 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
281 memcpy(c
.d
.asBytes
, key
, 6);
282 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
283 clearCommandBuffer();
287 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
288 uint8_t isOK
= resp
.arg
[0] & 0xff;
289 PrintAndLog("isOk:%02x", isOK
);
291 PrintAndLog("Command execute timeout");
297 int CmdHF14AMfRdBl(const char *Cmd
) {
300 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
306 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
307 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
311 blockNo
= param_get8(Cmd
, 0);
312 cmdp
= param_getchar(Cmd
, 1);
314 PrintAndLog("Key type must be A or B");
317 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
318 if (param_gethex(Cmd
, 2, key
, 12)) {
319 PrintAndLog("Key must include 12 HEX symbols");
322 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
324 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
325 memcpy(c
.d
.asBytes
, key
, 6);
326 clearCommandBuffer();
330 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
331 uint8_t isOK
= resp
.arg
[0] & 0xff;
332 uint8_t *data
= resp
.d
.asBytes
;
335 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
337 PrintAndLog("isOk:%02x", isOK
);
339 PrintAndLog("Command execute timeout");
345 int CmdHF14AMfRdSc(const char *Cmd
) {
347 uint8_t sectorNo
= 0;
349 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
351 uint8_t *data
= NULL
;
355 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
356 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
360 sectorNo
= param_get8(Cmd
, 0);
362 PrintAndLog("Sector number must be less than 40");
365 cmdp
= param_getchar(Cmd
, 1);
366 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
367 PrintAndLog("Key type must be A or B");
370 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
371 if (param_gethex(Cmd
, 2, key
, 12)) {
372 PrintAndLog("Key must include 12 HEX symbols");
375 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
377 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
378 memcpy(c
.d
.asBytes
, key
, 6);
379 clearCommandBuffer();
384 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
385 isOK
= resp
.arg
[0] & 0xff;
386 data
= resp
.d
.asBytes
;
388 PrintAndLog("isOk:%02x", isOK
);
390 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
391 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
393 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
396 PrintAndLog("Command execute timeout");
402 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
406 return 32 * 4 + (sectorNo
- 32) * 16;
410 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
418 int CmdHF14AMfDump(const char *Cmd
) {
419 uint8_t sectorNo
, blockNo
;
423 uint8_t rights
[40][4];
424 uint8_t carddata
[256][16];
425 uint8_t numSectors
= 16;
432 char cmdp
= param_getchar(Cmd
, 0);
434 case '0' : numSectors
= 5; break;
436 case '\0': numSectors
= 16; break;
437 case '2' : numSectors
= 32; break;
438 case '4' : numSectors
= 40; break;
439 default: numSectors
= 16;
442 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
443 PrintAndLog("Usage: hf mf dump [card memory]");
444 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
446 PrintAndLog("Samples: hf mf dump");
447 PrintAndLog(" hf mf dump 4");
451 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
452 PrintAndLog("Could not find file dumpkeys.bin");
456 // Read keys A from file
458 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
459 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
460 if ( bytes_read
== 0) {
461 PrintAndLog("File reading error.");
467 // Read keys B from file
468 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
469 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
470 if ( bytes_read
== 0) {
471 PrintAndLog("File reading error.");
479 PrintAndLog("|-----------------------------------------|");
480 PrintAndLog("|------ Reading sector access bits...-----|");
481 PrintAndLog("|-----------------------------------------|");
483 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
484 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
485 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
486 clearCommandBuffer();
489 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
490 uint8_t isOK
= resp
.arg
[0] & 0xff;
491 uint8_t *data
= resp
.d
.asBytes
;
493 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
494 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
495 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
496 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
498 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
499 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
500 rights
[sectorNo
][3] = 0x01;
503 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
504 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
505 rights
[sectorNo
][3] = 0x01;
509 PrintAndLog("|-----------------------------------------|");
510 PrintAndLog("|----- Dumping all blocks to file... -----|");
511 PrintAndLog("|-----------------------------------------|");
514 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
515 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
516 bool received
= false;
518 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
519 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
520 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
521 clearCommandBuffer();
523 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
524 } else { // data block. Check if it can be read with key A or key B
525 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
526 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
527 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
528 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
530 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
531 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
533 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
534 } else { // key A would work
535 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
536 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
537 clearCommandBuffer();
539 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
544 isOK
= resp
.arg
[0] & 0xff;
545 uint8_t *data
= resp
.d
.asBytes
;
546 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
547 data
[0] = (keyA
[sectorNo
][0]);
548 data
[1] = (keyA
[sectorNo
][1]);
549 data
[2] = (keyA
[sectorNo
][2]);
550 data
[3] = (keyA
[sectorNo
][3]);
551 data
[4] = (keyA
[sectorNo
][4]);
552 data
[5] = (keyA
[sectorNo
][5]);
553 data
[10] = (keyB
[sectorNo
][0]);
554 data
[11] = (keyB
[sectorNo
][1]);
555 data
[12] = (keyB
[sectorNo
][2]);
556 data
[13] = (keyB
[sectorNo
][3]);
557 data
[14] = (keyB
[sectorNo
][4]);
558 data
[15] = (keyB
[sectorNo
][5]);
561 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
562 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
564 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
570 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
577 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
578 PrintAndLog("Could not create file name dumpdata.bin");
581 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
582 fwrite(carddata
, 1, 16*numblocks
, fout
);
584 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
590 int CmdHF14AMfRestore(const char *Cmd
) {
591 uint8_t sectorNo
,blockNo
;
593 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
594 uint8_t bldata
[16] = {0x00};
602 char cmdp
= param_getchar(Cmd
, 0);
604 case '0' : numSectors
= 5; break;
606 case '\0': numSectors
= 16; break;
607 case '2' : numSectors
= 32; break;
608 case '4' : numSectors
= 40; break;
609 default: numSectors
= 16;
612 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
613 PrintAndLog("Usage: hf mf restore [card memory]");
614 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
616 PrintAndLog("Samples: hf mf restore");
617 PrintAndLog(" hf mf restore 4");
621 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
622 PrintAndLog("Could not find file dumpkeys.bin");
627 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
628 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
629 if ( bytes_read
== 0) {
630 PrintAndLog("File reading error (dumpkeys.bin).");
636 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
637 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
638 if ( bytes_read
== 0) {
639 PrintAndLog("File reading error (dumpkeys.bin).");
647 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
648 PrintAndLog("Could not find file dumpdata.bin");
651 PrintAndLog("Restoring dumpdata.bin to card");
653 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
654 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
655 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
656 memcpy(c
.d
.asBytes
, key
, 6);
657 bytes_read
= fread(bldata
, 1, 16, fdump
);
658 if ( bytes_read
== 0) {
659 PrintAndLog("File reading error (dumpdata.bin).");
664 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
665 bldata
[0] = (keyA
[sectorNo
][0]);
666 bldata
[1] = (keyA
[sectorNo
][1]);
667 bldata
[2] = (keyA
[sectorNo
][2]);
668 bldata
[3] = (keyA
[sectorNo
][3]);
669 bldata
[4] = (keyA
[sectorNo
][4]);
670 bldata
[5] = (keyA
[sectorNo
][5]);
671 bldata
[10] = (keyB
[sectorNo
][0]);
672 bldata
[11] = (keyB
[sectorNo
][1]);
673 bldata
[12] = (keyB
[sectorNo
][2]);
674 bldata
[13] = (keyB
[sectorNo
][3]);
675 bldata
[14] = (keyB
[sectorNo
][4]);
676 bldata
[15] = (keyB
[sectorNo
][5]);
679 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
681 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
682 clearCommandBuffer();
686 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
687 uint8_t isOK
= resp
.arg
[0] & 0xff;
688 PrintAndLog("isOk:%02x", isOK
);
690 PrintAndLog("Command execute timeout");
699 int CmdHF14AMfNested(const char *Cmd
) {
700 int i
, j
, res
, iterations
;
701 sector
*e_sector
= NULL
;
704 uint8_t trgBlockNo
= 0;
705 uint8_t trgKeyType
= 0;
706 uint8_t SectorsCnt
= 0;
707 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
708 uint8_t keyBlock
[6*6];
710 bool transferToEml
= false;
712 bool createDumpFile
= false;
714 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
715 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
717 if (strlen(Cmd
)<3) return usage_hf14_nested();
720 cmdp
= param_getchar(Cmd
, 0);
721 blockNo
= param_get8(Cmd
, 1);
722 ctmp
= param_getchar(Cmd
, 2);
724 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
725 PrintAndLog("Key type must be A or B");
729 if (ctmp
!= 'A' && ctmp
!= 'a')
732 if (param_gethex(Cmd
, 3, key
, 12)) {
733 PrintAndLog("Key must include 12 HEX symbols");
737 if (cmdp
== 'o' || cmdp
== 'O') {
739 trgBlockNo
= param_get8(Cmd
, 4);
740 ctmp
= param_getchar(Cmd
, 5);
741 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
742 PrintAndLog("Target key type must be A or B");
745 if (ctmp
!= 'A' && ctmp
!= 'a')
750 case '0': SectorsCnt
= 05; break;
751 case '1': SectorsCnt
= 16; break;
752 case '2': SectorsCnt
= 32; break;
753 case '4': SectorsCnt
= 40; break;
754 default: SectorsCnt
= 16;
758 ctmp
= param_getchar(Cmd
, 4);
759 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
760 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
762 ctmp
= param_getchar(Cmd
, 6);
763 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
764 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
767 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
769 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
770 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
771 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
772 case -4 : PrintAndLog("No valid key found"); break;
774 key64
= bytes_to_num(keyBlock
, 6);
776 // transfer key to the emulator
778 uint8_t sectortrailer
;
779 if (trgBlockNo
< 32*4) { // 4 block sector
780 sectortrailer
= (trgBlockNo
& 0x03) + 3;
781 } else { // 16 block sector
782 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
784 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
787 num_to_bytes(key64
, 6, keyBlock
);
789 num_to_bytes(key64
, 6, &keyBlock
[10]);
790 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
793 default : PrintAndLog("Unknown Error.\n");
797 else { // ------------------------------------ multiple sectors working
798 clock_t t1
= clock();
799 unsigned long elapsed_time
;
803 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
804 if (e_sector
== NULL
) return 1;
806 //test current key and additional standard keys first
807 memcpy(keyBlock
, key
, 6);
808 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
809 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
810 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
811 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
812 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
814 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
815 for (i
= 0; i
< SectorsCnt
; i
++) {
816 for (j
= 0; j
< 2; j
++) {
817 if (e_sector
[i
].foundKey
[j
]) continue;
819 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
822 e_sector
[i
].Key
[j
] = key64
;
823 e_sector
[i
].foundKey
[j
] = TRUE
;
827 clock_t t2
= clock() - t1
;
829 elapsed_time
= difftime(end
, start
);
831 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
833 PrintAndLog("enter nested...");
837 bool calibrate
= true;
839 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
840 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
841 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
843 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
845 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
847 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
848 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
849 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
850 case -4 : //key not found
857 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
858 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
861 default : PrintAndLog("Unknown Error.\n");
871 elapsed_time
= difftime(end
, start
);
873 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
876 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
877 PrintAndLog("trying to read key B...");
878 for (i
= 0; i
< SectorsCnt
; i
++) {
879 // KEY A but not KEY B
880 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
882 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
884 PrintAndLog("Reading block %d", sectrail
);
886 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
887 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
888 clearCommandBuffer();
892 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
894 uint8_t isOK
= resp
.arg
[0] & 0xff;
897 uint8_t *data
= resp
.d
.asBytes
;
898 key64
= bytes_to_num(data
+10, 6);
900 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
901 e_sector
[i
].foundKey
[1] = TRUE
;
902 e_sector
[i
].Key
[1] = key64
;
909 printKeyTable( SectorsCnt
, e_sector
);
911 // transfer them to the emulator
913 for (i
= 0; i
< SectorsCnt
; i
++) {
914 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
915 if (e_sector
[i
].foundKey
[0])
916 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
917 if (e_sector
[i
].foundKey
[1])
918 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
919 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
924 if (createDumpFile
) {
925 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
926 PrintAndLog("Could not create file dumpkeys.bin");
930 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
931 for(i
=0; i
<SectorsCnt
; i
++) {
932 if (e_sector
[i
].foundKey
[0]){
933 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
934 fwrite ( tempkey
, 1, 6, fkeys
);
937 fwrite ( &standart
, 1, 6, fkeys
);
940 for(i
=0; i
<SectorsCnt
; i
++) {
941 if (e_sector
[i
].foundKey
[1]){
942 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
943 fwrite ( tempkey
, 1, 6, fkeys
);
946 fwrite ( &standart
, 1, 6, fkeys
);
957 int CmdHF14AMfNestedHard(const char *Cmd
) {
960 uint8_t trgBlockNo
= 0;
961 uint8_t trgKeyType
= 0;
962 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
963 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
966 ctmp
= param_getchar(Cmd
, 0);
967 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
968 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
970 bool know_target_key
= false;
971 bool nonce_file_read
= false;
972 bool nonce_file_write
= false;
976 if (ctmp
== 'R' || ctmp
== 'r') {
977 nonce_file_read
= true;
978 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
979 know_target_key
= true;
981 } else if (ctmp
== 'T' || ctmp
== 't') {
982 tests
= param_get32ex(Cmd
, 1, 100, 10);
984 blockNo
= param_get8(Cmd
, 0);
985 ctmp
= param_getchar(Cmd
, 1);
986 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
987 PrintAndLog("Key type must be A or B");
990 if (ctmp
!= 'A' && ctmp
!= 'a') {
994 if (param_gethex(Cmd
, 2, key
, 12)) {
995 PrintAndLog("Key must include 12 HEX symbols");
999 trgBlockNo
= param_get8(Cmd
, 3);
1000 ctmp
= param_getchar(Cmd
, 4);
1001 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1002 PrintAndLog("Target key type must be A or B");
1005 if (ctmp
!= 'A' && ctmp
!= 'a') {
1011 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
1012 know_target_key
= true;
1016 while ((ctmp
= param_getchar(Cmd
, i
))) {
1017 if (ctmp
== 's' || ctmp
== 'S') {
1019 } else if (ctmp
== 'w' || ctmp
== 'W') {
1020 nonce_file_write
= true;
1022 PrintAndLog("Possible options are w and/or s");
1029 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 ",
1032 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1033 know_target_key
? "" : " (not set)",
1034 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1035 slow
? "Yes" : "No",
1038 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1042 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1043 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1052 int CmdHF14AMfChk(const char *Cmd
) {
1054 if (strlen(Cmd
)<3) return usage_hf14_chk();
1057 char filename
[FILE_PATH_SIZE
]={0};
1059 uint8_t *keyBlock
= NULL
, *p
;
1060 uint8_t stKeyBlock
= 20;
1062 sector
*e_sector
= NULL
;
1067 uint8_t blockNo
= 0;
1068 uint8_t SectorsCnt
= 1;
1069 uint8_t keyType
= 0;
1072 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1074 int transferToEml
= 0;
1075 int createDumpFile
= 0;
1077 keyBlock
= calloc(stKeyBlock
, 6);
1078 if (keyBlock
== NULL
) return 1;
1080 uint64_t defaultKeys
[] = {
1081 0xffffffffffff, // Default key (first key used by program if no user defined key)
1082 0x000000000000, // Blank key
1083 0xa0a1a2a3a4a5, // NFCForum MAD key
1095 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1097 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1098 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1101 if (param_getchar(Cmd
, 0)=='*') {
1103 switch(param_getchar(Cmd
+1, 0)) {
1104 case '0': SectorsCnt
= 5; break;
1105 case '1': SectorsCnt
= 16; break;
1106 case '2': SectorsCnt
= 32; break;
1107 case '4': SectorsCnt
= 40; break;
1108 default: SectorsCnt
= 16;
1111 blockNo
= param_get8(Cmd
, 0);
1114 ctmp
= param_getchar(Cmd
, 1);
1126 PrintAndLog("Key type must be A , B or ?");
1131 ctmp
= param_getchar(Cmd
, 2);
1132 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1133 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1135 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1136 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1137 if ( stKeyBlock
- keycnt
< 2) {
1138 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1140 PrintAndLog("Cannot allocate memory for Keys");
1146 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1147 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1148 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1151 // May be a dic file
1152 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1153 PrintAndLog("File name too long");
1158 if ( (f
= fopen( filename
, "r")) ) {
1159 while( fgets(buf
, sizeof(buf
), f
) ){
1160 if (strlen(buf
) < 12 || buf
[11] == '\n')
1163 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1165 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1167 if (!isxdigit(buf
[0])){
1168 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1174 if ( stKeyBlock
- keycnt
< 2) {
1175 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1177 PrintAndLog("Cannot allocate memory for defKeys");
1184 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1185 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1186 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1188 memset(buf
, 0, sizeof(buf
));
1192 PrintAndLog("File: %s: not found or locked.", filename
);
1201 PrintAndLog("No key specified, trying default keys");
1202 for (;keycnt
< defaultKeysSize
; keycnt
++)
1203 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1204 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1205 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1208 // initialize storage for found keys
1209 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1210 if (e_sector
== NULL
) {
1216 for(int i
= 0; i
< SectorsCnt
; ++i
){
1217 e_sector
[i
].Key
[0] = 0xffffffffffff;
1218 e_sector
[i
].Key
[1] = 0xffffffffffff;
1219 e_sector
[i
].foundKey
[0] = FALSE
;
1220 e_sector
[i
].foundKey
[1] = FALSE
;
1224 uint8_t trgKeyType
= 0;
1225 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1228 clock_t t1
= clock();
1233 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1236 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1238 // skip already found keys.
1239 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1241 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1244 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1246 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1248 e_sector
[i
].Key
[trgKeyType
] = key64
;
1249 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1253 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1258 unsigned long elapsed_time
= difftime(end
, start
);
1260 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1263 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1264 if ( keyType
!= 1 ) {
1265 PrintAndLog("testing to read key B...");
1266 for (i
= 0; i
< SectorsCnt
; i
++) {
1267 // KEY A but not KEY B
1268 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1270 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1272 PrintAndLog("Reading block %d", sectrail
);
1274 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1275 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1276 clearCommandBuffer();
1280 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1282 uint8_t isOK
= resp
.arg
[0] & 0xff;
1283 if (!isOK
) continue;
1285 uint8_t *data
= resp
.d
.asBytes
;
1286 key64
= bytes_to_num(data
+10, 6);
1288 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1289 e_sector
[i
].foundKey
[1] = 1;
1290 e_sector
[i
].Key
[1] = key64
;
1298 printKeyTable( SectorsCnt
, e_sector
);
1300 if (transferToEml
) {
1301 uint8_t block
[16] = {0x00};
1302 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1303 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1304 if (e_sector
[i
].foundKey
[0])
1305 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1306 if (e_sector
[i
].foundKey
[1])
1307 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1308 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1310 PrintAndLog("Found keys have been transferred to the emulator memory");
1313 if (createDumpFile
) {
1314 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1315 if (fkeys
== NULL
) {
1316 PrintAndLog("Could not create file dumpkeys.bin");
1321 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1323 for( i
=0; i
<SectorsCnt
; i
++) {
1324 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1325 fwrite ( tempkey
, 1, 6, fkeys
);
1327 for(i
=0; i
<SectorsCnt
; i
++) {
1328 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1329 fwrite ( tempkey
, 1, 6, fkeys
);
1332 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1340 #define ATTACK_KEY_COUNT 8
1341 sector
*k_sector
= NULL
;
1342 uint8_t k_sectorsCount
= 16;
1343 void readerAttack(nonces_t data
[], bool setEmulatorMem
) {
1345 // initialize storage for found keys
1346 if (k_sector
== NULL
)
1347 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1348 if (k_sector
== NULL
)
1354 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1355 k_sector
[i
].Key
[0] = 0xffffffffffff;
1356 k_sector
[i
].Key
[1] = 0xffffffffffff;
1357 k_sector
[i
].foundKey
[0] = FALSE
;
1358 k_sector
[i
].foundKey
[1] = FALSE
;
1361 printf("enter reader attack\n");
1362 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1363 if (data
[i
].ar2
> 0) {
1365 if (tryMfk32(data
[i
], &key
)) {
1366 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1367 , (data
[i
].keytype
) ? "B" : "A"
1372 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1373 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1375 //set emulator memory for keys
1376 if (setEmulatorMem
) {
1377 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1378 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1379 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1380 mfEmlSetMem( memBlock
, i
*4 + 3, 1);
1381 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1383 , sprint_hex( memBlock
, sizeof(memBlock
))
1389 // if (tryMfk32_moebius(data[i+ATTACK_KEY_COUNT], &key)) {
1390 // PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"
1391 // ,(data[i+ATTACK_KEY_COUNT].keytype) ? "B" : "A"
1392 // , data[i+ATTACK_KEY_COUNT].sector
1400 int CmdHF14AMf1kSim(const char *Cmd
) {
1402 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1403 uint8_t exitAfterNReads
= 0;
1404 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1406 bool setEmulatorMem
= false;
1408 bool errors
= false;
1410 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1411 switch(param_getchar(Cmd
, cmdp
)) {
1414 setEmulatorMem
= true;
1419 return usage_hf14_mf1ksim();
1422 flags
|= FLAG_INTERACTIVE
;
1427 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1432 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1434 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1435 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1436 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1437 default: return usage_hf14_mf1ksim();
1443 flags
|= FLAG_NR_AR_ATTACK
;
1447 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1454 if(errors
) return usage_hf14_mf1ksim();
1456 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1457 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1462 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1463 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1464 clearCommandBuffer();
1467 if(flags
& FLAG_INTERACTIVE
) {
1468 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1470 nonces_t data
[ATTACK_KEY_COUNT
*2];
1474 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1476 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1477 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1479 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1480 readerAttack(data
, setEmulatorMem
);
1483 if (k_sector
!= NULL
) {
1484 printKeyTable(k_sectorsCount
, k_sector
);
1491 int CmdHF14AMfSniff(const char *Cmd
){
1492 bool wantLogToFile
= FALSE
;
1493 bool wantDecrypt
= FALSE
;
1494 //bool wantSaveToEml = FALSE; TODO
1495 bool wantSaveToEmlFile
= FALSE
;
1505 uint8_t uid_len
= 0;
1506 uint8_t atqa
[2] = {0x00, 0x00};
1509 uint8_t *buf
= NULL
;
1510 uint16_t bufsize
= 0;
1511 uint8_t *bufPtr
= NULL
;
1512 uint16_t traceLen
= 0;
1514 memset(uid
, 0x00, sizeof(uid
));
1516 char ctmp
= param_getchar(Cmd
, 0);
1517 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1519 for (int i
= 0; i
< 4; i
++) {
1520 ctmp
= param_getchar(Cmd
, i
);
1521 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1522 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1523 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1524 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1527 printf("-------------------------------------------------------------------------\n");
1528 printf("Executing mifare sniffing command. \n");
1529 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1530 printf("Press the key on pc keyboard to abort the client.\n");
1531 printf("-------------------------------------------------------------------------\n");
1533 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1534 clearCommandBuffer();
1542 tmpchar
= getchar();
1544 printf("\naborted via keyboard!\n");
1549 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1550 res
= resp
.arg
[0] & 0xff;
1551 traceLen
= resp
.arg
[1];
1560 if (res
== 1) { // there is (more) data to be transferred
1561 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1562 if (traceLen
> bufsize
) {
1564 if (buf
== NULL
) // not yet allocated
1565 p
= malloc(traceLen
);
1566 else // need more memory
1567 p
= realloc(buf
, traceLen
);
1570 PrintAndLog("Cannot allocate memory for trace");
1578 memset(buf
, 0x00, traceLen
);
1580 if (bufPtr
== NULL
) {
1581 PrintAndLog("Cannot allocate memory for trace");
1585 // what happens if LEN is bigger then TRACELEN --iceman
1586 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1591 if (res
== 2) { // received all data, start displaying
1592 blockLen
= bufPtr
- buf
;
1595 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1596 while (bufPtr
- buf
< blockLen
) {
1597 bufPtr
+= 6; // skip (void) timing information
1598 len
= *((uint16_t *)bufPtr
);
1606 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1607 memcpy(uid
, bufPtr
+ 2, 10);
1608 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1609 switch (atqa
[0] & 0xC0) {
1610 case 0x80: uid_len
= 10; break;
1611 case 0x40: uid_len
= 7; break;
1612 default: uid_len
= 4; break;
1615 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1616 sprint_hex(uid
, uid_len
),
1620 if (wantLogToFile
|| wantDecrypt
) {
1621 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1622 AddLogCurrentDT(logHexFileName
);
1625 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1627 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1629 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1631 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1635 bufPtr
+= ((len
-1)/8+1); // ignore parity
1646 int CmdHF14AMfDbg(const char *Cmd
) {
1648 char ctmp
= param_getchar(Cmd
, 0);
1649 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1651 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1652 if (dbgMode
> 4) return usage_hf14_dbg();
1654 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1659 int CmdHF14AMfKeyBrute(const char *Cmd
) {
1661 uint8_t blockNo
= 0, keytype
= 0;
1662 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
1663 uint64_t foundkey
= 0;
1665 char cmdp
= param_getchar(Cmd
, 0);
1666 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_keybrute();
1669 blockNo
= param_get8(Cmd
, 0);
1672 cmdp
= param_getchar(Cmd
, 1);
1673 if (cmdp
== 'B' || cmdp
== 'b') keytype
= 1;
1676 if (param_gethex(Cmd
, 2, key
, 12)) return usage_hf14_keybrute();
1678 clock_t t1
= clock();
1682 if (mfKeyBrute( blockNo
, keytype
, key
, &foundkey
))
1683 PrintAndLog("Found valid key: %012"llx
" \n", foundkey
);
1685 PrintAndLog("Key not found");
1689 unsigned long elapsed_time
= difftime(end
, start
);
1691 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1696 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1697 PrintAndLog("|---|----------------|---|----------------|---|");
1698 PrintAndLog("|sec|key A |res|key B |res|");
1699 PrintAndLog("|---|----------------|---|----------------|---|");
1700 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1701 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1702 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1703 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1706 PrintAndLog("|---|----------------|---|----------------|---|");
1709 // EMULATOR COMMANDS
1710 int CmdHF14AMfEGet(const char *Cmd
)
1712 uint8_t blockNo
= 0;
1713 uint8_t data
[16] = {0x00};
1715 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1716 PrintAndLog("Usage: hf mf eget <block number>");
1717 PrintAndLog(" sample: hf mf eget 0 ");
1721 blockNo
= param_get8(Cmd
, 0);
1724 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1725 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1727 PrintAndLog("Command execute timeout");
1733 int CmdHF14AMfEClear(const char *Cmd
)
1735 if (param_getchar(Cmd
, 0) == 'h') {
1736 PrintAndLog("Usage: hf mf eclr");
1737 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1741 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1746 int CmdHF14AMfESet(const char *Cmd
)
1748 uint8_t memBlock
[16];
1749 uint8_t blockNo
= 0;
1751 memset(memBlock
, 0x00, sizeof(memBlock
));
1753 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1754 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1755 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1759 blockNo
= param_get8(Cmd
, 0);
1761 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1762 PrintAndLog("block data must include 32 HEX symbols");
1767 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1768 memcpy(c
.d
.asBytes
, memBlock
, 16);
1773 int CmdHF14AMfELoad(const char *Cmd
)
1776 char filename
[FILE_PATH_SIZE
];
1777 char *fnameptr
= filename
;
1778 char buf
[64] = {0x00};
1779 uint8_t buf8
[64] = {0x00};
1780 int i
, len
, blockNum
, numBlocks
;
1781 int nameParamNo
= 1;
1782 uint8_t blockWidth
= 32;
1783 char ctmp
= param_getchar(Cmd
, 0);
1785 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1786 PrintAndLog("It loads emul dump from the file `filename.eml`");
1787 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1788 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1790 PrintAndLog(" sample: hf mf eload filename");
1791 PrintAndLog(" hf mf eload 4 filename");
1796 case '0' : numBlocks
= 5*4; break;
1798 case '\0': numBlocks
= 16*4; break;
1799 case '2' : numBlocks
= 32*4; break;
1800 case '4' : numBlocks
= 256; break;
1801 case 'U' : // fall through
1802 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1808 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1809 if (numblk2
> 0) numBlocks
= numblk2
;
1811 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1813 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1817 sprintf(fnameptr
, ".eml");
1820 f
= fopen(filename
, "r");
1822 PrintAndLog("File %s not found or locked", filename
);
1828 memset(buf
, 0, sizeof(buf
));
1830 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1832 if (blockNum
>= numBlocks
) break;
1834 PrintAndLog("File reading error.");
1839 if (strlen(buf
) < blockWidth
){
1840 if(strlen(buf
) && feof(f
))
1842 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1847 for (i
= 0; i
< blockWidth
; i
+= 2) {
1848 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1850 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1851 PrintAndLog("Cant set emul block: %3d", blockNum
);
1858 if (blockNum
>= numBlocks
) break;
1863 if ((blockNum
!= numBlocks
)) {
1864 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1867 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1871 int CmdHF14AMfESave(const char *Cmd
)
1874 char filename
[FILE_PATH_SIZE
];
1875 char * fnameptr
= filename
;
1877 int i
, j
, len
, numBlocks
;
1878 int nameParamNo
= 1;
1880 memset(filename
, 0, sizeof(filename
));
1881 memset(buf
, 0, sizeof(buf
));
1883 char ctmp
= param_getchar(Cmd
, 0);
1885 if ( ctmp
== 'h' || ctmp
== 'H') {
1886 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1887 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1888 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1890 PrintAndLog(" sample: hf mf esave ");
1891 PrintAndLog(" hf mf esave 4");
1892 PrintAndLog(" hf mf esave 4 filename");
1897 case '0' : numBlocks
= 5*4; break;
1899 case '\0': numBlocks
= 16*4; break;
1900 case '2' : numBlocks
= 32*4; break;
1901 case '4' : numBlocks
= 256; break;
1908 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1910 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1912 // user supplied filename?
1914 // get filename (UID from memory)
1915 if (mfEmlGetMem(buf
, 0, 1)) {
1916 PrintAndLog("Can\'t get UID from block: %d", 0);
1917 len
= sprintf(fnameptr
, "dump");
1921 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1922 sprintf(fnameptr
, "%02X", buf
[j
]);
1928 // add file extension
1929 sprintf(fnameptr
, ".eml");
1932 f
= fopen(filename
, "w+");
1935 PrintAndLog("Can't open file %s ", filename
);
1940 for (i
= 0; i
< numBlocks
; i
++) {
1941 if (mfEmlGetMem(buf
, i
, 1)) {
1942 PrintAndLog("Cant get block: %d", i
);
1945 for (j
= 0; j
< 16; j
++)
1946 fprintf(f
, "%02X", buf
[j
]);
1951 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1956 int CmdHF14AMfECFill(const char *Cmd
)
1958 uint8_t keyType
= 0;
1959 uint8_t numSectors
= 16;
1961 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1962 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1963 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1965 PrintAndLog("samples: hf mf ecfill A");
1966 PrintAndLog(" hf mf ecfill A 4");
1967 PrintAndLog("Read card and transfer its data to emulator memory.");
1968 PrintAndLog("Keys must be laid in the emulator memory. \n");
1972 char ctmp
= param_getchar(Cmd
, 0);
1973 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1974 PrintAndLog("Key type must be A or B");
1977 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1979 ctmp
= param_getchar(Cmd
, 1);
1981 case '0' : numSectors
= 5; break;
1983 case '\0': numSectors
= 16; break;
1984 case '2' : numSectors
= 32; break;
1985 case '4' : numSectors
= 40; break;
1986 default: numSectors
= 16;
1989 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1990 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1995 int CmdHF14AMfEKeyPrn(const char *Cmd
)
2000 uint64_t keyA
, keyB
;
2002 char cmdp
= param_getchar(Cmd
, 0);
2004 if ( cmdp
== 'h' || cmdp
== 'H' ) {
2005 PrintAndLog("It prints the keys loaded in the emulator memory");
2006 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2007 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2009 PrintAndLog(" sample: hf mf ekeyprn 1");
2014 case '0' : numSectors
= 5; break;
2016 case '\0': numSectors
= 16; break;
2017 case '2' : numSectors
= 32; break;
2018 case '4' : numSectors
= 40; break;
2019 default: numSectors
= 16;
2022 PrintAndLog("|---|----------------|----------------|");
2023 PrintAndLog("|sec|key A |key B |");
2024 PrintAndLog("|---|----------------|----------------|");
2025 for (i
= 0; i
< numSectors
; i
++) {
2026 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
2027 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
2030 keyA
= bytes_to_num(data
, 6);
2031 keyB
= bytes_to_num(data
+ 10, 6);
2032 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
2034 PrintAndLog("|---|----------------|----------------|");
2039 // CHINESE MAGIC COMMANDS
2041 int CmdHF14AMfCSetUID(const char *Cmd
) {
2042 uint8_t wipeCard
= 0;
2043 uint8_t uid
[8] = {0x00};
2044 uint8_t oldUid
[8] = {0x00};
2045 uint8_t atqa
[2] = {0x00};
2046 uint8_t sak
[1] = {0x00};
2047 uint8_t atqaPresent
= 1;
2052 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
2053 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2054 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2056 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2058 PrintAndLog("sample: hf mf csetuid 01020304");
2059 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2063 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2064 PrintAndLog("UID must include 8 HEX symbols");
2069 ctmp
= param_getchar(Cmd
, argi
);
2070 if (ctmp
== 'w' || ctmp
== 'W') {
2076 if (param_getchar(Cmd
, argi
)) {
2077 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2078 PrintAndLog("ATQA must include 4 HEX symbols");
2082 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2083 PrintAndLog("SAK must include 2 HEX symbols");
2092 ctmp
= param_getchar(Cmd
, argi
);
2093 if (ctmp
== 'w' || ctmp
== 'W') {
2098 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2100 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2102 PrintAndLog("Can't set UID. error=%d", res
);
2106 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2107 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2111 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2112 uint8_t block
[16] = {0x00};
2113 uint8_t blockNo
= 0;
2114 uint8_t params
= MAGIC_SINGLE
;
2117 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2118 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2119 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2120 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2121 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2125 blockNo
= param_get8(Cmd
, 0);
2127 if (param_gethex(Cmd
, 1, block
, 32)) {
2128 PrintAndLog("block data must include 32 HEX symbols");
2132 char ctmp
= param_getchar(Cmd
, 2);
2133 if (ctmp
== 'w' || ctmp
== 'W')
2134 params
|= MAGIC_WIPE
;
2136 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2138 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2140 PrintAndLog("Can't write block. error=%d", res
);
2146 int CmdHF14AMfCLoad(const char *Cmd
) {
2148 char filename
[FILE_PATH_SIZE
];
2149 char * fnameptr
= filename
;
2150 char buf
[64] = {0x00};
2151 uint8_t buf8
[64] = {0x00};
2152 uint8_t fillFromEmulator
= 0;
2153 int i
, len
, blockNum
, flags
=0;
2155 memset(filename
, 0, sizeof(filename
));
2157 char ctmp
= param_getchar(Cmd
, 0);
2159 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2160 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2161 PrintAndLog("or from emulator memory (option `e`)");
2162 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2163 PrintAndLog(" or: hf mf cload e ");
2164 PrintAndLog(" sample: hf mf cload filename");
2168 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2170 if (fillFromEmulator
) {
2171 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2172 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2173 PrintAndLog("Cant get block: %d", blockNum
);
2176 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2177 if (blockNum
== 1) flags
= 0; // just write
2178 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2180 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2181 PrintAndLog("Cant set magic card block: %d", blockNum
);
2188 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2190 memcpy(filename
, Cmd
, len
);
2193 sprintf(fnameptr
, ".eml");
2196 f
= fopen(filename
, "r");
2198 PrintAndLog("File not found or locked.");
2205 memset(buf
, 0, sizeof(buf
));
2207 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2209 PrintAndLog("File reading error.");
2213 if (strlen(buf
) < 32) {
2214 if(strlen(buf
) && feof(f
))
2216 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2220 for (i
= 0; i
< 32; i
+= 2)
2221 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2223 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2224 if (blockNum
== 1) flags
= 0; // just write
2225 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2227 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2228 PrintAndLog("Can't set magic card block: %d", blockNum
);
2234 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2239 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2240 PrintAndLog("File content error. There must be 64 blocks");
2243 PrintAndLog("Loaded from file: %s", filename
);
2249 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2251 uint8_t blockNo
= 0;
2253 memset(data
, 0x00, sizeof(data
));
2254 char ctmp
= param_getchar(Cmd
, 0);
2256 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2257 PrintAndLog("Usage: hf mf cgetblk <block number>");
2258 PrintAndLog("sample: hf mf cgetblk 1");
2259 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2263 blockNo
= param_get8(Cmd
, 0);
2265 PrintAndLog("--block number:%2d ", blockNo
);
2267 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2269 PrintAndLog("Can't read block. error=%d", res
);
2273 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2277 int CmdHF14AMfCGetSc(const char *Cmd
) {
2279 uint8_t sectorNo
= 0;
2281 memset(data
, 0x00, sizeof(data
));
2282 char ctmp
= param_getchar(Cmd
, 0);
2284 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2285 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2286 PrintAndLog("sample: hf mf cgetsc 0");
2287 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2291 sectorNo
= param_get8(Cmd
, 0);
2292 if (sectorNo
> 15) {
2293 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2297 PrintAndLog("--sector number:%d ", sectorNo
);
2298 PrintAndLog("block | data");
2300 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2301 for (i
= 0; i
< 4; i
++) {
2302 if (i
== 1) flags
= 0;
2303 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2305 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2307 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2310 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2315 int CmdHF14AMfCSave(const char *Cmd
) {
2318 char filename
[FILE_PATH_SIZE
];
2319 char * fnameptr
= filename
;
2320 uint8_t fillFromEmulator
= 0;
2322 int i
, j
, len
, flags
;
2324 memset(filename
, 0, sizeof(filename
));
2325 memset(buf
, 0, sizeof(buf
));
2326 char ctmp
= param_getchar(Cmd
, 0);
2328 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2329 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2330 PrintAndLog("or into emulator memory (option `e`)");
2331 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2332 PrintAndLog(" sample: hf mf esave ");
2333 PrintAndLog(" hf mf esave filename");
2334 PrintAndLog(" hf mf esave e \n");
2337 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2339 if (fillFromEmulator
) {
2340 // put into emulator
2341 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2342 for (i
= 0; i
< 16 * 4; i
++) {
2343 if (i
== 1) flags
= 0;
2344 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2346 if (mfCGetBlock(i
, buf
, flags
)) {
2347 PrintAndLog("Cant get block: %d", i
);
2351 if (mfEmlSetMem(buf
, i
, 1)) {
2352 PrintAndLog("Cant set emul block: %d", i
);
2359 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2361 // get filename based on UID
2364 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2365 PrintAndLog("Cant get block: %d", 0);
2366 len
= sprintf(fnameptr
, "dump");
2369 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2370 sprintf(fnameptr
, "%02x", buf
[j
]);
2373 memcpy(filename
, Cmd
, len
);
2377 // add .eml extension
2378 sprintf(fnameptr
, ".eml");
2381 f
= fopen(filename
, "w+");
2384 PrintAndLog("File not found or locked.");
2389 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2390 for (i
= 0; i
< 16 * 4; i
++) {
2391 if (i
== 1) flags
= 0;
2392 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2394 if (mfCGetBlock(i
, buf
, flags
)) {
2395 PrintAndLog("Cant get block: %d", i
);
2398 for (j
= 0; j
< 16; j
++)
2399 fprintf(f
, "%02x", buf
[j
]);
2404 PrintAndLog("Saved to file: %s", filename
);
2409 //needs nt, ar, at, Data to decrypt
2410 int CmdHf14MfDecryptBytes(const char *Cmd
){
2412 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2413 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2414 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2417 param_gethex_ex(Cmd
, 3, data
, &len
);
2420 int limit
= sizeof(data
) / 2;
2425 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2428 static command_t CommandTable
[] = {
2429 {"help", CmdHelp
, 1, "This help"},
2430 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2431 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2432 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2433 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2434 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2435 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2436 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2437 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2438 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2439 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2440 {"keybrute", CmdHF14AMfKeyBrute
, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2441 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2442 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2443 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2444 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2445 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2446 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2447 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2448 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2449 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2450 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2451 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2452 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2453 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2454 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2455 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2456 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2457 {NULL
, NULL
, 0, NULL
}
2460 int CmdHFMF(const char *Cmd
) {
2461 clearCommandBuffer();
2462 CmdsParse(CommandTable
, Cmd
);
2466 int CmdHelp(const char *Cmd
) {
2467 CmdsHelp(CommandTable
);