| View previous topic :: View next topic |
| Author |
Message |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Sun Jun 01, 2008 8:29 pm Post subject: [C++] Writing Inline API Hooks |
|
|
| 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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Sun Jun 01, 2008 8:48 pm Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jun 01, 2008 9:04 pm Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Sun Jun 01, 2008 9:43 pm Post subject: |
|
|
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 |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Mon Jun 02, 2008 8:19 pm Post subject: |
|
|
| 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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Mon Jun 02, 2008 8:21 pm Post subject: |
|
|
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 |
|
 |
|