-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsstack_windows.c
More file actions
427 lines (392 loc) · 14.9 KB
/
sstack_windows.c
File metadata and controls
427 lines (392 loc) · 14.9 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
420
421
422
423
424
425
426
427
#include "sstack_common.h"
#include <windows.h>
#include <tlhelp32.h>
#include <dbghelp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
typedef BOOL (WINAPI *PSymInitialize)(HANDLE, PCSTR, BOOL);
typedef BOOL (WINAPI *PSymFromAddr)(HANDLE, DWORD64, PDWORD64, PSYMBOL_INFO);
typedef BOOL (WINAPI *PSymSetOptions)(DWORD);
typedef DWORD64 (WINAPI *PSymLoadModuleExW)(HANDLE, HANDLE, PCWSTR, PCWSTR, DWORD64, DWORD, PVOID, DWORD);
typedef BOOL (WINAPI *PStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64, PVOID,
PREAD_PROCESS_MEMORY_ROUTINE64,
PFUNCTION_TABLE_ACCESS_ROUTINE64,
PGET_MODULE_BASE_ROUTINE64,
PTRANSLATE_ADDRESS_ROUTINE64);
typedef BOOL (WINAPI *PIsWow64Process)(HANDLE, PBOOL);
static HMODULE load_dbghelp(void) {
return LoadLibraryA("dbghelp.dll");
}
static BOOL is_wow64_process(HANDLE process) {
HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
if (!kernel32) return FALSE;
PIsWow64Process pIsWow64Process = (PIsWow64Process)GetProcAddress(kernel32, "IsWow64Process");
if (!pIsWow64Process) return FALSE;
BOOL is_wow64 = FALSE;
pIsWow64Process(process, &is_wow64);
return is_wow64;
}
static int init_dbghelp(HMODULE dbghelp, HANDLE process,
PSymInitialize *symInitialize,
PSymFromAddr *symFromAddr,
PSymSetOptions *symSetOptions,
PStackWalk64 *stackWalk64,
PFUNCTION_TABLE_ACCESS_ROUTINE64 *functionTableAccess,
PGET_MODULE_BASE_ROUTINE64 *getModuleBase,
PSymLoadModuleExW *symLoadModuleExW) {
*symInitialize = (PSymInitialize)GetProcAddress(dbghelp, "SymInitialize");
*symFromAddr = (PSymFromAddr)GetProcAddress(dbghelp, "SymFromAddr");
*symSetOptions = (PSymSetOptions)GetProcAddress(dbghelp, "SymSetOptions");
*functionTableAccess = (PFUNCTION_TABLE_ACCESS_ROUTINE64)GetProcAddress(dbghelp, "SymFunctionTableAccess64");
*getModuleBase = (PGET_MODULE_BASE_ROUTINE64)GetProcAddress(dbghelp, "SymGetModuleBase64");
*stackWalk64 = (PStackWalk64)GetProcAddress(dbghelp, "StackWalk64");
*symLoadModuleExW = (PSymLoadModuleExW)GetProcAddress(dbghelp, "SymLoadModuleExW");
if (!*symInitialize || !*symFromAddr || !*symSetOptions || !*functionTableAccess ||
!*getModuleBase || !*stackWalk64 || !*symLoadModuleExW) {
return 0;
}
(*symSetOptions)(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS);
if (!(*symInitialize)(process, NULL, FALSE)) {
return 0;
}
return 1;
}
static void load_remote_modules(HANDLE process, DWORD pid, PSymLoadModuleExW symLoadModuleExW) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (snapshot == INVALID_HANDLE_VALUE) {
return;
}
MODULEENTRY32W me;
me.dwSize = sizeof(me);
if (Module32FirstW(snapshot, &me)) {
do {
symLoadModuleExW(process,
NULL,
me.szExePath,
NULL,
(DWORD64)(uintptr_t)me.modBaseAddr,
me.modBaseSize,
NULL,
0);
} while (Module32NextW(snapshot, &me));
}
CloseHandle(snapshot);
}
// adress & offset
static int find_module_for_address(HANDLE process, DWORD pid, DWORD64 addr, char *out_text, size_t out_len) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (snapshot == INVALID_HANDLE_VALUE) {
return 0;
}
int found = 0;
MODULEENTRY32W me;
me.dwSize = sizeof(me);
if (Module32FirstW(snapshot, &me)) {
do {
DWORD64 base = (DWORD64)(uintptr_t)me.modBaseAddr;
DWORD64 end = base + me.modBaseSize;
if (addr >= base && addr < end) {
char module_name[MAX_PATH];
wcstombs(module_name, me.szModule, sizeof(module_name) - 1);
snprintf(out_text, out_len, "%s+0x%llx", module_name, addr - base);
found = 1;
break;
}
} while (Module32NextW(snapshot, &me));
}
CloseHandle(snapshot);
return found;
}
static void write_thread_stack(FILE *out,
HANDLE process,
HANDLE thread,
DWORD thread_id,
DWORD pid,
CONTEXT *context,
PStackWalk64 stackWalk64,
PFUNCTION_TABLE_ACCESS_ROUTINE64 functionTableAccess,
PGET_MODULE_BASE_ROUTINE64 getModuleBase,
PSymFromAddr symFromAddr,
int json_output) {
STACKFRAME64 frame;
memset(&frame, 0, sizeof(frame));
#if defined(_M_IX86)
DWORD machine = IMAGE_FILE_MACHINE_I386;
frame.AddrPC.Offset = context->Eip;
frame.AddrFrame.Offset = context->Ebp;
frame.AddrStack.Offset = context->Esp;
#elif defined(_M_X64)
DWORD machine = IMAGE_FILE_MACHINE_AMD64;
frame.AddrPC.Offset = context->Rip;
frame.AddrFrame.Offset = context->Rbp;
frame.AddrStack.Offset = context->Rsp;
#elif defined(_M_ARM64)
DWORD machine = IMAGE_FILE_MACHINE_ARM64;
frame.AddrPC.Offset = context->Pc;
frame.AddrFrame.Offset = context->Fp;
frame.AddrStack.Offset = context->Sp;
#else
DWORD machine = IMAGE_FILE_MACHINE_UNKNOWN;
frame.AddrPC.Offset = context->Rip;
frame.AddrFrame.Offset = context->Rbp;
frame.AddrStack.Offset = context->Rsp;
#endif
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrStack.Mode = AddrModeFlat;
if (!json_output) {
fprintf(out, "Thread %lu stack:\n", thread_id);
}
int frame_count = 0;
for (int frame_number = 0; frame_number < 128; ++frame_number) {
if (!stackWalk64(machine,
process,
thread,
&frame,
context,
(PREAD_PROCESS_MEMORY_ROUTINE64)ReadProcessMemory,
functionTableAccess,
getModuleBase,
NULL)) {
break;
}
if (frame.AddrPC.Offset == 0) {
break;
}
DWORD64 displacement = 0;
char symbol_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(char)];
PSYMBOL_INFO symbol = (PSYMBOL_INFO)symbol_buffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
if (json_output) {
if (frame_number > 0) fprintf(out, ",");
fprintf(out, "\n {\"address\": \"0x%016llX\"",
(unsigned long long)frame.AddrPC.Offset);
if (symFromAddr(process, frame.AddrPC.Offset, &displacement, symbol)) {
fprintf(out, ", \"symbol\": \"%s\", \"displacement\": %llu",
symbol->Name,
(unsigned long long)displacement);
} else {
// what fucking module does this address belong to
char module_info[256];
if (find_module_for_address(process, pid, frame.AddrPC.Offset, module_info, sizeof(module_info))) {
fprintf(out, ", \"symbol\": \"%s\"", module_info);
}
}
fprintf(out, "}");
} else {
if (symFromAddr(process, frame.AddrPC.Offset, &displacement, symbol)) {
fprintf(out, " #%02d 0x%016llX %s + 0x%llX\n",
frame_number,
(unsigned long long)frame.AddrPC.Offset,
symbol->Name,
(unsigned long long)displacement);
} else {
// what fucking module does this address belong to 2
char module_info[256];
if (find_module_for_address(process, pid, frame.AddrPC.Offset, module_info, sizeof(module_info))) {
fprintf(out, " #%02d 0x%016llX %s\n",
frame_number,
(unsigned long long)frame.AddrPC.Offset,
module_info);
} else {
fprintf(out, " #%02d 0x%016llX\n",
frame_number,
(unsigned long long)frame.AddrPC.Offset);
}
}
}
++frame_count;
}
if (json_output) {
fprintf(out, "\n ]");
} else {
fprintf(out, "\n");
}
}
void platform_stack(uint64_t pid, int json_output) {
FILE *out;
char output_path[128];
if (json_output) {
out = stdout;
fprintf(out, "{\n");
fprintf(out, " \"pid\": %" PRIu64 ",\n", pid);
fprintf(out, " \"threads\": [\n");
} else {
out = open_dump_file(pid, output_path, sizeof(output_path));
if (!out) {
fprintf(stderr, "[ERROR] Cannot write dump file\n");
return;
}
fprintf(out, "Stack dump for PID %" PRIu64 "\n\n", pid);
}
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, (DWORD)pid);
if (!process) {
fprintf(stderr, "[ERROR] OpenProcess failed (%lu)\n", GetLastError());
if (!json_output) {
fclose(out);
}
return;
}
BOOL is_wow64 = is_wow64_process(process);
if (!json_output && is_wow64) {
fprintf(out, "[*] Target is 32-bit (WoW64) process on 64-bit OS\n\n");
}
HMODULE dbghelp = load_dbghelp();
if (!dbghelp) {
fprintf(stderr, "[ERROR] Could not load dbghelp.dll\n");
if (!json_output) {
fclose(out);
}
CloseHandle(process);
return;
}
PSymInitialize symInitialize;
PSymFromAddr symFromAddr;
PSymSetOptions symSetOptions;
PStackWalk64 stackWalk64;
PFUNCTION_TABLE_ACCESS_ROUTINE64 functionTableAccess;
PGET_MODULE_BASE_ROUTINE64 getModuleBase;
PSymLoadModuleExW symLoadModuleExW;
if (!init_dbghelp(dbghelp,
process,
&symInitialize,
&symFromAddr,
&symSetOptions,
&stackWalk64,
&functionTableAccess,
&getModuleBase,
&symLoadModuleExW)) {
fprintf(stderr, "[ERROR] dbghelp initialization failed\n");
FreeLibrary(dbghelp);
if (!json_output) {
fclose(out);
}
CloseHandle(process);
return;
}
load_remote_modules(process, (DWORD)pid, symLoadModuleExW);
if (!json_output) {
fprintf(out, "Stack dump for PID %" PRIu64 "\n\n", pid);
}
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snapshot == INVALID_HANDLE_VALUE) {
fprintf(stderr, "[ERROR] Failed to create thread snapshot\n");
FreeLibrary(dbghelp);
if (!json_output) {
fclose(out);
}
CloseHandle(process);
return;
}
THREADENTRY32 te;
te.dwSize = sizeof(te);
if (!Thread32First(snapshot, &te)) {
fprintf(stderr, "[ERROR] Thread32First failed\n");
CloseHandle(snapshot);
FreeLibrary(dbghelp);
if (!json_output) {
fclose(out);
}
CloseHandle(process);
return;
}
int thread_count = 0;
int first_thread = 1;
do {
if (te.th32OwnerProcessID != (DWORD)pid) {
continue;
}
HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
FALSE,
te.th32ThreadID);
if (!thread) {
if (json_output) {
if (!first_thread) fprintf(out, ",");
fprintf(out, "\n {\n \"thread_id\": %lu,\n \"error\": \"OpenThread failed (%lu)\"\n }",
te.th32ThreadID,
GetLastError());
first_thread = 0;
} else {
fprintf(out, "Thread %lu: OpenThread failed (%lu)\n\n",
te.th32ThreadID,
GetLastError());
}
continue;
}
DWORD suspend_count = SuspendThread(thread);
if (suspend_count == (DWORD)-1) {
if (json_output) {
if (!first_thread) fprintf(out, ",");
fprintf(out, "\n {\n \"thread_id\": %lu,\n \"error\": \"SuspendThread failed (%lu)\"\n }",
te.th32ThreadID,
GetLastError());
first_thread = 0;
} else {
fprintf(out, "Thread %lu: SuspendThread failed (%lu)\n\n",
te.th32ThreadID,
GetLastError());
}
CloseHandle(thread);
continue;
}
CONTEXT context;
memset(&context, 0, sizeof(context));
context.ContextFlags = CONTEXT_FULL;
if (GetThreadContext(thread, &context)) {
if (json_output) {
if (!first_thread) fprintf(out, ",");
fprintf(out, "\n {\n \"thread_id\": %lu,\n \"frames\": [", te.th32ThreadID);
first_thread = 0;
} else {
if (thread_count > 0) fprintf(out, "\n");
}
write_thread_stack(out,
process,
thread,
te.th32ThreadID,
(DWORD)pid,
&context,
stackWalk64,
functionTableAccess,
getModuleBase,
symFromAddr,
json_output);
if (json_output) {
fprintf(out, "\n }");
}
} else {
if (json_output) {
if (!first_thread) fprintf(out, ",");
fprintf(out, "\n {\n \"thread_id\": %lu,\n \"error\": \"GetThreadContext failed (%lu)\"\n }",
te.th32ThreadID,
GetLastError());
first_thread = 0;
} else {
fprintf(out, "Thread %lu: GetThreadContext failed (%lu)\n\n",
te.th32ThreadID,
GetLastError());
}
}
ResumeThread(thread);
CloseHandle(thread);
++thread_count;
} while (Thread32Next(snapshot, &te));
if (json_output) {
fprintf(out, "\n ],\n");
fprintf(out, " \"thread_count\": %d,\n", thread_count);
fprintf(out, " \"is_wow64\": %s\n", is_wow64 ? "true" : "false");
fprintf(out, "}\n");
} else {
fprintf(out, "Found %d threads\n", thread_count);
}
CloseHandle(snapshot);
FreeLibrary(dbghelp);
CloseHandle(process);
if (!json_output) {
fclose(out);
printf("[*] Dump written to %s\n", output_path);
}
}