ssleay32.dll

c++ / delphi package - dll injection and api hooking

ssleay32.dll

Postby newbie » Mon Aug 23, 2004 9:02 am

I just find this great site, I'm really new in this dll injecting area.
I would like to control the SSL_read() and SSL_write() methods of this dll from delphi.
But each of these methods have an SSL typed parameter. I found this dll source, but I can't translate this c++ type to delphi.

int SSL_read(SSL *s,void *buf,int num)
int SSL_write(SSL *s,const void *buf,int num)

Anybody could help me how can I start?

In the first step I don't need this first parameter, but later I would like to call the original methods in the original dll with these parameters.
newbie
 
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Postby madshi » Mon Aug 23, 2004 2:22 pm

Just use "pointer".
madshi
Site Admin
 
Posts: 9810
Joined: Sun Mar 21, 2004 5:25 pm

Postby newbie » Mon Aug 23, 2004 3:12 pm

madshi wrote:Just use "pointer".


Thanks. I try, but something wrong.

This is my code, maybe you can help me:
Code: Select all
unit ssl_hack;

interface

uses Windows, madCodeHook;

const

  ssleay32_dll : PChar = 'C:\Program Files\...\ssleay32.dll';

type

  TSSL_read  = function  (ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
  //TSSL_write = function  (ssl: Pointer; const buf: PChar; num: Integer):Integer; cdecl;
  TSSL_write = function  (ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;


var

  SSL_readNextHook  : TSSL_read;
  SSL_writeNextHook : TSSL_write;

  ssl_readlog : TSSL_read;
  ssl_writelog : TSSL_write;

function  SSL_readHookProc(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
function  SSL_writeHookProc(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;

procedure InstallHook;
procedure UninstallHook;
procedure SetLogFunctions(r:TSSL_read;w:TSSL_write);


implementation

procedure read_log(ssl: Pointer; buf: PChar; num: Integer);
begin
  if assigned(ssl_writelog) then
    ssl_writelog(ssl,buf,num);
end;

procedure write_log(ssl: Pointer; buf: PChar; num: Integer);
begin
  if assigned(ssl_readlog) then
    ssl_readlog(ssl,buf,num);
end;

procedure SetLogFunctions(r:TSSL_read;w:TSSL_write);
begin
  ssl_readlog := r;
  ssl_writelog := w;
end;


function  SSL_readHookProc(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
begin
  // Log
  read_log(ssl,buf,num);
  // Call original
  result := SSL_readNextHook(ssl,buf,num);
end;

function  SSL_writeHookProc(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
begin
  // Log
  write_log(ssl,buf,num);
  // Call original
  result := SSL_writeNextHook(ssl,buf,num);
end;

procedure InstallHook;
begin
  HookAPI(ssleay32_dll, 'SSL_read', @SSL_readHookProc, @SSL_readNextHook);
  HookAPI(ssleay32_dll, 'SSL_write', @SSL_writeHookProc, @SSL_writeNextHook);
end;

procedure UninstallHook;
begin
  UnhookAPI(@SSL_readNextHook);
  UnhookAPI(@SSL_writeNextHook);
end;

initialization

  ssl_readlog := nil;
  ssl_writelog := nil;

end.




Code: Select all
unit main;

interface

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

type
  TfrmMain = class(TForm)
    Hook: TButton;
    memoLog: TMemo;
    Unhook: TButton;
    procedure HookClick(Sender: TObject);
    procedure UnhookClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

function OnFormRead(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
function OnFormWrite(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;


implementation

{$R *.dfm}

function  OnFormRead(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
begin
  frmMain.memoLog.Lines.Add('Read:');
  frmMain.memoLog.Lines.Add(buf);
  result := 0;
end;

function  OnFormWrite(ssl: Pointer; buf: PChar; num: Integer):Integer; cdecl;
begin
  frmMain.memoLog.Lines.Add('Write:');
  frmMain.memoLog.Lines.Add(buf);
  result := 0;
end;


procedure TfrmMain.HookClick(Sender: TObject);
begin
  //
  SetLogFunctions(OnFormRead,OnFormWrite);
  InstallHook;
end;

procedure TfrmMain.UnhookClick(Sender: TObject);
begin
  UninstallHook;
end;

end.


The target app working, but nothing happened :(
newbie
 
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Postby madshi » Mon Aug 23, 2004 3:49 pm

Replace this:

const
ssleay32_dll : PChar = 'C:\Program Files\...\ssleay32.dll';

With this:

const
ssleay32_dll : pchar = 'ssleay32.dll';

If that still doesn't work check out whether you named the APIs correctly when calling HookAPI. The API names are case sensitive.
madshi
Site Admin
 
Posts: 9810
Joined: Sun Mar 21, 2004 5:25 pm

Postby newbie » Mon Aug 23, 2004 4:05 pm

madshi wrote:Replace this:

const
ssleay32_dll : PChar = 'C:\Program Files\...\ssleay32.dll';

With this:

const
ssleay32_dll : pchar = 'ssleay32.dll';

If that still doesn't work check out whether you named the APIs correctly when calling HookAPI. The API names are case sensitive.


Thanks.
But still doesn't work.

What do you mean "API names"?
(Or what document I need to read to understand this?)
newbie
 
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Postby madshi » Tue Aug 24, 2004 6:11 am

API name is "SSL_read". Is it really "SSL_read"? Or maybe it is "SSL_Read" or "ssl_read"? Case matters here...
madshi
Site Admin
 
Posts: 9810
Joined: Sun Mar 21, 2004 5:25 pm

Postby newbie » Tue Aug 24, 2004 7:36 am

madshi wrote:API name is "SSL_read". Is it really "SSL_read"? Or maybe it is "SSL_Read" or "ssl_read"? Case matters here...


Ok, these are thefuntion names.
I check they, and these are the correct format.
newbie
 
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Postby madshi » Tue Aug 24, 2004 7:58 am

What does HookAPI return? True or false?
madshi
Site Admin
 
Posts: 9810
Joined: Sun Mar 21, 2004 5:25 pm

Postby newbie » Tue Aug 24, 2004 8:22 am

madshi wrote:What does HookAPI return? True or false?


Both call return with true. I already verify it.
But I found the problem:

After the call:
Code: Select all
HookAPI(ssleay32_dll, 'SSL_read', @SSL_readHookProc, @SSL_readNextHook);


the @SSL_readNextHook is nil! (I forget to verify it before).
newbie
 
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Postby madshi » Tue Aug 24, 2004 8:30 am

In which process do you want to hook those APIs? In your own process or in another process? Your code only hooks those APIs in the current (= your own) process. Is that what you want? If not, please check out the madCodeHook demos to see who to hook APIs system wide.
madshi
Site Admin
 
Posts: 9810
Joined: Sun Mar 21, 2004 5:25 pm

Postby newbie » Tue Aug 24, 2004 8:41 am

madshi wrote:In which process do you want to hook those APIs? In your own process or in another process? Your code only hooks those APIs in the current (= your own) process. Is that what you want? If not, please check out the madCodeHook demos to see who to hook APIs system wide.


A was afraid from this, because i would like to hook in other process.
So, in this case I need to create an inject dll inject it and I need IPC functions (what is not really undertandable for me yet) to communicate with my main program.

Thanks again for the help and congratulation for this great collection.
newbie
 
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Postby madshi » Tue Aug 24, 2004 8:45 am

newbie wrote:So, in this case I need to create an inject dll inject it and I need IPC functions (what is not really undertandable for me yet) to communicate with my main program.

Correct.
madshi
Site Admin
 
Posts: 9810
Joined: Sun Mar 21, 2004 5:25 pm


Return to madCodeHook

Who is online

Users browsing this forum: Alexa [Bot] and 6 guests