program Project2;
uses
Windows;
procedure SetKernelFunBind(TargetModule, TargetProc: string; MyProc: pointer);
var
OldProtect: longword;
KerFunProc: pointer;
ret: dword; addr: Integer; jmp: Byte;
begin
//api地址
KerFunProc := GetProcAddress(LoadLibrary(pchar(TargetModule)), pchar(TargetProc));
//如果被断直接退吧
if byte(KerFunProc^) = $CC then Exit;
//修改MyProc的内存保护方式
VirtualProtect(MyProc, 10, PAGE_EXECUTE_READWRITE, OldProtect);
//复制系统api函数的前5字节覆盖到本地
WriteProcessMemory($FFFFFFFF, MyProc, KerFunProc, 5, ret);
//谢入跳转数据 修改本地函数执行流程
jmp := $E9;
WriteProcessMemory($FFFFFFFF, pointer(longword(MyProc) + 5), @jmp, 1, ret);
addr := longword(KerFunProc) - longword(MyProc) - 5;
WriteProcessMemory($FFFFFFFF, pointer(longword(MyProc) + 5 + 1), @addr, 4, ret);
//恢复保护方式
VirtualProtect(MyProc, 10, OldProtect, OldProtect);
end;
function msgbox(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;
begin
MessageBoxA(0, '要上班啦!', 0, 0);
//因为SetKernelFunBind判断CC断点,所以这里也可以做一下简单的端点检测
end;
begin
msgbox(0, '国庆快乐', 0, 0); //这里尚未绑定api函数,所以只会弹出 要上班啦!
SetKernelFunBind('user32.dll', 'MessageBoxA', @msgbox); //绑定函数msgbox到系统api函数的MessageBoxA
msgbox(0, '国庆快乐', 0, 0); //以MessageBoxA来执行参数,弹出 国庆快乐
end.