C
ChJK
Unregistered / Unconfirmed
GUEST, unregistred user!
如何给IE,Opera,Firefox等浏览器加钩子,截获浏览器发出的数据,以下是我写的用钩子截获浏览器发出的数据包,但有很大问题,请高手帮忙指点!
钩子部分:
library HOOK;
uses
SysUtils,
windows,
Messages,
APIHook in 'APIHook.pas';
var
Hook1: THandle;//钩子句柄
Hooked: Boolean;//是否已经被钩上
{------------------------------------}
{过程名:HookProc
{过程功能:HOOK过程
{过程参数:nCode, wParam, lParam消息的相
{ 关参数
{------------------------------------}
procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall;
begin
if not Hookedthen
begin
HookAPI;
Hooked := True;
CallNextHookEx(Hook1, nCode, wParam, lParam);
end
end;
{------------------------------------}
{函数名:InstallHook
{函数功能:在指定窗口上安装HOOK
{函数参数:sWindow:要安装HOOK的窗口
{返回值:成功返回TRUE,失败返回FALSE
{------------------------------------}
function InstallHook(SWindow: LongWORD;MainHwnd:HWND):Boolean;stdcall;
var
ThreadID: LongWORD;
begin
Result := False;
Hook1 := 0;
strData^.Main:= MainHwnd;//用共享区域保存应用程序的窗口句柄
ThreadID := GetWindowThreadProcessId(sWindow, nil);
//给指定窗口挂上钩子
Hook1 := SetWindowsHookEx(WH_GETMESSAGE, @HookProc, Hinstance,ThreadID);
if Hook1 > 0 then
Result := True //是否成功HOOK
else
exit;
end;
{------------------------------------}
{过程名:UnHook
{过程功能:卸载HOOK
{过程参数:无
{------------------------------------}
procedure UnHook;stdcall;
begin
UnHookAPI;
//卸载Hook
UnhookWindowsHookEx(Hook1);
end;
{------------------------------------}
{过程名LL入口函数
{过程功能:进行DLL初始化,释放等
{过程参数LL状态
{------------------------------------}
procedure MyDLLHandler(Reason: Integer);
var
FHandle: LongWORD;
begin
case Reason of
DLL_PROCESS_ATTACH:
begin //建立文件映射,以实现DLL中的全局变量
FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, sizeOf(TData), 'MYDLLDATA');
if FHandle = 0 then
if GetLastError = ERROR_ALREADY_EXISTS then
begin
FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');
if FHandle = 0 then Exit;
end else Exit;
strData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if strData = nil then
CloseHandle(FHandle);
end;
DLL_PROCESS_DETACH:
begin
UnHookAPI;
if Assigned(strData) then
begin
UnmapViewOfFile(strData);
strData := nil;
end;
end;
end;
end;
{$R *.res}
exports
InstallHook, UnHook, GetURL;
begin
DLLProc := @MyDLLHandler;
MyDLLhandler(DLL_PROCESS_ATTACH);
Hooked := False;
end.
// APIHook单元
unit APIHook;
interface
uses
SysUtils,
Windows, Messages, WinSock;
const
WM_SENDAtA = WM_USER+200;
type
//要HOOK的API函数定义
TSockProc = function (s: TSocket; var Buf ; len, flags: Integer): Integer; stdcall;
PJmpCode = ^TJmpCode;
TJmpCode = packed record
JmpCode: BYTE;
Address: TSockProc;
MovEAX: Array [0..2] of BYTE;
end;
type
PData = ^TData;
TData = record
Main:HWND;//应用程序窗口句柄
IPstr:string[254];//截获的发送包,不知道有没有别的方法保存数据
end;
//--------------------函数声明---------------------------
procedure HookAPI;
procedure UnHookAPI;
function GetURLData;
var
OldSend: TSockProc; //原来的API地址
JmpCode: TJmpCode;
OldProc: TJmpCode;
AddSend: pointer; //API地址
TmpJmp: TJmpCode;
ProcessHandle: THandle;
strData: PData;
implementation
{---------------------------------------}
{函数功能:Send函数的HOOK
{函数参数:同Send
{函数返回值:integer
{---------------------------------------}
function MySend(s: TSocket;var Buf; len, flags: Integer): Integer; stdcall;
var
dwSize: cardinal;
i:integer;
b: ^Byte;
temp_str:string;
SaveFile: TextFile;
begin
//这儿进行发送的数据处理
b:=@buf ;
temp_str:='';
for i:=0 to len-1 do
begin
temp_str:= temp_str+ chr(b^);
inc(b);
end;
strData^.IPstr:= copy(temp_str,0,254);
PostMessage(strData^.Main,WM_INSTALLHOOK,0,0);
//调用真正的Send函数
WriteProcessMemory(ProcessHandle, AddSend, @OldProc, 8, dwSize);
Result := OldSend(S, Buf, len, flags);
JmpCode.Address := @MySend;
WriteProcessMemory(ProcessHandle, AddSend, @JmpCode, 8, dwSize);
end;
{------------------------------------}
{过程功能:HookAPI
{过程参数:无
{------------------------------------}
procedure HookAPI;
var
DLLModule: THandle;
dwSize: cardinal;
begin
ProcessHandle := GetCurrentProcess;
DLLModule := LoadLibrary('ws2_32.dll');
AddSend := GetProcAddress(DLLModule, 'send'); //取得API地址
JmpCode.JmpCode := $B8;
JmpCode.MovEAX[0] := $FF;
JmpCode.MovEAX[1] := $E0;
JmpCode.MovEAX[2] := 0;
ReadProcessMemory(ProcessHandle, AddSend, @OldProc, 8, dwSize);
JmpCode.Address := @MySend;
WriteProcessMemory(ProcessHandle, AddSend, @JmpCode, 8, dwSize); //修改Send入口
OldSend := AddSend;
end;
{------------------------------------}
{过程功能:取消HOOKAPI
{过程参数:无
{------------------------------------}
procedure UnHookAPI;
var
dwSize: Cardinal;
begin
WriteProcessMemory(ProcessHandle, AddSend, @OldProc, 8, dwSize);
end;
function GetURLData;
begin
result:= strData;
end;
end.
钩子部分:
library HOOK;
uses
SysUtils,
windows,
Messages,
APIHook in 'APIHook.pas';
var
Hook1: THandle;//钩子句柄
Hooked: Boolean;//是否已经被钩上
{------------------------------------}
{过程名:HookProc
{过程功能:HOOK过程
{过程参数:nCode, wParam, lParam消息的相
{ 关参数
{------------------------------------}
procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall;
begin
if not Hookedthen
begin
HookAPI;
Hooked := True;
CallNextHookEx(Hook1, nCode, wParam, lParam);
end
end;
{------------------------------------}
{函数名:InstallHook
{函数功能:在指定窗口上安装HOOK
{函数参数:sWindow:要安装HOOK的窗口
{返回值:成功返回TRUE,失败返回FALSE
{------------------------------------}
function InstallHook(SWindow: LongWORD;MainHwnd:HWND):Boolean;stdcall;
var
ThreadID: LongWORD;
begin
Result := False;
Hook1 := 0;
strData^.Main:= MainHwnd;//用共享区域保存应用程序的窗口句柄
ThreadID := GetWindowThreadProcessId(sWindow, nil);
//给指定窗口挂上钩子
Hook1 := SetWindowsHookEx(WH_GETMESSAGE, @HookProc, Hinstance,ThreadID);
if Hook1 > 0 then
Result := True //是否成功HOOK
else
exit;
end;
{------------------------------------}
{过程名:UnHook
{过程功能:卸载HOOK
{过程参数:无
{------------------------------------}
procedure UnHook;stdcall;
begin
UnHookAPI;
//卸载Hook
UnhookWindowsHookEx(Hook1);
end;
{------------------------------------}
{过程名LL入口函数
{过程功能:进行DLL初始化,释放等
{过程参数LL状态
{------------------------------------}
procedure MyDLLHandler(Reason: Integer);
var
FHandle: LongWORD;
begin
case Reason of
DLL_PROCESS_ATTACH:
begin //建立文件映射,以实现DLL中的全局变量
FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, sizeOf(TData), 'MYDLLDATA');
if FHandle = 0 then
if GetLastError = ERROR_ALREADY_EXISTS then
begin
FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');
if FHandle = 0 then Exit;
end else Exit;
strData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if strData = nil then
CloseHandle(FHandle);
end;
DLL_PROCESS_DETACH:
begin
UnHookAPI;
if Assigned(strData) then
begin
UnmapViewOfFile(strData);
strData := nil;
end;
end;
end;
end;
{$R *.res}
exports
InstallHook, UnHook, GetURL;
begin
DLLProc := @MyDLLHandler;
MyDLLhandler(DLL_PROCESS_ATTACH);
Hooked := False;
end.
// APIHook单元
unit APIHook;
interface
uses
SysUtils,
Windows, Messages, WinSock;
const
WM_SENDAtA = WM_USER+200;
type
//要HOOK的API函数定义
TSockProc = function (s: TSocket; var Buf ; len, flags: Integer): Integer; stdcall;
PJmpCode = ^TJmpCode;
TJmpCode = packed record
JmpCode: BYTE;
Address: TSockProc;
MovEAX: Array [0..2] of BYTE;
end;
type
PData = ^TData;
TData = record
Main:HWND;//应用程序窗口句柄
IPstr:string[254];//截获的发送包,不知道有没有别的方法保存数据
end;
//--------------------函数声明---------------------------
procedure HookAPI;
procedure UnHookAPI;
function GetURLData;
var
OldSend: TSockProc; //原来的API地址
JmpCode: TJmpCode;
OldProc: TJmpCode;
AddSend: pointer; //API地址
TmpJmp: TJmpCode;
ProcessHandle: THandle;
strData: PData;
implementation
{---------------------------------------}
{函数功能:Send函数的HOOK
{函数参数:同Send
{函数返回值:integer
{---------------------------------------}
function MySend(s: TSocket;var Buf; len, flags: Integer): Integer; stdcall;
var
dwSize: cardinal;
i:integer;
b: ^Byte;
temp_str:string;
SaveFile: TextFile;
begin
//这儿进行发送的数据处理
b:=@buf ;
temp_str:='';
for i:=0 to len-1 do
begin
temp_str:= temp_str+ chr(b^);
inc(b);
end;
strData^.IPstr:= copy(temp_str,0,254);
PostMessage(strData^.Main,WM_INSTALLHOOK,0,0);
//调用真正的Send函数
WriteProcessMemory(ProcessHandle, AddSend, @OldProc, 8, dwSize);
Result := OldSend(S, Buf, len, flags);
JmpCode.Address := @MySend;
WriteProcessMemory(ProcessHandle, AddSend, @JmpCode, 8, dwSize);
end;
{------------------------------------}
{过程功能:HookAPI
{过程参数:无
{------------------------------------}
procedure HookAPI;
var
DLLModule: THandle;
dwSize: cardinal;
begin
ProcessHandle := GetCurrentProcess;
DLLModule := LoadLibrary('ws2_32.dll');
AddSend := GetProcAddress(DLLModule, 'send'); //取得API地址
JmpCode.JmpCode := $B8;
JmpCode.MovEAX[0] := $FF;
JmpCode.MovEAX[1] := $E0;
JmpCode.MovEAX[2] := 0;
ReadProcessMemory(ProcessHandle, AddSend, @OldProc, 8, dwSize);
JmpCode.Address := @MySend;
WriteProcessMemory(ProcessHandle, AddSend, @JmpCode, 8, dwSize); //修改Send入口
OldSend := AddSend;
end;
{------------------------------------}
{过程功能:取消HOOKAPI
{过程参数:无
{------------------------------------}
procedure UnHookAPI;
var
dwSize: Cardinal;
begin
WriteProcessMemory(ProcessHandle, AddSend, @OldProc, 8, dwSize);
end;
function GetURLData;
begin
result:= strData;
end;
end.