-
Notifications
You must be signed in to change notification settings - Fork 111
/
WinDbgMonitor.cpp
382 lines (325 loc) · 10.6 KB
/
WinDbgMonitor.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
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
/*
* System Informer Plugins -
* qt port of Debug View Plugin
*
* Copyright (C) 2015-2017 dmex
* Copyright (C) 2020-2022 David Xanatos
*
* This file is part of Task Explorer and contains System Informer code.
*
*/
#include "stdafx.h"
#include "WinDbgMonitor.h"
#include "../ProcessHacker.h"
#include "../ProcessHacker/GpuCounters.h"
#include "../WinProcess.h"
#include "../WindowsAPI.h"
#include "../../../../MiscHelpers/Common/Settings.h"
#include <Sddl.h>
#define DBWIN_BUFFER_READY L"DBWIN_BUFFER_READY"
#define DBWIN_DATA_READY L"DBWIN_DATA_READY"
#define DBWIN_BUFFER L"DBWIN_BUFFER"
#define DBWIN_BUFFER_READY_OBJECT_NAME L"\\BaseNamedObjects\\DBWIN_BUFFER_READY"
#define DBWIN_DATA_READY_OBJECT_NAME L"\\BaseNamedObjects\\DBWIN_DATA_READY"
#define DBWIN_BUFFER_SECTION_NAME L"\\BaseNamedObjects\\DBWIN_BUFFER"
// The Win32 OutputDebugString buffer.
typedef struct _DBWIN_PAGE_BUFFER
{
ULONG ProcessId; /** The ID of the process. */
CHAR Buffer[PAGE_SIZE - sizeof(ULONG)]; /** The buffer containing the debug message. */
} DBWIN_PAGE_BUFFER, *PDBWIN_PAGE_BUFFER;
struct SWinDbgMonitor
{
SWinDbgMonitor()
{
LocalCaptureEnabled = FALSE;
LocalBufferReadyEvent = NULL;
LocalDataReadyEvent = NULL;
LocalDataBufferHandle = NULL;
LocalDebugBuffer = NULL;
GlobalCaptureEnabled = FALSE;
GlobalBufferReadyEvent = NULL;
GlobalDataReadyEvent = NULL;
GlobalDataBufferHandle = NULL;
GlobalDebugBuffer = NULL;
KernelCaptureEnabled = FALSE;
DbgCreateSecurityAttributes();
}
~SWinDbgMonitor()
{
DbgCleanupSecurityAttributes();
}
SECURITY_ATTRIBUTES SecurityAttributes;
BOOLEAN LocalCaptureEnabled;
HANDLE LocalBufferReadyEvent;
HANDLE LocalDataReadyEvent;
HANDLE LocalDataBufferHandle;
PDBWIN_PAGE_BUFFER LocalDebugBuffer;
BOOLEAN GlobalCaptureEnabled;
HANDLE GlobalBufferReadyEvent;
HANDLE GlobalDataReadyEvent;
HANDLE GlobalDataBufferHandle;
PDBWIN_PAGE_BUFFER GlobalDebugBuffer;
BOOLEAN KernelCaptureEnabled;
BOOL DbgCreateSecurityAttributes()
{
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle = FALSE;
return ConvertStringSecurityDescriptorToSecurityDescriptor(
L"D:(A;;GRGWGX;;;WD)(A;;GA;;;SY)(A;;GA;;;BA)(A;;GRGWGX;;;AN)(A;;GRGWGX;;;RC)(A;;GRGWGX;;;S-1-15-2-1)S:(ML;;NW;;;LW)",
SDDL_REVISION, &SecurityAttributes.lpSecurityDescriptor, NULL);
}
void DbgCleanupSecurityAttributes()
{
if (SecurityAttributes.lpSecurityDescriptor)
{
LocalFree(SecurityAttributes.lpSecurityDescriptor);
SecurityAttributes.lpSecurityDescriptor = NULL;
}
}
VOID DbgFormatObjectName(
_In_ BOOLEAN LocalName,
_In_ PWSTR OriginalName,
_Out_ PUNICODE_STRING ObjectName
)
{
SIZE_T length;
SIZE_T originalNameLength;
// Sessions other than session 0 require SeCreateGlobalPrivilege.
if (LocalName && NtCurrentPeb()->SessionId != 0)
{
WCHAR buffer[256] = L"";
memcpy(buffer, L"\\Sessions\\", 10 * sizeof(WCHAR));
_ultow(NtCurrentPeb()->SessionId, buffer + 10, 10);
length = PhCountStringZ(buffer);
originalNameLength = PhCountStringZ(OriginalName);
memcpy(buffer + length, OriginalName, (originalNameLength + 1) * sizeof(WCHAR));
length += originalNameLength;
ObjectName->Buffer = buffer;
ObjectName->MaximumLength = (ObjectName->Length = (USHORT)(length * sizeof(WCHAR))) + sizeof(WCHAR);
}
else
{
RtlInitUnicodeString(ObjectName, OriginalName);
}
}
STATUS Init(bool bGlobal)
{
BOOLEAN& CaptureEnabled = bGlobal ? GlobalCaptureEnabled : LocalCaptureEnabled;
HANDLE& BufferReadyEvent = bGlobal ? GlobalBufferReadyEvent : LocalBufferReadyEvent;
HANDLE& DataReadyEvent = bGlobal ? GlobalDataReadyEvent : LocalDataReadyEvent;
HANDLE& DataBufferHandle = bGlobal ? GlobalDataBufferHandle : LocalDataBufferHandle;
PDBWIN_PAGE_BUFFER& DebugBuffer = bGlobal ? GlobalDebugBuffer : LocalDebugBuffer;
SIZE_T viewSize;
LARGE_INTEGER maximumSize;
OBJECT_ATTRIBUTES objectAttributes;
UNICODE_STRING objectName;
maximumSize.QuadPart = PAGE_SIZE;
viewSize = sizeof(DBWIN_PAGE_BUFFER);
if (!(BufferReadyEvent = CreateEvent(&SecurityAttributes, FALSE, FALSE, (std::wstring(bGlobal ? L"Global\\" : L"Local\\") + DBWIN_BUFFER_READY).c_str())))
return ERR("DBWIN_BUFFER_READY", GetLastError());
if (!(DataReadyEvent = CreateEvent(&SecurityAttributes, FALSE, FALSE, (std::wstring(bGlobal ? L"Global\\" : L"Local\\") + DBWIN_DATA_READY).c_str())))
return ERR("DBWIN_DATA_READY", GetLastError());
DbgFormatObjectName(bGlobal ? FALSE : TRUE, DBWIN_BUFFER_SECTION_NAME, &objectName);
InitializeObjectAttributes(
&objectAttributes,
&objectName,
OBJ_CASE_INSENSITIVE,
NULL,
SecurityAttributes.lpSecurityDescriptor
);
if (!NT_SUCCESS(NtCreateSection(
&DataBufferHandle,
STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
&objectAttributes,
&maximumSize,
PAGE_READWRITE,
SEC_COMMIT,
NULL
)))
{
return ERR("NtCreateSection", GetLastError());
}
if (!NT_SUCCESS(NtMapViewOfSection(
DataBufferHandle,
NtCurrentProcess(),
(PVOID*)&DebugBuffer,
0,
0,
NULL,
&viewSize,
ViewShare,
0,
PAGE_READONLY
)))
{
return ERR("NtMapViewOfSection", GetLastError());
}
CaptureEnabled = TRUE;
return OK;
}
void UnInit(bool bGlobal)
{
BOOLEAN& CaptureEnabled = bGlobal ? GlobalCaptureEnabled : LocalCaptureEnabled;
HANDLE& BufferReadyEvent = bGlobal ? GlobalBufferReadyEvent : LocalBufferReadyEvent;
HANDLE& DataReadyEvent = bGlobal ? GlobalDataReadyEvent : LocalDataReadyEvent;
HANDLE& DataBufferHandle = bGlobal ? GlobalDataBufferHandle : LocalDataBufferHandle;
PDBWIN_PAGE_BUFFER& DebugBuffer = bGlobal ? GlobalDebugBuffer : LocalDebugBuffer;
CaptureEnabled = FALSE;
if (DebugBuffer)
{
NtUnmapViewOfSection(NtCurrentProcess(), DebugBuffer);
DebugBuffer = NULL;
}
if (DataBufferHandle)
{
NtClose(DataBufferHandle);
DataBufferHandle = NULL;
}
if (BufferReadyEvent)
{
NtClose(BufferReadyEvent);
BufferReadyEvent = NULL;
}
if (DataReadyEvent)
{
NtClose(DataReadyEvent);
DataReadyEvent = NULL;
}
}
};
CWinDbgMonitor::CWinDbgMonitor(QObject *parent)
: QObject(parent)
{
m = new SWinDbgMonitor;
}
CWinDbgMonitor::~CWinDbgMonitor()
{
if (m->LocalCaptureEnabled)
m->UnInit(false);
if (m->GlobalCaptureEnabled)
m->UnInit(true);
if (m->KernelCaptureEnabled)
KphSetDebugLog(FALSE);
delete m;
}
static NTSTATUS DbgEventsThread(bool bGlobal, CWinDbgMonitor* This)
{
HANDLE& BufferReadyEvent = bGlobal ? This->m->GlobalBufferReadyEvent : This->m->LocalBufferReadyEvent;
HANDLE& DataReadyEvent = bGlobal ? This->m->GlobalDataReadyEvent : This->m->LocalDataReadyEvent;
PDBWIN_PAGE_BUFFER debugMessageBuffer = bGlobal ? This->m->GlobalDebugBuffer : This->m->LocalDebugBuffer;
while (TRUE)
{
NtSetEvent(BufferReadyEvent, NULL);
LARGE_INTEGER timeout;
NTSTATUS status = NtWaitForSingleObject(
DataReadyEvent,
FALSE,
PhTimeoutFromMilliseconds(&timeout, 100)
);
if (status == STATUS_TIMEOUT)
continue;
if (status != STATUS_SUCCESS)
break;
// The process calling OutputDebugString is blocked here...
emit This->DebugMessage(debugMessageBuffer->ProcessId, QString::fromLatin1(debugMessageBuffer->Buffer));
}
return STATUS_SUCCESS;
}
static NTSTATUS DbgEventsLocalThread(PVOID Parameter)
{
return DbgEventsThread(false, (CWinDbgMonitor*)Parameter);
}
static NTSTATUS DbgEventsGlobalThread(PVOID Parameter)
{
return DbgEventsThread(true, (CWinDbgMonitor*)Parameter);
}
//static NTSTATUS DbgEventsKernelThread(PVOID Parameter)
//{
// CWinDbgMonitor* This = (CWinDbgMonitor*)Parameter;
//
// ULONG SequenceNumber = 0;
//
// while (TRUE)
// {
// SIZE_T Length = 0;
// char Buffer[8 * 1024] = { 0 };
// NTSTATUS status = KphReadDebugLog(&SequenceNumber, Buffer, ARRSIZE(Buffer), &Length);
// if (status == STATUS_NO_MORE_ENTRIES) {
// QThread::msleep(10);
// continue;
// }
// if (!NT_SUCCESS(status) && status != STATUS_BUFFER_TOO_SMALL) // Note: if the buffer was to small it still wil hold a truncated result
// break;
//
// emit This->DebugMessage((quint64)SYSTEM_PROCESS_ID, QString::fromLatin1(Buffer, Length));
// }
//
// return STATUS_SUCCESS;
//}
extern void (*g_KernelDebugLogger)(const QString& Output);
void KernelDebugLogger(const QString& Output)
{
if (theAPI && ((CWindowsAPI*)theAPI)->m_pDebugMonitor)
((CWindowsAPI*)theAPI)->m_pDebugMonitor->DebugMessage((quint64)SYSTEM_PROCESS_ID, Output);
}
STATUS CWinDbgMonitor::SetMonitor(EModes Mode)
{
if ((Mode & eLocal) != 0)
{
if (!m->LocalCaptureEnabled)
{
STATUS Status = m->Init(false);
if (Status.IsError())
return Status;
if (HANDLE threadHandle = PhCreateThread(0, (PUSER_THREAD_START_ROUTINE)DbgEventsLocalThread, this))
NtClose(threadHandle);
}
}
else if (m->LocalCaptureEnabled)
m->UnInit(false);
if ((Mode & eGlobal) != 0)
{
if (!m->GlobalCaptureEnabled)
{
STATUS Status = m->Init(true);
if (Status.IsError())
return Status;
if (HANDLE threadHandle = PhCreateThread(0, (PUSER_THREAD_START_ROUTINE)DbgEventsGlobalThread, this))
NtClose(threadHandle);
}
}
else if(m->GlobalCaptureEnabled)
m->UnInit(true);
if ((Mode & eKernel) != 0)
{
if (!m->KernelCaptureEnabled)
{
if(!g_KernelDebugLogger)
g_KernelDebugLogger = KernelDebugLogger;
NTSTATUS status = KphSetDebugLog(TRUE);
if (!NT_SUCCESS(status))
return ERR("KphSetDebugLog", status);
m->KernelCaptureEnabled = TRUE;
//if (HANDLE threadHandle = PhCreateThread(0, (PUSER_THREAD_START_ROUTINE)DbgEventsKernelThread, this))
// NtClose(threadHandle);
}
}
else if(m->KernelCaptureEnabled)
{
KphSetDebugLog(FALSE);
m->KernelCaptureEnabled = FALSE;
}
return OK;
}
CWinDbgMonitor::EModes CWinDbgMonitor::GetMode() const
{
int Mode = eNone;
if (m->LocalCaptureEnabled)
Mode |= eLocal;
if (m->GlobalCaptureEnabled)
Mode |= eGlobal;
if (m->KernelCaptureEnabled)
Mode |= eKernel;
return (CWinDbgMonitor::EModes)Mode;
}