]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
ea73bf959f4b926957a09d772cd5815da326c80b
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
14 int usage_hf14_mifare(void){
15 PrintAndLog("Usage: hf mf mifare [h] <block number> <A|B>");
16 PrintAndLog("options:");
17 PrintAndLog(" h this help");
18 PrintAndLog(" <block number> (Optional) target other block");
19 PrintAndLog(" <A|B> (optional) target key type");
20 PrintAndLog("samples:");
21 PrintAndLog(" hf mf mifare");
22 PrintAndLog(" hf mf mifare 16");
23 PrintAndLog(" hf mf mifare 16 B");
26 int usage_hf14_mf1ksim(void){
27 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14,20 hex symbols)> n <numreads> i x");
28 PrintAndLog("options:");
29 PrintAndLog(" h this help");
30 PrintAndLog(" u (Optional) UID 4,7 or 10bytes. If not specified, the UID 4b from emulator memory will be used");
31 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
32 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
33 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
34 PrintAndLog(" e (Optional) Fill simulator keys from what we crack");
35 PrintAndLog(" v (Optional) Show maths used for cracking reader. Useful for debugging.");
36 PrintAndLog("samples:");
37 PrintAndLog(" hf mf sim u 0a0a0a0a");
38 PrintAndLog(" hf mf sim u 11223344556677");
39 PrintAndLog(" hf mf sim u 112233445566778899AA");
42 int usage_hf14_dbg(void){
43 PrintAndLog("Usage: hf mf dbg [h] <debug level>");
44 PrintAndLog("options:");
45 PrintAndLog(" h this help");
46 PrintAndLog(" <debug level> (Optional) see list for valid levels");
47 PrintAndLog(" 0 - no debug messages");
48 PrintAndLog(" 1 - error messages");
49 PrintAndLog(" 2 - plus information messages");
50 PrintAndLog(" 3 - plus debug messages");
51 PrintAndLog(" 4 - print even debug messages in timing critical functions");
52 PrintAndLog(" Note: this option therefore may cause malfunction itself");
53 PrintAndLog("samples:");
54 PrintAndLog(" hf mf dbg 3");
57 int usage_hf14_sniff(void){
58 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
59 PrintAndLog("Usage: hf mf sniff [h] [l] [d] [f]");
60 PrintAndLog("options:");
61 PrintAndLog(" h this help");
62 PrintAndLog(" l save encrypted sequence to logfile `uid.log`");
63 PrintAndLog(" d decrypt sequence and put it to log file `uid.log`");
64 // PrintAndLog(" n/a e decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
65 PrintAndLog(" f decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
66 PrintAndLog("sample:");
67 PrintAndLog(" hf mf sniff l d f");
70 int usage_hf14_nested(void){
71 PrintAndLog("Usage:");
72 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
73 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
74 PrintAndLog(" <target block number> <target key A/B> [t]");
75 PrintAndLog("options:");
76 PrintAndLog(" h this help");
77 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
78 PrintAndLog(" t transfer keys into emulator memory");
79 PrintAndLog(" d write keys to binary file");
81 PrintAndLog("samples:");
82 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF ");
83 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF t ");
84 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF d ");
85 PrintAndLog(" hf mf nested o 0 A FFFFFFFFFFFF 4 A");
88 int usage_hf14_hardnested(void){
89 PrintAndLog("Usage:");
90 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
91 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
92 PrintAndLog(" or hf mf hardnested r [known target key]");
94 PrintAndLog("options:");
95 PrintAndLog(" h this help");
96 PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
97 PrintAndLog(" s slower acquisition (required by some non standard cards)");
98 PrintAndLog(" r read nonces.bin and start attack");
99 PrintAndLog(" t tests?");
101 PrintAndLog("samples:");
102 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
103 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
104 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
105 PrintAndLog(" hf mf hardnested r");
106 PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
108 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
109 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
112 int usage_hf14_chk(void){
113 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
114 PrintAndLog("options:");
115 PrintAndLog(" h this help");
116 PrintAndLog(" * all sectors based on card memory, other values then below defaults to 1k");
117 PrintAndLog(" 0 - MINI(320 bytes)");
118 PrintAndLog(" 1 - 1K");
119 PrintAndLog(" 2 - 2K");
120 PrintAndLog(" 4 - 4K");
121 PrintAndLog(" d write keys to binary file");
122 PrintAndLog(" t write keys to emulator memory\n");
124 PrintAndLog("samples:");
125 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic -- target block 0, Key A");
126 PrintAndLog(" hf mf chk *1 ? t -- target all blocks, all keys, 1K, write to emul");
127 PrintAndLog(" hf mf chk *1 ? d -- target all blocks, all keys, 1K, write to file");
130 int usage_hf14_keybrute(void){
131 PrintAndLog("J_Run's 2nd phase of multiple sector nested authentication key recovery");
132 PrintAndLog("You have a known 4 last bytes of a key recovered with mf_nonce_brute tool.");
133 PrintAndLog("First 2 bytes of key will be bruteforced");
135 PrintAndLog("Usage: hf mf keybrute [h] <block number> <A|B> <key>");
136 PrintAndLog("options:");
137 PrintAndLog(" h this help");
138 PrintAndLog(" <block number> target block number");
139 PrintAndLog(" <A|B> target key type");
140 PrintAndLog(" <key> candidate key from mf_nonce_brute tool");
141 PrintAndLog("samples:");
142 PrintAndLog(" hf mf keybrute 1 A 000011223344");
146 int CmdHF14AMifare(const char *Cmd
) {
148 uint32_t nt
= 0, nr
= 0;
149 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
152 uint8_t blockNo
= 0, keytype
= MIFARE_AUTH_KEYA
;
154 char cmdp
= param_getchar(Cmd
, 0);
155 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
157 blockNo
= param_get8(Cmd
, 0);
159 cmdp
= param_getchar(Cmd
, 1);
160 if (cmdp
== 'B' || cmdp
== 'b')
161 keytype
= MIFARE_AUTH_KEYB
;
163 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, keytype
}};
166 printf("-------------------------------------------------------------------------\n");
167 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
168 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
169 printf("-------------------------------------------------------------------------\n");
170 clock_t t1
= clock();
175 clearCommandBuffer();
191 printf("\naborted via keyboard!\n");
196 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
199 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
200 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
201 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
202 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
203 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
206 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
207 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
208 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
209 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
210 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
219 if (isOK
== -4 && par_list
== 0) {
220 // this special attack when parities is zero, uses checkkeys. Which now with block/keytype option also needs.
221 // but it uses 0|1 instead of 0x60|0x61...
222 if (nonce2key_ex(blockNo
, keytype
- 0x60 , uid
, nt
, nr
, ks_list
, &r_key
) ){
223 PrintAndLog("Key not found (lfsr_common_prefix list is null).");
224 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
228 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
234 if (isOK
!= 1) return 1;
236 // execute original function from util nonce2key
237 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
239 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
240 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
245 // nonce2key found a candidate key. Lets verify it.
246 uint8_t keyblock
[] = {0,0,0,0,0,0};
247 num_to_bytes(r_key
, 6, keyblock
);
249 int res
= mfCheckKeys(blockNo
, keytype
- 0x60 , false, 1, keyblock
, &key64
);
251 PrintAndLog("Candidate Key found (%012"llx
") - Test authentication failed. Starting over darkside attack", r_key
);
254 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
259 unsigned long elapsed_time
= difftime(end
, start
);
261 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
265 int CmdHF14AMfWrBl(const char *Cmd
) {
268 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
269 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
274 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
275 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
279 blockNo
= param_get8(Cmd
, 0);
280 cmdp
= param_getchar(Cmd
, 1);
282 PrintAndLog("Key type must be A or B");
285 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
286 if (param_gethex(Cmd
, 2, key
, 12)) {
287 PrintAndLog("Key must include 12 HEX symbols");
290 if (param_gethex(Cmd
, 3, bldata
, 32)) {
291 PrintAndLog("Block data must include 32 HEX symbols");
294 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
295 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
297 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
298 memcpy(c
.d
.asBytes
, key
, 6);
299 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
300 clearCommandBuffer();
304 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
305 uint8_t isOK
= resp
.arg
[0] & 0xff;
306 PrintAndLog("isOk:%02x", isOK
);
308 PrintAndLog("Command execute timeout");
314 int CmdHF14AMfRdBl(const char *Cmd
) {
317 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
323 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
324 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
328 blockNo
= param_get8(Cmd
, 0);
329 cmdp
= param_getchar(Cmd
, 1);
331 PrintAndLog("Key type must be A or B");
334 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
335 if (param_gethex(Cmd
, 2, key
, 12)) {
336 PrintAndLog("Key must include 12 HEX symbols");
339 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
341 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
342 memcpy(c
.d
.asBytes
, key
, 6);
343 clearCommandBuffer();
347 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
348 uint8_t isOK
= resp
.arg
[0] & 0xff;
349 uint8_t *data
= resp
.d
.asBytes
;
352 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
354 PrintAndLog("isOk:%02x", isOK
);
356 PrintAndLog("Command execute timeout");
362 int CmdHF14AMfRdSc(const char *Cmd
) {
364 uint8_t sectorNo
= 0;
366 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
368 uint8_t *data
= NULL
;
372 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
373 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
377 sectorNo
= param_get8(Cmd
, 0);
379 PrintAndLog("Sector number must be less than 40");
382 cmdp
= param_getchar(Cmd
, 1);
383 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
384 PrintAndLog("Key type must be A or B");
387 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
388 if (param_gethex(Cmd
, 2, key
, 12)) {
389 PrintAndLog("Key must include 12 HEX symbols");
392 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
394 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
395 memcpy(c
.d
.asBytes
, key
, 6);
396 clearCommandBuffer();
401 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
402 isOK
= resp
.arg
[0] & 0xff;
403 data
= resp
.d
.asBytes
;
405 PrintAndLog("isOk:%02x", isOK
);
407 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
408 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
410 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
413 PrintAndLog("Command execute timeout");
419 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
423 return 32 * 4 + (sectorNo
- 32) * 16;
427 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
435 int CmdHF14AMfDump(const char *Cmd
) {
436 uint8_t sectorNo
, blockNo
;
440 uint8_t rights
[40][4];
441 uint8_t carddata
[256][16];
442 uint8_t numSectors
= 16;
449 char cmdp
= param_getchar(Cmd
, 0);
451 case '0' : numSectors
= 5; break;
453 case '\0': numSectors
= 16; break;
454 case '2' : numSectors
= 32; break;
455 case '4' : numSectors
= 40; break;
456 default: numSectors
= 16;
459 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
460 PrintAndLog("Usage: hf mf dump [card memory]");
461 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
463 PrintAndLog("Samples: hf mf dump");
464 PrintAndLog(" hf mf dump 4");
468 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
469 PrintAndLog("Could not find file dumpkeys.bin");
473 // Read keys A from file
475 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
476 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
477 if ( bytes_read
== 0) {
478 PrintAndLog("File reading error.");
485 // Read keys B from file
486 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
487 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
488 if ( bytes_read
== 0) {
489 PrintAndLog("File reading error.");
499 PrintAndLog("|-----------------------------------------|");
500 PrintAndLog("|------ Reading sector access bits...-----|");
501 PrintAndLog("|-----------------------------------------|");
503 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
504 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
505 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
506 clearCommandBuffer();
509 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
510 uint8_t isOK
= resp
.arg
[0] & 0xff;
511 uint8_t *data
= resp
.d
.asBytes
;
513 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
514 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
515 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
516 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
518 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
519 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
520 rights
[sectorNo
][3] = 0x01;
523 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
524 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
525 rights
[sectorNo
][3] = 0x01;
529 PrintAndLog("|-----------------------------------------|");
530 PrintAndLog("|----- Dumping all blocks to file... -----|");
531 PrintAndLog("|-----------------------------------------|");
534 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
535 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
536 bool received
= false;
538 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
539 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
540 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
541 clearCommandBuffer();
543 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
544 } else { // data block. Check if it can be read with key A or key B
545 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
546 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
547 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
548 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
550 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
551 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
553 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
554 } else { // key A would work
555 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
556 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
557 clearCommandBuffer();
559 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
564 isOK
= resp
.arg
[0] & 0xff;
565 uint8_t *data
= resp
.d
.asBytes
;
566 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
567 data
[0] = (keyA
[sectorNo
][0]);
568 data
[1] = (keyA
[sectorNo
][1]);
569 data
[2] = (keyA
[sectorNo
][2]);
570 data
[3] = (keyA
[sectorNo
][3]);
571 data
[4] = (keyA
[sectorNo
][4]);
572 data
[5] = (keyA
[sectorNo
][5]);
573 data
[10] = (keyB
[sectorNo
][0]);
574 data
[11] = (keyB
[sectorNo
][1]);
575 data
[12] = (keyB
[sectorNo
][2]);
576 data
[13] = (keyB
[sectorNo
][3]);
577 data
[14] = (keyB
[sectorNo
][4]);
578 data
[15] = (keyB
[sectorNo
][5]);
581 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
582 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
584 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
590 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
597 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
598 PrintAndLog("Could not create file name dumpdata.bin");
601 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
602 fwrite(carddata
, 1, 16*numblocks
, fout
);
605 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
611 int CmdHF14AMfRestore(const char *Cmd
) {
612 uint8_t sectorNo
,blockNo
;
614 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
615 uint8_t bldata
[16] = {0x00};
623 char cmdp
= param_getchar(Cmd
, 0);
625 case '0' : numSectors
= 5; break;
627 case '\0': numSectors
= 16; break;
628 case '2' : numSectors
= 32; break;
629 case '4' : numSectors
= 40; break;
630 default: numSectors
= 16;
633 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
634 PrintAndLog("Usage: hf mf restore [card memory]");
635 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
637 PrintAndLog("Samples: hf mf restore");
638 PrintAndLog(" hf mf restore 4");
642 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
643 PrintAndLog("Could not find file dumpkeys.bin");
648 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
649 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
650 if ( bytes_read
== 0) {
651 PrintAndLog("File reading error (dumpkeys.bin).");
658 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
659 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
660 if ( bytes_read
== 0) {
661 PrintAndLog("File reading error (dumpkeys.bin).");
670 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
671 PrintAndLog("Could not find file dumpdata.bin");
674 PrintAndLog("Restoring dumpdata.bin to card");
676 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
677 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
678 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
679 memcpy(c
.d
.asBytes
, key
, 6);
680 bytes_read
= fread(bldata
, 1, 16, fdump
);
681 if ( bytes_read
== 0) {
682 PrintAndLog("File reading error (dumpdata.bin).");
688 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
689 bldata
[0] = (keyA
[sectorNo
][0]);
690 bldata
[1] = (keyA
[sectorNo
][1]);
691 bldata
[2] = (keyA
[sectorNo
][2]);
692 bldata
[3] = (keyA
[sectorNo
][3]);
693 bldata
[4] = (keyA
[sectorNo
][4]);
694 bldata
[5] = (keyA
[sectorNo
][5]);
695 bldata
[10] = (keyB
[sectorNo
][0]);
696 bldata
[11] = (keyB
[sectorNo
][1]);
697 bldata
[12] = (keyB
[sectorNo
][2]);
698 bldata
[13] = (keyB
[sectorNo
][3]);
699 bldata
[14] = (keyB
[sectorNo
][4]);
700 bldata
[15] = (keyB
[sectorNo
][5]);
703 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
705 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
706 clearCommandBuffer();
710 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
711 uint8_t isOK
= resp
.arg
[0] & 0xff;
712 PrintAndLog("isOk:%02x", isOK
);
714 PrintAndLog("Command execute timeout");
724 int CmdHF14AMfNested(const char *Cmd
) {
725 int i
, j
, res
, iterations
;
726 sector
*e_sector
= NULL
;
729 uint8_t trgBlockNo
= 0;
730 uint8_t trgKeyType
= 0;
731 uint8_t SectorsCnt
= 0;
732 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
733 uint8_t keyBlock
[6*6];
735 bool transferToEml
= false;
737 bool createDumpFile
= false;
739 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
740 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
742 if (strlen(Cmd
)<3) return usage_hf14_nested();
745 cmdp
= param_getchar(Cmd
, 0);
746 blockNo
= param_get8(Cmd
, 1);
747 ctmp
= param_getchar(Cmd
, 2);
749 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
750 PrintAndLog("Key type must be A or B");
754 if (ctmp
!= 'A' && ctmp
!= 'a')
757 if (param_gethex(Cmd
, 3, key
, 12)) {
758 PrintAndLog("Key must include 12 HEX symbols");
762 if (cmdp
== 'o' || cmdp
== 'O') {
764 trgBlockNo
= param_get8(Cmd
, 4);
765 ctmp
= param_getchar(Cmd
, 5);
766 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
767 PrintAndLog("Target key type must be A or B");
770 if (ctmp
!= 'A' && ctmp
!= 'a')
775 case '0': SectorsCnt
= 05; break;
776 case '1': SectorsCnt
= 16; break;
777 case '2': SectorsCnt
= 32; break;
778 case '4': SectorsCnt
= 40; break;
779 default: SectorsCnt
= 16;
783 ctmp
= param_getchar(Cmd
, 4);
784 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
785 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
787 ctmp
= param_getchar(Cmd
, 6);
788 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
789 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
792 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
794 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
795 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
796 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
797 case -4 : PrintAndLog("No valid key found"); break;
799 key64
= bytes_to_num(keyBlock
, 6);
801 // transfer key to the emulator
803 uint8_t sectortrailer
;
804 if (trgBlockNo
< 32*4) { // 4 block sector
805 sectortrailer
= (trgBlockNo
& 0x03) + 3;
806 } else { // 16 block sector
807 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
809 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
812 num_to_bytes(key64
, 6, keyBlock
);
814 num_to_bytes(key64
, 6, &keyBlock
[10]);
815 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
818 default : PrintAndLog("Unknown Error.\n");
822 else { // ------------------------------------ multiple sectors working
823 clock_t t1
= clock();
824 unsigned long elapsed_time
;
828 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
829 if (e_sector
== NULL
) return 1;
831 //test current key and additional standard keys first
832 memcpy(keyBlock
, key
, 6);
833 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
834 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
835 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
836 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
837 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
839 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
840 for (i
= 0; i
< SectorsCnt
; i
++) {
841 for (j
= 0; j
< 2; j
++) {
842 if (e_sector
[i
].foundKey
[j
]) continue;
844 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
847 e_sector
[i
].Key
[j
] = key64
;
848 e_sector
[i
].foundKey
[j
] = TRUE
;
852 clock_t t2
= clock() - t1
;
854 elapsed_time
= difftime(end
, start
);
856 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
858 PrintAndLog("enter nested...");
862 bool calibrate
= true;
864 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
865 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
866 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
868 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
870 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
872 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
873 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
874 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
875 case -4 : //key not found
882 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
883 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
886 default : PrintAndLog("Unknown Error.\n");
896 elapsed_time
= difftime(end
, start
);
898 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
901 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
902 PrintAndLog("trying to read key B...");
903 for (i
= 0; i
< SectorsCnt
; i
++) {
904 // KEY A but not KEY B
905 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
907 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
909 PrintAndLog("Reading block %d", sectrail
);
911 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
912 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
913 clearCommandBuffer();
917 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
919 uint8_t isOK
= resp
.arg
[0] & 0xff;
922 uint8_t *data
= resp
.d
.asBytes
;
923 key64
= bytes_to_num(data
+10, 6);
925 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
926 e_sector
[i
].foundKey
[1] = TRUE
;
927 e_sector
[i
].Key
[1] = key64
;
934 printKeyTable( SectorsCnt
, e_sector
);
936 // transfer them to the emulator
938 for (i
= 0; i
< SectorsCnt
; i
++) {
939 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
940 if (e_sector
[i
].foundKey
[0])
941 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
942 if (e_sector
[i
].foundKey
[1])
943 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
944 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
949 if (createDumpFile
) {
950 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
951 PrintAndLog("Could not create file dumpkeys.bin");
955 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
956 for(i
=0; i
<SectorsCnt
; i
++) {
957 if (e_sector
[i
].foundKey
[0]){
958 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
959 fwrite ( tempkey
, 1, 6, fkeys
);
962 fwrite ( &standart
, 1, 6, fkeys
);
965 for(i
=0; i
<SectorsCnt
; i
++) {
966 if (e_sector
[i
].foundKey
[1]){
967 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
968 fwrite ( tempkey
, 1, 6, fkeys
);
971 fwrite ( &standart
, 1, 6, fkeys
);
982 int CmdHF14AMfNestedHard(const char *Cmd
) {
985 uint8_t trgBlockNo
= 0;
986 uint8_t trgKeyType
= 0;
987 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
988 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
991 ctmp
= param_getchar(Cmd
, 0);
992 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
993 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
995 bool know_target_key
= false;
996 bool nonce_file_read
= false;
997 bool nonce_file_write
= false;
1001 if (ctmp
== 'R' || ctmp
== 'r') {
1002 nonce_file_read
= true;
1003 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
1004 know_target_key
= true;
1006 } else if (ctmp
== 'T' || ctmp
== 't') {
1007 tests
= param_get32ex(Cmd
, 1, 100, 10);
1009 blockNo
= param_get8(Cmd
, 0);
1010 ctmp
= param_getchar(Cmd
, 1);
1011 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1012 PrintAndLog("Key type must be A or B");
1015 if (ctmp
!= 'A' && ctmp
!= 'a') {
1019 if (param_gethex(Cmd
, 2, key
, 12)) {
1020 PrintAndLog("Key must include 12 HEX symbols");
1024 trgBlockNo
= param_get8(Cmd
, 3);
1025 ctmp
= param_getchar(Cmd
, 4);
1026 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1027 PrintAndLog("Target key type must be A or B");
1030 if (ctmp
!= 'A' && ctmp
!= 'a') {
1036 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
1037 know_target_key
= true;
1041 while ((ctmp
= param_getchar(Cmd
, i
))) {
1042 if (ctmp
== 's' || ctmp
== 'S') {
1044 } else if (ctmp
== 'w' || ctmp
== 'W') {
1045 nonce_file_write
= true;
1047 PrintAndLog("Possible options are w and/or s");
1054 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 ",
1057 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1058 know_target_key
? "" : " (not set)",
1059 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1060 slow
? "Yes" : "No",
1063 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1067 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1068 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1077 int CmdHF14AMfChk(const char *Cmd
) {
1079 if (strlen(Cmd
)<3) return usage_hf14_chk();
1082 char filename
[FILE_PATH_SIZE
]={0};
1084 uint8_t *keyBlock
= NULL
, *p
;
1085 uint8_t stKeyBlock
= 20;
1087 sector
*e_sector
= NULL
;
1092 uint8_t blockNo
= 0;
1093 uint8_t SectorsCnt
= 1;
1094 uint8_t keyType
= 0;
1097 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1099 int transferToEml
= 0;
1100 int createDumpFile
= 0;
1102 keyBlock
= calloc(stKeyBlock
, 6);
1103 if (keyBlock
== NULL
) return 1;
1105 uint64_t defaultKeys
[] = {
1106 0xffffffffffff, // Default key (first key used by program if no user defined key)
1107 0x000000000000, // Blank key
1108 0xa0a1a2a3a4a5, // NFCForum MAD key
1120 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1122 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1123 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1126 if (param_getchar(Cmd
, 0)=='*') {
1128 switch(param_getchar(Cmd
+1, 0)) {
1129 case '0': SectorsCnt
= 5; break;
1130 case '1': SectorsCnt
= 16; break;
1131 case '2': SectorsCnt
= 32; break;
1132 case '4': SectorsCnt
= 40; break;
1133 default: SectorsCnt
= 16;
1136 blockNo
= param_get8(Cmd
, 0);
1139 ctmp
= param_getchar(Cmd
, 1);
1151 PrintAndLog("Key type must be A , B or ?");
1156 ctmp
= param_getchar(Cmd
, 2);
1157 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1158 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1160 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1161 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1162 if ( stKeyBlock
- keycnt
< 2) {
1163 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1165 PrintAndLog("Cannot allocate memory for Keys");
1171 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1172 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1173 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1176 // May be a dic file
1177 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1178 PrintAndLog("File name too long");
1183 if ( (f
= fopen( filename
, "r")) ) {
1184 while( fgets(buf
, sizeof(buf
), f
) ){
1185 if (strlen(buf
) < 12 || buf
[11] == '\n')
1188 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1190 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1192 if (!isxdigit(buf
[0])){
1193 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1199 if ( stKeyBlock
- keycnt
< 2) {
1200 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1202 PrintAndLog("Cannot allocate memory for defKeys");
1209 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1210 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1211 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1213 memset(buf
, 0, sizeof(buf
));
1217 PrintAndLog("File: %s: not found or locked.", filename
);
1226 PrintAndLog("No key specified, trying default keys");
1227 for (;keycnt
< defaultKeysSize
; keycnt
++)
1228 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1229 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1230 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1233 // initialize storage for found keys
1234 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1235 if (e_sector
== NULL
) {
1241 for(int i
= 0; i
< SectorsCnt
; ++i
){
1242 e_sector
[i
].Key
[0] = 0xffffffffffff;
1243 e_sector
[i
].Key
[1] = 0xffffffffffff;
1244 e_sector
[i
].foundKey
[0] = FALSE
;
1245 e_sector
[i
].foundKey
[1] = FALSE
;
1249 uint8_t trgKeyType
= 0;
1250 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1253 clock_t t1
= clock();
1258 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1261 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1263 // skip already found keys.
1264 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1266 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1269 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1271 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1273 e_sector
[i
].Key
[trgKeyType
] = key64
;
1274 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1278 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1283 unsigned long elapsed_time
= difftime(end
, start
);
1285 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1288 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1289 if ( keyType
!= 1 ) {
1290 PrintAndLog("testing to read key B...");
1291 for (i
= 0; i
< SectorsCnt
; i
++) {
1292 // KEY A but not KEY B
1293 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1295 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1297 PrintAndLog("Reading block %d", sectrail
);
1299 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1300 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1301 clearCommandBuffer();
1305 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1307 uint8_t isOK
= resp
.arg
[0] & 0xff;
1308 if (!isOK
) continue;
1310 uint8_t *data
= resp
.d
.asBytes
;
1311 key64
= bytes_to_num(data
+10, 6);
1313 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1314 e_sector
[i
].foundKey
[1] = 1;
1315 e_sector
[i
].Key
[1] = key64
;
1323 printKeyTable( SectorsCnt
, e_sector
);
1325 if (transferToEml
) {
1326 uint8_t block
[16] = {0x00};
1327 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1328 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1329 if (e_sector
[i
].foundKey
[0])
1330 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1331 if (e_sector
[i
].foundKey
[1])
1332 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1333 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1335 PrintAndLog("Found keys have been transferred to the emulator memory");
1338 if (createDumpFile
) {
1339 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1340 if (fkeys
== NULL
) {
1341 PrintAndLog("Could not create file dumpkeys.bin");
1346 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1348 for( i
=0; i
<SectorsCnt
; i
++) {
1349 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1350 fwrite ( tempkey
, 1, 6, fkeys
);
1352 for(i
=0; i
<SectorsCnt
; i
++) {
1353 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1354 fwrite ( tempkey
, 1, 6, fkeys
);
1357 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1365 #define ATTACK_KEY_COUNT 8
1366 sector
*k_sector
= NULL
;
1367 uint8_t k_sectorsCount
= 16;
1368 void readerAttack(nonces_t data
[], bool setEmulatorMem
, bool showMaths
) {
1370 // initialize storage for found keys
1371 if (k_sector
== NULL
)
1372 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1373 if (k_sector
== NULL
)
1379 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1380 k_sector
[i
].Key
[0] = 0xffffffffffff;
1381 k_sector
[i
].Key
[1] = 0xffffffffffff;
1382 k_sector
[i
].foundKey
[0] = FALSE
;
1383 k_sector
[i
].foundKey
[1] = FALSE
;
1386 printf("enter reader attack\n");
1387 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1388 if (data
[i
].ar2
> 0) {
1390 // We can probably skip this, mfkey32v2 is more reliable.
1391 #ifdef HFMF_TRYMFK32
1392 if (tryMfk32(data
[i
], &key
)) {
1393 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1394 , (data
[i
].keytype
) ? "B" : "A"
1399 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1400 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1402 //set emulator memory for keys
1403 if (setEmulatorMem
) {
1404 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1405 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1406 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1407 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1408 , ((data
[i
].sector
)*4) + 3
1409 , sprint_hex( memBlock
, sizeof(memBlock
))
1411 mfEmlSetMem( memBlock
, ((data
[i
].sector
)*4) + 3, 1);
1417 if (tryMfk32_moebius(data
[i
+ATTACK_KEY_COUNT
], &key
, showMaths
)) {
1418 uint8_t sectorNum
= data
[i
+ATTACK_KEY_COUNT
].sector
;
1419 uint8_t keyType
= data
[i
+ATTACK_KEY_COUNT
].keytype
;
1421 PrintAndLog("M-Found Key%s for sector %02d: [%012"llx
"]"
1422 , keyType
? "B" : "A"
1427 k_sector
[sectorNum
].Key
[keyType
] = key
;
1428 k_sector
[sectorNum
].foundKey
[keyType
] = TRUE
;
1430 //set emulator memory for keys
1431 if (setEmulatorMem
) {
1432 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1433 num_to_bytes( k_sector
[sectorNum
].Key
[0], 6, memBlock
);
1434 num_to_bytes( k_sector
[sectorNum
].Key
[1], 6, memBlock
+10);
1435 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1437 , sprint_hex( memBlock
, sizeof(memBlock
))
1439 mfEmlSetMem( memBlock
, (sectorNum
*4) + 3, 1);
1448 int CmdHF14AMf1kSim(const char *Cmd
) {
1450 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1451 uint8_t exitAfterNReads
= 0;
1452 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1454 bool setEmulatorMem
= false;
1456 bool errors
= false;
1458 // If set to true, we should show our workings when doing NR_AR_ATTACK.
1459 bool showMaths
= false;
1461 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1462 switch(param_getchar(Cmd
, cmdp
)) {
1465 setEmulatorMem
= true;
1470 return usage_hf14_mf1ksim();
1473 flags
|= FLAG_INTERACTIVE
;
1478 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1483 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1485 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1486 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1487 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1488 default: return usage_hf14_mf1ksim();
1499 flags
|= FLAG_NR_AR_ATTACK
;
1503 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1510 if(errors
) return usage_hf14_mf1ksim();
1512 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1513 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1518 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1519 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1520 clearCommandBuffer();
1523 if(flags
& FLAG_INTERACTIVE
) {
1524 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1526 nonces_t data
[ATTACK_KEY_COUNT
*2];
1530 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1532 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1533 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1535 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1536 readerAttack(data
, setEmulatorMem
, showMaths
);
1539 if (k_sector
!= NULL
) {
1540 printKeyTable(k_sectorsCount
, k_sector
);
1548 int CmdHF14AMfSniff(const char *Cmd
){
1549 bool wantLogToFile
= FALSE
;
1550 bool wantDecrypt
= FALSE
;
1551 //bool wantSaveToEml = FALSE; TODO
1552 bool wantSaveToEmlFile
= FALSE
;
1562 uint8_t uid_len
= 0;
1563 uint8_t atqa
[2] = {0x00, 0x00};
1566 uint8_t *buf
= NULL
;
1567 uint16_t bufsize
= 0;
1568 uint8_t *bufPtr
= NULL
;
1569 uint16_t traceLen
= 0;
1571 memset(uid
, 0x00, sizeof(uid
));
1573 char ctmp
= param_getchar(Cmd
, 0);
1574 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1576 for (int i
= 0; i
< 4; i
++) {
1577 ctmp
= param_getchar(Cmd
, i
);
1578 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1579 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1580 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1581 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1584 printf("-------------------------------------------------------------------------\n");
1585 printf("Executing mifare sniffing command. \n");
1586 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1587 printf("Press the key on pc keyboard to abort the client.\n");
1588 printf("-------------------------------------------------------------------------\n");
1590 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1591 clearCommandBuffer();
1599 tmpchar
= getchar();
1601 printf("\naborted via keyboard!\n");
1606 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1607 res
= resp
.arg
[0] & 0xff;
1608 traceLen
= resp
.arg
[1];
1617 if (res
== 1) { // there is (more) data to be transferred
1618 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1619 if (traceLen
> bufsize
) {
1621 if (buf
== NULL
) // not yet allocated
1622 p
= malloc(traceLen
);
1623 else // need more memory
1624 p
= realloc(buf
, traceLen
);
1627 PrintAndLog("Cannot allocate memory for trace");
1635 memset(buf
, 0x00, traceLen
);
1637 if (bufPtr
== NULL
) {
1638 PrintAndLog("Cannot allocate memory for trace");
1642 // what happens if LEN is bigger then TRACELEN --iceman
1643 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1648 if (res
== 2) { // received all data, start displaying
1649 blockLen
= bufPtr
- buf
;
1652 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1653 while (bufPtr
- buf
< blockLen
) {
1654 bufPtr
+= 6; // skip (void) timing information
1655 len
= *((uint16_t *)bufPtr
);
1663 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1664 memcpy(uid
, bufPtr
+ 2, 10);
1665 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1666 switch (atqa
[0] & 0xC0) {
1667 case 0x80: uid_len
= 10; break;
1668 case 0x40: uid_len
= 7; break;
1669 default: uid_len
= 4; break;
1672 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1673 sprint_hex(uid
, uid_len
),
1677 if (wantLogToFile
|| wantDecrypt
) {
1678 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1679 AddLogCurrentDT(logHexFileName
);
1682 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1684 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1686 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1688 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1692 bufPtr
+= ((len
-1)/8+1); // ignore parity
1703 int CmdHF14AMfDbg(const char *Cmd
) {
1705 char ctmp
= param_getchar(Cmd
, 0);
1706 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1708 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1709 if (dbgMode
> 4) return usage_hf14_dbg();
1711 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1716 int CmdHF14AMfKeyBrute(const char *Cmd
) {
1718 uint8_t blockNo
= 0, keytype
= 0;
1719 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
1720 uint64_t foundkey
= 0;
1722 char cmdp
= param_getchar(Cmd
, 0);
1723 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_keybrute();
1726 blockNo
= param_get8(Cmd
, 0);
1729 cmdp
= param_getchar(Cmd
, 1);
1730 if (cmdp
== 'B' || cmdp
== 'b') keytype
= 1;
1733 if (param_gethex(Cmd
, 2, key
, 12)) return usage_hf14_keybrute();
1735 clock_t t1
= clock();
1739 if (mfKeyBrute( blockNo
, keytype
, key
, &foundkey
))
1740 PrintAndLog("Found valid key: %012"llx
" \n", foundkey
);
1742 PrintAndLog("Key not found");
1746 unsigned long elapsed_time
= difftime(end
, start
);
1748 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1753 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1754 PrintAndLog("|---|----------------|---|----------------|---|");
1755 PrintAndLog("|sec|key A |res|key B |res|");
1756 PrintAndLog("|---|----------------|---|----------------|---|");
1757 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1758 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1759 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1760 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1763 PrintAndLog("|---|----------------|---|----------------|---|");
1766 // EMULATOR COMMANDS
1767 int CmdHF14AMfEGet(const char *Cmd
)
1769 uint8_t blockNo
= 0;
1770 uint8_t data
[16] = {0x00};
1772 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1773 PrintAndLog("Usage: hf mf eget <block number>");
1774 PrintAndLog(" sample: hf mf eget 0 ");
1778 blockNo
= param_get8(Cmd
, 0);
1781 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1782 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1784 PrintAndLog("Command execute timeout");
1790 int CmdHF14AMfEClear(const char *Cmd
)
1792 if (param_getchar(Cmd
, 0) == 'h') {
1793 PrintAndLog("Usage: hf mf eclr");
1794 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1798 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1803 int CmdHF14AMfESet(const char *Cmd
)
1805 uint8_t memBlock
[16];
1806 uint8_t blockNo
= 0;
1808 memset(memBlock
, 0x00, sizeof(memBlock
));
1810 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1811 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1812 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1816 blockNo
= param_get8(Cmd
, 0);
1818 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1819 PrintAndLog("block data must include 32 HEX symbols");
1824 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1825 memcpy(c
.d
.asBytes
, memBlock
, 16);
1830 int CmdHF14AMfELoad(const char *Cmd
)
1833 char filename
[FILE_PATH_SIZE
];
1834 char *fnameptr
= filename
;
1835 char buf
[64] = {0x00};
1836 uint8_t buf8
[64] = {0x00};
1837 int i
, len
, blockNum
, numBlocks
;
1838 int nameParamNo
= 1;
1839 uint8_t blockWidth
= 32;
1840 char ctmp
= param_getchar(Cmd
, 0);
1842 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1843 PrintAndLog("It loads emul dump from the file `filename.eml`");
1844 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1845 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1847 PrintAndLog(" sample: hf mf eload filename");
1848 PrintAndLog(" hf mf eload 4 filename");
1853 case '0' : numBlocks
= 5*4; break;
1855 case '\0': numBlocks
= 16*4; break;
1856 case '2' : numBlocks
= 32*4; break;
1857 case '4' : numBlocks
= 256; break;
1858 case 'U' : // fall through
1859 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1865 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1866 if (numblk2
> 0) numBlocks
= numblk2
;
1868 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1870 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1874 sprintf(fnameptr
, ".eml");
1877 f
= fopen(filename
, "r");
1879 PrintAndLog("File %s not found or locked", filename
);
1885 memset(buf
, 0, sizeof(buf
));
1887 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1889 if (blockNum
>= numBlocks
) break;
1891 PrintAndLog("File reading error.");
1896 if (strlen(buf
) < blockWidth
){
1897 if(strlen(buf
) && feof(f
))
1899 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1904 for (i
= 0; i
< blockWidth
; i
+= 2) {
1905 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1907 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1908 PrintAndLog("Cant set emul block: %3d", blockNum
);
1915 if (blockNum
>= numBlocks
) break;
1920 if ((blockNum
!= numBlocks
)) {
1921 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1924 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1928 int CmdHF14AMfESave(const char *Cmd
)
1931 char filename
[FILE_PATH_SIZE
];
1932 char * fnameptr
= filename
;
1934 int i
, j
, len
, numBlocks
;
1935 int nameParamNo
= 1;
1937 memset(filename
, 0, sizeof(filename
));
1938 memset(buf
, 0, sizeof(buf
));
1940 char ctmp
= param_getchar(Cmd
, 0);
1942 if ( ctmp
== 'h' || ctmp
== 'H') {
1943 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1944 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1945 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1947 PrintAndLog(" sample: hf mf esave ");
1948 PrintAndLog(" hf mf esave 4");
1949 PrintAndLog(" hf mf esave 4 filename");
1954 case '0' : numBlocks
= 5*4; break;
1956 case '\0': numBlocks
= 16*4; break;
1957 case '2' : numBlocks
= 32*4; break;
1958 case '4' : numBlocks
= 256; break;
1965 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1967 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1969 // user supplied filename?
1971 // get filename (UID from memory)
1972 if (mfEmlGetMem(buf
, 0, 1)) {
1973 PrintAndLog("Can\'t get UID from block: %d", 0);
1974 len
= sprintf(fnameptr
, "dump");
1978 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1979 sprintf(fnameptr
, "%02X", buf
[j
]);
1985 // add file extension
1986 sprintf(fnameptr
, ".eml");
1989 f
= fopen(filename
, "w+");
1992 PrintAndLog("Can't open file %s ", filename
);
1997 for (i
= 0; i
< numBlocks
; i
++) {
1998 if (mfEmlGetMem(buf
, i
, 1)) {
1999 PrintAndLog("Cant get block: %d", i
);
2002 for (j
= 0; j
< 16; j
++)
2003 fprintf(f
, "%02X", buf
[j
]);
2008 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
2013 int CmdHF14AMfECFill(const char *Cmd
)
2015 uint8_t keyType
= 0;
2016 uint8_t numSectors
= 16;
2018 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2019 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
2020 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2022 PrintAndLog("samples: hf mf ecfill A");
2023 PrintAndLog(" hf mf ecfill A 4");
2024 PrintAndLog("Read card and transfer its data to emulator memory.");
2025 PrintAndLog("Keys must be laid in the emulator memory. \n");
2029 char ctmp
= param_getchar(Cmd
, 0);
2030 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
2031 PrintAndLog("Key type must be A or B");
2034 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
2036 ctmp
= param_getchar(Cmd
, 1);
2038 case '0' : numSectors
= 5; break;
2040 case '\0': numSectors
= 16; break;
2041 case '2' : numSectors
= 32; break;
2042 case '4' : numSectors
= 40; break;
2043 default: numSectors
= 16;
2046 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
2047 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
2052 int CmdHF14AMfEKeyPrn(const char *Cmd
)
2057 uint64_t keyA
, keyB
;
2059 char cmdp
= param_getchar(Cmd
, 0);
2061 if ( cmdp
== 'h' || cmdp
== 'H' ) {
2062 PrintAndLog("It prints the keys loaded in the emulator memory");
2063 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2064 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2066 PrintAndLog(" sample: hf mf ekeyprn 1");
2071 case '0' : numSectors
= 5; break;
2073 case '\0': numSectors
= 16; break;
2074 case '2' : numSectors
= 32; break;
2075 case '4' : numSectors
= 40; break;
2076 default: numSectors
= 16;
2079 PrintAndLog("|---|----------------|----------------|");
2080 PrintAndLog("|sec|key A |key B |");
2081 PrintAndLog("|---|----------------|----------------|");
2082 for (i
= 0; i
< numSectors
; i
++) {
2083 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
2084 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
2087 keyA
= bytes_to_num(data
, 6);
2088 keyB
= bytes_to_num(data
+ 10, 6);
2089 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
2091 PrintAndLog("|---|----------------|----------------|");
2096 // CHINESE MAGIC COMMANDS
2098 int CmdHF14AMfCSetUID(const char *Cmd
) {
2099 uint8_t wipeCard
= 0;
2100 uint8_t uid
[8] = {0x00};
2101 uint8_t oldUid
[8] = {0x00};
2102 uint8_t atqa
[2] = {0x00};
2103 uint8_t sak
[1] = {0x00};
2104 uint8_t atqaPresent
= 1;
2109 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
2110 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2111 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2113 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2115 PrintAndLog("sample: hf mf csetuid 01020304");
2116 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2120 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2121 PrintAndLog("UID must include 8 HEX symbols");
2126 ctmp
= param_getchar(Cmd
, argi
);
2127 if (ctmp
== 'w' || ctmp
== 'W') {
2133 if (param_getchar(Cmd
, argi
)) {
2134 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2135 PrintAndLog("ATQA must include 4 HEX symbols");
2139 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2140 PrintAndLog("SAK must include 2 HEX symbols");
2149 ctmp
= param_getchar(Cmd
, argi
);
2150 if (ctmp
== 'w' || ctmp
== 'W') {
2155 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2157 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2159 PrintAndLog("Can't set UID. error=%d", res
);
2163 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2164 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2168 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2169 uint8_t block
[16] = {0x00};
2170 uint8_t blockNo
= 0;
2171 uint8_t params
= MAGIC_SINGLE
;
2174 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2175 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2176 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2177 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2178 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2182 blockNo
= param_get8(Cmd
, 0);
2184 if (param_gethex(Cmd
, 1, block
, 32)) {
2185 PrintAndLog("block data must include 32 HEX symbols");
2189 char ctmp
= param_getchar(Cmd
, 2);
2190 if (ctmp
== 'w' || ctmp
== 'W')
2191 params
|= MAGIC_WIPE
;
2193 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2195 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2197 PrintAndLog("Can't write block. error=%d", res
);
2203 int CmdHF14AMfCLoad(const char *Cmd
) {
2205 char filename
[FILE_PATH_SIZE
];
2206 char * fnameptr
= filename
;
2207 char buf
[64] = {0x00};
2208 uint8_t buf8
[64] = {0x00};
2209 uint8_t fillFromEmulator
= 0;
2210 int i
, len
, blockNum
, flags
=0;
2212 memset(filename
, 0, sizeof(filename
));
2214 char ctmp
= param_getchar(Cmd
, 0);
2216 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2217 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2218 PrintAndLog("or from emulator memory (option `e`)");
2219 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2220 PrintAndLog(" or: hf mf cload e ");
2221 PrintAndLog(" sample: hf mf cload filename");
2225 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2227 if (fillFromEmulator
) {
2228 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2229 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2230 PrintAndLog("Cant get block: %d", blockNum
);
2233 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2234 if (blockNum
== 1) flags
= 0; // just write
2235 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2237 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2238 PrintAndLog("Cant set magic card block: %d", blockNum
);
2245 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2247 memcpy(filename
, Cmd
, len
);
2250 sprintf(fnameptr
, ".eml");
2253 f
= fopen(filename
, "r");
2255 PrintAndLog("File not found or locked.");
2262 memset(buf
, 0, sizeof(buf
));
2264 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2266 PrintAndLog("File reading error.");
2270 if (strlen(buf
) < 32) {
2271 if(strlen(buf
) && feof(f
))
2273 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2277 for (i
= 0; i
< 32; i
+= 2)
2278 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2280 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2281 if (blockNum
== 1) flags
= 0; // just write
2282 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2284 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2285 PrintAndLog("Can't set magic card block: %d", blockNum
);
2291 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2296 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2297 PrintAndLog("File content error. There must be 64 blocks");
2300 PrintAndLog("Loaded from file: %s", filename
);
2306 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2308 uint8_t blockNo
= 0;
2310 memset(data
, 0x00, sizeof(data
));
2311 char ctmp
= param_getchar(Cmd
, 0);
2313 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2314 PrintAndLog("Usage: hf mf cgetblk <block number>");
2315 PrintAndLog("sample: hf mf cgetblk 1");
2316 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2320 blockNo
= param_get8(Cmd
, 0);
2322 PrintAndLog("--block number:%2d ", blockNo
);
2324 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2326 PrintAndLog("Can't read block. error=%d", res
);
2330 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2334 int CmdHF14AMfCGetSc(const char *Cmd
) {
2336 uint8_t sectorNo
= 0;
2338 memset(data
, 0x00, sizeof(data
));
2339 char ctmp
= param_getchar(Cmd
, 0);
2341 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2342 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2343 PrintAndLog("sample: hf mf cgetsc 0");
2344 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2348 sectorNo
= param_get8(Cmd
, 0);
2349 if (sectorNo
> 15) {
2350 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2354 PrintAndLog("--sector number:%d ", sectorNo
);
2355 PrintAndLog("block | data");
2357 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2358 for (i
= 0; i
< 4; i
++) {
2359 if (i
== 1) flags
= 0;
2360 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2362 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2364 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2367 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2372 int CmdHF14AMfCSave(const char *Cmd
) {
2375 char filename
[FILE_PATH_SIZE
];
2376 char * fnameptr
= filename
;
2377 uint8_t fillFromEmulator
= 0;
2379 int i
, j
, len
, flags
;
2381 memset(filename
, 0, sizeof(filename
));
2382 memset(buf
, 0, sizeof(buf
));
2383 char ctmp
= param_getchar(Cmd
, 0);
2385 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2386 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2387 PrintAndLog("or into emulator memory (option `e`)");
2388 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2389 PrintAndLog(" sample: hf mf esave ");
2390 PrintAndLog(" hf mf esave filename");
2391 PrintAndLog(" hf mf esave e \n");
2394 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2396 if (fillFromEmulator
) {
2397 // put into emulator
2398 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2399 for (i
= 0; i
< 16 * 4; i
++) {
2400 if (i
== 1) flags
= 0;
2401 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2403 if (mfCGetBlock(i
, buf
, flags
)) {
2404 PrintAndLog("Cant get block: %d", i
);
2408 if (mfEmlSetMem(buf
, i
, 1)) {
2409 PrintAndLog("Cant set emul block: %d", i
);
2416 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2418 // get filename based on UID
2421 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2422 PrintAndLog("Cant get block: %d", 0);
2423 len
= sprintf(fnameptr
, "dump");
2426 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2427 sprintf(fnameptr
, "%02x", buf
[j
]);
2430 memcpy(filename
, Cmd
, len
);
2434 // add .eml extension
2435 sprintf(fnameptr
, ".eml");
2438 f
= fopen(filename
, "w+");
2441 PrintAndLog("File not found or locked.");
2446 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2447 for (i
= 0; i
< 16 * 4; i
++) {
2448 if (i
== 1) flags
= 0;
2449 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2451 if (mfCGetBlock(i
, buf
, flags
)) {
2452 PrintAndLog("Cant get block: %d", i
);
2455 for (j
= 0; j
< 16; j
++)
2456 fprintf(f
, "%02x", buf
[j
]);
2461 PrintAndLog("Saved to file: %s", filename
);
2466 //needs nt, ar, at, Data to decrypt
2467 int CmdHf14MfDecryptBytes(const char *Cmd
){
2469 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2470 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2471 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2474 param_gethex_ex(Cmd
, 3, data
, &len
);
2477 int limit
= sizeof(data
) / 2;
2482 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2485 static command_t CommandTable
[] = {
2486 {"help", CmdHelp
, 1, "This help"},
2487 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2488 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2489 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2490 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2491 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2492 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2493 {"chk", CmdHF14AMfChk
, 0, "Check keys"},
2494 {"mifare", CmdHF14AMifare
, 0, "Darkside attack. read parity error messages."},
2495 {"nested", CmdHF14AMfNested
, 0, "Nested attack. Test nested authentication"},
2496 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2497 {"keybrute", CmdHF14AMfKeyBrute
, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2498 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2499 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2500 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2501 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2502 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2503 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2504 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2505 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2506 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2507 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2508 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2509 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2510 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2511 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2512 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2513 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2514 {NULL
, NULL
, 0, NULL
}
2517 int CmdHFMF(const char *Cmd
) {
2518 clearCommandBuffer();
2519 CmdsParse(CommandTable
, Cmd
);
2523 int CmdHelp(const char *Cmd
) {
2524 CmdsHelp(CommandTable
);