-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHooking.h
164 lines (144 loc) · 4.35 KB
/
Hooking.h
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
#pragma once
#include <map>
#include <string>
#include <unordered_map> //c++11 hash_map
#include <stack>
#include <unordered_set>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#ifdef _WIN32
#define ASM_START _asm {
#define ASM_END }
#define ASM_MACRO_SS _asm
#define EXPORT _declspec(dllexport)
#define FASTCALL __fastcall
#define NAKED _declspec(naked)
#endif
#define HOOKCLASS(classname) namespace SBApiHooks \
{ \
class classname \
{ \
#define ENDHOOKCLASS };}
//addr is temporary, will be replaced with symbol/signature later
#define HOOKMEMBERFUNCTION(funcName, addr) void hook_funcName();
#define HOOKDEST_DECL(classname) friend class classname##_init; \
public: \
static std::unordered_map<std::string, void*> m_selfFuncAddrs; \
static std::unordered_map<std::string, void*> m_funcAddrs;
#define HOOKDEST_DEF(classname) std::unordered_map<std::string, void*> PluginTitle::m_selfFuncAddrs; \
std::unordered_map<std::string, void*> PluginTitle::m_funcAddrs;
#define HOOKTABLE_START(classname) template<typename T> \
struct classname##_hookinit { \
classname##_hookinit() {
#define SETUPHOOKEDFUNC(tgtClassname, funcName) T::m_selfFuncAddrs[ #funcName ] = &(void*&)T::##funcName; \
g_hookedClasses[ #tgtClassname ]->hookMemberFunc( #funcName , &(void*&)T::##funcName);
#define HOOKTABLE_END(classname) }}; \
static classname##_hookinit<##classname##> classname##_init;
#define CALL_FUNC(funcName, addrVarName) addrVarName = m_funcAddrs[ #funcName ]; \
ASM_START \
ASM_MACRO_SS mov ecx, this \
ASM_MACRO_SS call addrVarName \
ASM_END
class ClassHooker;
extern std::unordered_map<std::string, ClassHooker*> g_hookedClasses;
int FASTCALL cmHookLanding(ClassHooker *hooker, const char* funcName, void* thisptr);
void hookCode();
class ClassHooker
{
const char* m_classname;
std::unordered_map<std::string, void*> m_hookableFunctions;
std::unordered_map<std::string, std::stack<void*>> m_destAddrs;
std::unordered_set<std::string> m_hookedFunctions;
std::unordered_map<std::string, char*> m_originalBytes;
protected:
std::unordered_map<std::string, void*> m_destAddress;
static ClassHooker* singleton;
ClassHooker(const char* classname) : m_classname(classname)
{
g_hookedClasses[classname] = this;
}
//addr is temporary, until scanning engine is in place
void setupHookMemberFunc(const char* funcName, void* addr)
{
m_hookableFunctions[funcName] = addr;
}
void doHook(const char* funcName)
{
DWORD temp;
VirtualProtect(&hookCode, 23, PAGE_EXECUTE_READ, &temp);
char* tCode = new char[23]; //tailored code
memcpy(tCode, &hookCode, 23);
const unsigned char mrk1[4] = {0xAB, 0xAB, 0xAB, 0xAB};
const unsigned char mrk2[4] = {0xBC, 0xBC, 0xBC, 0xBC};
const unsigned char mrk3[4] = {0xCD, 0xCD, 0xCD, 0xCD};
for(unsigned int i = 0; i < 23; ++i)
{
unsigned char c = tCode[i];
if(c == mrk1[0])
{
if(memcmp(tCode+i, mrk1, 4) == 0)
{
*(int*)(tCode+i) = (int)this;
}
}
else if(c == mrk2[0])
{
if(memcmp(tCode+i, mrk2, 4) == 0)
{
*(int*)(tCode+i) = (int)funcName;
}
}
else if(c == mrk3[0])
{
if(memcmp(tCode+i, mrk3, 4) == 0)
{
*(int*)(tCode+i) = (int)&cmHookLanding;
}
}
}
VirtualProtect(m_hookableFunctions[funcName], 23, PAGE_EXECUTE_READWRITE, &temp);
char* orig = new char[23];
memcpy(orig, m_hookableFunctions[funcName], 23);
m_originalBytes[funcName] = orig;
memcpy(m_hookableFunctions[funcName], tCode, 23);
delete [] tCode;
}
void doUnhook(const char* funcName)
{
memcpy(m_hookableFunctions[funcName], m_originalBytes[funcName], 23);
delete [] m_originalBytes[funcName];
m_originalBytes[funcName] = NULL;
}
public:
void* getDestAddr(const char* funcName)
{
if(m_destAddrs[funcName].empty())
{
return m_hookableFunctions[funcName];
}
return m_destAddrs[funcName].top();
}
void hookMemberFunc(const char* funcName, void* newAddr)
{
if(m_hookableFunctions.find(funcName) == m_hookableFunctions.end())
{
return;
}
if(m_hookedFunctions.find(funcName) == m_hookedFunctions.end())
{
doHook(funcName);
}
m_destAddrs[funcName].push(newAddr);
}
void unhookMemberFunc(const char* funcName)
{
if(!m_destAddrs[funcName].empty())
{
m_destAddrs[funcName].pop();
if(m_destAddrs[funcName].empty())
{
doUnhook(funcName);
}
}
}
};