]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
14 int usage_hf14_mifare(void){
15 PrintAndLog("Usage: hf mf mifare [h] <block number> <A|B>");
16 PrintAndLog("options:");
17 PrintAndLog(" h this help");
18 PrintAndLog(" <block number> (Optional) target other block");
19 PrintAndLog(" <A|B> (optional) target key type");
20 PrintAndLog("samples:");
21 PrintAndLog(" hf mf mifare");
22 PrintAndLog(" hf mf mifare 16");
23 PrintAndLog(" hf mf mifare 16 B");
26 int usage_hf14_mf1ksim(void){
27 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14,20 hex symbols)> n <numreads> i x");
28 PrintAndLog("options:");
29 PrintAndLog(" h this help");
30 PrintAndLog(" u (Optional) UID 4,7 or 10bytes. If not specified, the UID 4b from emulator memory will be used");
31 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
32 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
33 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
34 PrintAndLog("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
") - Test authentication failed. Starting over darkside attack", r_key
);
252 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
257 unsigned long elapsed_time
= difftime(end
, start
);
259 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
263 int CmdHF14AMfWrBl(const char *Cmd
) {
266 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
267 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
272 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
273 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
277 blockNo
= param_get8(Cmd
, 0);
278 cmdp
= param_getchar(Cmd
, 1);
280 PrintAndLog("Key type must be A or B");
283 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
284 if (param_gethex(Cmd
, 2, key
, 12)) {
285 PrintAndLog("Key must include 12 HEX symbols");
288 if (param_gethex(Cmd
, 3, bldata
, 32)) {
289 PrintAndLog("Block data must include 32 HEX symbols");
292 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
293 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
295 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
296 memcpy(c
.d
.asBytes
, key
, 6);
297 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
298 clearCommandBuffer();
302 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
303 uint8_t isOK
= resp
.arg
[0] & 0xff;
304 PrintAndLog("isOk:%02x", isOK
);
306 PrintAndLog("Command execute timeout");
312 int CmdHF14AMfRdBl(const char *Cmd
) {
315 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
321 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
322 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
326 blockNo
= param_get8(Cmd
, 0);
327 cmdp
= param_getchar(Cmd
, 1);
329 PrintAndLog("Key type must be A or B");
332 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
333 if (param_gethex(Cmd
, 2, key
, 12)) {
334 PrintAndLog("Key must include 12 HEX symbols");
337 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
339 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
340 memcpy(c
.d
.asBytes
, key
, 6);
341 clearCommandBuffer();
345 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
346 uint8_t isOK
= resp
.arg
[0] & 0xff;
347 uint8_t *data
= resp
.d
.asBytes
;
350 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
352 PrintAndLog("isOk:%02x", isOK
);
354 PrintAndLog("Command execute timeout");
360 int CmdHF14AMfRdSc(const char *Cmd
) {
362 uint8_t sectorNo
= 0;
364 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
366 uint8_t *data
= NULL
;
370 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
371 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
375 sectorNo
= param_get8(Cmd
, 0);
377 PrintAndLog("Sector number must be less than 40");
380 cmdp
= param_getchar(Cmd
, 1);
381 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
382 PrintAndLog("Key type must be A or B");
385 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
386 if (param_gethex(Cmd
, 2, key
, 12)) {
387 PrintAndLog("Key must include 12 HEX symbols");
390 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
392 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
393 memcpy(c
.d
.asBytes
, key
, 6);
394 clearCommandBuffer();
399 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
400 isOK
= resp
.arg
[0] & 0xff;
401 data
= resp
.d
.asBytes
;
403 PrintAndLog("isOk:%02x", isOK
);
405 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
406 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
408 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
411 PrintAndLog("Command execute timeout");
417 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
421 return 32 * 4 + (sectorNo
- 32) * 16;
425 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
433 int CmdHF14AMfDump(const char *Cmd
) {
434 uint8_t sectorNo
, blockNo
;
438 uint8_t rights
[40][4];
439 uint8_t carddata
[256][16];
440 uint8_t numSectors
= 16;
447 char cmdp
= param_getchar(Cmd
, 0);
449 case '0' : numSectors
= 5; break;
451 case '\0': numSectors
= 16; break;
452 case '2' : numSectors
= 32; break;
453 case '4' : numSectors
= 40; break;
454 default: numSectors
= 16;
457 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
458 PrintAndLog("Usage: hf mf dump [card memory]");
459 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
461 PrintAndLog("Samples: hf mf dump");
462 PrintAndLog(" hf mf dump 4");
466 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
467 PrintAndLog("Could not find file dumpkeys.bin");
471 // Read keys A from file
473 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
474 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
475 if ( bytes_read
== 0) {
476 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.");
497 PrintAndLog("|-----------------------------------------|");
498 PrintAndLog("|------ Reading sector access bits...-----|");
499 PrintAndLog("|-----------------------------------------|");
501 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
502 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
503 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
504 clearCommandBuffer();
507 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
508 uint8_t isOK
= resp
.arg
[0] & 0xff;
509 uint8_t *data
= resp
.d
.asBytes
;
511 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
512 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
513 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
514 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
516 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
517 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
518 rights
[sectorNo
][3] = 0x01;
521 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
522 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
523 rights
[sectorNo
][3] = 0x01;
527 PrintAndLog("|-----------------------------------------|");
528 PrintAndLog("|----- Dumping all blocks to file... -----|");
529 PrintAndLog("|-----------------------------------------|");
532 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
533 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
534 bool received
= false;
536 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
537 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
538 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
539 clearCommandBuffer();
541 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
542 } else { // data block. Check if it can be read with key A or key B
543 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
544 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
545 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
546 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
548 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
549 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
551 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
552 } else { // key A would work
553 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
554 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
555 clearCommandBuffer();
557 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
562 isOK
= resp
.arg
[0] & 0xff;
563 uint8_t *data
= resp
.d
.asBytes
;
564 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
565 data
[0] = (keyA
[sectorNo
][0]);
566 data
[1] = (keyA
[sectorNo
][1]);
567 data
[2] = (keyA
[sectorNo
][2]);
568 data
[3] = (keyA
[sectorNo
][3]);
569 data
[4] = (keyA
[sectorNo
][4]);
570 data
[5] = (keyA
[sectorNo
][5]);
571 data
[10] = (keyB
[sectorNo
][0]);
572 data
[11] = (keyB
[sectorNo
][1]);
573 data
[12] = (keyB
[sectorNo
][2]);
574 data
[13] = (keyB
[sectorNo
][3]);
575 data
[14] = (keyB
[sectorNo
][4]);
576 data
[15] = (keyB
[sectorNo
][5]);
579 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
580 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
582 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
588 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
595 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
596 PrintAndLog("Could not create file name dumpdata.bin");
599 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
600 fwrite(carddata
, 1, 16*numblocks
, fout
);
603 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
609 int CmdHF14AMfRestore(const char *Cmd
) {
610 uint8_t sectorNo
,blockNo
;
612 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
613 uint8_t bldata
[16] = {0x00};
621 char cmdp
= param_getchar(Cmd
, 0);
623 case '0' : numSectors
= 5; break;
625 case '\0': numSectors
= 16; break;
626 case '2' : numSectors
= 32; break;
627 case '4' : numSectors
= 40; break;
628 default: numSectors
= 16;
631 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
632 PrintAndLog("Usage: hf mf restore [card memory]");
633 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
635 PrintAndLog("Samples: hf mf restore");
636 PrintAndLog(" hf mf restore 4");
640 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
641 PrintAndLog("Could not find file dumpkeys.bin");
646 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
647 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
648 if ( bytes_read
== 0) {
649 PrintAndLog("File reading error (dumpkeys.bin).");
656 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
657 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
658 if ( bytes_read
== 0) {
659 PrintAndLog("File reading error (dumpkeys.bin).");
668 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
669 PrintAndLog("Could not find file dumpdata.bin");
672 PrintAndLog("Restoring dumpdata.bin to card");
674 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
675 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
676 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
677 memcpy(c
.d
.asBytes
, key
, 6);
678 bytes_read
= fread(bldata
, 1, 16, fdump
);
679 if ( bytes_read
== 0) {
680 PrintAndLog("File reading error (dumpdata.bin).");
686 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
687 bldata
[0] = (keyA
[sectorNo
][0]);
688 bldata
[1] = (keyA
[sectorNo
][1]);
689 bldata
[2] = (keyA
[sectorNo
][2]);
690 bldata
[3] = (keyA
[sectorNo
][3]);
691 bldata
[4] = (keyA
[sectorNo
][4]);
692 bldata
[5] = (keyA
[sectorNo
][5]);
693 bldata
[10] = (keyB
[sectorNo
][0]);
694 bldata
[11] = (keyB
[sectorNo
][1]);
695 bldata
[12] = (keyB
[sectorNo
][2]);
696 bldata
[13] = (keyB
[sectorNo
][3]);
697 bldata
[14] = (keyB
[sectorNo
][4]);
698 bldata
[15] = (keyB
[sectorNo
][5]);
701 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
703 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
704 clearCommandBuffer();
708 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
709 uint8_t isOK
= resp
.arg
[0] & 0xff;
710 PrintAndLog("isOk:%02x", isOK
);
712 PrintAndLog("Command execute timeout");
722 int CmdHF14AMfNested(const char *Cmd
) {
723 int i
, j
, res
, iterations
;
724 sector
*e_sector
= NULL
;
727 uint8_t trgBlockNo
= 0;
728 uint8_t trgKeyType
= 0;
729 uint8_t SectorsCnt
= 0;
730 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
731 uint8_t keyBlock
[6*6];
733 bool transferToEml
= false;
735 bool createDumpFile
= false;
737 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
738 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
740 if (strlen(Cmd
)<3) return usage_hf14_nested();
743 cmdp
= param_getchar(Cmd
, 0);
744 blockNo
= param_get8(Cmd
, 1);
745 ctmp
= param_getchar(Cmd
, 2);
747 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
748 PrintAndLog("Key type must be A or B");
752 if (ctmp
!= 'A' && ctmp
!= 'a')
755 if (param_gethex(Cmd
, 3, key
, 12)) {
756 PrintAndLog("Key must include 12 HEX symbols");
760 if (cmdp
== 'o' || cmdp
== 'O') {
762 trgBlockNo
= param_get8(Cmd
, 4);
763 ctmp
= param_getchar(Cmd
, 5);
764 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
765 PrintAndLog("Target key type must be A or B");
768 if (ctmp
!= 'A' && ctmp
!= 'a')
773 case '0': SectorsCnt
= 05; break;
774 case '1': SectorsCnt
= 16; break;
775 case '2': SectorsCnt
= 32; break;
776 case '4': SectorsCnt
= 40; break;
777 default: SectorsCnt
= 16;
781 ctmp
= param_getchar(Cmd
, 4);
782 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
783 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
785 ctmp
= param_getchar(Cmd
, 6);
786 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
787 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
790 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
792 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
793 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
794 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
795 case -4 : PrintAndLog("No valid key found"); break;
797 key64
= bytes_to_num(keyBlock
, 6);
799 // transfer key to the emulator
801 uint8_t sectortrailer
;
802 if (trgBlockNo
< 32*4) { // 4 block sector
803 sectortrailer
= (trgBlockNo
& 0x03) + 3;
804 } else { // 16 block sector
805 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
807 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
810 num_to_bytes(key64
, 6, keyBlock
);
812 num_to_bytes(key64
, 6, &keyBlock
[10]);
813 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
816 default : PrintAndLog("Unknown Error.\n");
820 else { // ------------------------------------ multiple sectors working
821 clock_t t1
= clock();
822 unsigned long elapsed_time
;
826 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
827 if (e_sector
== NULL
) return 1;
829 //test current key and additional standard keys first
830 memcpy(keyBlock
, key
, 6);
831 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
832 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
833 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
834 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
835 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
837 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
838 for (i
= 0; i
< SectorsCnt
; i
++) {
839 for (j
= 0; j
< 2; j
++) {
840 if (e_sector
[i
].foundKey
[j
]) continue;
842 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
845 e_sector
[i
].Key
[j
] = key64
;
846 e_sector
[i
].foundKey
[j
] = TRUE
;
850 clock_t t2
= clock() - t1
;
852 elapsed_time
= difftime(end
, start
);
854 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
856 PrintAndLog("enter nested...");
860 bool calibrate
= true;
862 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
863 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
864 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
866 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
868 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
870 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
871 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
872 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
873 case -4 : //key not found
880 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
881 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
884 default : PrintAndLog("Unknown Error.\n");
894 elapsed_time
= difftime(end
, start
);
896 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
899 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
900 PrintAndLog("trying to read key B...");
901 for (i
= 0; i
< SectorsCnt
; i
++) {
902 // KEY A but not KEY B
903 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
905 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
907 PrintAndLog("Reading block %d", sectrail
);
909 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
910 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
911 clearCommandBuffer();
915 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
917 uint8_t isOK
= resp
.arg
[0] & 0xff;
920 uint8_t *data
= resp
.d
.asBytes
;
921 key64
= bytes_to_num(data
+10, 6);
923 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
924 e_sector
[i
].foundKey
[1] = TRUE
;
925 e_sector
[i
].Key
[1] = key64
;
932 printKeyTable( SectorsCnt
, e_sector
);
934 // transfer them to the emulator
936 for (i
= 0; i
< SectorsCnt
; i
++) {
937 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
938 if (e_sector
[i
].foundKey
[0])
939 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
940 if (e_sector
[i
].foundKey
[1])
941 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
942 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
947 if (createDumpFile
) {
948 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
949 PrintAndLog("Could not create file dumpkeys.bin");
953 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
954 for(i
=0; i
<SectorsCnt
; i
++) {
955 if (e_sector
[i
].foundKey
[0]){
956 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
957 fwrite ( tempkey
, 1, 6, fkeys
);
960 fwrite ( &standart
, 1, 6, fkeys
);
963 for(i
=0; i
<SectorsCnt
; i
++) {
964 if (e_sector
[i
].foundKey
[1]){
965 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
966 fwrite ( tempkey
, 1, 6, fkeys
);
969 fwrite ( &standart
, 1, 6, fkeys
);
980 int CmdHF14AMfNestedHard(const char *Cmd
) {
983 uint8_t trgBlockNo
= 0;
984 uint8_t trgKeyType
= 0;
985 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
986 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
989 ctmp
= param_getchar(Cmd
, 0);
990 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
991 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
993 bool know_target_key
= false;
994 bool nonce_file_read
= false;
995 bool nonce_file_write
= false;
999 if (ctmp
== 'R' || ctmp
== 'r') {
1000 nonce_file_read
= true;
1001 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
1002 know_target_key
= true;
1004 } else if (ctmp
== 'T' || ctmp
== 't') {
1005 tests
= param_get32ex(Cmd
, 1, 100, 10);
1007 blockNo
= param_get8(Cmd
, 0);
1008 ctmp
= param_getchar(Cmd
, 1);
1009 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1010 PrintAndLog("Key type must be A or B");
1013 if (ctmp
!= 'A' && ctmp
!= 'a') {
1017 if (param_gethex(Cmd
, 2, key
, 12)) {
1018 PrintAndLog("Key must include 12 HEX symbols");
1022 trgBlockNo
= param_get8(Cmd
, 3);
1023 ctmp
= param_getchar(Cmd
, 4);
1024 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1025 PrintAndLog("Target key type must be A or B");
1028 if (ctmp
!= 'A' && ctmp
!= 'a') {
1034 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
1035 know_target_key
= true;
1039 while ((ctmp
= param_getchar(Cmd
, i
))) {
1040 if (ctmp
== 's' || ctmp
== 'S') {
1042 } else if (ctmp
== 'w' || ctmp
== 'W') {
1043 nonce_file_write
= true;
1045 PrintAndLog("Possible options are w and/or s");
1052 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 ",
1055 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1056 know_target_key
? "" : " (not set)",
1057 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1058 slow
? "Yes" : "No",
1061 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1065 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1066 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1075 int CmdHF14AMfChk(const char *Cmd
) {
1077 if (strlen(Cmd
)<3) return usage_hf14_chk();
1080 char filename
[FILE_PATH_SIZE
]={0};
1082 uint8_t *keyBlock
= NULL
, *p
;
1083 uint8_t stKeyBlock
= 20;
1085 sector
*e_sector
= NULL
;
1090 uint8_t blockNo
= 0;
1091 uint8_t SectorsCnt
= 1;
1092 uint8_t keyType
= 0;
1095 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1097 int transferToEml
= 0;
1098 int createDumpFile
= 0;
1100 keyBlock
= calloc(stKeyBlock
, 6);
1101 if (keyBlock
== NULL
) return 1;
1103 uint64_t defaultKeys
[] = {
1104 0xffffffffffff, // Default key (first key used by program if no user defined key)
1105 0x000000000000, // Blank key
1106 0xa0a1a2a3a4a5, // NFCForum MAD key
1118 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1120 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1121 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1124 if (param_getchar(Cmd
, 0)=='*') {
1126 switch(param_getchar(Cmd
+1, 0)) {
1127 case '0': SectorsCnt
= 5; break;
1128 case '1': SectorsCnt
= 16; break;
1129 case '2': SectorsCnt
= 32; break;
1130 case '4': SectorsCnt
= 40; break;
1131 default: SectorsCnt
= 16;
1134 blockNo
= param_get8(Cmd
, 0);
1137 ctmp
= param_getchar(Cmd
, 1);
1149 PrintAndLog("Key type must be A , B or ?");
1154 ctmp
= param_getchar(Cmd
, 2);
1155 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1156 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1158 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1159 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1160 if ( stKeyBlock
- keycnt
< 2) {
1161 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1163 PrintAndLog("Cannot allocate memory for Keys");
1169 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1170 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1171 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1174 // May be a dic file
1175 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1176 PrintAndLog("File name too long");
1181 if ( (f
= fopen( filename
, "r")) ) {
1182 while( fgets(buf
, sizeof(buf
), f
) ){
1183 if (strlen(buf
) < 12 || buf
[11] == '\n')
1186 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1188 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1190 if (!isxdigit(buf
[0])){
1191 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1197 if ( stKeyBlock
- keycnt
< 2) {
1198 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1200 PrintAndLog("Cannot allocate memory for defKeys");
1207 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1208 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1209 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1211 memset(buf
, 0, sizeof(buf
));
1215 PrintAndLog("File: %s: not found or locked.", filename
);
1224 PrintAndLog("No key specified, trying default keys");
1225 for (;keycnt
< defaultKeysSize
; keycnt
++)
1226 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1227 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1228 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1231 // initialize storage for found keys
1232 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1233 if (e_sector
== NULL
) {
1239 for(int i
= 0; i
< SectorsCnt
; ++i
){
1240 e_sector
[i
].Key
[0] = 0xffffffffffff;
1241 e_sector
[i
].Key
[1] = 0xffffffffffff;
1242 e_sector
[i
].foundKey
[0] = FALSE
;
1243 e_sector
[i
].foundKey
[1] = FALSE
;
1247 uint8_t trgKeyType
= 0;
1248 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1251 clock_t t1
= clock();
1256 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1259 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1261 // skip already found keys.
1262 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1264 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1267 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1269 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1271 e_sector
[i
].Key
[trgKeyType
] = key64
;
1272 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1276 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1281 unsigned long elapsed_time
= difftime(end
, start
);
1283 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1286 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1287 if ( keyType
!= 1 ) {
1288 PrintAndLog("testing to read key B...");
1289 for (i
= 0; i
< SectorsCnt
; i
++) {
1290 // KEY A but not KEY B
1291 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1293 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1295 PrintAndLog("Reading block %d", sectrail
);
1297 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1298 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1299 clearCommandBuffer();
1303 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1305 uint8_t isOK
= resp
.arg
[0] & 0xff;
1306 if (!isOK
) continue;
1308 uint8_t *data
= resp
.d
.asBytes
;
1309 key64
= bytes_to_num(data
+10, 6);
1311 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1312 e_sector
[i
].foundKey
[1] = 1;
1313 e_sector
[i
].Key
[1] = key64
;
1321 printKeyTable( SectorsCnt
, e_sector
);
1323 if (transferToEml
) {
1324 uint8_t block
[16] = {0x00};
1325 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1326 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1327 if (e_sector
[i
].foundKey
[0])
1328 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1329 if (e_sector
[i
].foundKey
[1])
1330 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1331 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1333 PrintAndLog("Found keys have been transferred to the emulator memory");
1336 if (createDumpFile
) {
1337 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1338 if (fkeys
== NULL
) {
1339 PrintAndLog("Could not create file dumpkeys.bin");
1344 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1346 for( i
=0; i
<SectorsCnt
; i
++) {
1347 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1348 fwrite ( tempkey
, 1, 6, fkeys
);
1350 for(i
=0; i
<SectorsCnt
; i
++) {
1351 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1352 fwrite ( tempkey
, 1, 6, fkeys
);
1355 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1363 #define ATTACK_KEY_COUNT 8
1364 sector
*k_sector
= NULL
;
1365 uint8_t k_sectorsCount
= 16;
1366 void readerAttack(nonces_t data
[], bool setEmulatorMem
) {
1368 // initialize storage for found keys
1369 if (k_sector
== NULL
)
1370 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1371 if (k_sector
== NULL
)
1377 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1378 k_sector
[i
].Key
[0] = 0xffffffffffff;
1379 k_sector
[i
].Key
[1] = 0xffffffffffff;
1380 k_sector
[i
].foundKey
[0] = FALSE
;
1381 k_sector
[i
].foundKey
[1] = FALSE
;
1384 printf("enter reader attack\n");
1385 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1386 if (data
[i
].ar2
> 0) {
1388 if (tryMfk32(data
[i
], &key
)) {
1389 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1390 , (data
[i
].keytype
) ? "B" : "A"
1395 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1396 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1398 //set emulator memory for keys
1399 if (setEmulatorMem
) {
1400 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1401 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1402 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1403 mfEmlSetMem( memBlock
, i
*4 + 3, 1);
1404 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1406 , sprint_hex( memBlock
, sizeof(memBlock
))
1412 // if (tryMfk32_moebius(data[i+ATTACK_KEY_COUNT], &key)) {
1413 // PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"
1414 // ,(data[i+ATTACK_KEY_COUNT].keytype) ? "B" : "A"
1415 // , data[i+ATTACK_KEY_COUNT].sector
1423 int CmdHF14AMf1kSim(const char *Cmd
) {
1425 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1426 uint8_t exitAfterNReads
= 0;
1427 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1429 bool setEmulatorMem
= false;
1431 bool errors
= false;
1433 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1434 switch(param_getchar(Cmd
, cmdp
)) {
1437 setEmulatorMem
= true;
1442 return usage_hf14_mf1ksim();
1445 flags
|= FLAG_INTERACTIVE
;
1450 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1455 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1457 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1458 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1459 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1460 default: return usage_hf14_mf1ksim();
1466 flags
|= FLAG_NR_AR_ATTACK
;
1470 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1477 if(errors
) return usage_hf14_mf1ksim();
1479 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1480 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1485 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1486 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1487 clearCommandBuffer();
1490 if(flags
& FLAG_INTERACTIVE
) {
1491 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1493 nonces_t data
[ATTACK_KEY_COUNT
*2];
1497 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1499 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1500 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1502 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1503 readerAttack(data
, setEmulatorMem
);
1506 if (k_sector
!= NULL
) {
1507 printKeyTable(k_sectorsCount
, k_sector
);
1514 int CmdHF14AMfSniff(const char *Cmd
){
1515 bool wantLogToFile
= FALSE
;
1516 bool wantDecrypt
= FALSE
;
1517 //bool wantSaveToEml = FALSE; TODO
1518 bool wantSaveToEmlFile
= FALSE
;
1528 uint8_t uid_len
= 0;
1529 uint8_t atqa
[2] = {0x00, 0x00};
1532 uint8_t *buf
= NULL
;
1533 uint16_t bufsize
= 0;
1534 uint8_t *bufPtr
= NULL
;
1535 uint16_t traceLen
= 0;
1537 memset(uid
, 0x00, sizeof(uid
));
1539 char ctmp
= param_getchar(Cmd
, 0);
1540 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1542 for (int i
= 0; i
< 4; i
++) {
1543 ctmp
= param_getchar(Cmd
, i
);
1544 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1545 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1546 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1547 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1550 printf("-------------------------------------------------------------------------\n");
1551 printf("Executing mifare sniffing command. \n");
1552 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1553 printf("Press the key on pc keyboard to abort the client.\n");
1554 printf("-------------------------------------------------------------------------\n");
1556 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1557 clearCommandBuffer();
1565 tmpchar
= getchar();
1567 printf("\naborted via keyboard!\n");
1572 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1573 res
= resp
.arg
[0] & 0xff;
1574 traceLen
= resp
.arg
[1];
1583 if (res
== 1) { // there is (more) data to be transferred
1584 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1585 if (traceLen
> bufsize
) {
1587 if (buf
== NULL
) // not yet allocated
1588 p
= malloc(traceLen
);
1589 else // need more memory
1590 p
= realloc(buf
, traceLen
);
1593 PrintAndLog("Cannot allocate memory for trace");
1601 memset(buf
, 0x00, traceLen
);
1603 if (bufPtr
== NULL
) {
1604 PrintAndLog("Cannot allocate memory for trace");
1608 // what happens if LEN is bigger then TRACELEN --iceman
1609 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1614 if (res
== 2) { // received all data, start displaying
1615 blockLen
= bufPtr
- buf
;
1618 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1619 while (bufPtr
- buf
< blockLen
) {
1620 bufPtr
+= 6; // skip (void) timing information
1621 len
= *((uint16_t *)bufPtr
);
1629 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1630 memcpy(uid
, bufPtr
+ 2, 10);
1631 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1632 switch (atqa
[0] & 0xC0) {
1633 case 0x80: uid_len
= 10; break;
1634 case 0x40: uid_len
= 7; break;
1635 default: uid_len
= 4; break;
1638 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1639 sprint_hex(uid
, uid_len
),
1643 if (wantLogToFile
|| wantDecrypt
) {
1644 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1645 AddLogCurrentDT(logHexFileName
);
1648 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1650 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1652 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1654 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1658 bufPtr
+= ((len
-1)/8+1); // ignore parity
1669 int CmdHF14AMfDbg(const char *Cmd
) {
1671 char ctmp
= param_getchar(Cmd
, 0);
1672 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1674 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1675 if (dbgMode
> 4) return usage_hf14_dbg();
1677 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1682 int CmdHF14AMfKeyBrute(const char *Cmd
) {
1684 uint8_t blockNo
= 0, keytype
= 0;
1685 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
1686 uint64_t foundkey
= 0;
1688 char cmdp
= param_getchar(Cmd
, 0);
1689 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_keybrute();
1692 blockNo
= param_get8(Cmd
, 0);
1695 cmdp
= param_getchar(Cmd
, 1);
1696 if (cmdp
== 'B' || cmdp
== 'b') keytype
= 1;
1699 if (param_gethex(Cmd
, 2, key
, 12)) return usage_hf14_keybrute();
1701 clock_t t1
= clock();
1705 if (mfKeyBrute( blockNo
, keytype
, key
, &foundkey
))
1706 PrintAndLog("Found valid key: %012"llx
" \n", foundkey
);
1708 PrintAndLog("Key not found");
1712 unsigned long elapsed_time
= difftime(end
, start
);
1714 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1719 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1720 PrintAndLog("|---|----------------|---|----------------|---|");
1721 PrintAndLog("|sec|key A |res|key B |res|");
1722 PrintAndLog("|---|----------------|---|----------------|---|");
1723 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1724 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1725 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1726 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1729 PrintAndLog("|---|----------------|---|----------------|---|");
1732 // EMULATOR COMMANDS
1733 int CmdHF14AMfEGet(const char *Cmd
)
1735 uint8_t blockNo
= 0;
1736 uint8_t data
[16] = {0x00};
1738 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1739 PrintAndLog("Usage: hf mf eget <block number>");
1740 PrintAndLog(" sample: hf mf eget 0 ");
1744 blockNo
= param_get8(Cmd
, 0);
1747 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1748 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1750 PrintAndLog("Command execute timeout");
1756 int CmdHF14AMfEClear(const char *Cmd
)
1758 if (param_getchar(Cmd
, 0) == 'h') {
1759 PrintAndLog("Usage: hf mf eclr");
1760 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1764 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1769 int CmdHF14AMfESet(const char *Cmd
)
1771 uint8_t memBlock
[16];
1772 uint8_t blockNo
= 0;
1774 memset(memBlock
, 0x00, sizeof(memBlock
));
1776 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1777 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1778 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1782 blockNo
= param_get8(Cmd
, 0);
1784 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1785 PrintAndLog("block data must include 32 HEX symbols");
1790 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1791 memcpy(c
.d
.asBytes
, memBlock
, 16);
1796 int CmdHF14AMfELoad(const char *Cmd
)
1799 char filename
[FILE_PATH_SIZE
];
1800 char *fnameptr
= filename
;
1801 char buf
[64] = {0x00};
1802 uint8_t buf8
[64] = {0x00};
1803 int i
, len
, blockNum
, numBlocks
;
1804 int nameParamNo
= 1;
1805 uint8_t blockWidth
= 32;
1806 char ctmp
= param_getchar(Cmd
, 0);
1808 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1809 PrintAndLog("It loads emul dump from the file `filename.eml`");
1810 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1811 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1813 PrintAndLog(" sample: hf mf eload filename");
1814 PrintAndLog(" hf mf eload 4 filename");
1819 case '0' : numBlocks
= 5*4; break;
1821 case '\0': numBlocks
= 16*4; break;
1822 case '2' : numBlocks
= 32*4; break;
1823 case '4' : numBlocks
= 256; break;
1824 case 'U' : // fall through
1825 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1831 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1832 if (numblk2
> 0) numBlocks
= numblk2
;
1834 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1836 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1840 sprintf(fnameptr
, ".eml");
1843 f
= fopen(filename
, "r");
1845 PrintAndLog("File %s not found or locked", filename
);
1851 memset(buf
, 0, sizeof(buf
));
1853 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1855 if (blockNum
>= numBlocks
) break;
1857 PrintAndLog("File reading error.");
1862 if (strlen(buf
) < blockWidth
){
1863 if(strlen(buf
) && feof(f
))
1865 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1870 for (i
= 0; i
< blockWidth
; i
+= 2) {
1871 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1873 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1874 PrintAndLog("Cant set emul block: %3d", blockNum
);
1881 if (blockNum
>= numBlocks
) break;
1886 if ((blockNum
!= numBlocks
)) {
1887 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1890 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1894 int CmdHF14AMfESave(const char *Cmd
)
1897 char filename
[FILE_PATH_SIZE
];
1898 char * fnameptr
= filename
;
1900 int i
, j
, len
, numBlocks
;
1901 int nameParamNo
= 1;
1903 memset(filename
, 0, sizeof(filename
));
1904 memset(buf
, 0, sizeof(buf
));
1906 char ctmp
= param_getchar(Cmd
, 0);
1908 if ( ctmp
== 'h' || ctmp
== 'H') {
1909 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1910 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1911 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1913 PrintAndLog(" sample: hf mf esave ");
1914 PrintAndLog(" hf mf esave 4");
1915 PrintAndLog(" hf mf esave 4 filename");
1920 case '0' : numBlocks
= 5*4; break;
1922 case '\0': numBlocks
= 16*4; break;
1923 case '2' : numBlocks
= 32*4; break;
1924 case '4' : numBlocks
= 256; break;
1931 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1933 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1935 // user supplied filename?
1937 // get filename (UID from memory)
1938 if (mfEmlGetMem(buf
, 0, 1)) {
1939 PrintAndLog("Can\'t get UID from block: %d", 0);
1940 len
= sprintf(fnameptr
, "dump");
1944 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1945 sprintf(fnameptr
, "%02X", buf
[j
]);
1951 // add file extension
1952 sprintf(fnameptr
, ".eml");
1955 f
= fopen(filename
, "w+");
1958 PrintAndLog("Can't open file %s ", filename
);
1963 for (i
= 0; i
< numBlocks
; i
++) {
1964 if (mfEmlGetMem(buf
, i
, 1)) {
1965 PrintAndLog("Cant get block: %d", i
);
1968 for (j
= 0; j
< 16; j
++)
1969 fprintf(f
, "%02X", buf
[j
]);
1974 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1979 int CmdHF14AMfECFill(const char *Cmd
)
1981 uint8_t keyType
= 0;
1982 uint8_t numSectors
= 16;
1984 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1985 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1986 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1988 PrintAndLog("samples: hf mf ecfill A");
1989 PrintAndLog(" hf mf ecfill A 4");
1990 PrintAndLog("Read card and transfer its data to emulator memory.");
1991 PrintAndLog("Keys must be laid in the emulator memory. \n");
1995 char ctmp
= param_getchar(Cmd
, 0);
1996 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1997 PrintAndLog("Key type must be A or B");
2000 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
2002 ctmp
= param_getchar(Cmd
, 1);
2004 case '0' : numSectors
= 5; break;
2006 case '\0': numSectors
= 16; break;
2007 case '2' : numSectors
= 32; break;
2008 case '4' : numSectors
= 40; break;
2009 default: numSectors
= 16;
2012 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
2013 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
2018 int CmdHF14AMfEKeyPrn(const char *Cmd
)
2023 uint64_t keyA
, keyB
;
2025 char cmdp
= param_getchar(Cmd
, 0);
2027 if ( cmdp
== 'h' || cmdp
== 'H' ) {
2028 PrintAndLog("It prints the keys loaded in the emulator memory");
2029 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2030 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2032 PrintAndLog(" sample: hf mf ekeyprn 1");
2037 case '0' : numSectors
= 5; break;
2039 case '\0': numSectors
= 16; break;
2040 case '2' : numSectors
= 32; break;
2041 case '4' : numSectors
= 40; break;
2042 default: numSectors
= 16;
2045 PrintAndLog("|---|----------------|----------------|");
2046 PrintAndLog("|sec|key A |key B |");
2047 PrintAndLog("|---|----------------|----------------|");
2048 for (i
= 0; i
< numSectors
; i
++) {
2049 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
2050 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
2053 keyA
= bytes_to_num(data
, 6);
2054 keyB
= bytes_to_num(data
+ 10, 6);
2055 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
2057 PrintAndLog("|---|----------------|----------------|");
2062 // CHINESE MAGIC COMMANDS
2064 int CmdHF14AMfCSetUID(const char *Cmd
) {
2065 uint8_t wipeCard
= 0;
2066 uint8_t uid
[8] = {0x00};
2067 uint8_t oldUid
[8] = {0x00};
2068 uint8_t atqa
[2] = {0x00};
2069 uint8_t sak
[1] = {0x00};
2070 uint8_t atqaPresent
= 1;
2075 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
2076 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2077 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2079 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2081 PrintAndLog("sample: hf mf csetuid 01020304");
2082 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2086 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2087 PrintAndLog("UID must include 8 HEX symbols");
2092 ctmp
= param_getchar(Cmd
, argi
);
2093 if (ctmp
== 'w' || ctmp
== 'W') {
2099 if (param_getchar(Cmd
, argi
)) {
2100 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2101 PrintAndLog("ATQA must include 4 HEX symbols");
2105 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2106 PrintAndLog("SAK must include 2 HEX symbols");
2115 ctmp
= param_getchar(Cmd
, argi
);
2116 if (ctmp
== 'w' || ctmp
== 'W') {
2121 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2123 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2125 PrintAndLog("Can't set UID. error=%d", res
);
2129 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2130 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2134 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2135 uint8_t block
[16] = {0x00};
2136 uint8_t blockNo
= 0;
2137 uint8_t params
= MAGIC_SINGLE
;
2140 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2141 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2142 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2143 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2144 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2148 blockNo
= param_get8(Cmd
, 0);
2150 if (param_gethex(Cmd
, 1, block
, 32)) {
2151 PrintAndLog("block data must include 32 HEX symbols");
2155 char ctmp
= param_getchar(Cmd
, 2);
2156 if (ctmp
== 'w' || ctmp
== 'W')
2157 params
|= MAGIC_WIPE
;
2159 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2161 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2163 PrintAndLog("Can't write block. error=%d", res
);
2169 int CmdHF14AMfCLoad(const char *Cmd
) {
2171 char filename
[FILE_PATH_SIZE
];
2172 char * fnameptr
= filename
;
2173 char buf
[64] = {0x00};
2174 uint8_t buf8
[64] = {0x00};
2175 uint8_t fillFromEmulator
= 0;
2176 int i
, len
, blockNum
, flags
=0;
2178 memset(filename
, 0, sizeof(filename
));
2180 char ctmp
= param_getchar(Cmd
, 0);
2182 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2183 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2184 PrintAndLog("or from emulator memory (option `e`)");
2185 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2186 PrintAndLog(" or: hf mf cload e ");
2187 PrintAndLog(" sample: hf mf cload filename");
2191 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2193 if (fillFromEmulator
) {
2194 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2195 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2196 PrintAndLog("Cant get block: %d", blockNum
);
2199 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2200 if (blockNum
== 1) flags
= 0; // just write
2201 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2203 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2204 PrintAndLog("Cant set magic card block: %d", blockNum
);
2211 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2213 memcpy(filename
, Cmd
, len
);
2216 sprintf(fnameptr
, ".eml");
2219 f
= fopen(filename
, "r");
2221 PrintAndLog("File not found or locked.");
2228 memset(buf
, 0, sizeof(buf
));
2230 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2232 PrintAndLog("File reading error.");
2236 if (strlen(buf
) < 32) {
2237 if(strlen(buf
) && feof(f
))
2239 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2243 for (i
= 0; i
< 32; i
+= 2)
2244 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2246 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2247 if (blockNum
== 1) flags
= 0; // just write
2248 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2250 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2251 PrintAndLog("Can't set magic card block: %d", blockNum
);
2257 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2262 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2263 PrintAndLog("File content error. There must be 64 blocks");
2266 PrintAndLog("Loaded from file: %s", filename
);
2272 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2274 uint8_t blockNo
= 0;
2276 memset(data
, 0x00, sizeof(data
));
2277 char ctmp
= param_getchar(Cmd
, 0);
2279 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2280 PrintAndLog("Usage: hf mf cgetblk <block number>");
2281 PrintAndLog("sample: hf mf cgetblk 1");
2282 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2286 blockNo
= param_get8(Cmd
, 0);
2288 PrintAndLog("--block number:%2d ", blockNo
);
2290 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2292 PrintAndLog("Can't read block. error=%d", res
);
2296 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2300 int CmdHF14AMfCGetSc(const char *Cmd
) {
2302 uint8_t sectorNo
= 0;
2304 memset(data
, 0x00, sizeof(data
));
2305 char ctmp
= param_getchar(Cmd
, 0);
2307 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2308 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2309 PrintAndLog("sample: hf mf cgetsc 0");
2310 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2314 sectorNo
= param_get8(Cmd
, 0);
2315 if (sectorNo
> 15) {
2316 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2320 PrintAndLog("--sector number:%d ", sectorNo
);
2321 PrintAndLog("block | data");
2323 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2324 for (i
= 0; i
< 4; i
++) {
2325 if (i
== 1) flags
= 0;
2326 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2328 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2330 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2333 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2338 int CmdHF14AMfCSave(const char *Cmd
) {
2341 char filename
[FILE_PATH_SIZE
];
2342 char * fnameptr
= filename
;
2343 uint8_t fillFromEmulator
= 0;
2345 int i
, j
, len
, flags
;
2347 memset(filename
, 0, sizeof(filename
));
2348 memset(buf
, 0, sizeof(buf
));
2349 char ctmp
= param_getchar(Cmd
, 0);
2351 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2352 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2353 PrintAndLog("or into emulator memory (option `e`)");
2354 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2355 PrintAndLog(" sample: hf mf esave ");
2356 PrintAndLog(" hf mf esave filename");
2357 PrintAndLog(" hf mf esave e \n");
2360 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2362 if (fillFromEmulator
) {
2363 // put into emulator
2364 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2365 for (i
= 0; i
< 16 * 4; i
++) {
2366 if (i
== 1) flags
= 0;
2367 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2369 if (mfCGetBlock(i
, buf
, flags
)) {
2370 PrintAndLog("Cant get block: %d", i
);
2374 if (mfEmlSetMem(buf
, i
, 1)) {
2375 PrintAndLog("Cant set emul block: %d", i
);
2382 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2384 // get filename based on UID
2387 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2388 PrintAndLog("Cant get block: %d", 0);
2389 len
= sprintf(fnameptr
, "dump");
2392 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2393 sprintf(fnameptr
, "%02x", buf
[j
]);
2396 memcpy(filename
, Cmd
, len
);
2400 // add .eml extension
2401 sprintf(fnameptr
, ".eml");
2404 f
= fopen(filename
, "w+");
2407 PrintAndLog("File not found or locked.");
2412 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2413 for (i
= 0; i
< 16 * 4; i
++) {
2414 if (i
== 1) flags
= 0;
2415 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2417 if (mfCGetBlock(i
, buf
, flags
)) {
2418 PrintAndLog("Cant get block: %d", i
);
2421 for (j
= 0; j
< 16; j
++)
2422 fprintf(f
, "%02x", buf
[j
]);
2427 PrintAndLog("Saved to file: %s", filename
);
2432 //needs nt, ar, at, Data to decrypt
2433 int CmdHf14MfDecryptBytes(const char *Cmd
){
2435 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2436 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2437 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2440 param_gethex_ex(Cmd
, 3, data
, &len
);
2443 int limit
= sizeof(data
) / 2;
2448 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2451 static command_t CommandTable
[] = {
2452 {"help", CmdHelp
, 1, "This help"},
2453 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2454 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2455 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2456 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2457 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2458 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2459 {"chk", CmdHF14AMfChk
, 0, "Check keys"},
2460 {"mifare", CmdHF14AMifare
, 0, "Darkside attack. read parity error messages."},
2461 {"nested", CmdHF14AMfNested
, 0, "Nested attack. Test nested authentication"},
2462 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2463 {"keybrute", CmdHF14AMfKeyBrute
, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2464 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2465 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2466 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2467 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2468 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2469 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2470 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2471 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2472 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2473 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2474 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2475 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2476 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2477 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2478 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2479 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2480 {NULL
, NULL
, 0, NULL
}
2483 int CmdHFMF(const char *Cmd
) {
2484 clearCommandBuffer();
2485 CmdsParse(CommandTable
, Cmd
);
2489 int CmdHelp(const char *Cmd
) {
2490 CmdsHelp(CommandTable
);