为啥一直出错呀?
unit apihook ;
interface
uses
SysUtils, Windows, WinSock, Dialogs;
type
{ 要HOOK的API函数定义 }
TWideToMulti = function (cp:cardinal; dwflags:cardinal; lpWideChar:PWideChar;
cchWideChar:Integer; lpMultiByteStr:PAnsiChar; cchMultiByte:Integer;
lpDefaultChar:PAnsiChar; lpUsedDefaultChar:PBOOL): Integer; stdcall;
procedure HookAPI;
procedure UnHookAPI;
var
ProcessHandle: HWND;
BaseAddress: Pointer;
OldProc: array [0..7] of Byte;
NewPorc: array [0..7] of Byte;
implementation
function MyWideToMulti(cp:cardinal; dwflags:cardinal; lpWideChar:PWideChar;
cchWideChar:Integer; lpMultiByteStr:PAnsiChar; cchMultiByte:Integer;
lpDefaultChar:PAnsiChar; lpUsedDefaultChar:PBOOL): Integer; stdcall;
var
nSize: Cardinal;
begin
{ 显示}
ShowMessage(string(lpWideChar));
WriteProcessMemory(ProcessHandle, BaseAddress, @OldProc, 8, nSize);//這一句是修改成原來函數的地址,
Result := WideCharToMultiByte(cp, dwflags, lpWideChar,
cchWideChar, lpMultiByteStr, cchMultiByte,
lpDefaultChar, lpUsedDefaultChar) ;
WriteProcessMemory(ProcessHandle, BaseAddress, @NewPorc, 8, nSize);//在程序正常打開後再修改成自定義函數的地址,使所有程序在下次打開時都要先調用自定義函數
// UnHookAPI;
end;
procedure HookAPI;
var
DLLModule: THandle;
nSize: Cardinal;
Dat: DWORD;
Tmp : array [0..3] of Byte;
begin
ProcessHandle := GetCurrentProcess();
DLLModule := LoadLibrary('kernel32.dll');
{ 系统函数入口点地址 }
BaseAddress := GetProcAddress(DLLModule,'WideCharToMultiByte');
Dat := DWORD(@MyWideToMulti);
Move(Dat, Tmp, 4);
NewPorc[0] := $B8; {B8 00400000 mov eax,4000}
NewPorc[1] := Tmp[0]; { 跳转到自身的函数 }
NewPorc[2] := Tmp[1];
NewPorc[3] := Tmp[2];
NewPorc[4] := Tmp[3];
NewPorc[5] := $FF; {FFE0 jmp eax}
NewPorc[6] := $E0;
NewPorc[7] := 0;
{ 读取系统函数内存地址 }
ReadProcessMemory(ProcessHandle, BaseAddress, @OldProc, 8, nSize);
{ 用自己的函数地址覆盖系统的函数地址 }
WriteProcessMemory(ProcessHandle, BaseAddress, @NewPorc, 8, nSize);
CloseHandle(ProcessHandle);
end;
procedure UnHookAPI;
var
nSize: Cardinal;
begin
{ 恢复所修改的地址 }
WriteProcessMemory(ProcessHandle, BaseAddress, @OldProc, 8, nSize);
end;
end.
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)