]>
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
); 
  17 int CmdHF14AMifare(const char *Cmd
) 
  20         uint32_t nt 
= 0, nr 
= 0; 
  21         uint64_t par_list 
= 0, ks_list 
= 0, r_key 
= 0; 
  26         char cmdp 
= param_getchar(Cmd
, 0);       
  27         if ( cmdp 
== 'H' || cmdp 
== 'h') { 
  28                 PrintAndLog("Usage:  hf mf mifare <block number>"); 
  29                 PrintAndLog("        sample: hf mf mifare 0"); 
  33         blockNo 
= param_get8(Cmd
, 0); 
  34         UsbCommand c 
= {CMD_READER_MIFARE
, {true, blockNo
, 0}}; 
  37         printf("-------------------------------------------------------------------------\n"); 
  38         printf("Executing command. Expected execution time: 25sec on average  :-)\n"); 
  39         printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n"); 
  40         printf("-------------------------------------------------------------------------\n"); 
  61                         printf("\naborted via keyboard!\n"); 
  66                 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)) { 
  68                         uid 
= (uint32_t)bytes_to_num(resp
.d
.asBytes 
+  0, 4); 
  69                         nt 
=  (uint32_t)bytes_to_num(resp
.d
.asBytes 
+  4, 4); 
  70                         par_list 
= bytes_to_num(resp
.d
.asBytes 
+  8, 8); 
  71                         ks_list 
= bytes_to_num(resp
.d
.asBytes 
+  16, 8); 
  72                         nr 
= bytes_to_num(resp
.d
.asBytes 
+ 24, 4); 
  75                                 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break; 
  76                                 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break; 
  77                                 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break; 
  78                                 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown"); 
  79                                                   PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break; 
  89         if (isOK 
!= 1) return 1; 
  91         // execute original function from util nonce2key 
  92         if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) { 
  94                 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);     
  95                 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce..."); 
 100                 printf("------------------------------------------------------------------\n"); 
 101                 PrintAndLog("Found valid key: %012"llx
" \n", r_key
); 
 105                 PrintAndLog("Time in darkside: %.0f ticks - %4.2f sec\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
); 
 110 int CmdHF14AMfWrBl(const char *Cmd
) 
 114         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 115         uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
 120                 PrintAndLog("Usage:  hf mf wrbl    <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>"); 
 121                 PrintAndLog("        sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F"); 
 125         blockNo 
= param_get8(Cmd
, 0); 
 126         cmdp 
= param_getchar(Cmd
, 1); 
 128                 PrintAndLog("Key type must be A or B"); 
 131         if (cmdp 
!= 'A' && cmdp 
!= 'a') keyType 
= 1; 
 132         if (param_gethex(Cmd
, 2, key
, 12)) { 
 133                 PrintAndLog("Key must include 12 HEX symbols"); 
 136         if (param_gethex(Cmd
, 3, bldata
, 32)) { 
 137                 PrintAndLog("Block data must include 32 HEX symbols"); 
 140         PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6)); 
 141         PrintAndLog("--data: %s", sprint_hex(bldata
, 16)); 
 143         UsbCommand c 
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}}; 
 144         memcpy(c
.d
.asBytes
, key
, 6); 
 145         memcpy(c
.d
.asBytes 
+ 10, bldata
, 16); 
 146         clearCommandBuffer(); 
 150         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 151                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 152                 PrintAndLog("isOk:%02x", isOK
); 
 154                 PrintAndLog("Command execute timeout"); 
 160 int CmdHF14AMfRdBl(const char *Cmd
) 
 164         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 170                 PrintAndLog("Usage:  hf mf rdbl    <block number> <key A/B> <key (12 hex symbols)>"); 
 171                 PrintAndLog("        sample: hf mf rdbl 0 A FFFFFFFFFFFF "); 
 175         blockNo 
= param_get8(Cmd
, 0); 
 176         cmdp 
= param_getchar(Cmd
, 1); 
 178                 PrintAndLog("Key type must be A or B"); 
 181         if (cmdp 
!= 'A' && cmdp 
!= 'a') keyType 
= 1; 
 182         if (param_gethex(Cmd
, 2, key
, 12)) { 
 183                 PrintAndLog("Key must include 12 HEX symbols"); 
 186         PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6)); 
 188         UsbCommand c 
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}}; 
 189         memcpy(c
.d
.asBytes
, key
, 6); 
 190         clearCommandBuffer(); 
 194         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 195                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 196                 uint8_t *data 
= resp
.d
.asBytes
; 
 199                         PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16)); 
 201                         PrintAndLog("isOk:%02x", isOK
); 
 203                 PrintAndLog("Command execute timeout"); 
 209 int CmdHF14AMfRdSc(const char *Cmd
) 
 212         uint8_t sectorNo 
= 0; 
 214         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 216         uint8_t *data  
= NULL
; 
 220                 PrintAndLog("Usage:  hf mf rdsc    <sector number> <key A/B> <key (12 hex symbols)>"); 
 221                 PrintAndLog("        sample: hf mf rdsc 0 A FFFFFFFFFFFF "); 
 225         sectorNo 
= param_get8(Cmd
, 0); 
 227                 PrintAndLog("Sector number must be less than 40"); 
 230         cmdp 
= param_getchar(Cmd
, 1); 
 231         if (cmdp 
!= 'a' && cmdp 
!= 'A' && cmdp 
!= 'b' && cmdp 
!= 'B') { 
 232                 PrintAndLog("Key type must be A or B"); 
 235         if (cmdp 
!= 'A' && cmdp 
!= 'a') keyType 
= 1; 
 236         if (param_gethex(Cmd
, 2, key
, 12)) { 
 237                 PrintAndLog("Key must include 12 HEX symbols"); 
 240         PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6)); 
 242         UsbCommand c 
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}}; 
 243         memcpy(c
.d
.asBytes
, key
, 6); 
 244         clearCommandBuffer(); 
 249         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 250                 isOK  
= resp
.arg
[0] & 0xff; 
 251                 data  
= resp
.d
.asBytes
; 
 253                 PrintAndLog("isOk:%02x", isOK
); 
 255                         for (i 
= 0; i 
< (sectorNo
<32?3:15); i
++) { 
 256                                 PrintAndLog("data   : %s", sprint_hex(data 
+ i 
* 16, 16)); 
 258                         PrintAndLog("trailer: %s", sprint_hex(data 
+ (sectorNo
<32?3:15) * 16, 16)); 
 261                 PrintAndLog("Command execute timeout"); 
 267 uint8_t FirstBlockOfSector(uint8_t sectorNo
) 
 272                 return 32 * 4 + (sectorNo 
- 32) * 16; 
 276 uint8_t NumBlocksPerSector(uint8_t sectorNo
) 
 285 int CmdHF14AMfDump(const char *Cmd
) 
 287         uint8_t sectorNo
, blockNo
; 
 291         uint8_t rights
[40][4]; 
 292         uint8_t carddata
[256][16]; 
 293         uint8_t numSectors 
= 16; 
 300         char cmdp 
= param_getchar(Cmd
, 0); 
 302                 case '0' : numSectors 
= 5; break; 
 304                 case '\0': numSectors 
= 16; break; 
 305                 case '2' : numSectors 
= 32; break; 
 306                 case '4' : numSectors 
= 40; break; 
 307                 default:   numSectors 
= 16; 
 310         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') { 
 311                 PrintAndLog("Usage:   hf mf dump [card memory]"); 
 312                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
 314                 PrintAndLog("Samples: hf mf dump"); 
 315                 PrintAndLog("         hf mf dump 4"); 
 319         if ((fin 
= fopen("dumpkeys.bin","rb")) == NULL
) { 
 320                 PrintAndLog("Could not find file dumpkeys.bin"); 
 324         // Read keys A from file 
 326         for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) { 
 327                 bytes_read 
= fread( keyA
[sectorNo
], 1, 6, fin 
); 
 328                 if ( bytes_read 
== 0) { 
 329                         PrintAndLog("File reading error."); 
 335         // Read keys B from file 
 336         for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) { 
 337                 bytes_read 
= fread( keyB
[sectorNo
], 1, 6, fin 
); 
 338                 if ( bytes_read 
== 0) { 
 339                         PrintAndLog("File reading error."); 
 347         PrintAndLog("|-----------------------------------------|"); 
 348         PrintAndLog("|------ Reading sector access bits...-----|"); 
 349         PrintAndLog("|-----------------------------------------|"); 
 351         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 352                 UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}}; 
 353                 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6); 
 354                 clearCommandBuffer(); 
 357                 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 358                         uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 359                         uint8_t *data  
= resp
.d
.asBytes
; 
 361                                 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0 
 362                                 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1 
 363                                 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2 
 364                                 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer 
 366                                 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
); 
 367                                 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00; 
 368                                 rights
[sectorNo
][3] = 0x01; 
 371                         PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
); 
 372                         rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00; 
 373                         rights
[sectorNo
][3] = 0x01; 
 377         PrintAndLog("|-----------------------------------------|"); 
 378         PrintAndLog("|----- Dumping all blocks to file... -----|"); 
 379         PrintAndLog("|-----------------------------------------|"); 
 382         for (sectorNo 
= 0; isOK 
&& sectorNo 
< numSectors
; sectorNo
++) { 
 383                 for (blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 384                         bool received 
= false; 
 386                         if (blockNo 
== NumBlocksPerSector(sectorNo
) - 1) {              // sector trailer. At least the Access Conditions can always be read with key A.  
 387                                 UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}}; 
 388                                 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6); 
 389                                 clearCommandBuffer(); 
 391                                 received 
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500); 
 392                         } else {                                                                                                // data block. Check if it can be read with key A or key B 
 393                                 uint8_t data_area 
= sectorNo
<32?blockNo
:blockNo
/5; 
 394                                 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) {   // only key B would work 
 395                                         UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}}; 
 396                                         memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6); 
 398                                         received 
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500); 
 399                                 } else if (rights
[sectorNo
][data_area
] == 0x07) {                                                                               // no key would work 
 401                                         PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
); 
 402                                 } else {                                                                                                                                                                // key A would work 
 403                                         UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}}; 
 404                                         memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6); 
 405                                         clearCommandBuffer(); 
 407                                         received 
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500); 
 412                                 isOK  
= resp
.arg
[0] & 0xff; 
 413                                 uint8_t *data  
= resp
.d
.asBytes
; 
 414                                 if (blockNo 
== NumBlocksPerSector(sectorNo
) - 1) {              // sector trailer. Fill in the keys. 
 415                                         data
[0]  = (keyA
[sectorNo
][0]); 
 416                                         data
[1]  = (keyA
[sectorNo
][1]); 
 417                                         data
[2]  = (keyA
[sectorNo
][2]); 
 418                                         data
[3]  = (keyA
[sectorNo
][3]); 
 419                                         data
[4]  = (keyA
[sectorNo
][4]); 
 420                                         data
[5]  = (keyA
[sectorNo
][5]); 
 421                                         data
[10] = (keyB
[sectorNo
][0]); 
 422                                         data
[11] = (keyB
[sectorNo
][1]); 
 423                                         data
[12] = (keyB
[sectorNo
][2]); 
 424                                         data
[13] = (keyB
[sectorNo
][3]); 
 425                                         data
[14] = (keyB
[sectorNo
][4]); 
 426                                         data
[15] = (keyB
[sectorNo
][5]); 
 429                                         memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16); 
 430                     PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
); 
 432                                         PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
); 
 438                                 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
); 
 445                 if ((fout 
= fopen("dumpdata.bin","wb")) == NULL
) {  
 446                         PrintAndLog("Could not create file name dumpdata.bin"); 
 449                 uint16_t numblocks 
= FirstBlockOfSector(numSectors 
- 1) + NumBlocksPerSector(numSectors 
- 1); 
 450                 fwrite(carddata
, 1, 16*numblocks
, fout
); 
 452                 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
); 
 458 int CmdHF14AMfRestore(const char *Cmd
) 
 460         uint8_t sectorNo
,blockNo
; 
 462         uint8_t key
[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; 
 463         uint8_t bldata
[16] = {0x00}; 
 471         char cmdp 
= param_getchar(Cmd
, 0); 
 473                 case '0' : numSectors 
= 5; break; 
 475                 case '\0': numSectors 
= 16; break; 
 476                 case '2' : numSectors 
= 32; break; 
 477                 case '4' : numSectors 
= 40; break; 
 478                 default:   numSectors 
= 16; 
 481         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') { 
 482                 PrintAndLog("Usage:   hf mf restore [card memory]"); 
 483                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
 485                 PrintAndLog("Samples: hf mf restore"); 
 486                 PrintAndLog("         hf mf restore 4"); 
 490         if ((fkeys 
= fopen("dumpkeys.bin","rb")) == NULL
) { 
 491                 PrintAndLog("Could not find file dumpkeys.bin"); 
 496         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 497                 bytes_read 
= fread( keyA
[sectorNo
], 1, 6, fkeys 
); 
 498                 if ( bytes_read 
== 0) { 
 499                         PrintAndLog("File reading error (dumpkeys.bin)."); 
 505         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 506                 bytes_read 
= fread( keyB
[sectorNo
], 1, 6, fkeys 
); 
 507                 if ( bytes_read 
== 0) { 
 508                         PrintAndLog("File reading error (dumpkeys.bin)."); 
 516         if ((fdump 
= fopen("dumpdata.bin","rb")) == NULL
) { 
 517                 PrintAndLog("Could not find file dumpdata.bin"); 
 520         PrintAndLog("Restoring dumpdata.bin to card"); 
 522         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 523                 for(blockNo 
= 0; blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 524                         UsbCommand c 
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}}; 
 525                         memcpy(c
.d
.asBytes
, key
, 6);                     
 526                         bytes_read 
= fread(bldata
, 1, 16, fdump
); 
 527                         if ( bytes_read 
== 0) { 
 528                                 PrintAndLog("File reading error (dumpdata.bin)."); 
 533                         if (blockNo 
== NumBlocksPerSector(sectorNo
) - 1) {      // sector trailer 
 534                                 bldata
[0]  = (keyA
[sectorNo
][0]); 
 535                                 bldata
[1]  = (keyA
[sectorNo
][1]); 
 536                                 bldata
[2]  = (keyA
[sectorNo
][2]); 
 537                                 bldata
[3]  = (keyA
[sectorNo
][3]); 
 538                                 bldata
[4]  = (keyA
[sectorNo
][4]); 
 539                                 bldata
[5]  = (keyA
[sectorNo
][5]); 
 540                                 bldata
[10] = (keyB
[sectorNo
][0]); 
 541                                 bldata
[11] = (keyB
[sectorNo
][1]); 
 542                                 bldata
[12] = (keyB
[sectorNo
][2]); 
 543                                 bldata
[13] = (keyB
[sectorNo
][3]); 
 544                                 bldata
[14] = (keyB
[sectorNo
][4]); 
 545                                 bldata
[15] = (keyB
[sectorNo
][5]); 
 548                         PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16)); 
 550                         memcpy(c
.d
.asBytes 
+ 10, bldata
, 16); 
 551                         clearCommandBuffer(); 
 555                         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 556                                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 557                                 PrintAndLog("isOk:%02x", isOK
); 
 559                                 PrintAndLog("Command execute timeout"); 
 568 int CmdHF14AMfNested(const char *Cmd
) 
 570         int i
, j
, res
, iterations
; 
 571         sector 
*e_sector 
= NULL
; 
 574         uint8_t trgBlockNo 
= 0; 
 575         uint8_t trgKeyType 
= 0; 
 576         uint8_t SectorsCnt 
= 0; 
 577         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 578         uint8_t keyBlock
[14*6]; 
 580         bool transferToEml 
= false; 
 582         bool createDumpFile 
= false; 
 584         uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
 585         uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
 590                 PrintAndLog("Usage:"); 
 591                 PrintAndLog(" all sectors:  hf mf nested  <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]"); 
 592                 PrintAndLog(" one sector:   hf mf nested  o <block number> <key A/B> <key (12 hex symbols)>"); 
 593                 PrintAndLog("               <target block number> <target key A/B> [t]"); 
 594                 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K"); 
 595                 PrintAndLog("t - transfer keys into emulator memory"); 
 596                 PrintAndLog("d - write keys to binary file"); 
 598                 PrintAndLog("      sample1: hf mf nested 1 0 A FFFFFFFFFFFF "); 
 599                 PrintAndLog("      sample2: hf mf nested 1 0 A FFFFFFFFFFFF t "); 
 600                 PrintAndLog("      sample3: hf mf nested 1 0 A FFFFFFFFFFFF d "); 
 601                 PrintAndLog("      sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A"); 
 605         cmdp 
= param_getchar(Cmd
, 0); 
 606         blockNo 
= param_get8(Cmd
, 1); 
 607         ctmp 
= param_getchar(Cmd
, 2); 
 609         if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
 610                 PrintAndLog("Key type must be A or B"); 
 614         if (ctmp 
!= 'A' && ctmp 
!= 'a')  
 617         if (param_gethex(Cmd
, 3, key
, 12)) { 
 618                 PrintAndLog("Key must include 12 HEX symbols"); 
 622         if (cmdp 
== 'o' || cmdp 
== 'O') { 
 624                 trgBlockNo 
= param_get8(Cmd
, 4); 
 625                 ctmp 
= param_getchar(Cmd
, 5); 
 626                 if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
 627                         PrintAndLog("Target key type must be A or B"); 
 630                 if (ctmp 
!= 'A' && ctmp 
!= 'a')  
 635                         case '0': SectorsCnt 
= 05; break; 
 636                         case '1': SectorsCnt 
= 16; break; 
 637                         case '2': SectorsCnt 
= 32; break; 
 638                         case '4': SectorsCnt 
= 40; break; 
 639                         default:  SectorsCnt 
= 16; 
 643         ctmp 
= param_getchar(Cmd
, 4); 
 644         if              (ctmp 
== 't' || ctmp 
== 'T') transferToEml 
= true; 
 645         else if (ctmp 
== 'd' || ctmp 
== 'D') createDumpFile 
= true; 
 647         ctmp 
= param_getchar(Cmd
, 6); 
 648         transferToEml 
|= (ctmp 
== 't' || ctmp 
== 'T'); 
 649         transferToEml 
|= (ctmp 
== 'd' || ctmp 
== 'D'); 
 652                 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A'); 
 653                 int16_t isOK 
= mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true); 
 656                                 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break; 
 657                                 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break; 
 658                                 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break; 
 659                                 default : PrintAndLog("Unknown Error.\n"); 
 663                 key64 
= bytes_to_num(keyBlock
, 6); 
 665                         PrintAndLog("Found valid key:%012"llx
, key64
); 
 667                         // transfer key to the emulator 
 669                                 uint8_t sectortrailer
; 
 670                                 if (trgBlockNo 
< 32*4) {        // 4 block sector 
 671                                         sectortrailer 
= (trgBlockNo 
& 0x03) + 3; 
 672                                 } else {                                        // 16 block sector 
 673                                         sectortrailer 
= (trgBlockNo 
& 0x0f) + 15; 
 675                                 mfEmlGetMem(keyBlock
, sectortrailer
, 1); 
 678                                         num_to_bytes(key64
, 6, keyBlock
); 
 680                                         num_to_bytes(key64
, 6, &keyBlock
[10]); 
 681                                 mfEmlSetMem(keyBlock
, sectortrailer
, 1);                 
 684                         PrintAndLog("No valid key found"); 
 687         else { // ------------------------------------  multiple sectors working 
 688                 clock_t t1 
= clock(); 
 690                 e_sector 
= calloc(SectorsCnt
, sizeof(sector
)); 
 691                 if (e_sector 
== NULL
) return 1; 
 693                 //test current key and additional standard keys first 
 694                 memcpy(keyBlock
, key
, 6); 
 695                 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock 
+ 1 * 6)); 
 696                 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock 
+ 2 * 6)); 
 697                 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock 
+ 3 * 6)); 
 698                 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock 
+ 4 * 6)); 
 699                 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock 
+ 5 * 6)); 
 700                 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock 
+ 6 * 6)); 
 701                 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock 
+ 7 * 6)); 
 702                 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock 
+ 8 * 6)); 
 703                 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock 
+ 9 * 6)); 
 704                 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock 
+ 10 * 6)); 
 705                 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock 
+ 11 * 6)); 
 706                 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock 
+ 12 * 6)); 
 707                 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock 
+ 13 * 6)); 
 709                 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
); 
 710                 for (i 
= 0; i 
< SectorsCnt
; i
++) { 
 711                         for (j 
= 0; j 
< 2; j
++) { 
 712                                 if (e_sector
[i
].foundKey
[j
]) continue; 
 714                                 res 
= mfCheckKeys(FirstBlockOfSector(i
), j
, true, 6, keyBlock
, &key64
); 
 717                                         e_sector
[i
].Key
[j
] = key64
; 
 718                                         e_sector
[i
].foundKey
[j
] = TRUE
; 
 725                 PrintAndLog("nested..."); 
 726                 bool calibrate 
= true; 
 727                 for (i 
= 0; i 
< NESTED_SECTOR_RETRY
; i
++) { 
 728                         for (uint8_t sectorNo 
= 0; sectorNo 
< SectorsCnt
; sectorNo
++) { 
 729                                 for (trgKeyType 
= 0; trgKeyType 
< 2; trgKeyType
++) {  
 730                                         if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue; 
 731                                         PrintAndLog("-----------------------------------------------"); 
 732                                         int16_t isOK 
= mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
); 
 735                                                         case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break; 
 736                                                         case -2 : PrintAndLog("Button pressed. Aborted.\n"); break; 
 737                                                         case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break; 
 738                                                         default : PrintAndLog("Unknown Error.\n"); 
 748                                         key64 
= bytes_to_num(keyBlock
, 6); 
 750                                                 PrintAndLog("Found valid key:%012"llx
, key64
); 
 751                                                 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1; 
 752                                                 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
; 
 758                 // 20160116 If Sector A is found, but not Sector B,  try just reading it of the tag? 
 759                 PrintAndLog("testing to read B..."); 
 760                 for (i 
= 0; i 
< SectorsCnt
; i
++) { 
 761                         // KEY A  but not KEY B 
 762                         if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) { 
 764                                 uint8_t sectrail 
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1); 
 766                                 PrintAndLog("Reading block %d", sectrail
); 
 768                                 UsbCommand c 
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}}; 
 769                                 num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A 
 770                                 clearCommandBuffer(); 
 774                                 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue; 
 776                                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 779                                 uint8_t *data 
= resp
.d
.asBytes
; 
 780                                 key64 
= bytes_to_num(data
+10, 6); 
 782                                         PrintAndLog("Data:%s", sprint_hex(data
+10, 6)); 
 783                                         e_sector
[i
].foundKey
[1] = 1; 
 784                                         e_sector
[i
].Key
[1] = key64
; 
 791                         PrintAndLog("Time in nested: %.0f ticks %4.2f sec (%4.2f sec per key)\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
, ((float)t1
)/iterations
/CLOCKS_PER_SEC
); 
 794                 printKeyTable( SectorsCnt
, e_sector 
); 
 796                 // transfer them to the emulator 
 798                         for (i 
= 0; i 
< SectorsCnt
; i
++) { 
 799                                 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1); 
 800                                 if (e_sector
[i
].foundKey
[0]) 
 801                                         num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
); 
 802                                 if (e_sector
[i
].foundKey
[1]) 
 803                                         num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]); 
 804                                 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1); 
 809                 if (createDumpFile
) { 
 810                         if ((fkeys 
= fopen("dumpkeys.bin","wb")) == NULL
) {  
 811                                 PrintAndLog("Could not create file dumpkeys.bin"); 
 815                         PrintAndLog("Printing keys to binary file dumpkeys.bin..."); 
 816                         for(i
=0; i
<SectorsCnt
; i
++) { 
 817                                 if (e_sector
[i
].foundKey
[0]){ 
 818                                         num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
); 
 819                                         fwrite ( tempkey
, 1, 6, fkeys 
); 
 822                                         fwrite ( &standart
, 1, 6, fkeys 
); 
 825                         for(i
=0; i
<SectorsCnt
; i
++) { 
 826                                 if (e_sector
[i
].foundKey
[1]){ 
 827                                         num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
); 
 828                                         fwrite ( tempkey
, 1, 6, fkeys 
); 
 831                                         fwrite ( &standart
, 1, 6, fkeys 
); 
 842 int CmdHF14AMfNestedHard(const char *Cmd
) 
 846         uint8_t trgBlockNo 
= 0; 
 847         uint8_t trgKeyType 
= 0; 
 848         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 849         uint8_t trgkey
[6] = {0, 0, 0, 0, 0, 0}; 
 852         ctmp 
= param_getchar(Cmd
, 0); 
 854         if (ctmp 
!= 'R' && ctmp 
!= 'r' && ctmp 
!= 'T' && ctmp 
!= 't' && strlen(Cmd
) < 20) { 
 855                 PrintAndLog("Usage:"); 
 856                 PrintAndLog("      hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>"); 
 857                 PrintAndLog("                       <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]"); 
 858                 PrintAndLog("  or  hf mf hardnested r [known target key]"); 
 860                 PrintAndLog("Options: "); 
 861                 PrintAndLog("      w: Acquire nonces and write them to binary file nonces.bin"); 
 862                 PrintAndLog("      s: Slower acquisition (required by some non standard cards)"); 
 863                 PrintAndLog("      r: Read nonces.bin and start attack"); 
 865                 PrintAndLog("      sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A"); 
 866                 PrintAndLog("      sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w"); 
 867                 PrintAndLog("      sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s"); 
 868                 PrintAndLog("      sample4: hf mf hardnested r"); 
 870                 PrintAndLog("Add the known target key to check if it is present in the remaining key space:"); 
 871                 PrintAndLog("      sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF"); 
 875         bool know_target_key 
= false; 
 876         bool nonce_file_read 
= false; 
 877         bool nonce_file_write 
= false; 
 882         if (ctmp 
== 'R' || ctmp 
== 'r') { 
 883                 nonce_file_read 
= true; 
 884                 if (!param_gethex(Cmd
, 1, trgkey
, 12)) { 
 885                         know_target_key 
= true; 
 887         } else if (ctmp 
== 'T' || ctmp 
== 't') { 
 888                 tests 
= param_get32ex(Cmd
, 1, 100, 10); 
 890                 blockNo 
= param_get8(Cmd
, 0); 
 891                 ctmp 
= param_getchar(Cmd
, 1); 
 892                 if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
 893                         PrintAndLog("Key type must be A or B"); 
 896                 if (ctmp 
!= 'A' && ctmp 
!= 'a') {  
 900                 if (param_gethex(Cmd
, 2, key
, 12)) { 
 901                         PrintAndLog("Key must include 12 HEX symbols"); 
 905                 trgBlockNo 
= param_get8(Cmd
, 3); 
 906                 ctmp 
= param_getchar(Cmd
, 4); 
 907                 if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
 908                         PrintAndLog("Target key type must be A or B"); 
 911                 if (ctmp 
!= 'A' && ctmp 
!= 'a') { 
 917                 if (!param_gethex(Cmd
, 5, trgkey
, 12)) { 
 918                         know_target_key 
= true; 
 922                 while ((ctmp 
= param_getchar(Cmd
, i
))) { 
 923                         if (ctmp 
== 's' || ctmp 
== 'S') { 
 925                         } else if (ctmp 
== 'w' || ctmp 
== 'W') { 
 926                                 nonce_file_write 
= true; 
 928                                 PrintAndLog("Possible options are w and/or s"); 
 935         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 ",  
 938                         trgkey
[0], trgkey
[1], trgkey
[2], trgkey
[3], trgkey
[4], trgkey
[5], 
 939                         know_target_key
?"":" (not set)", 
 940                         nonce_file_write
?"write":nonce_file_read
?"read":"none", 
 944         int16_t isOK 
= mfnestedhard(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
?trgkey
:NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
); 
 948                         case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break; 
 949                         case 2 : PrintAndLog("Button pressed. Aborted.\n"); break; 
 958 int CmdHF14AMfChk(const char *Cmd
) 
 961                 PrintAndLog("Usage:  hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]"); 
 962                 PrintAndLog("          * - all sectors"); 
 963                 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K"); 
 964                 PrintAndLog("d - write keys to binary file"); 
 965                 PrintAndLog("t - write keys to emulator memory\n"); 
 966                 PrintAndLog("      sample: hf mf chk 0 A 1234567890ab keys.dic"); 
 967                 PrintAndLog("              hf mf chk *1 ? t"); 
 968                 PrintAndLog("              hf mf chk *1 ? d"); 
 973         char filename
[FILE_PATH_SIZE
]={0}; 
 975         uint8_t *keyBlock 
= NULL
, *p
; 
 976         uint8_t stKeyBlock 
= 20; 
 978         sector 
*e_sector 
= NULL
; 
 984         uint8_t SectorsCnt 
= 1; 
 988         uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
 990         int transferToEml 
= 0; 
 991         int createDumpFile 
= 0; 
 993         keyBlock 
= calloc(stKeyBlock
, 6); 
 994         if (keyBlock 
== NULL
) return 1; 
 996         uint64_t defaultKeys
[] = 
 998                 0xffffffffffff, // Default key (first key used by program if no user defined key) 
 999                 0x000000000000, // Blank key 
1000                 0xa0a1a2a3a4a5, // NFCForum MAD key 
1012         int defaultKeysSize 
= sizeof(defaultKeys
) / sizeof(uint64_t); 
1014         for (int defaultKeyCounter 
= 0; defaultKeyCounter 
< defaultKeysSize
; defaultKeyCounter
++) 
1016                 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock 
+ defaultKeyCounter 
* 6)); 
1019         if (param_getchar(Cmd
, 0)=='*') { 
1021                 switch(param_getchar(Cmd
+1, 0)) { 
1022                         case '0': SectorsCnt 
=  5; break; 
1023                         case '1': SectorsCnt 
= 16; break; 
1024                         case '2': SectorsCnt 
= 32; break; 
1025                         case '4': SectorsCnt 
= 40; break; 
1026                         default:  SectorsCnt 
= 16; 
1030                 blockNo 
= param_get8(Cmd
, 0); 
1032         ctmp 
= param_getchar(Cmd
, 1); 
1044                 PrintAndLog("Key type must be A , B or ?"); 
1049         ctmp 
= param_getchar(Cmd
, 2); 
1050         if              (ctmp 
== 't' || ctmp 
== 'T') transferToEml 
= 1; 
1051         else if (ctmp 
== 'd' || ctmp 
== 'D') createDumpFile 
= 1; 
1053         for (i 
= transferToEml 
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) { 
1054                 if (!param_gethex(Cmd
, 2 + i
, keyBlock 
+ 6 * keycnt
, 12)) { 
1055                         if ( stKeyBlock 
- keycnt 
< 2) { 
1056                                 p 
= realloc(keyBlock
, 6*(stKeyBlock
+=10)); 
1058                                         PrintAndLog("Cannot allocate memory for Keys"); 
1064                         PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
, 
1065                         (keyBlock 
+ 6*keycnt
)[0],(keyBlock 
+ 6*keycnt
)[1], (keyBlock 
+ 6*keycnt
)[2], 
1066                         (keyBlock 
+ 6*keycnt
)[3], (keyBlock 
+ 6*keycnt
)[4],     (keyBlock 
+ 6*keycnt
)[5], 6); 
1069                         // May be a dic file 
1070                         if ( param_getstr(Cmd
, 2 + i
,filename
) >= FILE_PATH_SIZE 
) { 
1071                                 PrintAndLog("File name too long"); 
1076                         if ( (f 
= fopen( filename 
, "r")) ) { 
1077                                 while( fgets(buf
, sizeof(buf
), f
) ){ 
1078                                         if (strlen(buf
) < 12 || buf
[11] == '\n') 
1081                                         while (fgetc(f
) != '\n' && !feof(f
)) ;  //goto next line 
1083                                         if( buf
[0]=='#' ) continue;     //The line start with # is comment, skip 
1085                                         if (!isxdigit(buf
[0])){ 
1086                                                 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
); 
1092                                         if ( stKeyBlock 
- keycnt 
< 2) { 
1093                                                 p 
= realloc(keyBlock
, 6*(stKeyBlock
+=10)); 
1095                                                         PrintAndLog("Cannot allocate memory for defKeys"); 
1102                                         memset(keyBlock 
+ 6 * keycnt
, 0, 6); 
1103                                         num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock 
+ 6*keycnt
); 
1104                                         PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock 
+ 6*keycnt
, 6)); 
1106                                         memset(buf
, 0, sizeof(buf
)); 
1110                                 PrintAndLog("File: %s: not found or locked.", filename
); 
1119                 PrintAndLog("No key specified, trying default keys"); 
1120                 for (;keycnt 
< defaultKeysSize
; keycnt
++) 
1121                         PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
, 
1122                                 (keyBlock 
+ 6*keycnt
)[0],(keyBlock 
+ 6*keycnt
)[1], (keyBlock 
+ 6*keycnt
)[2], 
1123                                 (keyBlock 
+ 6*keycnt
)[3], (keyBlock 
+ 6*keycnt
)[4],     (keyBlock 
+ 6*keycnt
)[5], 6); 
1126         // initialize storage for found keys 
1127         e_sector 
= calloc(SectorsCnt
, sizeof(sector
)); 
1128         if (e_sector 
== NULL
) { 
1133         uint8_t trgKeyType 
= 0; 
1136         clock_t t1 
= clock(); 
1139         for (trgKeyType 
= 0; trgKeyType 
< 2; ++trgKeyType
) { 
1141                 for (int i 
= 0; i 
< SectorsCnt
; ++i
) { 
1143                         // skip already found keys. 
1144                         if (e_sector
[i
].foundKey
[trgKeyType
]) continue; 
1146                         PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, trgKeyType 
? 'B':'A', keycnt
); 
1148                         uint32_t max_keys 
= keycnt 
> (USB_CMD_DATA_SIZE
/6) ? (USB_CMD_DATA_SIZE
/6) : keycnt
; 
1150                         for (uint32_t c 
= 0; c 
< keycnt
; c 
+= max_keys
) { 
1152                                 uint32_t size 
= keycnt
-c 
> max_keys 
? max_keys 
: keycnt
-c
; 
1154                                 res 
= mfCheckKeys(b
, trgKeyType
, true, size
, &keyBlock
[6*c
], &key64
); 
1156                                         PrintAndLog("Found valid key:[%012"llx
"]",key64
);                                        
1157                                         e_sector
[i
].Key
[trgKeyType
] = key64
; 
1158                                         e_sector
[i
].foundKey
[trgKeyType
] = TRUE
; 
1161                                         e_sector
[i
].Key
[trgKeyType
] = 0xffffffffffff; 
1162                                         e_sector
[i
].foundKey
[trgKeyType
] = FALSE
; 
1165                         b 
< 127 ? ( b 
+=4 ) : ( b 
+= 16 );       
1168         // 20160116 If Sector A is found, but not Sector B,  try just reading it of the tag? 
1169         PrintAndLog("testing to read B..."); 
1170         for (i 
= 0; i 
< SectorsCnt
; i
++) { 
1171                 // KEY A  but not KEY B 
1172                 if ( e_sector
[i
].foundKey
[0] && !e_sector
[i
].foundKey
[1] ) { 
1174                         uint8_t sectrail 
= (FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1); 
1176                         PrintAndLog("Reading block %d", sectrail
); 
1178                         UsbCommand c 
= {CMD_MIFARE_READBL
, {sectrail
, 0, 0}}; 
1179                         num_to_bytes(e_sector
[i
].Key
[0], 6, c
.d
.asBytes
); // KEY A 
1180                         clearCommandBuffer(); 
1184                         if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) continue; 
1186                         uint8_t isOK  
= resp
.arg
[0] & 0xff; 
1187                         if (!isOK
) continue; 
1189                         uint8_t *data 
= resp
.d
.asBytes
; 
1190                         key64 
= bytes_to_num(data
+10, 6); 
1192                                 PrintAndLog("Data:%s", sprint_hex(data
+10, 6)); 
1193                                 e_sector
[i
].foundKey
[1] = 1; 
1194                                 e_sector
[i
].Key
[1] = key64
; 
1201                 printf("Time in checkkeys: %.0f ticks  %1.2f sec (%1.2f sec per key)\n\n", (float)t1
, ((float)t1
)/CLOCKS_PER_SEC
, ((float)t1
)/keycnt
/CLOCKS_PER_SEC
); 
1204         printKeyTable( SectorsCnt
, e_sector 
); 
1206         if (transferToEml
) { 
1207                 uint8_t block
[16] = {0x00}; 
1208                 for (uint8_t i 
= 0; i 
< SectorsCnt
; ++i 
) { 
1209                         mfEmlGetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1); 
1210                         if (e_sector
[i
].foundKey
[0]) 
1211                                 num_to_bytes(e_sector
[i
].Key
[0], 6, block
); 
1212                         if (e_sector
[i
].foundKey
[1]) 
1213                                 num_to_bytes(e_sector
[i
].Key
[1], 6, block
+10); 
1214                         mfEmlSetMem(block
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1); 
1216                 PrintAndLog("Found keys have been transferred to the emulator memory"); 
1219         if (createDumpFile
) { 
1220                 FILE *fkeys 
= fopen("dumpkeys.bin","wb"); 
1221                 if (fkeys 
== NULL
) {  
1222                         PrintAndLog("Could not create file dumpkeys.bin"); 
1227                 PrintAndLog("Printing keys to binary file dumpkeys.bin..."); 
1229                 for( i
=0; i
<SectorsCnt
; i
++) { 
1230                         num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
); 
1231                         fwrite ( tempkey
, 1, 6, fkeys 
); 
1233                 for(i
=0; i
<SectorsCnt
; i
++) { 
1234                         num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
); 
1235                         fwrite ( tempkey
, 1, 6, fkeys 
); 
1238                 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");                     
1247 void printKeyTable( uint8_t sectorscnt
, sector 
*e_sector 
){ 
1248         PrintAndLog("|---|----------------|---|----------------|---|"); 
1249         PrintAndLog("|sec|key A           |res|key B           |res|"); 
1250         PrintAndLog("|---|----------------|---|----------------|---|"); 
1251         for (uint8_t i 
= 0; i 
< sectorscnt
; ++i
) { 
1252                 PrintAndLog("|%03d|  %012"llx
"  | %d |  %012"llx
"  | %d |", i
, 
1253                         e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0],  
1254                         e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1] 
1257         PrintAndLog("|---|----------------|---|----------------|---|"); 
1260 int CmdHF14AMf1kSim(const char *Cmd
) 
1262         uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0}; 
1263         uint8_t exitAfterNReads 
= 0; 
1266         uint8_t cmdp 
= param_getchar(Cmd
, 0); 
1268         if (cmdp 
== 'h' || cmdp 
== 'H') { 
1269                 PrintAndLog("Usage:  hf mf sim  u <uid (8 hex symbols)> n <numreads> i x"); 
1270                 PrintAndLog("           h    this help"); 
1271                 PrintAndLog("           u    (Optional) UID. If not specified, the UID from emulator memory will be used"); 
1272                 PrintAndLog("           n    (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite"); 
1273                 PrintAndLog("           i    (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted"); 
1274                 PrintAndLog("           x    (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)"); 
1276                 PrintAndLog("           sample: hf mf sim u 0a0a0a0a "); 
1280         if (param_getchar(Cmd
, pnr
) == 'u') { 
1281                 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0) 
1283                         flags 
|= FLAG_4B_UID_IN_DATA
; // UID from packet 
1284                 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) { 
1285                         flags 
|= FLAG_7B_UID_IN_DATA
;// UID from packet 
1287                         PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols"); 
1293         if (param_getchar(Cmd
, pnr
) == 'n') { 
1294                 exitAfterNReads 
= param_get8(Cmd
,pnr
+1); 
1298         if (param_getchar(Cmd
, pnr
) == 'i' ) { 
1299                 //Using a flag to signal interactiveness, least significant bit 
1300                 flags 
|= FLAG_INTERACTIVE
; 
1304         if (param_getchar(Cmd
, pnr
) == 'x' ) { 
1305                 //Using a flag to signal interactiveness, least significant bit 
1306                 flags 
|= FLAG_NR_AR_ATTACK
; 
1309         PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ", 
1310                                 flags 
& FLAG_4B_UID_IN_DATA 
? sprint_hex(uid
,4): 
1311                                                                                           flags 
& FLAG_7B_UID_IN_DATA   
? sprint_hex(uid
,7): "N/A" 
1317         UsbCommand c 
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}}; 
1318         memcpy(c
.d
.asBytes
, uid
, sizeof(uid
)); 
1319         clearCommandBuffer(); 
1322         if(flags 
& FLAG_INTERACTIVE
) 
1328                 PrintAndLog("Press pm3-button or send another cmd to abort simulation"); 
1331                         if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) continue; 
1333                         if ( !(flags 
& FLAG_NR_AR_ATTACK
) ) break; 
1335                         if ( (resp
.arg
[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD 
) break; 
1337                                 memset(data
, 0x00, sizeof(data
)); 
1338                                 memset(key
, 0x00, sizeof(key
)); 
1339                                 int len 
= (resp
.arg
[1] > sizeof(data
)) ? sizeof(data
) : resp
.arg
[1]; 
1341                                 memcpy(data
, resp
.d
.asBytes
, len
); 
1343                                 uint64_t corr_uid 
= 0; 
1345                                 // this IF?  what was I thinking of? 
1346                                 if ( memcmp(data
, "\x00\x00\x00\x00", 4) == 0 ) { 
1347                                         corr_uid 
= ((uint64_t)(data
[3] << 24)) | (data
[2] << 16) | (data
[1] << 8) | data
[0]; 
1348                                         tryMfk32(corr_uid
, data
, key
); 
1350                                         corr_uid 
|= (uint64_t)data
[2] << 48;  
1351                                         corr_uid 
|= (uint64_t)data
[1] << 40;  
1352                                         corr_uid 
|= (uint64_t)data
[0] << 32; 
1353                                         corr_uid 
|= (uint64_t)data
[7] << 24; 
1354                                         corr_uid 
|= (uint64_t)data
[6] << 16; 
1355                                         corr_uid 
|= (uint64_t)data
[5] << 8; 
1356                                         corr_uid 
|= (uint64_t)data
[4]; 
1357                                         tryMfk64(corr_uid
, data
, key
); 
1365 int CmdHF14AMfDbg(const char *Cmd
) 
1367         int dbgMode 
= param_get32ex(Cmd
, 0, 0, 10); 
1369                 PrintAndLog("Max debug mode parameter is 4 \n"); 
1372         if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode 
> 4) { 
1373                 PrintAndLog("Usage:  hf mf dbg  <debug level>"); 
1374                 PrintAndLog(" 0 - no debug messages"); 
1375                 PrintAndLog(" 1 - error messages"); 
1376                 PrintAndLog(" 2 - plus information messages"); 
1377                 PrintAndLog(" 3 - plus debug messages"); 
1378                 PrintAndLog(" 4 - print even debug messages in timing critical functions"); 
1379                 PrintAndLog("     Note: this option therefore may cause malfunction itself"); 
1383         UsbCommand c 
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}}; 
1389 int CmdHF14AMfEGet(const char *Cmd
) 
1391         uint8_t blockNo 
= 0; 
1392         uint8_t data
[16] = {0x00}; 
1394         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1395                 PrintAndLog("Usage:  hf mf eget <block number>"); 
1396                 PrintAndLog(" sample: hf mf eget 0 "); 
1400         blockNo 
= param_get8(Cmd
, 0); 
1403         if (!mfEmlGetMem(data
, blockNo
, 1)) { 
1404                 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16)); 
1406                 PrintAndLog("Command execute timeout"); 
1412 int CmdHF14AMfEClear(const char *Cmd
) 
1414         if (param_getchar(Cmd
, 0) == 'h') { 
1415                 PrintAndLog("Usage:  hf mf eclr"); 
1416                 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n"); 
1420         UsbCommand c 
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}}; 
1425 int CmdHF14AMfESet(const char *Cmd
) 
1427         uint8_t memBlock
[16]; 
1428         uint8_t blockNo 
= 0; 
1430         memset(memBlock
, 0x00, sizeof(memBlock
)); 
1432         if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') { 
1433                 PrintAndLog("Usage:  hf mf eset <block number> <block data (32 hex symbols)>"); 
1434                 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f "); 
1438         blockNo 
= param_get8(Cmd
, 0); 
1440         if (param_gethex(Cmd
, 1, memBlock
, 32)) { 
1441                 PrintAndLog("block data must include 32 HEX symbols"); 
1446         UsbCommand c 
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}}; 
1447         memcpy(c
.d
.asBytes
, memBlock
, 16); 
1452 int CmdHF14AMfELoad(const char *Cmd
) 
1455         char filename
[FILE_PATH_SIZE
]; 
1456         char *fnameptr 
= filename
; 
1457         char buf
[64] = {0x00}; 
1458         uint8_t buf8
[64] = {0x00}; 
1459         int i
, len
, blockNum
, numBlocks
; 
1460         int nameParamNo 
= 1; 
1461         uint8_t blockWidth 
= 32; 
1462         char ctmp 
= param_getchar(Cmd
, 0); 
1464         if ( ctmp 
== 'h' || ctmp 
== 'H' || ctmp 
== 0x00) { 
1465                 PrintAndLog("It loads emul dump from the file `filename.eml`"); 
1466                 PrintAndLog("Usage:  hf mf eload [card memory] <file name w/o `.eml`> [numblocks]"); 
1467                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL"); 
1469                 PrintAndLog(" sample: hf mf eload filename"); 
1470                 PrintAndLog("         hf mf eload 4 filename"); 
1475                 case '0' : numBlocks 
= 5*4; break; 
1477                 case '\0': numBlocks 
= 16*4; break; 
1478                 case '2' : numBlocks 
= 32*4; break; 
1479                 case '4' : numBlocks 
= 256; break; 
1480                 case 'U' : // fall through 
1481                 case 'u' : numBlocks 
= 255; blockWidth 
= 8; break; 
1487         uint32_t numblk2 
= param_get32ex(Cmd
,2,0,10); 
1488         if (numblk2 
> 0) numBlocks 
= numblk2
;    
1490         len 
= param_getstr(Cmd
,nameParamNo
,filename
); 
1492         if (len 
> FILE_PATH_SIZE 
- 5) len 
= FILE_PATH_SIZE 
- 5; 
1496         sprintf(fnameptr
, ".eml");  
1499         f 
= fopen(filename
, "r"); 
1501                 PrintAndLog("File %s not found or locked", filename
); 
1507                 memset(buf
, 0, sizeof(buf
)); 
1509                 if (fgets(buf
, sizeof(buf
), f
) == NULL
) { 
1511                         if (blockNum 
>= numBlocks
) break; 
1513                         PrintAndLog("File reading error."); 
1518                 if (strlen(buf
) < blockWidth
){ 
1519                         if(strlen(buf
) && feof(f
)) 
1521                         PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth
); 
1526                 for (i 
= 0; i 
< blockWidth
; i 
+= 2) { 
1527                         sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i 
/ 2]); 
1529                 if (mfEmlSetMem_xt(buf8
, blockNum
, 1, blockWidth
/2)) { 
1530                         PrintAndLog("Cant set emul block: %3d", blockNum
); 
1537                 if (blockNum 
>= numBlocks
) break; 
1542         if ((blockNum 
!= numBlocks
)) { 
1543                 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum
, numBlocks
); 
1546         PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
); 
1550 int CmdHF14AMfESave(const char *Cmd
) 
1553         char filename
[FILE_PATH_SIZE
]; 
1554         char * fnameptr 
= filename
; 
1556         int i
, j
, len
, numBlocks
; 
1557         int nameParamNo 
= 1; 
1559         memset(filename
, 0, sizeof(filename
)); 
1560         memset(buf
, 0, sizeof(buf
)); 
1562         char ctmp 
= param_getchar(Cmd
, 0); 
1564         if ( ctmp 
== 'h' || ctmp 
== 'H') { 
1565                 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`"); 
1566                 PrintAndLog(" Usage:  hf mf esave [card memory] [file name w/o `.eml`]"); 
1567                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
1569                 PrintAndLog(" sample: hf mf esave "); 
1570                 PrintAndLog("         hf mf esave 4"); 
1571                 PrintAndLog("         hf mf esave 4 filename"); 
1576                 case '0' : numBlocks 
= 5*4; break; 
1578                 case '\0': numBlocks 
= 16*4; break; 
1579                 case '2' : numBlocks 
= 32*4; break; 
1580                 case '4' : numBlocks 
= 256; break; 
1587         len 
= param_getstr(Cmd
,nameParamNo
,filename
); 
1589         if (len 
> FILE_PATH_SIZE 
- 5) len 
= FILE_PATH_SIZE 
- 5; 
1591         // user supplied filename? 
1593                 // get filename (UID from memory) 
1594                 if (mfEmlGetMem(buf
, 0, 1)) { 
1595                         PrintAndLog("Can\'t get UID from block: %d", 0); 
1596                         len 
= sprintf(fnameptr
, "dump"); 
1600                         for (j 
= 0; j 
< 7; j
++, fnameptr 
+= 2) 
1601                                 sprintf(fnameptr
, "%02X", buf
[j
]); 
1607         // add file extension 
1608         sprintf(fnameptr
, ".eml");  
1611         f 
= fopen(filename
, "w+"); 
1614                 PrintAndLog("Can't open file %s ", filename
); 
1619         for (i 
= 0; i 
< numBlocks
; i
++) { 
1620                 if (mfEmlGetMem(buf
, i
, 1)) { 
1621                         PrintAndLog("Cant get block: %d", i
); 
1624                 for (j 
= 0; j 
< 16; j
++) 
1625                         fprintf(f
, "%02X", buf
[j
]);  
1630         PrintAndLog("Saved %d blocks to file: %s", numBlocks
, filename
); 
1635 int CmdHF14AMfECFill(const char *Cmd
) 
1637         uint8_t keyType 
= 0; 
1638         uint8_t numSectors 
= 16; 
1640         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1641                 PrintAndLog("Usage:  hf mf ecfill <key A/B> [card memory]"); 
1642                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
1644                 PrintAndLog("samples:  hf mf ecfill A"); 
1645                 PrintAndLog("          hf mf ecfill A 4"); 
1646                 PrintAndLog("Read card and transfer its data to emulator memory."); 
1647                 PrintAndLog("Keys must be laid in the emulator memory. \n"); 
1651         char ctmp 
= param_getchar(Cmd
, 0); 
1652         if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
1653                 PrintAndLog("Key type must be A or B"); 
1656         if (ctmp 
!= 'A' && ctmp 
!= 'a') keyType 
= 1; 
1658         ctmp 
= param_getchar(Cmd
, 1); 
1660                 case '0' : numSectors 
= 5; break; 
1662                 case '\0': numSectors 
= 16; break; 
1663                 case '2' : numSectors 
= 32; break; 
1664                 case '4' : numSectors 
= 40; break; 
1665                 default:   numSectors 
= 16; 
1668         printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
); 
1669         UsbCommand c 
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}}; 
1674 int CmdHF14AMfEKeyPrn(const char *Cmd
) 
1679         uint64_t keyA
, keyB
; 
1681         char cmdp 
= param_getchar(Cmd
, 0); 
1683         if ( cmdp 
== 'h' || cmdp 
== 'H' ) { 
1684                 PrintAndLog("It prints the keys loaded in the emulator memory"); 
1685                 PrintAndLog("Usage:  hf mf ekeyprn [card memory]"); 
1686                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
1688                 PrintAndLog(" sample: hf mf ekeyprn 1"); 
1693                 case '0' : numSectors 
= 5; break; 
1695                 case '\0': numSectors 
= 16; break; 
1696                 case '2' : numSectors 
= 32; break; 
1697                 case '4' : numSectors 
= 40; break; 
1698                 default:   numSectors 
= 16; 
1701         PrintAndLog("|---|----------------|----------------|"); 
1702         PrintAndLog("|sec|key A           |key B           |"); 
1703         PrintAndLog("|---|----------------|----------------|"); 
1704         for (i 
= 0; i 
< numSectors
; i
++) { 
1705                 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) { 
1706                         PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1); 
1709                 keyA 
= bytes_to_num(data
, 6); 
1710                 keyB 
= bytes_to_num(data 
+ 10, 6); 
1711                 PrintAndLog("|%03d|  %012"llx
"  |  %012"llx
"  |", i
, keyA
, keyB
); 
1713         PrintAndLog("|---|----------------|----------------|"); 
1718 int CmdHF14AMfCSetUID(const char *Cmd
) 
1720         uint8_t wipeCard 
= 0; 
1721         uint8_t uid
[8] = {0x00}; 
1722         uint8_t oldUid
[8] = {0x00}; 
1723         uint8_t atqa
[2] = {0x00}; 
1724         uint8_t sak
[1] = {0x00}; 
1725         uint8_t atqaPresent 
= 1; 
1730         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, argi
) == 'h') { 
1731                 PrintAndLog("Usage:  hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]"); 
1732                 PrintAndLog("sample:  hf mf csetuid 01020304"); 
1733                 PrintAndLog("sample:  hf mf csetuid 01020304 0004 08 w"); 
1734                 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)"); 
1735                 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line."); 
1739         if (param_getchar(Cmd
, argi
) && param_gethex(Cmd
, argi
, uid
, 8)) { 
1740                 PrintAndLog("UID must include 8 HEX symbols"); 
1745         ctmp 
= param_getchar(Cmd
, argi
); 
1746         if (ctmp 
== 'w' || ctmp 
== 'W') { 
1752                 if (param_getchar(Cmd
, argi
)) { 
1753                         if (param_gethex(Cmd
, argi
, atqa
, 4)) { 
1754                                 PrintAndLog("ATQA must include 4 HEX symbols"); 
1758                         if (!param_getchar(Cmd
, argi
) || param_gethex(Cmd
, argi
, sak
, 2)) { 
1759                                 PrintAndLog("SAK must include 2 HEX symbols"); 
1768                 ctmp 
= param_getchar(Cmd
, argi
); 
1769                 if (ctmp 
== 'w' || ctmp 
== 'W') { 
1774         PrintAndLog("--wipe card:%s  uid:%s", (wipeCard
)?"YES":"NO", sprint_hex(uid
, 4)); 
1776         res 
= mfCSetUID(uid
, (atqaPresent
)?atqa
:NULL
, (atqaPresent
)?sak
:NULL
, oldUid
, wipeCard
); 
1778                         PrintAndLog("Can't set UID. error=%d", res
); 
1782         PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4)); 
1783         PrintAndLog("new UID:%s", sprint_hex(uid
, 4)); 
1787 int CmdHF14AMfCSetBlk(const char *Cmd
) 
1789         uint8_t block
[16] = {0x00}; 
1790         uint8_t blockNo 
= 0; 
1791         uint8_t params 
= MAGIC_SINGLE
; 
1794         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1795                 PrintAndLog("Usage:  hf mf csetblk <block number> <block data (32 hex symbols)> [w]"); 
1796                 PrintAndLog("sample:  hf mf csetblk 1 01020304050607080910111213141516"); 
1797                 PrintAndLog("Set block data for magic Chinese card (only works with such cards)"); 
1798                 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line"); 
1802         blockNo 
= param_get8(Cmd
, 0); 
1804         if (param_gethex(Cmd
, 1, block
, 32)) { 
1805                 PrintAndLog("block data must include 32 HEX symbols"); 
1809         char ctmp 
= param_getchar(Cmd
, 2); 
1810         if (ctmp 
== 'w' || ctmp 
== 'W') 
1811                 params 
|= MAGIC_WIPE
; 
1813         PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(block
, 16)); 
1815         res 
= mfCSetBlock(blockNo
, block
, NULL
, params
); 
1817                 PrintAndLog("Can't write block. error=%d", res
); 
1823 int CmdHF14AMfCLoad(const char *Cmd
) 
1826         char filename
[FILE_PATH_SIZE
]; 
1827         char * fnameptr 
= filename
; 
1828         char buf
[64] = {0x00}; 
1829         uint8_t buf8
[64] = {0x00}; 
1830         uint8_t fillFromEmulator 
= 0; 
1831         int i
, len
, blockNum
, flags
=0; 
1833         memset(filename
, 0, sizeof(filename
)); 
1835         char ctmp 
= param_getchar(Cmd
, 0); 
1837         if (ctmp 
== 'h' || ctmp 
== 'H' || ctmp 
== 0x00) { 
1838                 PrintAndLog("It loads magic Chinese card from the file `filename.eml`"); 
1839                 PrintAndLog("or from emulator memory (option `e`)"); 
1840                 PrintAndLog("Usage:  hf mf cload <file name w/o `.eml`>"); 
1841                 PrintAndLog("   or:  hf mf cload e "); 
1842                 PrintAndLog(" sample: hf mf cload filename"); 
1846         if (ctmp 
== 'e' || ctmp 
== 'E') fillFromEmulator 
= 1; 
1848         if (fillFromEmulator
) { 
1849                 for (blockNum 
= 0; blockNum 
< 16 * 4; blockNum 
+= 1) { 
1850                         if (mfEmlGetMem(buf8
, blockNum
, 1)) { 
1851                                 PrintAndLog("Cant get block: %d", blockNum
); 
1854                         if (blockNum 
== 0) flags 
= MAGIC_INIT 
+ MAGIC_WUPC
;                             // switch on field and send magic sequence 
1855                         if (blockNum 
== 1) flags 
= 0;                                                                                                   // just write 
1856                         if (blockNum 
== 16 * 4 - 1) flags 
= MAGIC_HALT 
+ MAGIC_OFF
;             // Done. Magic Halt and switch off field. 
1858                         if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) { 
1859                                 PrintAndLog("Cant set magic card block: %d", blockNum
); 
1866                 if (len 
> FILE_PATH_SIZE 
- 5) len 
= FILE_PATH_SIZE 
- 5; 
1868                 memcpy(filename
, Cmd
, len
); 
1871                 sprintf(fnameptr
, ".eml");  
1874                 f 
= fopen(filename
, "r"); 
1876                         PrintAndLog("File not found or locked."); 
1883                         memset(buf
, 0, sizeof(buf
)); 
1885                         if (fgets(buf
, sizeof(buf
), f
) == NULL
) { 
1887                                 PrintAndLog("File reading error."); 
1891                         if (strlen(buf
) < 32) { 
1892                                 if(strlen(buf
) && feof(f
)) 
1894                                 PrintAndLog("File content error. Block data must include 32 HEX symbols"); 
1898                         for (i 
= 0; i 
< 32; i 
+= 2) 
1899                                 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i 
/ 2]); 
1901                         if (blockNum 
== 0) flags 
= MAGIC_INIT 
+ MAGIC_WUPC
;                             // switch on field and send magic sequence 
1902                         if (blockNum 
== 1) flags 
= 0;                                                                                                   // just write 
1903                         if (blockNum 
== 16 * 4 - 1) flags 
= MAGIC_HALT 
+ MAGIC_OFF
;             // Done. Switch off field. 
1905                         if (mfCSetBlock(blockNum
, buf8
, NULL
, flags
)) { 
1906                                 PrintAndLog("Can't set magic card block: %d", blockNum
); 
1912                         if (blockNum 
>= 16 * 4) break;  // magic card type - mifare 1K 
1917                 if (blockNum 
!= 16 * 4 && blockNum 
!= 32 * 4 + 8 * 16){ 
1918                         PrintAndLog("File content error. There must be 64 blocks"); 
1921                 PrintAndLog("Loaded from file: %s", filename
); 
1927 int CmdHF14AMfCGetBlk(const char *Cmd
) { 
1929         uint8_t blockNo 
= 0; 
1931         memset(data
, 0x00, sizeof(data
)); 
1932         char ctmp 
= param_getchar(Cmd
, 0); 
1934         if (strlen(Cmd
) < 1 || ctmp 
== 'h' || ctmp 
== 'H') { 
1935                 PrintAndLog("Usage:  hf mf cgetblk <block number>"); 
1936                 PrintAndLog("sample:  hf mf cgetblk 1"); 
1937                 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n"); 
1941         blockNo 
= param_get8(Cmd
, 0); 
1943         PrintAndLog("--block number:%2d ", blockNo
); 
1945         res 
= mfCGetBlock(blockNo
, data
, MAGIC_SINGLE
); 
1947                 PrintAndLog("Can't read block. error=%d", res
); 
1951         PrintAndLog("data: %s", sprint_hex(data
, sizeof(data
))); 
1955 int CmdHF14AMfCGetSc(const char *Cmd
) { 
1957         uint8_t sectorNo 
= 0; 
1959         memset(data
, 0x00, sizeof(data
)); 
1960         char ctmp 
= param_getchar(Cmd
, 0); 
1962         if (strlen(Cmd
) < 1 || ctmp 
== 'h' || ctmp 
== 'H') { 
1963                 PrintAndLog("Usage:  hf mf cgetsc <sector number>"); 
1964                 PrintAndLog("sample:  hf mf cgetsc 0"); 
1965                 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n"); 
1969         sectorNo 
= param_get8(Cmd
, 0); 
1970         if (sectorNo 
> 15) { 
1971                 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic."); 
1975         PrintAndLog("--sector number:%d ", sectorNo
); 
1976         PrintAndLog("block | data"); 
1978         flags 
= MAGIC_INIT 
+ MAGIC_WUPC
; 
1979         for (i 
= 0; i 
< 4; i
++) { 
1980                 if (i 
== 1) flags 
= 0; 
1981                 if (i 
== 3) flags 
= MAGIC_HALT 
+ MAGIC_OFF
; 
1983                 res 
= mfCGetBlock(sectorNo 
* 4 + i
, data
, flags
); 
1985                         PrintAndLog("Can't read block. %d error=%d", sectorNo 
* 4 + i
, res
); 
1988                 PrintAndLog(" %3d | %s", sectorNo 
* 4 + i
, sprint_hex(data
, sizeof(data
))); 
1993 int CmdHF14AMfCSave(const char *Cmd
) { 
1996         char filename
[FILE_PATH_SIZE
]; 
1997         char * fnameptr 
= filename
; 
1998         uint8_t fillFromEmulator 
= 0; 
2000         int i
, j
, len
, flags
; 
2002         memset(filename
, 0, sizeof(filename
)); 
2003         memset(buf
, 0, sizeof(buf
)); 
2004         char ctmp 
= param_getchar(Cmd
, 0); 
2006         if ( ctmp 
== 'h' || ctmp 
== 'H' ) { 
2007                 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`"); 
2008                 PrintAndLog("or into emulator memory (option `e`)"); 
2009                 PrintAndLog("Usage:  hf mf esave [file name w/o `.eml`][e]"); 
2010                 PrintAndLog(" sample: hf mf esave "); 
2011                 PrintAndLog("         hf mf esave filename"); 
2012                 PrintAndLog("         hf mf esave e \n"); 
2015         if (ctmp 
== 'e' || ctmp 
== 'E') fillFromEmulator 
= 1; 
2017         if (fillFromEmulator
) { 
2018                 // put into emulator 
2019                 flags 
= MAGIC_INIT 
+ MAGIC_WUPC
; 
2020                 for (i 
= 0; i 
< 16 * 4; i
++) { 
2021                         if (i 
== 1) flags 
= 0; 
2022                         if (i 
== 16 * 4 - 1) flags 
= MAGIC_HALT 
+ MAGIC_OFF
; 
2024                         if (mfCGetBlock(i
, buf
, flags
)) { 
2025                                 PrintAndLog("Cant get block: %d", i
); 
2029                         if (mfEmlSetMem(buf
, i
, 1)) { 
2030                                 PrintAndLog("Cant set emul block: %d", i
); 
2037                 if (len 
> FILE_PATH_SIZE 
- 5) len 
= FILE_PATH_SIZE 
- 5; 
2039                 // get filename based on UID 
2042                         if (mfCGetBlock(0, buf
, MAGIC_SINGLE
)) { 
2043                                 PrintAndLog("Cant get block: %d", 0); 
2044                                 len 
= sprintf(fnameptr
, "dump"); 
2047                                 for (j 
= 0; j 
< 7; j
++, fnameptr 
+= 2) 
2048                                         sprintf(fnameptr
, "%02x", buf
[j
]);  
2051                         memcpy(filename
, Cmd
, len
); 
2055                 // add .eml extension 
2056                 sprintf(fnameptr
, ".eml");  
2059                 f 
= fopen(filename
, "w+"); 
2062                         PrintAndLog("File not found or locked."); 
2067                 flags 
= MAGIC_INIT 
+ MAGIC_WUPC
; 
2068                 for (i 
= 0; i 
< 16 * 4; i
++) { 
2069                         if (i 
== 1) flags 
= 0; 
2070                         if (i 
== 16 * 4 - 1) flags 
= MAGIC_HALT 
+ MAGIC_OFF
; 
2072                         if (mfCGetBlock(i
, buf
, flags
)) { 
2073                                 PrintAndLog("Cant get block: %d", i
); 
2076                         for (j 
= 0; j 
< 16; j
++) 
2077                                 fprintf(f
, "%02x", buf
[j
]);  
2082                 PrintAndLog("Saved to file: %s", filename
); 
2087 int CmdHF14AMfSniff(const char *Cmd
){ 
2089         bool wantLogToFile 
= 0; 
2090         bool wantDecrypt 
= 0; 
2091         //bool wantSaveToEml = 0; TODO 
2092         bool wantSaveToEmlFile 
= 0; 
2103         uint8_t atqa
[2] = {0x00}; 
2106         uint8_t *buf 
= NULL
; 
2107         uint16_t bufsize 
= 0; 
2108         uint8_t *bufPtr 
= NULL
; 
2110         char ctmp 
= param_getchar(Cmd
, 0); 
2111         if ( ctmp 
== 'h' || ctmp 
== 'H' ) { 
2112                 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file."); 
2113                 PrintAndLog("You can specify:"); 
2114                 PrintAndLog("    l - save encrypted sequence to logfile `uid.log`"); 
2115                 PrintAndLog("    d - decrypt sequence and put it to log file `uid.log`"); 
2116                 PrintAndLog(" n/a   e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory"); 
2117                 PrintAndLog("    f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`"); 
2118                 PrintAndLog("Usage:  hf mf sniff [l][d][e][f]"); 
2119                 PrintAndLog("  sample: hf mf sniff l d e"); 
2123         for (int i 
= 0; i 
< 4; i
++) { 
2124                 ctmp 
= param_getchar(Cmd
, i
); 
2125                 if (ctmp 
== 'l' || ctmp 
== 'L') wantLogToFile 
= true; 
2126                 if (ctmp 
== 'd' || ctmp 
== 'D') wantDecrypt 
= true; 
2127                 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO 
2128                 if (ctmp 
== 'f' || ctmp 
== 'F') wantSaveToEmlFile 
= true; 
2131         printf("-------------------------------------------------------------------------\n"); 
2132         printf("Executing command. \n"); 
2133         printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n"); 
2134         printf("Press the key on pc keyboard to abort the client.\n"); 
2135         printf("-------------------------------------------------------------------------\n"); 
2137         UsbCommand c 
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}}; 
2138         clearCommandBuffer(); 
2146                         tmpchar 
= getchar(); 
2148                         printf("\naborted via keyboard!\n"); 
2153                 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) { 
2154                         res 
= resp
.arg
[0] & 0xff; 
2155                         uint16_t traceLen 
= resp
.arg
[1]; 
2160                                 return 0;                                               // we are done 
2163                         if (res 
== 1) {                                                         // there is (more) data to be transferred 
2164                                 if (pckNum 
== 0) {                                              // first packet, (re)allocate necessary buffer 
2165                                         if (traceLen 
> bufsize
) { 
2167                                                 if (buf 
== NULL
) {                              // not yet allocated 
2168                                                         p 
= malloc(traceLen
); 
2169                                                 } else {                                                // need more memory 
2170                                                         p 
= realloc(buf
, traceLen
); 
2173                                                         PrintAndLog("Cannot allocate memory for trace"); 
2181                                         memset(buf
, 0x00, traceLen
); 
2183                                 if (bufPtr 
== NULL
) { 
2184                                         PrintAndLog("Cannot allocate memory for trace"); 
2188                                 memcpy(bufPtr
, resp
.d
.asBytes
, len
); 
2193                         if (res 
== 2) {                                                         // received all data, start displaying 
2194                                 blockLen 
= bufPtr 
- buf
; 
2197                                 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
); 
2198                                 while (bufPtr 
- buf 
< blockLen
) { 
2199                                         bufPtr 
+= 6;                                            // skip (void) timing information 
2200                                         len 
= *((uint16_t *)bufPtr
); 
2208                                         if ((len 
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff) && (bufPtr
[12] == 0xff) && (bufPtr
[13] == 0xff)) { 
2209                                                 memcpy(uid
, bufPtr 
+ 2, 7); 
2210                                                 memcpy(atqa
, bufPtr 
+ 2 + 7, 2); 
2211                                                 uid_len 
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4; 
2213                                                 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",  
2214                                                         sprint_hex(uid 
+ (7 - uid_len
), uid_len
), 
2218                                                 if (wantLogToFile 
|| wantDecrypt
) { 
2219                                                         FillFileNameByUID(logHexFileName
, uid 
+ (7 - uid_len
), ".log", uid_len
); 
2220                                                         AddLogCurrentDT(logHexFileName
); 
2223                                                         mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
); 
2225                                                 PrintAndLog("%s(%d):%s", isTag 
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
)); 
2227                                                         AddLogHex(logHexFileName
, isTag 
? "TAG: ":"RDR: ", bufPtr
, len
); 
2229                                                         mfTraceDecode(bufPtr
, len
, wantSaveToEmlFile
); 
2233                                         bufPtr 
+= ((len
-1)/8+1);        // ignore parity 
2244 //needs nt, ar, at, Data to decrypt 
2245 int CmdHf14MfDecryptBytes(const char *Cmd
){ 
2248         uint32_t nt     
= param_get32ex(Cmd
,0,0,16); 
2249         uint32_t ar_enc 
= param_get32ex(Cmd
,1,0,16); 
2250         uint32_t at_enc 
= param_get32ex(Cmd
,2,0,16); 
2253         param_gethex_ex(Cmd
, 3, data
, &len
); 
2256         int limit 
= sizeof(data
) / 2; 
2261         return tryDecryptWord( nt
, ar_enc
, at_enc
, data
, len
); 
2264 static command_t CommandTable
[] = { 
2265         {"help",                CmdHelp
,                                1, "This help"}, 
2266         {"dbg",                 CmdHF14AMfDbg
,                  0, "Set default debug mode"}, 
2267         {"rdbl",                CmdHF14AMfRdBl
,                 0, "Read MIFARE classic block"}, 
2268         {"rdsc",                CmdHF14AMfRdSc
,                 0, "Read MIFARE classic sector"}, 
2269         {"dump",                CmdHF14AMfDump
,                 0, "Dump MIFARE classic tag to binary file"}, 
2270         {"restore",             CmdHF14AMfRestore
,              0, "Restore MIFARE classic binary file to BLANK tag"}, 
2271         {"wrbl",                CmdHF14AMfWrBl
,                 0, "Write MIFARE classic block"}, 
2272         {"chk",                 CmdHF14AMfChk
,                  0, "Test block keys"}, 
2273         {"mifare",              CmdHF14AMifare
,                 0, "Read parity error messages."}, 
2274         {"nested",              CmdHF14AMfNested
,               0, "Test nested authentication"}, 
2275         {"hardnested",  CmdHF14AMfNestedHard
,   0, "Nested attack for hardened Mifare cards"}, 
2276         {"sniff",               CmdHF14AMfSniff
,                0, "Sniff card-reader communication"}, 
2277         {"sim",                 CmdHF14AMf1kSim
,                0, "Simulate MIFARE card"}, 
2278         {"eclr",                CmdHF14AMfEClear
,               0, "Clear simulator memory block"}, 
2279         {"eget",                CmdHF14AMfEGet
,                 0, "Get simulator memory block"}, 
2280         {"eset",                CmdHF14AMfESet
,                 0, "Set simulator memory block"}, 
2281         {"eload",               CmdHF14AMfELoad
,                0, "Load from file emul dump"}, 
2282         {"esave",               CmdHF14AMfESave
,                0, "Save to file emul dump"}, 
2283         {"ecfill",              CmdHF14AMfECFill
,               0, "Fill simulator memory with help of keys from simulator"}, 
2284         {"ekeyprn",             CmdHF14AMfEKeyPrn
,              0, "Print keys from simulator memory"}, 
2285         {"csetuid",             CmdHF14AMfCSetUID
,              0, "Set UID for magic Chinese card"}, 
2286         {"csetblk",             CmdHF14AMfCSetBlk
,              0, "Write block - Magic Chinese card"}, 
2287         {"cgetblk",             CmdHF14AMfCGetBlk
,              0, "Read block - Magic Chinese card"}, 
2288         {"cgetsc",              CmdHF14AMfCGetSc
,               0, "Read sector - Magic Chinese card"}, 
2289         {"cload",               CmdHF14AMfCLoad
,                0, "Load dump into magic Chinese card"}, 
2290         {"csave",               CmdHF14AMfCSave
,                0, "Save dump from magic Chinese card into file or emulator"}, 
2291         {"decrypt",             CmdHf14MfDecryptBytes
,  1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"}, 
2292         {NULL
, NULL
, 0, NULL
} 
2295 int CmdHFMF(const char *Cmd
) { 
2297         clearCommandBuffer(); 
2298         //WaitForResponseTimeout(CMD_ACK,NULL,100); 
2299         CmdsParse(CommandTable
, Cmd
); 
2303 int CmdHelp(const char *Cmd
) { 
2304         CmdsHelp(CommandTable
);