Solved

Use Madshi's MadCodeHook to Hook winsock2

Posted on 2003-11-28
4
833 Views
Last Modified: 2012-06-21
Anyone know how to use Madshi's MadCodeHook to Hook winsock2's recv and send function?
please give me an example
Thanks a lot
sorry for my poor english...
0
Comment
Question by:xmichen
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 9841873
The following code should work, I guess. I've not tested it, though. Just typed it in here without even test compiling. It's not that difficult, is it?

Regards, Madshi.


library HookWinSock2;

uses Windows, WinSock, madCodeHook;

var
  sendNext : function (socket: TSocket; buf: pointer; len, flags: integer) : integer; stdcall;
  recvNext : function (socket: TSocket; buf: pointer; len, flags: integer) : integer; stdcall;

function sendCallback(socket: TSocket; buf: pointer; len, flags: integer) : integer; stdcall;
begin
  result := sendNext(socket, buf, len, flags);
end;

function recvCallback(socket: TSocket; buf: pointer; len, flags: integer) : integer; stdcall;
begin
  result := recvNext(socket, buf, len, flags);
end;

begin
  HookAPI('ws2_32.dll', 'send', @sendCallback, @sendNext);
  HookAPI('ws2_32.dll', 'recv', @recvCallback, @recvNext);
end.
0
 

Author Comment

by:xmichen
ID: 9841951
I have wrote my code like this
And my dll is global hook
If other application send packet begin with $AA001410, that application sometime will be crash...

type
  SockBuf    = array[0..4095] of Byte;

var
  sendNext : function( s       : Integer;
               const buffer  : SockBuf;
               length  : Integer;
               flags   : Integer ):Integer; stdcall;
function sendCallback( s       : Integer;
               const buffer  : SockBuf;
               length  : Integer;
               flags   : Integer ):Integer; stdcall;
var
  i : Integer;
begin
  if (buffer[0] = $AA) AND (buffer[1] = $0)
     AND (buffer[2] = $14) AND (buffer[3] = $10) then
  begin
    for i:=0 to 10 do
    begin
     AccName := AccName + Chr(buffer[i + 16]);
    end;
  end;
  Result := sendNext(s, buffer, length, flags);
end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 9841960
Please check whether the buffer is assigned:

if @buffer <> nil then begin
  // all the other code here
end;

Does the solve the problem?
0
 

Author Comment

by:xmichen
ID: 9841970
Oh, I think that is the pocket less then 26 byte...
Thank madshi~~~
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now