use exact address for hooking

Before I tried to hook the entry in my iat, which obviously leads to various
dumb failures...
This commit is contained in:
2018-01-08 18:42:35 +01:00
parent bce85be82e
commit f1ec585409
6 changed files with 72 additions and 27 deletions

View File

@@ -3,6 +3,14 @@ class AbstractHookEngine {
private: private:
const char* name_; const char* name_;
public:
static void* getSmall() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_small")); }
static void* getBranch() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_branch")); }
static void* getRipRelative() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_rip_relative")); }
static void* getAVX() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_AVX")); }
static void* getRDRAND() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_RDRAND")); }
static void* getLoop() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_loop")); }
static void* getTailRecursion() { return (void*)(GetProcAddress(LoadLibrary(L"test_cases.dll"), "_tail_recursion")); }
public: public:
/* boolean for each hook test case, which are set by the hooks */ /* boolean for each hook test case, which are set by the hooks */
struct { struct {

View File

@@ -1,17 +1,54 @@
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <Windows.h>
#include <memory>
#include "../test_cases/test_cases.h" #include "../test_cases/test_cases.h"
#include "abstracthook.h" #include "abstracthook.h"
#include "mhook.h"
#pragma comment(lib, "..\\x64\\debug\\test_cases.lib")
//#pragma comment(lib, "..\\x64\\debug\\test_cases.lib")
extern AbstractHookEngine* g_mhook, extern AbstractHookEngine* g_mhook,
*g_PolyHook, *g_PolyHook,
*g_MinHook; *g_MinHook;
#if 0
typedef BOOL(__stdcall* tBitBlt)(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,
HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop);
tBitBlt oBitBlt;
//Just an int that gets incremented to verify handler got called for unit tests
int BitBltHookVerifier = 0;
BOOL __stdcall hkBitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,
HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop)
{
BitBltHookVerifier += 1337;
return oBitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
}
void test()
{
std::shared_ptr<PLH::Detour> Detour_Ex(new PLH::Detour);
//REQUIRE(Detour_Ex->GetType() == PLH::HookType::Detour);
assert(BitBltHookVerifier == 0);
Detour_Ex->SetupHook((uint8_t*)GetProcAddress(LoadLibrary(L"Gdi32.dll"), "BitBlt"), (BYTE*)&hkBitBlt);
assert(Detour_Ex->Hook());
oBitBlt = Detour_Ex->GetOriginal<tBitBlt>();
BitBlt(NULL, 0, 0, 0, 0, NULL, 0, 0, 0);
assert(BitBltHookVerifier == 1337);
Detour_Ex->UnHook();
BitBlt(NULL, 0, 0, 0, 0, NULL, 0, 0, 0);
assert(BitBltHookVerifier == 1337);
std::cout << (Detour_Ex->GetLastError().GetSeverity() != PLH::RuntimeError::Severity::UnRecoverable) << '\n';
std::cout << (Detour_Ex->GetLastError().GetSeverity() != PLH::RuntimeError::Severity::Critical) << '\n';
}
#endif
int main(int argc, char** argv) { int main(int argc, char** argv) {
AbstractHookEngine* engines[] = { AbstractHookEngine* engines[] = {
g_mhook, g_mhook,

View File

@@ -9,13 +9,13 @@
#pragma comment(lib, "..\\x64\\debug\\test_cases.lib") #pragma comment(lib, "..\\x64\\debug\\test_cases.lib")
static TypeSmall trueSmall = &_small; static TypeSmall trueSmall = (TypeSmall)AbstractHookEngine::getSmall();
static TypeBranch trueBranch = &_branch; static TypeBranch trueBranch = (TypeBranch)AbstractHookEngine::getBranch();
static TypeRip_relative trueRip_Relative = &_rip_relative; static TypeRip_relative trueRip_Relative = (TypeRip_relative)AbstractHookEngine::getRipRelative();
static TypeAVX trueAVX = &_AVX; static TypeAVX trueAVX = (TypeAVX)AbstractHookEngine::getAVX();
static TypeRDRAND trueRDRAND = &_RDRAND; static TypeRDRAND trueRDRAND = (TypeRDRAND)AbstractHookEngine::getRDRAND();
static TypeLoop trueLoop = &_loop; static TypeLoop trueLoop = (TypeLoop)AbstractHookEngine::getLoop();
static TypeTailRecursion trueTailRecursion = &_tail_recursion; static TypeTailRecursion trueTailRecursion = (TypeTailRecursion)AbstractHookEngine::getTailRecursion();
AbstractHookEngine* g_mhook = new MHook(); AbstractHookEngine* g_mhook = new MHook();

View File

@@ -63,19 +63,19 @@ uint32_t MinHook_Hooks::hookTail_recursion(uint32_t x) {
} }
bool MinHook::hook_all(void) { bool MinHook::hook_all(void) {
bool ret = MH_CreateHook(&_small, &MinHook_Hooks::hookSmall, (LPVOID*)&trueSmall) == MH_OK; bool ret0 = MH_CreateHook(getSmall(), &MinHook_Hooks::hookSmall, (LPVOID*)&trueSmall) == MH_OK;
ret |= MH_CreateHook(&_branch, &MinHook_Hooks::hookBranch, (LPVOID*)&trueBranch) == MH_OK; bool ret1 = MH_CreateHook(getBranch(), &MinHook_Hooks::hookBranch, (LPVOID*)&trueBranch) == MH_OK;
ret |= MH_CreateHook(&rip_relative, &MinHook_Hooks::hookRip_relative, (LPVOID*)&trueRip_Relative) == MH_OK; bool ret2 = MH_CreateHook(getRipRelative(), &MinHook_Hooks::hookRip_relative, (LPVOID*)&trueRip_Relative) == MH_OK;
ret |= MH_CreateHook(&_AVX, &MinHook_Hooks::hookAVX, (LPVOID*)&trueAVX) == MH_OK; bool ret3 = MH_CreateHook(getAVX(), &MinHook_Hooks::hookAVX, (LPVOID*)&trueAVX) == MH_OK;
ret |= MH_CreateHook(&_RDRAND, &MinHook_Hooks::hookRDRAND, (LPVOID*)&trueRDRAND) == MH_OK; bool ret4 = MH_CreateHook(getRDRAND(), &MinHook_Hooks::hookRDRAND, (LPVOID*)&trueRDRAND) == MH_OK;
ret |= MH_CreateHook(&_loop, &MinHook_Hooks::hookLoop, (LPVOID*)&trueLoop) == MH_OK; bool ret5 = MH_CreateHook(getLoop(), &MinHook_Hooks::hookLoop, (LPVOID*)&trueLoop) == MH_OK;
ret |= MH_CreateHook(&_tail_recursion, &MinHook_Hooks::hookTail_recursion, (LPVOID*)&trueTailRecursion) == MH_OK; bool ret6 = MH_CreateHook(getTailRecursion(), &MinHook_Hooks::hookTail_recursion, (LPVOID*)&trueTailRecursion) == MH_OK;
ret |= MH_EnableHook(MH_ALL_HOOKS) == MH_OK; bool ret7 = MH_EnableHook(MH_ALL_HOOKS) == MH_OK;
return ret; return ret0;
} }
bool MinHook::unhook_all() { bool MinHook::unhook_all() {

View File

@@ -62,15 +62,15 @@ uint32_t PolyHook_Hooks::hookTail_recursion(uint32_t x) {
} }
bool PolyHook::hook_all(void) { bool PolyHook::hook_all(void) {
bool ret = hook<decltype(&_small)>(detSmall, &_small, trueSmall, &PolyHook_Hooks::hookSmall); bool ret = hook<decltype(&_small)>(detSmall, getSmall(), trueSmall, &PolyHook_Hooks::hookSmall);
ret |= hook<decltype(&_branch)>(detBranch, &_branch, trueBranch, &PolyHook_Hooks::hookBranch); ret |= hook<decltype(&_branch)>(detBranch, getBranch(), trueBranch, &PolyHook_Hooks::hookBranch);
ret |= hook<decltype(&_rip_relative)>(detRIPRelative, &_rip_relative, trueRip_Relative, &PolyHook_Hooks::hookRip_relative); ret |= hook<decltype(&_rip_relative)>(detRIPRelative, getRipRelative(), trueRip_Relative, &PolyHook_Hooks::hookRip_relative);
ret |= hook<decltype(&_AVX)>(detAVX, &_AVX, trueAVX, &PolyHook_Hooks::hookAVX); ret |= hook<decltype(&_AVX)>(detAVX, getAVX(), trueAVX, &PolyHook_Hooks::hookAVX);
ret |= hook<decltype(&_RDRAND)>(detRDRAND, &_RDRAND, trueRDRAND, &PolyHook_Hooks::hookRDRAND); ret |= hook<decltype(&_RDRAND)>(detRDRAND, getRDRAND(), trueRDRAND, &PolyHook_Hooks::hookRDRAND);
ret |= hook<decltype(&_loop)>(detLoop, &_loop, trueLoop, &PolyHook_Hooks::hookLoop); ret |= hook<decltype(&_loop)>(detLoop, getLoop(), trueLoop, &PolyHook_Hooks::hookLoop);
ret |= hook<decltype(&_tail_recursion)>(detTailRecursion, &_tail_recursion, trueTailRecursion, &PolyHook_Hooks::hookTail_recursion); ret |= hook<decltype(&_tail_recursion)>(detTailRecursion, getTailRecursion(), trueTailRecursion, &PolyHook_Hooks::hookTail_recursion);
return ret; return ret;
} }

View File

@@ -21,10 +21,10 @@
<ClCompile Include="mhook.cpp"> <ClCompile Include="mhook.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="polyhook.cpp"> <ClCompile Include="minhook.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="minhook.cpp"> <ClCompile Include="polyhook.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>