]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
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
== -1 && par_list
== 0) {
218 if (!nonce2key_ex(uid
, nt
, nr
, ks_list
, &r_key
) ){
219 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
225 if (isOK
!= 1) return 1;
227 // execute original function from util nonce2key
228 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
230 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
231 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
235 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
240 unsigned long elapsed_time
= difftime(end
, start
);
242 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
246 int CmdHF14AMfWrBl(const char *Cmd
) {
249 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
250 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
255 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
256 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
260 blockNo
= param_get8(Cmd
, 0);
261 cmdp
= param_getchar(Cmd
, 1);
263 PrintAndLog("Key type must be A or B");
266 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
267 if (param_gethex(Cmd
, 2, key
, 12)) {
268 PrintAndLog("Key must include 12 HEX symbols");
271 if (param_gethex(Cmd
, 3, bldata
, 32)) {
272 PrintAndLog("Block data must include 32 HEX symbols");
275 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
276 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
278 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
279 memcpy(c
.d
.asBytes
, key
, 6);
280 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
281 clearCommandBuffer();
285 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
286 uint8_t isOK
= resp
.arg
[0] & 0xff;
287 PrintAndLog("isOk:%02x", isOK
);
289 PrintAndLog("Command execute timeout");
295 int CmdHF14AMfRdBl(const char *Cmd
) {
298 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
304 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
305 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
309 blockNo
= param_get8(Cmd
, 0);
310 cmdp
= param_getchar(Cmd
, 1);
312 PrintAndLog("Key type must be A or B");
315 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
316 if (param_gethex(Cmd
, 2, key
, 12)) {
317 PrintAndLog("Key must include 12 HEX symbols");
320 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
322 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
323 memcpy(c
.d
.asBytes
, key
, 6);
324 clearCommandBuffer();
328 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
329 uint8_t isOK
= resp
.arg
[0] & 0xff;
330 uint8_t *data
= resp
.d
.asBytes
;
333 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
335 PrintAndLog("isOk:%02x", isOK
);
337 PrintAndLog("Command execute timeout");
343 int CmdHF14AMfRdSc(const char *Cmd
) {
345 uint8_t sectorNo
= 0;
347 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
349 uint8_t *data
= NULL
;
353 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
354 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
358 sectorNo
= param_get8(Cmd
, 0);
360 PrintAndLog("Sector number must be less than 40");
363 cmdp
= param_getchar(Cmd
, 1);
364 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
365 PrintAndLog("Key type must be A or B");
368 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
369 if (param_gethex(Cmd
, 2, key
, 12)) {
370 PrintAndLog("Key must include 12 HEX symbols");
373 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
375 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
376 memcpy(c
.d
.asBytes
, key
, 6);
377 clearCommandBuffer();
382 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
383 isOK
= resp
.arg
[0] & 0xff;
384 data
= resp
.d
.asBytes
;
386 PrintAndLog("isOk:%02x", isOK
);
388 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
389 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
391 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
394 PrintAndLog("Command execute timeout");
400 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
404 return 32 * 4 + (sectorNo
- 32) * 16;
408 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
416 int CmdHF14AMfDump(const char *Cmd
) {
417 uint8_t sectorNo
, blockNo
;
421 uint8_t rights
[40][4];
422 uint8_t carddata
[256][16];
423 uint8_t numSectors
= 16;
430 char cmdp
= param_getchar(Cmd
, 0);
432 case '0' : numSectors
= 5; break;
434 case '\0': numSectors
= 16; break;
435 case '2' : numSectors
= 32; break;
436 case '4' : numSectors
= 40; break;
437 default: numSectors
= 16;
440 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
441 PrintAndLog("Usage: hf mf dump [card memory]");
442 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
444 PrintAndLog("Samples: hf mf dump");
445 PrintAndLog(" hf mf dump 4");
449 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
450 PrintAndLog("Could not find file dumpkeys.bin");
454 // Read keys A from file
456 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
457 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
458 if ( bytes_read
== 0) {
459 PrintAndLog("File reading error.");
465 // Read keys B from file
466 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
467 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
468 if ( bytes_read
== 0) {
469 PrintAndLog("File reading error.");
477 PrintAndLog("|-----------------------------------------|");
478 PrintAndLog("|------ Reading sector access bits...-----|");
479 PrintAndLog("|-----------------------------------------|");
481 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
482 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
483 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
484 clearCommandBuffer();
487 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
488 uint8_t isOK
= resp
.arg
[0] & 0xff;
489 uint8_t *data
= resp
.d
.asBytes
;
491 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
492 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
493 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
494 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
496 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
497 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
498 rights
[sectorNo
][3] = 0x01;
501 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
502 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
503 rights
[sectorNo
][3] = 0x01;
507 PrintAndLog("|-----------------------------------------|");
508 PrintAndLog("|----- Dumping all blocks to file... -----|");
509 PrintAndLog("|-----------------------------------------|");
512 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
513 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
514 bool received
= false;
516 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
517 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
518 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
519 clearCommandBuffer();
521 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
522 } else { // data block. Check if it can be read with key A or key B
523 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
524 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
525 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
526 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
528 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
529 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
531 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
532 } else { // key A would work
533 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
534 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
535 clearCommandBuffer();
537 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
542 isOK
= resp
.arg
[0] & 0xff;
543 uint8_t *data
= resp
.d
.asBytes
;
544 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
545 data
[0] = (keyA
[sectorNo
][0]);
546 data
[1] = (keyA
[sectorNo
][1]);
547 data
[2] = (keyA
[sectorNo
][2]);
548 data
[3] = (keyA
[sectorNo
][3]);
549 data
[4] = (keyA
[sectorNo
][4]);
550 data
[5] = (keyA
[sectorNo
][5]);
551 data
[10] = (keyB
[sectorNo
][0]);
552 data
[11] = (keyB
[sectorNo
][1]);
553 data
[12] = (keyB
[sectorNo
][2]);
554 data
[13] = (keyB
[sectorNo
][3]);
555 data
[14] = (keyB
[sectorNo
][4]);
556 data
[15] = (keyB
[sectorNo
][5]);
559 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
560 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
562 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
568 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
575 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
576 PrintAndLog("Could not create file name dumpdata.bin");
579 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
580 fwrite(carddata
, 1, 16*numblocks
, fout
);
582 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
588 int CmdHF14AMfRestore(const char *Cmd
) {
589 uint8_t sectorNo
,blockNo
;
591 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
592 uint8_t bldata
[16] = {0x00};
600 char cmdp
= param_getchar(Cmd
, 0);
602 case '0' : numSectors
= 5; break;
604 case '\0': numSectors
= 16; break;
605 case '2' : numSectors
= 32; break;
606 case '4' : numSectors
= 40; break;
607 default: numSectors
= 16;
610 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
611 PrintAndLog("Usage: hf mf restore [card memory]");
612 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
614 PrintAndLog("Samples: hf mf restore");
615 PrintAndLog(" hf mf restore 4");
619 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
620 PrintAndLog("Could not find file dumpkeys.bin");
625 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
626 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
627 if ( bytes_read
== 0) {
628 PrintAndLog("File reading error (dumpkeys.bin).");
634 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
635 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
636 if ( bytes_read
== 0) {
637 PrintAndLog("File reading error (dumpkeys.bin).");
645 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
646 PrintAndLog("Could not find file dumpdata.bin");
649 PrintAndLog("Restoring dumpdata.bin to card");
651 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
652 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
653 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
654 memcpy(c
.d
.asBytes
, key
, 6);
655 bytes_read
= fread(bldata
, 1, 16, fdump
);
656 if ( bytes_read
== 0) {
657 PrintAndLog("File reading error (dumpdata.bin).");
662 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
663 bldata
[0] = (keyA
[sectorNo
][0]);
664 bldata
[1] = (keyA
[sectorNo
][1]);
665 bldata
[2] = (keyA
[sectorNo
][2]);
666 bldata
[3] = (keyA
[sectorNo
][3]);
667 bldata
[4] = (keyA
[sectorNo
][4]);
668 bldata
[5] = (keyA
[sectorNo
][5]);
669 bldata
[10] = (keyB
[sectorNo
][0]);
670 bldata
[11] = (keyB
[sectorNo
][1]);
671 bldata
[12] = (keyB
[sectorNo
][2]);
672 bldata
[13] = (keyB
[sectorNo
][3]);
673 bldata
[14] = (keyB
[sectorNo
][4]);
674 bldata
[15] = (keyB
[sectorNo
][5]);
677 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
679 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
680 clearCommandBuffer();
684 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
685 uint8_t isOK
= resp
.arg
[0] & 0xff;
686 PrintAndLog("isOk:%02x", isOK
);
688 PrintAndLog("Command execute timeout");
697 int CmdHF14AMfNested(const char *Cmd
) {
698 int i
, j
, res
, iterations
;
699 sector
*e_sector
= NULL
;
702 uint8_t trgBlockNo
= 0;
703 uint8_t trgKeyType
= 0;
704 uint8_t SectorsCnt
= 0;
705 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
706 uint8_t keyBlock
[6*6];
708 bool transferToEml
= false;
710 bool createDumpFile
= false;
712 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
713 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
715 if (strlen(Cmd
)<3) return usage_hf14_nested();
718 cmdp
= param_getchar(Cmd
, 0);
719 blockNo
= param_get8(Cmd
, 1);
720 ctmp
= param_getchar(Cmd
, 2);
722 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
723 PrintAndLog("Key type must be A or B");
727 if (ctmp
!= 'A' && ctmp
!= 'a')
730 if (param_gethex(Cmd
, 3, key
, 12)) {
731 PrintAndLog("Key must include 12 HEX symbols");
735 if (cmdp
== 'o' || cmdp
== 'O') {
737 trgBlockNo
= param_get8(Cmd
, 4);
738 ctmp
= param_getchar(Cmd
, 5);
739 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
740 PrintAndLog("Target key type must be A or B");
743 if (ctmp
!= 'A' && ctmp
!= 'a')
748 case '0': SectorsCnt
= 05; break;
749 case '1': SectorsCnt
= 16; break;
750 case '2': SectorsCnt
= 32; break;
751 case '4': SectorsCnt
= 40; break;
752 default: SectorsCnt
= 16;
756 ctmp
= param_getchar(Cmd
, 4);
757 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
758 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
760 ctmp
= param_getchar(Cmd
, 6);
761 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
762 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
765 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
767 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
768 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
769 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
770 case -4 : PrintAndLog("No valid key found"); break;
772 key64
= bytes_to_num(keyBlock
, 6);
774 // transfer key to the emulator
776 uint8_t sectortrailer
;
777 if (trgBlockNo
< 32*4) { // 4 block sector
778 sectortrailer
= (trgBlockNo
& 0x03) + 3;
779 } else { // 16 block sector
780 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
782 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
785 num_to_bytes(key64
, 6, keyBlock
);
787 num_to_bytes(key64
, 6, &keyBlock
[10]);
788 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
791 default : PrintAndLog("Unknown Error.\n");
795 else { // ------------------------------------ multiple sectors working
796 clock_t t1
= clock();
797 unsigned long elapsed_time
;
801 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
802 if (e_sector
== NULL
) return 1;
804 //test current key and additional standard keys first
805 memcpy(keyBlock
, key
, 6);
806 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
807 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
808 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
809 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
810 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
812 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
813 for (i
= 0; i
< SectorsCnt
; i
++) {
814 for (j
= 0; j
< 2; j
++) {
815 if (e_sector
[i
].foundKey
[j
]) continue;
817 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
820 e_sector
[i
].Key
[j
] = key64
;
821 e_sector
[i
].foundKey
[j
] = TRUE
;
825 clock_t t2
= clock() - t1
;
827 elapsed_time
= difftime(end
, start
);
829 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
831 PrintAndLog("enter nested...");
835 bool calibrate
= true;
837 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
838 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
839 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
841 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
843 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
845 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
846 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
847 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
848 case -4 : //key not found
855 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
856 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
859 default : PrintAndLog("Unknown Error.\n");
869 elapsed_time
= difftime(end
, start
);
871 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
874 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
875 PrintAndLog("trying to read key B...");
876 for (i
= 0; i
< SectorsCnt
; i
++) {
877 // KEY A but not KEY B
878 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
880 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
882 PrintAndLog("Reading block %d", sectrail
);
884 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
885 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
886 clearCommandBuffer();
890 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
892 uint8_t isOK
= resp
.arg
[0] & 0xff;
895 uint8_t *data
= resp
.d
.asBytes
;
896 key64
= bytes_to_num(data
+10, 6);
898 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
899 e_sector
[i
].foundKey
[1] = TRUE
;
900 e_sector
[i
].Key
[1] = key64
;
907 printKeyTable( SectorsCnt
, e_sector
);
909 // transfer them to the emulator
911 for (i
= 0; i
< SectorsCnt
; i
++) {
912 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
913 if (e_sector
[i
].foundKey
[0])
914 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
915 if (e_sector
[i
].foundKey
[1])
916 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
917 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
922 if (createDumpFile
) {
923 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
924 PrintAndLog("Could not create file dumpkeys.bin");
928 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
929 for(i
=0; i
<SectorsCnt
; i
++) {
930 if (e_sector
[i
].foundKey
[0]){
931 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
932 fwrite ( tempkey
, 1, 6, fkeys
);
935 fwrite ( &standart
, 1, 6, fkeys
);
938 for(i
=0; i
<SectorsCnt
; i
++) {
939 if (e_sector
[i
].foundKey
[1]){
940 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
941 fwrite ( tempkey
, 1, 6, fkeys
);
944 fwrite ( &standart
, 1, 6, fkeys
);
955 int CmdHF14AMfNestedHard(const char *Cmd
) {
958 uint8_t trgBlockNo
= 0;
959 uint8_t trgKeyType
= 0;
960 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
961 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
964 ctmp
= param_getchar(Cmd
, 0);
965 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
966 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
968 bool know_target_key
= false;
969 bool nonce_file_read
= false;
970 bool nonce_file_write
= false;
974 if (ctmp
== 'R' || ctmp
== 'r') {
975 nonce_file_read
= true;
976 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
977 know_target_key
= true;
979 } else if (ctmp
== 'T' || ctmp
== 't') {
980 tests
= param_get32ex(Cmd
, 1, 100, 10);
982 blockNo
= param_get8(Cmd
, 0);
983 ctmp
= param_getchar(Cmd
, 1);
984 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
985 PrintAndLog("Key type must be A or B");
988 if (ctmp
!= 'A' && ctmp
!= 'a') {
992 if (param_gethex(Cmd
, 2, key
, 12)) {
993 PrintAndLog("Key must include 12 HEX symbols");
997 trgBlockNo
= param_get8(Cmd
, 3);
998 ctmp
= param_getchar(Cmd
, 4);
999 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1000 PrintAndLog("Target key type must be A or B");
1003 if (ctmp
!= 'A' && ctmp
!= 'a') {
1009 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
1010 know_target_key
= true;
1014 while ((ctmp
= param_getchar(Cmd
, i
))) {
1015 if (ctmp
== 's' || ctmp
== 'S') {
1017 } else if (ctmp
== 'w' || ctmp
== 'W') {
1018 nonce_file_write
= true;
1020 PrintAndLog("Possible options are w and/or s");
1027 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 ",
1030 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1031 know_target_key
? "" : " (not set)",
1032 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1033 slow
? "Yes" : "No",
1036 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1040 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1041 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1050 int CmdHF14AMfChk(const char *Cmd
) {
1052 if (strlen(Cmd
)<3) return usage_hf14_chk();
1055 char filename
[FILE_PATH_SIZE
]={0};
1057 uint8_t *keyBlock
= NULL
, *p
;
1058 uint8_t stKeyBlock
= 20;
1060 sector
*e_sector
= NULL
;
1065 uint8_t blockNo
= 0;
1066 uint8_t SectorsCnt
= 1;
1067 uint8_t keyType
= 0;
1070 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1072 int transferToEml
= 0;
1073 int createDumpFile
= 0;
1075 keyBlock
= calloc(stKeyBlock
, 6);
1076 if (keyBlock
== NULL
) return 1;
1078 uint64_t defaultKeys
[] = {
1079 0xffffffffffff, // Default key (first key used by program if no user defined key)
1080 0x000000000000, // Blank key
1081 0xa0a1a2a3a4a5, // NFCForum MAD key
1093 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1095 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1096 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1099 if (param_getchar(Cmd
, 0)=='*') {
1101 switch(param_getchar(Cmd
+1, 0)) {
1102 case '0': SectorsCnt
= 5; break;
1103 case '1': SectorsCnt
= 16; break;
1104 case '2': SectorsCnt
= 32; break;
1105 case '4': SectorsCnt
= 40; break;
1106 default: SectorsCnt
= 16;
1109 blockNo
= param_get8(Cmd
, 0);
1112 ctmp
= param_getchar(Cmd
, 1);
1124 PrintAndLog("Key type must be A , B or ?");
1129 ctmp
= param_getchar(Cmd
, 2);
1130 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1131 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1133 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1134 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1135 if ( stKeyBlock
- keycnt
< 2) {
1136 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1138 PrintAndLog("Cannot allocate memory for Keys");
1144 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1145 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1146 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1149 // May be a dic file
1150 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1151 PrintAndLog("File name too long");
1156 if ( (f
= fopen( filename
, "r")) ) {
1157 while( fgets(buf
, sizeof(buf
), f
) ){
1158 if (strlen(buf
) < 12 || buf
[11] == '\n')
1161 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1163 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1165 if (!isxdigit(buf
[0])){
1166 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1172 if ( stKeyBlock
- keycnt
< 2) {
1173 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1175 PrintAndLog("Cannot allocate memory for defKeys");
1182 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1183 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1184 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1186 memset(buf
, 0, sizeof(buf
));
1190 PrintAndLog("File: %s: not found or locked.", filename
);
1199 PrintAndLog("No key specified, trying default keys");
1200 for (;keycnt
< defaultKeysSize
; keycnt
++)
1201 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1202 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1203 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1206 // initialize storage for found keys
1207 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1208 if (e_sector
== NULL
) {
1214 for(int i
= 0; i
< SectorsCnt
; ++i
){
1215 e_sector
[i
].Key
[0] = 0xffffffffffff;
1216 e_sector
[i
].Key
[1] = 0xffffffffffff;
1217 e_sector
[i
].foundKey
[0] = FALSE
;
1218 e_sector
[i
].foundKey
[1] = FALSE
;
1222 uint8_t trgKeyType
= 0;
1223 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1226 clock_t t1
= clock();
1231 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1234 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1236 // skip already found keys.
1237 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1239 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1242 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1244 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1246 e_sector
[i
].Key
[trgKeyType
] = key64
;
1247 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1251 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1256 unsigned long elapsed_time
= difftime(end
, start
);
1258 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1261 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1262 if ( keyType
!= 1 ) {
1263 PrintAndLog("testing to read key B...");
1264 for (i
= 0; i
< SectorsCnt
; i
++) {
1265 // KEY A but not KEY B
1266 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1268 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1270 PrintAndLog("Reading block %d", sectrail
);
1272 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1273 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1274 clearCommandBuffer();
1278 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1280 uint8_t isOK
= resp
.arg
[0] & 0xff;
1281 if (!isOK
) continue;
1283 uint8_t *data
= resp
.d
.asBytes
;
1284 key64
= bytes_to_num(data
+10, 6);
1286 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1287 e_sector
[i
].foundKey
[1] = 1;
1288 e_sector
[i
].Key
[1] = key64
;
1296 printKeyTable( SectorsCnt
, e_sector
);
1298 if (transferToEml
) {
1299 uint8_t block
[16] = {0x00};
1300 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1301 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1302 if (e_sector
[i
].foundKey
[0])
1303 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1304 if (e_sector
[i
].foundKey
[1])
1305 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1306 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1308 PrintAndLog("Found keys have been transferred to the emulator memory");
1311 if (createDumpFile
) {
1312 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1313 if (fkeys
== NULL
) {
1314 PrintAndLog("Could not create file dumpkeys.bin");
1319 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1321 for( i
=0; i
<SectorsCnt
; i
++) {
1322 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1323 fwrite ( tempkey
, 1, 6, fkeys
);
1325 for(i
=0; i
<SectorsCnt
; i
++) {
1326 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1327 fwrite ( tempkey
, 1, 6, fkeys
);
1330 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1338 #define ATTACK_KEY_COUNT 8
1339 sector
*k_sector
= NULL
;
1340 uint8_t k_sectorsCount
= 16;
1341 void readerAttack(nonces_t data
[], bool setEmulatorMem
) {
1343 // initialize storage for found keys
1344 if (k_sector
== NULL
)
1345 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1346 if (k_sector
== NULL
)
1352 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1353 k_sector
[i
].Key
[0] = 0xffffffffffff;
1354 k_sector
[i
].Key
[1] = 0xffffffffffff;
1355 k_sector
[i
].foundKey
[0] = FALSE
;
1356 k_sector
[i
].foundKey
[1] = FALSE
;
1359 printf("enter reader attack\n");
1360 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1361 if (data
[i
].ar2
> 0) {
1363 if (tryMfk32(data
[i
], &key
)) {
1364 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1365 , (data
[i
].keytype
) ? "B" : "A"
1370 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1371 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1373 //set emulator memory for keys
1374 if (setEmulatorMem
) {
1375 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1376 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1377 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1378 mfEmlSetMem( memBlock
, i
*4 + 3, 1);
1379 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1381 , sprint_hex( memBlock
, sizeof(memBlock
))
1387 // if (tryMfk32_moebius(data[i+ATTACK_KEY_COUNT], &key)) {
1388 // PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"
1389 // ,(data[i+ATTACK_KEY_COUNT].keytype) ? "B" : "A"
1390 // , data[i+ATTACK_KEY_COUNT].sector
1398 int CmdHF14AMf1kSim(const char *Cmd
) {
1400 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1401 uint8_t exitAfterNReads
= 0;
1402 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1404 bool setEmulatorMem
= false;
1406 bool errors
= false;
1408 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1409 switch(param_getchar(Cmd
, cmdp
)) {
1412 setEmulatorMem
= true;
1417 return usage_hf14_mf1ksim();
1420 flags
|= FLAG_INTERACTIVE
;
1425 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1430 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1432 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1433 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1434 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1435 default: return usage_hf14_mf1ksim();
1441 flags
|= FLAG_NR_AR_ATTACK
;
1445 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1452 if(errors
) return usage_hf14_mf1ksim();
1454 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1455 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1460 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1461 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1462 clearCommandBuffer();
1465 if(flags
& FLAG_INTERACTIVE
) {
1466 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1468 nonces_t data
[ATTACK_KEY_COUNT
*2];
1472 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1474 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1475 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1477 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1478 readerAttack(data
, setEmulatorMem
);
1481 if (k_sector
!= NULL
) {
1482 printKeyTable(k_sectorsCount
, k_sector
);
1489 int CmdHF14AMfSniff(const char *Cmd
){
1490 bool wantLogToFile
= FALSE
;
1491 bool wantDecrypt
= FALSE
;
1492 //bool wantSaveToEml = FALSE; TODO
1493 bool wantSaveToEmlFile
= FALSE
;
1503 uint8_t uid_len
= 0;
1504 uint8_t atqa
[2] = {0x00, 0x00};
1507 uint8_t *buf
= NULL
;
1508 uint16_t bufsize
= 0;
1509 uint8_t *bufPtr
= NULL
;
1510 uint16_t traceLen
= 0;
1512 memset(uid
, 0x00, sizeof(uid
));
1514 char ctmp
= param_getchar(Cmd
, 0);
1515 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1517 for (int i
= 0; i
< 4; i
++) {
1518 ctmp
= param_getchar(Cmd
, i
);
1519 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1520 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1521 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1522 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1525 printf("-------------------------------------------------------------------------\n");
1526 printf("Executing mifare sniffing command. \n");
1527 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1528 printf("Press the key on pc keyboard to abort the client.\n");
1529 printf("-------------------------------------------------------------------------\n");
1531 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1532 clearCommandBuffer();
1540 tmpchar
= getchar();
1542 printf("\naborted via keyboard!\n");
1547 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1548 res
= resp
.arg
[0] & 0xff;
1549 traceLen
= resp
.arg
[1];
1558 if (res
== 1) { // there is (more) data to be transferred
1559 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1560 if (traceLen
> bufsize
) {
1562 if (buf
== NULL
) // not yet allocated
1563 p
= malloc(traceLen
);
1564 else // need more memory
1565 p
= realloc(buf
, traceLen
);
1568 PrintAndLog("Cannot allocate memory for trace");
1576 memset(buf
, 0x00, traceLen
);
1578 if (bufPtr
== NULL
) {
1579 PrintAndLog("Cannot allocate memory for trace");
1583 // what happens if LEN is bigger then TRACELEN --iceman
1584 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1589 if (res
== 2) { // received all data, start displaying
1590 blockLen
= bufPtr
- buf
;
1593 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1594 while (bufPtr
- buf
< blockLen
) {
1595 bufPtr
+= 6; // skip (void) timing information
1596 len
= *((uint16_t *)bufPtr
);
1604 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1605 memcpy(uid
, bufPtr
+ 2, 10);
1606 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1607 switch (atqa
[0] & 0xC0) {
1608 case 0x80: uid_len
= 10; break;
1609 case 0x40: uid_len
= 7; break;
1610 default: uid_len
= 4; break;
1613 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1614 sprint_hex(uid
, uid_len
),
1618 if (wantLogToFile
|| wantDecrypt
) {
1619 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1620 AddLogCurrentDT(logHexFileName
);
1623 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1625 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1627 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1629 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1633 bufPtr
+= ((len
-1)/8+1); // ignore parity
1644 int CmdHF14AMfDbg(const char *Cmd
) {
1646 char ctmp
= param_getchar(Cmd
, 0);
1647 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1649 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1650 if (dbgMode
> 4) return usage_hf14_dbg();
1652 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1657 int CmdHF14AMfKeyBrute(const char *Cmd
) {
1659 uint8_t blockNo
= 0, keytype
= 0;
1660 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
1661 uint64_t foundkey
= 0;
1663 char cmdp
= param_getchar(Cmd
, 0);
1664 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_keybrute();
1667 blockNo
= param_get8(Cmd
, 0);
1670 cmdp
= param_getchar(Cmd
, 1);
1671 if (cmdp
== 'B' || cmdp
== 'b') keytype
= 1;
1674 if (param_gethex(Cmd
, 2, key
, 12)) return usage_hf14_keybrute();
1676 clock_t t1
= clock();
1680 if (mfKeyBrute( blockNo
, keytype
, key
, &foundkey
))
1681 PrintAndLog("Found valid key: %012"llx
" \n", foundkey
);
1683 PrintAndLog("Key not found");
1687 unsigned long elapsed_time
= difftime(end
, start
);
1689 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1694 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1695 PrintAndLog("|---|----------------|---|----------------|---|");
1696 PrintAndLog("|sec|key A |res|key B |res|");
1697 PrintAndLog("|---|----------------|---|----------------|---|");
1698 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1699 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1700 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1701 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1704 PrintAndLog("|---|----------------|---|----------------|---|");
1707 // EMULATOR COMMANDS
1708 int CmdHF14AMfEGet(const char *Cmd
)
1710 uint8_t blockNo
= 0;
1711 uint8_t data
[16] = {0x00};
1713 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1714 PrintAndLog("Usage: hf mf eget <block number>");
1715 PrintAndLog(" sample: hf mf eget 0 ");
1719 blockNo
= param_get8(Cmd
, 0);
1722 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1723 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1725 PrintAndLog("Command execute timeout");
1731 int CmdHF14AMfEClear(const char *Cmd
)
1733 if (param_getchar(Cmd
, 0) == 'h') {
1734 PrintAndLog("Usage: hf mf eclr");
1735 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1739 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1744 int CmdHF14AMfESet(const char *Cmd
)
1746 uint8_t memBlock
[16];
1747 uint8_t blockNo
= 0;
1749 memset(memBlock
, 0x00, sizeof(memBlock
));
1751 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1752 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1753 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1757 blockNo
= param_get8(Cmd
, 0);
1759 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1760 PrintAndLog("block data must include 32 HEX symbols");
1765 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1766 memcpy(c
.d
.asBytes
, memBlock
, 16);
1771 int CmdHF14AMfELoad(const char *Cmd
)
1774 char filename
[FILE_PATH_SIZE
];
1775 char *fnameptr
= filename
;
1776 char buf
[64] = {0x00};
1777 uint8_t buf8
[64] = {0x00};
1778 int i
, len
, blockNum
, numBlocks
;
1779 int nameParamNo
= 1;
1780 uint8_t blockWidth
= 32;
1781 char ctmp
= param_getchar(Cmd
, 0);
1783 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1784 PrintAndLog("It loads emul dump from the file `filename.eml`");
1785 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1786 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1788 PrintAndLog(" sample: hf mf eload filename");
1789 PrintAndLog(" hf mf eload 4 filename");
1794 case '0' : numBlocks
= 5*4; break;
1796 case '\0': numBlocks
= 16*4; break;
1797 case '2' : numBlocks
= 32*4; break;
1798 case '4' : numBlocks
= 256; break;
1799 case 'U' : // fall through
1800 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1806 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1807 if (numblk2
> 0) numBlocks
= numblk2
;
1809 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1811 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1815 sprintf(fnameptr
, ".eml");
1818 f
= fopen(filename
, "r");
1820 PrintAndLog("File %s not found or locked", filename
);
1826 memset(buf
, 0, sizeof(buf
));
1828 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1830 if (blockNum
>= numBlocks
) break;
1832 PrintAndLog("File reading error.");
1837 if (strlen(buf
) < blockWidth
){
1838 if(strlen(buf
) && feof(f
))
1840 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1845 for (i
= 0; i
< blockWidth
; i
+= 2) {
1846 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1848 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1849 PrintAndLog("Cant set emul block: %3d", blockNum
);
1856 if (blockNum
>= numBlocks
) break;
1861 if ((blockNum
!= numBlocks
)) {
1862 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1865 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1869 int CmdHF14AMfESave(const char *Cmd
)
1872 char filename
[FILE_PATH_SIZE
];
1873 char * fnameptr
= filename
;
1875 int i
, j
, len
, numBlocks
;
1876 int nameParamNo
= 1;
1878 memset(filename
, 0, sizeof(filename
));
1879 memset(buf
, 0, sizeof(buf
));
1881 char ctmp
= param_getchar(Cmd
, 0);
1883 if ( ctmp
== 'h' || ctmp
== 'H') {
1884 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1885 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1886 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1888 PrintAndLog(" sample: hf mf esave ");
1889 PrintAndLog(" hf mf esave 4");
1890 PrintAndLog(" hf mf esave 4 filename");
1895 case '0' : numBlocks
= 5*4; break;
1897 case '\0': numBlocks
= 16*4; break;
1898 case '2' : numBlocks
= 32*4; break;
1899 case '4' : numBlocks
= 256; break;
1906 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1908 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1910 // user supplied filename?
1912 // get filename (UID from memory)
1913 if (mfEmlGetMem(buf
, 0, 1)) {
1914 PrintAndLog("Can\'t get UID from block: %d", 0);
1915 len
= sprintf(fnameptr
, "dump");
1919 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1920 sprintf(fnameptr
, "%02X", buf
[j
]);
1926 // add file extension
1927 sprintf(fnameptr
, ".eml");
1930 f
= fopen(filename
, "w+");
1933 PrintAndLog("Can't open file %s ", filename
);
1938 for (i
= 0; i
< numBlocks
; i
++) {
1939 if (mfEmlGetMem(buf
, i
, 1)) {
1940 PrintAndLog("Cant get block: %d", i
);
1943 for (j
= 0; j
< 16; j
++)
1944 fprintf(f
, "%02X", buf
[j
]);
1949 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1954 int CmdHF14AMfECFill(const char *Cmd
)
1956 uint8_t keyType
= 0;
1957 uint8_t numSectors
= 16;
1959 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1960 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1961 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1963 PrintAndLog("samples: hf mf ecfill A");
1964 PrintAndLog(" hf mf ecfill A 4");
1965 PrintAndLog("Read card and transfer its data to emulator memory.");
1966 PrintAndLog("Keys must be laid in the emulator memory. \n");
1970 char ctmp
= param_getchar(Cmd
, 0);
1971 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1972 PrintAndLog("Key type must be A or B");
1975 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1977 ctmp
= param_getchar(Cmd
, 1);
1979 case '0' : numSectors
= 5; break;
1981 case '\0': numSectors
= 16; break;
1982 case '2' : numSectors
= 32; break;
1983 case '4' : numSectors
= 40; break;
1984 default: numSectors
= 16;
1987 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1988 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1993 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1998 uint64_t keyA
, keyB
;
2000 char cmdp
= param_getchar(Cmd
, 0);
2002 if ( cmdp
== 'h' || cmdp
== 'H' ) {
2003 PrintAndLog("It prints the keys loaded in the emulator memory");
2004 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2005 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2007 PrintAndLog(" sample: hf mf ekeyprn 1");
2012 case '0' : numSectors
= 5; break;
2014 case '\0': numSectors
= 16; break;
2015 case '2' : numSectors
= 32; break;
2016 case '4' : numSectors
= 40; break;
2017 default: numSectors
= 16;
2020 PrintAndLog("|---|----------------|----------------|");
2021 PrintAndLog("|sec|key A |key B |");
2022 PrintAndLog("|---|----------------|----------------|");
2023 for (i
= 0; i
< numSectors
; i
++) {
2024 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
2025 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
2028 keyA
= bytes_to_num(data
, 6);
2029 keyB
= bytes_to_num(data
+ 10, 6);
2030 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
2032 PrintAndLog("|---|----------------|----------------|");
2037 // CHINESE MAGIC COMMANDS
2039 int CmdHF14AMfCSetUID(const char *Cmd
) {
2040 uint8_t wipeCard
= 0;
2041 uint8_t uid
[8] = {0x00};
2042 uint8_t oldUid
[8] = {0x00};
2043 uint8_t atqa
[2] = {0x00};
2044 uint8_t sak
[1] = {0x00};
2045 uint8_t atqaPresent
= 1;
2050 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
2051 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2052 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2054 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2056 PrintAndLog("sample: hf mf csetuid 01020304");
2057 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2061 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2062 PrintAndLog("UID must include 8 HEX symbols");
2067 ctmp
= param_getchar(Cmd
, argi
);
2068 if (ctmp
== 'w' || ctmp
== 'W') {
2074 if (param_getchar(Cmd
, argi
)) {
2075 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2076 PrintAndLog("ATQA must include 4 HEX symbols");
2080 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2081 PrintAndLog("SAK must include 2 HEX symbols");
2090 ctmp
= param_getchar(Cmd
, argi
);
2091 if (ctmp
== 'w' || ctmp
== 'W') {
2096 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2098 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2100 PrintAndLog("Can't set UID. error=%d", res
);
2104 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2105 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2109 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2110 uint8_t block
[16] = {0x00};
2111 uint8_t blockNo
= 0;
2112 uint8_t params
= MAGIC_SINGLE
;
2115 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2116 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2117 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2118 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2119 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2123 blockNo
= param_get8(Cmd
, 0);
2125 if (param_gethex(Cmd
, 1, block
, 32)) {
2126 PrintAndLog("block data must include 32 HEX symbols");
2130 char ctmp
= param_getchar(Cmd
, 2);
2131 if (ctmp
== 'w' || ctmp
== 'W')
2132 params
|= MAGIC_WIPE
;
2134 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2136 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2138 PrintAndLog("Can't write block. error=%d", res
);
2144 int CmdHF14AMfCLoad(const char *Cmd
) {
2146 char filename
[FILE_PATH_SIZE
];
2147 char * fnameptr
= filename
;
2148 char buf
[64] = {0x00};
2149 uint8_t buf8
[64] = {0x00};
2150 uint8_t fillFromEmulator
= 0;
2151 int i
, len
, blockNum
, flags
=0;
2153 memset(filename
, 0, sizeof(filename
));
2155 char ctmp
= param_getchar(Cmd
, 0);
2157 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2158 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2159 PrintAndLog("or from emulator memory (option `e`)");
2160 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2161 PrintAndLog(" or: hf mf cload e ");
2162 PrintAndLog(" sample: hf mf cload filename");
2166 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2168 if (fillFromEmulator
) {
2169 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2170 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2171 PrintAndLog("Cant get block: %d", blockNum
);
2174 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2175 if (blockNum
== 1) flags
= 0; // just write
2176 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2178 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2179 PrintAndLog("Cant set magic card block: %d", blockNum
);
2186 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2188 memcpy(filename
, Cmd
, len
);
2191 sprintf(fnameptr
, ".eml");
2194 f
= fopen(filename
, "r");
2196 PrintAndLog("File not found or locked.");
2203 memset(buf
, 0, sizeof(buf
));
2205 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2207 PrintAndLog("File reading error.");
2211 if (strlen(buf
) < 32) {
2212 if(strlen(buf
) && feof(f
))
2214 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2218 for (i
= 0; i
< 32; i
+= 2)
2219 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2221 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2222 if (blockNum
== 1) flags
= 0; // just write
2223 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2225 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2226 PrintAndLog("Can't set magic card block: %d", blockNum
);
2232 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2237 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2238 PrintAndLog("File content error. There must be 64 blocks");
2241 PrintAndLog("Loaded from file: %s", filename
);
2247 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2249 uint8_t blockNo
= 0;
2251 memset(data
, 0x00, sizeof(data
));
2252 char ctmp
= param_getchar(Cmd
, 0);
2254 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2255 PrintAndLog("Usage: hf mf cgetblk <block number>");
2256 PrintAndLog("sample: hf mf cgetblk 1");
2257 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2261 blockNo
= param_get8(Cmd
, 0);
2263 PrintAndLog("--block number:%2d ", blockNo
);
2265 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2267 PrintAndLog("Can't read block. error=%d", res
);
2271 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2275 int CmdHF14AMfCGetSc(const char *Cmd
) {
2277 uint8_t sectorNo
= 0;
2279 memset(data
, 0x00, sizeof(data
));
2280 char ctmp
= param_getchar(Cmd
, 0);
2282 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2283 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2284 PrintAndLog("sample: hf mf cgetsc 0");
2285 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2289 sectorNo
= param_get8(Cmd
, 0);
2290 if (sectorNo
> 15) {
2291 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2295 PrintAndLog("--sector number:%d ", sectorNo
);
2296 PrintAndLog("block | data");
2298 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2299 for (i
= 0; i
< 4; i
++) {
2300 if (i
== 1) flags
= 0;
2301 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2303 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2305 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2308 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2313 int CmdHF14AMfCSave(const char *Cmd
) {
2316 char filename
[FILE_PATH_SIZE
];
2317 char * fnameptr
= filename
;
2318 uint8_t fillFromEmulator
= 0;
2320 int i
, j
, len
, flags
;
2322 memset(filename
, 0, sizeof(filename
));
2323 memset(buf
, 0, sizeof(buf
));
2324 char ctmp
= param_getchar(Cmd
, 0);
2326 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2327 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2328 PrintAndLog("or into emulator memory (option `e`)");
2329 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2330 PrintAndLog(" sample: hf mf esave ");
2331 PrintAndLog(" hf mf esave filename");
2332 PrintAndLog(" hf mf esave e \n");
2335 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2337 if (fillFromEmulator
) {
2338 // put into emulator
2339 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2340 for (i
= 0; i
< 16 * 4; i
++) {
2341 if (i
== 1) flags
= 0;
2342 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2344 if (mfCGetBlock(i
, buf
, flags
)) {
2345 PrintAndLog("Cant get block: %d", i
);
2349 if (mfEmlSetMem(buf
, i
, 1)) {
2350 PrintAndLog("Cant set emul block: %d", i
);
2357 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2359 // get filename based on UID
2362 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2363 PrintAndLog("Cant get block: %d", 0);
2364 len
= sprintf(fnameptr
, "dump");
2367 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2368 sprintf(fnameptr
, "%02x", buf
[j
]);
2371 memcpy(filename
, Cmd
, len
);
2375 // add .eml extension
2376 sprintf(fnameptr
, ".eml");
2379 f
= fopen(filename
, "w+");
2382 PrintAndLog("File not found or locked.");
2387 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2388 for (i
= 0; i
< 16 * 4; i
++) {
2389 if (i
== 1) flags
= 0;
2390 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2392 if (mfCGetBlock(i
, buf
, flags
)) {
2393 PrintAndLog("Cant get block: %d", i
);
2396 for (j
= 0; j
< 16; j
++)
2397 fprintf(f
, "%02x", buf
[j
]);
2402 PrintAndLog("Saved to file: %s", filename
);
2407 //needs nt, ar, at, Data to decrypt
2408 int CmdHf14MfDecryptBytes(const char *Cmd
){
2410 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2411 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2412 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2415 param_gethex_ex(Cmd
, 3, data
, &len
);
2418 int limit
= sizeof(data
) / 2;
2423 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2426 static command_t CommandTable
[] = {
2427 {"help", CmdHelp
, 1, "This help"},
2428 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2429 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2430 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2431 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2432 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2433 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2434 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2435 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2436 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2437 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2438 {"keybrute", CmdHF14AMfKeyBrute
, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2439 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2440 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2441 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2442 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2443 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2444 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2445 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2446 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2447 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2448 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2449 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2450 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2451 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2452 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2453 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2454 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2455 {NULL
, NULL
, 0, NULL
}
2458 int CmdHFMF(const char *Cmd
) {
2459 clearCommandBuffer();
2460 CmdsParse(CommandTable
, Cmd
);
2464 int CmdHelp(const char *Cmd
) {
2465 CmdsHelp(CommandTable
);