simple_tests with mhook

This commit is contained in:
2017-12-27 19:07:15 +01:00
parent 72af6439a7
commit 72b73ee462
8 changed files with 64 additions and 14 deletions

View File

@@ -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

View File

@@ -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_;

View File

@@ -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();

View File

@@ -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();
}
uint64_t MHook_Hooks::hookBranch(uint64_t x) {
g_mhook->branch = true;
return trueBranch(x);
}
uint64_t MHook_Hooks::hookRip_relative(void) {
g_mhook->rip_relative = true;
return trueRip_Relative();
}
bool MHook::hook_all(void) {
return Mhook_SetHook((PVOID*)&trueSmall, hookSmall);
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);
}
bool MHook::all_hooked() {
return true;
return Mhook_Unhook((PVOID*)&trueSmall) &&
Mhook_Unhook((PVOID*)&trueBranch) &&
Mhook_Unhook((PVOID*)&trueRip_Relative);
}

View File

@@ -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);
};

View File

@@ -107,6 +107,7 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">

View File

@@ -35,9 +35,9 @@
</ItemGroup>
<ItemGroup>
<Object Include="..\x64\Debug\mhook.obj" />
<Object Include="..\x64\Debug\disasm.obj" />
<Object Include="..\x64\Debug\disasm_x86.obj" />
<Object Include="..\x64\Debug\misc.obj" />
<Object Include="..\x64\Debug\disasm_x86.obj" />
<Object Include="..\x64\Debug\disasm.obj" />
<Object Include="..\x64\Debug\cpu.obj" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,8 @@
#pragma once
#include "../test_cases/test_cases.h"
typedef uint64_t(*TypeSmall)(void);
typedef uint64_t(*TypeSmall)(void);
typedef uint64_t (*TypeBranch)(uint64_t);
typedef uint64_t (*TypeRip_relative)(void);