]>
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
);
15 int CmdHF14AMifare(const char *Cmd
)
18 uint32_t nt
= 0, nr
= 0;
19 uint64_t par_list
= 0, ks_list
= 0, r_key
= 0;
22 UsbCommand c
= {CMD_READER_MIFARE
, {true, 0, 0}};
25 printf("-------------------------------------------------------------------------\n");
26 printf("Executing command. Expected execution time: 25sec on average :-)\n");
27 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
28 printf("-------------------------------------------------------------------------\n");
36 while (ukbhit()) getchar();
44 printf("\naborted via keyboard!\n");
49 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)) {
51 uid
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 0, 4);
52 nt
= (uint32_t)bytes_to_num(resp
.d
.asBytes
+ 4, 4);
53 par_list
= bytes_to_num(resp
.d
.asBytes
+ 8, 8);
54 ks_list
= bytes_to_num(resp
.d
.asBytes
+ 16, 8);
55 nr
= bytes_to_num(resp
.d
.asBytes
+ 24, 4);
58 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
59 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
60 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
70 if (isOK
!= 1) return 1;
72 // execute original function from util nonce2key
73 if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) {
75 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);
76 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
81 printf("------------------------------------------------------------------\n");
82 PrintAndLog("Found valid key:%012"llx
" \n", r_key
);
89 int CmdHF14AMfWrBl(const char *Cmd
)
93 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
94 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
99 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
100 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
104 blockNo
= param_get8(Cmd
, 0);
105 cmdp
= param_getchar(Cmd
, 1);
107 PrintAndLog("Key type must be A or B");
110 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
111 if (param_gethex(Cmd
, 2, key
, 12)) {
112 PrintAndLog("Key must include 12 HEX symbols");
115 if (param_gethex(Cmd
, 3, bldata
, 32)) {
116 PrintAndLog("Block data must include 32 HEX symbols");
119 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
120 PrintAndLog("--data: %s", sprint_hex(bldata
, 16));
122 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}};
123 memcpy(c
.d
.asBytes
, key
, 6);
124 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
128 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
129 uint8_t isOK
= resp
.arg
[0] & 0xff;
130 PrintAndLog("isOk:%02x", isOK
);
132 PrintAndLog("Command execute timeout");
138 int CmdHF14AMfRdBl(const char *Cmd
)
142 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
148 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
149 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
153 blockNo
= param_get8(Cmd
, 0);
154 cmdp
= param_getchar(Cmd
, 1);
156 PrintAndLog("Key type must be A or B");
159 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
160 if (param_gethex(Cmd
, 2, key
, 12)) {
161 PrintAndLog("Key must include 12 HEX symbols");
164 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6));
166 UsbCommand c
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}};
167 memcpy(c
.d
.asBytes
, key
, 6);
171 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
172 uint8_t isOK
= resp
.arg
[0] & 0xff;
173 uint8_t *data
= resp
.d
.asBytes
;
176 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16));
178 PrintAndLog("isOk:%02x", isOK
);
180 PrintAndLog("Command execute timeout");
186 int CmdHF14AMfRdSc(const char *Cmd
)
189 uint8_t sectorNo
= 0;
191 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
193 uint8_t *data
= NULL
;
197 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
198 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
202 sectorNo
= param_get8(Cmd
, 0);
204 PrintAndLog("Sector number must be less than 40");
207 cmdp
= param_getchar(Cmd
, 1);
208 if (cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B') {
209 PrintAndLog("Key type must be A or B");
212 if (cmdp
!= 'A' && cmdp
!= 'a') keyType
= 1;
213 if (param_gethex(Cmd
, 2, key
, 12)) {
214 PrintAndLog("Key must include 12 HEX symbols");
217 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6));
219 UsbCommand c
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}};
220 memcpy(c
.d
.asBytes
, key
, 6);
225 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
226 isOK
= resp
.arg
[0] & 0xff;
227 data
= resp
.d
.asBytes
;
229 PrintAndLog("isOk:%02x", isOK
);
231 for (i
= 0; i
< (sectorNo
<32?3:15); i
++) {
232 PrintAndLog("data : %s", sprint_hex(data
+ i
* 16, 16));
234 PrintAndLog("trailer: %s", sprint_hex(data
+ (sectorNo
<32?3:15) * 16, 16));
237 PrintAndLog("Command execute timeout");
243 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
248 return 32 * 4 + (sectorNo
- 32) * 16;
252 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
261 int CmdHF14AMfDump(const char *Cmd
)
263 uint8_t sectorNo
, blockNo
;
267 uint8_t rights
[40][4];
268 uint8_t carddata
[256][16];
269 uint8_t numSectors
= 16;
276 char cmdp
= param_getchar(Cmd
, 0);
278 case '0' : numSectors
= 5; break;
280 case '\0': numSectors
= 16; break;
281 case '2' : numSectors
= 32; break;
282 case '4' : numSectors
= 40; break;
283 default: numSectors
= 16;
286 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
287 PrintAndLog("Usage: hf mf dump [card memory]");
288 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
290 PrintAndLog("Samples: hf mf dump");
291 PrintAndLog(" hf mf dump 4");
295 if ((fin
= fopen("dumpkeys.bin","rb")) == NULL
) {
296 PrintAndLog("Could not find file dumpkeys.bin");
300 // Read keys A from file
301 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
302 if (fread( keyA
[sectorNo
], 1, 6, fin
) == 0) {
303 PrintAndLog("File reading error.");
309 // Read keys B from file
310 for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) {
311 if (fread( keyB
[sectorNo
], 1, 6, fin
) == 0) {
312 PrintAndLog("File reading error.");
320 PrintAndLog("|-----------------------------------------|");
321 PrintAndLog("|------ Reading sector access bits...-----|");
322 PrintAndLog("|-----------------------------------------|");
324 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
325 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}};
326 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
329 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
330 uint8_t isOK
= resp
.arg
[0] & 0xff;
331 uint8_t *data
= resp
.d
.asBytes
;
333 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0
334 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1
335 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2
336 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer
338 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
);
339 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
340 rights
[sectorNo
][3] = 0x01;
343 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
);
344 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00;
345 rights
[sectorNo
][3] = 0x01;
349 PrintAndLog("|-----------------------------------------|");
350 PrintAndLog("|----- Dumping all blocks to file... -----|");
351 PrintAndLog("|-----------------------------------------|");
354 for (sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
355 for (blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
356 bool received
= false;
358 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
359 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
360 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
362 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
363 } else { // data block. Check if it can be read with key A or key B
364 uint8_t data_area
= sectorNo
<32?blockNo
:blockNo
/5;
365 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) { // only key B would work
366 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}};
367 memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6);
369 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
370 } else if (rights
[sectorNo
][data_area
] == 0x07) { // no key would work
372 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
);
373 } else { // key A would work
374 UsbCommand c
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}};
375 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6);
377 received
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500);
382 isOK
= resp
.arg
[0] & 0xff;
383 uint8_t *data
= resp
.d
.asBytes
;
384 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer. Fill in the keys.
385 data
[0] = (keyA
[sectorNo
][0]);
386 data
[1] = (keyA
[sectorNo
][1]);
387 data
[2] = (keyA
[sectorNo
][2]);
388 data
[3] = (keyA
[sectorNo
][3]);
389 data
[4] = (keyA
[sectorNo
][4]);
390 data
[5] = (keyA
[sectorNo
][5]);
391 data
[10] = (keyB
[sectorNo
][0]);
392 data
[11] = (keyB
[sectorNo
][1]);
393 data
[12] = (keyB
[sectorNo
][2]);
394 data
[13] = (keyB
[sectorNo
][3]);
395 data
[14] = (keyB
[sectorNo
][4]);
396 data
[15] = (keyB
[sectorNo
][5]);
399 memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16);
400 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
);
402 PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
);
408 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
);
415 if ((fout
= fopen("dumpdata.bin","wb")) == NULL
) {
416 PrintAndLog("Could not create file name dumpdata.bin");
419 uint16_t numblocks
= FirstBlockOfSector(numSectors
- 1) + NumBlocksPerSector(numSectors
- 1);
420 fwrite(carddata
, 1, 16*numblocks
, fout
);
422 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
);
428 int CmdHF14AMfRestore(const char *Cmd
)
430 uint8_t sectorNo
,blockNo
;
432 uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
433 uint8_t bldata
[16] = {0x00};
441 char cmdp
= param_getchar(Cmd
, 0);
443 case '0' : numSectors
= 5; break;
445 case '\0': numSectors
= 16; break;
446 case '2' : numSectors
= 32; break;
447 case '4' : numSectors
= 40; break;
448 default: numSectors
= 16;
451 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
452 PrintAndLog("Usage: hf mf restore [card memory]");
453 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
455 PrintAndLog("Samples: hf mf restore");
456 PrintAndLog(" hf mf restore 4");
460 if ((fkeys
= fopen("dumpkeys.bin","rb")) == NULL
) {
461 PrintAndLog("Could not find file dumpkeys.bin");
465 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
466 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) {
467 PrintAndLog("File reading error (dumpkeys.bin).");
474 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
475 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) {
476 PrintAndLog("File reading error (dumpkeys.bin).");
484 if ((fdump
= fopen("dumpdata.bin","rb")) == NULL
) {
485 PrintAndLog("Could not find file dumpdata.bin");
488 PrintAndLog("Restoring dumpdata.bin to card");
490 for (sectorNo
= 0; sectorNo
< numSectors
; sectorNo
++) {
491 for(blockNo
= 0; blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
492 UsbCommand c
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}};
493 memcpy(c
.d
.asBytes
, key
, 6);
495 if (fread(bldata
, 1, 16, fdump
) == 0) {
496 PrintAndLog("File reading error (dumpdata.bin).");
501 if (blockNo
== NumBlocksPerSector(sectorNo
) - 1) { // sector trailer
502 bldata
[0] = (keyA
[sectorNo
][0]);
503 bldata
[1] = (keyA
[sectorNo
][1]);
504 bldata
[2] = (keyA
[sectorNo
][2]);
505 bldata
[3] = (keyA
[sectorNo
][3]);
506 bldata
[4] = (keyA
[sectorNo
][4]);
507 bldata
[5] = (keyA
[sectorNo
][5]);
508 bldata
[10] = (keyB
[sectorNo
][0]);
509 bldata
[11] = (keyB
[sectorNo
][1]);
510 bldata
[12] = (keyB
[sectorNo
][2]);
511 bldata
[13] = (keyB
[sectorNo
][3]);
512 bldata
[14] = (keyB
[sectorNo
][4]);
513 bldata
[15] = (keyB
[sectorNo
][5]);
516 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16));
518 memcpy(c
.d
.asBytes
+ 10, bldata
, 16);
522 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
523 uint8_t isOK
= resp
.arg
[0] & 0xff;
524 PrintAndLog("isOk:%02x", isOK
);
526 PrintAndLog("Command execute timeout");
535 int CmdHF14AMfNested(const char *Cmd
)
537 int i
, j
, res
, iterations
;
538 sector
*e_sector
= NULL
;
541 uint8_t trgBlockNo
= 0;
542 uint8_t trgKeyType
= 0;
543 uint8_t SectorsCnt
= 0;
544 uint8_t key
[6] = {0, 0, 0, 0, 0, 0};
545 uint8_t keyBlock
[14*6];
547 bool transferToEml
= false;
549 bool createDumpFile
= false;
551 uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
552 uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
557 PrintAndLog("Usage:");
558 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
559 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
560 PrintAndLog(" <target block number> <target key A/B> [t]");
561 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
562 PrintAndLog("t - transfer keys into emulator memory");
563 PrintAndLog("d - write keys to binary file");
565 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
566 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
567 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
568 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
572 cmdp
= param_getchar(Cmd
, 0);
573 blockNo
= param_get8(Cmd
, 1);
574 ctmp
= param_getchar(Cmd
, 2);
576 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
577 PrintAndLog("Key type must be A or B");
581 if (ctmp
!= 'A' && ctmp
!= 'a')
584 if (param_gethex(Cmd
, 3, key
, 12)) {
585 PrintAndLog("Key must include 12 HEX symbols");
589 if (cmdp
== 'o' || cmdp
== 'O') {
591 trgBlockNo
= param_get8(Cmd
, 4);
592 ctmp
= param_getchar(Cmd
, 5);
593 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
594 PrintAndLog("Target key type must be A or B");
597 if (ctmp
!= 'A' && ctmp
!= 'a')
602 case '0': SectorsCnt
= 05; break;
603 case '1': SectorsCnt
= 16; break;
604 case '2': SectorsCnt
= 32; break;
605 case '4': SectorsCnt
= 40; break;
606 default: SectorsCnt
= 16;
610 ctmp
= param_getchar(Cmd
, 4);
611 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= true;
612 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= true;
614 ctmp
= param_getchar(Cmd
, 6);
615 transferToEml
|= (ctmp
== 't' || ctmp
== 'T');
616 transferToEml
|= (ctmp
== 'd' || ctmp
== 'D');
619 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A');
620 int16_t isOK
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true);
623 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
624 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
625 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
626 default : PrintAndLog("Unknown Error.\n");
630 key64
= bytes_to_num(keyBlock
, 6);
632 PrintAndLog("Found valid key:%012"llx
, key64
);
634 // transfer key to the emulator
636 uint8_t sectortrailer
;
637 if (trgBlockNo
< 32*4) { // 4 block sector
638 sectortrailer
= (trgBlockNo
& 0x03) + 3;
639 } else { // 16 block sector
640 sectortrailer
= (trgBlockNo
& 0x0f) + 15;
642 mfEmlGetMem(keyBlock
, sectortrailer
, 1);
645 num_to_bytes(key64
, 6, keyBlock
);
647 num_to_bytes(key64
, 6, &keyBlock
[10]);
648 mfEmlSetMem(keyBlock
, sectortrailer
, 1);
651 PrintAndLog("No valid key found");
654 else { // ------------------------------------ multiple sectors working
658 e_sector
= calloc(SectorsCnt
, sizeof(sector
));
659 if (e_sector
== NULL
) return 1;
661 //test current key and additional standard keys first
662 memcpy(keyBlock
, key
, 6);
663 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock
+ 1 * 6));
664 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock
+ 2 * 6));
665 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock
+ 3 * 6));
666 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock
+ 4 * 6));
667 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock
+ 5 * 6));
668 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock
+ 6 * 6));
669 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock
+ 7 * 6));
670 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock
+ 8 * 6));
671 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock
+ 9 * 6));
672 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock
+ 10 * 6));
673 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock
+ 11 * 6));
674 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock
+ 12 * 6));
675 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock
+ 13 * 6));
677 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
);
678 for (i
= 0; i
< SectorsCnt
; i
++) {
679 for (j
= 0; j
< 2; j
++) {
680 if (e_sector
[i
].foundKey
[j
]) continue;
682 res
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
);
685 e_sector
[i
].Key
[j
] = key64
;
686 e_sector
[i
].foundKey
[j
] = 1;
693 PrintAndLog("nested...");
694 bool calibrate
= true;
695 for (i
= 0; i
< NESTED_SECTOR_RETRY
; i
++) {
696 for (uint8_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
697 for (trgKeyType
= 0; trgKeyType
< 2; trgKeyType
++) {
698 if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue;
699 PrintAndLog("-----------------------------------------------");
700 int16_t isOK
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
);
703 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
704 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
705 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
706 default : PrintAndLog("Unknown Error.\n");
716 key64
= bytes_to_num(keyBlock
, 6);
718 PrintAndLog("Found valid key:%012"llx
, key64
);
719 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1;
720 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
;
726 printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1
)/CLOCKS_PER_SEC
, ((float)clock() - time1
)/iterations
/CLOCKS_PER_SEC
);
728 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
);
730 PrintAndLog("|---|----------------|---|----------------|---|");
731 PrintAndLog("|sec|key A |res|key B |res|");
732 PrintAndLog("|---|----------------|---|----------------|---|");
733 for (i
= 0; i
< SectorsCnt
; i
++) {
734 PrintAndLog("|%03d| %012"llx
" | %d | %012"llx
" | %d |", i
,
735 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]);
737 PrintAndLog("|---|----------------|---|----------------|---|");
739 // transfer them to the emulator
741 for (i
= 0; i
< SectorsCnt
; i
++) {
742 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
743 if (e_sector
[i
].foundKey
[0])
744 num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
);
745 if (e_sector
[i
].foundKey
[1])
746 num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]);
747 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1);
752 if (createDumpFile
) {
753 if ((fkeys
= fopen("dumpkeys.bin","wb")) == NULL
) {
754 PrintAndLog("Could not create file dumpkeys.bin");
758 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
759 for(i
=0; i
<SectorsCnt
; i
++) {
760 if (e_sector
[i
].foundKey
[0]){
761 num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
);
762 fwrite ( tempkey
, 1, 6, fkeys
);
765 fwrite ( &standart
, 1, 6, fkeys
);
768 for(i
=0; i
<SectorsCnt
; i
++) {
769 if (e_sector
[i
].foundKey
[1]){
770 num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
);
771 fwrite ( tempkey
, 1, 6, fkeys
);
774 fwrite ( &standart
, 1, 6, fkeys
);
785 int CmdHF14AMfChk(const char *Cmd
)
788 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
789 PrintAndLog(" * - all sectors");
790 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
791 PrintAndLog("d - write keys to binary file\n");
792 PrintAndLog("t - write keys to emulator memory");
793 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
794 PrintAndLog(" hf mf chk *1 ? t");
795 PrintAndLog(" hf mf chk *1 ? d");
800 char filename
[FILE_PATH_SIZE
]={0};
802 uint8_t *keyBlock
= NULL
, *p
;
803 uint8_t stKeyBlock
= 20;
809 uint8_t SectorsCnt
= 1;
813 int transferToEml
= 0;
814 int createDumpFile
= 0;
816 keyBlock
= calloc(stKeyBlock
, 6);
817 if (keyBlock
== NULL
) return 1;
819 uint64_t defaultKeys
[] =
821 0xffffffffffff, // Default key (first key used by program if no user defined key)
822 0x000000000000, // Blank key
823 0xa0a1a2a3a4a5, // NFCForum MAD key
835 int defaultKeysSize
= sizeof(defaultKeys
) / sizeof(uint64_t);
837 for (int defaultKeyCounter
= 0; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
839 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock
+ defaultKeyCounter
* 6));
842 if (param_getchar(Cmd
, 0)=='*') {
844 switch(param_getchar(Cmd
+1, 0)) {
845 case '0': SectorsCnt
= 5; break;
846 case '1': SectorsCnt
= 16; break;
847 case '2': SectorsCnt
= 32; break;
848 case '4': SectorsCnt
= 40; break;
849 default: SectorsCnt
= 16;
853 blockNo
= param_get8(Cmd
, 0);
855 ctmp
= param_getchar(Cmd
, 1);
867 PrintAndLog("Key type must be A , B or ?");
871 ctmp
= param_getchar(Cmd
, 2);
872 if (ctmp
== 't' || ctmp
== 'T') transferToEml
= 1;
873 else if (ctmp
== 'd' || ctmp
== 'D') createDumpFile
= 1;
875 for (i
= transferToEml
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) {
876 if (!param_gethex(Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12)) {
877 if ( stKeyBlock
- keycnt
< 2) {
878 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
880 PrintAndLog("Cannot allocate memory for Keys");
886 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
887 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
888 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
892 if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE
) {
893 PrintAndLog("File name too long");
898 if ( (f
= fopen( filename
, "r")) ) {
899 while( fgets(buf
, sizeof(buf
), f
) ){
900 if (strlen(buf
) < 12 || buf
[11] == '\n')
903 while (fgetc(f
) != '\n' && !feof(f
)) ; //goto next line
905 if( buf
[0]=='#' ) continue; //The line start with # is comment, skip
907 if (!isxdigit(buf
[0])){
908 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
);
914 if ( stKeyBlock
- keycnt
< 2) {
915 p
= realloc(keyBlock
, 6*(stKeyBlock
+=10));
917 PrintAndLog("Cannot allocate memory for defKeys");
923 memset(keyBlock
+ 6 * keycnt
, 0, 6);
924 num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock
+ 6*keycnt
);
925 PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock
+ 6*keycnt
, 6));
927 memset(buf
, 0, sizeof(buf
));
931 PrintAndLog("File: %s: not found or locked.", filename
);
940 PrintAndLog("No key specified, trying default keys");
941 for (;keycnt
< defaultKeysSize
; keycnt
++)
942 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
,
943 (keyBlock
+ 6*keycnt
)[0],(keyBlock
+ 6*keycnt
)[1], (keyBlock
+ 6*keycnt
)[2],
944 (keyBlock
+ 6*keycnt
)[3], (keyBlock
+ 6*keycnt
)[4], (keyBlock
+ 6*keycnt
)[5], 6);
947 // initialize storage for found keys
948 bool validKey
[2][40];
949 uint8_t foundKey
[2][40][6];
950 for (uint16_t t
= 0; t
< 2; t
++) {
951 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
952 validKey
[t
][sectorNo
] = false;
953 for (uint16_t i
= 0; i
< 6; i
++) {
954 foundKey
[t
][sectorNo
][i
] = 0xff;
959 for ( int t
= !keyType
; t
< 2; keyType
==2?(t
++):(t
=2) ) {
961 for (int i
= 0; i
< SectorsCnt
; ++i
) {
962 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
);
963 uint32_t max_keys
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
;
964 for (uint32_t c
= 0; c
< keycnt
; c
+=max_keys
) {
965 uint32_t size
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
;
966 res
= mfCheckKeys(b
, t
, true, size
, &keyBlock
[6*c
], &key64
);
969 PrintAndLog("Found valid key:[%012"llx
"]",key64
);
970 num_to_bytes(key64
, 6, foundKey
[t
][i
]);
971 validKey
[t
][i
] = true;
974 PrintAndLog("Command execute timeout");
977 b
<127?(b
+=4):(b
+=16);
983 for (uint16_t sectorNo
= 0; sectorNo
< SectorsCnt
; sectorNo
++) {
984 if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) {
985 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
986 for (uint16_t t
= 0; t
< 2; t
++) {
987 if (validKey
[t
][sectorNo
]) {
988 memcpy(block
+ t
*10, foundKey
[t
][sectorNo
], 6);
991 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1);
994 PrintAndLog("Found keys have been transferred to the emulator memory");
997 if (createDumpFile
) {
998 FILE *fkeys
= fopen("dumpkeys.bin","wb");
1000 PrintAndLog("Could not create file dumpkeys.bin");
1004 for (uint16_t t
= 0; t
< 2; t
++) {
1005 fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
);
1008 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1016 int CmdHF14AMf1kSim(const char *Cmd
)
1018 uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0};
1019 uint8_t exitAfterNReads
= 0;
1022 uint8_t cmdp
= param_getchar(Cmd
, 0);
1024 if (cmdp
== 'h' || cmdp
== 'H') {
1025 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1026 PrintAndLog(" h this help");
1027 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1028 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1029 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1030 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1032 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1036 if (param_getchar(Cmd
, pnr
) == 'u') {
1037 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0)
1039 flags
|= FLAG_4B_UID_IN_DATA
; // UID from packet
1040 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) {
1041 flags
|= FLAG_7B_UID_IN_DATA
;// UID from packet
1043 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1048 if (param_getchar(Cmd
, pnr
) == 'n') {
1049 exitAfterNReads
= param_get8(Cmd
,pnr
+1);
1052 if (param_getchar(Cmd
, pnr
) == 'i' ) {
1053 //Using a flag to signal interactiveness, least significant bit
1054 flags
|= FLAG_INTERACTIVE
;
1058 if (param_getchar(Cmd
, pnr
) == 'x' ) {
1059 //Using a flag to signal interactiveness, least significant bit
1060 flags
|= FLAG_NR_AR_ATTACK
;
1062 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1063 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex(uid
,4):
1064 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex(uid
,7): "N/A"
1065 , exitAfterNReads
, flags
,flags
);
1068 UsbCommand c
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}};
1069 memcpy(c
.d
.asBytes
, uid
, sizeof(uid
));
1072 if(flags
& FLAG_INTERACTIVE
)
1075 PrintAndLog("Press pm3-button to abort simulation");
1076 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1077 //We're waiting only 1.5 s at a time, otherwise we get the
1078 // annoying message about "Waiting for a response... "
1085 int CmdHF14AMfDbg(const char *Cmd
)
1087 int dbgMode
= param_get32ex(Cmd
, 0, 0, 10);
1089 PrintAndLog("Max debug mode parameter is 4 \n");
1092 if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode
> 4) {
1093 PrintAndLog("Usage: hf mf dbg <debug level>");
1094 PrintAndLog(" 0 - no debug messages");
1095 PrintAndLog(" 1 - error messages");
1096 PrintAndLog(" 2 - plus information messages");
1097 PrintAndLog(" 3 - plus debug messages");
1098 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1099 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1103 UsbCommand c
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}};
1109 int CmdHF14AMfEGet(const char *Cmd
)
1111 uint8_t blockNo
= 0;
1112 uint8_t data
[16] = {0x00};
1114 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1115 PrintAndLog("Usage: hf mf eget <block number>");
1116 PrintAndLog(" sample: hf mf eget 0 ");
1120 blockNo
= param_get8(Cmd
, 0);
1123 if (!mfEmlGetMem(data
, blockNo
, 1)) {
1124 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16));
1126 PrintAndLog("Command execute timeout");
1132 int CmdHF14AMfEClear(const char *Cmd
)
1134 if (param_getchar(Cmd
, 0) == 'h') {
1135 PrintAndLog("Usage: hf mf eclr");
1136 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1140 UsbCommand c
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}};
1146 int CmdHF14AMfESet(const char *Cmd
)
1148 uint8_t memBlock
[16];
1149 uint8_t blockNo
= 0;
1151 memset(memBlock
, 0x00, sizeof(memBlock
));
1153 if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') {
1154 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1155 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1159 blockNo
= param_get8(Cmd
, 0);
1161 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1162 PrintAndLog("block data must include 32 HEX symbols");
1167 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}};
1168 memcpy(c
.d
.asBytes
, memBlock
, 16);
1174 int CmdHF14AMfELoad(const char *Cmd
)
1177 char filename
[FILE_PATH_SIZE
];
1178 char *fnameptr
= filename
;
1179 char buf
[64] = {0x00};
1180 uint8_t buf8
[64] = {0x00};
1181 int i
, len
, blockNum
, numBlocks
;
1182 int nameParamNo
= 1;
1184 char ctmp
= param_getchar(Cmd
, 0);
1186 if ( ctmp
== 'h' || ctmp
== 0x00) {
1187 PrintAndLog("It loads emul dump from the file `filename.eml`");
1188 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1189 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1191 PrintAndLog(" sample: hf mf eload filename");
1192 PrintAndLog(" hf mf eload 4 filename");
1197 case '0' : numBlocks
= 5*4; break;
1199 case '\0': numBlocks
= 16*4; break;
1200 case '2' : numBlocks
= 32*4; break;
1201 case '4' : numBlocks
= 256; break;
1208 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1210 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1214 sprintf(fnameptr
, ".eml");
1217 f
= fopen(filename
, "r");
1219 PrintAndLog("File %s not found or locked", filename
);
1225 memset(buf
, 0, sizeof(buf
));
1227 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1229 if (blockNum
>= numBlocks
) break;
1231 PrintAndLog("File reading error.");
1236 if (strlen(buf
) < 32){
1237 if(strlen(buf
) && feof(f
))
1239 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1244 for (i
= 0; i
< 32; i
+= 2) {
1245 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1248 if (mfEmlSetMem(buf8
, blockNum
, 1)) {
1249 PrintAndLog("Cant set emul block: %3d", blockNum
);
1256 if (blockNum
>= numBlocks
) break;
1261 if ((blockNum
!= numBlocks
)) {
1262 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
);
1265 PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
);
1270 int CmdHF14AMfESave(const char *Cmd
)
1273 char filename
[FILE_PATH_SIZE
];
1274 char * fnameptr
= filename
;
1276 int i
, j
, len
, numBlocks
;
1277 int nameParamNo
= 1;
1279 memset(filename
, 0, sizeof(filename
));
1280 memset(buf
, 0, sizeof(buf
));
1282 char ctmp
= param_getchar(Cmd
, 0);
1284 if ( ctmp
== 'h' || ctmp
== 'H') {
1285 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1286 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1287 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1289 PrintAndLog(" sample: hf mf esave ");
1290 PrintAndLog(" hf mf esave 4");
1291 PrintAndLog(" hf mf esave 4 filename");
1296 case '0' : numBlocks
= 5*4; break;
1298 case '\0': numBlocks
= 16*4; break;
1299 case '2' : numBlocks
= 32*4; break;
1300 case '4' : numBlocks
= 256; break;
1307 len
= param_getstr(Cmd
,nameParamNo
,filename
);
1309 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1311 // user supplied filename?
1313 // get filename (UID from memory)
1314 if (mfEmlGetMem(buf
, 0, 1)) {
1315 PrintAndLog("Can\'t get UID from block: %d", 0);
1316 len
= sprintf(fnameptr
, "dump");
1320 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1321 sprintf(fnameptr
, "%02X", buf
[j
]);
1327 // add file extension
1328 sprintf(fnameptr
, ".eml");
1331 f
= fopen(filename
, "w+");
1334 PrintAndLog("Can't open file %s ", filename
);
1339 for (i
= 0; i
< numBlocks
; i
++) {
1340 if (mfEmlGetMem(buf
, i
, 1)) {
1341 PrintAndLog("Cant get block: %d", i
);
1344 for (j
= 0; j
< 16; j
++)
1345 fprintf(f
, "%02X", buf
[j
]);
1350 PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
);
1356 int CmdHF14AMfECFill(const char *Cmd
)
1358 uint8_t keyType
= 0;
1359 uint8_t numSectors
= 16;
1361 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1362 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1363 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1365 PrintAndLog("samples: hf mf ecfill A");
1366 PrintAndLog(" hf mf ecfill A 4");
1367 PrintAndLog("Read card and transfer its data to emulator memory.");
1368 PrintAndLog("Keys must be laid in the emulator memory. \n");
1372 char ctmp
= param_getchar(Cmd
, 0);
1373 if (ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B') {
1374 PrintAndLog("Key type must be A or B");
1377 if (ctmp
!= 'A' && ctmp
!= 'a') keyType
= 1;
1379 ctmp
= param_getchar(Cmd
, 1);
1381 case '0' : numSectors
= 5; break;
1383 case '\0': numSectors
= 16; break;
1384 case '2' : numSectors
= 32; break;
1385 case '4' : numSectors
= 40; break;
1386 default: numSectors
= 16;
1389 printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
);
1390 UsbCommand c
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}};
1396 int CmdHF14AMfEKeyPrn(const char *Cmd
)
1401 uint64_t keyA
, keyB
;
1403 if (param_getchar(Cmd
, 0) == 'h') {
1404 PrintAndLog("It prints the keys loaded in the emulator memory");
1405 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1406 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1408 PrintAndLog(" sample: hf mf ekeyprn 1");
1412 char cmdp
= param_getchar(Cmd
, 0);
1415 case '0' : numSectors
= 5; break;
1417 case '\0': numSectors
= 16; break;
1418 case '2' : numSectors
= 32; break;
1419 case '4' : numSectors
= 40; break;
1420 default: numSectors
= 16;
1423 PrintAndLog("|---|----------------|----------------|");
1424 PrintAndLog("|sec|key A |key B |");
1425 PrintAndLog("|---|----------------|----------------|");
1426 for (i
= 0; i
< numSectors
; i
++) {
1427 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) {
1428 PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1);
1431 keyA
= bytes_to_num(data
, 6);
1432 keyB
= bytes_to_num(data
+ 10, 6);
1433 PrintAndLog("|%03d| %012"llx
" | %012"llx
" |", i
, keyA
, keyB
);
1435 PrintAndLog("|---|----------------|----------------|");
1441 int CmdHF14AMfCSetUID(const char *Cmd
)
1443 uint8_t wipeCard
= 0;
1444 uint8_t uid
[8] = {0x00};
1445 uint8_t oldUid
[8] = {0x00};
1446 uint8_t atqa
[2] = {0x00};
1447 uint8_t sak
[1] = {0x00};
1448 uint8_t atqaPresent
= 1;
1453 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') {
1454 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1455 PrintAndLog("sample: hf mf csetuid 01020304");
1456 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1457 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1458 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1462 if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) {
1463 PrintAndLog("UID must include 8 HEX symbols");
1468 ctmp
= param_getchar(Cmd
, argi
);
1469 if (ctmp
== 'w' || ctmp
== 'W') {
1475 if (param_getchar(Cmd
, argi
)) {
1476 if (param_gethex(Cmd
, argi
, atqa
, 4)) {
1477 PrintAndLog("ATQA must include 4 HEX symbols");
1481 if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) {
1482 PrintAndLog("SAK must include 2 HEX symbols");
1491 ctmp
= param_getchar(Cmd
, argi
);
1492 if (ctmp
== 'w' || ctmp
== 'W') {
1497 PrintAndLog("--wipe card:%s uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4));
1499 res
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
);
1501 PrintAndLog("Can't set UID. error=%d", res
);
1505 PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4));
1506 PrintAndLog("new UID:%s", sprint_hex(uid
, 4));
1510 int CmdHF14AMfCSetBlk(const char *Cmd
)
1512 uint8_t memBlock
[16] = {0x00};
1513 uint8_t blockNo
= 0;
1514 bool wipeCard
= FALSE
;
1517 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1518 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1519 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1520 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1521 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1525 blockNo
= param_get8(Cmd
, 0);
1527 if (param_gethex(Cmd
, 1, memBlock
, 32)) {
1528 PrintAndLog("block data must include 32 HEX symbols");
1532 char ctmp
= param_getchar(Cmd
, 2);
1533 wipeCard
= (ctmp
== 'w' || ctmp
== 'W');
1534 PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16));
1536 res
= mfCSetBlock(blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1538 PrintAndLog("Can't write block. error=%d", res
);
1545 int CmdHF14AMfCLoad(const char *Cmd
)
1548 char filename
[FILE_PATH_SIZE
] = {0x00};
1549 char * fnameptr
= filename
;
1550 char buf
[64] = {0x00};
1551 uint8_t buf8
[64] = {0x00};
1552 uint8_t fillFromEmulator
= 0;
1553 int i
, len
, blockNum
, flags
=0;
1555 if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) {
1556 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1557 PrintAndLog("or from emulator memory (option `e`)");
1558 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1559 PrintAndLog(" or: hf mf cload e ");
1560 PrintAndLog(" sample: hf mf cload filename");
1564 char ctmp
= param_getchar(Cmd
, 0);
1565 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1567 if (fillFromEmulator
) {
1568 for (blockNum
= 0; blockNum
< 16 * 4; blockNum
+= 1) {
1569 if (mfEmlGetMem(buf8
, blockNum
, 1)) {
1570 PrintAndLog("Cant get block: %d", blockNum
);
1573 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1574 if (blockNum
== 1) flags
= 0; // just write
1575 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1577 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1578 PrintAndLog("Cant set magic card block: %d", blockNum
);
1585 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1587 memcpy(filename
, Cmd
, len
);
1590 sprintf(fnameptr
, ".eml");
1593 f
= fopen(filename
, "r");
1595 PrintAndLog("File not found or locked.");
1602 memset(buf
, 0, sizeof(buf
));
1604 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
1606 PrintAndLog("File reading error.");
1610 if (strlen(buf
) < 32) {
1611 if(strlen(buf
) && feof(f
))
1613 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1617 for (i
= 0; i
< 32; i
+= 2)
1618 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i
/ 2]);
1620 if (blockNum
== 0) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1621 if (blockNum
== 1) flags
= 0; // just write
1622 if (blockNum
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1624 if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) {
1625 PrintAndLog("Can't set magic card block: %d", blockNum
);
1630 if (blockNum
>= 16 * 4) break; // magic card type - mifare 1K
1634 if (blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16){
1635 PrintAndLog("File content error. There must be 64 blocks");
1638 PrintAndLog("Loaded from file: %s", filename
);
1644 int CmdHF14AMfCGetBlk(const char *Cmd
) {
1645 uint8_t memBlock
[16];
1646 uint8_t blockNo
= 0;
1648 memset(memBlock
, 0x00, sizeof(memBlock
));
1650 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1651 PrintAndLog("Usage: hf mf cgetblk <block number>");
1652 PrintAndLog("sample: hf mf cgetblk 1");
1653 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1657 blockNo
= param_get8(Cmd
, 0);
1659 PrintAndLog("--block number:%2d ", blockNo
);
1661 res
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1663 PrintAndLog("Can't read block. error=%d", res
);
1667 PrintAndLog("block data:%s", sprint_hex(memBlock
, 16));
1672 int CmdHF14AMfCGetSc(const char *Cmd
) {
1673 uint8_t memBlock
[16] = {0x00};
1674 uint8_t sectorNo
= 0;
1677 if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') {
1678 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1679 PrintAndLog("sample: hf mf cgetsc 0");
1680 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1684 sectorNo
= param_get8(Cmd
, 0);
1685 if (sectorNo
> 15) {
1686 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1690 PrintAndLog("--sector number:%d ", sectorNo
);
1692 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1693 for (i
= 0; i
< 4; i
++) {
1694 if (i
== 1) flags
= 0;
1695 if (i
== 3) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1697 res
= mfCGetBlock(sectorNo
* 4 + i
, memBlock
, flags
);
1699 PrintAndLog("Can't read block. %d error=%d", sectorNo
* 4 + i
, res
);
1703 PrintAndLog("block %3d data:%s", sectorNo
* 4 + i
, sprint_hex(memBlock
, 16));
1709 int CmdHF14AMfCSave(const char *Cmd
) {
1712 char filename
[FILE_PATH_SIZE
] = {0x00};
1713 char * fnameptr
= filename
;
1714 uint8_t fillFromEmulator
= 0;
1715 uint8_t buf
[64] = {0x00};
1716 int i
, j
, len
, flags
;
1718 // memset(filename, 0, sizeof(filename));
1719 // memset(buf, 0, sizeof(buf));
1721 if (param_getchar(Cmd
, 0) == 'h') {
1722 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1723 PrintAndLog("or into emulator memory (option `e`)");
1724 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1725 PrintAndLog(" sample: hf mf esave ");
1726 PrintAndLog(" hf mf esave filename");
1727 PrintAndLog(" hf mf esave e \n");
1731 char ctmp
= param_getchar(Cmd
, 0);
1732 if (ctmp
== 'e' || ctmp
== 'E') fillFromEmulator
= 1;
1734 if (fillFromEmulator
) {
1735 // put into emulator
1736 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1737 for (i
= 0; i
< 16 * 4; i
++) {
1738 if (i
== 1) flags
= 0;
1739 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1741 if (mfCGetBlock(i
, buf
, flags
)) {
1742 PrintAndLog("Cant get block: %d", i
);
1746 if (mfEmlSetMem(buf
, i
, 1)) {
1747 PrintAndLog("Cant set emul block: %d", i
);
1754 if (len
> FILE_PATH_SIZE
- 4) len
= FILE_PATH_SIZE
- 4;
1758 if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) {
1759 PrintAndLog("Cant get block: %d", 0);
1760 len
= sprintf(fnameptr
, "dump");
1764 for (j
= 0; j
< 7; j
++, fnameptr
+= 2)
1765 sprintf(fnameptr
, "%02x", buf
[j
]);
1768 memcpy(filename
, Cmd
, len
);
1772 sprintf(fnameptr
, ".eml");
1775 f
= fopen(filename
, "w+");
1778 PrintAndLog("File not found or locked.");
1783 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1784 for (i
= 0; i
< 16 * 4; i
++) {
1785 if (i
== 1) flags
= 0;
1786 if (i
== 16 * 4 - 1) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1788 if (mfCGetBlock(i
, buf
, flags
)) {
1789 PrintAndLog("Cant get block: %d", i
);
1792 for (j
= 0; j
< 16; j
++)
1793 fprintf(f
, "%02x", buf
[j
]);
1798 PrintAndLog("Saved to file: %s", filename
);
1805 int CmdHF14AMfSniff(const char *Cmd
){
1807 bool wantLogToFile
= 0;
1808 bool wantDecrypt
= 0;
1809 //bool wantSaveToEml = 0; TODO
1810 bool wantSaveToEmlFile
= 0;
1820 uint8_t atqa
[2] = {0x00};
1823 uint8_t *buf
= NULL
;
1824 uint16_t bufsize
= 0;
1825 uint8_t *bufPtr
= NULL
;
1827 char ctmp
= param_getchar(Cmd
, 0);
1828 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1829 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1830 PrintAndLog("You can specify:");
1831 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1832 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1833 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1834 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1835 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1836 PrintAndLog(" sample: hf mf sniff l d e");
1840 for (int i
= 0; i
< 4; i
++) {
1841 ctmp
= param_getchar(Cmd
, i
);
1842 if (ctmp
== 'l' || ctmp
== 'L') wantLogToFile
= true;
1843 if (ctmp
== 'd' || ctmp
== 'D') wantDecrypt
= true;
1844 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1845 if (ctmp
== 'f' || ctmp
== 'F') wantSaveToEmlFile
= true;
1848 printf("-------------------------------------------------------------------------\n");
1849 printf("Executing command. \n");
1850 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1851 printf("Press the key on pc keyboard to abort the client.\n");
1852 printf("-------------------------------------------------------------------------\n");
1854 UsbCommand c
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}};
1855 clearCommandBuffer();
1864 printf("\naborted via keyboard!\n");
1869 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) {
1870 res
= resp
.arg
[0] & 0xff;
1871 uint16_t traceLen
= resp
.arg
[1];
1874 if (res
== 0) return 0; // we are done
1876 if (res
== 1) { // there is (more) data to be transferred
1877 if (pckNum
== 0) { // first packet, (re)allocate necessary buffer
1878 if (traceLen
> bufsize
) {
1880 if (buf
== NULL
) { // not yet allocated
1881 p
= malloc(traceLen
);
1882 } else { // need more memory
1883 p
= realloc(buf
, traceLen
);
1886 PrintAndLog("Cannot allocate memory for trace");
1894 memset(buf
, 0x00, traceLen
);
1896 memcpy(bufPtr
, resp
.d
.asBytes
, len
);
1901 if (res
== 2) { // received all data, start displaying
1902 blockLen
= bufPtr
- buf
;
1905 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
);
1906 while (bufPtr
- buf
< blockLen
) {
1907 bufPtr
+= 6; // skip (void) timing information
1908 len
= *((uint16_t *)bufPtr
);
1916 if ((len
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) {
1917 memcpy(uid
, bufPtr
+ 2, 7);
1918 memcpy(atqa
, bufPtr
+ 2 + 7, 2);
1919 uid_len
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4;
1921 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1922 sprint_hex(uid
+ (7 - uid_len
), uid_len
),
1926 if (wantLogToFile
|| wantDecrypt
) {
1927 FillFileNameByUID(logHexFileName
, uid
+ (7 - uid_len
), ".log", uid_len
);
1928 AddLogCurrentDT(logHexFileName
);
1931 mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
);
1933 PrintAndLog("%s(%d):%s", isTag
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
));
1935 AddLogHex(logHexFileName
, isTag
? "TAG: ":"RDR: ", bufPtr
, len
);
1937 mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
);
1941 bufPtr
+= ((len
-1)/8+1); // ignore parity
1953 static command_t CommandTable
[] =
1955 {"help", CmdHelp
, 1, "This help"},
1956 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1957 {"rdbl", CmdHF14AMfRdBl
, 0, "Read MIFARE classic block"},
1958 {"rdsc", CmdHF14AMfRdSc
, 0, "Read MIFARE classic sector"},
1959 {"dump", CmdHF14AMfDump
, 0, "Dump MIFARE classic tag to binary file"},
1960 {"restore", CmdHF14AMfRestore
, 0, "Restore MIFARE classic binary file to BLANK tag"},
1961 {"wrbl", CmdHF14AMfWrBl
, 0, "Write MIFARE classic block"},
1962 {"chk", CmdHF14AMfChk
, 0, "Test block keys"},
1963 {"mifare", CmdHF14AMifare
, 0, "Read parity error messages."},
1964 {"nested", CmdHF14AMfNested
, 0, "Test nested authentication"},
1965 {"sniff", CmdHF14AMfSniff
, 0, "Sniff card-reader communication"},
1966 {"sim", CmdHF14AMf1kSim
, 0, "Simulate MIFARE card"},
1967 {"eclr", CmdHF14AMfEClear
, 0, "Clear simulator memory block"},
1968 {"eget", CmdHF14AMfEGet
, 0, "Get simulator memory block"},
1969 {"eset", CmdHF14AMfESet
, 0, "Set simulator memory block"},
1970 {"eload", CmdHF14AMfELoad
, 0, "Load from file emul dump"},
1971 {"esave", CmdHF14AMfESave
, 0, "Save to file emul dump"},
1972 {"ecfill", CmdHF14AMfECFill
, 0, "Fill simulator memory with help of keys from simulator"},
1973 {"ekeyprn", CmdHF14AMfEKeyPrn
, 0, "Print keys from simulator memory"},
1974 {"csetuid", CmdHF14AMfCSetUID
, 0, "Set UID for magic Chinese card"},
1975 {"csetblk", CmdHF14AMfCSetBlk
, 0, "Write block - Magic Chinese card"},
1976 {"cgetblk", CmdHF14AMfCGetBlk
, 0, "Read block - Magic Chinese card"},
1977 {"cgetsc", CmdHF14AMfCGetSc
, 0, "Read sector - Magic Chinese card"},
1978 {"cload", CmdHF14AMfCLoad
, 0, "Load dump into magic Chinese card"},
1979 {"csave", CmdHF14AMfCSave
, 0, "Save dump from magic Chinese card into file or emulator"},
1980 {NULL
, NULL
, 0, NULL
}
1983 int CmdHFMF(const char *Cmd
)
1986 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1988 CmdsParse(CommandTable
, Cmd
);
1992 int CmdHelp(const char *Cmd
)
1994 CmdsHelp(CommandTable
);