-
Notifications
You must be signed in to change notification settings - Fork 2
/
Injector.h
225 lines (180 loc) · 5.18 KB
/
Injector.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
//
// Header with helpful stuff for ASI memory hacking
//
// by LINK/2012 (http://www.link2012world.blogspot.com/)
// Licensed under WTFPLv2 (http://www.wtfpl.net/txt/copying/)
//
*/
#pragma once
#include <Windows.h>
#include <type_traits>
#include <cstdint>
#include <cassert>
#ifdef _DEBUG
#include <cstdio>
#include <cstdarg>
#endif
//
// ASM EPILOG and ASM PROLOG macros
//
#define ASM_PROLOG() _asm \
{ \
_asm push ebp \
_asm mov ebp, esp \
_asm sub esp, __LOCAL_SIZE \
}
#define ASM_EPILOG() _asm \
{ \
_asm mov esp, ebp \
_asm pop ebp \
}
//
//
//
union memory_pointer
{
void* p;
uintptr_t a;
memory_pointer() { p = nullptr; }
memory_pointer(void* x) : p(x) {}
memory_pointer(uint32_t x) : a(x) {}
operator void*() { return p; }
operator uintptr_t() { return a; }
memory_pointer& operator=(void* x) { return p = x, *this; }
memory_pointer& operator=(uintptr_t x) { return a = x, *this; }
};
union memory_pointer_a // used to hack the compiler, don't use for general purposes
{
void* p;
uintptr_t a;
memory_pointer_a() { p = nullptr; }
memory_pointer_a(void* x) : p(x) {}
memory_pointer_a(uint32_t x) : a(x) {}
template<class T>
operator T*() { return reinterpret_cast<T*>(p); }
};
//
//
//
inline bool ProtectMemory(memory_pointer Address, size_t size, DWORD Protect)
{
return VirtualProtect(Address, size, Protect, &Protect) != 0;
}
inline bool UnprotectMemory(memory_pointer Address, size_t size, DWORD& Out_OldProtect)
{
return VirtualProtect(Address, size, PAGE_EXECUTE_READWRITE, &Out_OldProtect) != 0;
}
inline void WriteMemoryRaw(memory_pointer Address, void* value, size_t size, bool vp)
{
DWORD oldProtect;
if(vp) UnprotectMemory(Address, size, oldProtect);
memcpy(Address, value, size);
if(vp) ProtectMemory(Address, size, oldProtect);
}
inline void ReadMemoryRaw(memory_pointer Address, void* ret, size_t size, bool vp)
{
DWORD oldProtect;
if(vp) UnprotectMemory(Address, size, oldProtect);
if(size == 1) *(uint8_t*)ret = *(uint8_t*)Address.p;
else if(size == 2) *(uint16_t*)ret = *(uint16_t*)Address.p;
else if(size == 4) *(uint32_t*)ret = *(uint32_t*)Address.p;
else memcpy(ret, Address, size);
if(vp) ProtectMemory(Address, size, oldProtect);
}
template<class T> inline
void WriteMemoryTo(memory_pointer Address, const T& value, bool vp = false)
{
DWORD oldProtect;
if(vp) UnprotectMemory(Address, sizeof(T), oldProtect);
*(T*)Address.p = value;
if(vp) VirtualProtect(Address, sizeof(T), oldProtect, &oldProtect);
}
template<class T> inline
T& ReadMemoryTo(memory_pointer Address, T& value, bool vp = false)
{
DWORD oldProtect;
if(vp) UnprotectMemory(Address, sizeof(T), oldProtect);
value = *(T*)Address.p;
if(vp) VirtualProtect(Address, sizeof(T), oldProtect, &oldProtect);
return value;
}
template<class T> inline
void WriteMemory(memory_pointer Address, T value, bool vp = false)
{
WriteMemoryTo(Address, value, vp);
}
template<class T> inline
T ReadMemory(memory_pointer Address, bool vp = false)
{
T value;
return ReadMemoryTo(Address, value, vp), value;
}
//
//
//
inline memory_pointer GetAbsoluteOffset(int rel_value, memory_pointer end_of_instruction)
{
return end_of_instruction + rel_value;
}
inline long GetRelativeOffset(int abs_value, memory_pointer end_of_instruction)
{
return abs_value - end_of_instruction;
}
inline memory_pointer ReadRelativeOffset(memory_pointer at, size_t sizeof_addr = 4)
{
switch(sizeof_addr)
{
case 1: return (GetAbsoluteOffset(ReadMemory<int8_t>(at, true), at+sizeof_addr));
case 2: return (GetAbsoluteOffset(ReadMemory<int16_t>(at, true), at+sizeof_addr));
case 4: return (GetAbsoluteOffset(ReadMemory<int32_t>(at, true), at+sizeof_addr));
}
return nullptr;
}
inline void MakeRelativeOffset(memory_pointer at, memory_pointer dest, size_t sizeof_addr = 4)
{
switch(sizeof_addr)
{
case 1: WriteMemory<int8_t>(at, static_cast<int8_t>(GetRelativeOffset(dest, at+sizeof_addr)), true);
case 2: WriteMemory<int16_t>(at, static_cast<int16_t>(GetRelativeOffset(dest, at+sizeof_addr)), true);
case 4: WriteMemory<int32_t>(at, static_cast<int32_t>(GetRelativeOffset(dest, at+sizeof_addr)), true);
}
}
inline memory_pointer GetAbsoluteOffsetInOpcode(memory_pointer at)
{
switch(ReadMemory<uint8_t>(at, true))
{
// TODO: Handle other instructions
// TODO: Handle prefix
case 0xE8: // call rel
case 0xE9: // jmp rel
return ReadRelativeOffset(at+1, 4);
}
return nullptr;
}
inline memory_pointer MakeJMP(memory_pointer at, memory_pointer dest)
{
memory_pointer p = GetAbsoluteOffsetInOpcode(at);
WriteMemory<uint8_t>(at, 0xE9, true);
MakeRelativeOffset(at+1, dest);
return p;
}
inline memory_pointer MakeCALL(memory_pointer at, memory_pointer dest)
{
memory_pointer p = GetAbsoluteOffsetInOpcode(at);
WriteMemory<uint8_t>(at, 0xE8, true);
MakeRelativeOffset(at+1, dest);
return p;
}
inline void MakeJA(memory_pointer at, memory_pointer dest)
{
WriteMemory<uint16_t>(at, 0x87F0, true);
MakeRelativeOffset(at+2, dest);
}
inline void MakeNOP(memory_pointer at, size_t size)
{
DWORD oldProtect;
UnprotectMemory(at, size, oldProtect);
memset(at.p, 0x90, size);
VirtualProtect(at, size, oldProtect, &oldProtect);
}