-
Notifications
You must be signed in to change notification settings - Fork 20
/
librariesManager.cpp
317 lines (257 loc) · 6.7 KB
/
librariesManager.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
#include <librariesManager.h>
#include <sm_stringhashmap.h>
#if defined __linux__
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <link.h>
#include <limits.h>
#include <errno.h>
#ifndef uint32
#define uint32 unsigned int
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef PAGESIZE
#define PAGESIZE sysconf(_SC_PAGESIZE)
#endif
typedef int BOOL;
#else
#include <windows.h>
#include <psapi.h>
#endif
namespace LibrariesManager
{
StringHashMap<LibraryInfo*>* LibraryNameToLibraryInfo = new StringHashMap < LibraryInfo* > ;
bool hasLibrary(const char *libraryName)
{
return LibraryNameToLibraryInfo->retrieve(libraryName);
}
#if defined __linux__
char* BaseAddress;
char* EndAddress;
char* LibraryName;
inline uint32 IAlign(uint32 address)
{
return (address & ~(PAGESIZE - 1));
}
inline uint32 IAlign2(uint32 address)
{
return (IAlign(address) + PAGESIZE);
}
static int dl_callback(struct dl_phdr_info *info, size_t size, void *data)
{
char* libraryName = (char*)data;
if ((!libraryName) || strstr(info->dlpi_name, libraryName) > 0)
{
int i;
BOOL ismain = FALSE;
if (info->dlpi_addr == 0x00)
ismain = TRUE;
else
BaseAddress = (char *)info->dlpi_addr;
for (i = 0; i < info->dlpi_phnum; ++i)
{
if (info->dlpi_phdr[i].p_memsz && IAlign(info->dlpi_phdr[i].p_vaddr))
{
if (ismain && (uint32)BaseAddress > IAlign(info->dlpi_phdr[i].p_vaddr))
BaseAddress = (char*)IAlign(info->dlpi_phdr[i].p_vaddr);
if ((uint32)EndAddress < (info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_memsz))
EndAddress = (char*)IAlign2((info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_memsz));
}
}
EndAddress += info->dlpi_addr;
return (int)BaseAddress;
}
return 0;
}
bool addLibrary(const char* libraryName, void* addressContained)
{
Dl_info info;
if (addressContained && dladdr(addressContained, &info))
{
LibraryName = (char*)info.dli_fname;
BaseAddress = (char *)0xffffffff;
EndAddress = 0;
if (dl_iterate_phdr(dl_callback, LibraryName))
{
LibraryInfo* libraryInfo = new LibraryInfo;
libraryInfo->baseAddress = (void*)BaseAddress;
libraryInfo->length = (long)EndAddress - (long)BaseAddress;
libraryInfo->handle = dlopen(LibraryName, RTLD_NOW);
LibraryNameToLibraryInfo->insert(libraryName, libraryInfo);
return true;
}
}
return false;
}
#else
bool addLibrary(const char* libraryName, void* addressContained)
{
HMODULE module;
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)addressContained, &module))
{
HANDLE process = GetCurrentProcess();
_MODULEINFO moduleInfo;
if (GetModuleInformation(process, module, &moduleInfo, sizeof moduleInfo))
{
CloseHandle(process);
LibraryInfo* libraryInfo = new LibraryInfo;
libraryInfo->baseAddress = (void*)moduleInfo.lpBaseOfDll;
libraryInfo->length = moduleInfo.SizeOfImage;
libraryInfo->handle = module;
LibraryNameToLibraryInfo->insert(libraryName, libraryInfo);
return true;
}
}
return false;
}
#endif
void* findFunction(const char* libraryName, const char* functionName)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
#if defined __linux__
return dlsym(libraryInfo->handle, functionName);
#else
return GetProcAddress((HMODULE)libraryInfo->handle, functionName);
#endif
}
return NULL;
}
bool compareSignature(unsigned char* address, unsigned char* signature, SignatureEntryType* signatureData, unsigned int length)
{
if (length == 1)
{
switch (*signatureData)
{
case AnyByteOrNothing:
case AnyByte:
{
return true;
}
case SpecificByte:
{
return *address == *signature;
}
}
}
else
{
switch (*signatureData)
{
case SpecificByte:
{
if (*address != *signature)
{
return false;
}
else
{
return compareSignature(address + 1, signature + 1, signatureData + 1, length - 1);
}
}
case AnyByteOrNothing:
{
if (compareSignature(address, signature + 1, signatureData + 1, length - 1))
{
return true;
}
}
case AnyByte:
{
return compareSignature(address + 1, signature + 1, signatureData + 1, length - 1);
}
}
}
return true;
}
void* findFunction(const char* libraryName, unsigned char* signature, SignatureEntryType* signatureData, unsigned int length)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
for (size_t i = 0; i <= libraryInfo->length - length; ++i)
{
if (compareSignature((unsigned char *)libraryInfo->baseAddress + i, signature, signatureData, length))
{
return (void*)(((unsigned char*)libraryInfo->baseAddress) + i);
}
}
}
return NULL;
}
void* findMemory(const char* libraryName, unsigned char* signature, SignatureEntryType* signatureData, unsigned int length, long start)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
for (unsigned int i = start - (unsigned int)libraryInfo->baseAddress; i <= libraryInfo->length - length; ++i)
{
if (compareSignature((unsigned char *)libraryInfo->baseAddress + i, signature, signatureData, length))
{
return (void*)(((unsigned char*)libraryInfo->baseAddress) + i);
}
}
}
return NULL;
}
bool libraryContainsAddress(const char* libraryName, long address)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
return (address >= (long)libraryInfo->baseAddress) && (address <= ((long)libraryInfo->baseAddress + libraryInfo->length));
}
return false;
}
long getAddressOffset(long address, const char* libraryName)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
return address - (long)libraryInfo->baseAddress;
}
return 0;
}
long getAddressWithOffset(long offset, const char* libraryName)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
if ((offset >= 0) && (offset <= (libraryInfo->length)))
{
return (long)libraryInfo->baseAddress + offset;
}
}
return 0;
}
long getLibraryAddress(const char* libraryName)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
return (long)libraryInfo->baseAddress;
}
return 0;
}
LibraryInfo* getLibrary(const char* libraryName)
{
LibraryInfo* libraryInfo;
if (LibraryNameToLibraryInfo->retrieve(libraryName, &libraryInfo))
{
return libraryInfo;
}
return NULL;
}
}