-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.cpp
More file actions
90 lines (65 loc) · 1.85 KB
/
Copy pathhook.cpp
File metadata and controls
90 lines (65 loc) · 1.85 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
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
#include "mem.h"
#include "hook.h"
uintptr_t FindDMAAddy(uintptr_t ptr, std::vector<unsigned int> offsets)
{
uintptr_t addr = ptr;
for (unsigned int i = 0; i < offsets.size(); ++i)
{
addr = *(uintptr_t*)addr;
addr += offsets[i];
}
return addr;
}
bool Detour32(BYTE* src, BYTE* dst, const uintptr_t len)
{
if (len < 5) return false;
DWORD curProtection;
VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &curProtection);
uintptr_t relativeAdress = dst - src - 5;
*src = 0xE9;
*(uintptr_t*)(src + 1) = relativeAdress;
VirtualProtect(src, len, curProtection, &curProtection);
return true;
}
BYTE* TrampHook32(BYTE* src, BYTE* dst, const uintptr_t len)
{
if (len < 5) return 0;
BYTE* gateway = (BYTE*)VirtualAlloc(0, len, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
memcpy_s(gateway, len, src, len);
uintptr_t gatewayRealtiveAddr = src - gateway - 5;
*(gateway + len) = 0xE9;
*(uintptr_t*)((uintptr_t)gateway + len + 1) = gatewayRealtiveAddr;
Detour32(src, dst, len);
return gateway;
}
Hook::Hook(BYTE* src, BYTE* dst, BYTE* PtrToGatewayFnPtr, uintptr_t len)
{
this->src = src;
this->dst = dst;
this->len = len;
this->PtrToGatewayFnPtr = PtrToGatewayFnPtr;
}
Hook::Hook(const char* exportName, const char* modName, BYTE* dst, BYTE* PtrToGatewayFnPtr, uintptr_t len)
{
HMODULE hMod = GetModuleHandleA(modName);
this->src = (BYTE*)GetProcAddress(hMod, exportName);
this->dst = dst;
this->len = len;
this->PtrToGatewayFnPtr = PtrToGatewayFnPtr;
}
void Hook::Enable()
{
memcpy(originalBytes, src, len);
*(uintptr_t*)PtrToGatewayFnPtr = (uintptr_t)TrampHook32(src, dst, len);
bStatus = true;
}
void Hook::Disable()
{
mem::Patch(src, originalBytes, len);
bStatus = false;
}
void Hook::Toggle()
{
if (!bStatus) Enable();
else Disable();
}