]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
14 int usage_hf14_mifare(void){
15 PrintAndLog("Usage: hf mf mifare [h] <block number> <A|B>");
16 PrintAndLog("options:");
17 PrintAndLog(" h this help");
18 PrintAndLog(" <block number> (Optional) target other block");
19 PrintAndLog(" <A|B> (optional) target key type");
20 PrintAndLog("samples:");
21 PrintAndLog(" hf mf mifare");
22 PrintAndLog(" hf mf mifare 16");
23 PrintAndLog(" hf mf mifare 16 B");
26 int usage_hf14_mf1ksim(void){
27 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14,20 hex symbols)> n <numreads> i x");
28 PrintAndLog("options:");
29 PrintAndLog(" h this help");
30 PrintAndLog(" u (Optional) UID 4,7 or 10bytes. If not specified, the UID 4b from emulator memory will be used");
31 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
32 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
33 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
34 PrintAndLog("samples:");
35 PrintAndLog(" hf mf sim u 0a0a0a0a");
36 PrintAndLog(" hf mf sim u 11223344556677");
37 PrintAndLog(" hf mf sim u 112233445566778899AA");
40 int usage_hf14_dbg(void){
41 PrintAndLog("Usage: hf mf dbg [h] <debug level>");
42 PrintAndLog("options:");
43 PrintAndLog(" h this help");
44 PrintAndLog(" <debug level> (Optional) see list for valid levels");
45 PrintAndLog(" 0 - no debug messages");
46 PrintAndLog(" 1 - error messages");
47 PrintAndLog(" 2 - plus information messages");
48 PrintAndLog(" 3 - plus debug messages");
49 PrintAndLog(" 4 - print even debug messages in timing critical functions");
50 PrintAndLog(" Note: this option therefore may cause malfunction itself");
51 PrintAndLog("samples:");
52 PrintAndLog(" hf mf dbg 3");
55 int usage_hf14_sniff(void){
56 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
57 PrintAndLog("Usage: hf mf sniff [h] [l] [d] [f]");
58 PrintAndLog("options:");
59 PrintAndLog(" h this help");
60 PrintAndLog(" l save encrypted sequence to logfile `uid.log`");
61 PrintAndLog(" d decrypt sequence and put it to log file `uid.log`");
62 // PrintAndLog(" n/a e decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
63 PrintAndLog(" f decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
64 PrintAndLog("sample:");
65 PrintAndLog(" hf mf sniff l d f");
68 int usage_hf14_nested(void){
69 PrintAndLog("Usage:");
70 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
71 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
72 PrintAndLog(" <target block number> <target key A/B> [t]");
73 PrintAndLog("options:");
74 PrintAndLog(" h this help");
75 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
76 PrintAndLog(" t transfer keys into emulator memory");
77 PrintAndLog(" d write keys to binary file");
79 PrintAndLog("samples:");
80 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF ");
81 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF t ");
82 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF d ");
83 PrintAndLog(" hf mf nested o 0 A FFFFFFFFFFFF 4 A");
86 int usage_hf14_hardnested(void){
87 PrintAndLog("Usage:");
88 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
89 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
90 PrintAndLog(" or hf mf hardnested r [known target key]");
92 PrintAndLog("options:");
93 PrintAndLog(" h this help");
94 PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
95 PrintAndLog(" s slower acquisition (required by some non standard cards)");
96 PrintAndLog(" r read nonces.bin and start attack");
97 PrintAndLog(" t tests?");
99 PrintAndLog("samples:");
100 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
101 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
102 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
103 PrintAndLog(" hf mf hardnested r");
104 PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
106 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
107 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
110 int usage_hf14_chk(void){
111 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
112 PrintAndLog("options:");
113 PrintAndLog(" h this help");
114 PrintAndLog(" * all sectors based on card memory, other values then below defaults to 1k");
115 PrintAndLog(" 0 - MINI(320 bytes)");
116 PrintAndLog(" 1 - 1K");
117 PrintAndLog(" 2 - 2K");
118 PrintAndLog(" 4 - 4K");
119 PrintAndLog(" d write keys to binary file");
120 PrintAndLog(" t write keys to emulator memory\n");
122 PrintAndLog("samples:");
123 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic -- target block 0, Key A");
124 PrintAndLog(" hf mf chk *1 ? t -- target all blocks, all keys, 1K, write to emul");
125 PrintAndLog(" hf mf chk *1 ? d -- target all blocks, all keys, 1K, write to file");
129 int CmdHF14AMifare(const char *Cmd
) {
131 uint32_t nt
= 0, nr
= 0;
132 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
135 uint8_t blockNo
= 0, keytype
= MIFARE_AUTH_KEYA
;
137 char cmdp
= param_getchar(Cmd
, 0);
138 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
140 blockNo
= param_get8(Cmd
, 0);
142 cmdp
= param_getchar(Cmd
, 1);
143 if (cmdp
== 'B' || cmdp
== 'b')
144 keytype
= MIFARE_AUTH_KEYB
;
146 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, keytype
}};
149 printf("-------------------------------------------------------------------------\n");
150 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
151 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
152 printf("-------------------------------------------------------------------------\n");
153 clock_t t1
= clock();
158 clearCommandBuffer();
174 printf("\naborted via keyboard!\n");
179 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
182 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
183 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
184 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
185 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
186 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
189 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
190 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
191 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
192 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
193 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
202 if (isOK
== -1 && par_list
== 0) {
203 if (!nonce2key_ex(uid
, nt
, nr
, ks_list
, &r_key
) ){
204 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
210 if (isOK
!= 1) return 1;
212 // execute original function from util nonce2key
213 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
215 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
216 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
220 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
225 unsigned long elapsed_time
= difftime(end
, start
);
227 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
231 int CmdHF14AMfWrBl(const char *Cmd
) {
234 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
235 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
240 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
241 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
245 blockNo
= param_get8(Cmd
, 0);
246 cmdp
= param_getchar(Cmd
, 1);
248 PrintAndLog("Key type must be A or B");
251 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
252 if (param_gethex(Cmd
, 2, key
, 12)) {
253 PrintAndLog("Key must include 12 HEX symbols");
256 if (param_gethex(Cmd
, 3, bldata
, 32)) {
257 PrintAndLog("Block data must include 32 HEX symbols");
260 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
261 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
263 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
264 memcpy(c
.d
.asBytes
, key
, 6);
265 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
266 clearCommandBuffer();
270 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
271 uint8_t isOK
= resp
.arg
[0] & 0xff;
272 PrintAndLog("isOk:%02x", isOK
);
274 PrintAndLog("Command execute timeout");
280 int CmdHF14AMfRdBl(const char *Cmd
) {
283 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
289 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
290 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
294 blockNo
= param_get8(Cmd
, 0);
295 cmdp
= param_getchar(Cmd
, 1);
297 PrintAndLog("Key type must be A or B");
300 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
301 if (param_gethex(Cmd
, 2, key
, 12)) {
302 PrintAndLog("Key must include 12 HEX symbols");
305 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
307 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
308 memcpy(c
.d
.asBytes
, key
, 6);
309 clearCommandBuffer();
313 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
314 uint8_t isOK
= resp
.arg
[0] & 0xff;
315 uint8_t *data
= resp
.d
.asBytes
;
318 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
320 PrintAndLog("isOk:%02x", isOK
);
322 PrintAndLog("Command execute timeout");
328 int CmdHF14AMfRdSc(const char *Cmd
) {
330 uint8_t sectorNo
= 0;
332 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
334 uint8_t *data
= NULL
;
338 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
339 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
343 sectorNo
= param_get8(Cmd
, 0);
345 PrintAndLog("Sector number must be less than 40");
348 cmdp
= param_getchar(Cmd
, 1);
349 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
350 PrintAndLog("Key type must be A or B");
353 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
354 if (param_gethex(Cmd
, 2, key
, 12)) {
355 PrintAndLog("Key must include 12 HEX symbols");
358 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
360 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
361 memcpy(c
.d
.asBytes
, key
, 6);
362 clearCommandBuffer();
367 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
368 isOK
= resp
.arg
[0] & 0xff;
369 data
= resp
.d
.asBytes
;
371 PrintAndLog("isOk:%02x", isOK
);
373 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
374 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
376 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
379 PrintAndLog("Command execute timeout");
385 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
389 return 32 * 4 + (sectorNo
- 32) * 16;
393 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
401 int CmdHF14AMfDump(const char *Cmd
) {
402 uint8_t sectorNo
, blockNo
;
406 uint8_t rights
[40][4];
407 uint8_t carddata
[256][16];
408 uint8_t numSectors
= 16;
415 char cmdp
= param_getchar(Cmd
, 0);
417 case '0' : numSectors
= 5; break;
419 case '\0': numSectors
= 16; break;
420 case '2' : numSectors
= 32; break;
421 case '4' : numSectors
= 40; break;
422 default: numSectors
= 16;
425 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
426 PrintAndLog("Usage: hf mf dump [card memory]");
427 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
429 PrintAndLog("Samples: hf mf dump");
430 PrintAndLog(" hf mf dump 4");
434 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
435 PrintAndLog("Could not find file dumpkeys.bin");
439 // Read keys A from file
441 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
442 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
443 if ( bytes_read
== 0) {
444 PrintAndLog("File reading error.");
450 // Read keys B from file
451 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
452 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
453 if ( bytes_read
== 0) {
454 PrintAndLog("File reading error.");
462 PrintAndLog("|-----------------------------------------|");
463 PrintAndLog("|------ Reading sector access bits...-----|");
464 PrintAndLog("|-----------------------------------------|");
466 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
467 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
468 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
469 clearCommandBuffer();
472 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
473 uint8_t isOK
= resp
.arg
[0] & 0xff;
474 uint8_t *data
= resp
.d
.asBytes
;
476 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
477 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
478 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
479 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
481 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
482 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
483 rights
[sectorNo
][3] = 0x01;
486 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
487 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
488 rights
[sectorNo
][3] = 0x01;
492 PrintAndLog("|-----------------------------------------|");
493 PrintAndLog("|----- Dumping all blocks to file... -----|");
494 PrintAndLog("|-----------------------------------------|");
497 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
498 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
499 bool received
= false;
501 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
502 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
503 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
504 clearCommandBuffer();
506 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
507 } else { // data block. Check if it can be read with key A or key B
508 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
509 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
510 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
511 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
513 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
514 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
516 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
517 } else { // key A would work
518 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
519 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
520 clearCommandBuffer();
522 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
527 isOK
= resp
.arg
[0] & 0xff;
528 uint8_t *data
= resp
.d
.asBytes
;
529 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
530 data
[0] = (keyA
[sectorNo
][0]);
531 data
[1] = (keyA
[sectorNo
][1]);
532 data
[2] = (keyA
[sectorNo
][2]);
533 data
[3] = (keyA
[sectorNo
][3]);
534 data
[4] = (keyA
[sectorNo
][4]);
535 data
[5] = (keyA
[sectorNo
][5]);
536 data
[10] = (keyB
[sectorNo
][0]);
537 data
[11] = (keyB
[sectorNo
][1]);
538 data
[12] = (keyB
[sectorNo
][2]);
539 data
[13] = (keyB
[sectorNo
][3]);
540 data
[14] = (keyB
[sectorNo
][4]);
541 data
[15] = (keyB
[sectorNo
][5]);
544 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
545 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
547 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
553 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
560 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
561 PrintAndLog("Could not create file name dumpdata.bin");
564 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
565 fwrite(carddata
, 1, 16*numblocks
, fout
);
567 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
573 int CmdHF14AMfRestore(const char *Cmd
) {
574 uint8_t sectorNo
,blockNo
;
576 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
577 uint8_t bldata
[16] = {0x00};
585 char cmdp
= param_getchar(Cmd
, 0);
587 case '0' : numSectors
= 5; break;
589 case '\0': numSectors
= 16; break;
590 case '2' : numSectors
= 32; break;
591 case '4' : numSectors
= 40; break;
592 default: numSectors
= 16;
595 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
596 PrintAndLog("Usage: hf mf restore [card memory]");
597 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
599 PrintAndLog("Samples: hf mf restore");
600 PrintAndLog(" hf mf restore 4");
604 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
605 PrintAndLog("Could not find file dumpkeys.bin");
610 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
611 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
612 if ( bytes_read
== 0) {
613 PrintAndLog("File reading error (dumpkeys.bin).");
619 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
620 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
621 if ( bytes_read
== 0) {
622 PrintAndLog("File reading error (dumpkeys.bin).");
630 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
631 PrintAndLog("Could not find file dumpdata.bin");
634 PrintAndLog("Restoring dumpdata.bin to card");
636 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
637 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
638 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
639 memcpy(c
.d
.asBytes
, key
, 6);
640 bytes_read
= fread(bldata
, 1, 16, fdump
);
641 if ( bytes_read
== 0) {
642 PrintAndLog("File reading error (dumpdata.bin).");
647 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
648 bldata
[0] = (keyA
[sectorNo
][0]);
649 bldata
[1] = (keyA
[sectorNo
][1]);
650 bldata
[2] = (keyA
[sectorNo
][2]);
651 bldata
[3] = (keyA
[sectorNo
][3]);
652 bldata
[4] = (keyA
[sectorNo
][4]);
653 bldata
[5] = (keyA
[sectorNo
][5]);
654 bldata
[10] = (keyB
[sectorNo
][0]);
655 bldata
[11] = (keyB
[sectorNo
][1]);
656 bldata
[12] = (keyB
[sectorNo
][2]);
657 bldata
[13] = (keyB
[sectorNo
][3]);
658 bldata
[14] = (keyB
[sectorNo
][4]);
659 bldata
[15] = (keyB
[sectorNo
][5]);
662 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
664 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
665 clearCommandBuffer();
669 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
670 uint8_t isOK
= resp
.arg
[0] & 0xff;
671 PrintAndLog("isOk:%02x", isOK
);
673 PrintAndLog("Command execute timeout");
682 int CmdHF14AMfNested(const char *Cmd
) {
683 int i
, j
, res
, iterations
;
684 sector
*e_sector
= NULL
;
687 uint8_t trgBlockNo
= 0;
688 uint8_t trgKeyType
= 0;
689 uint8_t SectorsCnt
= 0;
690 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
691 uint8_t keyBlock
[6*6];
693 bool transferToEml
= false;
695 bool createDumpFile
= false;
697 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
698 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
700 if (strlen(Cmd
)<3) return usage_hf14_nested();
703 cmdp
= param_getchar(Cmd
, 0);
704 blockNo
= param_get8(Cmd
, 1);
705 ctmp
= param_getchar(Cmd
, 2);
707 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
708 PrintAndLog("Key type must be A or B");
712 if (ctmp
!= 'A' && ctmp
!= 'a')
715 if (param_gethex(Cmd
, 3, key
, 12)) {
716 PrintAndLog("Key must include 12 HEX symbols");
720 if (cmdp
== 'o' || cmdp
== 'O') {
722 trgBlockNo
= param_get8(Cmd
, 4);
723 ctmp
= param_getchar(Cmd
, 5);
724 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
725 PrintAndLog("Target key type must be A or B");
728 if (ctmp
!= 'A' && ctmp
!= 'a')
733 case '0': SectorsCnt
= 05; break;
734 case '1': SectorsCnt
= 16; break;
735 case '2': SectorsCnt
= 32; break;
736 case '4': SectorsCnt
= 40; break;
737 default: SectorsCnt
= 16;
741 ctmp
= param_getchar(Cmd
, 4);
742 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
743 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
745 ctmp
= param_getchar(Cmd
, 6);
746 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
747 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
750 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
752 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
753 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
754 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
755 case -4 : PrintAndLog("No valid key found"); break;
757 key64
= bytes_to_num(keyBlock
, 6);
759 // transfer key to the emulator
761 uint8_t sectortrailer
;
762 if (trgBlockNo
< 32*4) { // 4 block sector
763 sectortrailer
= (trgBlockNo
& 0x03) + 3;
764 } else { // 16 block sector
765 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
767 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
770 num_to_bytes(key64
, 6, keyBlock
);
772 num_to_bytes(key64
, 6, &keyBlock
[10]);
773 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
776 default : PrintAndLog("Unknown Error.\n");
780 else { // ------------------------------------ multiple sectors working
781 clock_t t1
= clock();
782 unsigned long elapsed_time
;
786 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
787 if (e_sector
== NULL
) return 1;
789 //test current key and additional standard keys first
790 memcpy(keyBlock
, key
, 6);
791 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
792 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
793 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
794 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
795 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
797 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
798 for (i
= 0; i
< SectorsCnt
; i
++) {
799 for (j
= 0; j
< 2; j
++) {
800 if (e_sector
[i
].foundKey
[j
]) continue;
802 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
805 e_sector
[i
].Key
[j
] = key64
;
806 e_sector
[i
].foundKey
[j
] = TRUE
;
810 clock_t t2
= clock() - t1
;
812 elapsed_time
= difftime(end
, start
);
814 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
816 PrintAndLog("enter nested...");
820 bool calibrate
= true;
822 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
823 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
824 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
826 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
828 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
830 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
831 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
832 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
833 case -4 : //key not found
840 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
841 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
844 default : PrintAndLog("Unknown Error.\n");
854 elapsed_time
= difftime(end
, start
);
856 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
859 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
860 PrintAndLog("trying to read key B...");
861 for (i
= 0; i
< SectorsCnt
; i
++) {
862 // KEY A but not KEY B
863 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
865 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
867 PrintAndLog("Reading block %d", sectrail
);
869 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
870 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
871 clearCommandBuffer();
875 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
877 uint8_t isOK
= resp
.arg
[0] & 0xff;
880 uint8_t *data
= resp
.d
.asBytes
;
881 key64
= bytes_to_num(data
+10, 6);
883 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
884 e_sector
[i
].foundKey
[1] = TRUE
;
885 e_sector
[i
].Key
[1] = key64
;
892 printKeyTable( SectorsCnt
, e_sector
);
894 // transfer them to the emulator
896 for (i
= 0; i
< SectorsCnt
; i
++) {
897 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
898 if (e_sector
[i
].foundKey
[0])
899 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
900 if (e_sector
[i
].foundKey
[1])
901 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
902 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
907 if (createDumpFile
) {
908 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
909 PrintAndLog("Could not create file dumpkeys.bin");
913 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
914 for(i
=0; i
<SectorsCnt
; i
++) {
915 if (e_sector
[i
].foundKey
[0]){
916 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
917 fwrite ( tempkey
, 1, 6, fkeys
);
920 fwrite ( &standart
, 1, 6, fkeys
);
923 for(i
=0; i
<SectorsCnt
; i
++) {
924 if (e_sector
[i
].foundKey
[1]){
925 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
926 fwrite ( tempkey
, 1, 6, fkeys
);
929 fwrite ( &standart
, 1, 6, fkeys
);
940 int CmdHF14AMfNestedHard(const char *Cmd
) {
943 uint8_t trgBlockNo
= 0;
944 uint8_t trgKeyType
= 0;
945 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
946 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
949 ctmp
= param_getchar(Cmd
, 0);
950 if (ctmp
== 'H' || ctmp
== 'h' ) return usage_hf14_hardnested();
951 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
953 bool know_target_key
= false;
954 bool nonce_file_read
= false;
955 bool nonce_file_write
= false;
959 if (ctmp
== 'R' || ctmp
== 'r') {
960 nonce_file_read
= true;
961 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
962 know_target_key
= true;
964 } else if (ctmp
== 'T' || ctmp
== 't') {
965 tests
= param_get32ex(Cmd
, 1, 100, 10);
967 blockNo
= param_get8(Cmd
, 0);
968 ctmp
= param_getchar(Cmd
, 1);
969 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
970 PrintAndLog("Key type must be A or B");
973 if (ctmp
!= 'A' && ctmp
!= 'a') {
977 if (param_gethex(Cmd
, 2, key
, 12)) {
978 PrintAndLog("Key must include 12 HEX symbols");
982 trgBlockNo
= param_get8(Cmd
, 3);
983 ctmp
= param_getchar(Cmd
, 4);
984 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
985 PrintAndLog("Target key type must be A or B");
988 if (ctmp
!= 'A' && ctmp
!= 'a') {
994 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
995 know_target_key
= true;
999 while ((ctmp
= param_getchar(Cmd
, i
))) {
1000 if (ctmp
== 's' || ctmp
== 'S') {
1002 } else if (ctmp
== 'w' || ctmp
== 'W') {
1003 nonce_file_write
= true;
1005 PrintAndLog("Possible options are w and/or s");
1012 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 ",
1015 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1016 know_target_key
? "" : " (not set)",
1017 nonce_file_write
? "write": nonce_file_read
? "read" : "none",
1018 slow
? "Yes" : "No",
1021 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1025 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1026 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1035 int CmdHF14AMfChk(const char *Cmd
) {
1037 if (strlen(Cmd
)<3) return usage_hf14_chk();
1040 char filename
[FILE_PATH_SIZE
]={0};
1042 uint8_t *keyBlock
= NULL
, *p
;
1043 uint8_t stKeyBlock
= 20;
1045 sector
*e_sector
= NULL
;
1050 uint8_t blockNo
= 0;
1051 uint8_t SectorsCnt
= 1;
1052 uint8_t keyType
= 0;
1055 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1057 int transferToEml
= 0;
1058 int createDumpFile
= 0;
1060 keyBlock
= calloc(stKeyBlock
, 6);
1061 if (keyBlock
== NULL
) return 1;
1063 uint64_t defaultKeys
[] = {
1064 0xffffffffffff, // Default key (first key used by program if no user defined key)
1065 0x000000000000, // Blank key
1066 0xa0a1a2a3a4a5, // NFCForum MAD key
1078 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1080 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1081 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1084 if (param_getchar(Cmd
, 0)=='*') {
1086 switch(param_getchar(Cmd
+1, 0)) {
1087 case '0': SectorsCnt
= 5; break;
1088 case '1': SectorsCnt
= 16; break;
1089 case '2': SectorsCnt
= 32; break;
1090 case '4': SectorsCnt
= 40; break;
1091 default: SectorsCnt
= 16;
1094 blockNo
= param_get8(Cmd
, 0);
1097 ctmp
= param_getchar(Cmd
, 1);
1109 PrintAndLog("Key type must be A , B or ?");
1114 ctmp
= param_getchar(Cmd
, 2);
1115 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1116 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1118 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1119 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1120 if ( stKeyBlock
- keycnt
< 2) {
1121 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1123 PrintAndLog("Cannot allocate memory for Keys");
1129 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1130 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1131 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1134 // May be a dic file
1135 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1136 PrintAndLog("File name too long");
1141 if ( (f
= fopen( filename
, "r")) ) {
1142 while( fgets(buf
, sizeof(buf
), f
) ){
1143 if (strlen(buf
) < 12 || buf
[11] == '\n')
1146 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1148 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1150 if (!isxdigit(buf
[0])){
1151 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1157 if ( stKeyBlock
- keycnt
< 2) {
1158 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1160 PrintAndLog("Cannot allocate memory for defKeys");
1167 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1168 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1169 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1171 memset(buf
, 0, sizeof(buf
));
1175 PrintAndLog("File: %s: not found or locked.", filename
);
1184 PrintAndLog("No key specified, trying default keys");
1185 for (;keycnt
< defaultKeysSize
; keycnt
++)
1186 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1187 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1188 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1191 // initialize storage for found keys
1192 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1193 if (e_sector
== NULL
) {
1199 for(int i
= 0; i
< SectorsCnt
; ++i
){
1200 e_sector
[i
].Key
[0] = 0xffffffffffff;
1201 e_sector
[i
].Key
[1] = 0xffffffffffff;
1202 e_sector
[i
].foundKey
[0] = FALSE
;
1203 e_sector
[i
].foundKey
[1] = FALSE
;
1207 uint8_t trgKeyType
= 0;
1208 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1211 clock_t t1
= clock();
1216 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1219 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1221 // skip already found keys.
1222 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1224 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1227 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1229 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1231 e_sector
[i
].Key
[trgKeyType
] = key64
;
1232 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1236 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1241 unsigned long elapsed_time
= difftime(end
, start
);
1243 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1246 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1247 if ( keyType
!= 1 ) {
1248 PrintAndLog("testing to read key B...");
1249 for (i
= 0; i
< SectorsCnt
; i
++) {
1250 // KEY A but not KEY B
1251 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1253 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1255 PrintAndLog("Reading block %d", sectrail
);
1257 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1258 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1259 clearCommandBuffer();
1263 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1265 uint8_t isOK
= resp
.arg
[0] & 0xff;
1266 if (!isOK
) continue;
1268 uint8_t *data
= resp
.d
.asBytes
;
1269 key64
= bytes_to_num(data
+10, 6);
1271 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1272 e_sector
[i
].foundKey
[1] = 1;
1273 e_sector
[i
].Key
[1] = key64
;
1281 printKeyTable( SectorsCnt
, e_sector
);
1283 if (transferToEml
) {
1284 uint8_t block
[16] = {0x00};
1285 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1286 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1287 if (e_sector
[i
].foundKey
[0])
1288 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1289 if (e_sector
[i
].foundKey
[1])
1290 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1291 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1293 PrintAndLog("Found keys have been transferred to the emulator memory");
1296 if (createDumpFile
) {
1297 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1298 if (fkeys
== NULL
) {
1299 PrintAndLog("Could not create file dumpkeys.bin");
1304 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1306 for( i
=0; i
<SectorsCnt
; i
++) {
1307 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1308 fwrite ( tempkey
, 1, 6, fkeys
);
1310 for(i
=0; i
<SectorsCnt
; i
++) {
1311 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1312 fwrite ( tempkey
, 1, 6, fkeys
);
1315 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1323 #define ATTACK_KEY_COUNT 8
1324 sector
*k_sector
= NULL
;
1325 uint8_t k_sectorsCount
= 16;
1326 void readerAttack(nonces_t data
[], bool setEmulatorMem
) {
1328 // initialize storage for found keys
1329 if (k_sector
== NULL
);
1330 k_sector
= calloc(k_sectorsCount
, sizeof(sector
));
1331 if (k_sector
== NULL
)
1337 for(int i
= 0; i
< k_sectorsCount
; ++i
){
1338 k_sector
[i
].Key
[0] = 0xffffffffffff;
1339 k_sector
[i
].Key
[1] = 0xffffffffffff;
1340 k_sector
[i
].foundKey
[0] = FALSE
;
1341 k_sector
[i
].foundKey
[1] = FALSE
;
1344 printf("enter reader attack\n");
1345 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; ++i
) {
1346 if (data
[i
].ar2
> 0) {
1348 if (tryMfk32(data
[i
], &key
)) {
1349 PrintAndLog("Found Key%s for sector %02d: [%012"llx
"]"
1350 , (data
[i
].keytype
) ? "B" : "A"
1355 k_sector
[i
].Key
[data
[i
].keytype
] = key
;
1356 k_sector
[i
].foundKey
[data
[i
].keytype
] = TRUE
;
1358 //set emulator memory for keys
1359 if (setEmulatorMem
) {
1360 uint8_t memBlock
[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1361 num_to_bytes( k_sector
[i
].Key
[0], 6, memBlock
);
1362 num_to_bytes( k_sector
[i
].Key
[1], 6, memBlock
+10);
1363 mfEmlSetMem( memBlock
, i
*4 + 3, 1);
1364 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1366 , sprint_hex( memBlock
, sizeof(memBlock
))
1372 // if (tryMfk32_moebius(data[i+ATTACK_KEY_COUNT], &key)) {
1373 // PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"
1374 // ,(data[i+ATTACK_KEY_COUNT].keytype) ? "B" : "A"
1375 // , data[i+ATTACK_KEY_COUNT].sector
1383 int CmdHF14AMf1kSim(const char *Cmd
) {
1385 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1386 uint8_t exitAfterNReads
= 0;
1387 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1389 bool setEmulatorMem
= false;
1391 bool errors
= false;
1393 while(param_getchar(Cmd
, cmdp
) != 0x00) {
1394 switch(param_getchar(Cmd
, cmdp
)) {
1397 setEmulatorMem
= true;
1402 return usage_hf14_mf1ksim();
1405 flags
|= FLAG_INTERACTIVE
;
1410 exitAfterNReads
= param_get8(Cmd
, cmdp
+1);
1415 param_gethex_ex(Cmd
, cmdp
+1, uid
, &uidlen
);
1417 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1418 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1419 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1420 default: return usage_hf14_mf1ksim();
1426 flags
|= FLAG_NR_AR_ATTACK
;
1430 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1437 if(errors
) return usage_hf14_mf1ksim();
1439 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1440 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1445 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1446 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1447 clearCommandBuffer();
1450 if(flags
& FLAG_INTERACTIVE
) {
1451 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1453 nonces_t data
[ATTACK_KEY_COUNT
*2];
1457 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1459 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1460 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1462 memcpy( data
, resp
.d
.asBytes
, sizeof(data
) );
1463 readerAttack(data
, setEmulatorMem
);
1466 if (k_sector
!= NULL
) {
1467 printKeyTable(k_sectorsCount
, k_sector
);
1474 int CmdHF14AMfSniff(const char *Cmd
){
1475 bool wantLogToFile
= FALSE
;
1476 bool wantDecrypt
= FALSE
;
1477 //bool wantSaveToEml = FALSE; TODO
1478 bool wantSaveToEmlFile
= FALSE
;
1488 uint8_t uid_len
= 0;
1489 uint8_t atqa
[2] = {0x00, 0x00};
1492 uint8_t *buf
= NULL
;
1493 uint16_t bufsize
= 0;
1494 uint8_t *bufPtr
= NULL
;
1495 uint16_t traceLen
= 0;
1497 memset(uid
, 0x00, sizeof(uid
));
1499 char ctmp
= param_getchar(Cmd
, 0);
1500 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1502 for (int i
= 0; i
< 4; i
++) {
1503 ctmp
= param_getchar(Cmd
, i
);
1504 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1505 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1506 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1507 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1510 printf("-------------------------------------------------------------------------\n");
1511 printf("Executing mifare sniffing command. \n");
1512 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1513 printf("Press the key on pc keyboard to abort the client.\n");
1514 printf("-------------------------------------------------------------------------\n");
1516 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1517 clearCommandBuffer();
1525 tmpchar
= getchar();
1527 printf("\naborted via keyboard!\n");
1532 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1533 res
= resp
.arg
[0] & 0xff;
1534 traceLen
= resp
.arg
[1];
1543 if (res
== 1) { // there is (more) data to be transferred
1544 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1545 if (traceLen
> bufsize
) {
1547 if (buf
== NULL
) // not yet allocated
1548 p
= malloc(traceLen
);
1549 else // need more memory
1550 p
= realloc(buf
, traceLen
);
1553 PrintAndLog("Cannot allocate memory for trace");
1561 memset(buf
, 0x00, traceLen
);
1563 if (bufPtr
== NULL
) {
1564 PrintAndLog("Cannot allocate memory for trace");
1568 // what happens if LEN is bigger then TRACELEN --iceman
1569 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1574 if (res
== 2) { // received all data, start displaying
1575 blockLen
= bufPtr
- buf
;
1578 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1579 while (bufPtr
- buf
< blockLen
) {
1580 bufPtr
+= 6; // skip (void) timing information
1581 len
= *((uint16_t *)bufPtr
);
1589 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1590 memcpy(uid
, bufPtr
+ 2, 10);
1591 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1592 switch (atqa
[0] & 0xC0) {
1593 case 0x80: uid_len
= 10; break;
1594 case 0x40: uid_len
= 7; break;
1595 default: uid_len
= 4; break;
1598 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1599 sprint_hex(uid
, uid_len
),
1603 if (wantLogToFile
|| wantDecrypt
) {
1604 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1605 AddLogCurrentDT(logHexFileName
);
1608 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1610 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1612 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1614 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1618 bufPtr
+= ((len
-1)/8+1); // ignore parity
1629 int CmdHF14AMfDbg(const char *Cmd
) {
1631 char ctmp
= param_getchar(Cmd
, 0);
1632 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_hf14_dbg();
1634 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1635 if (dbgMode
> 4) return usage_hf14_dbg();
1637 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1642 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1643 PrintAndLog("|---|----------------|---|----------------|---|");
1644 PrintAndLog("|sec|key A |res|key B |res|");
1645 PrintAndLog("|---|----------------|---|----------------|---|");
1646 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1647 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1648 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1649 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1652 PrintAndLog("|---|----------------|---|----------------|---|");
1655 // EMULATOR COMMANDS
1656 int CmdHF14AMfEGet(const char *Cmd
)
1658 uint8_t blockNo
= 0;
1659 uint8_t data
[16] = {0x00};
1661 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1662 PrintAndLog("Usage: hf mf eget <block number>");
1663 PrintAndLog(" sample: hf mf eget 0 ");
1667 blockNo
= param_get8(Cmd
, 0);
1670 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1671 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1673 PrintAndLog("Command execute timeout");
1679 int CmdHF14AMfEClear(const char *Cmd
)
1681 if (param_getchar(Cmd
, 0) == 'h') {
1682 PrintAndLog("Usage: hf mf eclr");
1683 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1687 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1692 int CmdHF14AMfESet(const char *Cmd
)
1694 uint8_t memBlock
[16];
1695 uint8_t blockNo
= 0;
1697 memset(memBlock
, 0x00, sizeof(memBlock
));
1699 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1700 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1701 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1705 blockNo
= param_get8(Cmd
, 0);
1707 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1708 PrintAndLog("block data must include 32 HEX symbols");
1713 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1714 memcpy(c
.d
.asBytes
, memBlock
, 16);
1719 int CmdHF14AMfELoad(const char *Cmd
)
1722 char filename
[FILE_PATH_SIZE
];
1723 char *fnameptr
= filename
;
1724 char buf
[64] = {0x00};
1725 uint8_t buf8
[64] = {0x00};
1726 int i
, len
, blockNum
, numBlocks
;
1727 int nameParamNo
= 1;
1728 uint8_t blockWidth
= 32;
1729 char ctmp
= param_getchar(Cmd
, 0);
1731 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1732 PrintAndLog("It loads emul dump from the file `filename.eml`");
1733 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1734 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1736 PrintAndLog(" sample: hf mf eload filename");
1737 PrintAndLog(" hf mf eload 4 filename");
1742 case '0' : numBlocks
= 5*4; break;
1744 case '\0': numBlocks
= 16*4; break;
1745 case '2' : numBlocks
= 32*4; break;
1746 case '4' : numBlocks
= 256; break;
1747 case 'U' : // fall through
1748 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1754 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1755 if (numblk2
> 0) numBlocks
= numblk2
;
1757 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1759 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1763 sprintf(fnameptr
, ".eml");
1766 f
= fopen(filename
, "r");
1768 PrintAndLog("File %s not found or locked", filename
);
1774 memset(buf
, 0, sizeof(buf
));
1776 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1778 if (blockNum
>= numBlocks
) break;
1780 PrintAndLog("File reading error.");
1785 if (strlen(buf
) < blockWidth
){
1786 if(strlen(buf
) && feof(f
))
1788 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1793 for (i
= 0; i
< blockWidth
; i
+= 2) {
1794 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1796 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1797 PrintAndLog("Cant set emul block: %3d", blockNum
);
1804 if (blockNum
>= numBlocks
) break;
1809 if ((blockNum
!= numBlocks
)) {
1810 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1813 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1817 int CmdHF14AMfESave(const char *Cmd
)
1820 char filename
[FILE_PATH_SIZE
];
1821 char * fnameptr
= filename
;
1823 int i
, j
, len
, numBlocks
;
1824 int nameParamNo
= 1;
1826 memset(filename
, 0, sizeof(filename
));
1827 memset(buf
, 0, sizeof(buf
));
1829 char ctmp
= param_getchar(Cmd
, 0);
1831 if ( ctmp
== 'h' || ctmp
== 'H') {
1832 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1833 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1834 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1836 PrintAndLog(" sample: hf mf esave ");
1837 PrintAndLog(" hf mf esave 4");
1838 PrintAndLog(" hf mf esave 4 filename");
1843 case '0' : numBlocks
= 5*4; break;
1845 case '\0': numBlocks
= 16*4; break;
1846 case '2' : numBlocks
= 32*4; break;
1847 case '4' : numBlocks
= 256; break;
1854 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1856 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1858 // user supplied filename?
1860 // get filename (UID from memory)
1861 if (mfEmlGetMem(buf
, 0, 1)) {
1862 PrintAndLog("Can\'t get UID from block: %d", 0);
1863 len
= sprintf(fnameptr
, "dump");
1867 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1868 sprintf(fnameptr
, "%02X", buf
[j
]);
1874 // add file extension
1875 sprintf(fnameptr
, ".eml");
1878 f
= fopen(filename
, "w+");
1881 PrintAndLog("Can't open file %s ", filename
);
1886 for (i
= 0; i
< numBlocks
; i
++) {
1887 if (mfEmlGetMem(buf
, i
, 1)) {
1888 PrintAndLog("Cant get block: %d", i
);
1891 for (j
= 0; j
< 16; j
++)
1892 fprintf(f
, "%02X", buf
[j
]);
1897 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1902 int CmdHF14AMfECFill(const char *Cmd
)
1904 uint8_t keyType
= 0;
1905 uint8_t numSectors
= 16;
1907 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1908 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1909 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1911 PrintAndLog("samples: hf mf ecfill A");
1912 PrintAndLog(" hf mf ecfill A 4");
1913 PrintAndLog("Read card and transfer its data to emulator memory.");
1914 PrintAndLog("Keys must be laid in the emulator memory. \n");
1918 char ctmp
= param_getchar(Cmd
, 0);
1919 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1920 PrintAndLog("Key type must be A or B");
1923 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1925 ctmp
= param_getchar(Cmd
, 1);
1927 case '0' : numSectors
= 5; break;
1929 case '\0': numSectors
= 16; break;
1930 case '2' : numSectors
= 32; break;
1931 case '4' : numSectors
= 40; break;
1932 default: numSectors
= 16;
1935 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1936 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1941 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1946 uint64_t keyA
, keyB
;
1948 char cmdp
= param_getchar(Cmd
, 0);
1950 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1951 PrintAndLog("It prints the keys loaded in the emulator memory");
1952 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1953 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1955 PrintAndLog(" sample: hf mf ekeyprn 1");
1960 case '0' : numSectors
= 5; break;
1962 case '\0': numSectors
= 16; break;
1963 case '2' : numSectors
= 32; break;
1964 case '4' : numSectors
= 40; break;
1965 default: numSectors
= 16;
1968 PrintAndLog("|---|----------------|----------------|");
1969 PrintAndLog("|sec|key A |key B |");
1970 PrintAndLog("|---|----------------|----------------|");
1971 for (i
= 0; i
< numSectors
; i
++) {
1972 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1973 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1976 keyA
= bytes_to_num(data
, 6);
1977 keyB
= bytes_to_num(data
+ 10, 6);
1978 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1980 PrintAndLog("|---|----------------|----------------|");
1985 // CHINESE MAGIC COMMANDS
1987 int CmdHF14AMfCSetUID(const char *Cmd
) {
1988 uint8_t wipeCard
= 0;
1989 uint8_t uid
[8] = {0x00};
1990 uint8_t oldUid
[8] = {0x00};
1991 uint8_t atqa
[2] = {0x00};
1992 uint8_t sak
[1] = {0x00};
1993 uint8_t atqaPresent
= 1;
1998 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1999 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2000 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2002 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2004 PrintAndLog("sample: hf mf csetuid 01020304");
2005 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2009 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
2010 PrintAndLog("UID must include 8 HEX symbols");
2015 ctmp
= param_getchar(Cmd
, argi
);
2016 if (ctmp
== 'w' || ctmp
== 'W') {
2022 if (param_getchar(Cmd
, argi
)) {
2023 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
2024 PrintAndLog("ATQA must include 4 HEX symbols");
2028 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
2029 PrintAndLog("SAK must include 2 HEX symbols");
2038 ctmp
= param_getchar(Cmd
, argi
);
2039 if (ctmp
== 'w' || ctmp
== 'W') {
2044 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
2046 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
2048 PrintAndLog("Can't set UID. error=%d", res
);
2052 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
2053 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
2057 int CmdHF14AMfCSetBlk(const char *Cmd
) {
2058 uint8_t block
[16] = {0x00};
2059 uint8_t blockNo
= 0;
2060 uint8_t params
= MAGIC_SINGLE
;
2063 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
2064 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2065 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2066 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2067 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2071 blockNo
= param_get8(Cmd
, 0);
2073 if (param_gethex(Cmd
, 1, block
, 32)) {
2074 PrintAndLog("block data must include 32 HEX symbols");
2078 char ctmp
= param_getchar(Cmd
, 2);
2079 if (ctmp
== 'w' || ctmp
== 'W')
2080 params
|= MAGIC_WIPE
;
2082 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2084 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2086 PrintAndLog("Can't write block. error=%d", res
);
2092 int CmdHF14AMfCLoad(const char *Cmd
) {
2094 char filename
[FILE_PATH_SIZE
];
2095 char * fnameptr
= filename
;
2096 char buf
[64] = {0x00};
2097 uint8_t buf8
[64] = {0x00};
2098 uint8_t fillFromEmulator
= 0;
2099 int i
, len
, blockNum
, flags
=0;
2101 memset(filename
, 0, sizeof(filename
));
2103 char ctmp
= param_getchar(Cmd
, 0);
2105 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2106 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2107 PrintAndLog("or from emulator memory (option `e`)");
2108 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2109 PrintAndLog(" or: hf mf cload e ");
2110 PrintAndLog(" sample: hf mf cload filename");
2114 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2116 if (fillFromEmulator
) {
2117 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2118 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2119 PrintAndLog("Cant get block: %d", blockNum
);
2122 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2123 if (blockNum
== 1) flags
= 0; // just write
2124 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2126 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2127 PrintAndLog("Cant set magic card block: %d", blockNum
);
2134 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2136 memcpy(filename
, Cmd
, len
);
2139 sprintf(fnameptr
, ".eml");
2142 f
= fopen(filename
, "r");
2144 PrintAndLog("File not found or locked.");
2151 memset(buf
, 0, sizeof(buf
));
2153 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2155 PrintAndLog("File reading error.");
2159 if (strlen(buf
) < 32) {
2160 if(strlen(buf
) && feof(f
))
2162 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2166 for (i
= 0; i
< 32; i
+= 2)
2167 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2169 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2170 if (blockNum
== 1) flags
= 0; // just write
2171 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2173 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2174 PrintAndLog("Can't set magic card block: %d", blockNum
);
2180 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2185 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2186 PrintAndLog("File content error. There must be 64 blocks");
2189 PrintAndLog("Loaded from file: %s", filename
);
2195 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2197 uint8_t blockNo
= 0;
2199 memset(data
, 0x00, sizeof(data
));
2200 char ctmp
= param_getchar(Cmd
, 0);
2202 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2203 PrintAndLog("Usage: hf mf cgetblk <block number>");
2204 PrintAndLog("sample: hf mf cgetblk 1");
2205 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2209 blockNo
= param_get8(Cmd
, 0);
2211 PrintAndLog("--block number:%2d ", blockNo
);
2213 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2215 PrintAndLog("Can't read block. error=%d", res
);
2219 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2223 int CmdHF14AMfCGetSc(const char *Cmd
) {
2225 uint8_t sectorNo
= 0;
2227 memset(data
, 0x00, sizeof(data
));
2228 char ctmp
= param_getchar(Cmd
, 0);
2230 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2231 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2232 PrintAndLog("sample: hf mf cgetsc 0");
2233 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2237 sectorNo
= param_get8(Cmd
, 0);
2238 if (sectorNo
> 15) {
2239 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2243 PrintAndLog("--sector number:%d ", sectorNo
);
2244 PrintAndLog("block | data");
2246 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2247 for (i
= 0; i
< 4; i
++) {
2248 if (i
== 1) flags
= 0;
2249 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2251 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2253 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2256 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2261 int CmdHF14AMfCSave(const char *Cmd
) {
2264 char filename
[FILE_PATH_SIZE
];
2265 char * fnameptr
= filename
;
2266 uint8_t fillFromEmulator
= 0;
2268 int i
, j
, len
, flags
;
2270 memset(filename
, 0, sizeof(filename
));
2271 memset(buf
, 0, sizeof(buf
));
2272 char ctmp
= param_getchar(Cmd
, 0);
2274 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2275 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2276 PrintAndLog("or into emulator memory (option `e`)");
2277 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2278 PrintAndLog(" sample: hf mf esave ");
2279 PrintAndLog(" hf mf esave filename");
2280 PrintAndLog(" hf mf esave e \n");
2283 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2285 if (fillFromEmulator
) {
2286 // put into emulator
2287 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2288 for (i
= 0; i
< 16 * 4; i
++) {
2289 if (i
== 1) flags
= 0;
2290 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2292 if (mfCGetBlock(i
, buf
, flags
)) {
2293 PrintAndLog("Cant get block: %d", i
);
2297 if (mfEmlSetMem(buf
, i
, 1)) {
2298 PrintAndLog("Cant set emul block: %d", i
);
2305 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2307 // get filename based on UID
2310 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2311 PrintAndLog("Cant get block: %d", 0);
2312 len
= sprintf(fnameptr
, "dump");
2315 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2316 sprintf(fnameptr
, "%02x", buf
[j
]);
2319 memcpy(filename
, Cmd
, len
);
2323 // add .eml extension
2324 sprintf(fnameptr
, ".eml");
2327 f
= fopen(filename
, "w+");
2330 PrintAndLog("File not found or locked.");
2335 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2336 for (i
= 0; i
< 16 * 4; i
++) {
2337 if (i
== 1) flags
= 0;
2338 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2340 if (mfCGetBlock(i
, buf
, flags
)) {
2341 PrintAndLog("Cant get block: %d", i
);
2344 for (j
= 0; j
< 16; j
++)
2345 fprintf(f
, "%02x", buf
[j
]);
2350 PrintAndLog("Saved to file: %s", filename
);
2355 //needs nt, ar, at, Data to decrypt
2356 int CmdHf14MfDecryptBytes(const char *Cmd
){
2358 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2359 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2360 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2363 param_gethex_ex(Cmd
, 3, data
, &len
);
2366 int limit
= sizeof(data
) / 2;
2371 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2374 static command_t CommandTable
[] = {
2375 {"help", CmdHelp
, 1, "This help"},
2376 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2377 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2378 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2379 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2380 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2381 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2382 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2383 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2384 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2385 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2386 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2387 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2388 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2389 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2390 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2391 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2392 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2393 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2394 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2395 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2396 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2397 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2398 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2399 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2400 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2401 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2402 {NULL
, NULL
, 0, NULL
}
2405 int CmdHFMF(const char *Cmd
) {
2406 clearCommandBuffer();
2407 CmdsParse(CommandTable
, Cmd
);
2411 int CmdHelp(const char *Cmd
) {
2412 CmdsHelp(CommandTable
);