Quantcast
Channel: Delphi Forum - Delphi Programming Kings of Code - Delphi Programming
Viewing all articles
Browse latest Browse all 173

DLL Injector in Delphi XE3 (32bit and 64bit App .)

$
0
0
PHP Code:
(*

  
DLL Injection in delphi :)

  
Tested on 32bit and 64Bit application

*)

function 
InjectDLL(const dwPIDDWORD; {$IFDEF UNICODEDLLPathPWideChar
{$ELSEDLLPathPAnsiChar {$ENDIF} ): Integer;

const
  
Kernel32 'kernel32.dll';
var
  
dwThreadIDCardinal;
  
hProchThreadhKernelTHandle;
  
BytesToWriteBytesWrittenSIZE_T;
  
pRemoteBufferpLoadLibraryPointer;
begin
  hProc 
:= OpenProcess(PROCESS_CREATE_THREAD or PROCESS_QUERY_INFORMATION or
    
PROCESS_VM_OPERATION or PROCESS_VM_WRITE or PROCESS_VM_READFalsedwPID);
  if 
hProc 0 then
    
exit(0);
  try
    
BytesToWrite := SizeOf(WideChar) * (Length(DLLPath) + 1);
    
pRemoteBuffer := VirtualAllocEx(hProcnilBytesToWriteMEM_COMMIT,
      
PAGE_READWRITE);
    if 
pRemoteBuffer nil then
      
exit(0);
    try
      if 
not WriteProcessMemory(hProcpRemoteBufferDLLPathBytesToWrite,
        
BytesWrittenthen
        
exit(0);
{
$REGION 'Check for UNICODE'}
{
$IFDEF UNICODE}
      
hKernel := GetModuleHandleW(Kernel32);
      
pLoadLibrary := GetProcAddress(hKernel'LoadLibraryW');
{
$ELSE}
      
hKernel := GetModuleHandleA(Kernel32);
      
pLoadLibrary := GetProcAddress(hKernel'LoadLibraryA');
{
$ENDIF}
{
$ENDREGION}
      
hThread := CreateRemoteThread(hProcnil0pLoadLibrarypRemoteBuffer,
        
0dwThreadID);
      try
        
WaitForSingleObject(hThreadINFINITE);
      
finally
        CloseHandle
(hThread);
      
end;
    
finally
      VirtualFreeEx
(hProcpRemoteBuffer0MEM_RELEASE);
    
end;
  
finally
    CloseHandle
(hProc);
  
end;
  exit(
1);
end;


// how to use ?
begin
                 
{4864 it this sample}
                {
Target process PID}   {Your dll dile path+name}
  if 
InjectDLL(4864'C:\SampleDLL') <> 0 then
    ShowMessage
('woO!');

end;
/////////////////// My Dll file \\\\\\\\\\\\\\
library SampleDLL;
uses
  System
.SysUtils,
  
System.Classes,

  
Winapi.Windows;
procedure mydllproc(ReasonInteger);
begin
  
case Reason of
    DLL_PROCESS_ATTACH
:
      
begin
        MessageBoxW
(0,'I am in your target : Dll file','woO!',0)
      
end;
  
end;
end;

begin
  DllProc 
:= mydllproc;
  
mydllproc(DLL_PROCESS_ATTACH);

end
 

Viewing all articles
Browse latest Browse all 173

Trending Articles