1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #include "etx.h"
DWORD CGLHook::Hook(char *szFuncName, void *pFuncHook)
{
DWORD dwFuncAddr = 0;
DWORD dwFuncTblAddr = 0;
DWORD dwFuncNativeOrig = 0;
if(!bInit || !pFuncHook) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to hook %s, but not initted, or no hook function provided", szFuncName);
#endif
return 0;
}
if(!(dwFuncAddr = (DWORD)GetProcAddress(hOpengl32, szFuncName))) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to hook %s, but function not found", szFuncName);
#endif
return 0;
}
if(!(dwFuncTblAddr = GetTblOffset(dwFuncAddr))) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to hook %s, but failed to find a table offset/address", szFuncName);
#endif
return 0;
}
if(!(dwFuncNativeOrig = HookGLNative(dwFuncTblAddr, pFuncHook))) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to hook %s, but couldn't retrieve a pointer to the native function", szFuncName);
#endif
return 0;
}
return dwFuncNativeOrig;
}
bool CGLHook::UnHook(char *szFuncName, void *pFuncOrig)
{
DWORD dwFuncAddr = 0;
DWORD dwFuncTblAddr = 0;
DWORD dwFuncNativeOrig = 0;
if(!bInit || !pFuncOrig) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to remove %s hook, but not initted, or no original function provided", szFuncName);
#endif
return false;
}
if(!(dwFuncAddr = (DWORD)GetProcAddress(hOpengl32, szFuncName))) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to remove %s hook, but function not found", szFuncName);
#endif
return false;
}
if(!(dwFuncTblAddr = GetTblOffset(dwFuncAddr))) {
#ifdef ETC_DEBUG
log("etx Error: Attempted to remove %s hook, but failed to find a table offset/address", szFuncName);
#endif
return false;
}
UnHookGLNative(dwFuncTblAddr, pFuncOrig);
return true;
}
DWORD CGLHook::GetGLTls()
{
#define INTEGRITY_CHECK(base, offset, glbase, glsize) (*(DWORD*)(base+offset) < (DWORD)(glbase) || *(DWORD*)(base+offset) > (DWORD)(glbase+glsize))
MODULEINFO glmi = {0};
DWORD dataBlock = 0;
if(!(hOpengl32 = GetModuleHandle("opengl32"))) {
#ifdef ETC_DEBUG
log("etx Error: Unable to retrieve a handle to opengl32");
#endif
return 0;
}
if(!GetModuleInformation(GetCurrentProcess(), hOpengl32, &glmi, sizeof(glmi))) {
#ifdef ETC_DEBUG
log("etx Error: Unable to retrieve module information on opengl32");
#endif
return 0;
}
for(dataBlock=0x7FFD0000; dataBlock<=0x7FFDF000; dataBlock+=0x1000)
{
if(IsBadReadPtr((void*)dataBlock, 1)) // valid memory?
continue;
if(INTEGRITY_CHECK(dataBlock, 0xA50, hOpengl32, glmi.SizeOfImage)) // valid thread data block?
continue;
if(INTEGRITY_CHECK(dataBlock, 0x7C4, hOpengl32, glmi.SizeOfImage)) { // tbl of native or opengl32 ptrs?
bInit = true; return dataBlock; // native
}
}
#ifdef ETC_DEBUG
log("etx Error: Unable to find a valid thread data block");
#endif
return 0;
}
DWORD CGLHook::GetTblOffset(DWORD dwFuncAddr)
{
if(*(BYTE*)(dwFuncAddr + 0x07) == 0x83) {
iHookMethod = 0;
return ((*(DWORD*)(dwTls + *(DWORD*)(*(DWORD*)(dwFuncAddr + 0x14)))) + (*(DWORD*)(dwFuncAddr + 0x1C)));
}
else if(*(BYTE*)(dwFuncAddr + 0x07) == 0xA0) {
iHookMethod = 1;
return (*(DWORD*)(dwFuncAddr + 0x08));
}
return 0;
}
DWORD CGLHook::HookGLNative(DWORD dwFuncTblAddr, void *pFuncHook)
{
DWORD dwFuncNativeOrig = 0;
switch(iHookMethod)
{
case 0:
dwFuncNativeOrig = *(DWORD*)dwFuncTblAddr;
*(DWORD*)dwFuncTblAddr = (DWORD)pFuncHook;
break;
case 1:
dwFuncNativeOrig = *(DWORD*)(dwTls + dwFuncTblAddr);
*(DWORD*)(dwTls + dwFuncTblAddr) = (DWORD)pFuncHook;
break;
default:
break;
}
return dwFuncNativeOrig;
}
void CGLHook::UnHookGLNative(DWORD dwFuncTblAddr, void *pFuncOrig)
{
switch(iHookMethod)
{
case 0:
*(DWORD*)dwFuncTblAddr = (DWORD)pFuncOrig;
case 1:
*(DWORD*)(dwTls + dwFuncTblAddr) = (DWORD)pFuncOrig;
default:
break;
}
} |
Partager