Recent

Author Topic: WinApI Hooking MessageBox()Lazarus  (Read 12354 times)

shonay

  • Full Member
  • ***
  • Posts: 169
WinApI Hooking MessageBox()Lazarus
« on: July 25, 2014, 01:22:52 pm »
Good morning everyone,

I have been battling with a simple MessageBox() hook in lazarus Delphi, code compiles without problems, but it doesn't show if its hooked or not, so I decided to paste source here, pls I do need help of some sort.

My source code 

Code: [Select]
program MessageBoxHookNew;

{$mode delphi}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes , Windows
  { you can add units after this };
var
      data: String;
      procedure hook(tFunc,nFunc:Pointer);
      var
            jumpTo:DWord;
            oldProtect:Cardinal;
var
         MainMessageBox : Function(hWnd:HWND; lpText,lpCaption:PAnsiChar; uType:UINT):Integer; stdcall;
         HookMessageBox: Function (hWnd:HWND; lpText,lpCaption:PAnsiChar; uType:UINT):Integer; stdcall;
         begin
           jumpTo:=DWord(nFunc)-DWord(tFunc)-5;
           VirtualProtect(tFunc,5,PAGE_EXECUTE_READWRITE,@oldProtect);
           pbyte(tFunc)^:=$e9;
           pdword(DWord(tFunc)+1)^:=jumpTo;
         end;
   function MainMessageBox(hWnd:HWND; lpText,lpCaption:PAnsiChar; uType:UINT):Integer;assembler;stdcall;
   asm
   push ebp
   push esp
   push ebx
   push esi
   push edi
   end;
      function HookMessageBox(hWnd:HWND; lpText,lpCaption:PAnsiChar; uType:UINT):Integer; stdcall;
       begin
            //MessageBox(0,'test','Hooked',MB_OK);
            Result:= MainMessageBox(hWnd,lpText,lpCaption,uType);
       end;

begin
  data:=Pointer(DWord(GetProcAddress(GetModuleHandle('user32.dll'),MessageBoxW))+5);
  hook(GetProcAddress(GetModuleHandle('user32.dll'),'MessageBoxW'),@HookMessageBox);
end.

Pls I need Help, and some form of explanation
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: WinApI Hooking MessageBox()Lazarus
« Reply #1 on: July 25, 2014, 02:14:51 pm »
code compiles without problems
It does not compile.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #2 on: July 25, 2014, 02:20:52 pm »
Ok, I tried again, when I added the GetModuleHandle(GetProcessAddress('user32.dll'),'MessageBoxW')+5

For the Data, yes I saw just now it doesn't compile and when I tried removing it, it did compile, gives me an error for Pchar, for this area
Code: [Select]
Data:= GetModuleHandle(GetProcessAddress('user32.dll'),'MessageBoxW')+5

Please what could possibly be the Problem?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: WinApI Hooking MessageBox()Lazarus
« Reply #3 on: July 25, 2014, 02:26:03 pm »
And to test you call windows.messageboxw() ?

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #4 on: July 25, 2014, 02:28:56 pm »
Nah, I don't understand ur Question @Marcov, soRry
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: WinApI Hooking MessageBox()Lazarus
« Reply #5 on: July 25, 2014, 02:29:20 pm »
Please what could possibly be the Problem?
Maybe because data is:
Code: [Select]
  data: String;
?

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #6 on: July 25, 2014, 02:34:18 pm »
Um, Let's say, I don't have an Idea, what am I supposed to include, the pointer I used threw back an Initial error, so I got confused along the line.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: WinApI Hooking MessageBox()Lazarus
« Reply #7 on: July 25, 2014, 02:43:21 pm »
From what I can see, data is not used, so it is not needed. Simply delete both lines:
Code: [Select]
var
  Data: pointer;

and

Code: [Select]
data:=Pointer(DWord(GetProcAddress(GetModuleHandle('user32.dll'),MessageBoxW))+5);

You asked similar questions before about hooking. What is your goal/target?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: WinApI Hooking MessageBox()Lazarus
« Reply #8 on: July 25, 2014, 02:56:34 pm »
Nah, I don't understand ur Question @Marcov, soRry

Your code hooks Windows.MessageboxW.  So how do you test if the hook works?

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #9 on: July 25, 2014, 02:57:48 pm »
Understanding Win32 and its behaviours, patching and the rest, mainly educational purposes.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #10 on: July 25, 2014, 03:01:28 pm »
Nah, I don't understand ur Question @Marcov, soRry

Your code hooks Windows.MessageboxW.  So how do you test if the hook works?

It should pop out a messageBox, and it should show Hooked.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: WinApI Hooking MessageBox()Lazarus
« Reply #11 on: July 25, 2014, 03:21:23 pm »
Understanding Win32 and its behaviours, patching and the rest, mainly educational purposes.
Don't you need to understand basics of the language you are using to patch, Pascal in this case?
For instance, the variable data is of type string and you can not assign a number/handle to it.

Code: [Select]
program MessageBoxHookNew;

{$mode delphi}{$H+}

uses
  Windows;

procedure hook(tFunc, nFunc: Pointer);
var
  jumpTo: DWord;
  oldProtect: cardinal;

  MainMessageBox: function(hWnd: HWND; lpText, lpCaption: PAnsiChar;
    uType: UINT): integer; stdcall;
  HookMessageBox: function(hWnd: HWND; lpText, lpCaption: PAnsiChar;
    uType: UINT): integer; stdcall;
begin
  jumpTo := DWord(nFunc) - DWord(tFunc) - 5;
  VirtualProtect(tFunc, 5, PAGE_EXECUTE_READWRITE, @oldProtect);
  pbyte(tFunc)^ := $e9;
  pdword(DWord(tFunc) + 1)^ := jumpTo;
end;

function HookMessageBox(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): integer; stdcall;
begin
  Result := MessageBox(0,'test','Hooked',MB_OK);
end;

begin
  hook(GetProcAddress(GetModuleHandle('user32.dll'), 'MessageBoxW'), @HookMessageBox);

  MessageBoxW(0,'A','B',0);//<--- Test
end.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #12 on: July 25, 2014, 03:27:56 pm »
Understanding Win32 and its behaviours, patching and the rest, mainly educational purposes.
Don't you need to understand basics of the language you are using to patch, Pascal in this case?
For instance, the variable data is of type string and you can not assign a number/handle to it.

Code: [Select]
program MessageBoxHookNew;

{$mode delphi}{$H+}

uses
  Windows;

procedure hook(tFunc, nFunc: Pointer);
var
  jumpTo: DWord;
  oldProtect: cardinal;

  MainMessageBox: function(hWnd: HWND; lpText, lpCaption: PAnsiChar;
    uType: UINT): integer; stdcall;
  HookMessageBox: function(hWnd: HWND; lpText, lpCaption: PAnsiChar;
    uType: UINT): integer; stdcall;
begin
  jumpTo := DWord(nFunc) - DWord(tFunc) - 5;
  VirtualProtect(tFunc, 5, PAGE_EXECUTE_READWRITE, @oldProtect);
  pbyte(tFunc)^ := $e9;
  pdword(DWord(tFunc) + 1)^ := jumpTo;
end;

function HookMessageBox(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): integer; stdcall;
begin
  Result := MessageBox(0,'test','Hooked',MB_OK);
end;

begin
  hook(GetProcAddress(GetModuleHandle('user32.dll'), 'MessageBoxW'), @HookMessageBox);

  MessageBoxW(0,'A','B',0);//<--- Test
end.

I know some of the basics as I use Lazarus for Delphi.I know some ReadLn, Writeln, and how to connect to databse mysql. Just wanted to switch to win32 to study some behavior. About it. That's what. Think I have to study this well. Thanks again.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: WinApI Hooking MessageBox()Lazarus
« Reply #13 on: July 25, 2014, 03:31:16 pm »
Just wanted to switch to win32 to study some behavior. About it. That's what. Think I have to study this well. Thanks again.
To switch to Win32 you don't need to hook functions.

Do you know what this line does:
Code: [Select]
  pbyte(tFunc)^ := $e9;

shonay

  • Full Member
  • ***
  • Posts: 169
Re: WinApI Hooking MessageBox()Lazarus
« Reply #14 on: July 25, 2014, 03:37:05 pm »
Jmp, used mainly or trampoline if I am correct. Trampolines are used in creating hooks (redirecting a function to your own already created function)

Am I correct?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

 

TinyPortal © 2005-2018