-
Notifications
You must be signed in to change notification settings - Fork 2
/
memytools.cpp
384 lines (325 loc) · 10.4 KB
/
memytools.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// obfuscate strings in this lib?
// requires https://github.com/adamyaxley/Obfuscate
// and >= c++ 14
// #include <util/obfuscate.h>
#include "memytools.h"
// insert your defines here
#if defined (OF_CLIENT_DLL) || defined (TF_CLASSIC_CLIENT)
#define cli yep
#elif defined (OF_DLL) || defined (TF_CLASSIC)
#define srv yep
#endif
#ifndef AY_OBFUSCATE
#define AY_OBFUSCATE
#endif
// #define memydbg yep
memy_init _memy_init;
memy_init::memy_init() : CAutoGameSystem("")
{
}
// shared between client and server
modbin* engine_bin = new modbin();
modbin* server_bin = new modbin();
modbin* tier0_bin = new modbin();
// client only
#ifdef cli
modbin* vgui_bin = new modbin();
modbin* client_bin = new modbin();
modbin* gameui_bin = new modbin();
#endif
char bins_list[][MAX_PATH] =
{
{}, // engine
{}, // server
{}, // tier0
// client
#if defined (cli)
{}, // vgui
{}, // client
{}, // gameui
#endif
};
modbin* modbins_list[]
{
engine_bin,
server_bin,
tier0_bin,
// client
#if defined (cli)
vgui_bin,
client_bin,
gameui_bin,
#endif
};
memy _memy;
memy::memy()
{
V_strncpy(bins_list[0], AY_OBFUSCATE("engine"), 32);
V_strncpy(bins_list[1], AY_OBFUSCATE("server"), 32);
V_strncpy(bins_list[2], AY_OBFUSCATE("tier0"), 32);
// client only
#if defined (cli)
V_strncpy(bins_list[3], AY_OBFUSCATE("vguimatsurface"), 32);
V_strncpy(bins_list[4], AY_OBFUSCATE("client"), 32);
V_strncpy(bins_list[5], AY_OBFUSCATE("GameUI"), 32);
#endif
}
bool memy_init::Init()
{
// memy();
memy::InitAllBins();
return true;
}
bool memy::InitAllBins()
{
// memy();
size_t sbin_size = sizeof(bins_list) / sizeof(bins_list[0]);
// loop thru our bins
for (size_t ibin = 0; ibin < sbin_size; ibin++)
{
if (!InitSingleBin(bins_list[ibin], modbins_list[ibin]))
{
Error("[MEMY] Couldn't init %s!", bins_list[ibin]);
}
}
return true;
}
bool memy::InitSingleBin(const char* binname, modbin* mbin)
{
// binname + .dll
char realbinname[256] = {};
#ifdef _WIN32
V_snprintf(realbinname, sizeof(realbinname), "%s.dll", binname);
HMODULE mhandle;
mhandle = GetModuleHandleA(realbinname);
if (!mhandle)
{
Error("[MEMY] Couldn't init %s!\n", realbinname);
return false;
}
MODULEINFO minfo;
GetModuleInformation(GetCurrentProcess(), mhandle, &minfo, sizeof(minfo));
mbin->addr = reinterpret_cast<uintptr_t>(minfo.lpBaseOfDll);
mbin->size = minfo.SizeOfImage;
if (!mbin->addr || !mbin->size)
{
Error("[MEMY] Couldn't init %s; addr = %x, size = %i!\n", realbinname, mbin->addr, mbin->size);
return false;
}
#ifdef memydbg
Warning("memy::InitSingleBin -> name %s, mbase %x, msize %i\n", realbinname, mbin->addr, mbin->size);
#endif
#else
// binname + .so
// funny special cases
if (strcmp(binname, "engine") == 0)
{
// client only
#if defined (cli)
V_snprintf(realbinname, sizeof(realbinname), "%s.so", binname);
// server only
#else
if (engine->IsDedicatedServer())
{
V_snprintf(realbinname, sizeof(realbinname), "%s_srv.so", binname);
}
else
{
V_snprintf(realbinname, sizeof(realbinname), "%s.so", binname);
}
#endif
}
// linux loads libtier0.so and libtier0_srv.so, and they are different. Yay!
else if (strcmp(binname, "tier0") == 0)
{
// client only
#if defined (cli)
V_snprintf(realbinname, sizeof(realbinname), "lib%s.so", binname);
// server only
#else
if (engine->IsDedicatedServer())
{
V_snprintf(realbinname, sizeof(realbinname), "lib%s_srv.so", binname);
}
else
{
V_snprintf(realbinname, sizeof(realbinname), "lib%s.so", binname);
}
#endif
}
else
{
V_snprintf(realbinname, sizeof(realbinname), "%s.so", binname);
}
void* mbase = nullptr;
size_t msize = 0;
if (GetModuleInformation(realbinname, &mbase, &msize))
{
Error("memy::InitSingleBin -> GetModuleInformation failed for %s!\n", realbinname);
return false;
}
mbin->addr = reinterpret_cast<uintptr_t>(mbase);
mbin->size = msize;
#ifdef memydbg
Warning("memy::InitSingleBin -> name %s, mbase %x, msize %i\n", realbinname, mbin->addr, mbin->size);
#endif
#endif
return true;
}
inline bool memy::comparedata(const byte* data, const char* pattern, size_t sigsize)
{
if (!data || !pattern || !sigsize)
{
#ifdef memydbg
Warning("memy::DataCompare -> Couldn't grab data %p, pattern %p, nor patternsize %i\n", data, pattern, sigsize);
#endif
return false;
}
for
(
size_t head = 0;
head < sigsize; // sigsize doesn't start from 0 so we don't need to <=
(head++, pattern++, data++)
)
{
// char at this pos in our pattern
byte pattern_byte = *(pattern);
#ifdef memydbg
if (head >= sigsize - 6)
{
Warning("memy::DataCompare -> head = %i; char = %.2x\n", head, pattern_byte);
}
#endif
// if it's a wildcard just skip it
if ( pattern_byte == '\x2A' )
{
continue;
}
// char at this pos in our memory
byte data_byte = *(data);
// if it doesn't match it's bunk; go to the next byte
if ( pattern_byte != data_byte )
{
return false;
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------
// Finds a pattern of bytes in the engine memory given a signature and a mask
// Returns the address of the first (and hopefully only) match with an optional offset, otherwise nullptr
//---------------------------------------------------------------------------------------------------------
uintptr_t memy::FindPattern(uintptr_t startaddr, size_t searchsize, const char* pattern, size_t sigsize, size_t offset)
{
#ifdef memydbg
char hexstr[128] = {};
V_binarytohex
(
reinterpret_cast<const byte*>(pattern),
(sigsize * 2) + 1, // sigsize -> bytes + nullterm
hexstr,
(sigsize * 2) + 1
);
#endif
if (!startaddr || !searchsize || !pattern)
{
#ifdef memydbg
Warning("memy::FindPattern -> Couldn't grab modbase %x, modsize %i, or pattern %p = %s\n", startaddr, searchsize, pattern, hexstr);
#endif
return NULL;
}
// iterate thru memory, starting at modbase + i, up to (modsize + searchsize) - sigsize
for (size_t i = 0; i <= (searchsize - sigsize); i++)
{
byte* addr = reinterpret_cast<byte*>(startaddr) + i;
if (comparedata(addr, pattern , sigsize))
{
#ifdef memydbg
Warning("memy::FindPattern -> found pattern %s, %i, %i!\n", hexstr, sigsize, offset);
#endif
return reinterpret_cast<uintptr_t>(addr + offset);
}
}
#ifdef memydbg
Warning("memy::FindPattern -> Failed, pattern %s, %i, %i!\n", hexstr, sigsize, offset);
#endif
return NULL;
}
bool memy::SetMemoryProtection(void* addr, size_t protlen, int wantprot)
{
#ifdef _WIN32
// VirtualProtect requires a valid pointer to store the old protection value
DWORD tmp;
DWORD prot;
switch (wantprot)
{
case (MEM_READ):
{
prot = PAGE_READONLY;
break;
}
case (MEM_READ | MEM_WRITE):
{
prot = PAGE_READWRITE;
break;
}
case (MEM_READ | MEM_EXEC):
{
prot = PAGE_EXECUTE_READ;
break;
}
case (MEM_READ | MEM_WRITE | MEM_EXEC):
default:
{
prot = PAGE_EXECUTE_READWRITE;
break;
}
}
// BOOL is typedef'd as an int on Windows, sometimes (lol), bang bang it to convert it to a bool proper
return !!(VirtualProtect(addr, protlen, prot, &tmp));
#else
// POSIX
return mprotect( LALIGN(addr), protlen + LALDIF(addr), wantprot) == 0;
#endif
}
#if defined (POSIX)
//returns 0 if successful
int memy::GetModuleInformation(const char *name, void **base, size_t *length)
{
// this is the only way to do this on linux, lol
FILE *f = fopen("/proc/self/maps", "r");
if (!f)
{
Warning("memy::GetModInfo -> Couldn't get proc->self->maps\n");
return 1;
}
char buf[PATH_MAX+100];
while (!feof(f))
{
if (!fgets(buf, sizeof(buf), f))
break;
char *tmp = strrchr(buf, '\n');
if (tmp)
*tmp = '\0';
char *mapname = strchr(buf, '/');
if (!mapname)
continue;
char perm[5];
unsigned long begin, end;
sscanf(buf, "%lx-%lx %4s", &begin, &end, perm);
if (strcmp(basename(mapname), name) == 0 && perm[0] == 'r' && perm[2] == 'x')
{
#ifdef memydbg
Warning("perm = %s\n", perm);
#endif
*base = (void*)begin;
*length = (size_t)end-begin;
fclose(f);
return 0;
}
}
fclose(f);
Warning("memy::GetModInfo -> Couldn't find info for modname %s\n", name);
return 2;
}
#endif