I konw someone can use madCodeHook to Hook Winsock sucessfully,
but I can't,
what's problem with my program? Here is my code, please help;
Code: Select all
library SockHook;
uses
SysUtils,
System,
winsock2,
windows,
Messages,
madCodeHook;
{$R *.res}
var
RecvNext: function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
SendNext: function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
WSASendNext: function (s:TSocket;lpBuffers:LPWSABUF;
dwBufferCount:dword;lpNumberOfBytesSent:LPDWORD;
dwFlags:DWORD;lpOverlapped:LPWSAOVERLAPPED;
lpCompletionROUTINE:LPWSAOVERLAPPED_COMPLETION_ROUTINE):integer;stdcall;
WSARecvNext: function (s:TSocket;lpBuffers:LPWSABUF;
dwBufferCount:dword;lpNumberOfBytesSent:LPDWORD;
dwFlags:DWORD;lpOverlapped:LPWSAOVERLAPPED;
lpCompletionROUTINE:LPWSAOVERLAPPED_COMPLETION_ROUTINE):integer;stdcall;
hParentWnd: THandle;
procedure SaveInfo(sMsg: String);
var
ds: TCopyDataStruct;
begin
ds.cbData := Length(sMsg) + 1;
GetMem(ds.lpData,ds.cbData);
StrCopy(ds.lpData,PChar(sMsg));
if hParentWND <> 0 then
SendMessage (hParentWND, WM_COPYDATA, 0,Cardinal(@ds));
FreeMem(ds.lpData);
end;
procedure GetParentHWND(s: THandle);
begin
hParentWND := s;
end;
function RecvCallback(s: TSocket; var Buf; len, flags: Integer) : Integer; stdcall;
begin
result := RecvNext(s,Buf,len,flags);
SaveInfo('Recv');
end;
function SendCallback(s: TSocket; var Buf; len, flags: Integer) : Integer; stdcall;
begin
result := SendNext(s,Buf,len,flags);
SaveInfo('Send');
end;
function WSASendHook(s:TSocket;lpBuffers:LPWSABUF;
dwBufferCount:dword;lpNumberOfBytesSent:LPDWORD;
dwFlags:DWORD;lpOverlapped:LPWSAOVERLAPPED;
lpCompletionROUTINE:LPWSAOVERLAPPED_COMPLETION_ROUTINE):integer;stdcall;
begin
WSASendHook:=WSASendNext(s,lpBuffers,dwBufferCount,
lpNumberOfBytesSent,dwFlags,lpOverlapped,lpCompletionROUTINE);
SaveInfo('WSASend');
end;
function WSARecvHook(s:TSocket;lpBuffers:LPWSABUF;
dwBufferCount:dword;lpNumberOfBytesSent:LPDWORD;
dwFlags:DWORD;lpOverlapped:LPWSAOVERLAPPED;
lpCompletionROUTINE:LPWSAOVERLAPPED_COMPLETION_ROUTINE):integer;stdcall;
begin
WSARecvHook:=WSARecvNext(s,lpBuffers,dwBufferCount,
lpNumberOfBytesSent,dwFlags,lpOverlapped,lpCompletionROUTINE);
SaveInfo('WSARecv');
end;
procedure DoHook;
begin
HookAPI('ws2_32.dll', 'recv', @RecvCallback, @RecvNext);
HookAPI('ws2_32.dll', 'send', @SendCallback, @SendNext);
HookAPI('ws2_32.dll','WSASend',@WSASendHook,@WSASendNext);
HookAPI('ws2_32.dll','WSARecv',@WSARecvHook,@WSARecvNext);
SaveInfo('Hooking.....');
end;
exports
GetParentHWND,DoHook;
begin
DoHook;
end.