forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_scanner.cpp
More file actions
419 lines (353 loc) · 12.2 KB
/
Copy pathmemory_scanner.cpp
File metadata and controls
419 lines (353 loc) · 12.2 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* =============================================================================
* Source Python
* Copyright (C) 2012 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <link.h>
#include <sys/mman.h>
#endif
#include "dynload.h"
#include "memory_scanner.h"
#include "memory_tools.h"
#include "utilities/sp_util.h"
#include "utilities/call_python.h"
#include "sp_main.h"
#include "eiface.h"
//-----------------------------------------------------------------------------
// Externals.
//-----------------------------------------------------------------------------
extern IVEngineServer* engine;
//-----------------------------------------------------------------------------
// BinaryFile class
//-----------------------------------------------------------------------------
CBinaryFile::CBinaryFile(unsigned long ulAddr, unsigned long ulSize)
{
m_ulAddr = ulAddr;
m_ulSize = ulSize;
}
CPointer* CBinaryFile::FindSignatureRaw(object oSignature)
{
unsigned char* sigstr = NULL;
PyArg_Parse(oSignature.ptr(), "y", &sigstr);
if (!sigstr)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to parse the signature.");
int iLength = len(oSignature);
unsigned char* base = (unsigned char *) m_ulAddr;
unsigned char* end = (unsigned char *) (base + m_ulSize - iLength);
while(base < end)
{
int i = 0;
for(; i < iLength; i++)
{
if (sigstr[i] == '\x2A')
continue;
if (sigstr[i] != base[i])
break;
}
if (i == iLength)
{
return new CPointer((unsigned long) base);
}
base++;
}
return new CPointer();
}
CPointer* CBinaryFile::FindSignature(object oSignature)
{
// This is required because there's no straight way to get a string from a python
// object from boost (without using the stl).
unsigned char* sigstr = NULL;
PyArg_Parse(oSignature.ptr(), "y", &sigstr);
if (!sigstr)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to parse the signature.");
// Search for a cached signature
PythonLog(4, "Searching for a cached signature...");
for (std::list<Signature_t>::iterator iter=m_Signatures.begin(); iter != m_Signatures.end(); iter++)
{
Signature_t sig = *iter;
if (strcmp((const char *) sig.m_szSignature, (const char *) sigstr) == 0)
{
PythonLog(4, "Found a cached signature!");
return new CPointer(sig.m_ulAddr);
}
}
PythonLog(4, "Could not find a cached signature. Searching in the binary...");
// Search for a signature in the binary
int iLength = len(oSignature);
CPointer* pPtr = FindSignatureRaw(oSignature);
// Found the signature?
if (pPtr->IsValid())
{
PythonLog(4, "Found a signature in the binary!");
// Add the signature to the cache
Signature_t sig_t = {new unsigned char[iLength+1], pPtr->m_ulAddr};
strcpy((char*) sig_t.m_szSignature, (char*) sigstr);
m_Signatures.push_back(sig_t);
// Return the result
return pPtr;
}
delete pPtr;
// Haven't found a match? Then seach for a hooked signature
PythonLog(4, "Could not find the signature in the binary. Searching for a hooked signature...");
// Check length of signature
if (iLength <= 6)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Signature is too short to search for a hooked signature.");
// Create the hooked signature
oSignature = import("binascii").attr("unhexlify")("E92A2A2A2A") + oSignature.slice(5, _);
// Try to find the hooked signature
pPtr = FindSignatureRaw(oSignature);
// Couldn't find it?
if (!pPtr->IsValid())
{
PythonLog(4, "Could not find a hooked signature.");
delete pPtr;
}
// Yay, we found a match! Now, check if the signature is unique
else
{
PythonLog(4, "Found a hooked signature! Checking if it's unique...");
// Add iLength, so we start searching after the match
CPointer new_ptr = CPointer(pPtr->m_ulAddr + iLength);
// Got another match after the first one?
CPointer* pNext = new_ptr.SearchBytes(oSignature, (m_ulAddr + m_ulSize) - new_ptr.m_ulAddr);
bool bIsValid = pNext->IsValid();
delete pNext;
if (bIsValid)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Found more than one hooked signatures. Please pass more bytes.");
PythonLog(4, "Signature is unique!");
// It's unique! So, add the original signature to the cache
Signature_t sig_t = {new unsigned char[iLength+1], pPtr->m_ulAddr};
strcpy((char*) sig_t.m_szSignature, (char*) sigstr);
m_Signatures.push_back(sig_t);
// Now, return the result
return pPtr;
}
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Could not find signature.");
return new CPointer(); // To fix a warning. This will never get called.
}
CPointer* CBinaryFile::FindSymbol(char* szSymbol)
{
#ifdef _WIN32
return new CPointer((unsigned long) GetProcAddress((HMODULE) m_ulAddr, szSymbol));
#elif defined(__linux__)
// -----------------------------------------
// We need to use mmap now that VALVe has
// made them all private!
// Thank you to DamagedSoul from AlliedMods
// for the following code.
// It can be found at:
// http://hg.alliedmods.net/sourcemod-central/file/dc361050274d/core/logic/MemoryUtils.cpp
// -----------------------------------------
struct link_map *dlmap;
struct stat dlstat;
int dlfile;
uintptr_t map_base;
Elf32_Ehdr *file_hdr;
Elf32_Shdr *sections, *shstrtab_hdr, *symtab_hdr, *strtab_hdr;
Elf32_Sym *symtab;
const char *shstrtab, *strtab;
uint16_t section_count;
uint32_t symbol_count;
dlmap = (struct link_map *) m_ulAddr;
symtab_hdr = NULL;
strtab_hdr = NULL;
dlfile = open(dlmap->l_name, O_RDONLY);
if (dlfile == -1 || fstat(dlfile, &dlstat) == -1)
{
close(dlfile);
return new CPointer();
}
/* Map library file into memory */
file_hdr = (Elf32_Ehdr *)mmap(NULL, dlstat.st_size, PROT_READ, MAP_PRIVATE, dlfile, 0);
map_base = (uintptr_t)file_hdr;
if (file_hdr == MAP_FAILED)
{
close(dlfile);
return new CPointer();
}
close(dlfile);
if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF)
{
munmap(file_hdr, dlstat.st_size);
return new CPointer();
}
sections = (Elf32_Shdr *)(map_base + file_hdr->e_shoff);
section_count = file_hdr->e_shnum;
/* Get ELF section header string table */
shstrtab_hdr = §ions[file_hdr->e_shstrndx];
shstrtab = (const char *)(map_base + shstrtab_hdr->sh_offset);
/* Iterate sections while looking for ELF symbol table and string table */
for (uint16_t i = 0; i < section_count; i++)
{
Elf32_Shdr &hdr = sections[i];
const char *section_name = shstrtab + hdr.sh_name;
if (strcmp(section_name, ".symtab") == 0)
symtab_hdr = &hdr;
else if (strcmp(section_name, ".strtab") == 0)
strtab_hdr = &hdr;
}
/* Uh oh, we don't have a symbol table or a string table */
if (symtab_hdr == NULL || strtab_hdr == NULL)
{
munmap(file_hdr, dlstat.st_size);
return new CPointer();
}
symtab = (Elf32_Sym *)(map_base + symtab_hdr->sh_offset);
strtab = (const char *)(map_base + strtab_hdr->sh_offset);
symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
void* sym_addr = NULL;
/* Iterate symbol table starting from the position we were at last time */
for (uint32_t i = 0; i < symbol_count; i++)
{
Elf32_Sym &sym = symtab[i];
unsigned char sym_type = ELF32_ST_TYPE(sym.st_info);
const char *sym_name = strtab + sym.st_name;
/* Skip symbols that are undefined or do not refer to functions or objects */
if (sym.st_shndx == SHN_UNDEF || (sym_type != STT_FUNC && sym_type != STT_OBJECT))
continue;
if (strcmp(szSymbol, sym_name) == 0)
{
sym_addr = (void *)(dlmap->l_addr + sym.st_value);
break;
}
}
// Unmap the file now.
munmap(file_hdr, dlstat.st_size);
return new CPointer((unsigned long) sym_addr);
#else
#error "BinaryFile::FindSymbol() is not implemented on this OS"
#endif
}
CPointer* CBinaryFile::FindPointer(object oIdentifier, int iOffset, unsigned int iLevel)
{
CPointer* ptr = FindAddress(oIdentifier);
if (ptr->IsValid())
{
ptr->m_ulAddr += iOffset;
while (iLevel > 0)
{
ptr = ptr->GetPtr();
iLevel = iLevel - 1;
}
}
return ptr;
}
CPointer* CBinaryFile::FindAddress(object oIdentifier)
{
#ifdef _WIN32
if(CheckClassname(oIdentifier, "bytes"))
return FindSignature(oIdentifier);
#endif
return FindSymbol(extract<char*>(oIdentifier));
}
//-----------------------------------------------------------------------------
// CBinaryManager class
//-----------------------------------------------------------------------------
// Small helper function
bool str_ends_with(const char *szString, const char *szSuffix)
{
int stringlen = strlen(szString);
int suffixlen = strlen(szSuffix);
if (suffixlen > stringlen)
return false;
return strncmp(szString + stringlen - suffixlen, szSuffix, suffixlen) == 0;
}
CBinaryFile* CBinaryManager::FindBinary(char* szPath, bool bSrvCheck /* = true */)
{
std::string szBinaryPath = szPath;
#ifdef __linux__
if (bSrvCheck && !str_ends_with(szBinaryPath.data(), "_srv") && !str_ends_with(szBinaryPath.data(), ".so"))
szBinaryPath += "_srv.so";
else if (!str_ends_with(szBinaryPath.data(), ".so"))
szBinaryPath += ".so";
#endif
unsigned long ulAddr = (unsigned long) dlLoadLibrary(szBinaryPath.data());
#ifdef __linux__
if (!ulAddr)
{
char szGameDir[MAX_GAME_PATH];
engine->GetGameDir(szGameDir, MAX_GAME_PATH);
// If the previous path failed, try the "bin" folder of the game.
// This will allow passing e.g. "server" to this function.
szBinaryPath = std::string(szGameDir) + "/bin/" + szBinaryPath;
ulAddr = (unsigned long) dlLoadLibrary(szBinaryPath.data());
}
#endif
if (!ulAddr)
{
szBinaryPath = "Unable to find " + szBinaryPath;
#ifdef _WIN32
if (!str_ends_with(szBinaryPath.data(), ".dll"))
szBinaryPath += ".dll";
#endif
BOOST_RAISE_EXCEPTION(PyExc_IOError, szBinaryPath.data())
}
// Search for an existing BinaryFile object
for (std::list<CBinaryFile *>::iterator iter=m_Binaries.begin(); iter != m_Binaries.end(); iter++)
{
CBinaryFile* binary = *iter;
if (binary->m_ulAddr == ulAddr)
{
// We don't need to open it several times
dlFreeLibrary((DLLib *) ulAddr);
return binary;
}
}
unsigned long ulSize;
#ifdef _WIN32
IMAGE_DOS_HEADER* dos = (IMAGE_DOS_HEADER *) ulAddr;
IMAGE_NT_HEADERS* nt = (IMAGE_NT_HEADERS *) ((BYTE *) dos + dos->e_lfanew);
ulSize = nt->OptionalHeader.SizeOfImage;
#elif defined(__linux__)
// TODO: Retrieve whole size
struct stat buf;
if (stat(szBinaryPath.data(), &buf) == -1)
{
dlFreeLibrary((DLLib *) ulAddr);
szBinaryPath = "Unable to retrieve the binary size for " + szBinaryPath;
BOOST_RAISE_EXCEPTION(PyExc_RuntimeError, szBinaryPath.data())
}
ulSize = buf.st_size;
#else
#error "BinaryManager::FindBinary() is not implemented on this OS"
#endif
// Create a new Binary object and add it to the list
CBinaryFile* binary = new CBinaryFile(ulAddr, ulSize);
m_Binaries.push_front(binary);
return binary;
}
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck /* = true */)
{
return s_pBinaryManager->FindBinary(szPath, bSrvCheck);
}