x64进程中的DLL注入不起作用

时间:2016-02-02 04:09:19

标签: delphi dll-injection

我已经制作了一个dll(32位平台),现在我想在任何x64进程中注入。

在Delphi的源代码网上发现了几个这样做的例子,但是经过测试,没有任何dll被注入任何x64进程,已经用x86进程测试时这些代码工作得非常好!。

我找到了一个独特的例子,承诺在x64进程中注入一个dll文件,但是在我的测试中,尝试在 notepad.exe 中注入一个dll时没有任何效果。

所以,这里的某个人有一个dll注入器工作的例子用于x64处理器,或者可以帮助我用这个例子执行此操作(如果可能的话)?

欢迎任何建议!

我的最后一次尝试是:

注射器

  function InjectDLL(const dwPID: DWORD; {$IFDEF UNICODE} DLLPath: PWideChar
    {$ELSE} DLLPath: PAnsiChar {$ENDIF} ): Integer;

    const
      Kernel32 = 'kernel32.dll';
    var
      dwThreadID: Cardinal;
      hProc, hThread, hKernel: THandle;
      BytesToWrite, BytesWritten: SIZE_T;
      pRemoteBuffer, pLoadLibrary: Pointer;
    begin
      hProc := OpenProcess(PROCESS_CREATE_THREAD or PROCESS_QUERY_INFORMATION or
        PROCESS_VM_OPERATION or PROCESS_VM_WRITE or PROCESS_VM_READ, False, dwPID);
      if hProc = 0 then
        exit(0);
      try
        BytesToWrite := SizeOf(WideChar) * (Length(DLLPath) + 1);
        pRemoteBuffer := VirtualAllocEx(hProc, nil, BytesToWrite, MEM_COMMIT,
          PAGE_READWRITE);
        if pRemoteBuffer = nil then
          exit(0);
        try
          if not WriteProcessMemory(hProc, pRemoteBuffer, DLLPath, BytesToWrite,
            BytesWritten) then
            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(hProc, nil, 0, pLoadLibrary, pRemoteBuffer,
            0, dwThreadID);
          try
            WaitForSingleObject(hThread, INFINITE);
          finally
            CloseHandle(hThread);
          end;
        finally
          VirtualFreeEx(hProc, pRemoteBuffer, 0, MEM_RELEASE);
        end;
      finally
        CloseHandle(hProc);
      end;
      exit(1);
    end;

begin

if InjectDLL(4864, 'C:\SampleDLL') <> 0 then begin
    ShowMessage('woO!');

end;

end.

DLL的

 uses
  System.SysUtils,
  System.Classes,
  Variants,
  Winapi.Windows;

Function StartThread(pFunction : TFNThreadStartRoutine; iPriority : Integer = Thread_Priority_Normal; iStartFlag : Integer = 0) : THandle;
var
ThreadID : DWORD;
begin
Result := CreateThread(nil, 0, pFunction, nil, iStartFlag, ThreadID);
if Result <> Null then
SetThreadPriority(Result, iPriority);
end;

Function CloseThread( ThreadHandle : THandle) : Boolean;
begin
Result := TerminateThread(ThreadHandle, 1);
CloseHandle(ThreadHandle);
end;

procedure ThisIsTheThread;
begin
 MessageBoxW(0,'I am in your target : Dll file','woO!',0)
end;

procedure Run;
Var
hThread : THandle;
begin
hThread := StartThread(@ThisIsTheThread);
hThread := StartThread(@ThisIsTheThread,THREAD_PRIORITY_ERROR_RETURN);
CloseThread(hThread);
end;


procedure mydllproc(Reason: Integer);
begin
  case Reason of
    DLL_PROCESS_ATTACH:
      begin
         Run;
      end;
  end;
end;

begin
  DllProc := mydllproc;
  mydllproc(DLL_PROCESS_ATTACH);

end.

PS:如上所述,32位进程正常工作。

Source

1 个答案:

答案 0 :(得分:2)

64位进程只能加载64位模块。 32位进程只能加载32位模块。

因此,应该清楚的是,您不能将32位DLL注入64位进程。为了注入64位进程,您需要将DLL重新编译为64位模块。

一旦你这样做,你将不得不改变你的DLL。您不能在DllMain中做很多事情,详见MSDN文档。当然显示对话是完全违反规则。从CreateThread致电DllMain并在那里开展工作。

相关问题