/
PointerPath.cpp
265 lines (227 loc) · 6.73 KB
/
PointerPath.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
#include "PointerPath.h"
#include <tchar.h>
#include "Log.h"
#ifdef _WIN64
#define LDT_ENTRY32 WOW64_LDT_ENTRY
#define GetThreadSelectorEntry32 Wow64GetThreadSelectorEntry
#else
#define LDT_ENTRY32 LDT_ENTRY
#define GetThreadSelectorEntry32 GetThreadSelectorEntry
#endif
PointerPath32::PointerPath32(std::vector<int> offsets) : offsets(offsets)
{
}
PointerPath32::~PointerPath32()
{
}
_Success_(return) bool PointerPath32::GetModuleInfo(DWORD pId, _In_ TCHAR * moduleName, _Out_ MODULEENTRY32 &moduleInfo)
{
bool found = false;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pId);
if (snapshot == INVALID_HANDLE_VALUE)
{
Log("ERROR | PointerPath::GetModuleInfo | Couldn't create module snapshot!");
LogLastError();
}
else
{
MODULEENTRY32 entry;
entry.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(snapshot, &entry))
{
Log("ERROR | PointerPath::GetModuleInfo | Couldn't enumerate first module!");
LogLastError();
}
else
{
do
{
if (_tcsicmp(entry.szModule, moduleName) == 0)
{
Log("INFO | PointerPath::GetModuleInfo | Found module", moduleName);
moduleInfo = entry;
found = true;
break;
}
} while (Module32Next(snapshot, &entry));
}
CloseHandle(snapshot);
}
return found;
}
DWORD PointerPath32::GetStackStart(_In_ HANDLE process, DWORD pId, _In_ HANDLE thread)
{
// address of the top of the stack
DWORD stacktopAddress = 0;
SIZE_T read = 0;
CONTEXT context;
ZeroMemory(&context, sizeof(context));
context.ContextFlags = CONTEXT_SEGMENTS;
if (GetThreadContext(thread, &context))
{
LDT_ENTRY32 ldtEntry;
if (GetThreadSelectorEntry32(thread, context.SegFs, &ldtEntry))
{
DWORD stackInfo = ldtEntry.BaseLow + (ldtEntry.HighWord.Bytes.BaseMid << 16) +
(ldtEntry.HighWord.Bytes.BaseHi << 24);
if (!ReadProcessMemory(process, reinterpret_cast<void*>(stackInfo + sizeof(DWORD)), &stacktopAddress,
sizeof(DWORD), &read))
{
Log("ERROR | PointerPath::GetStackStart | Couldn't read address of the top of the stack!");
LogLastError();
return 0;
}
else if (read != sizeof(DWORD))
{
Log("WARNING | PointerPath::GetStackStart | Didn't finish while reading address of the top of the stack!");
}
}
else
{
Log("ERROR | PointerPath::GetStackStart | Could not get descriptor table entry!");
LogLastError();
return 0;
}
}
else
{
Log("ERROR | PointerPath::GetStackStart | Could not get thread context!");
LogLastError();
return 0;
}
// with stacktopAddress, we know where the stack ends. Now search the last SEARCH_SIZE bytes of the
// stack for a pointer, that points to the function that calls "ExitXXXXXThread"
// According to CheatEngine, it's the first entry that points to an address in kernel32
// read last SEARCH_SIZE bytes of the stack
unsigned int const SEARCH_SIZE = 4096;
unsigned int const LENGTH = SEARCH_SIZE / sizeof(DWORD);
DWORD stack[LENGTH];
DWORD firstAddress = stacktopAddress - SEARCH_SIZE;
if (!ReadProcessMemory(process, reinterpret_cast<void *>(firstAddress), stack, SEARCH_SIZE, &read))
{
Log("ERROR | PointerPath::GetStackStart | Couldn't read part of the stack!");
LogLastError();
return 0;
}
else if (read != SEARCH_SIZE)
{
Log("WARNING | PointerPath::GetStackStart | Didn't finish while reading part of the stack");
}
// get start and end address of kernel32 module
MODULEENTRY32 moduleInfo;
if (!PointerPath32::GetModuleInfo(pId, TEXT("kernel32.dll"), moduleInfo))
{
Log("ERROR | PointerPath::GetStackStart | Couldn't get kernel32.dll module info!");
return 0;
}
UINT_PTR kernel32Start = reinterpret_cast<UINT_PTR>(moduleInfo.hModule);
UINT_PTR kernel32End = kernel32Start + moduleInfo.modBaseSize;
for (int i = LENGTH - 1; i >= 0; --i)
{
if (stack[i] >= kernel32Start && stack[i] < kernel32End)
{
// Hoorayyy, finally found it!
DWORD stackStart = firstAddress + i * sizeof(DWORD);
Log("INFO | PointerPath::GetStackStart | Found stack start: ", stackStart, true);
return stackStart;
}
}
Log("WARNING | PointerPath::GetStackStart | Couldn't find stack start!");
return 0;
}
DWORD PointerPath32::GetThreadAddress(_In_ HANDLE process, DWORD pId, unsigned int threadId)
{
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snapshot == INVALID_HANDLE_VALUE)
{
Log("ERROR | PointerPath::GetThreadAddress | Couldn't create thread snapshot!");
LogLastError();
return 0;
}
DWORD address = 0;
THREADENTRY32 entry;
entry.dwSize = sizeof(THREADENTRY32);
if (Thread32First(snapshot, &entry))
{
unsigned int currentId = 0;
do
{
if (entry.th32OwnerProcessID == pId)
{
if (currentId == threadId)
{
HANDLE thread = OpenThread(THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT, FALSE, entry.th32ThreadID);
if (thread == nullptr)
{
Log("ERROR | PointerPath::GetThreadAddress | Couldn't open thread with ID ", entry.th32ThreadID);
LogLastError();
}
else
{
address = GetStackStart(process, pId, thread);
CloseHandle(thread);
}
break;
}
currentId += 1;
}
} while (Thread32Next(snapshot, &entry));
}
else
{
Log("ERROR | PointerPath::GetThreadAddress | Couldn't enumerate first thread!");
LogLastError();
}
CloseHandle(snapshot);
return address;
}
_Success_(return) bool PointerPath32::Deref(_In_ HANDLE process, DWORD base, _Out_ long &value)
{
DWORD pointer = DerefOffsets(process, base);
SIZE_T read = 0;
if (!ReadProcessMemory(process, reinterpret_cast<void *>(pointer), &value, sizeof(value), &read))
{
return false;
}
else if (read != sizeof(value))
{
Log("WARNING | PointerPath::Deref | Didn't finish while reading int value at", pointer, true);
}
return true;
}
std::string PointerPath32::Deref(_In_ HANDLE process, DWORD base, unsigned int const LENGTH)
{
DWORD pointer = DerefOffsets(process, base);
SIZE_T read = 0;
char * buffer = new char[LENGTH + 1];
if (!ReadProcessMemory(process, reinterpret_cast<void *>(pointer), buffer, LENGTH, &read))
{
buffer[0] = '\0';
}
else if (read != LENGTH)
{
Log("WARNING | PointerPath::Deref | Didn't finish while reading string at ", pointer, true);
}
// make sure the string terminates
buffer[LENGTH] = '\0';
std::string str = std::string(buffer);
delete[] buffer;
return str;
}
DWORD PointerPath32::DerefOffsets(_In_ HANDLE process, DWORD base)
{
DWORD pointer = base;
SIZE_T read = 0;
for (auto it = offsets.begin(); it != offsets.end() - 1; it += 1)
{
if (!ReadProcessMemory(process, reinterpret_cast<void *>(pointer + *it), &pointer, sizeof(DWORD), &read))
{
return 0;
}
else if (read != sizeof(DWORD))
{
Log("WARNING | PointerPath::DerefOffsets | Didn't finish while derefing ", pointer + *it, true);
}
}
return pointer + offsets[offsets.size() - 1];
}