2#include <TraceLoggingProvider.h>
6#pragma comment(lib, "advapi32.lib")
8#pragma region TraceLogging Initialization
11 (0xd9aa2fdd, 0x2853, 0x4a97, 0xa4, 0x2f, 0xe7, 0x7e, 0x42, 0x92, 0x17, 0x04));
15 TraceLoggingRegister(g_hHookProvider);
20 TraceLoggingUnregister(g_hHookProvider);
25static VOID DebugMessage(
const char* Format, ...)
30 va_start(args, Format);
31 _vsnprintf_s(message, _TRUNCATE, Format, args);
33 OutputDebugStringA(message);
35 UNREFERENCED_PARAMETER(Format);
39#pragma region Target identity
47static HANDLE DuplicateForQuery(HANDLE Handle, ACCESS_MASK Access)
50 if (!DuplicateHandle(GetCurrentProcess(), Handle, GetCurrentProcess(), &query, Access, FALSE, 0))
59static DWORD TargetProcessIdOf(HANDLE ProcessHandle)
61 DWORD processId = GetProcessId(ProcessHandle);
64 HANDLE query = DuplicateForQuery(ProcessHandle, PROCESS_QUERY_LIMITED_INFORMATION);
67 processId = GetProcessId(query);
76static VOID TargetThreadIdsOf(HANDLE ThreadHandle, DWORD* ProcessId, DWORD* ThreadId)
78 *ProcessId = GetProcessIdOfThread(ThreadHandle);
79 *ThreadId = GetThreadId(ThreadHandle);
82 HANDLE query = DuplicateForQuery(ThreadHandle, THREAD_QUERY_LIMITED_INFORMATION);
85 *ProcessId = GetProcessIdOfThread(query);
86 *ThreadId = GetThreadId(query);
94#pragma region Detections
95VOID
Log_PEWritten(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, BOOL Executable)
102 TraceLoggingWrite(g_hHookProvider,
"PEWritten",
103 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
105 TraceLoggingString(
"NtWriteVirtualMemory",
"Api"),
106 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle),
"TargetProcessId"),
107 TraceLoggingBool(!CurrentProcess,
"Remote"),
108 TraceLoggingPointer(BaseAddress,
"BaseAddress"),
109 TraceLoggingBool(Executable,
"Executable"));
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");
115VOID
Log_PEBWritten(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
122 TraceLoggingWrite(g_hHookProvider,
"PEBWritten",
123 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
125 TraceLoggingString(
"NtWriteVirtualMemory",
"Api"),
126 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle),
"TargetProcessId"),
127 TraceLoggingBool(!CurrentProcess,
"Remote"),
128 TraceLoggingUInt64((UINT64)Size,
"Size"),
129 TraceLoggingHexUInt64((UINT64)PebOffset,
"PebOffset"));
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);
136 PVOID EntryPoint, BOOL CodeWritten, BOOL FieldWritten)
143 TraceLoggingWrite(g_hHookProvider,
"EntryPointWritten",
144 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
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"));
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);
161VOID
Log_PEBRead(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
170 TraceLoggingWrite(g_hHookProvider,
"PEBRead",
171 TraceLoggingLevel(WINEVENT_LEVEL_INFO),
173 TraceLoggingString(
"NtReadVirtualMemory",
"Api"),
174 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle),
"TargetProcessId"),
175 TraceLoggingBool(!CurrentProcess,
"Remote"),
176 TraceLoggingUInt64((UINT64)Size,
"Size"),
177 TraceLoggingHexUInt64((UINT64)PebOffset,
"PebOffset"));
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);
190 TraceLoggingWrite(g_hHookProvider,
"ProtectionSetAttempt",
191 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
193 TraceLoggingString(
"NtSetInformationProcess",
"Api"),
194 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle),
"TargetProcessId"),
195 TraceLoggingBool(!CurrentProcess,
"Remote"),
196 TraceLoggingNTStatus(Status,
"Status"));
198 DebugMessage(
"[Panoptes] NtSetInformationProcess: attempt to set the protection of the %s process, status 0x%08lx\n",
199 CurrentProcess ?
"current" :
"remote", (ULONG)Status);
202VOID
Log_ApcQueued(HANDLE ThreadHandle, BOOL CurrentProcess,
const char* Api, PVOID ApcRoutine, ULONG ApcFlags)
209 DWORD targetProcessId = 0;
210 DWORD targetThreadId = 0;
211 TargetThreadIdsOf(ThreadHandle, &targetProcessId, &targetThreadId);
213 TraceLoggingWrite(g_hHookProvider,
"ApcQueued",
214 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
216 TraceLoggingString(Api,
"Api"),
217 TraceLoggingUInt32(targetProcessId,
"TargetProcessId"),
218 TraceLoggingBool(!CurrentProcess,
"Remote"),
219 TraceLoggingUInt32(targetThreadId,
"TargetThreadId"),
220 TraceLoggingPointer(ApcRoutine,
"ApcRoutine"),
221 TraceLoggingHexUInt32(ApcFlags,
"ApcFlags"));
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);
234 TraceLoggingWrite(g_hHookProvider,
"RemoteThreadCreated",
235 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
237 TraceLoggingString(
"NtCreateThreadEx",
"Api"),
238 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle),
"TargetProcessId"),
239 TraceLoggingBool(TRUE,
"Remote"),
240 TraceLoggingPointer(StartRoutine,
"StartRoutine"),
241 TraceLoggingHexUInt32(CreateFlags,
"CreateFlags"));
243 DebugMessage(
"[Panoptes] NtCreateThreadEx: thread created in a remote process, start %p, flags 0x%lx\n",
244 StartRoutine, CreateFlags);
248 PVOID BaseAddress, SIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
255 TraceLoggingWrite(g_hHookProvider,
"ExecutableAllocated",
256 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
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"));
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);
271 PVOID BaseAddress, BOOL IsImage)
278 TraceLoggingWrite(g_hHookProvider,
"ExecutableUnmapped",
279 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
281 TraceLoggingString(Api,
"Api"),
282 TraceLoggingUInt32(TargetProcessIdOf(ProcessHandle),
"TargetProcessId"),
283 TraceLoggingBool(!CurrentProcess,
"Remote"),
284 TraceLoggingPointer(BaseAddress,
"BaseAddress"),
285 TraceLoggingBool(IsImage,
"IsImage"));
287 DebugMessage(
"[Panoptes] %s: executable %s at %p unmapped from the %s process\n",
288 Api, IsImage ?
"image" :
"view", BaseAddress, CurrentProcess ?
"current" :
"remote");
292 ULONG Protection, BOOL Executable, BOOL IsImage)
299 TraceLoggingWrite(g_hHookProvider,
"RemoteViewMapped",
300 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
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"));
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");
317 ULONG OldProtection, ULONG NewProtection)
324 TraceLoggingWrite(g_hHookProvider,
"PEMadeExecutable",
325 TraceLoggingLevel(WINEVENT_LEVEL_WARNING),
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"));
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);
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...
#define HOOK_KEYWORD_DETECTION
#define HOOK_PROVIDER_NAME