Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Help] C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
slippppppppp
Grandmaster Cheater
Reputation: 0

Joined: 08 Aug 2006
Posts: 929

PostPosted: Sat Jul 12, 2008 10:22 pm    Post subject: [Help] C++ Reply with quote

Here is a basic hook function that i coded with some help from noz.

Code:
#define JMP(frm, to) (int)(((int)to - (int)frm) - 5);

bool HookProc(VOID* NewProc, char * OldProc, char * pLIB)
{
   ULONG OldFunction = (DWORD)GetProcAddress(LoadLibrary(pLIB), OldProc);

   *(DWORD*)OldFunction = 0xE9;

   *(DWORD*)(OldFunction + 1) = JMP(OldFunction, NewProc);

return true;
}


If you don't already know, what this does it have a certain api, when called, jump to your own procedure.
For example, let's say you called PostMessageA, it would jump to PostMessage, but then see another jump, and jump to my own procedure.
Hooking.

Now my question is, when i use this, it works but, it closes the process, notepad.exe for example. But when i inject into maplestory, it just freezes the injector. Anyone know what's wrong?

Btw, here's an example of using it:
Code:

void MyFunction()
{
}

HookProc(&MyFunction, "PostMessageA", "User32.dll");
Back to top
View user's profile Send private message AIM Address MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Sat Jul 12, 2008 10:30 pm    Post subject: Reply with quote

Are you putting everything into the state the program expects? ie returning any/the right kind of variable
_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Jul 12, 2008 10:36 pm    Post subject: Reply with quote

If your going to hook a function you have to declare your targeted function the same way. I.e.

Code:

BOOL PostMessageHook(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
   return FALSE;
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
slippppppppp
Grandmaster Cheater
Reputation: 0

Joined: 08 Aug 2006
Posts: 929

PostPosted: Sat Jul 12, 2008 10:37 pm    Post subject: Reply with quote

ohh, so i have to also make a function for the api im trying to hook?

Last edited by slippppppppp on Sat Jul 12, 2008 10:39 pm; edited 1 time in total
Back to top
View user's profile Send private message AIM Address MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Jul 12, 2008 10:39 pm    Post subject: Reply with quote

Otherwise it gets messed up. Declare it like this:

Code:

BOOL PostMessageHook(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
   return FALSE; //if you want it to fail
}


Oh, and declare NewProc as LPVOID instead of void* and declare OldProc and pLib as LPCSTR.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
slippppppppp
Grandmaster Cheater
Reputation: 0

Joined: 08 Aug 2006
Posts: 929

PostPosted: Sat Jul 12, 2008 10:41 pm    Post subject: Reply with quote

Actually, this is how im using it:

Quote:
/* Wrappers for user32.dll */

#include "windows.h"

_declspec(naked) BOOL WINAPI __stdcall Blocked_PostMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ShowMessage("Hai Guys");
HWND MsHandle = FindWindow(NULL, "MapleStory");
if(MsHandle == hWnd)
hWnd = 0;
return 0;
}

void InstallHooks()
{
HookProc(&Blocked_PostMessage, "PostMessageA", "User32.dll"))
}
Back to top
View user's profile Send private message AIM Address MSN Messenger
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Mon Jul 14, 2008 3:52 am    Post subject: Reply with quote

You should just use Microsoft's Detour Library.

I think your JMP is wrong. Doesn't it just jump straight to the address of the start of the new function?

Change *(DWORD*)OldFunction = 0xE9; to *(BYTE*)OldFunction = 0xE9;

and

try *(DWORD*)(OldFunction + 1) = (DWORD)NewProc;
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Mon Jul 14, 2008 4:11 am    Post subject: Reply with quote

Zand wrote:
You should just use Microsoft's Detour Library.

I think your JMP is wrong. Doesn't it just jump straight to the address of the start of the new function?

Change *(DWORD*)OldFunction = 0xE9; to *(BYTE*)OldFunction = 0xE9;

and

try *(DWORD*)(OldFunction + 1) = (DWORD)NewProc;


You need to calculate the offset for the jmp and i dont think the DWORD is a problem because the bytes after get set anyway.
Back to top
View user's profile Send private message MSN Messenger
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Tue Jul 15, 2008 10:40 am    Post subject: Reply with quote

Here

Code:
void *DetourFunc(BYTE *src, const BYTE *dst, const int len)
{
   BYTE *jmp = (BYTE*)malloc(len+5);
   DWORD dwback;

   VirtualProtect(src, len, PAGE_READWRITE, &dwback);

   memcpy(jmp, src, len);   jmp += len;
   
   jmp[0] = 0xE9;
   *(DWORD*)(jmp+1) = (DWORD)(src+len - jmp) - 5;

   src[0] = 0xE9;
   *(DWORD*)(src+1) = (DWORD)(dst - src) - 5;

   VirtualProtect(src, len, dwback, &dwback);

   return (jmp-len);
}


Usage
Code:
DetourFunc((BYTE*)GetProcAddress(LoadLibrary("user32.dll"), "PostMessageA"), (BYTE*)myfunc,2);


Last edited by Zand on Tue Jul 15, 2008 10:45 am; edited 3 times in total
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Tue Jul 15, 2008 10:42 am    Post subject: Reply with quote

slippppppppp wrote:
Actually, this is how im using it:

Quote:
/* Wrappers for user32.dll */

#include "windows.h"

_declspec(naked) BOOL WINAPI __stdcall Blocked_PostMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ShowMessage("Hai Guys");
HWND MsHandle = FindWindow(NULL, "MapleStory");
if(MsHandle == hWnd)
hWnd = 0;
return 0;
}

void InstallHooks()
{
HookProc(&Blocked_PostMessage, "PostMessageA", "User32.dll"))
}


Lol... you know WINAPI is a definition for __stdcall

_________________
Back to top
View user's profile Send private message
WafflesFTW
Expert Cheater
Reputation: 0

Joined: 21 Mar 2008
Posts: 131

PostPosted: Wed Jul 16, 2008 9:49 am    Post subject: Reply with quote

Why are you using stdcall twice?
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites