【问题标题】:GetWindowThreadProcessId() IAT hooking: How compare "dwProcessID" parameter?GetWindowThreadProcessId() IAT 挂钩:如何比较“dwProcessID”参数?
【发布时间】:2018-10-12 22:49:18
【问题描述】:

我正在使用以下代码成功连接GetWindowThreadProcessId()

现在我想检查 dwProcessID 参数是否对应于确定进程的 id 并在肯定的情况下阻止执行原始函数:

Result := OldGetWindowThreadProcessId(hWnd, dwProcessID);

我试过了,但没有成功:

if dwProcessID = 12345 then exit;

这是我的完整代码:

library MyLIB;

uses
  Windows,
  ImageHlp;

{$R *.res}

type
  PGetWindowThreadProcessId = function(hWnd: THandle; dwProcessID: DWord)
    : DWord; stdcall;

var
  OldGetWindowThreadProcessId: PGetWindowThreadProcessId;

function HookGetWindowThreadProcessId(hWnd: THandle; dwProcessID: DWord)
  : DWord; stdcall;

begin
  try
    // Check if is some process
  except
    MessageBox(0, 'Error', 'HookGetWindowThreadProcessId Error', 0);
  end;
  Result := OldGetWindowThreadProcessId(hWnd, dwProcessID);
end;

procedure PatchIAT(strMod: PAnsichar; Alt, Neu: Pointer);
var
  pImportDir: pImage_Import_Descriptor;
  size: CardinaL;
  Base: CardinaL;
  pThunk: PDWORD;
begin
  Base := GetModuleHandle(nil);
  pImportDir := ImageDirectoryEntryToData(Pointer(Base), True,
    IMAGE_DIRECTORY_ENTRY_IMPORT, size);
  while pImportDir^.Name <> 0 Do
  begin
    If (lstrcmpiA(PAnsichar(pImportDir^.Name + Base), strMod) = 0) then
    begin
      pThunk := PDWORD(Base + pImportDir^.FirstThunk);
      While pThunk^ <> 0 Do
      begin
        if DWord(Alt) = pThunk^ Then
        begin
          pThunk^ := CardinaL(Neu);
        end;
        Inc(pThunk);
      end;
    end;
    Inc(pImportDir);
  end;
end;

procedure DllMain(reason: Integer);

begin
  case reason of
    DLL_PROCESS_ATTACH:
      begin
        OldGetWindowThreadProcessId := GetProcAddress(GetModuleHandle(user32),
          'GetWindowThreadProcessId');

        PatchIAT(user32, GetProcAddress(GetModuleHandle(user32),
          'GetWindowThreadProcessId'), @HookGetWindowThreadProcessId);

      end;
    DLL_PROCESS_DETACH:
      begin
      end;
  end;
end;

begin
  DllProc := @DllMain;
  DllProc(DLL_PROCESS_ATTACH);

end.

【问题讨论】:

    标签: delphi delphi-10-seattle api-hook


    【解决方案1】:

    您的PGetWindowThreadProcessId 类型和HookGetWindowThreadProcessId() 函数都错误地声明了dwProcessID 参数。它是一个输出参数,因此需要声明为var dwProcessID: DWorddwProcessID: PDWord

    然后您需要调用 OldGetWindowThreadProcessId() 来检索实际的 PID,然后才能将其与任何内容进行比较。因此,您对“在肯定情况下阻止执行原始函数”的要求是不现实的,因为您需要执行原始函数才能确定要比较的 dwProcessID 值。

    试试这个:

    type
      PGetWindowThreadProcessId = function(hWnd: THandle; var dwProcessID: DWord): DWord; stdcall;
    
    ...
    
    function HookGetWindowThreadProcessId(hWnd: THandle; var dwProcessID: DWord): DWord; stdcall;
    begin
      Result := OldGetWindowThreadProcessId(hWnd, dwProcessID);
      try
        if dwProcessID = ... then
          ...
      except
        MessageBox(0, 'Error', 'HookGetWindowThreadProcessId Error', 0);
      end;
    end;
    

    【讨论】:

    • 好的,谢谢。但基于最后一个答案see here 的示例,似乎GetWindowThreadProcessId() 仍在返回我正在比较的进程的 thread idpid,我有一个 @ 987654331@比较后。为什么会这样?
    • 因为上面的钩子代码正在调用 original GetWindowThreadProcessId() 并返回原始返回的任何内容。你想发生什么?也许您正在寻找这样的东西? Result := OldGetWindowThreadProcessId(hWnd, dwProcessID); if dwProcessID = ... then begin dwProcessID := 0; Result := 0; Exit; end;
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2021-10-24
    相关资源
    最近更新 更多