-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.h
More file actions
321 lines (236 loc) · 6.47 KB
/
Memory.h
File metadata and controls
321 lines (236 loc) · 6.47 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef __MEMORY_H__
#define __MEMORY_H__
#pragma once
// from boost libraries
template <uint64_t FnvPrime, uint64_t OffsetBasis>
struct basic_fnv_1 {
uint64_t operator()( std::string const& text ) const {
uint64_t hash = OffsetBasis;
for ( std::string::const_iterator it = text.begin(), end = text.end(); it != end; ++it ) {
hash *= FnvPrime;
hash ^= *it;
}
return hash;
}
};
const uint64_t fnv_prime = 1099511628211u;
const uint64_t fnv_offset_basis = 14695981039346656037u;
typedef basic_fnv_1<fnv_prime, fnv_offset_basis> fnv_1;
namespace Memory {
void TransformPattern( const std::string & pattern, std::string & data, std::string & mask );
class executable_meta {
private:
uintptr_t m_begin;
uintptr_t m_end;
DWORD m_size;
public:
executable_meta()
: m_begin( 0 ), m_end( 0 ) {
}
void EnsureInit();
inline uintptr_t begin() { return m_begin; }
inline uintptr_t end() { return m_end; }
inline DWORD size() { return m_size; }
};
class pattern_match {
private:
void * m_pointer;
public:
inline pattern_match( void * pointer ) {
m_pointer = pointer;
}
template<typename T>
T * get( int offset ) {
if ( m_pointer == nullptr ) {
return nullptr;
}
char * ptr = reinterpret_cast<char*>( m_pointer );
return reinterpret_cast<T*>( ptr + offset );
}
template<typename T>
T * get() {
return get<T>( 0 );
}
};
typedef std::vector<pattern_match> matchVec;
class pattern {
private:
std::string m_bytes;
std::string m_mask;
uint64_t m_hash;
size_t m_size;
matchVec m_matches;
bool m_matched;
private:
void Initialize( const char* pattern, size_t length );
bool ConsiderMatch( uintptr_t offset );
void EnsureMatches( int maxCount );
public:
template<size_t Len>
pattern( const char( &pattern )[Len] ) {
Initialize( pattern, Len );
}
inline pattern & count( int expected ) {
if ( !m_matched ) {
EnsureMatches( expected );
}
return *this;
}
inline size_t size() {
if ( !m_matched ) {
EnsureMatches( INT_MAX );
}
return m_matches.size();
}
inline pattern_match & get( int index ) {
if ( !m_matched ) {
EnsureMatches( INT_MAX );
}
if ( m_matches.size() == 0 ) {
m_matches.push_back( pattern_match( nullptr ) );
return m_matches[0];
}
return m_matches[index];
}
public:
// define a hint
static void hint( uint64_t hash, uintptr_t address );
};
// for link /DYNAMICBASE executables
static ptrdiff_t baseAddressDifference;
// sets the base address difference based on an obtained pointer
inline void set_base(uintptr_t address)
{
#ifdef _M_IX86
uintptr_t addressDiff = (address - 0x400000);
#elif defined(_M_AMD64)
uintptr_t addressDiff = (address - 0x140000000);
#endif
// pointer-style cast to ensure unsigned overflow ends up copied directly into a signed value
baseAddressDifference = *(ptrdiff_t*)&addressDiff;
}
// gets the main base of the process
uintptr_t get_base();
DWORD get_size();
// gets the main base of the process with Offset
uintptr_t get_base_offsetted(DWORD offset);
// gets a multilayer pointer address
uintptr_t get_multilayer_pointer(uintptr_t base_address, std::vector<DWORD> offsets);
// sets the base to the process main base
inline void set_base()
{
set_base((uintptr_t)GetModuleHandle(NULL));
}
// adjusts the address passed to the base as set above
template<typename T>
inline void adjust_base(T& address)
{
*(uintptr_t*)&address += baseAddressDifference;
}
// returns the adjusted address to the stated base
template<typename T>
inline uintptr_t get_adjusted(T address)
{
return (uintptr_t)address + baseAddressDifference;
}
template<typename ValueType, typename AddressType>
inline void put(AddressType address, ValueType value)
{
adjust_base(address);
memcpy((void*)address, &value, sizeof(value));
}
template<typename ValueType, typename AddressType>
inline void putVP(AddressType address, ValueType value)
{
adjust_base(address);
DWORD oldProtect;
VirtualProtect((void*)address, sizeof(value), PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy((void*)address, &value, sizeof(value));
VirtualProtect((void*)address, sizeof(value), oldProtect, &oldProtect);
}
template<typename AddressType>
inline void nop(AddressType address, size_t length)
{
adjust_base(address);
memset((void*)address, 0x90, length);
}
template<typename AddressType>
inline void return_function(AddressType address, uint16_t stackSize = 0)
{
if (stackSize == 0)
{
put<uint8_t>(address, 0xC3);
}
else
{
put<uint8_t>(address, 0xC2);
put<uint16_t>((uintptr_t)address + 1, stackSize);
}
}
template<typename T>
inline T* getRVA(uintptr_t rva)
{
#ifdef _M_IX86
return (T*)(baseAddressDifference + 0x400000 + rva);
#elif defined(_M_AMD64)
return (T*)(0x140000000 + rva);
#endif
}
template<typename T, typename AT>
inline void jump(AT address, T func)
{
put<uint8_t>(address, 0xE9);
put<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
template<typename T, typename AT>
inline void call(AT address, T func)
{
put<uint8_t>(address, 0xE8);
put<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
template<typename T>
inline T get_call(T address)
{
intptr_t target = *(uintptr_t*)(get_adjusted(address) + 1);
target += (get_adjusted(address) + 5);
return (T)target;
}
template<typename TTarget, typename T>
inline void set_call(TTarget* target, T address)
{
*(T*)target = get_call(address);
}
namespace vp
{
template<typename T, typename AT>
inline void jump(AT address, T func)
{
putVP<uint8_t>(address, 0xE9);
putVP<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
template<typename T, typename AT>
inline void call(AT address, T func)
{
putVP<uint8_t>(address, 0xE8);
putVP<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
}
std::vector<DWORD64> get_string_addresses(std::string str);
template <typename T>
T get_value(std::vector<DWORD> offsets) {
uintptr_t Addr = get_multilayer_pointer(Hooking::getWorldPtr(), offsets);
if (Addr == NULL) {
return NULL;
}
return *((T*)Addr);
}
template <typename T>
void set_value(std::vector<DWORD> offsets, T value) {
uintptr_t Addr = get_multilayer_pointer(Hooking::getWorldPtr(), offsets);
if (Addr == NULL) {
return;
}
*reinterpret_cast<T*>(Addr) = value;
}
}
#endif // __MEMORY_H__