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 


[C++] Writing Inline API Hooks

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

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Sun Jun 01, 2008 8:29 pm    Post subject: [C++] Writing Inline API Hooks Reply with quote

How do you write inline API hooks with C++? Those are the ones that put a jmp at the start of the API. So far, I can only write IAT hooks with C++. When I try to do it the inline way, after hooking the API, the program crashes. An example inline API hook in C++ would be helpful.
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

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

PostPosted: Sun Jun 01, 2008 8:48 pm    Post subject: Reply with quote

If you hook the function, you'll need to do something so it doesn't crash. depending on what you want to do, you could have it jump to your code-cave, then you can do some stuff, then you'd need to do the first 5 bytes of the api that you wrote over with your jump yourself. If you want to disable that api, I'm not sure what you'd need to do to keep it from crashing. jsut a return will probably not be sufficient and it will probably be different for most APIs.
_________________
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: Sun Jun 01, 2008 9:04 pm    Post subject: Reply with quote

Ferocious's Trampoline documentation was great. In fact, I suggest using it for all your inline API hooking needs. Btw, I would greatly love an example code for IAT hooks, do you mind putting a link to where you were taught it? Anyway here's the code.

Code:

BOOL TrampolineAPI(HMODULE hModule, LPCWSTR DllName, LPCSTR ProcName, DWORD dwReplaced)
{
   DWORD dwReturn;
   DWORD dwOldProtect;
   DWORD dwAddressToHook =  (DWORD)GetProcAddress(GetModuleHandle(DllName), ProcName);
   BYTE *pbTargetCode = (BYTE *)dwAddressToHook;
   BYTE *pbReplaced = (BYTE *)dwReplaced;
   VirtualProtect((LPVOID)dwAddressToHook, 5,      PAGE_EXECUTE_READWRITE, &dwOldProtect);
   *pbTargetCode++ = 0xE9;
   *((signed int*)(pbTargetCode)) = pbReplaced - (pbTargetCode + 4);
   VirtualProtect((LPVOID)dwAddressToHook, 5, PAGE_EXECUTE,   &dwOldProtect);
   dwReturn = dwAddressToHook + 5;
   FlushInstructionCache(GetCurrentProcess(), NULL, NULL);
   return TRUE;
}
void WINAPI MessageBoxReplaced(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
{
   //Your Code Here
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
   if (fdwReason == DLL_PROCESS_ATTACH)
   {
      TrampolineAPI(hinstDLL, "USER32.DLL", "MessageBoxA",   (DWORD)MessageBoxReplaced);
   }
return TRUE;
}

_________________


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
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Sun Jun 01, 2008 9:43 pm    Post subject: Reply with quote

There is ton of examples for IAT hooking on GameDeception. Their site is currently down as their hosts building power room 'exploded' yesterday. So their entire network is down at the moment. Should be back up tomorrow though according to them.

Article on the server thing:
http://tech.slashdot.org/tech/08/06/01/1715247.shtml

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Mon Jun 02, 2008 8:19 pm    Post subject: Reply with quote

I found the problem that makes the program crash. In C++, when you use two __declspec calling convensions, the second one is ignored. I used __declspec(dllexport) and __declspec(naked). As a result, the naked is omitted and C++ adds prologue code to the hook function. That corrupts the stack and then the program crashes. Took out the __declspec(dllexport) and the hook works fine.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Mon Jun 02, 2008 8:21 pm    Post subject: Reply with quote

Just a side note, you shouldn't have been trying to export the function since you wanted to use it inline anyway. Only reason to export it is to allow outside access to the function (with ease).
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
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