-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhook.cpp
More file actions
63 lines (53 loc) · 1.98 KB
/
Copy pathhook.cpp
File metadata and controls
63 lines (53 loc) · 1.98 KB
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
#include "include/hook.h"
// credit goes to Trypthamine (https://github.com/NougatBitz/HookBasedInjection) for this
#define Offset(Base, Rva) reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(Base) + Rva)
__forceinline void* read_jmp_rel32(Process& p, void* instruction) {
int32_t relativeoffset = 0;
p.read_mem(Offset(instruction, 1), &relativeoffset, sizeof(int32_t), NULL);
return Offset(Offset(instruction, 5), relativeoffset);
}
__forceinline void* read_jmp_m64(Process& p, void* instruction) {
void* m64 = 0;
p.read_mem(Offset(instruction, 6), &m64, sizeof(void*), NULL);
return m64;
}
__forceinline void* write_jmp_m64(Process& p, void* instruction, void* target) {
void* oldtarget = read_jmp_m64(p, instruction);
DWORD oldprot;
p.change_protection(Offset(instruction, 6), sizeof(void*), PAGE_EXECUTE_READWRITE, &oldprot);
p.write_mem(Offset(instruction, 6), &target, sizeof(void*), NULL);
p.change_protection(Offset(instruction, 6), sizeof(void*), oldprot, NULL);
return oldtarget;
}
Hook::Hook(std::string name, void* address)
{
this->currentFunction.name = name;
this->currentFunction.address = address;
}
Hook::HookedFunction Hook::get_hooked_function()
{
return this->currentFunction;
}
Hook::CurrentHook Hook::get_current_hook()
{
return this->currentHook;
}
void Hook::hook_function(Process& p, void* jmpPage)
{
auto dynamicstub = read_jmp_rel32(p, this->currentFunction.address);
printf("[%s] Dynamic Stub Jmp Relative Offset = 0x%p\n", __FUNCTION__, dynamicstub);
auto hook = read_jmp_m64(p, dynamicstub);
printf("[%s] Hook Jmp m64 = 0x%p\n", __FUNCTION__, hook);
write_jmp_m64(p, dynamicstub, jmpPage);
this->currentHook.address = jmpPage;
this->currentHook.previousaddress = hook;
this->currentHook.stub = dynamicstub;
}
void Hook::unhook(Process& p)
{
write_jmp_m64(p, this->currentHook.stub, this->currentHook.previousaddress);
FlushInstructionCache(p.get_handle(), nullptr, 0);
}
Hook::~Hook()
{
}