如何给IE,Opera,Firefox等浏览器加钩子,高手请帮忙 ( 积分: 50 )

  • 主题发起人 主题发起人 ChJK
  • 开始时间 开始时间
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;

{------------------------------------}
{过程名:DLL入口函数
{过程功能:进行DLL初始化,释放等
{过程参数:DLL状态
{------------------------------------}
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 GetURL:PData;

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 GetURL:PData;
begin
result:= strData;
end;

end.
 
应用程序部分:
unit getExplorer;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ShareData;

const
WM_SENDATA = WM_USER+200;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
ShareMemory:PData;
procedure WMSendHOOK(var Message: TMessage); message WM_SENDATA;
public
{ Public declarations }
end;

var
Form1: TForm1;
InstallHook: function (SWindow: THandle;MainHwnd:HWND):Boolean;stdcall;
UnHook: procedure;stdcall;
procedure SetHook;

implementation

{$R *.dfm}

procedure TForm1.WMSendHOOK(var Message: TMessage);
begin
ShareMemory:= GetURL;
memo1.Lines.Add(ShareMemory^.IPstr);
end;

function EnumWindowsFunc(Handle: THandle; List: TStringList) : boolean ; stdcall;
var Caption: array[0..256] of Char;
begin
if GetWindowText
(Handle, Caption, SizeOf(Caption)-1) <> 0 then
begin
List.Add(Caption);
end;

Result :=True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SetHook;
end;

procedure SetHook;
var
ModuleHandle: THandle;
TmpWndHandle: THandle;
szText:array [0..254] of char;
CaptionStr:TStrings;
i:integer;
begin

ModuleHandle := LoadLibrary('Hook.dll');
@InstallHook := GetProcAddress(ModuleHandle, 'InstallHook');
@UnHook := GetProcAddress(ModuleHandle, 'UnHook');

TmpWndHandle := 0;
CaptionStr:=TStringList.Create;
EnumWindows(@EnumWindowsFunc, LParam(CaptionStr));//枚举所有窗口名

for i:=0 to CaptionStr.Count -1 do
if (CaptionStr<>'') and((pos('Explorer',CaptionStr)>0) or (pos('Firefox',CaptionStr

)>0)
or (pos('Tencent Traveler',CaptionStr)>0) or (pos('Opera',CaptionStr)>0) ) then

begin
TmpWndHandle := FindWindow(nil, PChar(CaptionStr));

if not isWindow(TmpWndHandle) then begin
showmessage(CaptionStr+' 不是窗口');
continue;
end;
if not InstallHook(TmpWndHandle,Form1.Handle) then
showmessage(CaptionStr+ ' 钩子安装失败');
end;
end;

end.

// ShareData单元
unit ShareData;

interface

uses
Windows;
type
PData = ^TData;
TData = record
Main:HWND;
IPstr:string[254];
end;

function GetURL: PData; stdcall;

implementation

function GetURL; external 'HOOK.DLL';

end.
 
给浏览器做钩子,拦截数据?
为什么不直接拦截80端口的数据?用HOOKAPI
 
拦截网络数据,一般情况下使用LSP就可以拦截了^_^
 
那如何拦截80接口数据啊?关键是找不到源代码,我不会写啊!LSP我也不懂,小弟太笨了,请高手再帮忙指点,谢谢各位了
 
我已经写好一个封堵80端口的DEMO,稍做修改可以满足你的要求,如果有需要留下M我发给你
 
独舞,您的demo可以得到80端口的数据吗,主要是TCP/IP的数据。谢谢你了,MYEmail:
chjunkai@126.com
 
to 暗夜中独舞,

我也想观摩下,sadnui@126.com
 
后退
顶部