Q
qinglan911
Unregistered / Unconfirmed
GUEST, unregistred user!
unit APIHook;
interface
uses
SysUtils,
Windows, WinSock, Dialogs, registry;
type
//要HOOK的API函数定义
// TSockProc = function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
Tregproc = function(hKey: hKey; lpValueName: PAnsiChar; Reserved: DWORD; dwType: DWORD; lpData: Pointer; cbData: DWORD): Longint; stdcall;
PJmpCode = ^TJmpCode;
TJmpCode = packed record
JmpCode: BYTE;
Address: Tregproc;
MovEAX: array[0..2] of BYTE;
end;
//--------------------函数声明---------------------------
procedure HookAPI;
procedure UnHookAPI;
var
Oldreg : Tregproc; //原来的API地址
JmpCode : TJmpCode;
Oldproc : array[0..1] of TJmpCode;
Addreg : Pointer; //API地址
TmpJmp : TJmpCode;
ProcessHandle : THandle;
implementation
function GetRootKey(hKey: hKey): Pchar;
begin
if hKey = HKEY_CLASSES_ROOT then
Result := 'HKEY_CLASSES_ROOT'
else if hKey = HKEY_CURRENT_CONFIG then
Result := 'HKEY_CURRENT_CONFIG'
else if hKey = HKEY_LOCAL_MACHINE then
Result := 'HKEY_LOCAL_MACHINE'
else if hKey = HKEY_USERS then
Result := 'HKEY_USERS'
else if hKey = HKEY_PERFORMANCE_DATA then
Result := 'HKEY_PERFORMANCE_DATA'
else
Result := '未知根路径';
end;
{---------------------------------------}
{函数功能:Send函数的HOOK
{函数参数:同Send
{函数返回值:integer
{---------------------------------------}
function Myreg(hKey: hKey; lpValueName: PAnsiChar; Reserved: DWORD; dwType: DWORD; lpData: Pointer; cbData: DWORD): Longint; stdcall;
var
dwSize : cardinal;
yeah : textfile;
str : array of char;
begin
//这个怎么转回来我没写明白,高人指点
{setlength(str,cbData+10);
copymemory(str,@lpData,cbData-1); }
//如需要保存LOG请去掉注释!
{assignfile(yeah,'c:/apihook.ini');
append(yeah);
writeln(yeah,'reg:'+GetRootKey(HKEY)+'1'+STRING(STR)+'1'+string(lpValueName));
closefile(yeah); }
//调用直正的Send函数
WriteProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);
if MessageDlg('被监视程序即将在注册表中写入值:' + #13 + string(lpValueName) + #13 + '是否允许?', mtWarning, [mbYes, mbNo], 1) = 6 then
begin
Result := Oldreg(hKey, lpValueName, Reserved, dwType, lpData, cbData);
end
else
begin
Result := 0;
end;
JmpCode.Address := @Myreg;
WriteProcessMemory(ProcessHandle, Addreg, @JmpCode, 8, dwSize);
end;
{------------------------------------}
{过程功能:HookAPI
{过程参数:无
{------------------------------------}
procedure HookAPI;
var
DLLModule : THandle;
dwSize : cardinal;
begin
ProcessHandle := GetCurrentProcess;
DLLModule := LoadLibrary('advapi32.dll');
Addreg := GetProcAddress(DLLModule, 'RegSetValueExA'); //取得API地址
JmpCode.JmpCode := $B8;
JmpCode.MovEAX[0] := $FF;
JmpCode.MovEAX[1] := $E0;
JmpCode.MovEAX[2] := 0;
ReadProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);
JmpCode.Address := @Myreg;
WriteProcessMemory(ProcessHandle, Addreg, @JmpCode, 8, dwSize); //修改Send入口
Oldreg := Addreg;
end;
{------------------------------------}
{过程功能:取消HOOKAPI
{过程参数:无
{------------------------------------}
procedure UnHookAPI;
var
dwSize : cardinal;
begin
WriteProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);
end;
end.
如何修改这段代码,让它1只监视HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run
interface
uses
SysUtils,
Windows, WinSock, Dialogs, registry;
type
//要HOOK的API函数定义
// TSockProc = function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
Tregproc = function(hKey: hKey; lpValueName: PAnsiChar; Reserved: DWORD; dwType: DWORD; lpData: Pointer; cbData: DWORD): Longint; stdcall;
PJmpCode = ^TJmpCode;
TJmpCode = packed record
JmpCode: BYTE;
Address: Tregproc;
MovEAX: array[0..2] of BYTE;
end;
//--------------------函数声明---------------------------
procedure HookAPI;
procedure UnHookAPI;
var
Oldreg : Tregproc; //原来的API地址
JmpCode : TJmpCode;
Oldproc : array[0..1] of TJmpCode;
Addreg : Pointer; //API地址
TmpJmp : TJmpCode;
ProcessHandle : THandle;
implementation
function GetRootKey(hKey: hKey): Pchar;
begin
if hKey = HKEY_CLASSES_ROOT then
Result := 'HKEY_CLASSES_ROOT'
else if hKey = HKEY_CURRENT_CONFIG then
Result := 'HKEY_CURRENT_CONFIG'
else if hKey = HKEY_LOCAL_MACHINE then
Result := 'HKEY_LOCAL_MACHINE'
else if hKey = HKEY_USERS then
Result := 'HKEY_USERS'
else if hKey = HKEY_PERFORMANCE_DATA then
Result := 'HKEY_PERFORMANCE_DATA'
else
Result := '未知根路径';
end;
{---------------------------------------}
{函数功能:Send函数的HOOK
{函数参数:同Send
{函数返回值:integer
{---------------------------------------}
function Myreg(hKey: hKey; lpValueName: PAnsiChar; Reserved: DWORD; dwType: DWORD; lpData: Pointer; cbData: DWORD): Longint; stdcall;
var
dwSize : cardinal;
yeah : textfile;
str : array of char;
begin
//这个怎么转回来我没写明白,高人指点
{setlength(str,cbData+10);
copymemory(str,@lpData,cbData-1); }
//如需要保存LOG请去掉注释!
{assignfile(yeah,'c:/apihook.ini');
append(yeah);
writeln(yeah,'reg:'+GetRootKey(HKEY)+'1'+STRING(STR)+'1'+string(lpValueName));
closefile(yeah); }
//调用直正的Send函数
WriteProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);
if MessageDlg('被监视程序即将在注册表中写入值:' + #13 + string(lpValueName) + #13 + '是否允许?', mtWarning, [mbYes, mbNo], 1) = 6 then
begin
Result := Oldreg(hKey, lpValueName, Reserved, dwType, lpData, cbData);
end
else
begin
Result := 0;
end;
JmpCode.Address := @Myreg;
WriteProcessMemory(ProcessHandle, Addreg, @JmpCode, 8, dwSize);
end;
{------------------------------------}
{过程功能:HookAPI
{过程参数:无
{------------------------------------}
procedure HookAPI;
var
DLLModule : THandle;
dwSize : cardinal;
begin
ProcessHandle := GetCurrentProcess;
DLLModule := LoadLibrary('advapi32.dll');
Addreg := GetProcAddress(DLLModule, 'RegSetValueExA'); //取得API地址
JmpCode.JmpCode := $B8;
JmpCode.MovEAX[0] := $FF;
JmpCode.MovEAX[1] := $E0;
JmpCode.MovEAX[2] := 0;
ReadProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);
JmpCode.Address := @Myreg;
WriteProcessMemory(ProcessHandle, Addreg, @JmpCode, 8, dwSize); //修改Send入口
Oldreg := Addreg;
end;
{------------------------------------}
{过程功能:取消HOOKAPI
{过程参数:无
{------------------------------------}
procedure UnHookAPI;
var
dwSize : cardinal;
begin
WriteProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);
end;
end.
如何修改这段代码,让它1只监视HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run