This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvfunc_hook.hpp
164 lines (123 loc) · 2.92 KB
/
vfunc_hook.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#define NOMINMAX
#include <Windows.h>
#include <cstdint>
#include <stdexcept>
#include <assert.h>
class vmthook
{
public:
vmthook();
vmthook(PDWORD* ppdwClassBase);
~vmthook();
bool initialize(PDWORD* ppdwClassBase);
bool initialize(PDWORD** pppdwClassBase);
void clear_class_base();
void unhook();
void rehook();
int get_func_count();
template <typename Fn>
Fn get_func_address(int Index)
{
if (Index >= 0 && Index <= (int)m_VTSize && m_OldVT != NULL)
return reinterpret_cast<Fn>(m_OldVT[Index]);
return NULL;
}
PDWORD get_old_vt();
DWORD hook_function(DWORD dwNewFunc, unsigned int iIndex);
private:
DWORD get_vt_count(PDWORD pdwVMT);
PDWORD* m_ClassBase;
PDWORD m_NewVT, m_OldVT;
DWORD m_VTSize;
};
class ProtectGuard
{
public:
ProtectGuard(void *base, uint32_t len, uint32_t protect) //-V688
{
this->base = base;
this->len = len;
VirtualProtect(base, len, protect, (PDWORD)&old_protect); //-V2001
}
~ProtectGuard()
{
VirtualProtect(base, len, old_protect, (PDWORD)&old_protect); //-V2001
}
private:
void *base;
uint32_t len;
uint32_t old_protect;
};
class ShadowVTManager
{
public:
ShadowVTManager() : class_base(nullptr), method_count(0), shadow_vtable(nullptr), original_vtable(nullptr) {}
ShadowVTManager(void *base) : class_base(base), method_count(0), shadow_vtable(nullptr), original_vtable(nullptr) {}
~ShadowVTManager()
{
RestoreTable();
delete[] shadow_vtable;
}
inline void Setup(void *base = nullptr)
{
if (base != nullptr)
class_base = base;
if (class_base == nullptr)
return;
original_vtable = *(uintptr_t**)class_base;
method_count = GetMethodCount(original_vtable);
if (method_count == 0)
return;
shadow_vtable = new uintptr_t[method_count + 1]();
shadow_vtable[0] = original_vtable[-1];
std::memcpy(&shadow_vtable[1], original_vtable, method_count * sizeof(uintptr_t));
try
{
auto guard = ProtectGuard{ class_base, sizeof(uintptr_t), PAGE_READWRITE };
*(uintptr_t**)class_base = &shadow_vtable[1];
}
catch (...)
{
delete[] shadow_vtable;
}
}
template<typename T>
inline void Hook(uint32_t index, T method)
{
shadow_vtable[index + 1] = reinterpret_cast<uintptr_t>(method);
}
inline void Unhook(uint32_t index)
{
shadow_vtable[index + 1] = original_vtable[index];
}
template<typename T>
inline T GetOriginal(uint32_t index)
{
return (T)original_vtable[index];
}
inline void RestoreTable()
{
try
{
if (original_vtable != nullptr)
{
auto guard = ProtectGuard{ class_base, sizeof(uintptr_t), PAGE_READWRITE };
*(uintptr_t**)class_base = original_vtable;
original_vtable = nullptr;
}
}
catch (...) {} //-V565
}
private:
inline uint32_t GetMethodCount(uintptr_t *vtable_start) //-V2009
{
uint32_t len = -1;
do ++len; while (vtable_start[len]);
return len;
}
void *class_base;
uint32_t method_count;
uintptr_t *shadow_vtable;
uintptr_t *original_vtable;
};