Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook_trace.cpp
Go to the documentation of this file.
1#include "hook_trace.hpp"
2#include <TraceLoggingProvider.h>
3#include <winmeta.h>
4#include <stdio.h>
5
6#pragma comment(lib, "advapi32.lib")
7
8#pragma region TraceLogging Initialization
9// {D9AA2FDD-2853-4A97-A42F-E77E42921704}
11 (0xd9aa2fdd, 0x2853, 0x4a97, 0xa4, 0x2f, 0xe7, 0x7e, 0x42, 0x92, 0x17, 0x04));
12
14{
15 TraceLoggingRegister(g_hHookProvider);
16}
17
19{
20 TraceLoggingUnregister(g_hHookProvider);
21}
22#pragma endregion
23
25static VOID DebugMessage(const char* Format, ...)
26{
27#ifdef _DEBUG
28 char message[192];
29 va_list args;
30 va_start(args, Format);
31 _vsnprintf_s(message, _TRUNCATE, Format, args);
32 va_end(args);
33 OutputDebugStringA(message);
34#else
35 UNREFERENCED_PARAMETER(Format);
36#endif
37}
38
39#pragma region Target identity
40
47static HANDLE DuplicateForQuery(HANDLE Handle, ACCESS_MASK Access)
48{
49 HANDLE query = NULL;
50 if (!DuplicateHandle(GetCurrentProcess(), Handle, GetCurrentProcess(), &query, Access, FALSE, 0))
51 {
52 return NULL;
53 }
54 return query;
55}
56
59static DWORD TargetProcessIdOf(HANDLE ProcessHandle)
60{
61 DWORD processId = GetProcessId(ProcessHandle);
62 if (processId == 0)
63 {
64 HANDLE query = DuplicateForQuery(ProcessHandle, PROCESS_QUERY_LIMITED_INFORMATION);
65 if (query != NULL)
66 {
67 processId = GetProcessId(query);
68 CloseHandle(query);
69 }
70 }
71 return processId;
72}
73
76static VOID TargetThreadIdsOf(HANDLE ThreadHandle, DWORD* ProcessId, DWORD* ThreadId)
77{
78 *ProcessId = GetProcessIdOfThread(ThreadHandle);
79 *ThreadId = GetThreadId(ThreadHandle);
80 if (*ProcessId == 0)
81 {
82 HANDLE query = DuplicateForQuery(ThreadHandle, THREAD_QUERY_LIMITED_INFORMATION);
83 if (query != NULL)
84 {
85 *ProcessId = GetProcessIdOfThread(query);
86 *ThreadId = GetThreadId(query);
87 CloseHandle(query);
88 }
89 }
90}
91
92#pragma endregion
93
94#pragma region Detections
95VOID Log_PEWritten(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, BOOL Executable)
96{
97 if (!HookLoggingEnabled())
98 {
99 return;
100 }
101
102 TraceLoggingWrite(g_hHookProvider, "PEWritten",
103 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
104 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
105 TraceLoggingString("NtWriteVirtualMemory", "Api"),
106 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
107 TraceLoggingBool(!CurrentProcess, "Remote"),
108 TraceLoggingPointer(BaseAddress, "BaseAddress"),
109 TraceLoggingBool(Executable, "Executable"));
110
111 DebugMessage("[Panoptes] NtWriteVirtualMemory: PE image written to %p in the %s process, destination %s\n",
112 BaseAddress, CurrentProcess ? "current" : "remote", Executable ? "executable" : "not executable");
113}
114
115VOID Log_PEBWritten(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
116{
117 if (!HookLoggingEnabled())
118 {
119 return;
120 }
121
122 TraceLoggingWrite(g_hHookProvider, "PEBWritten",
123 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
124 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
125 TraceLoggingString("NtWriteVirtualMemory", "Api"),
126 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
127 TraceLoggingBool(!CurrentProcess, "Remote"),
128 TraceLoggingUInt64((UINT64)Size, "Size"),
129 TraceLoggingHexUInt64((UINT64)PebOffset, "PebOffset"));
130
131 DebugMessage("[Panoptes] NtWriteVirtualMemory: %Iu bytes written to the PEB of the %s process at offset 0x%Ix\n",
132 Size, CurrentProcess ? "current" : "remote", PebOffset);
133}
134
135VOID Log_EntryPointWritten(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, SIZE_T Size,
136 PVOID EntryPoint, BOOL CodeWritten, BOOL FieldWritten)
137{
138 if (!HookLoggingEnabled())
139 {
140 return;
141 }
142
143 TraceLoggingWrite(g_hHookProvider, "EntryPointWritten",
144 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
145 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
146 TraceLoggingString("NtWriteVirtualMemory", "Api"),
147 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
148 TraceLoggingBool(!CurrentProcess, "Remote"),
149 TraceLoggingPointer(BaseAddress, "BaseAddress"),
150 TraceLoggingUInt64((UINT64)Size, "Size"),
151 TraceLoggingPointer(EntryPoint, "EntryPoint"),
152 TraceLoggingBool(CodeWritten, "CodeWritten"),
153 TraceLoggingBool(FieldWritten, "FieldWritten"));
154
155 const char* what = (CodeWritten && FieldWritten) ? "entry point code and AddressOfEntryPoint field"
156 : CodeWritten ? "entry point code" : "AddressOfEntryPoint field";
157 DebugMessage("[Panoptes] NtWriteVirtualMemory: %Iu bytes written over the main image's %s in the %s process, entry point %p\n",
158 Size, what, CurrentProcess ? "current" : "remote", EntryPoint);
159}
160
161VOID Log_PEBRead(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
162{
163 if (!HookLoggingEnabled())
164 {
165 return;
166 }
167
168 // Debuggers, Task Manager and Process Explorer read other processes' PEBs routinely, so this
169 // is informational rather than a warning
170 TraceLoggingWrite(g_hHookProvider, "PEBRead",
171 TraceLoggingLevel(WINEVENT_LEVEL_INFO),
172 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
173 TraceLoggingString("NtReadVirtualMemory", "Api"),
174 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
175 TraceLoggingBool(!CurrentProcess, "Remote"),
176 TraceLoggingUInt64((UINT64)Size, "Size"),
177 TraceLoggingHexUInt64((UINT64)PebOffset, "PebOffset"));
178
179 DebugMessage("[Panoptes] NtReadVirtualMemory: %Iu bytes read from the PEB of the %s process at offset 0x%Ix\n",
180 Size, CurrentProcess ? "current" : "remote", PebOffset);
181}
182
183VOID Log_ProtectionSetAttempt(HANDLE ProcessHandle, BOOL CurrentProcess, NTSTATUS Status)
184{
185 if (!HookLoggingEnabled())
186 {
187 return;
188 }
189
190 TraceLoggingWrite(g_hHookProvider, "ProtectionSetAttempt",
191 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
192 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
193 TraceLoggingString("NtSetInformationProcess", "Api"),
194 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
195 TraceLoggingBool(!CurrentProcess, "Remote"),
196 TraceLoggingNTStatus(Status, "Status"));
197
198 DebugMessage("[Panoptes] NtSetInformationProcess: attempt to set the protection of the %s process, status 0x%08lx\n",
199 CurrentProcess ? "current" : "remote", (ULONG)Status);
200}
201
202VOID Log_ApcQueued(HANDLE ThreadHandle, BOOL CurrentProcess, const char* Api, PVOID ApcRoutine, ULONG ApcFlags)
203{
204 if (!HookLoggingEnabled())
205 {
206 return;
207 }
208
209 DWORD targetProcessId = 0;
210 DWORD targetThreadId = 0;
211 TargetThreadIdsOf(ThreadHandle, &targetProcessId, &targetThreadId);
212
213 TraceLoggingWrite(g_hHookProvider, "ApcQueued",
214 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
215 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
216 TraceLoggingString(Api, "Api"),
217 TraceLoggingUInt32(targetProcessId, "TargetProcessId"),
218 TraceLoggingBool(!CurrentProcess, "Remote"),
219 TraceLoggingUInt32(targetThreadId, "TargetThreadId"),
220 TraceLoggingPointer(ApcRoutine, "ApcRoutine"),
221 TraceLoggingHexUInt32(ApcFlags, "ApcFlags"));
222
223 DebugMessage("[Panoptes] %s: APC queued to a thread in the %s process, routine %p, flags 0x%lx\n",
224 Api, CurrentProcess ? "current" : "remote", ApcRoutine, ApcFlags);
225}
226
227VOID Log_RemoteThreadCreated(HANDLE ProcessHandle, PVOID StartRoutine, ULONG CreateFlags)
228{
229 if (!HookLoggingEnabled())
230 {
231 return;
232 }
233
234 TraceLoggingWrite(g_hHookProvider, "RemoteThreadCreated",
235 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
236 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
237 TraceLoggingString("NtCreateThreadEx", "Api"),
238 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
239 TraceLoggingBool(TRUE, "Remote"),
240 TraceLoggingPointer(StartRoutine, "StartRoutine"),
241 TraceLoggingHexUInt32(CreateFlags, "CreateFlags"));
242
243 DebugMessage("[Panoptes] NtCreateThreadEx: thread created in a remote process, start %p, flags 0x%lx\n",
244 StartRoutine, CreateFlags);
245}
246
247VOID Log_ExecutableAllocated(HANDLE ProcessHandle, BOOL CurrentProcess, const char* Api,
248 PVOID BaseAddress, SIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
249{
250 if (!HookLoggingEnabled())
251 {
252 return;
253 }
254
255 TraceLoggingWrite(g_hHookProvider, "ExecutableAllocated",
256 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
257 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
258 TraceLoggingString(Api, "Api"),
259 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
260 TraceLoggingBool(!CurrentProcess, "Remote"),
261 TraceLoggingPointer(BaseAddress, "BaseAddress"),
262 TraceLoggingUInt64((UINT64)RegionSize, "RegionSize"),
263 TraceLoggingHexUInt32(AllocationType, "AllocationType"),
264 TraceLoggingHexUInt32(Protection, "Protection"));
265
266 DebugMessage("[Panoptes] %s: %Iu executable bytes allocated at %p in the %s process (protection 0x%lx)\n",
267 Api, RegionSize, BaseAddress, CurrentProcess ? "current" : "remote", Protection);
268}
269
270VOID Log_ExecutableUnmapped(HANDLE ProcessHandle, BOOL CurrentProcess, const char* Api,
271 PVOID BaseAddress, BOOL IsImage)
272{
273 if (!HookLoggingEnabled())
274 {
275 return;
276 }
277
278 TraceLoggingWrite(g_hHookProvider, "ExecutableUnmapped",
279 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
280 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
281 TraceLoggingString(Api, "Api"),
282 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
283 TraceLoggingBool(!CurrentProcess, "Remote"),
284 TraceLoggingPointer(BaseAddress, "BaseAddress"),
285 TraceLoggingBool(IsImage, "IsImage"));
286
287 DebugMessage("[Panoptes] %s: executable %s at %p unmapped from the %s process\n",
288 Api, IsImage ? "image" : "view", BaseAddress, CurrentProcess ? "current" : "remote");
289}
290
291VOID Log_RemoteViewMapped(HANDLE ProcessHandle, const char* Api, PVOID BaseAddress, SIZE_T ViewSize,
292 ULONG Protection, BOOL Executable, BOOL IsImage)
293{
294 if (!HookLoggingEnabled())
295 {
296 return;
297 }
298
299 TraceLoggingWrite(g_hHookProvider, "RemoteViewMapped",
300 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
301 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
302 TraceLoggingString(Api, "Api"),
303 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
304 TraceLoggingBool(TRUE, "Remote"),
305 TraceLoggingPointer(BaseAddress, "BaseAddress"),
306 TraceLoggingUInt64((UINT64)ViewSize, "ViewSize"),
307 TraceLoggingHexUInt32(Protection, "Protection"),
308 TraceLoggingBool(Executable, "Executable"),
309 TraceLoggingBool(IsImage, "IsImage"));
310
311 DebugMessage("[Panoptes] %s: %Iu-byte %s mapped into a remote process at %p (protection 0x%lx, %s)\n",
312 Api, ViewSize, IsImage ? "image" : "view", BaseAddress, Protection,
313 Executable ? "executable" : "not executable");
314}
315
316VOID Log_PEMadeExecutable(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress,
317 ULONG OldProtection, ULONG NewProtection)
318{
319 if (!HookLoggingEnabled())
320 {
321 return;
322 }
323
324 TraceLoggingWrite(g_hHookProvider, "PEMadeExecutable",
325 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
326 TraceLoggingKeyword(HOOK_KEYWORD_DETECTION),
327 TraceLoggingString("NtProtectVirtualMemory", "Api"),
328 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle), "TargetProcessId"),
329 TraceLoggingBool(!CurrentProcess, "Remote"),
330 TraceLoggingPointer(BaseAddress, "BaseAddress"),
331 TraceLoggingHexUInt32(OldProtection, "OldProtection"),
332 TraceLoggingHexUInt32(NewProtection, "NewProtection"));
333
334 DebugMessage("[Panoptes] NtProtectVirtualMemory: PE image at %p in the %s process made executable (0x%lx -> 0x%lx)\n",
335 BaseAddress, CurrentProcess ? "current" : "remote", OldProtection, NewProtection);
336}
337#pragma endregion
VOID Log_ProtectionSetAttempt(HANDLE ProcessHandle, BOOL CurrentProcess, NTSTATUS Status)
NtSetInformationProcess was called to set a process's protection level (ProcessProtectionInformation)...
VOID Log_ApcQueued(HANDLE ThreadHandle, BOOL CurrentProcess, const char *Api, PVOID ApcRoutine, ULONG ApcFlags)
NtQueueApcThread or NtQueueApcThreadEx2 queued an APC to a thread. Event "ApcQueued"....
TRACELOGGING_DEFINE_PROVIDER(g_hHookProvider, HOOK_PROVIDER_NAME,(0xd9aa2fdd, 0x2853, 0x4a97, 0xa4, 0x2f, 0xe7, 0x7e, 0x42, 0x92, 0x17, 0x04))
VOID Log_RemoteViewMapped(HANDLE ProcessHandle, const char *Api, PVOID BaseAddress, SIZE_T ViewSize, ULONG Protection, BOOL Executable, BOOL IsImage)
NtMapViewOfSection or NtMapViewOfSectionEx mapped a view into another process. Mapping a shared secti...
VOID Log_PEMadeExecutable(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, ULONG OldProtection, ULONG NewProtection)
NtProtectVirtualMemory made a region starting with a PE image executable. Event "PEMadeExecutable".
VOID HookTraceInit()
Registers the provider. Call before placing the hooks.
VOID Log_ExecutableAllocated(HANDLE ProcessHandle, BOOL CurrentProcess, const char *Api, PVOID BaseAddress, SIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
NtAllocateVirtualMemory or NtAllocateVirtualMemoryEx allocated executable memory. Event "ExecutableAl...
VOID Log_RemoteThreadCreated(HANDLE ProcessHandle, PVOID StartRoutine, ULONG CreateFlags)
NtCreateThreadEx created a thread in another process. Event "RemoteThreadCreated".
VOID Log_EntryPointWritten(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, SIZE_T Size, PVOID EntryPoint, BOOL CodeWritten, BOOL FieldWritten)
NtWriteVirtualMemory wrote over the entry point of a process's main image (the image PEB....
VOID Log_PEBWritten(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
NtWriteVirtualMemory wrote to a process's PEB. Event "PEBWritten".
VOID HookTraceUninit()
Unregisters the provider. Call after removing the hooks.
VOID Log_PEWritten(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, BOOL Executable)
NtWriteVirtualMemory wrote a PE image. Event "PEWritten".
VOID Log_ExecutableUnmapped(HANDLE ProcessHandle, BOOL CurrentProcess, const char *Api, PVOID BaseAddress, BOOL IsImage)
NtUnmapViewOfSection or NtUnmapViewOfSectionEx unmapped a view with executable pages....
VOID Log_PEBRead(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
NtReadVirtualMemory read from a process's PEB. Event "PEBRead".
BOOL HookLoggingEnabled()
Whether the Log_* functions should write. FALSE on the thread running a Detours transaction (so the h...
Definition hook.cpp:860
#define HOOK_KEYWORD_DETECTION
#define HOOK_PROVIDER_NAME