From 72b73ee462b6d6c8e0ebd5fb85186f776377ac95 Mon Sep 17 00:00:00 2001 From: aaaaaa aaaaaaa Date: Wed, 27 Dec 2017 19:07:15 +0100 Subject: [PATCH] simple_tests with mhook --- hook_tests.sln | 1 + tester/abstracthook.h | 13 ++++++++++++- tester/main.cpp | 9 +++++++-- tester/mhook.cpp | 32 +++++++++++++++++++++++++------- tester/mhook.h | 12 +++++++++++- tester/tester.vcxproj | 1 + tester/tester.vcxproj.filters | 4 ++-- tester/typedefs.h | 6 +++++- 8 files changed, 64 insertions(+), 14 deletions(-) diff --git a/hook_tests.sln b/hook_tests.sln index 61b894f..d34a07a 100644 --- a/hook_tests.sln +++ b/hook_tests.sln @@ -7,6 +7,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_cases", "test_cases\te EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tester", "tester\tester.vcxproj", "{8182D1BA-E651-4668-9EC1-A3023AAFD5AC}" ProjectSection(ProjectDependencies) = postProject + {0E055CAF-C68B-42CB-A302-F775CA5A917F} = {0E055CAF-C68B-42CB-A302-F775CA5A917F} {8C444ABC-D25C-4B44-8F27-081B464D9AE4} = {8C444ABC-D25C-4B44-8F27-081B464D9AE4} EndProjectSection EndProject diff --git a/tester/abstracthook.h b/tester/abstracthook.h index cc83062..3fb485e 100644 --- a/tester/abstracthook.h +++ b/tester/abstracthook.h @@ -2,6 +2,14 @@ class AbstractHookEngine { private: const char* name_; + +public: + /* boolean for each hook test case, which are set by the hooks */ + struct { + bool small_; + bool branch; + bool rip_relative; + }; public: AbstractHookEngine(const char* name) : name_(name) { @@ -9,7 +17,10 @@ public: virtual bool hook_all() = 0; virtual bool unhook_all() = 0; - virtual bool all_hooked() = 0; + + bool all_hooked() { + return small_ && branch && rip_relative; + } const char* name() { return name_; diff --git a/tester/main.cpp b/tester/main.cpp index eed9162..a5f59b1 100644 --- a/tester/main.cpp +++ b/tester/main.cpp @@ -6,7 +6,7 @@ #include "abstracthook.h" #include "mhook.h" -#pragma comment(lib, "..\\x64\\release\\test_cases.lib") +#pragma comment(lib, "..\\x64\\debug\\test_cases.lib") extern AbstractHookEngine* g_mhook; @@ -16,7 +16,12 @@ int main(int argc, char** argv) { }; for(auto&& x : engines) { - x->hook_all(); + if (!x->hook_all()) { + std::cerr << x->name() << " can't hook\n"; + x->unhook_all(); + continue; + } + SelfTest(); std::cout << x->name() << ':' << x->all_hooked() << '\n'; x->unhook_all(); diff --git a/tester/mhook.cpp b/tester/mhook.cpp index cd59c8d..aed9db2 100644 --- a/tester/mhook.cpp +++ b/tester/mhook.cpp @@ -8,21 +8,39 @@ #pragma comment(lib, "..\\x64\\debug\\test_cases.lib") static TypeSmall trueSmall = &_small; +static TypeBranch trueBranch = &_branch; +static TypeRip_relative trueRip_Relative = &_rip_relative; AbstractHookEngine* g_mhook = new MHook(); -static uint64_t hookSmall(void) { +uint64_t MHook_Hooks::hookSmall(void) { + g_mhook->small_ = true; + return trueSmall(); } -bool MHook::hook_all(void) { - return Mhook_SetHook((PVOID*)&trueSmall, hookSmall); +uint64_t MHook_Hooks::hookBranch(uint64_t x) { + g_mhook->branch = true; + + return trueBranch(x); } -bool MHook::unhook_all() { - return Mhook_Unhook((PVOID*)&trueSmall); +uint64_t MHook_Hooks::hookRip_relative(void) { + g_mhook->rip_relative = true; + + return trueRip_Relative(); } -bool MHook::all_hooked() { - return true; +bool MHook::hook_all(void) { + bool ret = Mhook_SetHook((PVOID*)&trueSmall, &MHook_Hooks::hookSmall); + ret |= Mhook_SetHook((PVOID*)&trueBranch, &MHook_Hooks::hookBranch); + ret |= Mhook_SetHook((PVOID*)&trueRip_Relative, &MHook_Hooks::hookRip_relative); + + return ret; +} + +bool MHook::unhook_all() { + return Mhook_Unhook((PVOID*)&trueSmall) && + Mhook_Unhook((PVOID*)&trueBranch) && + Mhook_Unhook((PVOID*)&trueRip_Relative); } \ No newline at end of file diff --git a/tester/mhook.h b/tester/mhook.h index 5369a59..0381caa 100644 --- a/tester/mhook.h +++ b/tester/mhook.h @@ -1,11 +1,21 @@ #pragma once + +namespace MHook_Hooks { + uint64_t hookSmall(void); + uint64_t hookBranch(uint64_t); + uint64_t hookRip_relative(void); +}; + class MHook : public AbstractHookEngine { public: bool hook_all(); bool unhook_all(); - bool all_hooked(); MHook() : AbstractHookEngine("MHook") { } + + friend uint64_t MHook_Hooks::hookSmall(void); + friend uint64_t MHook_Hooks::hookBranch(uint64_t); + friend uint64_t MHook_Hooks::hookRip_relative(void); }; \ No newline at end of file diff --git a/tester/tester.vcxproj b/tester/tester.vcxproj index 6332917..247cbd0 100644 --- a/tester/tester.vcxproj +++ b/tester/tester.vcxproj @@ -107,6 +107,7 @@ Console true + %(AdditionalDependencies) diff --git a/tester/tester.vcxproj.filters b/tester/tester.vcxproj.filters index e8c7fe6..8b3d159 100644 --- a/tester/tester.vcxproj.filters +++ b/tester/tester.vcxproj.filters @@ -35,9 +35,9 @@ - - + + \ No newline at end of file diff --git a/tester/typedefs.h b/tester/typedefs.h index 3a33f20..7d96529 100644 --- a/tester/typedefs.h +++ b/tester/typedefs.h @@ -1,4 +1,8 @@ #pragma once #include "../test_cases/test_cases.h" -typedef uint64_t(*TypeSmall)(void); \ No newline at end of file +typedef uint64_t(*TypeSmall)(void); + +typedef uint64_t (*TypeBranch)(uint64_t); + +typedef uint64_t (*TypeRip_relative)(void); \ No newline at end of file