-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetour.cpp
157 lines (118 loc) · 3.01 KB
/
Detour.cpp
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
#pragma once
#include "Detour.h"
#ifdef _WIN64
#define DET_MIN_SIZE 0x0C
#else
#define DET_MIN_SIZE 5
#endif
Detour::Detour()
{
m_pTarget = nullptr;
m_pHook = nullptr;
m_pTrp = nullptr;
m_Length = 0;
m_State = false;
m_Init = false;
}
Detour::~Detour()
{
Remove();
}
void* Detour::CreateDetour(void* pTarget, void* pHook, UINT Length, bool Active)
{
if (!pTarget || !pHook || Length < DET_MIN_SIZE)
return nullptr;
if (m_Init)
Remove();
m_pTrp = VirtualAlloc(nullptr, Length + DET_MIN_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!m_pTrp)
return nullptr;
m_pTarget = pTarget;
m_pHook = pHook;
m_Length = Length;
memcpy(m_pTrp, m_pTarget, m_Length);
#ifdef _WIN64
BYTE CodeCave[] =
{
0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // + 0x00 (+ 0x02) -> mov rax, pHook
0xFF, 0xE0 // + 0x0A -> jmp rax
};
*reinterpret_cast<UINT_PTR*>(CodeCave + 2) = reinterpret_cast<UINT_PTR>(m_pTarget) + Length;
#else
BYTE CodeCave[] =
{
0xE9, 0x00, 0x00, 0x00, 0x00 // + 0x00 (+ 0x01) -> jmp pHook
};
*reinterpret_cast<DWORD*>(CodeCave + 1) = reinterpret_cast<DWORD>(m_pTarget) - reinterpret_cast<DWORD>(m_pTrp) - 5;
#endif
memcpy(reinterpret_cast<BYTE*>(m_pTrp) + Length, CodeCave, sizeof(CodeCave));
m_Init = true;
if (Active)
Activate();
return m_pTrp;
}
bool Detour::Activate()
{
if (m_State)
return true;
if (!m_pTarget || !m_pHook || !m_Length || !m_Init)
return false;
if (!Hook())
return false;
m_State = true;
return true;
}
bool Detour::Deactivate()
{
if (!m_State)
return true;
if (!m_pTarget || !m_pHook || !m_Length || !m_Init)
return false;
DWORD dwOld;
if (!VirtualProtect(m_pTarget, m_Length, PAGE_EXECUTE_READWRITE, &dwOld))
return false;
memcpy(m_pTarget, m_pTrp, m_Length);
VirtualProtect(m_pTarget, m_Length, dwOld, &dwOld);
m_State = false;
return true;
}
bool Detour::Remove()
{
if (Deactivate())
{
VirtualFree(m_pTrp, 0, MEM_RELEASE);
m_pTarget = nullptr;
m_pHook = nullptr;
m_pTrp = nullptr;
m_Length = 0;
m_State = false;
m_Init = false;
return true;
}
return false;
}
bool Detour::Hook()
{
if (!m_Init)
return false;
#ifdef _WIN64
BYTE CodeCave[] =
{
0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // + 0x00 (+ 0x02) -> mov rax, pHook
0xFF, 0xE0 // + 0x0A -> jmp rax
};
*reinterpret_cast<UINT_PTR*>(CodeCave + 2) = reinterpret_cast<UINT_PTR>(m_pHook);
#else
BYTE CodeCave[] =
{
0xE9, 0x00, 0x00, 0x00, 0x00 // + 0x00 (+ 0x01) -> jmp pHook
};
*reinterpret_cast<DWORD*>(CodeCave + 1) = reinterpret_cast<DWORD>(m_pHook) - reinterpret_cast<DWORD>(m_pTarget) - 5;
#endif
DWORD dwOld = 0;
if (!VirtualProtect(m_pTarget, sizeof(CodeCave), PAGE_EXECUTE_READWRITE, &dwOld))
return false;
memcpy(m_pTarget, CodeCave, sizeof(CodeCave));
VirtualProtect(m_pTarget, sizeof(CodeCave), dwOld, &dwOld);
return true;
}