Created
March 27, 2019 17:22
-
-
Save luodaoyi/33cec71c795e394e03bf0fad9355c396 to your computer and use it in GitHub Desktop.
openGL Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```c | |
namespace OpenGLMemory | |
{ | |
template<typename T> | |
T Read(DWORD address) | |
{ | |
return *((T*)address); | |
} | |
template<typename T> | |
void Write(DWORD address, T value) | |
{ | |
*((T*)address) = value; | |
} | |
template<typename T> | |
DWORD Protect(DWORD address, DWORD prot) | |
{ | |
DWORD oldProt; | |
VirtualProtect((LPVOID)address, sizeof(T), prot, &oldProt); | |
return oldProt; | |
} | |
DWORD OpenGLJumpHook(DWORD hookAt, DWORD newFunc, int size) | |
{ | |
DWORD newOffset = newFunc - hookAt - 5; // -5 用于写入jmp 到新函数的代码 | |
auto oldProtection = OpenGLMemory::Protect<DWORD[3]>(hookAt + 1, PAGE_EXECUTE_READWRITE); //改内存属性为可写 | |
OpenGLMemory::Write<BYTE>(hookAt, 0xE9); //JMP | |
OpenGLMemory::Write<DWORD>(hookAt + 1, newOffset); //跳转到新函数地址 | |
for (unsigned int i = 5; i < size; i++) //NOP额外字节,因此它不会破坏任何指令 | |
OpenGLMemory::Write<BYTE>(hookAt + i, 0x90); | |
OpenGLMemory::Protect<DWORD[3]>(hookAt + 1, oldProtection); | |
return hookAt + 5; | |
} | |
} | |
void hwglSwapBuffers(_In_ HDC hDc) | |
{ | |
CString hDcLogger; | |
hDcLogger.Format("OpenGL HOOK: hdc: %08X",hDc); | |
OutputDebugString(hDcLogger); | |
} | |
DWORD owglSwapBuffers = 0; | |
void __declspec(naked) SwapTrampoline() | |
{ | |
__asm { | |
PUSHFD //保存EFLAGS | |
PUSHAD //保存寄存器 | |
CALL hwglSwapBuffers //重定向到自己的buffer | |
POPAD //恢复寄存器 | |
POPFD //恢复EFLAGS | |
PUSH EBP //恢复hook前的汇编代码 | |
MOV EBP, ESP | |
JMP[owglSwapBuffers] //返回原本的执行位置继续执行 | |
} | |
} | |
void OpenGLHook() | |
{ | |
//DWORD AddressToHook = (DWORD)GetProcAddress(GetModuleHandle(L"opengl32.dll"),"wglSwapBuffers"); | |
DWORD wglSwapBuffersAddress = (DWORD)GetProcAddress(GetModuleHandle(_T("opengl32.dll")),"wglSwapBuffers"); | |
owglSwapBuffers = OpenGLMemory::OpenGLJumpHook(wglSwapBuffersAddress, (DWORD)SwapTrampoline, 5); | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment