为了账号安全,请及时绑定邮箱和手机立即绑定

在delphi中打补丁例程调用

在delphi中打补丁例程调用

侃侃无极 2019-11-30 14:07:08
我想修补例行调用,以便自己进行一些修改即可处理它。我正在写一个资源加载器。我想用我的来修补Delphi的LoadResourceModule和InitInheritedComponent例程。我已经检查了MadExcept.pas单元中的PatchAPI调用,但无法确定是否可以将其用于我的项目。我想要类似的东西我的exe在运行时调用-> LoadResourceModule->跳至-> MyCustomResourceModule ...关于此的任何指示将非常有帮助。
查看完整描述

3 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

我使用以下代码:


procedure PatchCode(Address: Pointer; const NewCode; Size: Integer);

var

  OldProtect: DWORD;

begin

  if VirtualProtect(Address, Size, PAGE_EXECUTE_READWRITE, OldProtect) then 

  begin

    Move(NewCode, Address^, Size);

    FlushInstructionCache(GetCurrentProcess, Address, Size);

    VirtualProtect(Address, Size, OldProtect, @OldProtect);

  end;

end;


type

  PInstruction = ^TInstruction;

  TInstruction = packed record

    Opcode: Byte;

    Offset: Integer;

  end;


procedure RedirectProcedure(OldAddress, NewAddress: Pointer);

var

  NewCode: TInstruction;

begin

  NewCode.Opcode := $E9;//jump relative

  NewCode.Offset := NativeInt(NewAddress)-NativeInt(OldAddress)-SizeOf(NewCode);

  PatchCode(OldAddress, NewCode, SizeOf(NewCode));

end;

您可以通过调用来实现钩子/补丁/绕行RedirectProcedure:


RedirectProcedure(@LoadResourceModule, @MyLoadResourceModule);

这将适用于32位代码。如果旧功能和新功能都位于同一可执行模块中,则它也适用于64位代码。否则,跳转距离可能会超出32位整数的范围。


如果有人可以提供一种适用于64位地址


查看完整回答
反对 回复 2019-11-30
  • 3 回答
  • 0 关注
  • 615 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信