Delphi 资源管理器在CreateProcess挂钩上崩溃

Delphi 资源管理器在CreateProcess挂钩上崩溃,delphi,hook,Delphi,Hook,我在explorer.exe中注入了一个DLL来钩住CreateProcess,这样我可以在用户打开一些可执行文件时拦截(我选择这个钩子方法是因为我试图了解更多关于钩子的信息,我知道可以使用WMI或其他方法来完成)。 我用来钩住的库是: 钩子正在工作,我执行的每个应用程序都会弹出我在HookProc中设置的messagebox,但是在messagebox之后,explorer.exe崩溃。 注入DLL的代码工作正常,如果我只注入一个空DLL或一个只有messagebox的DLL,一切正常。所以

我在explorer.exe中注入了一个DLL来钩住CreateProcess,这样我可以在用户打开一些可执行文件时拦截(我选择这个钩子方法是因为我试图了解更多关于钩子的信息,我知道可以使用WMI或其他方法来完成)。 我用来钩住的库是:

钩子正在工作,我执行的每个应用程序都会弹出我在HookProc中设置的messagebox,但是在messagebox之后,explorer.exe崩溃。 注入DLL的代码工作正常,如果我只注入一个空DLL或一个只有messagebox的DLL,一切正常。所以我相信问题出在钩子的设置上。以下是DLL代码:

library DLL;

uses
  Windows, DDetours;

{$R *.res}

var
  CreateProcessHook: function(var lpApplicationName:String;
            lpCommandLine:String;
            lpProcessAttributes:IntPtr;
            lpThreadAttributes:IntPtr;
            bInheritHandles:Boolean;
            dwCreationFlags:Int32;
            lpEnvironment:IntPtr;
            lpCurrentDirectory:IntPtr;
            lpStartupInfo:STARTUPINFO;
            lpProcessInformation:PROCESS_INFORMATION): Boolean; stdcall = nil;

function InterceptCreateProcess(lpApplicationName:String;
            lpCommandLine:String;
            lpProcessAttributes:IntPtr;
            lpThreadAttributes:IntPtr;
            bInheritHandles:Boolean;
            dwCreationFlags:Int32;
            lpEnvironment:IntPtr;
            lpCurrentDirectory:IntPtr;
            lpStartupInfo:STARTUPINFO;
            lpProcessInformation:PROCESS_INFORMATION): Boolean; stdcall;
  begin
    MessageBoxA(0, 'Process created :)', 'Hooked', 0);
  end;

procedure DLLMain(dwReason: DWORD);
begin
  case dwReason of
  DLL_PROCESS_ATTACH:
  begin
    MessageBoxA(0,'Injected', 'Injected', MB_OK);
    @CreateProcessHook:= InterceptCreate(@CreateProcess, @InterceptCreateProcess);
  end;
  end;
end;

begin
 DLLProc := @DLLMain;
 DLLMain(DLL_PROCESS_ATTACH);
end.
正如您所看到的,InterceptCreateProcess只显示了一个消息框,当我打开一些可执行文件时,这就起作用了,但是如上所述,explorer崩溃了。我认为这就是CreateProcess函数变量的声明。有什么建议吗?
所有内容都是64位的

您的
CreateProcess
绕道签名完全错误。该函数是Win32函数,不在Delphi字符串上运行。最后两个参数是指向结构的指针

第一步是修复这些签名。使用RTL中
Windows
单元的签名

它看起来像这样:

function CreateProcessW(
  lpApplicationName: PWideChar;
  lpCommandLine: PWideChar;
  lpProcessAttributes: PSecurityAttributes;
  lpThreadAttributes: PSecurityAttributes;
  bInheritHandles: BOOL;
  dwCreationFlags: DWORD;
  lpEnvironment: Pointer;
  lpCurrentDirectory: PWideChar;
  const lpStartupInfo: STARTUPINFO;
  var lpProcessInformation: PROCESS_INFORMATION
): BOOL; stdcall;

钩子函数与
CreateProcess()
的正确签名不匹配。请尝试以下方法:

library DLL;

uses
  Windows, DDetours;

{$R *.res}

var
  CreateProcessHook: function(lpApplicationName: PChar;
            lpCommandLine: PChar;
            lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
            bInheritHandles: BOOL;
            dwCreationFlags: DWORD;
            lpEnvironment: Pointer;
            lpCurrentDirectory: PChar;
            const lpStartupInfo: STARTUPINFO;
            var lpProcessInformation: PROCESS_INFORMATION): BOOL; stdcall = nil;

function InterceptCreateProcess(lpApplicationName: PChar;
            lpCommandLine: PChar;
            lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
            bInheritHandles: BOOL;
            dwCreationFlags: DWORD;
            lpEnvironment: Pointer;
            lpCurrentDirectory: PChar;
            const lpStartupInfo: STARTUPINFO;
            var lpProcessInformation: PROCESS_INFORMATION): BOOL; stdcall;
begin
  Result := CreateProcessHook(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);    
  MessageBox(0, 'CreateProcess', 'Hooked', 0);
end;

procedure DLLMain(dwReason: DWORD);
begin
  case dwReason of
    DLL_PROCESS_ATTACH:
    begin
      @CreateProcessHook := InterceptCreate(@CreateProcess, @InterceptCreateProcess);
      MessageBox(0, 'Injected', 'Injected', MB_OK);
    end;
    DLL_PROCESS_DETACH:
    begin
      InterceptRemove(@CreateProcessHook);
    end;
  end;
end;

begin
 DLLProc := @DLLMain;
 DLLMain(DLL_PROCESS_ATTACH);
end.

是的,我试过了。但仍然崩溃。等等,我再试一次,把签名贴在这里。不完全是。看我的答案,不。你只需要照我在回答中说的去做。使用与Windows单元中完全相同的定义。我回滚了您的编辑,因为它使我的答案无效。好的。没问题。我来检查一下Windows设备。谢谢大家的帮助。与另一个问题相关的评论属于那个问题,而不是这个问题。是的,这很有效。奇怪的是,当我尝试使用与msdn完全相同的签名时(正如我在主要问题中所说的),仍然失败。您使用的签名与msdn中的签名不同。现在,我将
Result:=CreateProcessHook(lpApplicationName…
试图打开我之前请求的进程,但我遇到了一个Windows错误,例如:系统找不到指定的文件…以及我打开的文件的正确路径。知道如何打开该文件吗?您是否验证了绕道收到的路径(在将被截获的
CreateProcess()
)传递到蹦床之前,传递给它的路径不是正确的吗?是的,因为路径显示在错误框中。它是正确的。可能与Unicode和Ansi有关……但我尝试更改如下:
结果:=CreateProcessHook(PAnsiChar(lpApplicationName)
但不起作用。你有使用C#的经验吗?