Recent

Author Topic: Please help create detour in Pascal  (Read 12035 times)

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #15 on: June 09, 2015, 04:22:29 pm »
Hence still not clear with it, you could help me look this out

From the code /source here


https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2114&lngWId=7
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #16 on: June 09, 2015, 04:26:28 pm »
Hmm don't get it twisted, how about seeing if it redirects and pops out a messagebox "hooked "
Ok. I've updated the example to actually show that the message has been modified to demo "hooking" abilities :)
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #17 on: June 09, 2015, 05:35:32 pm »
Hmm don't get it twisted, how about seeing if it redirects and pops out a messagebox "hooked "
Ok. I've updated the example to actually show that the message has been modified to demo "hooking" abilities :)

Ok sir lemme check and see how it goes

You have a screen shot as apparently am not near ma computer now sir, sorry for causing you problems sir
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #18 on: June 09, 2015, 06:02:27 pm »
a screen shot?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #19 on: June 09, 2015, 06:24:58 pm »
a screen shot?

Thanks sir for your time.
Normally the fake function is supposed to redirect back to the original function. Like this

Code: [Select]

program MsgBox;

uses
  Windows,
  afxCodeHook;

var
  MessageBoxANextHook: function(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;

function MessageBoxAHookProc(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
begin
  Result := MessageBoxANextHook(0, '', 'bye', 0);
end;

begin
  //test the target API
  MessageBox(0, '', 'hi', 0);

  //hook the API
  HookCode('user32', 'MessageBoxA', @MessageBoxAHookProc, @MessageBoxANextHook);

  //the message should be changed
  MessageBox(0, '', 'hi', 0);

  //unhoook the API
  UnhookCode(@MessageBoxANextHook);

  //test the target API
  MessageBox(0, '', 'hi', 0);
end

Sorry only that he used afxcodehook, tho he used a detour library. It wouldn't redirect to msg here sir, it redirects to the main function as you can see sir.
The detour function still needs to be looked at as apparently it doesn't redirect the hook properly.
« Last Edit: June 09, 2015, 06:29:55 pm by shonay »
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #20 on: June 09, 2015, 07:06:53 pm »
Thanks sir for your time.
Normally the fake function is supposed to redirect back to the original function.
that's exactly what's happening in hooking.zip example
Code: [Select]
type
  TMessageBoxProc = function (hWnd:HWND; lpText:LPCSTR; lpCaption:LPCSTR; uType:UINT):longint; stdcall;

var
  msg : TMessageBoxProc = nil; // this would keep the reference to call the original function

function HijackMessageBox(hWnd:HWND; lpText:LPCSTR; lpCaption:LPCSTR; uType:UINT):longint; stdcall;
var
  txt : String;
  capt : string;
begin
  writeln('hwnd:    ', hWnd);
  writeln('text:    ', lpText);
  writeln('caption: ', lpCaption);
  writeln('uType:   ', uType);
  if Assigned(msg) then begin
    txt:='hooked '+lpText;
    capt:='capt '+lpCaption;
    // calling the original with the adjusted parameters
    Result:=msg(hWnd, PAnsiChar(txt), PAnsiChar(capt), uType);
    // must release them, since FPC won't do that!
    txt:='';
    capt:='';
  end else
    Result:=0;

end;

begin
  try
    MessageBox(0, 'world', 'hello', 0);
    // DetourFunction - returns the pointer to call the previous version of the hooked function
    msg := TMessageBoxProc(DetourFunction( @MessageBox, @HijackMessageBox, 6));
    MessageBox(0, 'world', 'hello', 0);
  except
    on e: exception do
      writeln(e.message);
  end;
end.
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #21 on: June 09, 2015, 07:17:56 pm »
Thanks sir for your time.
Normally the fake function is supposed to redirect back to the original function.
that's exactly what's happening in hooking.zip example
Code: [Select]
type
  TMessageBoxProc = function (hWnd:HWND; lpText:LPCSTR; lpCaption:LPCSTR; uType:UINT):longint; stdcall;

var
  msg : TMessageBoxProc = nil; // this would keep the reference to call the original function

function HijackMessageBox(hWnd:HWND; lpText:LPCSTR; lpCaption:LPCSTR; uType:UINT):longint; stdcall;
var
  txt : String;
  capt : string;
begin
  writeln('hwnd:    ', hWnd);
  writeln('text:    ', lpText);
  writeln('caption: ', lpCaption);
  writeln('uType:   ', uType);
  if Assigned(msg) then begin
    txt:='hooked '+lpText;
    capt:='capt '+lpCaption;
    // calling the original with the adjusted parameters
    Result:=msg(hWnd, PAnsiChar(txt), PAnsiChar(capt), uType);
    // must release them, since FPC won't do that!
    txt:='';
    capt:='';
  end else
    Result:=0;

end;

begin
  try
    MessageBox(0, 'world', 'hello', 0);
    // DetourFunction - returns the pointer to call the previous version of the hooked function
    msg := TMessageBoxProc(DetourFunction( @MessageBox, @HijackMessageBox, 6));
    MessageBox(0, 'world', 'hello', 0);
  except
    on e: exception do
      writeln(e.message);
  end;
end.

Ok what about using to hook send() and recv(), how possible is it gonna be sir with this, I just need examples. 

Thank you sir for your time..
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #22 on: June 09, 2015, 07:27:21 pm »
Ok what about using to hook send() and recv(), how possible is it gonna be sir with this, I just need examples. 
Is there an issue to do them with afxCodeHook library?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #23 on: June 09, 2015, 07:30:58 pm »
Ok what about using to hook send() and recv(), how possible is it gonna be sir with this, I just need examples. 
Is there an issue to do them with afxCodeHook library?

No issues, only I haven't done it successfully with it. Hence I decided to ask on here, you have an example you can show me with afxcodehook on lazarus, I would be happy to learn from it.

Thank you sir.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #24 on: June 09, 2015, 08:22:16 pm »
does "haven't done it successfully" actually mean that these functions were not called?
because an application might not call them. Maybe they're using WinSockA functions?

anyway, here's the example!
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #25 on: June 09, 2015, 08:30:11 pm »
does "haven't done it successfully" actually mean that these functions were not called?
because an application might not call them. Maybe they're using WinSockA functions?

anyway, here's the example!

Hmmm, downloaded already u tested with Firefox or? Like which programs use WinsockA functions?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #26 on: June 09, 2015, 08:42:44 pm »
Hmmm, downloaded already u tested with Firefox or? Like which programs use WinsockA functions?
neither :) 
the test is in the program itself.
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

shonay

  • Full Member
  • ***
  • Posts: 169
Re: Please help create detour in Pascal
« Reply #27 on: June 09, 2015, 09:33:28 pm »
Hmmm, downloaded already u tested with Firefox or? Like which programs use WinsockA functions?
neither :) 
the test is in the program itself.

Hmmm ok let's try it this way

Code: [Select]
library Project1;

{$mode delphi}{$H+}

uses
  Classes , Windows , Winsock , SysUtils
  { you can add units after this };

var
  oldsend:function(const s:Tsocket; var :buf; len, flags:Integer):Integer;stdcall;
dwModule : DWORD;

function DetourFunction()
begin
   //type Detour Function here
end;

function: newsend(const s:Tsocket; var :buf; len, flags:Integer):Integer;stdcall;
begin
  MessageBox(0,buf,MB_OK);
  Result:=oldsend(s,buf,len,flags);
end;

procedure DLLEntryPoint(dwReason: DWORD);
begin
  case dwReason of
  DLL_PROCESS_ATTACH:
    begin
      if not Assigned(oldsend) then
      @oldsend := DetourFunction(@Send, @newsend);
      end;
  end;

begin
  DllProc:=@DLLEntryPoint;
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.

By just adding the detoured function only, kindly help me see if the code compiles without further problems.

Thank you.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Please help create detour in Pascal
« Reply #28 on: June 10, 2015, 03:12:22 pm »
sample.zip
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: Please help create detour in Pascal
« Reply #29 on: June 10, 2015, 06:25:09 pm »
Hmm is this possible in linux? I would like to patch some of the methods in the FPC RTL for linux..

 

TinyPortal © 2005-2018