]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
c25ba32bb4ed8653d79f4d1d0be7c650062d02b6
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
14 int usage_hf14_mifare(void){
15 PrintAndLog("Usage: hf mf mifare [h] <block number> <A|B>");
16 PrintAndLog("options:");
17 PrintAndLog(" h this help");
18 PrintAndLog(" <block number> (Optional) target other block");
19 PrintAndLog(" <A|B> (optional) target key type");
20 PrintAndLog("samples:");
21 PrintAndLog(" hf mf mifare");
22 PrintAndLog(" hf mf mifare 16");
23 PrintAndLog(" hf mf mifare 16 B");
26 int usage_hf14_mf1ksim(void){
27 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14,20 hex symbols)> n <numreads> i x");
28 PrintAndLog("options:");
29 PrintAndLog(" h this help");
30 PrintAndLog(" u (Optional) UID 4,7 or 10bytes. If not specified, the UID 4b from emulator memory will be used");
31 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
32 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
33 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
34 PrintAndLog("samples:");
35 PrintAndLog(" hf mf sim u 0a0a0a0a");
36 PrintAndLog(" hf mf sim u 11223344556677");
37 PrintAndLog(" hf mf sim u 112233445566778899AA");
40 int usage_hf14_dbg(void){
41 PrintAndLog("Usage: hf mf dbg [h] <debug level>");
42 PrintAndLog("options:");
43 PrintAndLog(" h this help");
44 PrintAndLog(" <debug level> (Optional) see list for valid levels");
45 PrintAndLog(" 0 - no debug messages");
46 PrintAndLog(" 1 - error messages");
47 PrintAndLog(" 2 - plus information messages");
48 PrintAndLog(" 3 - plus debug messages");
49 PrintAndLog(" 4 - print even debug messages in timing critical functions");
50 PrintAndLog(" Note: this option therefore may cause malfunction itself");
51 PrintAndLog("samples:");
52 PrintAndLog(" hf mf dbg 3");
55 int usage_hf14_sniff(void){
56 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
57 PrintAndLog("Usage: hf mf sniff [h] [l] [d] [f]");
58 PrintAndLog("options:");
59 PrintAndLog(" h this help");
60 PrintAndLog(" l save encrypted sequence to logfile `uid.log`");
61 PrintAndLog(" d decrypt sequence and put it to log file `uid.log`");
62 // PrintAndLog(" n/a e decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
63 PrintAndLog(" f decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
64 PrintAndLog("sample:");
65 PrintAndLog(" hf mf sniff l d f");
68 int usage_hf14_nested(void){
69 PrintAndLog("Usage:");
70 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
71 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
72 PrintAndLog(" <target block number> <target key A/B> [t]");
73 PrintAndLog("options:");
74 PrintAndLog(" h this help");
75 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
76 PrintAndLog(" t transfer keys into emulator memory");
77 PrintAndLog(" d write keys to binary file");
79 PrintAndLog("samples:");
80 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF ");
81 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF t ");
82 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF d ");
83 PrintAndLog(" hf mf nested o 0 A FFFFFFFFFFFF 4 A");
86 int usage_hf14_hardnested(void){
87 PrintAndLog("Usage:");
88 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
89 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
90 PrintAndLog(" or hf mf hardnested r [known target key]");
92 PrintAndLog("options:");
93 PrintAndLog(" h this help");
94 PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
95 PrintAndLog(" s slower acquisition (required by some non standard cards)");
96 PrintAndLog(" r read nonces.bin and start attack");
97 PrintAndLog(" t tests?");
99 PrintAndLog("samples:");
100 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
101 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
102 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
103 PrintAndLog(" hf mf hardnested r");
104 PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
106 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
107 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
110 int usage_hf14_chk(void){
111 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
112 PrintAndLog("options:");
113 PrintAndLog(" h this help");
114 PrintAndLog(" * all sectors based on card memory, other values then below defaults to 1k");
115 PrintAndLog(" 0 - MINI(320 bytes)");
116 PrintAndLog(" 1 - 1K");
117 PrintAndLog(" 2 - 2K");
118 PrintAndLog(" 4 - 4K");
119 PrintAndLog(" d write keys to binary file");
120 PrintAndLog(" t write keys to emulator memory\n");
122 PrintAndLog("samples:");
123 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic -- target block 0, Key A");
124 PrintAndLog(" hf mf chk *1 ? t -- target all blocks, all keys, 1K, write to emul");
125 PrintAndLog(" hf mf chk *1 ? d -- target all blocks, all keys, 1K, write to file");
128 int usage_hf14_keybrute(void){
129 PrintAndLog("J_Run's 2nd phase of multiple sector nested authentication key recovery");
130 PrintAndLog("You have a known 4 last bytes of a key recovered with mf_nonce_brute tool.");
131 PrintAndLog("First 2 bytes of key will be bruteforced");
133 PrintAndLog("Usage: hf mf keybrute [h] <block number> <A|B> <key>");
134 PrintAndLog("options:");
135 PrintAndLog(" h this help");
136 PrintAndLog(" <block number> target block number");
137 PrintAndLog(" <A|B> target key type");
138 PrintAndLog(" <key> candidate key from mf_nonce_brute tool");
139 PrintAndLog("samples:");
140 PrintAndLog(" hf mf keybrute 1 A 000011223344");
144 int CmdHF14AMifare(const char *Cmd
) {
146 uint32_t nt
= 0, nr
= 0;
147 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
150 uint8_t blockNo
= 0, keytype
= MIFARE_AUTH_KEYA
;
152 char cmdp
= param_getchar(Cmd
, 0);
153 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
155 blockNo
= param_get8(Cmd
, 0);
157 cmdp
= param_getchar(Cmd
, 1);
158 if (cmdp
== 'B' || cmdp
== 'b')
159 keytype
= MIFARE_AUTH_KEYB
;
161 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, keytype
}};
164 printf("-------------------------------------------------------------------------\n");
165 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
166 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
167 printf("-------------------------------------------------------------------------\n");
168 clock_t t1
= clock();
173 clearCommandBuffer();
189 printf("\naborted via keyboard!\n");
194 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
197 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
198 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
199 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
200 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
201 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
204 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
205 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
206 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
207 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
208 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
217 if (isOK
== -4 && par_list
== 0) {
218 // this special attack when parities is zero, uses checkkeys. Which now with block/keytype option also needs.
219 // but it uses 0|1 instead of 0x60|0x61...
220 if (nonce2key_ex(blockNo
, keytype
- 0x60 , uid
, nt
, nr
, ks_list
, &r_key
) ){
221 PrintAndLog("Key not found (lfsr_common_prefix list is null).");
222 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
226 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
232 if (isOK
!= 1) return 1;
234 // execute original function from util nonce2key
235 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
237 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
238 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
243 // nonce2key found a candidate key. Lets verify it.
244 uint8_t keyblock
[] = {0,0,0,0,0,0};
245 num_to_bytes(r_key
, 6, keyblock
);
247 int res
= mfCheckKeys(blockNo
, keytype
- 0x60 , false, 1, keyblock
, &key64
);
249 PrintAndLog("Candidate Key found (%012"llx
")", r_key
);
250 PrintAndLog("Failing is expected to happen. Trying again ...");
253 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
258 unsigned long elapsed_time
= difftime(end
, start
);
260 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
264 int CmdHF14AMfWrBl(const char *Cmd
) {
267 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
268 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
273 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
274 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
278 blockNo
= param_get8(Cmd
, 0);
279 cmdp
= param_getchar(Cmd
, 1);
281 PrintAndLog("Key type must be A or B");
284 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
285 if (param_gethex(Cmd
, 2, key
, 12)) {
286 PrintAndLog("Key must include 12 HEX symbols");
289 if (param_gethex(Cmd
, 3, bldata
, 32)) {
290 PrintAndLog("Block data must include 32 HEX symbols");
293 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
294 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
296 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
297 memcpy(c
.d
.asBytes
, key
, 6);
298 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
299 clearCommandBuffer();
303 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
304 uint8_t isOK
= resp
.arg
[0] & 0xff;
305 PrintAndLog("isOk:%02x", isOK
);
307 PrintAndLog("Command execute timeout");
313 int CmdHF14AMfRdBl(const char *Cmd
) {
316 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
322 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
323 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
327 blockNo
= param_get8(Cmd
, 0);
328 cmdp
= param_getchar(Cmd
, 1);
330 PrintAndLog("Key type must be A or B");
333 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
334 if (param_gethex(Cmd
, 2, key
, 12)) {
335 PrintAndLog("Key must include 12 HEX symbols");
338 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
340 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
341 memcpy(c
.d
.asBytes
, key
, 6);
342 clearCommandBuffer();
346 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
347 uint8_t isOK
= resp
.arg
[0] & 0xff;
348 uint8_t *data
= resp
.d
.asBytes
;
351 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
353 PrintAndLog("isOk:%02x", isOK
);
355 PrintAndLog("Command execute timeout");
361 int CmdHF14AMfRdSc(const char *Cmd
) {
363 uint8_t sectorNo
= 0;
365 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
367 uint8_t *data
= NULL
;
371 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
372 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
376 sectorNo
= param_get8(Cmd
, 0);
378 PrintAndLog("Sector number must be less than 40");
381 cmdp
= param_getchar(Cmd
, 1);
382 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
383 PrintAndLog("Key type must be A or B");
386 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
387 if (param_gethex(Cmd
, 2, key
, 12)) {
388 PrintAndLog("Key must include 12 HEX symbols");
391 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
393 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
394 memcpy(c
.d
.asBytes
, key
, 6);
395 clearCommandBuffer();
400 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
401 isOK
= resp
.arg
[0] & 0xff;
402 data
= resp
.d
.asBytes
;
404 PrintAndLog("isOk:%02x", isOK
);
406 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
407 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
409 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
412 PrintAndLog("Command execute timeout");
418 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
422 return 32 * 4 + (sectorNo
- 32) * 16;
426 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
434 int CmdHF14AMfDump(const char *Cmd
) {
435 uint8_t sectorNo
, blockNo
;
439 uint8_t rights
[40][4];
440 uint8_t carddata
[256][16];
441 uint8_t numSectors
= 16;
448 char cmdp
= param_getchar(Cmd
, 0);
450 case '0' : numSectors
= 5; break;
452 case '\0': numSectors
= 16; break;
453 case '2' : numSectors
= 32; break;
454 case '4' : numSectors
= 40; break;
455 default: numSectors
= 16;
458 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
459 PrintAndLog("Usage: hf mf dump [card memory]");
460 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
462 PrintAndLog("Samples: hf mf dump");
463 PrintAndLog(" hf mf dump 4");
467 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
468 PrintAndLog("Could not find file dumpkeys.bin");
472 // Read keys A from file
474 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
475 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
476 if ( bytes_read
== 0) {
477 PrintAndLog("File reading error.");
483 // Read keys B from file
484 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
485 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
486 if ( bytes_read
== 0) {
487 PrintAndLog("File reading error.");
495 PrintAndLog("|-----------------------------------------|");
496 PrintAndLog("|------ Reading sector access bits...-----|");
497 PrintAndLog("|-----------------------------------------|");
499 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
500 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
501 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
502 clearCommandBuffer();
505 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
506 uint8_t isOK
= resp
.arg
[0] & 0xff;
507 uint8_t *data
= resp
.d
.asBytes
;
509 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
510 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
511 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
512 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
514 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
515 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
516 rights
[sectorNo
][3] = 0x01;
519 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
520 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
521 rights
[sectorNo
][3] = 0x01;
525 PrintAndLog("|-----------------------------------------|");
526 PrintAndLog("|----- Dumping all blocks to file... -----|");
527 PrintAndLog("|-----------------------------------------|");
530 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
531 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
532 bool received
= false;
534 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
535 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
536 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
537 clearCommandBuffer();
539 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
540 } else { // data block. Check if it can be read with key A or key B
541 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
542 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
543 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
544 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
546 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
547 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
549 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
550 } else { // key A would work
551 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
552 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
553 clearCommandBuffer();
555 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
560 isOK
= resp
.arg
[0] & 0xff;
561 uint8_t *data
= resp
.d
.asBytes
;
562 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
563 data
[0] = (keyA
[sectorNo
][0]);
564 data
[1] = (keyA
[sectorNo
][1]);
565 data
[2] = (keyA
[sectorNo
][2]);
566 data
[3] = (keyA
[sectorNo
][3]);
567 data
[4] = (keyA
[sectorNo
][4]);
568 data
[5] = (keyA
[sectorNo
][5]);
569 data
[10] = (keyB
[sectorNo
][0]);
570 data
[11] = (keyB
[sectorNo
][1]);
571 data
[12] = (keyB
[sectorNo
][2]);
572 data
[13] = (keyB
[sectorNo
][3]);
573 data
[14] = (keyB
[sectorNo
][4]);
574 data
[15] = (keyB
[sectorNo
][5]);
577 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
578 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
580 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
586 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
593 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
594 PrintAndLog("Could not create file name dumpdata.bin");
597 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
598 fwrite(carddata
, 1, 16*numblocks
, fout
);
600 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
606 int CmdHF14AMfRestore(const char *Cmd
) {
607 uint8_t sectorNo
,blockNo
;
609 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
610 uint8_t bldata
[16] = {0x00};
618 char cmdp
= param_getchar(Cmd
, 0);
620 case '0' : numSectors
= 5; break;
622 case '\0': numSectors
= 16; break;
623 case '2' : numSectors
= 32; break;
624 case '4' : numSectors
= 40; break;
625 default: numSectors
= 16;
628 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
629 PrintAndLog("Usage: hf mf restore [card memory]");
630 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
632 PrintAndLog("Samples: hf mf restore");
633 PrintAndLog(" hf mf restore 4");
637 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
638 PrintAndLog("Could not find file dumpkeys.bin");
643 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
644 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
645 if ( bytes_read
== 0) {
646 PrintAndLog("File reading error (dumpkeys.bin).");
652 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
653 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
654 if ( bytes_read
== 0) {
655 PrintAndLog("File reading error (dumpkeys.bin).");
663 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
664 PrintAndLog("Could not find file dumpdata.bin");
667 PrintAndLog("Restoring dumpdata.bin to card");
669 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
670 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
671 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
672 memcpy(c
.d
.asBytes
, key
, 6);
673 bytes_read
= fread(bldata
, 1, 16, fdump
);
674 if ( bytes_read
== 0) {
675 PrintAndLog("File reading error (dumpdata.bin).");
680 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
681 bldata
[0] = (keyA
[sectorNo
][0]);
682 bldata
[1] = (keyA
[sectorNo
][1]);
683 bldata
[2] = (keyA
[sectorNo
][2]);
684 bldata
[3] = (keyA
[sectorNo
][3]);
685 bldata
[4] = (keyA
[sectorNo
][4]);
686 bldata
[5] = (keyA
[sectorNo
][5]);
687 bldata
[10] = (keyB
[sectorNo
][0]);
688 bldata
[11] = (keyB
[sectorNo
][1]);
689 bldata
[12] = (keyB
[sectorNo
][2]);
690 bldata
[13] = (keyB
[sectorNo
][3]);
691 bldata
[14] = (keyB
[sectorNo
][4]);
692 bldata
[15] = (keyB
[sectorNo
][5]);
695 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
697 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
698 clearCommandBuffer();
702 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
703 uint8_t isOK
= resp
.arg
[0] & 0xff;
704 PrintAndLog("isOk:%02x", isOK
);
706 PrintAndLog("Command execute timeout");
715 int CmdHF14AMfNested(const char *Cmd
) {
716 int i
, j
, res
, iterations
;
717 sector
*e_sector
= NULL
;
720 uint8_t trgBlockNo
= 0;
721 uint8_t trgKeyType
= 0;
722 uint8_t SectorsCnt
= 0;
723 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
724 uint8_t keyBlock
[6*6];
726 bool transferToEml
= false;
728 bool createDumpFile
= false;
730 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
731 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
733 if (strlen(Cmd
)<3) return usage_hf14_nested();
736 cmdp
= param_getchar(Cmd
, 0);
737 blockNo
= param_get8(Cmd
, 1);
738 ctmp
= param_getchar(Cmd
, 2);
740 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
741 PrintAndLog("Key type must be A or B");
745 if (ctmp
!= 'A' && ctmp
!= 'a')
748 if (param_gethex(Cmd
, 3, key
, 12)) {
749 PrintAndLog("Key must include 12 HEX symbols");
753 if (cmdp
== 'o' || cmdp
== 'O') {
755 trgBlockNo
= param_get8(Cmd
, 4);
756 ctmp
= param_getchar(Cmd
, 5);
757 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
758 PrintAndLog("Target key type must be A or B");
761 if (ctmp
!= 'A' && ctmp
!= 'a')
766 case '0': SectorsCnt
= 05; break;
767 case '1': SectorsCnt
= 16; break;
768 case '2': SectorsCnt
= 32; break;
769 case '4': SectorsCnt
= 40; break;
770 default: SectorsCnt
= 16;
774 ctmp
= param_getchar(Cmd
, 4);
775 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
776 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
778 ctmp
= param_getchar(Cmd
, 6);
779 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
780 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
783 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
785 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
786 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
787 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
788 case -4 : PrintAndLog("No valid key found"); break;
790 key64
= bytes_to_num(keyBlock
, 6);
792 // transfer key to the emulator
794 uint8_t sectortrailer
;
795 if (trgBlockNo
< 32*4) { // 4 block sector
796 sectortrailer
= (trgBlockNo
& 0x03) + 3;
797 } else { // 16 block sector
798 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
800 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
803 num_to_bytes(key64
, 6, keyBlock
);
805 num_to_bytes(key64
, 6, &keyBlock
[10]);
806 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
809 default : PrintAndLog("Unknown Error.\n");
813 else { // ------------------------------------ multiple sectors working
814 clock_t t1
= clock();
815 unsigned long elapsed_time
;
819 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
820 if (e_sector
== NULL
) return 1;
822 //test current key and additional standard keys first
823 memcpy(keyBlock
, key
, 6);
824 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
825 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
826 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
827 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
828 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
830 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
831 for (i
= 0; i
< SectorsCnt
; i
++) {
832 for (j
= 0; j
< 2; j
++) {
833 if (e_sector
[i
].foundKey
[j
]) continue;
835 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
838 e_sector
[i
].Key
[j
] = key64
;
839 e_sector
[i
].foundKey
[j
] = TRUE
;
843 clock_t t2
= clock() - t1
;
845 elapsed_time
= difftime(end
, start
);
847 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
849 PrintAndLog("enter nested...");
853 bool calibrate
= true;
855 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
856 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
857 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
859 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
861 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
863 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
864 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
865 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
866 case -4 : //key not found
873 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
874 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
877 default : PrintAndLog("Unknown Error.\n");
887 elapsed_time
= difftime(end
, start
);
889 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
892 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
893 PrintAndLog("trying to read key B...");
894 for (i
= 0; i
< SectorsCnt
; i
++) {
895 // KEY A but not KEY B
896 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
898 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
900 PrintAndLog("Reading block %d", sectrail
);
902 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
903 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
904 clearCommandBuffer();
908 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
910 uint8_t isOK
= resp
.arg
[0] & 0xff;
913 uint8_t *data
= resp
.d
.asBytes
;
914 key64
= bytes_to_num(data
+10, 6);
916 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
917 e_sector
[i
].foundKey
[1] = TRUE
;
918 e_sector
[i
].Key
[1] = key64
;
925 printKeyTable( SectorsCnt
, e_sector
);
927 // transfer them to the emulator
929 for (i
= 0; i
< SectorsCnt
; i
++) {
930 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
931 if (e_sector
[i
].foundKey
[0])
932 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
933 if (e_sector
[i
].foundKey
[1])
934 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
935 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
940 if (createDumpFile
) {
941 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
942 PrintAndLog("Could not create file dumpkeys.bin");
946 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
947 for(i
=0; i
<SectorsCnt
; i
++) {
948 if (e_sector
[i
].foundKey
[0]){
949 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
950 fwrite ( tempkey
, 1, 6, fkeys
);
953 fwrite ( &standart
, 1, 6, fkeys
);
956 for(i
=0; i
<SectorsCnt
; i
++) {
957 if (e_sector
[i
].foundKey
[1]){
958 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
959 fwrite ( tempkey
, 1, 6, fkeys
);
962 fwrite ( &standart
, 1, 6, fkeys
);
973 int CmdHF14AMfNestedHard(const char *Cmd
) {
976 uint8_t trgBlockNo
= 0;
977 uint8_t trgKeyType
= 0;
978 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
979 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
982 ctmp
= param_getchar(Cmd
, 0);
983 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
984 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
986 bool know_target_key
= false;
987 bool nonce_file_read
= false;
988 bool nonce_file_write
= false;
992 if (ctmp
== 'R' || ctmp
== 'r') {
993 nonce_file_read
= true;
994 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
995 know_target_key
= true;
997 } else if (ctmp
== 'T' || ctmp
== 't') {
998 tests
= param_get32ex(Cmd
, 1, 100, 10);
1000 blockNo
= param_get8(Cmd
, 0);
1001 ctmp
= param_getchar(Cmd
, 1);
1002 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1003 PrintAndLog("Key type must be A or B");
1006 if (ctmp
!= 'A' && ctmp
!= 'a') {
1010 if (param_gethex(Cmd
, 2, key
, 12)) {
1011 PrintAndLog("Key must include 12 HEX symbols");
1015 trgBlockNo
= param_get8(Cmd
, 3);
1016 ctmp
= param_getchar(Cmd
, 4);
1017 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1018 PrintAndLog("Target key type must be A or B");
1021 if (ctmp
!= 'A' && ctmp
!= 'a') {
1027 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
1028 know_target_key
= true;
1032 while ((ctmp
= param_getchar(Cmd
, i
))) {
1033 if (ctmp
== 's' || ctmp
== 'S') {
1035 } else if (ctmp
== 'w' || ctmp
== 'W') {
1036 nonce_file_write
= true;
1038 PrintAndLog("Possible options are w and/or s");
1045 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 ",
1048 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1049 know_target_key
? "" : " (not set)",
1050 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1051 slow
? "Yes" : "No",
1054 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1058 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1059 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1068 int CmdHF14AMfChk(const char *Cmd
) {
1070 if (strlen(Cmd
)<3) return usage_hf14_chk();
1073 char filename
[FILE_PATH_SIZE
]={0};
1075 uint8_t *keyBlock
= NULL
, *p
;
1076 uint8_t stKeyBlock
= 20;
1078 sector
*e_sector
= NULL
;
1083 uint8_t blockNo
= 0;
1084 uint8_t SectorsCnt
= 1;
1085 uint8_t keyType
= 0;
1088 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1090 int transferToEml
= 0;
1091 int createDumpFile
= 0;
1093 keyBlock
= calloc(stKeyBlock
, 6);
1094 if (keyBlock
== NULL
) return 1;
1096 uint64_t defaultKeys
[] = {
1097 0xffffffffffff, // Default key (first key used by program if no user defined key)
1098 0x000000000000, // Blank key
1099 0xa0a1a2a3a4a5, // NFCForum MAD key
1111 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1113 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1114 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1117 if (param_getchar(Cmd
, 0)=='*') {
1119 switch(param_getchar(Cmd
+1, 0)) {
1120 case '0': SectorsCnt
= 5; break;
1121 case '1': SectorsCnt
= 16; break;
1122 case '2': SectorsCnt
= 32; break;
1123 case '4': SectorsCnt
= 40; break;
1124 default: SectorsCnt
= 16;
1127 blockNo
= param_get8(Cmd
, 0);
1130 ctmp
= param_getchar(Cmd
, 1);
1142 PrintAndLog("Key type must be A , B or ?");
1147 ctmp
= param_getchar(Cmd
, 2);
1148 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1149 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1151 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1152 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1153 if ( stKeyBlock
- keycnt
< 2) {
1154 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1156 PrintAndLog("Cannot allocate memory for Keys");
1162 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1163 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1164 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1167 // May be a dic file
1168 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1169 PrintAndLog("File name too long");
1174 if ( (f
= fopen( filename
, "r")) ) {
1175 while( fgets(buf
, sizeof(buf
), f
) ){
1176 if (strlen(buf
) < 12 || buf
[11] == '\n')
1179 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1181 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1183 if (!isxdigit(buf
[0])){
1184 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1190 if ( stKeyBlock
- keycnt
< 2) {
1191 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1193 PrintAndLog("Cannot allocate memory for defKeys");
1200 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1201 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1202 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1204 memset(buf
, 0, sizeof(buf
));
1208 PrintAndLog("File: %s: not found or locked.", filename
);
1217 PrintAndLog("No key specified, trying default keys");
1218 for (;keycnt
< defaultKeysSize
; keycnt
++)
1219 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1220 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1221 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1224 // initialize storage for found keys
1225 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1226 if (e_sector
== NULL
) {
1232 for(int i
= 0; i
< SectorsCnt
; ++i
){
1233 e_sector
[i
].Key
[0] = 0xffffffffffff;
1234 e_sector
[i
].Key
[1] = 0xffffffffffff;
1235 e_sector
[i
].foundKey
[0] = FALSE
;
1236 e_sector
[i
].foundKey
[1] = FALSE
;
1240 uint8_t trgKeyType
= 0;
1241 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1244 clock_t t1
= clock();
1249 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1252 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1254 // skip already found keys.
1255 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1257 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1260 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1262 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1264 e_sector
[i
].Key
[trgKeyType
] = key64
;
1265 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1269 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1274 unsigned long elapsed_time
= difftime(end
, start
);
1276 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1279 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1280 if ( keyType
!= 1 ) {
1281 PrintAndLog("testing to read key B...");
1282 for (i
= 0; i
< SectorsCnt
; i
++) {
1283 // KEY A but not KEY B
1284 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1286 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1288 PrintAndLog("Reading block %d", sectrail
);
1290 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1291 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1292 clearCommandBuffer();
1296 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1298 uint8_t isOK
= resp
.arg
[0] & 0xff;
1299 if (!isOK
) continue;
1301 uint8_t *data
= resp
.d
.asBytes
;
1302 key64
= bytes_to_num(data
+10, 6);
1304 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1305 e_sector
[i
].foundKey
[1] = 1;
1306 e_sector
[i
].Key
[1] = key64
;
1314 printKeyTable( SectorsCnt
, e_sector
);
1316 if (transferToEml
) {
1317 uint8_t block
[16] = {0x00};
1318 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1319 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1320 if (e_sector
[i
].foundKey
[0])
1321 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1322 if (e_sector
[i
].foundKey
[1])
1323 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1324 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1326 PrintAndLog("Found keys have been transferred to the emulator memory");
1329 if (createDumpFile
) {
1330 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1331 if (fkeys
== NULL
) {
1332 PrintAndLog("Could not create file dumpkeys.bin");
1337 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1339 for( i
=0; i
<SectorsCnt
; i
++) {
1340 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1341 fwrite ( tempkey
, 1, 6, fkeys
);
1343 for(i
=0; i
<SectorsCnt
; i
++) {
1344 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1345 fwrite ( tempkey
, 1, 6, fkeys
);
1348 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1356 #define ATTACK_KEY_COUNT 8
1357 sector
*k_sector
= NULL
;
1358 uint8_t k_sectorsCount
= 16;
1359 void readerAttack(nonces_t data
[], bool setEmulatorMem
) {
1361 // initialize storage for found keys
1362 if (k_sector
== NULL
)
1363 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1364 if (k_sector
== NULL
)
1370 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1371 k_sector
[i
].Key
[0] = 0xffffffffffff;
1372 k_sector
[i
].Key
[1] = 0xffffffffffff;
1373 k_sector
[i
].foundKey
[0] = FALSE
;
1374 k_sector
[i
].foundKey
[1] = FALSE
;
1377 printf("enter reader attack\n");
1378 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1379 if (data
[i
].ar2
> 0) {
1381 if (tryMfk32(data
[i
], &key
)) {
1382 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1383 , (data
[i
].keytype
) ? "B" : "A"
1388 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1389 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1391 //set emulator memory for keys
1392 if (setEmulatorMem
) {
1393 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1394 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1395 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1396 mfEmlSetMem( memBlock
, i
*4 + 3, 1);
1397 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1399 , sprint_hex( memBlock
, sizeof(memBlock
))
1405 // if (tryMfk32_moebius(data[i+ATTACK_KEY_COUNT], &key)) {
1406 // PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"
1407 // ,(data[i+ATTACK_KEY_COUNT].keytype) ? "B" : "A"
1408 // , data[i+ATTACK_KEY_COUNT].sector
1416 int CmdHF14AMf1kSim(const char *Cmd
) {
1418 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1419 uint8_t exitAfterNReads
= 0;
1420 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1422 bool setEmulatorMem
= false;
1424 bool errors
= false;
1426 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1427 switch(param_getchar(Cmd
, cmdp
)) {
1430 setEmulatorMem
= true;
1435 return usage_hf14_mf1ksim();
1438 flags
|= FLAG_INTERACTIVE
;
1443 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1448 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1450 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1451 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1452 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1453 default: return usage_hf14_mf1ksim();
1459 flags
|= FLAG_NR_AR_ATTACK
;
1463 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1470 if(errors
) return usage_hf14_mf1ksim();
1472 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1473 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1478 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1479 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1480 clearCommandBuffer();
1483 if(flags
& FLAG_INTERACTIVE
) {
1484 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1486 nonces_t data
[ATTACK_KEY_COUNT
*2];
1490 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1492 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1493 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1495 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1496 readerAttack(data
, setEmulatorMem
);
1499 if (k_sector
!= NULL
) {
1500 printKeyTable(k_sectorsCount
, k_sector
);
1507 int CmdHF14AMfSniff(const char *Cmd
){
1508 bool wantLogToFile
= FALSE
;
1509 bool wantDecrypt
= FALSE
;
1510 //bool wantSaveToEml = FALSE; TODO
1511 bool wantSaveToEmlFile
= FALSE
;
1521 uint8_t uid_len
= 0;
1522 uint8_t atqa
[2] = {0x00, 0x00};
1525 uint8_t *buf
= NULL
;
1526 uint16_t bufsize
= 0;
1527 uint8_t *bufPtr
= NULL
;
1528 uint16_t traceLen
= 0;
1530 memset(uid
, 0x00, sizeof(uid
));
1532 char ctmp
= param_getchar(Cmd
, 0);
1533 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1535 for (int i
= 0; i
< 4; i
++) {
1536 ctmp
= param_getchar(Cmd
, i
);
1537 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1538 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1539 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1540 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1543 printf("-------------------------------------------------------------------------\n");
1544 printf("Executing mifare sniffing command. \n");
1545 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1546 printf("Press the key on pc keyboard to abort the client.\n");
1547 printf("-------------------------------------------------------------------------\n");
1549 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1550 clearCommandBuffer();
1558 tmpchar
= getchar();
1560 printf("\naborted via keyboard!\n");
1565 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1566 res
= resp
.arg
[0] & 0xff;
1567 traceLen
= resp
.arg
[1];
1576 if (res
== 1) { // there is (more) data to be transferred
1577 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1578 if (traceLen
> bufsize
) {
1580 if (buf
== NULL
) // not yet allocated
1581 p
= malloc(traceLen
);
1582 else // need more memory
1583 p
= realloc(buf
, traceLen
);
1586 PrintAndLog("Cannot allocate memory for trace");
1594 memset(buf
, 0x00, traceLen
);
1596 if (bufPtr
== NULL
) {
1597 PrintAndLog("Cannot allocate memory for trace");
1601 // what happens if LEN is bigger then TRACELEN --iceman
1602 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1607 if (res
== 2) { // received all data, start displaying
1608 blockLen
= bufPtr
- buf
;
1611 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1612 while (bufPtr
- buf
< blockLen
) {
1613 bufPtr
+= 6; // skip (void) timing information
1614 len
= *((uint16_t *)bufPtr
);
1622 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1623 memcpy(uid
, bufPtr
+ 2, 10);
1624 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1625 switch (atqa
[0] & 0xC0) {
1626 case 0x80: uid_len
= 10; break;
1627 case 0x40: uid_len
= 7; break;
1628 default: uid_len
= 4; break;
1631 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1632 sprint_hex(uid
, uid_len
),
1636 if (wantLogToFile
|| wantDecrypt
) {
1637 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1638 AddLogCurrentDT(logHexFileName
);
1641 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1643 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1645 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1647 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1651 bufPtr
+= ((len
-1)/8+1); // ignore parity
1662 int CmdHF14AMfDbg(const char *Cmd
) {
1664 char ctmp
= param_getchar(Cmd
, 0);
1665 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1667 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1668 if (dbgMode
> 4) return usage_hf14_dbg();
1670 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1675 int CmdHF14AMfKeyBrute(const char *Cmd
) {
1677 uint8_t blockNo
= 0, keytype
= 0;
1678 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
1679 uint64_t foundkey
= 0;
1681 char cmdp
= param_getchar(Cmd
, 0);
1682 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_keybrute();
1685 blockNo
= param_get8(Cmd
, 0);
1688 cmdp
= param_getchar(Cmd
, 1);
1689 if (cmdp
== 'B' || cmdp
== 'b') keytype
= 1;
1692 if (param_gethex(Cmd
, 2, key
, 12)) return usage_hf14_keybrute();
1694 clock_t t1
= clock();
1698 if (mfKeyBrute( blockNo
, keytype
, key
, &foundkey
))
1699 PrintAndLog("Found valid key: %012"llx
" \n", foundkey
);
1701 PrintAndLog("Key not found");
1705 unsigned long elapsed_time
= difftime(end
, start
);
1707 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1712 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1713 PrintAndLog("|---|----------------|---|----------------|---|");
1714 PrintAndLog("|sec|key A |res|key B |res|");
1715 PrintAndLog("|---|----------------|---|----------------|---|");
1716 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1717 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1718 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1719 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1722 PrintAndLog("|---|----------------|---|----------------|---|");
1725 // EMULATOR COMMANDS
1726 int CmdHF14AMfEGet(const char *Cmd
)
1728 uint8_t blockNo
= 0;
1729 uint8_t data
[16] = {0x00};
1731 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1732 PrintAndLog("Usage: hf mf eget <block number>");
1733 PrintAndLog(" sample: hf mf eget 0 ");
1737 blockNo
= param_get8(Cmd
, 0);
1740 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1741 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1743 PrintAndLog("Command execute timeout");
1749 int CmdHF14AMfEClear(const char *Cmd
)
1751 if (param_getchar(Cmd
, 0) == 'h') {
1752 PrintAndLog("Usage: hf mf eclr");
1753 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1757 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1762 int CmdHF14AMfESet(const char *Cmd
)
1764 uint8_t memBlock
[16];
1765 uint8_t blockNo
= 0;
1767 memset(memBlock
, 0x00, sizeof(memBlock
));
1769 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1770 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1771 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1775 blockNo
= param_get8(Cmd
, 0);
1777 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1778 PrintAndLog("block data must include 32 HEX symbols");
1783 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1784 memcpy(c
.d
.asBytes
, memBlock
, 16);
1789 int CmdHF14AMfELoad(const char *Cmd
)
1792 char filename
[FILE_PATH_SIZE
];
1793 char *fnameptr
= filename
;
1794 char buf
[64] = {0x00};
1795 uint8_t buf8
[64] = {0x00};
1796 int i
, len
, blockNum
, numBlocks
;
1797 int nameParamNo
= 1;
1798 uint8_t blockWidth
= 32;
1799 char ctmp
= param_getchar(Cmd
, 0);
1801 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1802 PrintAndLog("It loads emul dump from the file `filename.eml`");
1803 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1804 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1806 PrintAndLog(" sample: hf mf eload filename");
1807 PrintAndLog(" hf mf eload 4 filename");
1812 case '0' : numBlocks
= 5*4; break;
1814 case '\0': numBlocks
= 16*4; break;
1815 case '2' : numBlocks
= 32*4; break;
1816 case '4' : numBlocks
= 256; break;
1817 case 'U' : // fall through
1818 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1824 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1825 if (numblk2
> 0) numBlocks
= numblk2
;
1827 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1829 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1833 sprintf(fnameptr
, ".eml");
1836 f
= fopen(filename
, "r");
1838 PrintAndLog("File %s not found or locked", filename
);
1844 memset(buf
, 0, sizeof(buf
));
1846 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1848 if (blockNum
>= numBlocks
) break;
1850 PrintAndLog("File reading error.");
1855 if (strlen(buf
) < blockWidth
){
1856 if(strlen(buf
) && feof(f
))
1858 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1863 for (i
= 0; i
< blockWidth
; i
+= 2) {
1864 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1866 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1867 PrintAndLog("Cant set emul block: %3d", blockNum
);
1874 if (blockNum
>= numBlocks
) break;
1879 if ((blockNum
!= numBlocks
)) {
1880 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1883 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1887 int CmdHF14AMfESave(const char *Cmd
)
1890 char filename
[FILE_PATH_SIZE
];
1891 char * fnameptr
= filename
;
1893 int i
, j
, len
, numBlocks
;
1894 int nameParamNo
= 1;
1896 memset(filename
, 0, sizeof(filename
));
1897 memset(buf
, 0, sizeof(buf
));
1899 char ctmp
= param_getchar(Cmd
, 0);
1901 if ( ctmp
== 'h' || ctmp
== 'H') {
1902 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1903 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1904 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1906 PrintAndLog(" sample: hf mf esave ");
1907 PrintAndLog(" hf mf esave 4");
1908 PrintAndLog(" hf mf esave 4 filename");
1913 case '0' : numBlocks
= 5*4; break;
1915 case '\0': numBlocks
= 16*4; break;
1916 case '2' : numBlocks
= 32*4; break;
1917 case '4' : numBlocks
= 256; break;
1924 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1926 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1928 // user supplied filename?
1930 // get filename (UID from memory)
1931 if (mfEmlGetMem(buf
, 0, 1)) {
1932 PrintAndLog("Can\'t get UID from block: %d", 0);
1933 len
= sprintf(fnameptr
, "dump");
1937 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1938 sprintf(fnameptr
, "%02X", buf
[j
]);
1944 // add file extension
1945 sprintf(fnameptr
, ".eml");
1948 f
= fopen(filename
, "w+");
1951 PrintAndLog("Can't open file %s ", filename
);
1956 for (i
= 0; i
< numBlocks
; i
++) {
1957 if (mfEmlGetMem(buf
, i
, 1)) {
1958 PrintAndLog("Cant get block: %d", i
);
1961 for (j
= 0; j
< 16; j
++)
1962 fprintf(f
, "%02X", buf
[j
]);
1967 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1972 int CmdHF14AMfECFill(const char *Cmd
)
1974 uint8_t keyType
= 0;
1975 uint8_t numSectors
= 16;
1977 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1978 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1979 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1981 PrintAndLog("samples: hf mf ecfill A");
1982 PrintAndLog(" hf mf ecfill A 4");
1983 PrintAndLog("Read card and transfer its data to emulator memory.");
1984 PrintAndLog("Keys must be laid in the emulator memory. \n");
1988 char ctmp
= param_getchar(Cmd
, 0);
1989 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1990 PrintAndLog("Key type must be A or B");
1993 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1995 ctmp
= param_getchar(Cmd
, 1);
1997 case '0' : numSectors
= 5; break;
1999 case '\0': numSectors
= 16; break;
2000 case '2' : numSectors
= 32; break;
2001 case '4' : numSectors
= 40; break;
2002 default: numSectors
= 16;
2005 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
2006 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
2011 int CmdHF14AMfEKeyPrn(const char *Cmd
)
2016 uint64_t keyA
, keyB
;
2018 char cmdp
= param_getchar(Cmd
, 0);
2020 if ( cmdp
== 'h' || cmdp
== 'H' ) {
2021 PrintAndLog("It prints the keys loaded in the emulator memory");
2022 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2023 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2025 PrintAndLog(" sample: hf mf ekeyprn 1");
2030 case '0' : numSectors
= 5; break;
2032 case '\0': numSectors
= 16; break;
2033 case '2' : numSectors
= 32; break;
2034 case '4' : numSectors
= 40; break;
2035 default: numSectors
= 16;
2038 PrintAndLog("|---|----------------|----------------|");
2039 PrintAndLog("|sec|key A |key B |");
2040 PrintAndLog("|---|----------------|----------------|");
2041 for (i
= 0; i
< numSectors
; i
++) {
2042 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
2043 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
2046 keyA
= bytes_to_num(data
, 6);
2047 keyB
= bytes_to_num(data
+ 10, 6);
2048 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
2050 PrintAndLog("|---|----------------|----------------|");
2055 // CHINESE MAGIC COMMANDS
2057 int CmdHF14AMfCSetUID(const char *Cmd
) {
2058 uint8_t wipeCard
= 0;
2059 uint8_t uid
[8] = {0x00};
2060 uint8_t oldUid
[8] = {0x00};
2061 uint8_t atqa
[2] = {0x00};
2062 uint8_t sak
[1] = {0x00};
2063 uint8_t atqaPresent
= 1;
2068 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
2069 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2070 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2072 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2074 PrintAndLog("sample: hf mf csetuid 01020304");
2075 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2079 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2080 PrintAndLog("UID must include 8 HEX symbols");
2085 ctmp
= param_getchar(Cmd
, argi
);
2086 if (ctmp
== 'w' || ctmp
== 'W') {
2092 if (param_getchar(Cmd
, argi
)) {
2093 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2094 PrintAndLog("ATQA must include 4 HEX symbols");
2098 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2099 PrintAndLog("SAK must include 2 HEX symbols");
2108 ctmp
= param_getchar(Cmd
, argi
);
2109 if (ctmp
== 'w' || ctmp
== 'W') {
2114 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2116 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2118 PrintAndLog("Can't set UID. error=%d", res
);
2122 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2123 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2127 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2128 uint8_t block
[16] = {0x00};
2129 uint8_t blockNo
= 0;
2130 uint8_t params
= MAGIC_SINGLE
;
2133 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2134 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2135 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2136 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2137 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2141 blockNo
= param_get8(Cmd
, 0);
2143 if (param_gethex(Cmd
, 1, block
, 32)) {
2144 PrintAndLog("block data must include 32 HEX symbols");
2148 char ctmp
= param_getchar(Cmd
, 2);
2149 if (ctmp
== 'w' || ctmp
== 'W')
2150 params
|= MAGIC_WIPE
;
2152 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2154 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2156 PrintAndLog("Can't write block. error=%d", res
);
2162 int CmdHF14AMfCLoad(const char *Cmd
) {
2164 char filename
[FILE_PATH_SIZE
];
2165 char * fnameptr
= filename
;
2166 char buf
[64] = {0x00};
2167 uint8_t buf8
[64] = {0x00};
2168 uint8_t fillFromEmulator
= 0;
2169 int i
, len
, blockNum
, flags
=0;
2171 memset(filename
, 0, sizeof(filename
));
2173 char ctmp
= param_getchar(Cmd
, 0);
2175 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2176 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2177 PrintAndLog("or from emulator memory (option `e`)");
2178 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2179 PrintAndLog(" or: hf mf cload e ");
2180 PrintAndLog(" sample: hf mf cload filename");
2184 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2186 if (fillFromEmulator
) {
2187 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2188 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2189 PrintAndLog("Cant get block: %d", blockNum
);
2192 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2193 if (blockNum
== 1) flags
= 0; // just write
2194 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2196 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2197 PrintAndLog("Cant set magic card block: %d", blockNum
);
2204 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2206 memcpy(filename
, Cmd
, len
);
2209 sprintf(fnameptr
, ".eml");
2212 f
= fopen(filename
, "r");
2214 PrintAndLog("File not found or locked.");
2221 memset(buf
, 0, sizeof(buf
));
2223 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2225 PrintAndLog("File reading error.");
2229 if (strlen(buf
) < 32) {
2230 if(strlen(buf
) && feof(f
))
2232 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2236 for (i
= 0; i
< 32; i
+= 2)
2237 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2239 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2240 if (blockNum
== 1) flags
= 0; // just write
2241 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2243 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2244 PrintAndLog("Can't set magic card block: %d", blockNum
);
2250 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2255 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2256 PrintAndLog("File content error. There must be 64 blocks");
2259 PrintAndLog("Loaded from file: %s", filename
);
2265 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2267 uint8_t blockNo
= 0;
2269 memset(data
, 0x00, sizeof(data
));
2270 char ctmp
= param_getchar(Cmd
, 0);
2272 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2273 PrintAndLog("Usage: hf mf cgetblk <block number>");
2274 PrintAndLog("sample: hf mf cgetblk 1");
2275 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2279 blockNo
= param_get8(Cmd
, 0);
2281 PrintAndLog("--block number:%2d ", blockNo
);
2283 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2285 PrintAndLog("Can't read block. error=%d", res
);
2289 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2293 int CmdHF14AMfCGetSc(const char *Cmd
) {
2295 uint8_t sectorNo
= 0;
2297 memset(data
, 0x00, sizeof(data
));
2298 char ctmp
= param_getchar(Cmd
, 0);
2300 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2301 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2302 PrintAndLog("sample: hf mf cgetsc 0");
2303 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2307 sectorNo
= param_get8(Cmd
, 0);
2308 if (sectorNo
> 15) {
2309 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2313 PrintAndLog("--sector number:%d ", sectorNo
);
2314 PrintAndLog("block | data");
2316 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2317 for (i
= 0; i
< 4; i
++) {
2318 if (i
== 1) flags
= 0;
2319 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2321 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2323 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2326 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2331 int CmdHF14AMfCSave(const char *Cmd
) {
2334 char filename
[FILE_PATH_SIZE
];
2335 char * fnameptr
= filename
;
2336 uint8_t fillFromEmulator
= 0;
2338 int i
, j
, len
, flags
;
2340 memset(filename
, 0, sizeof(filename
));
2341 memset(buf
, 0, sizeof(buf
));
2342 char ctmp
= param_getchar(Cmd
, 0);
2344 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2345 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2346 PrintAndLog("or into emulator memory (option `e`)");
2347 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2348 PrintAndLog(" sample: hf mf esave ");
2349 PrintAndLog(" hf mf esave filename");
2350 PrintAndLog(" hf mf esave e \n");
2353 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2355 if (fillFromEmulator
) {
2356 // put into emulator
2357 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2358 for (i
= 0; i
< 16 * 4; i
++) {
2359 if (i
== 1) flags
= 0;
2360 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2362 if (mfCGetBlock(i
, buf
, flags
)) {
2363 PrintAndLog("Cant get block: %d", i
);
2367 if (mfEmlSetMem(buf
, i
, 1)) {
2368 PrintAndLog("Cant set emul block: %d", i
);
2375 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2377 // get filename based on UID
2380 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2381 PrintAndLog("Cant get block: %d", 0);
2382 len
= sprintf(fnameptr
, "dump");
2385 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2386 sprintf(fnameptr
, "%02x", buf
[j
]);
2389 memcpy(filename
, Cmd
, len
);
2393 // add .eml extension
2394 sprintf(fnameptr
, ".eml");
2397 f
= fopen(filename
, "w+");
2400 PrintAndLog("File not found or locked.");
2405 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2406 for (i
= 0; i
< 16 * 4; i
++) {
2407 if (i
== 1) flags
= 0;
2408 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2410 if (mfCGetBlock(i
, buf
, flags
)) {
2411 PrintAndLog("Cant get block: %d", i
);
2414 for (j
= 0; j
< 16; j
++)
2415 fprintf(f
, "%02x", buf
[j
]);
2420 PrintAndLog("Saved to file: %s", filename
);
2425 //needs nt, ar, at, Data to decrypt
2426 int CmdHf14MfDecryptBytes(const char *Cmd
){
2428 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2429 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2430 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2433 param_gethex_ex(Cmd
, 3, data
, &len
);
2436 int limit
= sizeof(data
) / 2;
2441 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2444 static command_t CommandTable
[] = {
2445 {"help", CmdHelp
, 1, "This help"},
2446 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2447 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2448 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2449 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2450 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2451 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2452 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2453 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2454 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2455 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2456 {"keybrute", CmdHF14AMfKeyBrute
, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2457 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2458 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2459 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2460 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2461 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2462 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2463 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2464 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2465 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2466 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2467 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2468 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2469 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2470 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2471 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2472 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2473 {NULL
, NULL
, 0, NULL
}
2476 int CmdHFMF(const char *Cmd
) {
2477 clearCommandBuffer();
2478 CmdsParse(CommandTable
, Cmd
);
2482 int CmdHelp(const char *Cmd
) {
2483 CmdsHelp(CommandTable
);