Recent

Author Topic: hooking recv() with delphi, Questions.  (Read 4241 times)

shonay

  • Full Member
  • ***
  • Posts: 169
hooking recv() with delphi, Questions.
« on: July 27, 2014, 02:20:26 pm »
Good afternoon,

Been home practising with MagicApiHooks in delphi, got to findout, delphi was cooler than I thought. But today foreducational purpose only, nothing harmful, I decided to hook recv(), but all the same, the DLL doesn't seem to compile.

The area of it saving to the txt file might workbut outputting the number of bytesn it didn't compile, so I decided to put ma source code, I am teaching myself delphi, using lazarus.

Code goes like this
Code: [Select]
library recvhook;

{$mode delphi}{$H+}

uses
  Classes ,Windows, MagicApiHooks ,Winsock, SysUtils
  { you can add units after this };
var
oldrecv: Function(s:TSocket;var buf;len,flags:Integer):Integer;stdcall;
const
  DLL_PROCESS_ATTACH=1;

  function newrecv(s:TSocket;var buf;len,flags:Integer):Integer;stdcall;
  var
    f:TextFile;
    begin
      Result:=oldrecv(s,buf,len,flags);
      if FileExists(ExtractFilePath(ParamStr(0))+'log.txt') then
      begin
        AssignFile(f,ExtractFilePath(ParamStr(0))+'log.txt');
        Append(f);
        ReWrite(f);
        Writeln(f,'Bytes Recieved:'+intToStr(len)+'Bytes:'+ buf +'');
        CloseFile(f);
        Result:=recv(s,buf,len,flags);
        end;
      end;
procedure DLLEntryPoint(dwReason:DWORD);
begin
  case dwReason of
  DLL_PROCESS_ATTACH: begin
    ApiHook('ws2_32.dll','recv',nil,@newrecv,@oldrecv);
    end;
  end;
begin
  DllProc:=@DLLEntryPoint;
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.


Sorry I may be too disturbing, just needsome help here.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: hooking recv() with delphi, Questions.
« Reply #1 on: July 27, 2014, 02:35:06 pm »
It would help to know what error was on which line with which version.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: hooking recv() with delphi, Questions.
« Reply #2 on: July 27, 2014, 02:42:22 pm »
It would help to know what error was on which line with which version.

These Three areas.

Code: [Select]
  Writeln(f,'Bytes Recieved:'+intToStr(len)+'Bytes:'+ buf +'');

Code: [Select]
  DllProc

And the final
Code: [Select]
end.

I am using the previous version of lazarus, I will check the version.I downloaded from lazarus website.
« Last Edit: July 27, 2014, 02:46:11 pm by shonay »
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: hooking recv() with delphi, Questions.
« Reply #3 on: July 27, 2014, 03:14:26 pm »
1/ Cast and convert "buf" to string

2/ http://wiki.freepascal.org/shared_library#dllproc

3/ DLLEntryPoint is missing one "end;"
« Last Edit: July 27, 2014, 05:20:01 pm by ChrisF »

shonay

  • Full Member
  • ***
  • Posts: 169
Re: hooking recv() with delphi, Questions.
« Reply #4 on: July 27, 2014, 09:17:32 pm »
1/ Cast and convert "buf" to string

2/ http://wiki.freepascal.org/shared_library#dllproc

3/ DLLEntryPoint is missing one "end;"

Okay I see its working now. When I injected into a process (Firefox) it didn't return data, maybe because of I am online with ma laptop. Wondering what could be wrong. Thanks chrisF
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

 

TinyPortal © 2005-2018