-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.c
More file actions
320 lines (218 loc) · 8.74 KB
/
Main.c
File metadata and controls
320 lines (218 loc) · 8.74 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
// structscan - a WinDbg extension that scans data structures for which you do not have
// private symbols and attempts to find interesting data in them.
// Joseph Ryan Ries - 2022. Watch the development on this extension on video
// here: https://www.youtube.com/watch?v=d1uT8tmnhZI
//
// TODO: Currently the only way I have gotten this to work is if I install my callback to
// get debugger output, execute the debugger command, then reinstall the original callback
// to write text to the debugger window. I feel like this constant flipping back and forth
// between the two callbacks is probably wrong. There must be a better way. I feel like I'm
// supposed to be supplying an array of callbacks that will be called in series, but I don't know...
// Need to define INITGUID before dbgeng.h because we need to use the GUIDs
// from C, since we don't have __uuidof
#define INITGUID
#include <DbgEng.h>
#include <stdio.h>
#include "Main.h"
#define EXTENSION_VERSION_MAJOR 1
#define EXTENSION_VERSION_MINOR 0
wchar_t gOutputBuffer[4096];
wchar_t gCommandBuffer[128];
IDebugOutputCallbacks2* gPrevOutputCallback;
IDebugOutputCallbacks2 gOutputCallback;
IDebugOutputCallbacks2Vtbl gOcbVtbl = {
.AddRef = &CbAddRef,
.Release = &CbRelease,
.Output = &CbOutput,
.Output2 = &CbOutput2,
.QueryInterface = &CbQueryInterface,
.GetInterestMask = &CbGetInterestMask };
// This is the entry point of our DLL. EngHost calls this as soon as you load the DLL.
__declspec(dllexport) HRESULT CALLBACK DebugExtensionInitialize(_Out_ PULONG Version, _Out_ PULONG Flags)
{
*Version = DEBUG_EXTENSION_VERSION(EXTENSION_VERSION_MAJOR, EXTENSION_VERSION_MINOR);
*Flags = 0;
gOutputCallback.lpVtbl = &gOcbVtbl;
return(S_OK);
}
__declspec(dllexport) HRESULT CALLBACK structscan(_In_ IDebugClient4* Client, _In_opt_ PCSTR Args)
{
HRESULT Hr = S_OK;
IDebugControl4* DebugControl = NULL;
IDebugSymbols4* Symbols = NULL;
wchar_t WideArgs[128] = { 0 };
wchar_t ModuleName[64] = { 0 };
size_t CharsConverted = 0;
ULONG ImageIndex = 0;
ULONG64 ImageBase = 0;
ULONG64 SearchHandle = 0;
ULONG64 SymbolAddress = 0;
DEBUG_MODULE_PARAMETERS ModuleParameters = { 0 };
wchar_t* DisplayMemCommands[] = { L"dS", L"ds" };
ULONG Offset = 0;
BOOL EndScan = FALSE;
mbstowcs_s(&CharsConverted, WideArgs, _countof(WideArgs), Args, _TRUNCATE);
if ((Hr = Client->lpVtbl->QueryInterface(Client, &IID_IDebugControl4, (void**)&DebugControl)) != S_OK)
{
goto Exit;
}
if ((Hr = Client->lpVtbl->QueryInterface(Client, &IID_IDebugSymbols4, (void**)&Symbols)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"QueryInterface failed! Hr = 0x%x!\n\n", Hr);
goto Exit;
}
// !structscan ntdsai!gAnchor
if (WideArgs == NULL ||
wcslen(WideArgs) >= 64 ||
wcschr(WideArgs, L'!') == NULL ||
WideArgs[0] == '!' ||
wcschr(WideArgs, L' ') ||
wcschr(WideArgs, L'*'))
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"structscan v%d.%d, Joseph Ryan Ries 2022\n\n", EXTENSION_VERSION_MAJOR, EXTENSION_VERSION_MINOR);
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Scans data structures for which you do not have private symbols, looking for interesting data.\n\n");
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"USAGE: !structscan module!struct\n\n");
goto Exit;
}
for (int c = 0; c < _countof(ModuleName); c++)
{
if (WideArgs[c] == L'!')
{
break;
}
ModuleName[c] = WideArgs[c];
}
if ((Hr = Symbols->lpVtbl->GetModuleByModuleNameWide(Symbols, ModuleName, 0, &ImageIndex, &ImageBase)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"GetModuleByModuleNameWide failed! Hr = 0x%x!\n\n");
goto Exit;
}
if ((Hr = Symbols->lpVtbl->GetModuleParameters(Symbols, 1, NULL, ImageIndex, &ModuleParameters)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"GetModuleParameters failed! Hr = 0x%x!\n\n");
goto Exit;
}
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Module name: %s\nImage base: 0x%p\nMemory Size: %llu\n", ModuleName, ImageBase, ModuleParameters.Size);
if ((Hr = Symbols->lpVtbl->StartSymbolMatchWide(Symbols, WideArgs, &SearchHandle)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"StartSymbolMatchWide failed! Hr = 0x%x!\n\n");
goto Exit;
}
Symbols->lpVtbl->GetNextSymbolMatch(Symbols, SearchHandle, NULL, 0, 0, &SymbolAddress);
Symbols->lpVtbl->EndSymbolMatch(Symbols, SearchHandle);
if (SymbolAddress == 0)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Symbol %s was not found!\n\n", WideArgs);
goto Exit;
}
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Symbol Address: %p\n", SymbolAddress);
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Press Ctrl+C to abort...\n");
if ((Hr = Client->lpVtbl->GetOutputCallbacks(Client, (PDEBUG_OUTPUT_CALLBACKS)&gPrevOutputCallback)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Failed to get existing output callback! Hr = 0x%x\n\n\n", Hr);
goto Exit;
}
if ((Hr = Client->lpVtbl->SetOutputCallbacks(Client, (PDEBUG_OUTPUT_CALLBACKS)&gOutputCallback)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Failed to set output callback! Hr = 0x%x\n\n\n", Hr);
goto Exit;
}
for (int c = 0; c < _countof(DisplayMemCommands); c++)
{
Offset = 0;
if (EndScan == TRUE)
{
break;
}
while (EndScan == FALSE)
{
wchar_t SymbolName[64] = { 0 };
_snwprintf_s(gCommandBuffer, _countof(gCommandBuffer), _TRUNCATE, L"%s %s+0x%lx", DisplayMemCommands[c], WideArgs, Offset);
DebugControl->lpVtbl->ExecuteWide(DebugControl, DEBUG_OUTCTL_THIS_CLIENT, gCommandBuffer, DEBUG_EXECUTE_DEFAULT);
if (gPrevOutputCallback)
{
if ((Hr = Client->lpVtbl->SetOutputCallbacks(Client, (PDEBUG_OUTPUT_CALLBACKS)gPrevOutputCallback)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Failed to restore original callback!\n\n");
}
}
if (wcsstr(gOutputBuffer, L"???") == 0 && (wcslen(gOutputBuffer) > 0))
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"%s = %s", gCommandBuffer, gOutputBuffer);
}
memset(gOutputBuffer, 0, sizeof(gOutputBuffer));
Client->lpVtbl->SetOutputCallbacks(Client, (PDEBUG_OUTPUT_CALLBACKS)&gOutputCallback);
Offset += 2;
Symbols->lpVtbl->GetNameByOffsetWide(Symbols, (SymbolAddress + Offset), SymbolName, _countof(SymbolName), NULL, NULL);
if (wcscmp(SymbolName, WideArgs) != 0)
{
break;
}
if ((Offset >= 0x1000) || ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(0x43) & 0x8000)))
{
EndScan = TRUE;
}
}
}
Exit:
if (gPrevOutputCallback)
{
if ((Hr = Client->lpVtbl->SetOutputCallbacks(Client, (PDEBUG_OUTPUT_CALLBACKS)gPrevOutputCallback)) != S_OK)
{
DebugControl->lpVtbl->OutputWide(DebugControl, DEBUG_OUTCTL_ALL_CLIENTS, L"Failed to restore original callback!\n\n");
}
}
if (Symbols)
{
Symbols->lpVtbl->Release(Symbols);
}
if (DebugControl)
{
DebugControl->lpVtbl->Release(DebugControl);
}
return(Hr);
}
// All of these CbXXX functions are my implementations of the methods that you would
// find on a IDebugCallbacks2 interface. I will put pointers to all of these functions
// into a "lpVtbl" and set that onto my callback during the extension initialization routine.
// All of these functions need to exist even if they don't do anything because the debug engine
// will try to call them and enghost will crash if any function pointers are null.
ULONG __cdecl CbAddRef(IDebugOutputCallbacks2* This)
{
UNREFERENCED_PARAMETER(This);
return(1);
}
ULONG __cdecl CbQueryInterface(IDebugOutputCallbacks2* This, IN REFIID InterfaceId, OUT PVOID* Interface)
{
UNREFERENCED_PARAMETER(InterfaceId);
*Interface = This;
CbAddRef(This);
return(S_OK);
}
ULONG __cdecl CbRelease(IDebugOutputCallbacks2* This)
{
UNREFERENCED_PARAMETER(This);
return(0);
}
HRESULT __cdecl CbGetInterestMask(IDebugOutputCallbacks2* This, OUT PULONG Mask)
{
UNREFERENCED_PARAMETER(This);
*Mask = DEBUG_OUTCBI_ANY_FORMAT;
return(S_OK);
}
HRESULT __stdcall CbOutput(IDebugOutputCallbacks2* This, IN ULONG Mask, IN PCSTR Text)
{
UNREFERENCED_PARAMETER(This);
UNREFERENCED_PARAMETER(Mask);
UNREFERENCED_PARAMETER(Text);
return(S_OK);
}
HRESULT __stdcall CbOutput2(IDebugOutputCallbacks2* This, IN ULONG Which, IN ULONG Flags, IN ULONG64 Arg, PCWSTR Text)
{
UNREFERENCED_PARAMETER(This);
UNREFERENCED_PARAMETER(Which);
UNREFERENCED_PARAMETER(Flags);
UNREFERENCED_PARAMETER(Arg);
wcscpy_s(gOutputBuffer, _countof(gOutputBuffer), Text);
return(S_OK);
}