]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
901ccd4921a3b7d0231c19751de6edbec5b52914
   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 //----------------------------------------------------------------------------- 
  10 #include "../include/mifare.h" 
  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; 
  21         uint8_t keyBlock
[8] = {0}; 
  23         UsbCommand c 
= {CMD_READER_MIFARE
, {true, 0, 0}}; 
  26         printf("-------------------------------------------------------------------------\n"); 
  27         printf("Executing command. Expected execution time: 25sec on average  :-)\n"); 
  28         printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n"); 
  29         printf("-------------------------------------------------------------------------\n"); 
  37         while (ukbhit())        getchar(); 
  45                         printf("\naborted via keyboard!\n"); 
  50                 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) { 
  51                         isOK  
= resp
.arg
[0] & 0xff; 
  52                         uid 
= (uint32_t)bytes_to_num(resp
.d
.asBytes 
+  0, 4); 
  53                         nt 
=  (uint32_t)bytes_to_num(resp
.d
.asBytes 
+  4, 4); 
  54                         par_list 
= bytes_to_num(resp
.d
.asBytes 
+  8, 8); 
  55                         ks_list 
= bytes_to_num(resp
.d
.asBytes 
+  16, 8); 
  56                         nr 
= bytes_to_num(resp
.d
.asBytes 
+ 24, 4); 
  58                         if (!isOK
) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n"); 
  66         if (isOK 
!= 1) return 1; 
  68         // execute original function from util nonce2key 
  69         if (nonce2key(uid
, nt
, nr
, par_list
, ks_list
, &r_key
)) 
  72                 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt
);     
  74                 printf("------------------------------------------------------------------\n"); 
  75                 PrintAndLog("Key found:%012"llx
" \n", r_key
); 
  77                 num_to_bytes(r_key
, 6, keyBlock
); 
  78                 isOK 
= mfCheckKeys(0, 0, 1, keyBlock
, &r_key
); 
  81                 PrintAndLog("Found valid key:%012"llx
, r_key
); 
  84                 if (isOK 
!= 2) PrintAndLog("Found invalid key. ");       
  85                 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce..."); 
  93 int CmdHF14AMfWrBl(const char *Cmd
) 
  97         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
  98         uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
 103                 PrintAndLog("Usage:  hf mf wrbl    <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>"); 
 104                 PrintAndLog("        sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F"); 
 108         blockNo 
= param_get8(Cmd
, 0); 
 109         cmdp 
= param_getchar(Cmd
, 1); 
 111                 PrintAndLog("Key type must be A or B"); 
 114         if (cmdp 
!= 'A' && cmdp 
!= 'a') keyType 
= 1; 
 115         if (param_gethex(Cmd
, 2, key
, 12)) { 
 116                 PrintAndLog("Key must include 12 HEX symbols"); 
 119         if (param_gethex(Cmd
, 3, bldata
, 32)) { 
 120                 PrintAndLog("Block data must include 32 HEX symbols"); 
 123         PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6)); 
 124         PrintAndLog("--data: %s", sprint_hex(bldata
, 16)); 
 126   UsbCommand c 
= {CMD_MIFARE_WRITEBL
, {blockNo
, keyType
, 0}}; 
 127         memcpy(c
.d
.asBytes
, key
, 6); 
 128         memcpy(c
.d
.asBytes 
+ 10, bldata
, 16); 
 132         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 133                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 134                 PrintAndLog("isOk:%02x", isOK
); 
 136                 PrintAndLog("Command execute timeout"); 
 142 /*  dublett finns i CMDHFMFU.C  
 143 int CmdHF14AMfUWrBl(const char *Cmd) 
 147         uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
 151                 PrintAndLog("Usage:  hf mf uwrbl    <block number> <block data (8 hex symbols)> <w>"); 
 152                 PrintAndLog("        sample: hf mf uwrbl 0 01020304"); 
 156         blockNo = param_get8(Cmd, 0); 
 157         if (param_gethex(Cmd, 1, bldata, 8)) { 
 158                 PrintAndLog("Block data must include 8 HEX symbols"); 
 162         if (strchr(Cmd,'w') != 0) { 
 169                                 PrintAndLog("Access Denied"); 
 171                                 PrintAndLog("--specialblock no:%d", blockNo); 
 172                                 PrintAndLog("--data: %s", sprint_hex(bldata, 4)); 
 173                                 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; 
 174                                 memcpy(d.d.asBytes,bldata, 4); 
 177                                 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 178                                         uint8_t isOK  = resp.arg[0] & 0xff; 
 179                                         PrintAndLog("isOk:%02x", isOK); 
 181                                         PrintAndLog("Command execute timeout"); 
 187                                 PrintAndLog("Access Denied"); 
 189                                 PrintAndLog("--specialblock no:%d", blockNo); 
 190                                 PrintAndLog("--data: %s", sprint_hex(bldata, 4)); 
 191                                 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; 
 192                                 memcpy(d.d.asBytes,bldata, 4); 
 195                                 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 196                                 uint8_t isOK  = resp.arg[0] & 0xff; 
 197                                 PrintAndLog("isOk:%02x", isOK); 
 199                                         PrintAndLog("Command execute timeout"); 
 205                                 PrintAndLog("Access Denied"); 
 207                                 PrintAndLog("--specialblock no:%d", blockNo); 
 208                                 PrintAndLog("--data: %s", sprint_hex(bldata, 4)); 
 209                                 UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}}; 
 210                                 memcpy(c.d.asBytes, bldata, 4); 
 213                                 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 214                                         uint8_t isOK  = resp.arg[0] & 0xff; 
 215                                         PrintAndLog("isOk:%02x", isOK); 
 217                                         PrintAndLog("Command execute timeout"); 
 222                         PrintAndLog("--specialblock no:%d", blockNo); 
 223                         PrintAndLog("--data: %s", sprint_hex(bldata, 4)); 
 224                         UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; 
 225                         memcpy(d.d.asBytes,bldata, 4); 
 228                         if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 229                                 uint8_t isOK  = resp.arg[0] & 0xff; 
 230                                 PrintAndLog("isOk:%02x", isOK); 
 232                                 PrintAndLog("Command execute timeout"); 
 236                         PrintAndLog("--block no:%d", blockNo); 
 237                         PrintAndLog("--data: %s", sprint_hex(bldata, 4));                
 238                         UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; 
 239                         memcpy(e.d.asBytes,bldata, 4); 
 242                         if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 243                                 uint8_t isOK  = resp.arg[0] & 0xff; 
 244                                 PrintAndLog("isOk:%02x", isOK); 
 246                                 PrintAndLog("Command execute timeout"); 
 253 int CmdHF14AMfRdBl(const char *Cmd
) 
 257         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 263                 PrintAndLog("Usage:  hf mf rdbl    <block number> <key A/B> <key (12 hex symbols)>"); 
 264                 PrintAndLog("        sample: hf mf rdbl 0 A FFFFFFFFFFFF "); 
 268         blockNo 
= param_get8(Cmd
, 0); 
 269         cmdp 
= param_getchar(Cmd
, 1); 
 271                 PrintAndLog("Key type must be A or B"); 
 274         if (cmdp 
!= 'A' && cmdp 
!= 'a') keyType 
= 1; 
 275         if (param_gethex(Cmd
, 2, key
, 12)) { 
 276                 PrintAndLog("Key must include 12 HEX symbols"); 
 279         PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo
, keyType
?'B':'A', sprint_hex(key
, 6)); 
 281   UsbCommand c 
= {CMD_MIFARE_READBL
, {blockNo
, keyType
, 0}}; 
 282         memcpy(c
.d
.asBytes
, key
, 6); 
 286         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 287                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 288                 uint8_t *data 
= resp
.d
.asBytes
; 
 291                         PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 16)); 
 293                         PrintAndLog("isOk:%02x", isOK
); 
 295                 PrintAndLog("Command execute timeout"); 
 301 /* dublett finns i CMDHFMFU.C  
 302 int CmdHF14AMfURdBl(const char *Cmd) 
 307                 PrintAndLog("Usage:  hf mf urdbl    <block number>"); 
 308                 PrintAndLog("        sample: hf mf urdbl 0"); 
 312     blockNo = param_get8(Cmd, 0); 
 313     PrintAndLog("--block no:%d", blockNo); 
 315         UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; 
 319     if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 320                 uint8_t isOK = resp.arg[0] & 0xff; 
 321         uint8_t *data = resp.d.asBytes; 
 324             PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4)); 
 326             PrintAndLog("isOk:%02x", isOK); 
 328         PrintAndLog("Command execute timeout"); 
 335 /* dublett finns i CMDHFMFU.C  
 336 int CmdHF14AMfURdCard(const char *Cmd) 
 339     uint8_t sectorNo = 0; 
 340         uint8_t *lockbytes_t=NULL; 
 341         uint8_t lockbytes[2]={0,0}; 
 342         bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
 345     uint8_t * data  = NULL; 
 348         PrintAndLog("Sector number must be less than 16"); 
 351     PrintAndLog("Attempting to Read Ultralight... "); 
 353         UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}}; 
 357     if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { 
 358         isOK = resp.arg[0] & 0xff; 
 359         data = resp.d.asBytes; 
 361         PrintAndLog("isOk:%02x", isOK); 
 363             for (i = 0; i < 16; i++) { 
 367                                         lockbytes_t=data+(i*4); 
 368                                         lockbytes[0]=lockbytes_t[2]; 
 369                                         lockbytes[1]=lockbytes_t[3]; 
 370                                         for(int j=0; j<16; j++){ 
 371                                                 bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8)); 
 373                                         //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]); 
 374                                         //PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]);             
 375                                         PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4)); 
 378                                         PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]); 
 381                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]); 
 384                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]); 
 387                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]); 
 390                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]); 
 393                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]); 
 396                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]); 
 399                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]); 
 402                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]); 
 405                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]); 
 408                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]); 
 411                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]); 
 414                     PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]); 
 417                                         PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4)); 
 422                 PrintAndLog("Command execute timeout"); 
 428 int CmdHF14AMfRdSc(const char *Cmd
) 
 431         uint8_t sectorNo 
= 0; 
 433         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 435         uint8_t *data  
= NULL
; 
 439                 PrintAndLog("Usage:  hf mf rdsc    <sector number> <key A/B> <key (12 hex symbols)>"); 
 440                 PrintAndLog("        sample: hf mf rdsc 0 A FFFFFFFFFFFF "); 
 444         sectorNo 
= param_get8(Cmd
, 0); 
 446                 PrintAndLog("Sector number must be less than 40"); 
 449         cmdp 
= param_getchar(Cmd
, 1); 
 450         if (cmdp 
!= 'a' && cmdp 
!= 'A' && cmdp 
!= 'b' && cmdp 
!= 'B') { 
 451                 PrintAndLog("Key type must be A or B"); 
 454         if (cmdp 
!= 'A' && cmdp 
!= 'a') keyType 
= 1; 
 455         if (param_gethex(Cmd
, 2, key
, 12)) { 
 456                 PrintAndLog("Key must include 12 HEX symbols"); 
 459         PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo
, keyType
?'B':'A', sprint_hex(key
, 6)); 
 461         UsbCommand c 
= {CMD_MIFARE_READSC
, {sectorNo
, keyType
, 0}}; 
 462         memcpy(c
.d
.asBytes
, key
, 6); 
 467         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 468                 isOK  
= resp
.arg
[0] & 0xff; 
 469                 data  
= resp
.d
.asBytes
; 
 471                 PrintAndLog("isOk:%02x", isOK
); 
 473                         for (i 
= 0; i 
< (sectorNo
<32?3:15); i
++) { 
 474                                 PrintAndLog("data   : %s", sprint_hex(data 
+ i 
* 16, 16)); 
 476                         PrintAndLog("trailer: %s", sprint_hex(data 
+ (sectorNo
<32?3:15) * 16, 16)); 
 479                 PrintAndLog("Command execute timeout"); 
 486 uint8_t FirstBlockOfSector(uint8_t sectorNo
) 
 491                 return 32 * 4 + (sectorNo 
- 32) * 16; 
 496 uint8_t NumBlocksPerSector(uint8_t sectorNo
) 
 506 int CmdHF14AMfDump(const char *Cmd
) 
 508         uint8_t sectorNo
, blockNo
; 
 512         uint8_t rights
[40][4]; 
 513         uint8_t carddata
[256][16]; 
 514         uint8_t numSectors 
= 16; 
 521         int size 
= GetCardSize();                
 522         char cmdp 
= param_getchar(Cmd
, 0); 
 525                 cmdp 
= (char)(48+size
); 
 527         PrintAndLog("Got %d",cmdp
); 
 530                 case '0' : numSectors 
= 5; break; 
 532                 case '\0': numSectors 
= 16; break; 
 533                 case '2' : numSectors 
= 32; break; 
 534                 case '4' : numSectors 
= 40; break; 
 535                 default:   numSectors 
= 16; 
 538         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') { 
 539                 PrintAndLog("Usage:   hf mf dump [card memory]"); 
 540                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
 542                 PrintAndLog("Samples: hf mf dump"); 
 543                 PrintAndLog("         hf mf dump 4"); 
 547         if ((fin 
= fopen("dumpkeys.bin","rb")) == NULL
) { 
 548                 PrintAndLog("Could not find file dumpkeys.bin");                 
 552         // Read keys A from file 
 553         for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) { 
 554                 if (fread( keyA
[sectorNo
], 1, 6, fin 
) == 0) { 
 555                         PrintAndLog("File reading error."); 
 561         // Read keys B from file 
 562         for (sectorNo
=0; sectorNo
<numSectors
; sectorNo
++) { 
 563                 if (fread( keyB
[sectorNo
], 1, 6, fin 
) == 0) { 
 564                         PrintAndLog("File reading error."); 
 572         PrintAndLog("|-----------------------------------------|"); 
 573         PrintAndLog("|------ Reading sector access bits...-----|"); 
 574         PrintAndLog("|-----------------------------------------|"); 
 576         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 577                 UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 0, 0}}; 
 578                 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6); 
 581                 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 582                         uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 583                         uint8_t *data  
= resp
.d
.asBytes
; 
 585                                 rights
[sectorNo
][0] = ((data
[7] & 0x10)>>2) | ((data
[8] & 0x1)<<1) | ((data
[8] & 0x10)>>4); // C1C2C3 for data area 0 
 586                                 rights
[sectorNo
][1] = ((data
[7] & 0x20)>>3) | ((data
[8] & 0x2)<<0) | ((data
[8] & 0x20)>>5); // C1C2C3 for data area 1 
 587                                 rights
[sectorNo
][2] = ((data
[7] & 0x40)>>4) | ((data
[8] & 0x4)>>1) | ((data
[8] & 0x40)>>6); // C1C2C3 for data area 2 
 588                                 rights
[sectorNo
][3] = ((data
[7] & 0x80)>>5) | ((data
[8] & 0x8)>>2) | ((data
[8] & 0x80)>>7); // C1C2C3 for sector trailer 
 590                                 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo
); 
 591                                 rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00; 
 592                                 rights
[sectorNo
][3] = 0x01; 
 595                         PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo
); 
 596                         rights
[sectorNo
][0] = rights
[sectorNo
][1] = rights
[sectorNo
][2] = 0x00; 
 597                         rights
[sectorNo
][3] = 0x01; 
 601         PrintAndLog("|-----------------------------------------|"); 
 602         PrintAndLog("|----- Dumping all blocks to file... -----|"); 
 603         PrintAndLog("|-----------------------------------------|"); 
 606         for (sectorNo 
= 0; isOK 
&& sectorNo 
< numSectors
; sectorNo
++) { 
 607                 for (blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 608                         bool received 
= false; 
 610                         if (blockNo 
== NumBlocksPerSector(sectorNo
) - 1) {              // sector trailer. At least the Access Conditions can always be read with key A.  
 611                                 UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}}; 
 612                                 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6); 
 614                                 received 
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500); 
 615                         } else {                                                                                                // data block. Check if it can be read with key A or key B 
 616                                 uint8_t data_area 
= sectorNo
<32?blockNo
:blockNo
/5; 
 617                                 if ((rights
[sectorNo
][data_area
] == 0x03) || (rights
[sectorNo
][data_area
] == 0x05)) {   // only key B would work 
 618                                         UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 1, 0}}; 
 619                                         memcpy(c
.d
.asBytes
, keyB
[sectorNo
], 6); 
 621                                         received 
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500); 
 622                                 } else if (rights
[sectorNo
][data_area
] == 0x07) {                                                                               // no key would work 
 624                                                 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo
, blockNo
); 
 625                                 } else {                                                                                                                                                                // key A would work 
 626                                                 UsbCommand c 
= {CMD_MIFARE_READBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, 0, 0}}; 
 627                                                 memcpy(c
.d
.asBytes
, keyA
[sectorNo
], 6); 
 629                                                 received 
= WaitForResponseTimeout(CMD_ACK
,&resp
,1500); 
 634                                 isOK  
= resp
.arg
[0] & 0xff; 
 635                                 uint8_t *data  
= resp
.d
.asBytes
; 
 636                                 if (blockNo 
== NumBlocksPerSector(sectorNo
) - 1) {              // sector trailer. Fill in the keys. 
 637                                         data
[0]  = (keyA
[sectorNo
][0]); 
 638                                         data
[1]  = (keyA
[sectorNo
][1]); 
 639                                         data
[2]  = (keyA
[sectorNo
][2]); 
 640                                         data
[3]  = (keyA
[sectorNo
][3]); 
 641                                         data
[4]  = (keyA
[sectorNo
][4]); 
 642                                         data
[5]  = (keyA
[sectorNo
][5]); 
 643                                         data
[10] = (keyB
[sectorNo
][0]); 
 644                                         data
[11] = (keyB
[sectorNo
][1]); 
 645                                         data
[12] = (keyB
[sectorNo
][2]); 
 646                                         data
[13] = (keyB
[sectorNo
][3]); 
 647                                         data
[14] = (keyB
[sectorNo
][4]); 
 648                                         data
[15] = (keyB
[sectorNo
][5]); 
 651                                         memcpy(carddata
[FirstBlockOfSector(sectorNo
) + blockNo
], data
, 16); 
 652                     PrintAndLog("Successfully read block %2d of sector %2d.", blockNo
, sectorNo
); 
 654                                         PrintAndLog("Could not read block %2d of sector %2d", blockNo
, sectorNo
); 
 660                                 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo
, sectorNo
); 
 667                 if ((fout 
= fopen("dumpdata.bin","wb")) == NULL
) {  
 668                         PrintAndLog("Could not create file name dumpdata.bin"); 
 671                 uint16_t numblocks 
= FirstBlockOfSector(numSectors 
- 1) + NumBlocksPerSector(numSectors 
- 1); 
 672                 fwrite(carddata
, 1, 16*numblocks
, fout
); 
 674                 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks
, 16*numblocks
); 
 681 int CmdHF14AMfRestore(const char *Cmd
) 
 684         uint8_t sectorNo
,blockNo
; 
 686         uint8_t key
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
 687         uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
 695         char cmdp 
= param_getchar(Cmd
, 0); 
 697                 case '0' : numSectors 
= 5; break; 
 699                 case '\0': numSectors 
= 16; break; 
 700                 case '2' : numSectors 
= 32; break; 
 701                 case '4' : numSectors 
= 40; break; 
 702                 default:   numSectors 
= 16; 
 705         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') { 
 706                 PrintAndLog("Usage:   hf mf restore [card memory]"); 
 707                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
 709                 PrintAndLog("Samples: hf mf restore"); 
 710                 PrintAndLog("         hf mf restore 4"); 
 714         if ((fdump 
= fopen("dumpdata.bin","rb")) == NULL
) { 
 715                 PrintAndLog("Could not find file dumpdata.bin"); 
 718         if ((fkeys 
= fopen("dumpkeys.bin","rb")) == NULL
) { 
 719                 PrintAndLog("Could not find file dumpkeys.bin"); 
 723         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 724                 if (fread(keyA
[sectorNo
], 1, 6, fkeys
) == 0) { 
 725                         PrintAndLog("File reading error (dumpkeys.bin)."); 
 730         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 731                 if (fread(keyB
[sectorNo
], 1, 6, fkeys
) == 0) { 
 732                         PrintAndLog("File reading error (dumpkeys.bin)."); 
 737         PrintAndLog("Restoring dumpdata.bin to card"); 
 739         for (sectorNo 
= 0; sectorNo 
< numSectors
; sectorNo
++) { 
 740                 for(blockNo 
= 0; blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 741                         UsbCommand c 
= {CMD_MIFARE_WRITEBL
, {FirstBlockOfSector(sectorNo
) + blockNo
, keyType
, 0}}; 
 742                         memcpy(c
.d
.asBytes
, key
, 6); 
 744                         if (fread(bldata
, 1, 16, fdump
) == 0) { 
 745                                 PrintAndLog("File reading error (dumpdata.bin)."); 
 749                         if (blockNo 
== NumBlocksPerSector(sectorNo
) - 1) {      // sector trailer 
 750                                 bldata
[0]  = (keyA
[sectorNo
][0]); 
 751                                 bldata
[1]  = (keyA
[sectorNo
][1]); 
 752                                 bldata
[2]  = (keyA
[sectorNo
][2]); 
 753                                 bldata
[3]  = (keyA
[sectorNo
][3]); 
 754                                 bldata
[4]  = (keyA
[sectorNo
][4]); 
 755                                 bldata
[5]  = (keyA
[sectorNo
][5]); 
 756                                 bldata
[10] = (keyB
[sectorNo
][0]); 
 757                                 bldata
[11] = (keyB
[sectorNo
][1]); 
 758                                 bldata
[12] = (keyB
[sectorNo
][2]); 
 759                                 bldata
[13] = (keyB
[sectorNo
][3]); 
 760                                 bldata
[14] = (keyB
[sectorNo
][4]); 
 761                                 bldata
[15] = (keyB
[sectorNo
][5]); 
 764                         PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo
) + blockNo
, sprint_hex(bldata
, 16)); 
 766                         memcpy(c
.d
.asBytes 
+ 10, bldata
, 16); 
 770                         if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
 771                                 uint8_t isOK  
= resp
.arg
[0] & 0xff; 
 772                                 PrintAndLog("isOk:%02x", isOK
); 
 774                                 PrintAndLog("Command execute timeout"); 
 785 int CmdHF14AMfNested(const char *Cmd
) 
 787         int i
, j
, res
, iterations
; 
 788         sector 
*e_sector 
= NULL
; 
 791         uint8_t trgBlockNo 
= 0; 
 792         uint8_t trgKeyType 
= 0; 
 793         uint8_t SectorsCnt 
= 0; 
 794         uint8_t key
[6] = {0, 0, 0, 0, 0, 0}; 
 795         uint8_t keyBlock
[13*6]; 
 797         bool transferToEml 
= false; 
 799         bool createDumpFile 
= false; 
 801         uint8_t standart
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
 802         uint8_t tempkey
[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
 807                 PrintAndLog("Usage:"); 
 808                 PrintAndLog(" all sectors:  hf mf nested  <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]"); 
 809                 PrintAndLog(" one sector:   hf mf nested  o <block number> <key A/B> <key (12 hex symbols)>"); 
 810                 PrintAndLog("               <target block number> <target key A/B> [t]"); 
 811                 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K"); 
 812                 PrintAndLog("t - transfer keys into emulator memory"); 
 813                 PrintAndLog("d - write keys to binary file"); 
 815                 PrintAndLog("      sample1: hf mf nested 1 0 A FFFFFFFFFFFF "); 
 816                 PrintAndLog("      sample2: hf mf nested 1 0 A FFFFFFFFFFFF t "); 
 817                 PrintAndLog("      sample3: hf mf nested 1 0 A FFFFFFFFFFFF d "); 
 818                 PrintAndLog("      sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A"); 
 822         cmdp 
= param_getchar(Cmd
, 0); 
 823         blockNo 
= param_get8(Cmd
, 1); 
 824         ctmp 
= param_getchar(Cmd
, 2); 
 826         if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
 827                 PrintAndLog("Key type must be A or B"); 
 831         if (ctmp 
!= 'A' && ctmp 
!= 'a')  
 834         if (param_gethex(Cmd
, 3, key
, 12)) { 
 835                 PrintAndLog("Key must include 12 HEX symbols"); 
 839         if (cmdp 
== 'o' || cmdp 
== 'O') { 
 841                 trgBlockNo 
= param_get8(Cmd
, 4); 
 842                 ctmp 
= param_getchar(Cmd
, 5); 
 843                 if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
 844                         PrintAndLog("Target key type must be A or B"); 
 847                 if (ctmp 
!= 'A' && ctmp 
!= 'a')  
 852                         case '0': SectorsCnt 
= 05; break; 
 853                         case '1': SectorsCnt 
= 16; break; 
 854                         case '2': SectorsCnt 
= 32; break; 
 855                         case '4': SectorsCnt 
= 40; break; 
 856                         default:  SectorsCnt 
= 16; 
 860         ctmp 
= param_getchar(Cmd
, 4); 
 861         if              (ctmp 
== 't' || ctmp 
== 'T') transferToEml 
= true; 
 862         else if (ctmp 
== 'd' || ctmp 
== 'D') createDumpFile 
= true; 
 864         ctmp 
= param_getchar(Cmd
, 6); 
 865         transferToEml 
|= (ctmp 
== 't' || ctmp 
== 'T'); 
 866         transferToEml 
|= (ctmp 
== 'd' || ctmp 
== 'D'); 
 869                 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo
, trgKeyType
?'B':'A'); 
 870                 if (mfnested(blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true)) { 
 871                         PrintAndLog("Nested error."); 
 874                 key64 
= bytes_to_num(keyBlock
, 6); 
 876                         PrintAndLog("Found valid key:%012"llx
, key64
); 
 878                         // transfer key to the emulator 
 880                                 uint8_t sectortrailer
; 
 881                                 if (trgBlockNo 
< 32*4) {        // 4 block sector 
 882                                         sectortrailer 
= (trgBlockNo 
& 0x03) + 3; 
 883                                 } else {                                        // 16 block sector 
 884                                         sectortrailer 
= (trgBlockNo 
& 0x0f) + 15; 
 886                                 mfEmlGetMem(keyBlock
, sectortrailer
, 1); 
 889                                         num_to_bytes(key64
, 6, keyBlock
); 
 891                                         num_to_bytes(key64
, 6, &keyBlock
[10]); 
 892                                 mfEmlSetMem(keyBlock
, sectortrailer
, 1);                 
 895                         PrintAndLog("No valid key found"); 
 898         else { // ------------------------------------  multiple sectors working 
 902                 e_sector 
= calloc(SectorsCnt
, sizeof(sector
)); 
 903                 if (e_sector 
== NULL
) return 1; 
 905                 //test current key and additional standard keys first 
 906                 memcpy(keyBlock
, key
, 6); 
 907                 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock 
+ 1 * 6)); 
 908                 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock 
+ 2 * 6)); 
 909                 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock 
+ 3 * 6)); 
 910                 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock 
+ 4 * 6)); 
 911                 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock 
+ 5 * 6)); 
 912                 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock 
+ 6 * 6)); 
 913                 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock 
+ 7 * 6)); 
 914                 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock 
+ 8 * 6)); 
 915                 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock 
+ 9 * 6)); 
 916                 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock 
+ 10 * 6)); 
 917                 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock 
+ 11 * 6)); 
 918                 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock 
+ 12 * 6)); 
 919                 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock 
+ 13 * 6)); 
 921                 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt
); 
 922                 for (i 
= 0; i 
< SectorsCnt
; i
++) { 
 923                         for (j 
= 0; j 
< 2; j
++) { 
 924                                 if (e_sector
[i
].foundKey
[j
]) continue; 
 926                                 res 
= mfCheckKeys(FirstBlockOfSector(i
), j
, 6, keyBlock
, &key64
); 
 929                                         e_sector
[i
].Key
[j
] = key64
; 
 930                                         e_sector
[i
].foundKey
[j
] = 1; 
 937                 PrintAndLog("nested..."); 
 938                 bool calibrate 
= true; 
 939                 for (i 
= 0; i 
< NESTED_SECTOR_RETRY
; i
++) { 
 940                         for (uint8_t sectorNo 
= 0; sectorNo 
< SectorsCnt
; sectorNo
++) { 
 943                                         printf("\naborted via keyboard!\n"); 
 948                                 for (trgKeyType 
= 0; trgKeyType 
< 2; trgKeyType
++) {  
 949                                         if (e_sector
[sectorNo
].foundKey
[trgKeyType
]) continue; 
 950                                         PrintAndLog("-----------------------------------------------"); 
 951                                         if(mfnested(blockNo
, keyType
, key
, FirstBlockOfSector(sectorNo
), trgKeyType
, keyBlock
, calibrate
)) { 
 952                                                 PrintAndLog("Nested error.\n"); 
 961                                         key64 
= bytes_to_num(keyBlock
, 6); 
 963                                                 PrintAndLog("Found valid key:%012"llx
, key64
); 
 964                                                 e_sector
[sectorNo
].foundKey
[trgKeyType
] = 1; 
 965                                                 e_sector
[sectorNo
].Key
[trgKeyType
] = key64
; 
 971                 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
); 
 973                 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations
); 
 975                 PrintAndLog("|---|----------------|---|----------------|---|"); 
 976                 PrintAndLog("|sec|key A           |res|key B           |res|"); 
 977                 PrintAndLog("|---|----------------|---|----------------|---|"); 
 978                 for (i 
= 0; i 
< SectorsCnt
; i
++) { 
 979                         PrintAndLog("|%03d|  %012"llx
"  | %d |  %012"llx
"  | %d |", i
, 
 980                                 e_sector
[i
].Key
[0], e_sector
[i
].foundKey
[0], e_sector
[i
].Key
[1], e_sector
[i
].foundKey
[1]); 
 982                 PrintAndLog("|---|----------------|---|----------------|---|"); 
 984                 // transfer them to the emulator 
 986                         for (i 
= 0; i 
< SectorsCnt
; i
++) { 
 987                                 mfEmlGetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1); 
 988                                 if (e_sector
[i
].foundKey
[0]) 
 989                                         num_to_bytes(e_sector
[i
].Key
[0], 6, keyBlock
); 
 990                                 if (e_sector
[i
].foundKey
[1]) 
 991                                         num_to_bytes(e_sector
[i
].Key
[1], 6, &keyBlock
[10]); 
 992                                 mfEmlSetMem(keyBlock
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1); 
 997                 if (createDumpFile
) { 
 998                         if ((fkeys 
= fopen("dumpkeys.bin","wb")) == NULL
) {  
 999                                 PrintAndLog("Could not create file dumpkeys.bin"); 
1003                         PrintAndLog("Printing keys to binary file dumpkeys.bin..."); 
1004                         for(i
=0; i
<SectorsCnt
; i
++) { 
1005                                 if (e_sector
[i
].foundKey
[0]){ 
1006                                         num_to_bytes(e_sector
[i
].Key
[0], 6, tempkey
); 
1007                                         fwrite ( tempkey
, 1, 6, fkeys 
); 
1010                                         fwrite ( &standart
, 1, 6, fkeys 
); 
1013                         for(i
=0; i
<SectorsCnt
; i
++) { 
1014                                 if (e_sector
[i
].foundKey
[1]){ 
1015                                         num_to_bytes(e_sector
[i
].Key
[1], 6, tempkey
); 
1016                                         fwrite ( tempkey
, 1, 6, fkeys 
); 
1019                                         fwrite ( &standart
, 1, 6, fkeys 
); 
1031 int CmdHF14AMfChk(const char *Cmd
) 
1033         if (strlen(Cmd
)<3) { 
1034                 PrintAndLog("Usage:  hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]"); 
1035                 PrintAndLog("          * - all sectors"); 
1036                 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K"); 
1037                 PrintAndLog("d - write keys to binary file\n"); 
1038                 PrintAndLog("t - write keys to emulator memory"); 
1039                 PrintAndLog("      sample: hf mf chk 0 A 1234567890ab keys.dic"); 
1040                 PrintAndLog("              hf mf chk *1 ? t"); 
1041                 PrintAndLog("              hf mf chk *1 ? d"); 
1046         char filename
[256]={0}; 
1048         uint8_t *keyBlock 
= NULL
, *p
; 
1049         uint8_t stKeyBlock 
= 20; 
1054         uint8_t blockNo 
= 0; 
1055         uint8_t SectorsCnt 
= 1; 
1056         uint8_t keyType 
= 0; 
1059         int transferToEml 
= 0; 
1060         int createDumpFile 
= 0; 
1062         keyBlock 
= calloc(stKeyBlock
, 6); 
1063         if (keyBlock 
== NULL
) return 1; 
1065         uint64_t defaultKeys
[] = 
1067                 0xffffffffffff, // Default key (first key used by program if no user defined key) 
1068                 0x000000000000, // Blank key 
1069                 0xa0a1a2a3a4a5, // NFCForum MAD key 
1081         int defaultKeysSize 
= sizeof(defaultKeys
) / sizeof(uint64_t); 
1083         for (int defaultKeyCounter 
= 0; defaultKeyCounter 
< defaultKeysSize
; defaultKeyCounter
++) 
1085                 num_to_bytes(defaultKeys
[defaultKeyCounter
], 6, (uint8_t*)(keyBlock 
+ defaultKeyCounter 
* 6)); 
1088         if (param_getchar(Cmd
, 0)=='*') { 
1090                 switch(param_getchar(Cmd
+1, 0)) { 
1091                         case '0': SectorsCnt 
=  5; break; 
1092                         case '1': SectorsCnt 
= 16; break; 
1093                         case '2': SectorsCnt 
= 32; break; 
1094                         case '4': SectorsCnt 
= 40; break; 
1095                         default:  SectorsCnt 
= 16; 
1099                 blockNo 
= param_get8(Cmd
, 0); 
1101         ctmp 
= param_getchar(Cmd
, 1); 
1113                 PrintAndLog("Key type must be A , B or ?"); 
1117         ctmp 
= param_getchar(Cmd
, 2); 
1118         if              (ctmp 
== 't' || ctmp 
== 'T') transferToEml 
= 1; 
1119         else if (ctmp 
== 'd' || ctmp 
== 'D') createDumpFile 
= 1; 
1121         for (i 
= transferToEml 
|| createDumpFile
; param_getchar(Cmd
, 2 + i
); i
++) { 
1122                 if (!param_gethex(Cmd
, 2 + i
, keyBlock 
+ 6 * keycnt
, 12)) { 
1123                         if ( stKeyBlock 
- keycnt 
< 2) { 
1124                                 p 
= realloc(keyBlock
, 6*(stKeyBlock
+=10)); 
1126                                         PrintAndLog("Cannot allocate memory for Keys"); 
1132                         PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
, 
1133                         (keyBlock 
+ 6*keycnt
)[0],(keyBlock 
+ 6*keycnt
)[1], (keyBlock 
+ 6*keycnt
)[2], 
1134                         (keyBlock 
+ 6*keycnt
)[3], (keyBlock 
+ 6*keycnt
)[4],     (keyBlock 
+ 6*keycnt
)[5], 6); 
1137                         // May be a dic file 
1138                         if ( param_getstr(Cmd
, 2 + i
,filename
) > 255 ) { 
1139                                 PrintAndLog("File name too long"); 
1144                         if ( (f 
= fopen( filename 
, "r")) ) { 
1145                                 while( fgets(buf
, sizeof(buf
), f
) ){ 
1146                                         if (strlen(buf
) < 12 || buf
[11] == '\n') 
1149                                         while (fgetc(f
) != '\n' && !feof(f
)) ;  //goto next line 
1151                                         if( buf
[0]=='#' ) continue;     //The line start with # is comment, skip 
1153                                         if (!isxdigit(buf
[0])){ 
1154                                                 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf
); 
1160                                         if ( stKeyBlock 
- keycnt 
< 2) { 
1161                                                 p 
= realloc(keyBlock
, 6*(stKeyBlock
+=10)); 
1163                                                         PrintAndLog("Cannot allocate memory for defKeys"); 
1169                                         memset(keyBlock 
+ 6 * keycnt
, 0, 6); 
1170                                         num_to_bytes(strtoll(buf
, NULL
, 16), 6, keyBlock 
+ 6*keycnt
); 
1171                                         PrintAndLog("chk custom key[%2d] %012"llx
, keycnt
, bytes_to_num(keyBlock 
+ 6*keycnt
, 6)); 
1173                                         memset(buf
, 0, sizeof(buf
)); 
1177                                 PrintAndLog("File: %s: not found or locked.", filename
); 
1186                 PrintAndLog("No key specified, trying default keys"); 
1187                 for (;keycnt 
< defaultKeysSize
; keycnt
++) 
1188                         PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt
, 
1189                         (keyBlock 
+ 6*keycnt
)[0],(keyBlock 
+ 6*keycnt
)[1], (keyBlock 
+ 6*keycnt
)[2], 
1190                         (keyBlock 
+ 6*keycnt
)[3], (keyBlock 
+ 6*keycnt
)[4],     (keyBlock 
+ 6*keycnt
)[5], 6); 
1193         // initialize storage for found keys 
1194         bool validKey
[2][40]; 
1195         uint8_t foundKey
[2][40][6]; 
1196         for (uint16_t t 
= 0; t 
< 2; t
++) { 
1197                 for (uint16_t sectorNo 
= 0; sectorNo 
< SectorsCnt
; sectorNo
++) { 
1198                         validKey
[t
][sectorNo
] = false; 
1199                         for (uint16_t i 
= 0; i 
< 6; i
++) { 
1200                                 foundKey
[t
][sectorNo
][i
] = 0xff; 
1205         for ( int t 
= !keyType
; t 
< 2; keyType
==2?(t
++):(t
=2) ) { 
1207                 for (int i 
= 0; i 
< SectorsCnt
; ++i
) { 
1208                         PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i
, b
, t
?'B':'A', keycnt
); 
1209                         uint32_t max_keys 
= keycnt
>USB_CMD_DATA_SIZE
/6?USB_CMD_DATA_SIZE
/6:keycnt
; 
1210                         for (uint32_t c 
= 0; c 
< keycnt
; c
+=max_keys
) { 
1211                                 uint32_t size 
= keycnt
-c
>max_keys
?max_keys
:keycnt
-c
; 
1212                                 res 
= mfCheckKeys(b
, t
, size
, &keyBlock
[6*c
], &key64
); 
1215                                                 PrintAndLog("Found valid key:[%012"llx
"]",key64
); 
1216                                                 num_to_bytes(key64
, 6, foundKey
[t
][i
]); 
1217                                                 validKey
[t
][i
] = true; 
1220                                         PrintAndLog("Command execute timeout"); 
1223                         b
<127?(b
+=4):(b
+=16);    
1227         if (transferToEml
) { 
1229                 for (uint16_t sectorNo 
= 0; sectorNo 
< SectorsCnt
; sectorNo
++) { 
1230                         if (validKey
[0][sectorNo
] || validKey
[1][sectorNo
]) { 
1231                                 mfEmlGetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1); 
1232                                 for (uint16_t t 
= 0; t 
< 2; t
++) { 
1233                                         if (validKey
[t
][sectorNo
]) { 
1234                                                 memcpy(block 
+ t
*10, foundKey
[t
][sectorNo
], 6); 
1237                                 mfEmlSetMem(block
, FirstBlockOfSector(sectorNo
) + NumBlocksPerSector(sectorNo
) - 1, 1); 
1240                 PrintAndLog("Found keys have been transferred to the emulator memory"); 
1243         if (createDumpFile
) { 
1244                 FILE *fkeys 
= fopen("dumpkeys.bin","wb"); 
1245                 if (fkeys 
== NULL
) {  
1246                         PrintAndLog("Could not create file dumpkeys.bin"); 
1250                 for (uint16_t t 
= 0; t 
< 2; t
++) { 
1251                         fwrite(foundKey
[t
], 1, 6*SectorsCnt
, fkeys
); 
1254                 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys."); 
1263 int CmdHF14AMf1kSim(const char *Cmd
) 
1265         uint8_t uid
[7] = {0, 0, 0, 0, 0, 0, 0}; 
1266         uint8_t exitAfterNReads 
= 0; 
1269         if (param_getchar(Cmd
, 0) == 'h') { 
1270                 PrintAndLog("Usage:  hf mf sim  u <uid (8 hex symbols)> n <numreads> i x"); 
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)"); 
1275                 PrintAndLog("           sample: hf mf sim u 0a0a0a0a "); 
1279         if (param_getchar(Cmd
, pnr
) == 'u') { 
1280                 if(param_gethex(Cmd
, pnr
+1, uid
, 8) == 0) 
1282                         flags 
|= FLAG_4B_UID_IN_DATA
; // UID from packet 
1283                 } else if(param_gethex(Cmd
,pnr
+1,uid
,14) == 0) { 
1284                         flags 
|= FLAG_7B_UID_IN_DATA
;// UID from packet 
1286                         PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols"); 
1291         if (param_getchar(Cmd
, pnr
) == 'n') { 
1292                 exitAfterNReads 
= param_get8(Cmd
,pnr
+1); 
1295         if (param_getchar(Cmd
, pnr
) == 'i' ) { 
1296                 //Using a flag to signal interactiveness, least significant bit 
1297                 flags 
|= FLAG_INTERACTIVE
; 
1301         if (param_getchar(Cmd
, pnr
) == 'x' ) { 
1302                 //Using a flag to signal interactiveness, least significant bit 
1303                 flags 
|= FLAG_NR_AR_ATTACK
; 
1305         PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ", 
1306                                 flags 
& FLAG_4B_UID_IN_DATA 
? sprint_hex(uid
,4): 
1307                                                                                           flags 
& FLAG_7B_UID_IN_DATA   
? sprint_hex(uid
,7): "N/A" 
1308                                 , exitAfterNReads
, flags
,flags
); 
1311         UsbCommand c 
= {CMD_SIMULATE_MIFARE_CARD
, {flags
, exitAfterNReads
,0}}; 
1312         memcpy(c
.d
.asBytes
, uid
, sizeof(uid
)); 
1315         if(flags 
& FLAG_INTERACTIVE
) 
1318                 PrintAndLog("Press pm3-button to abort simulation"); 
1319                 while(! WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) { 
1320                         //We're waiting only 1.5 s at a time, otherwise we get the 
1321                         // annoying message about "Waiting for a response... " 
1329 int CmdHF14AMfDbg(const char *Cmd
) 
1331         int dbgMode 
= param_get32ex(Cmd
, 0, 0, 10); 
1333                 PrintAndLog("Max debug mode parameter is 4 \n"); 
1336         if (strlen(Cmd
) < 1 || !param_getchar(Cmd
, 0) || dbgMode 
> 4) { 
1337                 PrintAndLog("Usage:  hf mf dbg  <debug level>"); 
1338                 PrintAndLog(" 0 - no debug messages"); 
1339                 PrintAndLog(" 1 - error messages"); 
1340                 PrintAndLog(" 2 - plus information messages"); 
1341                 PrintAndLog(" 3 - plus debug messages"); 
1342                 PrintAndLog(" 4 - print even debug messages in timing critical functions"); 
1343                 PrintAndLog("     Note: this option therefore may cause malfunction itself"); 
1347   UsbCommand c 
= {CMD_MIFARE_SET_DBGMODE
, {dbgMode
, 0, 0}}; 
1353 int CmdHF14AMfEGet(const char *Cmd
) 
1355         uint8_t blockNo 
= 0; 
1358         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1359                 PrintAndLog("Usage:  hf mf eget <block number>"); 
1360                 PrintAndLog(" sample: hf mf eget 0 "); 
1364         blockNo 
= param_get8(Cmd
, 0); 
1367         if (!mfEmlGetMem(data
, blockNo
, 1)) { 
1368                 PrintAndLog("data[%3d]:%s", blockNo
, sprint_hex(data
, 16)); 
1370                 PrintAndLog("Command execute timeout"); 
1377 int CmdHF14AMfEClear(const char *Cmd
) 
1379         if (param_getchar(Cmd
, 0) == 'h') { 
1380                 PrintAndLog("Usage:  hf mf eclr"); 
1381                 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n"); 
1385   UsbCommand c 
= {CMD_MIFARE_EML_MEMCLR
, {0, 0, 0}}; 
1391 int CmdHF14AMfESet(const char *Cmd
) 
1393         uint8_t memBlock
[16]; 
1394         uint8_t blockNo 
= 0; 
1396         memset(memBlock
, 0x00, sizeof(memBlock
)); 
1398         if (strlen(Cmd
) < 3 || param_getchar(Cmd
, 0) == 'h') { 
1399                 PrintAndLog("Usage:  hf mf eset <block number> <block data (32 hex symbols)>"); 
1400                 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f "); 
1404         blockNo 
= param_get8(Cmd
, 0); 
1406         if (param_gethex(Cmd
, 1, memBlock
, 32)) { 
1407                 PrintAndLog("block data must include 32 HEX symbols"); 
1412         UsbCommand c 
= {CMD_MIFARE_EML_MEMSET
, {blockNo
, 1, 0}}; 
1413         memcpy(c
.d
.asBytes
, memBlock
, 16); 
1419 int CmdHF14AMfELoad(const char *Cmd
) 
1423         char *fnameptr 
= filename
; 
1426         int i
, len
, blockNum
; 
1428         memset(filename
, 0, sizeof(filename
)); 
1429         memset(buf
, 0, sizeof(buf
)); 
1431         if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) { 
1432                 PrintAndLog("It loads emul dump from the file `filename.eml`"); 
1433                 PrintAndLog("Usage:  hf mf eload <file name w/o `.eml`>"); 
1434                 PrintAndLog(" sample: hf mf eload filename"); 
1439         if (len 
> 250) len 
= 250; 
1441         memcpy(filename
, Cmd
, len
); 
1444         sprintf(fnameptr
, ".eml");  
1447         f 
= fopen(filename
, "r"); 
1449                 PrintAndLog("File not found or locked."); 
1455                 memset(buf
, 0, sizeof(buf
)); 
1456                 if (fgets(buf
, sizeof(buf
), f
) == NULL
) { 
1457                         if((blockNum 
== 16*4) || (blockNum 
== 32*4 + 8*16)) {   // supports both old (1K) and new (4K) .eml files) 
1460                         PrintAndLog("File reading error."); 
1464                 if (strlen(buf
) < 32){ 
1465                         if(strlen(buf
) && feof(f
)) 
1467                         PrintAndLog("File content error. Block data must include 32 HEX symbols"); 
1470                 for (i 
= 0; i 
< 32; i 
+= 2) { 
1471                         sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i 
/ 2]); 
1472 //                      PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16)); 
1474                 if (mfEmlSetMem(buf8
, blockNum
, 1)) { 
1475                         PrintAndLog("Cant set emul block: %3d", blockNum
); 
1480                 if (blockNum 
>= 32*4 + 8*16) break; 
1484         if ((blockNum 
!= 16*4) && (blockNum 
!= 32*4 + 8*16)) { 
1485                 PrintAndLog("File content error. There must be 64 or 256 blocks."); 
1489         PrintAndLog("Loaded %d blocks from file: %s", blockNum
, filename
); 
1494 int CmdHF14AMfESave(const char *Cmd
) 
1498         char * fnameptr 
= filename
; 
1502         memset(filename
, 0, sizeof(filename
)); 
1503         memset(buf
, 0, sizeof(buf
)); 
1505         if (param_getchar(Cmd
, 0) == 'h') { 
1506                 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`"); 
1507                 PrintAndLog("Usage:  hf mf esave [file name w/o `.eml`]"); 
1508                 PrintAndLog(" sample: hf mf esave "); 
1509                 PrintAndLog("         hf mf esave filename"); 
1514         if (len 
> 250) len 
= 250; 
1518                 if (mfEmlGetMem(buf
, 0, 1)) { 
1519                         PrintAndLog("Cant get block: %d", 0); 
1522                 for (j 
= 0; j 
< 7; j
++, fnameptr 
+= 2) 
1523                         sprintf(fnameptr
, "%02x", buf
[j
]);  
1525                 memcpy(filename
, Cmd
, len
); 
1529         sprintf(fnameptr
, ".eml");  
1532         f 
= fopen(filename
, "w+"); 
1535         for (i 
= 0; i 
< 32*4 + 8*16; i
++) { 
1536                 if (mfEmlGetMem(buf
, i
, 1)) { 
1537                         PrintAndLog("Cant get block: %d", i
); 
1540                 for (j 
= 0; j 
< 16; j
++) 
1541                         fprintf(f
, "%02x", buf
[j
]);  
1546         PrintAndLog("Saved to file: %s", filename
); 
1552 int CmdHF14AMfECFill(const char *Cmd
) 
1554         uint8_t keyType 
= 0; 
1555         uint8_t numSectors 
= 16; 
1557         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1558                 PrintAndLog("Usage:  hf mf ecfill <key A/B> [card memory]"); 
1559                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
1561                 PrintAndLog("samples:  hf mf ecfill A"); 
1562                 PrintAndLog("          hf mf ecfill A 4"); 
1563                 PrintAndLog("Read card and transfer its data to emulator memory."); 
1564                 PrintAndLog("Keys must be laid in the emulator memory. \n"); 
1568         char ctmp 
= param_getchar(Cmd
, 0); 
1569         if (ctmp 
!= 'a' && ctmp 
!= 'A' && ctmp 
!= 'b' && ctmp 
!= 'B') { 
1570                 PrintAndLog("Key type must be A or B"); 
1573         if (ctmp 
!= 'A' && ctmp 
!= 'a') keyType 
= 1; 
1575         ctmp 
= param_getchar(Cmd
, 1); 
1577                 case '0' : numSectors 
= 5; break; 
1579                 case '\0': numSectors 
= 16; break; 
1580                 case '2' : numSectors 
= 32; break; 
1581                 case '4' : numSectors 
= 40; break; 
1582                 default:   numSectors 
= 16; 
1585         printf("--params: numSectors: %d, keyType:%d", numSectors
, keyType
); 
1586         UsbCommand c 
= {CMD_MIFARE_EML_CARDLOAD
, {numSectors
, keyType
, 0}}; 
1592 int CmdHF14AMfEKeyPrn(const char *Cmd
) 
1597         uint64_t keyA
, keyB
; 
1599         if (param_getchar(Cmd
, 0) == 'h') { 
1600                 PrintAndLog("It prints the keys loaded in the emulator memory"); 
1601                 PrintAndLog("Usage:  hf mf ekeyprn [card memory]"); 
1602                 PrintAndLog("  [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K"); 
1604                 PrintAndLog(" sample: hf mf ekeyprn 1"); 
1608         char cmdp 
= param_getchar(Cmd
, 0); 
1611                 case '0' : numSectors 
= 5; break; 
1613                 case '\0': numSectors 
= 16; break; 
1614                 case '2' : numSectors 
= 32; break; 
1615                 case '4' : numSectors 
= 40; break; 
1616                 default:   numSectors 
= 16; 
1619         PrintAndLog("|---|----------------|----------------|"); 
1620         PrintAndLog("|sec|key A           |key B           |"); 
1621         PrintAndLog("|---|----------------|----------------|"); 
1622         for (i 
= 0; i 
< numSectors
; i
++) { 
1623                 if (mfEmlGetMem(data
, FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1, 1)) { 
1624                         PrintAndLog("error get block %d", FirstBlockOfSector(i
) + NumBlocksPerSector(i
) - 1); 
1627                 keyA 
= bytes_to_num(data
, 6); 
1628                 keyB 
= bytes_to_num(data 
+ 10, 6); 
1629                 PrintAndLog("|%03d|  %012"llx
"  |  %012"llx
"  |", i
, keyA
, keyB
); 
1631         PrintAndLog("|---|----------------|----------------|"); 
1637 int CmdHF14AMfCSetUID(const char *Cmd
) 
1639         uint8_t wipeCard 
= 0; 
1640         uint8_t uid
[8] = {0x00}; 
1641         uint8_t oldUid
[8] = {0x00}; 
1644         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1645                 PrintAndLog("Usage:  hf mf csetuid <UID 8 hex symbols> <w>"); 
1646                 PrintAndLog("sample:  hf mf csetuid 01020304 w"); 
1647                 PrintAndLog("Set UID for magic Chinese card (only works with!!!)"); 
1648                 PrintAndLog("If you want wipe card then add 'w' into command line. \n"); 
1652         if (param_getchar(Cmd
, 0) && param_gethex(Cmd
, 0, uid
, 8)) { 
1653                 PrintAndLog("UID must include 8 HEX symbols"); 
1657         char ctmp 
= param_getchar(Cmd
, 1); 
1658         if (ctmp 
== 'w' || ctmp 
== 'W') wipeCard 
= 1; 
1660         PrintAndLog("--wipe card:%02x uid:%s", wipeCard
, sprint_hex(uid
, 4)); 
1662         res 
= mfCSetUID(uid
, oldUid
, wipeCard
); 
1664                         PrintAndLog("Can't set UID. error=%d", res
); 
1668         PrintAndLog("old UID:%s", sprint_hex(oldUid
, 4)); 
1672 int CmdHF14AMfCSetBlk(const char *Cmd
) 
1675         uint8_t memBlock
[16]; 
1676         uint8_t blockNo 
= 0; 
1678         memset(memBlock
, 0x00, sizeof(memBlock
)); 
1680         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1681                 PrintAndLog("Usage:  hf mf csetblk <block number> <block data (32 hex symbols)>"); 
1682                 PrintAndLog("sample:  hf mf csetblk 1 01020304050607080910111213141516"); 
1683                 PrintAndLog("Set block data for magic Chinese card (only works with!!!)"); 
1684                 PrintAndLog("If you want wipe card then add 'w' into command line. \n"); 
1688         blockNo 
= param_get8(Cmd
, 0); 
1690         if (param_gethex(Cmd
, 1, memBlock
, 32)) { 
1691                 PrintAndLog("block data must include 32 HEX symbols"); 
1695         PrintAndLog("--block number:%2d data:%s", blockNo
, sprint_hex(memBlock
, 16)); 
1697         res 
= mfCSetBlock(blockNo
, memBlock
, uid
, 0, CSETBLOCK_SINGLE_OPER
); 
1699                         PrintAndLog("Can't write block. error=%d", res
); 
1703         PrintAndLog("UID:%s", sprint_hex(uid
, 4)); 
1708 int CmdHF14AMfCLoad(const char *Cmd
) 
1712         char * fnameptr 
= filename
; 
1715         uint8_t fillFromEmulator 
= 0; 
1716         int i
, len
, blockNum
, flags
; 
1718         memset(filename
, 0, sizeof(filename
)); 
1719         memset(buf
, 0, sizeof(buf
)); 
1721         if (param_getchar(Cmd
, 0) == 'h' || param_getchar(Cmd
, 0)== 0x00) { 
1722                 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`"); 
1723                 PrintAndLog("or from emulator memory (option `e`)"); 
1724                 PrintAndLog("Usage:  hf mf cload <file name w/o `.eml`>"); 
1725                 PrintAndLog("   or:  hf mf cload e "); 
1726                 PrintAndLog(" sample: hf mf cload filename"); 
1730         char ctmp 
= param_getchar(Cmd
, 0); 
1731         if (ctmp 
== 'e' || ctmp 
== 'E') fillFromEmulator 
= 1; 
1733         if (fillFromEmulator
) { 
1734                 flags 
= CSETBLOCK_INIT_FIELD 
+ CSETBLOCK_WUPC
; 
1735                 for (blockNum 
= 0; blockNum 
< 16 * 4; blockNum 
+= 1) { 
1736                         if (mfEmlGetMem(buf8
, blockNum
, 1)) { 
1737                                 PrintAndLog("Cant get block: %d", blockNum
); 
1741                         if (blockNum 
== 2) flags 
= 0; 
1742                         if (blockNum 
== 16 * 4 - 1) flags 
= CSETBLOCK_HALT 
+ CSETBLOCK_RESET_FIELD
; 
1744                         if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) { 
1745                                 PrintAndLog("Cant set magic card block: %d", blockNum
); 
1752                 if (len 
> 250) len 
= 250; 
1754                 memcpy(filename
, Cmd
, len
); 
1757                 sprintf(fnameptr
, ".eml");  
1760                 f 
= fopen(filename
, "r"); 
1762                         PrintAndLog("File not found or locked."); 
1767                 flags 
= CSETBLOCK_INIT_FIELD 
+ CSETBLOCK_WUPC
; 
1769                         memset(buf
, 0, sizeof(buf
)); 
1770                         if (fgets(buf
, sizeof(buf
), f
) == NULL
) { 
1771                                 PrintAndLog("File reading error."); 
1775                         if (strlen(buf
) < 32){ 
1776                                 if(strlen(buf
) && feof(f
)) 
1778                                 PrintAndLog("File content error. Block data must include 32 HEX symbols"); 
1781                         for (i 
= 0; i 
< 32; i 
+= 2) 
1782                                 sscanf(&buf
[i
], "%02x", (unsigned int *)&buf8
[i 
/ 2]); 
1784                         if (blockNum 
== 2) flags 
= 0; 
1785                         if (blockNum 
== 16 * 4 - 1) flags 
= CSETBLOCK_HALT 
+ CSETBLOCK_RESET_FIELD
; 
1787                         if (mfCSetBlock(blockNum
, buf8
, NULL
, 0, flags
)) { 
1788                                 PrintAndLog("Can't set magic card block: %d", blockNum
); 
1793                         if (blockNum 
>= 16 * 4) break;  // magic card type - mifare 1K 
1797                 if (blockNum 
!= 16 * 4 && blockNum 
!= 32 * 4 + 8 * 16){ 
1798                         PrintAndLog("File content error. There must be 64 blocks"); 
1801                 PrintAndLog("Loaded from file: %s", filename
); 
1806 int CmdHF14AMfCGetBlk(const char *Cmd
) { 
1807         uint8_t memBlock
[16]; 
1808         uint8_t blockNo 
= 0; 
1810         memset(memBlock
, 0x00, sizeof(memBlock
)); 
1812         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1813                 PrintAndLog("Usage:  hf mf cgetblk <block number>"); 
1814                 PrintAndLog("sample:  hf mf cgetblk 1"); 
1815                 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n"); 
1819         blockNo 
= param_get8(Cmd
, 0); 
1821         PrintAndLog("--block number:%2d ", blockNo
); 
1823         res 
= mfCGetBlock(blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
); 
1825                         PrintAndLog("Can't read block. error=%d", res
); 
1829         PrintAndLog("block data:%s", sprint_hex(memBlock
, 16)); 
1834 int CmdHF14AMfCGetSc(const char *Cmd
) { 
1835         uint8_t memBlock
[16]; 
1836         uint8_t sectorNo 
= 0; 
1838         memset(memBlock
, 0x00, sizeof(memBlock
)); 
1840         if (strlen(Cmd
) < 1 || param_getchar(Cmd
, 0) == 'h') { 
1841                 PrintAndLog("Usage:  hf mf cgetsc <sector number>"); 
1842                 PrintAndLog("sample:  hf mf cgetsc 0"); 
1843                 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n"); 
1847         sectorNo 
= param_get8(Cmd
, 0); 
1848         if (sectorNo 
> 15) { 
1849                 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic."); 
1853         PrintAndLog("--sector number:%d ", sectorNo
); 
1855         flags 
= CSETBLOCK_INIT_FIELD 
+ CSETBLOCK_WUPC
; 
1856         for (i 
= 0; i 
< 4; i
++) { 
1857                 if (i 
== 1) flags 
= 0; 
1858                 if (i 
== 3) flags 
= CSETBLOCK_HALT 
+ CSETBLOCK_RESET_FIELD
; 
1860                 res 
= mfCGetBlock(sectorNo 
* 4 + i
, memBlock
, flags
); 
1862                         PrintAndLog("Can't read block. %d error=%d", sectorNo 
* 4 + i
, res
); 
1866                 PrintAndLog("block %3d data:%s", sectorNo 
* 4 + i
, sprint_hex(memBlock
, 16)); 
1872 int CmdHF14AMfCSave(const char *Cmd
) { 
1876         char * fnameptr 
= filename
; 
1877         uint8_t fillFromEmulator 
= 0; 
1879         int i
, j
, len
, flags
; 
1881         memset(filename
, 0, sizeof(filename
)); 
1882         memset(buf
, 0, sizeof(buf
)); 
1884         if (param_getchar(Cmd
, 0) == 'h') { 
1885                 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`"); 
1886                 PrintAndLog("or into emulator memory (option `e`)"); 
1887                 PrintAndLog("Usage:  hf mf esave [file name w/o `.eml`][e]"); 
1888                 PrintAndLog(" sample: hf mf esave "); 
1889                 PrintAndLog("         hf mf esave filename"); 
1890                 PrintAndLog("         hf mf esave e \n"); 
1894         char ctmp 
= param_getchar(Cmd
, 0); 
1895         if (ctmp 
== 'e' || ctmp 
== 'E') fillFromEmulator 
= 1; 
1897         if (fillFromEmulator
) { 
1898                 // put into emulator 
1899                 flags 
= CSETBLOCK_INIT_FIELD 
+ CSETBLOCK_WUPC
; 
1900                 for (i 
= 0; i 
< 16 * 4; i
++) { 
1901                         if (i 
== 1) flags 
= 0; 
1902                         if (i 
== 16 * 4 - 1) flags 
= CSETBLOCK_HALT 
+ CSETBLOCK_RESET_FIELD
; 
1904                         if (mfCGetBlock(i
, buf
, flags
)) { 
1905                                 PrintAndLog("Cant get block: %d", i
); 
1909                         if (mfEmlSetMem(buf
, i
, 1)) { 
1910                                 PrintAndLog("Cant set emul block: %d", i
); 
1917                 if (len 
> 250) len 
= 250; 
1921                         if (mfCGetBlock(0, buf
, CSETBLOCK_SINGLE_OPER
)) { 
1922                                 PrintAndLog("Cant get block: %d", 0); 
1925                         for (j 
= 0; j 
< 7; j
++, fnameptr 
+= 2) 
1926                                 sprintf(fnameptr
, "%02x", buf
[j
]);  
1928                         memcpy(filename
, Cmd
, len
); 
1932                 sprintf(fnameptr
, ".eml");  
1935                 f 
= fopen(filename
, "w+"); 
1938                 flags 
= CSETBLOCK_INIT_FIELD 
+ CSETBLOCK_WUPC
; 
1939                 for (i 
= 0; i 
< 16 * 4; i
++) { 
1940                         if (i 
== 1) flags 
= 0; 
1941                         if (i 
== 16 * 4 - 1) flags 
= CSETBLOCK_HALT 
+ CSETBLOCK_RESET_FIELD
; 
1943                         if (mfCGetBlock(i
, buf
, flags
)) { 
1944                                 PrintAndLog("Cant get block: %d", i
); 
1947                         for (j 
= 0; j 
< 16; j
++) 
1948                                 fprintf(f
, "%02x", buf
[j
]);  
1953                 PrintAndLog("Saved to file: %s", filename
); 
1960 int CmdHF14AMfSniff(const char *Cmd
){ 
1962         bool wantLogToFile 
= 0; 
1963         bool wantDecrypt 
= 0; 
1964         //bool wantSaveToEml = 0; TODO 
1965         bool wantSaveToEmlFile 
= 0; 
1980         uint8_t * bufPtr 
= buf
; 
1981         memset(buf
, 0x00, 3000); 
1983         if (param_getchar(Cmd
, 0) == 'h') { 
1984                 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file."); 
1985                 PrintAndLog("You can specify:"); 
1986                 PrintAndLog("    l - save encrypted sequence to logfile `uid.log`"); 
1987                 PrintAndLog("    d - decrypt sequence and put it to log file `uid.log`"); 
1988                 PrintAndLog(" n/a   e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory"); 
1989                 PrintAndLog("    r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`"); 
1990                 PrintAndLog("Usage:  hf mf sniff [l][d][e][r]"); 
1991                 PrintAndLog("  sample: hf mf sniff l d e"); 
1995         for (int i 
= 0; i 
< 4; i
++) { 
1996                 char ctmp 
= param_getchar(Cmd
, i
); 
1997                 if (ctmp 
== 'l' || ctmp 
== 'L') wantLogToFile 
= true; 
1998                 if (ctmp 
== 'd' || ctmp 
== 'D') wantDecrypt 
= true; 
1999                 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO 
2000                 if (ctmp 
== 'f' || ctmp 
== 'F') wantSaveToEmlFile 
= true; 
2003         printf("-------------------------------------------------------------------------\n"); 
2004         printf("Executing command. \n"); 
2005         printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n"); 
2006         printf("Press the key on pc keyboard to abort the client.\n"); 
2007         printf("-------------------------------------------------------------------------\n"); 
2009         UsbCommand c 
= {CMD_MIFARE_SNIFFER
, {0, 0, 0}}; 
2010         clearCommandBuffer(); 
2019                         printf("\naborted via keyboard!\n"); 
2024     if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000)) { 
2025                         res 
= resp
.arg
[0] & 0xff; 
2029                         if (res 
== 0) return 0; 
2033                                         memset(buf
, 0x00, 3000); 
2035                                 memcpy(bufPtr
, resp
.d
.asBytes
, len
); 
2040                                 blockLen 
= bufPtr 
- buf
; 
2043                                 PrintAndLog("received trace len: %d packages: %d", blockLen
, pckNum
); 
2045                                 while (bufPtr 
- buf 
+ 9 < blockLen
) { 
2046                                         isTag 
= bufPtr
[3] & 0x80 ? true:false; 
2048                                         parity 
= *((uint32_t *)(bufPtr
)); 
2052                                         if ((len 
== 14) && (bufPtr
[0] == 0xff) && (bufPtr
[1] == 0xff)) { 
2053                                                 memcpy(uid
, bufPtr 
+ 2, 7); 
2054                                                 memcpy(atqa
, bufPtr 
+ 2 + 7, 2); 
2055                                                 uid_len 
= (atqa
[0] & 0xC0) == 0x40 ? 7 : 4; 
2058                                                 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",  
2059                                                         sprint_hex(uid 
+ (7 - uid_len
), uid_len
), 
2063                                                 if (wantLogToFile 
|| wantDecrypt
) { 
2064                                                         FillFileNameByUID(logHexFileName
, uid 
+ (7 - uid_len
), ".log", uid_len
); 
2065                                                         AddLogCurrentDT(logHexFileName
); 
2068                                                         mfTraceInit(uid
, atqa
, sak
, wantSaveToEmlFile
); 
2070                                                 PrintAndLog("%s(%d):%s", isTag 
? "TAG":"RDR", num
, sprint_hex(bufPtr
, len
)); 
2072                                                         AddLogHex(logHexFileName
, isTag 
? "TAG: ":"RDR: ", bufPtr
, len
); 
2074                                                         mfTraceDecode(bufPtr
, len
, parity
, wantSaveToEmlFile
); 
2086 // Tries to identify cardsize. 
2087 // Returns <num>  where num is: 
2089 //  0 - MINI (320bytes) 
2095         UsbCommand c 
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}}; 
2099         WaitForResponse(CMD_ACK
,&resp
); 
2101         if(resp
.arg
[0] == 0) { 
2102                 PrintAndLog("iso14443a card select failed"); 
2106         iso14a_card_select_t 
*card 
= (iso14a_card_select_t 
*)resp
.d
.asBytes
; 
2108         PrintAndLog("Trying to detect card size."); 
2112         atqa 
= (card
->atqa
[1] & 0xff) << 8; 
2113     atqa 
+= card
->atqa
[0] & 0xff; 
2116         // https://code.google.com/p/libnfc/source/browse/libnfc/target-subr.c 
2118         PrintAndLog("found ATAQ: %04X SAK: %02X", atqa
, sak
); 
2121         // NXP MIFARE Mini 0.3k 
2122         if ( ( (atqa 
& 0xff0f) == 0x0004) && (sak 
== 0x09) ) return 0; 
2124         // MIFARE Classic 1K 
2125         if ( ((atqa 
& 0xff0f) == 0x0004) && (sak 
== 0x08) ) return 1; 
2127         // MIFARE Classik 4K 
2128         if ( ((atqa 
& 0xff0f) == 0x0002) && (sak 
== 0x18) ) return 4; 
2130         // SmartMX with MIFARE 1K emulation  
2131         if ( ((atqa 
& 0xf0ff) == 0x0004) ) return 1; 
2133         // SmartMX with MIFARE 4K emulation  
2134         if ( ((atqa 
& 0xf0ff) == 0x0002) ) return 4;     
2136         // Infineon MIFARE CLASSIC 1K 
2137         if ( ((atqa 
& 0xffff) == 0x0004) && (sak 
== 0x88) ) return 1; 
2139         // MFC 4K emulated by Nokia 6212 Classic 
2140         if ( ((atqa 
& 0xffff) == 0x0002) && (sak 
== 0x38) ) return 4; 
2142         // MFC 4K emulated by Nokia 6131 NFC 
2143         if ( ((atqa 
& 0xffff) == 0x0008) && (sak 
== 0x38) ) return 4; 
2146         PrintAndLog("BEFOOO  1K %02X",  (atqa 
& 0xff0f)); 
2148         // MIFARE Plus (4 Byte UID or 4 Byte RID) 
2149         // MIFARE Plus (7 Byte UID) 
2151                         ((atqa 
& 0xffff) == 0x0002) | 
2152                         ((atqa 
& 0xffff) == 0x0004) | 
2153                         ((atqa 
& 0xffff) == 0x0042) |    
2154                         ((atqa 
& 0xffff) == 0x0044)  
2181 static command_t CommandTable
[] = 
2183   {"help",              CmdHelp
,                                1, "This help"}, 
2184   {"dbg",               CmdHF14AMfDbg
,                  0, "Set default debug mode"}, 
2185   {"rdbl",              CmdHF14AMfRdBl
,                 0, "Read MIFARE classic block"}, 
2186   //{"urdbl",              CmdHF14AMfURdBl,                 0, "Read MIFARE Ultralight block"}, 
2187   //{"urdcard",           CmdHF14AMfURdCard,               0,"Read MIFARE Ultralight Card"}, 
2188   //{"uwrbl",           CmdHF14AMfUWrBl,                0,"Write MIFARE Ultralight block"}, 
2189   {"rdsc",              CmdHF14AMfRdSc
,                 0, "Read MIFARE classic sector"}, 
2190   {"dump",              CmdHF14AMfDump
,                 0, "Dump MIFARE classic tag to binary file"}, 
2191   {"restore",   CmdHF14AMfRestore
,              0, "Restore MIFARE classic binary file to BLANK tag"}, 
2192   {"wrbl",              CmdHF14AMfWrBl
,                 0, "Write MIFARE classic block"}, 
2193   {"chk",               CmdHF14AMfChk
,                  0, "Test block keys"}, 
2194   {"mifare",    CmdHF14AMifare
,                 0, "Read parity error messages."}, 
2195   {"nested",    CmdHF14AMfNested
,               0, "Test nested authentication"}, 
2196   {"sniff",             CmdHF14AMfSniff
,                0, "Sniff card-reader communication"}, 
2197   {"sim",               CmdHF14AMf1kSim
,                0, "Simulate MIFARE card"}, 
2198   {"eclr",              CmdHF14AMfEClear
,               0, "Clear simulator memory block"}, 
2199   {"eget",              CmdHF14AMfEGet
,                 0, "Get simulator memory block"}, 
2200   {"eset",              CmdHF14AMfESet
,                 0, "Set simulator memory block"}, 
2201   {"eload",             CmdHF14AMfELoad
,                0, "Load from file emul dump"}, 
2202   {"esave",             CmdHF14AMfESave
,                0, "Save to file emul dump"}, 
2203   {"ecfill",    CmdHF14AMfECFill
,               0, "Fill simulator memory with help of keys from simulator"}, 
2204   {"ekeyprn",   CmdHF14AMfEKeyPrn
,              0, "Print keys from simulator memory"}, 
2205   {"csetuid",   CmdHF14AMfCSetUID
,              0, "Set UID for magic Chinese card"}, 
2206   {"csetblk",   CmdHF14AMfCSetBlk
,              0, "Write block into magic Chinese card"}, 
2207   {"cgetblk",   CmdHF14AMfCGetBlk
,              0, "Read block from magic Chinese card"}, 
2208   {"cgetsc",    CmdHF14AMfCGetSc
,               0, "Read sector from magic Chinese card"}, 
2209   {"cload",             CmdHF14AMfCLoad
,                0, "Load dump into magic Chinese card"}, 
2210   {"csave",             CmdHF14AMfCSave
,                0, "Save dump from magic Chinese card into file or emulator"}, 
2211   {NULL
, NULL
, 0, NULL
} 
2214 int CmdHFMF(const char *Cmd
) 
2217         WaitForResponseTimeout(CMD_ACK
,NULL
,100); 
2219   CmdsParse(CommandTable
, Cmd
); 
2223 int CmdHelp(const char *Cmd
) 
2225   CmdsHelp(CommandTable
);