ssleay32.dll

c++ / delphi package - dll injection and api hooking
Post Reply
newbie
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

ssleay32.dll

Post by newbie »

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.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Just use "pointer".
newbie
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Post by newbie »

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 :(
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

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.
newbie
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Post by newbie »

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?)
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

API name is "SSL_read". Is it really "SSL_read"? Or maybe it is "SSL_Read" or "ssl_read"? Case matters here...
newbie
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Post by newbie »

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.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

What does HookAPI return? True or false?
newbie
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Post by newbie »

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).
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

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.
newbie
Posts: 6
Joined: Mon Aug 23, 2004 8:47 am

Post by newbie »

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.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

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.
Post Reply