【问题标题】:Is possible hook EnumWindowsProc callback function?是否可以挂钩 EnumWindowsProc 回调函数?
【发布时间】:2018-07-10 13:38:57
【问题描述】:

我想知道是否可以使用inline hook 方法挂钩一个回调函数,例如EnumWindowsProc()?如果是,可以提供一个代码 sn-p(示例)吗?

谢谢。


版本:

EnumWindowsProc 是在其他应用中实现的回调。我没有在我的应用程序中调用它。

我想通过 dll 注入将 EnumWindowsProc 挂接到另一个应用程序中。

【问题讨论】:

  • 您必须先挂钩EnumWindows() 才能访问传递给它的任何回调函数。然后你可以在第一次调用这些回调时挂钩它们

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


【解决方案1】:

你必须首先处理 EnumWindows,然后你必须将指向原始 EnumWindowsProc 的指针替换为自己。

我的例子是有效的 fow win32

unit Patch;

interface

procedure PatchEnumWindows(Patch: Boolean);

implementation
uses SysUtils, SyncObjs, Windows;

const
  INSTR_SIZE = 6;

var
  OldEnumWindows: array [0..INSTR_SIZE-1] of Byte;
  EnumWindowsPatched: Boolean = False;

function PatchedEnumWindows(EnumWindowsProc: Pointer; Param: Pointer); stdcall;
begin
  // You have to replace original EnumWindowsProc to yourself
end;


procedure ApiRedirect(OrigFunction, NewFunction: Pointer; var Old);
const
  TEMP_JMP: array[0..INSTR_SIZE-1] of Byte = ($E9,$90,$90,$90,$90,$C3);
var
  JmpSize: DWORD;
  JMP: array [0..INSTR_SIZE-1] of Byte;
  OldProtect: DWORD;
begin
  Move(TEMP_JMP, JMP, INSTR_SIZE);
  JmpSize := DWORD(NewFunction) - DWORD(OrigFunction) - 5;
  if not VirtualProtect(LPVOID(OrigFunction), INSTR_SIZE, PAGE_EXECUTE_READWRITE,         OldProtect) then
    raise Exception.CreateFmt('%s', [SysErrorMessage(GetLastError)]);
  Move(OrigFunction^, Old, INSTR_SIZE);
  Move(JmpSize, JMP[1], 4);
  Move(JMP, OrigFunction^, INSTR_SIZE);
  VirtualProtect(LPVOID(OrigFunction), INSTR_SIZE, OldProtect, nil);
end;

procedure PatchEnumWindows(Patch: Boolean);
var
  OrigEnumWindows: Pointer;
begin
  if Patch <> EnumWindowsProcPatched then begin
    OrigEnumWindows := GetProcAddress(GetModuleHandle('user32.dll'), 'EnumWindows');
    if Patch then begin
      ApiRedirect(OrigEnumWindows, @PatchedEnumWindows, OldEnumWindows);
    end
    else begin
      Move(OldEnumWindows, OrigEnumWindows, INSTR_SIZE);
    end;
    EnumWindowsPatched := Patch;
  end;
end;


end.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多