-
Notifications
You must be signed in to change notification settings - Fork 2
/
inthook.hpp
156 lines (140 loc) · 4.95 KB
/
inthook.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
#pragma once
#include <Windows.h>
#include <vector>
#define INT3 0xCC
namespace inthook {
UCHAR original_call[]{
#ifdef _WIN64
0x51, // push rcx
0x52, // push rdx
0x41, 0x50, // push r8
0x41, 0x51, // push r9
0x50, // push rax
0x48, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov rcx, function
0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov rax, inthook::ignore
0xFF, 0xD0, // call rax
0x58, // pop rax
0x41, 0x59, // pop r9
0x41, 0x58, // pop r8
0x5A, // pop rdx
0x59, // pop rcx
0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov rax, function
0xFF, 0xE0 // jmp rax
#elif _WIN32
0x68, 0x00, 0x00, 0x00, 0x00, // push function
0xB8, 0xFF, 0xFF, 0xFF, 0xFF, // mov eax, inthook::ignore
0xFF, 0xD0, // call eax
0x58, // pop eax
0xB8, 0x00, 0x00, 0x00, 0x00, // mov eax, function
0xFF, 0xE0 // jmp eax
#endif
};
struct info {
PVOID function;
PVOID hook;
PVOID original;
UCHAR old_byte;
DWORD old_protect;
BOOL ignore;
BOOL disabled;
};
std::vector<info> hooks{};
PVOID seh;
LONG NTAPI vectored_handler(_EXCEPTION_POINTERS* exception) {
DWORD ex_code = exception->ExceptionRecord->ExceptionCode;
if (ex_code != EXCEPTION_BREAKPOINT && ex_code != EXCEPTION_SINGLE_STEP)
return EXCEPTION_CONTINUE_SEARCH;
for (info& cur : hooks) {
if (cur.disabled)
continue;
if (ex_code == EXCEPTION_BREAKPOINT && exception->ExceptionRecord->ExceptionAddress == cur.function) {
if (cur.ignore) {
*(UCHAR*)cur.function = cur.old_byte; // set original byte
exception->ContextRecord->EFlags |= 0x100; // signle step execution
return EXCEPTION_CONTINUE_EXECUTION;
}
#ifdef _WIN64
exception->ContextRecord->Rip = (DWORD64)cur.hook;
#elif _WIN32
exception->ContextRecord->Eip = (DWORD)cur.hook;
#endif
return EXCEPTION_CONTINUE_EXECUTION;
}
else if (ex_code == EXCEPTION_SINGLE_STEP && cur.ignore) {
exception->ContextRecord->EFlags &= ~0x100; // clear signle step flag
*(UCHAR*)cur.function = INT3; // set int3 byte
cur.ignore = false;
return EXCEPTION_CONTINUE_EXECUTION;
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
bool ignore(void* function) {
for (info& cur : hooks) {
if (function != cur.function && cur.disabled)
continue;
cur.ignore = true;
return true;
}
return false;
}
void* original(void* function) {
void* address = VirtualAlloc(0, sizeof(original_call), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!address)
return 0;
memcpy(address, &original_call, sizeof(original_call));
#ifdef _WIN64
*(DWORD64*)((DWORD64)address + 9) = (DWORD64)function;
*(DWORD64*)((DWORD64)address + 19) = (DWORD64)inthook::ignore;
*(DWORD64*)((DWORD64)address + 38) = (DWORD64)function;
#elif _WIN32
*(DWORD*)((DWORD)address + 1) = (DWORD)function;
*(DWORD*)((DWORD)address + 6) = (DWORD)inthook::ignore;
*(DWORD*)((DWORD)address + 14) = (DWORD)function;
#endif
return address;
}
bool create(void* function, void* hook, void* &original) {
info new_hook = { function, hook };
if (!VirtualProtect(new_hook.function, 0x1, PAGE_EXECUTE_READWRITE, &new_hook.old_protect))
return false;
new_hook.old_byte = *(UCHAR*)new_hook.function;
*(UCHAR*)new_hook.function = INT3; // set int3 byte
new_hook.original = inthook::original(new_hook.function);
if (!new_hook.original)
return false;
original = new_hook.original;
hooks.push_back(new_hook);
return true;
}
bool remove(void* function) {
DWORD unused;
for (info& cur : hooks) {
if (function != cur.function && cur.disabled)
continue;
*(UCHAR*)cur.function = cur.old_byte; // set original byte
VirtualProtect(cur.function, 0x1, cur.old_protect, &unused); // set original protect
VirtualFree(cur.original, NULL, MEM_RELEASE);
cur.disabled = true;
return true;
}
return false;
}
bool init() {
seh = AddVectoredExceptionHandler(1, vectored_handler);
return seh != 0;
}
bool uninit() {
DWORD unused;
for (info& cur : hooks) {
if (cur.disabled)
continue;
*(UCHAR*)cur.function = cur.old_byte; // set original byte
VirtualProtect(cur.function, 0x1, cur.old_protect, &unused); // set original protect
VirtualFree(cur.original, NULL, MEM_RELEASE);
cur.disabled = true;
}
hooks.clear();
return RemoveVectoredExceptionHandler(seh) != 0;
}
}