This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
/
vmt_smart_hook.hpp
181 lines (152 loc) · 4.4 KB
/
vmt_smart_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* This file is part of nSkinz by namazso, licensed under the MIT license:
*
* MIT License
*
* Copyright (c) namazso 2018
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "platform.hpp"
#include <cstring>
#include <cassert>
using proc_t = void(*)();
class vmt_base_hook
{
protected:
constexpr vmt_base_hook() = default;
public:
~vmt_base_hook()
{
if(m_new_vmt)
delete[] (m_new_vmt - 1);
}
vmt_base_hook(const vmt_base_hook&) = delete;
vmt_base_hook(vmt_base_hook&&) = delete;
vmt_base_hook& operator=(const vmt_base_hook&) = delete;
vmt_base_hook& operator=(vmt_base_hook&&) = delete;
protected:
auto initialize(proc_t* original_table) -> void
{
m_old_vmt = original_table;
size_t size = 0;
while(m_old_vmt[size] && platform::is_code_ptr(m_old_vmt[size]))
++size;
m_new_vmt = (new proc_t[size + 1]) + 1;
//std::copy(m_old_vmt - 1, m_old_vmt + size, m_new_vmt - 1);
memcpy(m_new_vmt - 1, m_old_vmt - 1, sizeof(void*) * (size + 1));
}
constexpr auto leak_table() -> void
{
m_new_vmt = nullptr;
}
auto hook_instance(void* inst) const -> void
{
auto& vtbl = *reinterpret_cast<proc_t**>(inst);
assert(vtbl == m_old_vmt || vtbl == m_new_vmt);
vtbl = m_new_vmt;
}
auto unhook_instance(void* inst) const -> void
{
auto& vtbl = *reinterpret_cast<proc_t**>(inst);
assert(vtbl == m_old_vmt || vtbl == m_new_vmt);
vtbl = m_old_vmt;
}
auto initialize_and_hook_instance(void* inst) -> bool
{
auto& vtbl = *reinterpret_cast<proc_t**>(inst);
auto initialized = false;
if(!m_old_vmt)
{
initialized = true;
initialize(vtbl);
}
hook_instance(inst);
return initialized;
}
template <typename Fn>
auto hook_function(Fn hooked_fn, const std::size_t index) -> Fn
{
m_new_vmt[index] = (proc_t)(hooked_fn);
return (Fn)(m_old_vmt[index]);
}
template<typename T>
auto apply_hook(std::size_t idx) -> void
{
T::m_original = hook_function(&T::hooked, idx);
}
template <typename Fn = uintptr_t>
auto get_original_function(const int index) -> Fn
{
return (Fn)(m_old_vmt[index]);
}
private:
proc_t* m_new_vmt = nullptr;
proc_t* m_old_vmt = nullptr;
};
class vmt_smart_hook : vmt_base_hook
{
public:
vmt_smart_hook(void* class_base)
: m_class{class_base}
{
initialize_and_hook_instance(class_base);
}
~vmt_smart_hook()
{
unhook_instance(m_class);
}
vmt_smart_hook(const vmt_smart_hook&) = delete;
vmt_smart_hook(vmt_smart_hook&&) = delete;
vmt_smart_hook& operator=(const vmt_smart_hook&) = delete;
vmt_smart_hook& operator=(vmt_smart_hook&&) = delete;
auto rehook() const -> void
{
hook_instance(m_class);
}
auto unhook() const -> void
{
unhook_instance(m_class);
}
using vmt_base_hook::apply_hook;
using vmt_base_hook::get_original_function;
using vmt_base_hook::hook_function;
private:
void* m_class = nullptr;
};
class vmt_multi_hook : vmt_base_hook
{
public:
constexpr vmt_multi_hook() = default;
~vmt_multi_hook()
{
leak_table();
}
vmt_multi_hook(const vmt_multi_hook&) = delete;
vmt_multi_hook(vmt_multi_hook&&) = delete;
vmt_multi_hook& operator=(const vmt_multi_hook&) = delete;
vmt_multi_hook& operator=(vmt_multi_hook&&) = delete;
using vmt_base_hook::apply_hook;
using vmt_base_hook::get_original_function;
using vmt_base_hook::hook_function;
using vmt_base_hook::hook_instance;
using vmt_base_hook::unhook_instance;
using vmt_base_hook::initialize;
using vmt_base_hook::initialize_and_hook_instance;
};