]>
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 //-----------------------------------------------------------------------------
12 #include "cmdhfmfhard.h"
13 #include "nonce2key/nonce2key.h"
15 static int CmdHelp(const char *Cmd
);
16 int usage_hf14_mifare(void){
17 PrintAndLog("Usage: hf mf mifare [h] <block number>");
18 PrintAndLog("options:");
19 PrintAndLog(" h this help");
20 PrintAndLog(" <block number> (Optional) target other key A than block 0.");
21 PrintAndLog("samples:");
22 PrintAndLog(" hf mf mifare");
23 PrintAndLog(" hf mf mifare 16");
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");
98 PrintAndLog("samples:");
99 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
100 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
101 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
102 PrintAndLog(" hf mf hardnested r");
104 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
105 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
108 int usage_hf14_chk(void){
109 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
110 PrintAndLog("options:");
111 PrintAndLog(" h this help");
112 PrintAndLog(" * all sectors");
113 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
114 PrintAndLog(" d write keys to binary file");
115 PrintAndLog(" t write keys to emulator memory\n");
117 PrintAndLog("samples:");
118 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic");
119 PrintAndLog(" hf mf chk *1 ? t");
120 PrintAndLog(" hf mf chk *1 ? d");
124 int CmdHF14AMifare(const char *Cmd
) {
126 uint32_t nt
= 0, nr
= 0;
127 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
132 char cmdp
= param_getchar(Cmd
, 0);
133 if ( cmdp
== 'H' || cmdp
== 'h') return usage_hf14_mifare();
135 blockNo
= param_get8(Cmd
, 0);
136 UsbCommand c
= {CMD_READER_MIFARE
, {true, blockNo
, 0}};
139 printf("-------------------------------------------------------------------------\n");
140 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
141 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
142 printf("-------------------------------------------------------------------------\n");
143 clock_t t1
= clock();
148 clearCommandBuffer();
164 printf("\naborted via keyboard!\n");
169 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
172 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
173 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
174 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
175 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
176 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
179 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
180 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
181 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
182 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
183 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
192 if (isOK
== -1 && par_list
== 0) {
193 if (!nonce2key_ex(uid
, nt
, nr
, ks_list
, &r_key
) ){
194 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
200 if (isOK
!= 1) return 1;
202 // execute original function from util nonce2key
203 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
205 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
206 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
210 PrintAndLog("Found valid key: %012"llx
" \n", r_key
);
215 unsigned long elapsed_time
= difftime(end
, start
);
217 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
221 int CmdHF14AMfWrBl(const char *Cmd
) {
224 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
225 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
230 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
231 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
235 blockNo
= param_get8(Cmd
, 0);
236 cmdp
= param_getchar(Cmd
, 1);
238 PrintAndLog("Key type must be A or B");
241 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
242 if (param_gethex(Cmd
, 2, key
, 12)) {
243 PrintAndLog("Key must include 12 HEX symbols");
246 if (param_gethex(Cmd
, 3, bldata
, 32)) {
247 PrintAndLog("Block data must include 32 HEX symbols");
250 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
251 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
253 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
254 memcpy(c
.d
.asBytes
, key
, 6);
255 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
256 clearCommandBuffer();
260 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
261 uint8_t isOK
= resp
.arg
[0] & 0xff;
262 PrintAndLog("isOk:%02x", isOK
);
264 PrintAndLog("Command execute timeout");
270 int CmdHF14AMfRdBl(const char *Cmd
) {
273 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
279 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
280 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
284 blockNo
= param_get8(Cmd
, 0);
285 cmdp
= param_getchar(Cmd
, 1);
287 PrintAndLog("Key type must be A or B");
290 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
291 if (param_gethex(Cmd
, 2, key
, 12)) {
292 PrintAndLog("Key must include 12 HEX symbols");
295 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
297 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
298 memcpy(c
.d
.asBytes
, key
, 6);
299 clearCommandBuffer();
303 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
304 uint8_t isOK
= resp
.arg
[0] & 0xff;
305 uint8_t *data
= resp
.d
.asBytes
;
308 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
310 PrintAndLog("isOk:%02x", isOK
);
312 PrintAndLog("Command execute timeout");
318 int CmdHF14AMfRdSc(const char *Cmd
) {
320 uint8_t sectorNo
= 0;
322 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
324 uint8_t *data
= NULL
;
328 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
329 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
333 sectorNo
= param_get8(Cmd
, 0);
335 PrintAndLog("Sector number must be less than 40");
338 cmdp
= param_getchar(Cmd
, 1);
339 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
340 PrintAndLog("Key type must be A or B");
343 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
344 if (param_gethex(Cmd
, 2, key
, 12)) {
345 PrintAndLog("Key must include 12 HEX symbols");
348 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
350 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
351 memcpy(c
.d
.asBytes
, key
, 6);
352 clearCommandBuffer();
357 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
358 isOK
= resp
.arg
[0] & 0xff;
359 data
= resp
.d
.asBytes
;
361 PrintAndLog("isOk:%02x", isOK
);
363 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
364 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
366 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
369 PrintAndLog("Command execute timeout");
375 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
379 return 32 * 4 + (sectorNo
- 32) * 16;
383 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
391 int CmdHF14AMfDump(const char *Cmd
) {
392 uint8_t sectorNo
, blockNo
;
396 uint8_t rights
[40][4];
397 uint8_t carddata
[256][16];
398 uint8_t numSectors
= 16;
405 char cmdp
= param_getchar(Cmd
, 0);
407 case '0' : numSectors
= 5; break;
409 case '\0': numSectors
= 16; break;
410 case '2' : numSectors
= 32; break;
411 case '4' : numSectors
= 40; break;
412 default: numSectors
= 16;
415 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
416 PrintAndLog("Usage: hf mf dump [card memory]");
417 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
419 PrintAndLog("Samples: hf mf dump");
420 PrintAndLog(" hf mf dump 4");
424 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
425 PrintAndLog("Could not find file dumpkeys.bin");
429 // Read keys A from file
431 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
432 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fin
);
433 if ( bytes_read
== 0) {
434 PrintAndLog("File reading error.");
440 // Read keys B from file
441 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
442 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fin
);
443 if ( bytes_read
== 0) {
444 PrintAndLog("File reading error.");
452 PrintAndLog("|-----------------------------------------|");
453 PrintAndLog("|------ Reading sector access bits...-----|");
454 PrintAndLog("|-----------------------------------------|");
456 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
457 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
458 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
459 clearCommandBuffer();
462 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
463 uint8_t isOK
= resp
.arg
[0] & 0xff;
464 uint8_t *data
= resp
.d
.asBytes
;
466 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
467 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
468 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
469 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
471 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
472 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
473 rights
[sectorNo
][3] = 0x01;
476 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
477 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
478 rights
[sectorNo
][3] = 0x01;
482 PrintAndLog("|-----------------------------------------|");
483 PrintAndLog("|----- Dumping all blocks to file... -----|");
484 PrintAndLog("|-----------------------------------------|");
487 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
488 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
489 bool received
= false;
491 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
492 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
493 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
494 clearCommandBuffer();
496 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
497 } else { // data block. Check if it can be read with key A or key B
498 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
499 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
500 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
501 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
503 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
504 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
506 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
507 } else { // key A would work
508 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
509 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
510 clearCommandBuffer();
512 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
517 isOK
= resp
.arg
[0] & 0xff;
518 uint8_t *data
= resp
.d
.asBytes
;
519 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
520 data
[0] = (keyA
[sectorNo
][0]);
521 data
[1] = (keyA
[sectorNo
][1]);
522 data
[2] = (keyA
[sectorNo
][2]);
523 data
[3] = (keyA
[sectorNo
][3]);
524 data
[4] = (keyA
[sectorNo
][4]);
525 data
[5] = (keyA
[sectorNo
][5]);
526 data
[10] = (keyB
[sectorNo
][0]);
527 data
[11] = (keyB
[sectorNo
][1]);
528 data
[12] = (keyB
[sectorNo
][2]);
529 data
[13] = (keyB
[sectorNo
][3]);
530 data
[14] = (keyB
[sectorNo
][4]);
531 data
[15] = (keyB
[sectorNo
][5]);
534 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
535 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
537 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
543 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
550 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
551 PrintAndLog("Could not create file name dumpdata.bin");
554 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
555 fwrite(carddata
, 1, 16*numblocks
, fout
);
557 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
563 int CmdHF14AMfRestore(const char *Cmd
) {
564 uint8_t sectorNo
,blockNo
;
566 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
567 uint8_t bldata
[16] = {0x00};
575 char cmdp
= param_getchar(Cmd
, 0);
577 case '0' : numSectors
= 5; break;
579 case '\0': numSectors
= 16; break;
580 case '2' : numSectors
= 32; break;
581 case '4' : numSectors
= 40; break;
582 default: numSectors
= 16;
585 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
586 PrintAndLog("Usage: hf mf restore [card memory]");
587 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
589 PrintAndLog("Samples: hf mf restore");
590 PrintAndLog(" hf mf restore 4");
594 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
595 PrintAndLog("Could not find file dumpkeys.bin");
600 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
601 bytes_read
= fread( keyA
[sectorNo
], 1, 6, fkeys
);
602 if ( bytes_read
== 0) {
603 PrintAndLog("File reading error (dumpkeys.bin).");
609 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
610 bytes_read
= fread( keyB
[sectorNo
], 1, 6, fkeys
);
611 if ( bytes_read
== 0) {
612 PrintAndLog("File reading error (dumpkeys.bin).");
620 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
621 PrintAndLog("Could not find file dumpdata.bin");
624 PrintAndLog("Restoring dumpdata.bin to card");
626 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
627 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
628 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
629 memcpy(c
.d
.asBytes
, key
, 6);
630 bytes_read
= fread(bldata
, 1, 16, fdump
);
631 if ( bytes_read
== 0) {
632 PrintAndLog("File reading error (dumpdata.bin).");
637 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
638 bldata
[0] = (keyA
[sectorNo
][0]);
639 bldata
[1] = (keyA
[sectorNo
][1]);
640 bldata
[2] = (keyA
[sectorNo
][2]);
641 bldata
[3] = (keyA
[sectorNo
][3]);
642 bldata
[4] = (keyA
[sectorNo
][4]);
643 bldata
[5] = (keyA
[sectorNo
][5]);
644 bldata
[10] = (keyB
[sectorNo
][0]);
645 bldata
[11] = (keyB
[sectorNo
][1]);
646 bldata
[12] = (keyB
[sectorNo
][2]);
647 bldata
[13] = (keyB
[sectorNo
][3]);
648 bldata
[14] = (keyB
[sectorNo
][4]);
649 bldata
[15] = (keyB
[sectorNo
][5]);
652 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
654 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
655 clearCommandBuffer();
659 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
660 uint8_t isOK
= resp
.arg
[0] & 0xff;
661 PrintAndLog("isOk:%02x", isOK
);
663 PrintAndLog("Command execute timeout");
672 int CmdHF14AMfNested(const char *Cmd
) {
673 int i
, j
, res
, iterations
;
674 sector
*e_sector
= NULL
;
677 uint8_t trgBlockNo
= 0;
678 uint8_t trgKeyType
= 0;
679 uint8_t SectorsCnt
= 0;
680 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
681 uint8_t keyBlock
[6*6];
683 bool transferToEml
= false;
685 bool createDumpFile
= false;
687 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
688 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
690 if (strlen(Cmd
)<3) return usage_hf14_nested();
693 cmdp
= param_getchar(Cmd
, 0);
694 blockNo
= param_get8(Cmd
, 1);
695 ctmp
= param_getchar(Cmd
, 2);
697 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
698 PrintAndLog("Key type must be A or B");
702 if (ctmp
!= 'A' && ctmp
!= 'a')
705 if (param_gethex(Cmd
, 3, key
, 12)) {
706 PrintAndLog("Key must include 12 HEX symbols");
710 if (cmdp
== 'o' || cmdp
== 'O') {
712 trgBlockNo
= param_get8(Cmd
, 4);
713 ctmp
= param_getchar(Cmd
, 5);
714 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
715 PrintAndLog("Target key type must be A or B");
718 if (ctmp
!= 'A' && ctmp
!= 'a')
723 case '0': SectorsCnt
= 05; break;
724 case '1': SectorsCnt
= 16; break;
725 case '2': SectorsCnt
= 32; break;
726 case '4': SectorsCnt
= 40; break;
727 default: SectorsCnt
= 16;
731 ctmp
= param_getchar(Cmd
, 4);
732 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
733 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
735 ctmp
= param_getchar(Cmd
, 6);
736 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
737 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
740 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
742 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
743 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
744 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
745 case -4 : PrintAndLog("No valid key found"); break;
747 key64
= bytes_to_num(keyBlock
, 6);
749 // transfer key to the emulator
751 uint8_t sectortrailer
;
752 if (trgBlockNo
< 32*4) { // 4 block sector
753 sectortrailer
= (trgBlockNo
& 0x03) + 3;
754 } else { // 16 block sector
755 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
757 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
760 num_to_bytes(key64
, 6, keyBlock
);
762 num_to_bytes(key64
, 6, &keyBlock
[10]);
763 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
766 default : PrintAndLog("Unknown Error.\n");
770 else { // ------------------------------------ multiple sectors working
771 clock_t t1
= clock();
772 unsigned long elapsed_time
;
776 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
777 if (e_sector
== NULL
) return 1;
779 //test current key and additional standard keys first
780 memcpy(keyBlock
, key
, 6);
781 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
782 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
783 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
784 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
785 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
787 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
788 for (i
= 0; i
< SectorsCnt
; i
++) {
789 for (j
= 0; j
< 2; j
++) {
790 if (e_sector
[i
].foundKey
[j
]) continue;
792 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
795 e_sector
[i
].Key
[j
] = key64
;
796 e_sector
[i
].foundKey
[j
] = TRUE
;
800 clock_t t2
= clock() - t1
;
802 elapsed_time
= difftime(end
, start
);
804 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2
, elapsed_time
);
806 PrintAndLog("enter nested...");
810 bool calibrate
= true;
812 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
813 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; ++sectorNo
) {
814 for (trgKeyType
= 0; trgKeyType
< 2; ++trgKeyType
) {
816 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
818 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
820 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
821 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
822 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
823 case -4 : //key not found
830 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
831 e_sector
[sectorNo
].Key
[trgKeyType
] = bytes_to_num(keyBlock
, 6);
834 default : PrintAndLog("Unknown Error.\n");
844 elapsed_time
= difftime(end
, start
);
846 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
849 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
850 PrintAndLog("trying to read key B...");
851 for (i
= 0; i
< SectorsCnt
; i
++) {
852 // KEY A but not KEY B
853 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
855 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
857 PrintAndLog("Reading block %d", sectrail
);
859 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
860 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
861 clearCommandBuffer();
865 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
867 uint8_t isOK
= resp
.arg
[0] & 0xff;
870 uint8_t *data
= resp
.d
.asBytes
;
871 key64
= bytes_to_num(data
+10, 6);
873 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
874 e_sector
[i
].foundKey
[1] = TRUE
;
875 e_sector
[i
].Key
[1] = key64
;
882 printKeyTable( SectorsCnt
, e_sector
);
884 // transfer them to the emulator
886 for (i
= 0; i
< SectorsCnt
; i
++) {
887 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
888 if (e_sector
[i
].foundKey
[0])
889 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
890 if (e_sector
[i
].foundKey
[1])
891 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
892 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
897 if (createDumpFile
) {
898 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
899 PrintAndLog("Could not create file dumpkeys.bin");
903 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
904 for(i
=0; i
<SectorsCnt
; i
++) {
905 if (e_sector
[i
].foundKey
[0]){
906 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
907 fwrite ( tempkey
, 1, 6, fkeys
);
910 fwrite ( &standart
, 1, 6, fkeys
);
913 for(i
=0; i
<SectorsCnt
; i
++) {
914 if (e_sector
[i
].foundKey
[1]){
915 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
916 fwrite ( tempkey
, 1, 6, fkeys
);
919 fwrite ( &standart
, 1, 6, fkeys
);
930 int CmdHF14AMfNestedHard(const char *Cmd
) {
933 uint8_t trgBlockNo
= 0;
934 uint8_t trgKeyType
= 0;
935 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
936 uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0};
939 ctmp
= param_getchar(Cmd
, 0);
940 if (ctmp
!= 'H' && ctmp
!= 'h' ) return usage_hf14_hardnested();
941 if (ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen(Cmd
) < 20) return usage_hf14_hardnested();
943 bool know_target_key
= false;
944 bool nonce_file_read
= false;
945 bool nonce_file_write
= false;
949 if (ctmp
== 'R' || ctmp
== 'r') {
950 nonce_file_read
= true;
951 if (!param_gethex(Cmd
, 1, trgkey
, 12)) {
952 know_target_key
= true;
954 } else if (ctmp
== 'T' || ctmp
== 't') {
955 tests
= param_get32ex(Cmd
, 1, 100, 10);
957 blockNo
= param_get8(Cmd
, 0);
958 ctmp
= param_getchar(Cmd
, 1);
959 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
960 PrintAndLog("Key type must be A or B");
963 if (ctmp
!= 'A' && ctmp
!= 'a') {
967 if (param_gethex(Cmd
, 2, key
, 12)) {
968 PrintAndLog("Key must include 12 HEX symbols");
972 trgBlockNo
= param_get8(Cmd
, 3);
973 ctmp
= param_getchar(Cmd
, 4);
974 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
975 PrintAndLog("Target key type must be A or B");
978 if (ctmp
!= 'A' && ctmp
!= 'a') {
984 if (!param_gethex(Cmd
, 5, trgkey
, 12)) {
985 know_target_key
= true;
989 while ((ctmp
= param_getchar(Cmd
, i
))) {
990 if (ctmp
== 's' || ctmp
== 'S') {
992 } else if (ctmp
== 'w' || ctmp
== 'W') {
993 nonce_file_write
= true;
995 PrintAndLog("Possible options are w and/or s");
1002 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 ",
1005 trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5],
1006 know_target_key
?"":" (not set)",
1007 nonce_file_write
?"write":nonce_file_read
?"read":"none",
1011 int16_t isOK
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1015 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1016 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1025 int CmdHF14AMfChk(const char *Cmd
) {
1027 if (strlen(Cmd
)<3) return usage_hf14_chk();
1030 char filename
[FILE_PATH_SIZE
]={0};
1032 uint8_t *keyBlock
= NULL
, *p
;
1033 uint8_t stKeyBlock
= 20;
1035 sector
*e_sector
= NULL
;
1040 uint8_t blockNo
= 0;
1041 uint8_t SectorsCnt
= 1;
1042 uint8_t keyType
= 0;
1045 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1047 int transferToEml
= 0;
1048 int createDumpFile
= 0;
1050 keyBlock
= calloc(stKeyBlock
, 6);
1051 if (keyBlock
== NULL
) return 1;
1053 uint64_t defaultKeys
[] = {
1054 0xffffffffffff, // Default key (first key used by program if no user defined key)
1055 0x000000000000, // Blank key
1056 0xa0a1a2a3a4a5, // NFCForum MAD key
1068 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
1070 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
1071 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
1074 if (param_getchar(Cmd
, 0)=='*') {
1076 switch(param_getchar(Cmd
+1, 0)) {
1077 case '0': SectorsCnt
= 5; break;
1078 case '1': SectorsCnt
= 16; break;
1079 case '2': SectorsCnt
= 32; break;
1080 case '4': SectorsCnt
= 40; break;
1081 default: SectorsCnt
= 16;
1084 blockNo
= param_get8(Cmd
, 0);
1087 ctmp
= param_getchar(Cmd
, 1);
1099 PrintAndLog("Key type must be A , B or ?");
1104 ctmp
= param_getchar(Cmd
, 2);
1105 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
1106 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
1108 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
1109 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
1110 if ( stKeyBlock
- keycnt
< 2) {
1111 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1113 PrintAndLog("Cannot allocate memory for Keys");
1119 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1120 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1121 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1124 // May be a dic file
1125 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
1126 PrintAndLog("File name too long");
1131 if ( (f
= fopen( filename
, "r")) ) {
1132 while( fgets(buf
, sizeof(buf
), f
) ){
1133 if (strlen(buf
) < 12 || buf
[11] == '\n')
1136 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
1138 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
1140 if (!isxdigit(buf
[0])){
1141 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
1147 if ( stKeyBlock
- keycnt
< 2) {
1148 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
1150 PrintAndLog("Cannot allocate memory for defKeys");
1157 memset(keyBlock
+ 6 * keycnt
, 0, 6);
1158 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
1159 PrintAndLog("check key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
1161 memset(buf
, 0, sizeof(buf
));
1165 PrintAndLog("File: %s: not found or locked.", filename
);
1174 PrintAndLog("No key specified, trying default keys");
1175 for (;keycnt
< defaultKeysSize
; keycnt
++)
1176 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
1177 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
1178 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
1181 // initialize storage for found keys
1182 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
1183 if (e_sector
== NULL
) {
1189 for(int i
= 0; i
< SectorsCnt
; ++i
){
1190 e_sector
[i
].Key
[0] = 0xffffffffffff;
1191 e_sector
[i
].Key
[1] = 0xffffffffffff;
1192 e_sector
[i
].foundKey
[0] = FALSE
;
1193 e_sector
[i
].foundKey
[1] = FALSE
;
1197 uint8_t trgKeyType
= 0;
1198 uint32_t max_keys
= keycnt
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
;
1201 clock_t t1
= clock();
1206 for (trgKeyType
= !keyType
; trgKeyType
< 2; (keyType
==2) ? (++trgKeyType
) : (trgKeyType
=2) ) {
1209 for (int i
= 0; i
< SectorsCnt
; ++i
) {
1211 // skip already found keys.
1212 if (e_sector
[i
].foundKey
[trgKeyType
]) continue;
1214 for (uint32_t c
= 0; c
< keycnt
; c
+= max_keys
) {
1216 uint32_t size
= keycnt
-c
> max_keys
? max_keys
: keycnt
-c
;
1218 res
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
);
1220 e_sector
[i
].Key
[trgKeyType
] = key64
;
1221 e_sector
[i
].foundKey
[trgKeyType
] = TRUE
;
1227 b
< 127 ? ( b
+=4 ) : ( b
+= 16 );
1232 unsigned long elapsed_time
= difftime(end
, start
);
1234 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1
, elapsed_time
);
1237 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1238 if ( keyType
!= 1 ) {
1239 PrintAndLog("testing to read key B...");
1240 for (i
= 0; i
< SectorsCnt
; i
++) {
1241 // KEY A but not KEY B
1242 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) {
1244 uint8_t sectrail
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1246 PrintAndLog("Reading block %d", sectrail
);
1248 UsbCommand c
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}};
1249 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A
1250 clearCommandBuffer();
1254 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue;
1256 uint8_t isOK
= resp
.arg
[0] & 0xff;
1257 if (!isOK
) continue;
1259 uint8_t *data
= resp
.d
.asBytes
;
1260 key64
= bytes_to_num(data
+10, 6);
1262 PrintAndLog("Data:%s", sprint_hex(data
+10, 6));
1263 e_sector
[i
].foundKey
[1] = 1;
1264 e_sector
[i
].Key
[1] = key64
;
1272 printKeyTable( SectorsCnt
, e_sector
);
1274 if (transferToEml
) {
1275 uint8_t block
[16] = {0x00};
1276 for (uint8_t i
= 0; i
< SectorsCnt
; ++i
) {
1277 mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1278 if (e_sector
[i
].foundKey
[0])
1279 num_to_bytes(e_sector
[i
].Key
[0], 6, block
);
1280 if (e_sector
[i
].foundKey
[1])
1281 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10);
1282 mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
1284 PrintAndLog("Found keys have been transferred to the emulator memory");
1287 if (createDumpFile
) {
1288 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1289 if (fkeys
== NULL
) {
1290 PrintAndLog("Could not create file dumpkeys.bin");
1295 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1297 for( i
=0; i
<SectorsCnt
; i
++) {
1298 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
1299 fwrite ( tempkey
, 1, 6, fkeys
);
1301 for(i
=0; i
<SectorsCnt
; i
++) {
1302 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
1303 fwrite ( tempkey
, 1, 6, fkeys
);
1306 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1315 int CmdHF14AMf1kSim(const char *Cmd
) {
1316 uint8_t uid
[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1317 uint8_t exitAfterNReads
= 0;
1318 uint8_t flags
= (FLAG_UID_IN_EMUL
| FLAG_4B_UID_IN_DATA
);
1321 uint8_t cmdp
= param_getchar(Cmd
, 0);
1323 if (cmdp
== 'h' || cmdp
== 'H') return usage_hf14_mf1ksim();
1325 cmdp
= param_getchar(Cmd
, pnr
);
1326 if (cmdp
== 'u' || cmdp
== 'U') {
1327 param_gethex_ex(Cmd
, pnr
+1, uid
, &uidlen
);
1329 case 20: flags
= FLAG_10B_UID_IN_DATA
; break;
1330 case 14: flags
= FLAG_7B_UID_IN_DATA
; break;
1331 case 8: flags
= FLAG_4B_UID_IN_DATA
; break;
1332 default: return usage_hf14_mf1ksim();
1337 cmdp
= param_getchar(Cmd
, pnr
);
1338 if (cmdp
== 'n' || cmdp
== 'N') {
1339 exitAfterNReads
= param_get8(Cmd
, pnr
+1);
1343 cmdp
= param_getchar(Cmd
, pnr
);
1344 if (cmdp
== 'i' || cmdp
== 'I' ) {
1345 flags
|= FLAG_INTERACTIVE
;
1349 cmdp
= param_getchar(Cmd
, pnr
);
1350 if (cmdp
== 'x' || cmdp
== 'X') {
1351 flags
|= FLAG_NR_AR_ATTACK
;
1354 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1355 , (uidlen
== 0 ) ? "N/A" : sprint_hex(uid
, uidlen
>>1)
1360 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
, 0}};
1361 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1362 clearCommandBuffer();
1365 if(flags
& FLAG_INTERACTIVE
) {
1369 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1371 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue;
1373 if ( !(flags
& FLAG_NR_AR_ATTACK
) ) break;
1374 if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD
) break;
1376 memset(data
, 0x00, sizeof(data
));
1377 int len
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1];
1379 memcpy(data
, resp
.d
.asBytes
, len
);
1381 bool found
= tryMfk32(data
, &key
);
1382 found
^= tryMfk32_moebius(data
, &key
);
1389 int CmdHF14AMfSniff(const char *Cmd
){
1390 bool wantLogToFile
= FALSE
;
1391 bool wantDecrypt
= FALSE
;
1392 //bool wantSaveToEml = FALSE; TODO
1393 bool wantSaveToEmlFile
= FALSE
;
1403 uint8_t uid_len
= 0;
1404 uint8_t atqa
[2] = {0x00, 0x00};
1407 uint8_t *buf
= NULL
;
1408 uint16_t bufsize
= 0;
1409 uint8_t *bufPtr
= NULL
;
1410 uint16_t traceLen
= 0;
1412 memset(uid
, 0x00, sizeof(uid
));
1414 char ctmp
= param_getchar(Cmd
, 0);
1415 if ( ctmp
== 'h' || ctmp
== 'H' ) return usage_hf14_sniff();
1417 for (int i
= 0; i
< 4; i
++) {
1418 ctmp
= param_getchar(Cmd
, i
);
1419 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1420 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1421 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1422 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1425 printf("-------------------------------------------------------------------------\n");
1426 printf("Executing mifare sniffing command. \n");
1427 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1428 printf("Press the key on pc keyboard to abort the client.\n");
1429 printf("-------------------------------------------------------------------------\n");
1431 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1432 clearCommandBuffer();
1440 tmpchar
= getchar();
1442 printf("\naborted via keyboard!\n");
1447 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
1448 res
= resp
.arg
[0] & 0xff;
1449 traceLen
= resp
.arg
[1];
1458 if (res
== 1) { // there is (more) data to be transferred
1459 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1460 if (traceLen
> bufsize
) {
1462 if (buf
== NULL
) // not yet allocated
1463 p
= malloc(traceLen
);
1464 else // need more memory
1465 p
= realloc(buf
, traceLen
);
1468 PrintAndLog("Cannot allocate memory for trace");
1476 memset(buf
, 0x00, traceLen
);
1478 if (bufPtr
== NULL
) {
1479 PrintAndLog("Cannot allocate memory for trace");
1483 // what happens if LEN is bigger then TRACELEN --iceman
1484 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1489 if (res
== 2) { // received all data, start displaying
1490 blockLen
= bufPtr
- buf
;
1493 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1494 while (bufPtr
- buf
< blockLen
) {
1495 bufPtr
+= 6; // skip (void) timing information
1496 len
= *((uint16_t *)bufPtr
);
1504 if ((len
== 17) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[15] == 0xff) && (bufPtr
[16] == 0xff)) {
1505 memcpy(uid
, bufPtr
+ 2, 10);
1506 memcpy(atqa
, bufPtr
+ 2 + 10, 2);
1507 switch (atqa
[0] & 0xC0) {
1508 case 0x80: uid_len
= 10; break;
1509 case 0x40: uid_len
= 7; break;
1510 default: uid_len
= 4; break;
1513 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1514 sprint_hex(uid
, uid_len
),
1518 if (wantLogToFile
|| wantDecrypt
) {
1519 FillFileNameByUID(logHexFileName
, uid
, ".log", uid_len
);
1520 AddLogCurrentDT(logHexFileName
);
1523 mfTraceInit(uid
, uid_len
, atqa
, sak
, wantSaveToEmlFile
);
1525 PrintAndLog("%03d| %s |%s", num
, isTag
? "TAG" : "RDR", sprint_hex(bufPtr
, len
));
1527 AddLogHex(logHexFileName
, isTag
? "TAG| ":"RDR| ", bufPtr
, len
);
1529 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1533 bufPtr
+= ((len
-1)/8+1); // ignore parity
1544 int CmdHF14AMfDbg(const char *Cmd
) {
1546 char ctmp
= param_getchar(Cmd
, 0);
1547 if (strlen(Cmd
) < 1 || ctmp
== 'h'|| ctmp
== 'H') return usage_hf14_dbg();
1549 uint8_t dbgMode
= param_get8ex(Cmd
, 0, 0, 10);
1550 if (dbgMode
> 4) return usage_hf14_dbg();
1552 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1557 void printKeyTable( uint8_t sectorscnt
, sector
*e_sector
){
1558 PrintAndLog("|---|----------------|---|----------------|---|");
1559 PrintAndLog("|sec|key A |res|key B |res|");
1560 PrintAndLog("|---|----------------|---|----------------|---|");
1561 for (uint8_t i
= 0; i
< sectorscnt
; ++i
) {
1562 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
1563 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],
1564 e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]
1567 PrintAndLog("|---|----------------|---|----------------|---|");
1570 // EMULATOR COMMANDS
1572 int CmdHF14AMfEGet(const char *Cmd
)
1574 uint8_t blockNo
= 0;
1575 uint8_t data
[16] = {0x00};
1577 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1578 PrintAndLog("Usage: hf mf eget <block number>");
1579 PrintAndLog(" sample: hf mf eget 0 ");
1583 blockNo
= param_get8(Cmd
, 0);
1586 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1587 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1589 PrintAndLog("Command execute timeout");
1595 int CmdHF14AMfEClear(const char *Cmd
)
1597 if (param_getchar(Cmd
, 0) == 'h') {
1598 PrintAndLog("Usage: hf mf eclr");
1599 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1603 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1608 int CmdHF14AMfESet(const char *Cmd
)
1610 uint8_t memBlock
[16];
1611 uint8_t blockNo
= 0;
1613 memset(memBlock
, 0x00, sizeof(memBlock
));
1615 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1616 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1617 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1621 blockNo
= param_get8(Cmd
, 0);
1623 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1624 PrintAndLog("block data must include 32 HEX symbols");
1629 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1630 memcpy(c
.d
.asBytes
, memBlock
, 16);
1635 int CmdHF14AMfELoad(const char *Cmd
)
1638 char filename
[FILE_PATH_SIZE
];
1639 char *fnameptr
= filename
;
1640 char buf
[64] = {0x00};
1641 uint8_t buf8
[64] = {0x00};
1642 int i
, len
, blockNum
, numBlocks
;
1643 int nameParamNo
= 1;
1644 uint8_t blockWidth
= 32;
1645 char ctmp
= param_getchar(Cmd
, 0);
1647 if ( ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
1648 PrintAndLog("It loads emul dump from the file `filename.eml`");
1649 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1650 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1652 PrintAndLog(" sample: hf mf eload filename");
1653 PrintAndLog(" hf mf eload 4 filename");
1658 case '0' : numBlocks
= 5*4; break;
1660 case '\0': numBlocks
= 16*4; break;
1661 case '2' : numBlocks
= 32*4; break;
1662 case '4' : numBlocks
= 256; break;
1663 case 'U' : // fall through
1664 case 'u' : numBlocks
= 255; blockWidth
= 8; break;
1670 uint32_t numblk2
= param_get32ex(Cmd
,2,0,10);
1671 if (numblk2
> 0) numBlocks
= numblk2
;
1673 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1675 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1679 sprintf(fnameptr
, ".eml");
1682 f
= fopen(filename
, "r");
1684 PrintAndLog("File %s not found or locked", filename
);
1690 memset(buf
, 0, sizeof(buf
));
1692 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1694 if (blockNum
>= numBlocks
) break;
1696 PrintAndLog("File reading error.");
1701 if (strlen(buf
) < blockWidth
){
1702 if(strlen(buf
) && feof(f
))
1704 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
);
1709 for (i
= 0; i
< blockWidth
; i
+= 2) {
1710 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1712 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) {
1713 PrintAndLog("Cant set emul block: %3d", blockNum
);
1720 if (blockNum
>= numBlocks
) break;
1725 if ((blockNum
!= numBlocks
)) {
1726 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1729 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1733 int CmdHF14AMfESave(const char *Cmd
)
1736 char filename
[FILE_PATH_SIZE
];
1737 char * fnameptr
= filename
;
1739 int i
, j
, len
, numBlocks
;
1740 int nameParamNo
= 1;
1742 memset(filename
, 0, sizeof(filename
));
1743 memset(buf
, 0, sizeof(buf
));
1745 char ctmp
= param_getchar(Cmd
, 0);
1747 if ( ctmp
== 'h' || ctmp
== 'H') {
1748 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1749 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1750 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1752 PrintAndLog(" sample: hf mf esave ");
1753 PrintAndLog(" hf mf esave 4");
1754 PrintAndLog(" hf mf esave 4 filename");
1759 case '0' : numBlocks
= 5*4; break;
1761 case '\0': numBlocks
= 16*4; break;
1762 case '2' : numBlocks
= 32*4; break;
1763 case '4' : numBlocks
= 256; break;
1770 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1772 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
1774 // user supplied filename?
1776 // get filename (UID from memory)
1777 if (mfEmlGetMem(buf
, 0, 1)) {
1778 PrintAndLog("Can\'t get UID from block: %d", 0);
1779 len
= sprintf(fnameptr
, "dump");
1783 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1784 sprintf(fnameptr
, "%02X", buf
[j
]);
1790 // add file extension
1791 sprintf(fnameptr
, ".eml");
1794 f
= fopen(filename
, "w+");
1797 PrintAndLog("Can't open file %s ", filename
);
1802 for (i
= 0; i
< numBlocks
; i
++) {
1803 if (mfEmlGetMem(buf
, i
, 1)) {
1804 PrintAndLog("Cant get block: %d", i
);
1807 for (j
= 0; j
< 16; j
++)
1808 fprintf(f
, "%02X", buf
[j
]);
1813 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1818 int CmdHF14AMfECFill(const char *Cmd
)
1820 uint8_t keyType
= 0;
1821 uint8_t numSectors
= 16;
1823 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1824 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1825 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1827 PrintAndLog("samples: hf mf ecfill A");
1828 PrintAndLog(" hf mf ecfill A 4");
1829 PrintAndLog("Read card and transfer its data to emulator memory.");
1830 PrintAndLog("Keys must be laid in the emulator memory. \n");
1834 char ctmp
= param_getchar(Cmd
, 0);
1835 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1836 PrintAndLog("Key type must be A or B");
1839 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1841 ctmp
= param_getchar(Cmd
, 1);
1843 case '0' : numSectors
= 5; break;
1845 case '\0': numSectors
= 16; break;
1846 case '2' : numSectors
= 32; break;
1847 case '4' : numSectors
= 40; break;
1848 default: numSectors
= 16;
1851 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1852 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1857 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1862 uint64_t keyA
, keyB
;
1864 char cmdp
= param_getchar(Cmd
, 0);
1866 if ( cmdp
== 'h' || cmdp
== 'H' ) {
1867 PrintAndLog("It prints the keys loaded in the emulator memory");
1868 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1869 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1871 PrintAndLog(" sample: hf mf ekeyprn 1");
1876 case '0' : numSectors
= 5; break;
1878 case '\0': numSectors
= 16; break;
1879 case '2' : numSectors
= 32; break;
1880 case '4' : numSectors
= 40; break;
1881 default: numSectors
= 16;
1884 PrintAndLog("|---|----------------|----------------|");
1885 PrintAndLog("|sec|key A |key B |");
1886 PrintAndLog("|---|----------------|----------------|");
1887 for (i
= 0; i
< numSectors
; i
++) {
1888 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1889 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1892 keyA
= bytes_to_num(data
, 6);
1893 keyB
= bytes_to_num(data
+ 10, 6);
1894 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1896 PrintAndLog("|---|----------------|----------------|");
1901 // CHINESE MAGIC COMMANDS
1903 int CmdHF14AMfCSetUID(const char *Cmd
) {
1904 uint8_t wipeCard
= 0;
1905 uint8_t uid
[8] = {0x00};
1906 uint8_t oldUid
[8] = {0x00};
1907 uint8_t atqa
[2] = {0x00};
1908 uint8_t sak
[1] = {0x00};
1909 uint8_t atqaPresent
= 1;
1914 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1915 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1916 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1918 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1920 PrintAndLog("sample: hf mf csetuid 01020304");
1921 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
1925 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1926 PrintAndLog("UID must include 8 HEX symbols");
1931 ctmp
= param_getchar(Cmd
, argi
);
1932 if (ctmp
== 'w' || ctmp
== 'W') {
1938 if (param_getchar(Cmd
, argi
)) {
1939 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1940 PrintAndLog("ATQA must include 4 HEX symbols");
1944 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1945 PrintAndLog("SAK must include 2 HEX symbols");
1954 ctmp
= param_getchar(Cmd
, argi
);
1955 if (ctmp
== 'w' || ctmp
== 'W') {
1960 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1962 res
= mfCSetUID(uid
, (atqaPresent
) ? atqa
: NULL
, (atqaPresent
) ? sak
: NULL
, oldUid
, wipeCard
);
1964 PrintAndLog("Can't set UID. error=%d", res
);
1968 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1969 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1973 int CmdHF14AMfCSetBlk(const char *Cmd
) {
1974 uint8_t block
[16] = {0x00};
1975 uint8_t blockNo
= 0;
1976 uint8_t params
= MAGIC_SINGLE
;
1979 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1980 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1981 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1982 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1983 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1987 blockNo
= param_get8(Cmd
, 0);
1989 if (param_gethex(Cmd
, 1, block
, 32)) {
1990 PrintAndLog("block data must include 32 HEX symbols");
1994 char ctmp
= param_getchar(Cmd
, 2);
1995 if (ctmp
== 'w' || ctmp
== 'W')
1996 params
|= MAGIC_WIPE
;
1998 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16));
2000 res
= mfCSetBlock(blockNo
, block
, NULL
, params
);
2002 PrintAndLog("Can't write block. error=%d", res
);
2008 int CmdHF14AMfCLoad(const char *Cmd
) {
2010 char filename
[FILE_PATH_SIZE
];
2011 char * fnameptr
= filename
;
2012 char buf
[64] = {0x00};
2013 uint8_t buf8
[64] = {0x00};
2014 uint8_t fillFromEmulator
= 0;
2015 int i
, len
, blockNum
, flags
=0;
2017 memset(filename
, 0, sizeof(filename
));
2019 char ctmp
= param_getchar(Cmd
, 0);
2021 if (ctmp
== 'h' || ctmp
== 'H' || ctmp
== 0x00) {
2022 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2023 PrintAndLog("or from emulator memory (option `e`)");
2024 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2025 PrintAndLog(" or: hf mf cload e ");
2026 PrintAndLog(" sample: hf mf cload filename");
2030 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2032 if (fillFromEmulator
) {
2033 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
2034 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
2035 PrintAndLog("Cant get block: %d", blockNum
);
2038 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2039 if (blockNum
== 1) flags
= 0; // just write
2040 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Magic Halt and switch off field.
2042 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2043 PrintAndLog("Cant set magic card block: %d", blockNum
);
2050 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2052 memcpy(filename
, Cmd
, len
);
2055 sprintf(fnameptr
, ".eml");
2058 f
= fopen(filename
, "r");
2060 PrintAndLog("File not found or locked.");
2067 memset(buf
, 0, sizeof(buf
));
2069 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
2071 PrintAndLog("File reading error.");
2075 if (strlen(buf
) < 32) {
2076 if(strlen(buf
) && feof(f
))
2078 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2082 for (i
= 0; i
< 32; i
+= 2)
2083 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
2085 if (blockNum
== 0) flags
= MAGIC_INIT
+ MAGIC_WUPC
; // switch on field and send magic sequence
2086 if (blockNum
== 1) flags
= 0; // just write
2087 if (blockNum
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
; // Done. Switch off field.
2089 if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) {
2090 PrintAndLog("Can't set magic card block: %d", blockNum
);
2096 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
2101 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
2102 PrintAndLog("File content error. There must be 64 blocks");
2105 PrintAndLog("Loaded from file: %s", filename
);
2111 int CmdHF14AMfCGetBlk(const char *Cmd
) {
2113 uint8_t blockNo
= 0;
2115 memset(data
, 0x00, sizeof(data
));
2116 char ctmp
= param_getchar(Cmd
, 0);
2118 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2119 PrintAndLog("Usage: hf mf cgetblk <block number>");
2120 PrintAndLog("sample: hf mf cgetblk 1");
2121 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2125 blockNo
= param_get8(Cmd
, 0);
2127 PrintAndLog("--block number:%2d ", blockNo
);
2129 res
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
);
2131 PrintAndLog("Can't read block. error=%d", res
);
2135 PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
)));
2139 int CmdHF14AMfCGetSc(const char *Cmd
) {
2141 uint8_t sectorNo
= 0;
2143 memset(data
, 0x00, sizeof(data
));
2144 char ctmp
= param_getchar(Cmd
, 0);
2146 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') {
2147 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2148 PrintAndLog("sample: hf mf cgetsc 0");
2149 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2153 sectorNo
= param_get8(Cmd
, 0);
2154 if (sectorNo
> 15) {
2155 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2159 PrintAndLog("--sector number:%d ", sectorNo
);
2160 PrintAndLog("block | data");
2162 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2163 for (i
= 0; i
< 4; i
++) {
2164 if (i
== 1) flags
= 0;
2165 if (i
== 3) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2167 res
= mfCGetBlock(sectorNo
* 4 + i
, data
, flags
);
2169 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
2172 PrintAndLog(" %3d | %s", sectorNo
* 4 + i
, sprint_hex(data
, sizeof(data
)));
2177 int CmdHF14AMfCSave(const char *Cmd
) {
2180 char filename
[FILE_PATH_SIZE
];
2181 char * fnameptr
= filename
;
2182 uint8_t fillFromEmulator
= 0;
2184 int i
, j
, len
, flags
;
2186 memset(filename
, 0, sizeof(filename
));
2187 memset(buf
, 0, sizeof(buf
));
2188 char ctmp
= param_getchar(Cmd
, 0);
2190 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2191 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2192 PrintAndLog("or into emulator memory (option `e`)");
2193 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2194 PrintAndLog(" sample: hf mf esave ");
2195 PrintAndLog(" hf mf esave filename");
2196 PrintAndLog(" hf mf esave e \n");
2199 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
2201 if (fillFromEmulator
) {
2202 // put into emulator
2203 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2204 for (i
= 0; i
< 16 * 4; i
++) {
2205 if (i
== 1) flags
= 0;
2206 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2208 if (mfCGetBlock(i
, buf
, flags
)) {
2209 PrintAndLog("Cant get block: %d", i
);
2213 if (mfEmlSetMem(buf
, i
, 1)) {
2214 PrintAndLog("Cant set emul block: %d", i
);
2221 if (len
> FILE_PATH_SIZE
- 5) len
= FILE_PATH_SIZE
- 5;
2223 // get filename based on UID
2226 if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) {
2227 PrintAndLog("Cant get block: %d", 0);
2228 len
= sprintf(fnameptr
, "dump");
2231 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
2232 sprintf(fnameptr
, "%02x", buf
[j
]);
2235 memcpy(filename
, Cmd
, len
);
2239 // add .eml extension
2240 sprintf(fnameptr
, ".eml");
2243 f
= fopen(filename
, "w+");
2246 PrintAndLog("File not found or locked.");
2251 flags
= MAGIC_INIT
+ MAGIC_WUPC
;
2252 for (i
= 0; i
< 16 * 4; i
++) {
2253 if (i
== 1) flags
= 0;
2254 if (i
== 16 * 4 - 1) flags
= MAGIC_HALT
+ MAGIC_OFF
;
2256 if (mfCGetBlock(i
, buf
, flags
)) {
2257 PrintAndLog("Cant get block: %d", i
);
2260 for (j
= 0; j
< 16; j
++)
2261 fprintf(f
, "%02x", buf
[j
]);
2266 PrintAndLog("Saved to file: %s", filename
);
2271 //needs nt, ar, at, Data to decrypt
2272 int CmdHf14MfDecryptBytes(const char *Cmd
){
2274 uint32_t nt
= param_get32ex(Cmd
,0,0,16);
2275 uint32_t ar_enc
= param_get32ex(Cmd
,1,0,16);
2276 uint32_t at_enc
= param_get32ex(Cmd
,2,0,16);
2279 param_gethex_ex(Cmd
, 3, data
, &len
);
2282 int limit
= sizeof(data
) / 2;
2287 return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
);
2290 static command_t CommandTable
[] = {
2291 {"help", CmdHelp
, 1, "This help"},
2292 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
2293 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
2294 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
2295 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
2296 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
2297 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
2298 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
2299 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
2300 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
2301 {"hardnested", CmdHF14AMfNestedHard
, 0, "Nested attack for hardened Mifare cards"},
2302 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
2303 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
2304 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
2305 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
2306 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
2307 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
2308 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
2309 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
2310 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
2311 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
2312 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
2313 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
2314 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
2315 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
2316 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
2317 {"decrypt", CmdHf14MfDecryptBytes
, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2318 {NULL
, NULL
, 0, NULL
}
2321 int CmdHFMF(const char *Cmd
) {
2322 clearCommandBuffer();
2323 CmdsParse(CommandTable
, Cmd
);
2327 int CmdHelp(const char *Cmd
) {
2328 CmdsHelp(CommandTable
);