Browse Source

polyhook

master
aaaaaa aaaaaaa 7 years ago
parent
commit
05573a8666
9 changed files with 175 additions and 14 deletions
  1. +10
    -0
      hook_tests.sln
  2. +15
    -2
      tester/abstracthook.h
  3. +6
    -4
      tester/main.cpp
  4. +2
    -0
      tester/mhook.cpp
  5. +90
    -0
      tester/minhook.cpp
  6. +33
    -0
      tester/minhook.h
  7. +9
    -8
      tester/polyhook.cpp
  8. +3
    -0
      tester/tester.vcxproj
  9. +7
    -0
      tester/tester.vcxproj.filters

+ 10
- 0
hook_tests.sln View File

EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PolyHook", "third_party\poly\PolyHook\PolyHook.vcxproj", "{64269F60-A538-4327-82EE-AB4BF4766CE9}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PolyHook", "third_party\poly\PolyHook\PolyHook.vcxproj", "{64269F60-A538-4327-82EE-AB4BF4766CE9}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libMinHook", "third_party\minhook\build\VC15\libMinHook.vcxproj", "{F142A341-5EE0-442D-A15F-98AE9B48DBAE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 Debug|x64 = Debug|x64
{64269F60-A538-4327-82EE-AB4BF4766CE9}.Release|x64.Build.0 = Release|x64 {64269F60-A538-4327-82EE-AB4BF4766CE9}.Release|x64.Build.0 = Release|x64
{64269F60-A538-4327-82EE-AB4BF4766CE9}.Release|x86.ActiveCfg = Release|Win32 {64269F60-A538-4327-82EE-AB4BF4766CE9}.Release|x86.ActiveCfg = Release|Win32
{64269F60-A538-4327-82EE-AB4BF4766CE9}.Release|x86.Build.0 = Release|Win32 {64269F60-A538-4327-82EE-AB4BF4766CE9}.Release|x86.Build.0 = Release|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x64.ActiveCfg = Debug|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x64.Build.0 = Debug|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x86.ActiveCfg = Debug|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x86.Build.0 = Debug|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x64.ActiveCfg = Release|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x64.Build.0 = Release|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x86.ActiveCfg = Release|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

+ 15
- 2
tester/abstracthook.h View File

virtual bool hook_all() = 0; virtual bool hook_all() = 0;
virtual bool unhook_all() = 0; virtual bool unhook_all() = 0;


bool all_hooked() {
bool all_hooked() const {
return small_ && branch && rip_relative && avx && rdrand && loop && tail_recursion; return small_ && branch && rip_relative && avx && rdrand && loop && tail_recursion;
} }


const char* name() {
const char* name() const {
return name_; return name_;
} }

friend std::ostream& operator<< (std::ostream& stream, const AbstractHookEngine& eng) {
std::cout << '|' << std::setw(10) << eng.name() << std::setw(1) << '|' <<
(eng.small_ ? 'X' : ' ') << '|' <<
(eng.branch ? 'X' : ' ') << '|' <<
(eng.rip_relative ? 'X' : ' ') << '|' <<
(eng.avx ? 'X' : ' ') << '|' <<
(eng.rdrand ? 'X' : ' ') << '|' <<
(eng.loop ? 'X' : ' ') << '|' <<
(eng.tail_recursion ? 'X' : ' ') << '|';

return stream;
}
}; };

+ 6
- 4
tester/main.cpp View File

#include <Windows.h>
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
#include <iomanip>
#include "../test_cases/test_cases.h" #include "../test_cases/test_cases.h"


#include "abstracthook.h" #include "abstracthook.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;


int main(int argc, char** argv) { int main(int argc, char** argv) {
AbstractHookEngine* engines[] = { AbstractHookEngine* engines[] = {
g_mhook, g_mhook,
g_PolyHook
g_PolyHook,
g_MinHook
}; };


for(auto&& x : engines) { for(auto&& x : engines) {
} }


SelfTest(); SelfTest();
std::cout << x->name() << ':' << x->all_hooked() << '\n';
std::cout << *x << std::endl;
x->unhook_all(); x->unhook_all();
} }
} }

+ 2
- 0
tester/mhook.cpp View File

#include <Windows.h> #include <Windows.h>
#include <iostream>
#include <iomanip>
#include <cstdint> #include <cstdint>
#include "../third_party/mhook/mhook-lib/mhook.h" #include "../third_party/mhook/mhook-lib/mhook.h"
#include "typedefs.h" #include "typedefs.h"

+ 90
- 0
tester/minhook.cpp View File

#include <cstdint>
#include <iostream>
#include <iomanip>

#include "typedefs.h"
#include "..\third_party\minhook\include\minhook.h"
#include "abstracthook.h"
#include "minhook.h"

static auto initalized = MH_Initialize();

static TypeSmall trueSmall = nullptr;
static TypeBranch trueBranch = nullptr;
static TypeRip_relative trueRip_Relative = nullptr;
static TypeAVX trueAVX = nullptr;
static TypeRDRAND trueRDRAND = nullptr;
static TypeLoop trueLoop = nullptr;
static TypeTailRecursion trueTailRecursion = nullptr;

AbstractHookEngine* g_MinHook = new MinHook();


uint64_t MinHook_Hooks::hookSmall(void) {
g_MinHook->small_ = true;

return trueSmall();
}

uint64_t MinHook_Hooks::hookBranch(uint64_t x) {
g_MinHook->branch = true;

return trueBranch(x);
}

uint64_t MinHook_Hooks::hookRip_relative(void) {
g_MinHook->rip_relative = true;

return trueRip_Relative();
}

void MinHook_Hooks::hookAVX(float num, void* res) {
g_MinHook->avx = true;

return trueAVX(num, res);
}

uint32_t MinHook_Hooks::hookRDRAND(void) {
g_MinHook->rdrand = true;

return trueRDRAND();
}

uint32_t MinHook_Hooks::hookLoop(uint32_t num, uint32_t cnt) {
g_MinHook->loop = true;

return trueLoop(num, cnt);
}

uint32_t MinHook_Hooks::hookTail_recursion(uint32_t x) {
g_MinHook->tail_recursion = true;

return trueTailRecursion(x);
}

bool MinHook::hook_all(void) {
bool ret = MH_CreateHook(&_small, &MinHook_Hooks::hookSmall, (LPVOID*)&trueSmall) == MH_OK;
ret |= MH_CreateHook(&_branch, &MinHook_Hooks::hookBranch, (LPVOID*)&trueBranch) == MH_OK;
ret |= MH_CreateHook(&rip_relative, &MinHook_Hooks::hookRip_relative, (LPVOID*)&trueRip_Relative) == MH_OK;

ret |= MH_CreateHook(&_AVX, &MinHook_Hooks::hookAVX, (LPVOID*)&trueAVX) == MH_OK;
ret |= MH_CreateHook(&_RDRAND, &MinHook_Hooks::hookRDRAND, (LPVOID*)&trueRDRAND) == MH_OK;

ret |= MH_CreateHook(&_loop, &MinHook_Hooks::hookLoop, (LPVOID*)&trueLoop) == MH_OK;
ret |= MH_CreateHook(&_tail_recursion, &MinHook_Hooks::hookTail_recursion, (LPVOID*)&trueTailRecursion) == MH_OK;

ret |= MH_EnableHook(MH_ALL_HOOKS) == MH_OK;

return ret;
}

bool MinHook::unhook_all() {
return MH_DisableHook(MH_ALL_HOOKS) == MH_OK &&
MH_RemoveHook(&_small) == MH_OK &&
MH_RemoveHook(&_branch) == MH_OK &&
MH_RemoveHook(&rip_relative) == MH_OK &&
MH_RemoveHook(&_AVX) == MH_OK &&
MH_RemoveHook(&_RDRAND) == MH_OK &&
MH_RemoveHook(&_loop) == MH_OK &&
MH_RemoveHook(&_tail_recursion) == MH_OK;
}

+ 33
- 0
tester/minhook.h View File

#pragma once

namespace MinHook_Hooks {
uint64_t hookSmall(void);
uint64_t hookBranch(uint64_t);
uint64_t hookRip_relative(void);
void hookAVX(float num, void* res);
uint32_t hookRDRAND(void);
uint32_t hookLoop(uint32_t num, uint32_t cnt);
uint32_t hookTail_recursion(uint32_t x);
};

class MinHook : public AbstractHookEngine {
private:
public:
bool hook_all();
bool unhook_all();

MinHook() : AbstractHookEngine("MinHook")
{
}

friend uint64_t MinHook_Hooks::hookSmall(void);
friend uint64_t MinHook_Hooks::hookBranch(uint64_t);
friend uint64_t MinHook_Hooks::hookRip_relative(void);

friend void MinHook_Hooks::hookAVX(float num, void* res);
friend uint32_t MinHook_Hooks::hookRDRAND(void);
friend uint32_t MinHook_Hooks::hookLoop(uint32_t num, uint32_t cnt);
friend uint32_t MinHook_Hooks::hookTail_recursion(uint32_t x);
};

+ 9
- 8
tester/polyhook.cpp View File

#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include "..\third_party\poly\PolyHook\PolyHook.hpp" #include "..\third_party\poly\PolyHook\PolyHook.hpp"


#pragma comment(lib, "..\\x64\\debug\\test_cases.lib") #pragma comment(lib, "..\\x64\\debug\\test_cases.lib")


static TypeSmall trueSmall = &_small;
static TypeBranch trueBranch = &_branch;
static TypeRip_relative trueRip_Relative = &_rip_relative;
static TypeAVX trueAVX = &_AVX;
static TypeRDRAND trueRDRAND = &_RDRAND;
static TypeLoop trueLoop = &_loop;
static TypeTailRecursion trueTailRecursion = &_tail_recursion;
static TypeSmall trueSmall = nullptr;
static TypeBranch trueBranch = nullptr;
static TypeRip_relative trueRip_Relative = nullptr;
static TypeAVX trueAVX = nullptr;
static TypeRDRAND trueRDRAND = nullptr;
static TypeLoop trueLoop = nullptr;
static TypeTailRecursion trueTailRecursion = nullptr;


AbstractHookEngine* g_PolyHook = new PolyHook(); AbstractHookEngine* g_PolyHook = new PolyHook();



+ 3
- 0
tester/tester.vcxproj View File

<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="mhook.cpp" /> <ClCompile Include="mhook.cpp" />
<ClCompile Include="minhook.cpp" />
<ClCompile Include="polyhook.cpp" /> <ClCompile Include="polyhook.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="abstracthook.h" /> <ClInclude Include="abstracthook.h" />
<ClInclude Include="mhook.h" /> <ClInclude Include="mhook.h" />
<ClInclude Include="minhook.h" />
<ClInclude Include="polyhook.h" /> <ClInclude Include="polyhook.h" />
<ClInclude Include="typedefs.h" /> <ClInclude Include="typedefs.h" />
</ItemGroup> </ItemGroup>
<Object Include="..\x64\Debug\misc.obj" /> <Object Include="..\x64\Debug\misc.obj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Library Include="..\lib\Debug\libMinHook.x64.lib" />
<Library Include="..\third_party\poly\Capstone\msvc\x64\Release\capstone.lib" /> <Library Include="..\third_party\poly\Capstone\msvc\x64\Release\capstone.lib" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 7
- 0
tester/tester.vcxproj.filters View File

<ClCompile Include="polyhook.cpp"> <ClCompile Include="polyhook.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="minhook.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="typedefs.h"> <ClInclude Include="typedefs.h">
<ClInclude Include="polyhook.h"> <ClInclude Include="polyhook.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="minhook.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Object Include="..\x64\Debug\mhook.obj" /> <Object Include="..\x64\Debug\mhook.obj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Library Include="..\third_party\poly\Capstone\msvc\x64\Release\capstone.lib" /> <Library Include="..\third_party\poly\Capstone\msvc\x64\Release\capstone.lib" />
<Library Include="..\lib\Debug\libMinHook.x64.lib" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Loading…
Cancel
Save