|
You last visited: Today at 05:36
Advertisement
[HELP]Hooking MessageBoxA
Discussion on [HELP]Hooking MessageBoxA within the C/C++ forum part of the Coders Den category.
01/29/2014, 14:40
|
#1
|
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
|
[HELP]Hooking MessageBoxA
---------------------
|
|
|
01/29/2014, 16:34
|
#2
|
elite*gold: LOCKED
Join Date: Dec 2009
Posts: 434
Received Thanks: 396
|
Code:
int MyMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{
UnHookFunction("user32.dll", "MessageBoxA", hook);
int x = MessageBox(hWnd, lpText, lpCaption, uType);
//MessageBox(hWnd, "HAHAHAHA", lpCaption, uType);
HookFunction("user32.dll", "MessageBoxA", MyMessageBoxA, hook);
return x;
}
??? Can't work.
Unhook = you "delete" the hook.
Try that: (didn't looked at your detours )
Code:
int MyMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{
lpText = "Hooked"
return POINTER_MessageBoxA(hWnd,....);//create a pointer.. :D
}
Written in this editor.
|
|
|
01/30/2014, 00:15
|
#3
|
elite*gold: 420
Join Date: Jan 2012
Posts: 1,082
Received Thanks: 1,000
|
Here you can find a full example of how to hook MessageBox (via hotpatching): Warrock - Code Snippets
|
|
|
01/30/2014, 02:59
|
#4
|
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
|
----------------------
|
|
|
01/30/2014, 10:51
|
#5
|
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
|
Please first learn the language instead of copy&pasting fom progamercity.net
First, if you want to use MessageBoxA to display that you injected successfully, then do this before hooking it.
And by the way, it's better to create a thread for hooking and unhooking to see the effect better.
Here is the corrected code:
Code:
...
...
DWORD WINAPI HackThread(LPVOID unused)
{
while(true)
{
if(GetAsyncKeyState(VK_F3))
{
MessageBoxA(NULL, "Successfully hooked MessageBoxA", "Hooking", MB_OK);
HookFunction("user32.dll", "MessageBoxA", MyMessageBoxA, hook);
}
if(GetAsyncKeyState(VK_F4))
{
UnHookFunction("user32.dll", "MessageBoxA", hook);
MessageBoxA(NULL, "Successfully unhooked MessageBoxA", "Hooking", MB_OK);
}
}
return 0;
}
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
MessageBoxA(0, "DLL_PROCESS_ATTACH\nPress F3 to hook MessageBoxA\nPress F4 to unhook MessageBoxA", "Hooking", MB_OK);
Sleep(10);
CreateThread(0, 0, &HackThread, 0, 0, 0);
break;
case DLL_PROCESS_DETACH:
UnHookFunction("user32.dll", "MessageBoxA", hook);
Sleep(10);
MessageBoxA(0, "DLL_PROCESS_DETACH", "Hooking", MB_OK);
break;
}
return TRUE;
}
int MyMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{
UnHookFunction("user32.dll", "MessageBoxA", hook);
int x = MessageBox(hWnd, "Hooked MessageBoxA", lpCaption, uType);
HookFunction("user32.dll", "MessageBoxA", MyMessageBoxA, hook);
return x;
}
...
...
|
|
|
01/30/2014, 13:13
|
#6
|
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
|
--------------------
|
|
|
01/30/2014, 13:21
|
#7
|
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
|
Quote:
Originally Posted by nerdsupreme
thank you, and btw: i didnt copypasted it..
but i still don't see what i did wrong, even your code works now.
is a thread needed? or what changes you made? lol
|
Huh.. then i will quote me because u didn't read it correctly:
Quote:
Originally Posted by XxharCs
First, if you want to use MessageBoxA to display that you injected successfully, then do this before hooking it.
|
You hook MessageBoxA and then use it (from your dll), of course it's gonna crash.
|
|
|
01/30/2014, 13:30
|
#8
|
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
|
---------------------
|
|
|
01/30/2014, 14:30
|
#9
|
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
|
Quote:
Originally Posted by nerdsupreme
maybe there are other soultions for detouring, without unhooking it everytime it gets into my function 
|
You don't even need to unhook the original function and hook yours. Just hook it, but don't forget to unhook the function after closing the application
|
|
|
01/30/2014, 17:15
|
#10
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
|
Use Microsoft detours - > problem solved
@XxharCs
You mean before ejecting the dll. When the application closes, it doesn't matter anyway, because well.. the application closes.
Cleanup is only needed if you want to keep the process working after ejecting the dll.
|
|
|
01/31/2014, 06:26
|
#11
|
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
|
------------------------
|
|
|
01/31/2014, 12:11
|
#12
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
|
Quote:
Originally Posted by nerdsupreme
it is not needed?
but then i am unable to call messagebox (the original function) in "mymessagebox" cause my call would land in my fake function again and again (endless loop).
look, you replace the the first bytes of messagebox with YOUR JMP to YOUR NEW function.
if you don't unhook it in YOUR function and CALL it again, logically it lands back to the REPLACED BYTES (the JMP TO YOUR FUNCTION) and then it jmps again to your function. it trys again to call MessageBoxA, but it gets again to your func.
or what you mean?
i could only copy the orig. func. to new allocated memory, so i can call it from there..
and M$-detours is $hit, cause it does not support x64(free version) :P
|
Ms Detours isnt ****, your hooking library obviously is, if it doesn't support a trampoline, because that's what solves your problem.
One does not simply unhook a hook that is currently executing (it's possible, but it has no point in most cases and it isn't thread-safe).
|
|
|
01/31/2014, 13:07
|
#13
|
elite*gold: 159
Join Date: Jun 2013
Posts: 1,776
Received Thanks: 2,004
|
Quote:
Originally Posted by MrSm!th
Ms Detours isnt ****, your hooking library obviously is, if it doesn't support a trampoline, because that's what solves your problem.
One does not simply unhook a hook that is currently executing (it's possible, but it has no point in most cases and it isn't thread-safe).
|
 <3
|
|
|
01/31/2014, 13:18
|
#14
|
elite*gold: 0
Join Date: Jan 2014
Posts: 103
Received Thanks: 55
|
------------------------
|
|
|
01/31/2014, 16:34
|
#15
|
elite*gold: 46
Join Date: Oct 2010
Posts: 782
Received Thanks: 525
|
Quote:
Originally Posted by nerdsupreme
it is not needed?
but then i am unable to call messagebox (the original function) in "mymessagebox" cause my call would land in my fake function again and again (endless loop).
|
If you would have a good Detour Function/Library you would have an pointer to the original function and you could just call it. Don't really understand want your detour code is returning there. ( Well i understand it, but i don't know why it is returning the start adress of the function.)
Code:
UINT WINAPI hookMessageBox(HWND hWnd, LPCSTR lpszText, LPCSTR lpszTitle, UINT uStyle)
{
return pMessageBoxA(hWnd, "bla", "MessageBoxFuncHooked", uStyle);
}
I got no idea why you exaclty want to Unhook the function, once the hooked function is called and then want to hook it again. Hook it one Time on load and Unhook it on unload but only if the process is not closing and the dll is being unloaded. MrSmith said the rest.
|
|
|
 |
|
Similar Threads
|
[Tutorial] Hooking API's using C#
08/17/2020 - Coding Tutorials - 6 Replies
Hello epvp, today i'm going to teach you how to hook MessageBox using C# ;)
Requirements & information ]
.NET framework 4
Visual Studio 20XX
EasyHook
MessageBox function(Windows)
Hooking - Wikipedia, the free encyclopedia
|
Help with API Hooking
01/30/2014 - C/C++ - 3 Replies
Hi epvp i tried hooking the api CreateProcessA to change the parameters of the process to be loaded. But something is wrong cause the process Crashes :(
Here is the code:
#include <windows.h>
void* detourFunc(BYTE *src, const BYTE *dst, const int len)
{
BYTE *jmp = (BYTE*)malloc(len+5);
DWORD dwback;
|
Hooking with D
02/20/2013 - CO2 Programming - 6 Replies
Comes with a homemade DLL injector although there's probably others that will do the job just as good.
If anyone's interested here's the tools I use
D compiler: Downloads - D Programming Language
IDE: Download - MonoDevelop
Mono-D (D support for MonoDevelop): Mono-D
|
C++ D3D Hooking
08/24/2009 - C/C++ - 12 Replies
Hallo zusammen,
ich stehe gerade vor folgendem Problem:
ich habe eine DLL und einen Loader gecoded, jedoch will ich anstelle des Loader einen Injecter haben, sprich: das spiel, in das injected werden soll, soll schon laufen. Natürlich hab ich das ganze schon probiert, jedoch werden die D3D-funktionen nicht wirklich gehookt, da die DLL auf ein Direct3DCreate9 wartet. Da diese Funktion aber wahrscheinlich direkt beim Starten des "Opfer-Spiels" ausgeführt wird, werden deswegen die anderen...
|
All times are GMT +1. The time now is 05:37.
|
|