From d0f91aed16a1cb40df6e1ab7c19b17410888906d Mon Sep 17 00:00:00 2001 From: BDKPlayer Date: Wed, 1 Apr 2020 20:57:35 +0200 Subject: Removed detours dependency --- VmtHook.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 VmtHook.cpp (limited to 'VmtHook.cpp') diff --git a/VmtHook.cpp b/VmtHook.cpp new file mode 100644 index 0000000..915e82f --- /dev/null +++ b/VmtHook.cpp @@ -0,0 +1,51 @@ +#include "VmtHook.h" + + + +VmtHook::VmtHook(void** vmt) +{ + this->vmt = vmt; +} + +void* VmtHook::Hook(int index, void* hookedFunction) +{ + hookedfuncs.insert(std::make_pair(index, vmt[index])); + + //make page writeable + DWORD pageProtection; + VirtualProtect(&vmt[index], sizeof(BYTE*), PAGE_EXECUTE_READWRITE, &pageProtection); + + //overwrite function pointer in VMT to hook function + vmt[index] = hookedFunction; + + //restore page protection + VirtualProtect(&vmt[index], sizeof(BYTE*), pageProtection, &pageProtection); + + return hookedfuncs[index]; +} + +// +//Unhook a single hooked function +bool VmtHook::Unhook(int index) +{ + auto entry = hookedfuncs.find(index); + if (entry != hookedfuncs.end()) + { + vmt[entry->first] = entry->second; + return true; + } + return false; +} + +// +//Unhook the entire Vmt +void VmtHook::Unhook() +{ + for (std::pair pair : hookedfuncs) + { + DWORD oldProtection; + VirtualProtect(&vmt[pair.first], sizeof(BYTE*), PAGE_EXECUTE_READWRITE, &oldProtection); + vmt[pair.first] = pair.second; + VirtualProtect(&vmt[pair.first], sizeof(BYTE*), oldProtection, &oldProtection); + } +} \ No newline at end of file -- cgit v1.2.3