Add IAT based hooking to allow hooking 64bit opengl32.dll

* Needs vigorous testing in as many situations as I can think of, not least
  32bit, 64bit, D3D11, D3D9, OpenGL, managed apps, different windows SDKs,
  delay loading dlls (not sure if these will work), statically linked libs,
  LoadLibrary + GetProcAddress
* Credit goes to jslutter for originally bringing it to my attention &
  suggesting it.
* References:
  http://msdn.microsoft.com/en-us/magazine/bb985992.aspx
  http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
  http://www.codeproject.com/Articles/6265/Process-wide-API-spying-an-ultimate-hack
This commit is contained in:
Baldur Karlsson committed 2014-08-06 15:43:16 +01:00
1 parent 23543d6a43
commit 12a30a7b97
10 files changed
+428 -11

No files matched your search

+6
View File
@@ -39,12 +39,14 @@ public:
{
bool success = true;
#if USE_MHOOK
// require dxgi.dll hooked as well for proper operation
if(GetModuleHandleA("dxgi.dll") == NULL)
{
RDCWARN("Failed to load dxgi.dll - not inserting D3D11 hooks.");
return false;
}
#endif
// also require d3dcompiler_??.dll
if(GetD3DCompiler() == NULL)
@@ -58,6 +60,7 @@ public:
if(!success) return false;
#if USE_MHOOK
// FRAPS compatibility. Save out the first 16 bytes (arbitrary number) of the 'real' function code.
// this should be
// jmp D3D11CreateDeviceAndSwapChain_hook
@@ -68,6 +71,7 @@ public:
void *hooked_func_ptr = GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
if(hooked_func_ptr == NULL) return false;
memcpy(CreateDeviceAndSwapChain_ident, hooked_func_ptr, 16);
#endif
m_HasHooks = true;
m_EnabledHooks = true;
@@ -188,6 +192,7 @@ private:
PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN createFunc = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
#if USE_MHOOK
if(createFunc)
{
byte ident[16];
@@ -211,6 +216,7 @@ private:
if(!memcmp(ident, CreateDeviceAndSwapChain_ident, 16) && m_HasHooks)
createFunc = CreateDeviceAndSwapChain();
}
#endif
// shouldn't ever get here, we should either have it from procaddress or the trampoline, but let's be
// safe.
+2
View File
@@ -40,8 +40,10 @@ public:
{
bool success = true;
#if USE_MHOOK
// require d3d11.dll hooked as well for proper operation
if(GetModuleHandleA("d3d11.dll") == NULL) return false;
#endif
success &= CreateDXGIFactory.Initialize("CreateDXGIFactory", DLL_NAME, CreateDXGIFactory_hook);
success &= CreateDXGIFactory1.Initialize("CreateDXGIFactory1", DLL_NAME, CreateDXGIFactory1_hook);
+2
View File
@@ -228,11 +228,13 @@ class OpenGLHook : LibraryHook
if(!m_EnabledHooks)
return false;
#if USE_MHOOK
if(GetModuleHandleA(DLL_NAME) == NULL)
{
RDCWARN("Failed to load %s - not inserting OpenGL hooks.", DLL_NAME);
return false;
}
#endif
bool success = SetupHooks(GL);
+39 -11
View File
@@ -35,36 +35,64 @@ using std::map;
#if defined(WIN32)
#include "3rdparty/mhook/mhook-lib/mhook.h"
#define USE_MHOOK 0
#define USE_IAT_HOOK 1
#if USE_MHOOK
#include "3rdparty/mhook/mhook-lib/mhook.h"
#elif USE_IAT_HOOK
#include "os/win32/win32_hook.h"
#else
#error "No hook method enabled"
#endif
template<typename FuncType>
class Hook
{
public:
Hook() { trampoline = NULL; }
~Hook() { Mhook_Unhook(&trampoline); }
Hook()
{
orig_funcptr = NULL;
}
~Hook()
{
#if USE_MHOOK
Mhook_Unhook(&orig_funcptr);
#endif
}
FuncType operator()()
{
return (FuncType)trampoline;
return (FuncType)orig_funcptr;
}
bool Initialize(const char *function, const char *module_name, void *destination_function_ptr)
{
trampoline = Process::GetFunctionAddress(module_name, function);
orig_funcptr = Process::GetFunctionAddress(module_name, function);
if(trampoline == NULL)
#if USE_MHOOK
if(orig_funcptr == NULL)
return false;
return Mhook_SetHook(&trampoline, destination_function_ptr);
return Mhook_SetHook(&orig_funcptr, destination_function_ptr);
#elif USE_IAT_HOOK
return Win32_IAT_Hook(&orig_funcptr, module_name, function, destination_function_ptr);
#else
#error "No hook method enabled"
#endif
}
private:
void* trampoline;
void *orig_funcptr;
};
#define HOOKS_BEGIN() Mhook_SuspendOtherThreads()
#define HOOKS_END() Mhook_ResumeOtherThreads()
#if USE_MHOOK
#define HOOKS_BEGIN() Mhook_SuspendOtherThreads()
#define HOOKS_END() Mhook_ResumeOtherThreads()
#else
#define HOOKS_BEGIN() Win32_IAT_BeginHooks()
#define HOOKS_END() Win32_IAT_EndHooks()
#endif
#elif defined(LINUX)