那位高人帮忙看看这段HookAPI 代码(Hook 'ws2_32.dll'中的Send,Recv函数)的毛病所在? (100分)

  • 主题发起人 主题发起人 zfine
  • 开始时间 开始时间
Z

zfine

Unregistered / Unconfirmed
GUEST, unregistred user!
这个例子可以编译通过,但只能拦截Send这个函数,Recv怎么都反映。代码如下:<br>(在此先谢谢了。找出问题200分送上)<br>OOK.DLL的代码:<br>library Hook;<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; windows,<br>&nbsp; Messages,<br>&nbsp; APIHook in 'APIHook.pas';<br><br>type<br>&nbsp; PData = ^TData;<br>&nbsp; TData = record<br>&nbsp; &nbsp; Hook: THandle;<br>&nbsp; &nbsp; Hooked: Boolean;<br>&nbsp; end;<br>&nbsp; <br>var<br>&nbsp; DLLData: PData;<br><br>{------------------------------------}<br>{过程名:HookProc<br>{过程功能:HOOK过程<br>{过程参数:nCode, wParam, lParam消息的相<br>{ &nbsp; &nbsp; &nbsp; &nbsp; 关参数<br>{------------------------------------}<br>procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall;<br>begin<br>&nbsp; if not DLLData^.Hooked then<br>&nbsp; begin<br>&nbsp; &nbsp; HookAPI;<br>&nbsp; &nbsp; DLLData^.Hooked := True;<br>&nbsp; end;<br>&nbsp; //调用下一个Hook<br>&nbsp; CallNextHookEx(DLLData^.Hook, nCode, wParam, lParam);<br>end;<br><br><br>{------------------------------------}<br>{函数名:InstallHook<br>{函数功能:在指定窗口上安装HOOK<br>{函数参数:sWindow:要安装HOOK的窗口<br>{返回值:成功返回TRUE,失败返回FALSE<br>{------------------------------------}<br>function InstallHook(SWindow: LongWORD):Boolean;stdcall;<br>var<br>&nbsp; ThreadID: LongWORD;<br>begin<br>&nbsp; Result := False;<br>&nbsp; DLLData^.Hook := 0;<br>&nbsp; if DLLData^.Hook &gt; 0 then<br>&nbsp; &nbsp; Result := True &nbsp;//是否成功HOOK<br>&nbsp; else<br>&nbsp; &nbsp; exit;<br>end;<br><br>{------------------------------------}<br>{过程名:UnHook<br>{过程功能:卸载HOOK<br>{过程参数:无<br>{------------------------------------}<br>procedure UnHook;stdcall;<br>begin<br>&nbsp; UnHookAPI;<br>&nbsp; //卸载Hook<br>&nbsp; UnhookWindowsHookEx(DLLData^.Hook);<br>end;<br><br>{------------------------------------}<br>{过程名:DLL入口函数<br>{过程功能:进行DLL初始化,释放等<br>{过程参数:DLL状态<br>{------------------------------------}<br>procedure MyDLLHandler(Reason: Integer);<br>var<br>&nbsp; FHandle: LongWORD;<br>begin<br>&nbsp; case Reason of<br>&nbsp; &nbsp; DLL_PROCESS_ATTACH:<br>&nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//建立文件映射,以实现DLL中的全局变量if FHandle = 0 then<br>&nbsp; &nbsp; &nbsp; if GetLastError = ERROR_ALREADY_EXISTS then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if FHandle = 0 then Exit;<br>&nbsp; &nbsp; &nbsp; end else Exit;<br>&nbsp; &nbsp; &nbsp; if DLLData = nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(FHandle);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; DLL_PROCESS_DETACH:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if Assigned(DLLData) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; UnmapViewOfFile(DLLData);<br>&nbsp; &nbsp; &nbsp; &nbsp; DLLData := nil;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>{$R *.res}<br>exports<br>&nbsp; InstallHook, UnHook, HookProc;<br><br>begin<br>&nbsp; DLLProc := @MyDLLHandler;<br>&nbsp; MyDLLhandler(DLL_PROCESS_ATTACH);<br>&nbsp; DLLData^.Hooked := False;<br>end.<br><br>----------------------------------------------------------------------------------------<br>APIHook.Pas的代码:<br><br>unit APIHook;<br><br>interface<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Windows, WinSock;<br><br>type<br>&nbsp; //要HOOK的API函数定义<br>&nbsp; TSockProc = function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;<br><br>&nbsp; PJmpCode = ^TJmpCode;<br>&nbsp; TJmpCode = packed record<br>&nbsp; &nbsp; JmpCode: BYTE;<br>&nbsp; &nbsp; Address: TSockProc;<br>&nbsp; &nbsp; MovEAX: Array [0..2] of BYTE;<br>&nbsp; end;<br><br>&nbsp; //--------------------函数声明---------------------------<br>&nbsp; procedure HookAPI;<br>&nbsp; procedure UnHookAPI;<br><br>var<br>&nbsp; OldSend, OldRecv: TSockProc; &nbsp; &nbsp; &nbsp;//原来的API地址<br>&nbsp; JmpCode: TJmpCode;<br>&nbsp; OldProc: array [0..1] of TJmpCode;<br>&nbsp; AddSend, AddRecv: pointer; &nbsp; &nbsp; &nbsp; &nbsp;//API地址<br>&nbsp; TmpJmp: TJmpCode;<br>&nbsp; ProcessHandle: THandle;<br>implementation<br><br>{---------------------------------------}<br>{函数功能:Send函数的HOOK<br>{函数参数:同Send<br>{函数返回值:integer<br>{---------------------------------------}<br>function MySend(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;<br>var<br>&nbsp; dwSize: cardinal;<br>begin<br>&nbsp; //这儿进行发送的数据处理<br>&nbsp; MessageBeep(1000); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //简单的响一声<br>&nbsp; //调用直正的Send函数<br>&nbsp; WriteProcessMemory(ProcessHandle, AddSend, @OldProc[0], 8, dwSize);<br>&nbsp; Result := OldSend(S, Buf, len, flags);<br>&nbsp; JmpCode.Address := @MySend;<br>&nbsp; WriteProcessMemory(ProcessHandle, AddSend, @JmpCode, 8, dwSize);<br>end;<br><br>{---------------------------------------}<br>{函数功能:Recv函数的HOOK<br>{函数参数:同Recv<br>{函数返回值:integer<br>{---------------------------------------}<br>function MyRecv(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;<br>var<br>&nbsp; dwSize: cardinal;<br>begin<br>&nbsp; //这儿进行接收的数据处理<br>&nbsp; MessageBeep(1000); &nbsp; &nbsp; &nbsp; &nbsp; //简单的响一声<br>&nbsp; //调用直正的Recv函数<br>&nbsp; WriteProcessMemory(ProcessHandle, AddRecv, @OldProc[1], 8, dwSize);<br>&nbsp; Result := OldRecv(S, Buf, len, flags);<br>&nbsp; JmpCode.Address := @MyRecv;<br>&nbsp; WriteProcessMemory(ProcessHandle, AddRecv, @JmpCode, 8, dwSize);<br>end;<br><br>{------------------------------------}<br>{过程功能:HookAPI<br>{过程参数:无<br>{------------------------------------}<br>{------------------------------------}<br>procedure UnHookAPI;<br>var<br>&nbsp; dwSize: Cardinal;<br>begin<br>&nbsp; WriteProcessMemory(ProcessHandle, AddSend, @OldProc[0], 8, dwSize);<br>&nbsp; WriteProcessMemory(ProcessHandle, AddRecv, @OldProc[1], 8, dwSize);<br>end;<br><br>end.<br><br>---------------------------------------------------------------------------------------------<br>编译这个DLL后,再新建一个程序调用这个DLL的InstallHook并传入目标进程的主窗口句柄就可:<br>unit fmMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; <br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; InstallHook: function (SWindow: THandle):Boolean;stdcall;<br>&nbsp; UnHook: procedure;stdcall;<br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; ModuleHandle: THandle;<br>&nbsp; TmpWndHandle: THandle;<br>begin<br>&nbsp; TmpWndHandle := 0;<br>&nbsp; TmpWndHandle := FindWindow(nil, '目标窗口的标题');<br>&nbsp; if not isWindow(TmpWndHandle) then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(self.Handle, '没有找到窗口', '!!!', MB_OK);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; ModuleHandle := LoadLibrary('Hook.dll');<br>&nbsp; @InstallHook := GetProcAddress(ModuleHandle, 'InstallHook');<br>&nbsp; @UnHook := GetProcAddress(ModuleHandle, 'UnHook');<br>&nbsp; if InstallHook(FindWindow(nil, 'Untitled')) then<br>&nbsp; &nbsp; ShowMessage('Hook OK');<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; UnHook<br>end;<br><br>end.
 
后退
顶部