Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook Directory Reference

Directories

 
include
 
src
 
test

Detailed Description

Panoptes Hook DLL (`PanoptesDLLx64.dll`, `PanoptesDLLx86.dll`)

The driver injects this DLL early in each new process. It hooks ntdll exports with Microsoft Detours to catch code injection and process tampering, and reports what it sees to PanoptesService over ETW. There is one DLL per architecture: 64-bit processes get the x64 DLL and 32-bit (WOW64) processes the x86 one. See the main README for how the components fit together.

Hooked APIs

Each hook calls the real function first and only reports a successful call, unless noted. "Remote" means the target is a different process than the one the hook runs in.

API Reported when Event
NtWriteVirtualMemory The buffer starts with the MZ magic bytes of a PE image. Reports whether the target is remote and whether the destination memory is executable. PEWritten
The write overlaps the target's PEB page, e.g. process hollowing rewriting ImageBaseAddress. From x64 a WOW64 target's 32-bit PEB counts too. Reports the offset into the PEB. PEBWritten
The write covers the first byte of the entry point code of the target's main image (the one PEB.ImageBaseAddress points at), or its AddressOfEntryPoint header field, e.g. entry point patching of a suspended process. The headers are read after the write, so a rewritten field reports the new entry point. Reports EntryPoint, CodeWritten and FieldWritten. Needs PROCESS_VM_READ on the handle. EntryPointWritten
NtReadVirtualMemory The read overlaps the target's PEB page. Informational: debuggers and tools such as Task Manager read other processes' PEBs routinely. PEBRead
NtProtectVirtualMemory Memory is made executable (PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_WRITECOPY, with any modifiers). Reports the old and new protection. PEMadeExecutable
NtAllocateVirtualMemory, NtAllocateVirtualMemoryEx Memory is allocated with an executable protection. Reports the base and size the kernel returned. ExecutableAllocated
NtMapViewOfSection, NtMapViewOfSectionEx A view is mapped into a remote process. Reports whether any page of the view is executable and whether it is an image. RemoteViewMapped
NtUnmapViewOfSection, NtUnmapViewOfSectionEx A view with any executable page is unmapped, e.g. the first step of process hollowing. The view is queried before the call, since its protection is gone afterwards; the whole view is walked, so an image whose first page is its read-only header still counts. ExecutableUnmapped
NtCreateThreadEx A thread is created in a remote process. Reports the start address and creation flags (e.g. suspended). RemoteThreadCreated
NtQueueApcThread, NtQueueApcThreadEx2 An APC is queued to a thread in a remote process. Reports the routine and flags (0x1 is a special user APC, which runs without the thread waiting alertably). QueueUserAPC goes through NtQueueApcThreadEx2, which passes ntdll's APC dispatcher as the routine. ApcQueued
NtSetInformationProcess Setting ProcessProtectionInformation (class 61) is attempted. Reported whether or not it succeeds: only the kernel can change a process's protection, so the attempt itself is the signal. ProtectionSetAttempt

Exports missing from a process's ntdll (e.g. the Ex variants on older Windows) are skipped.

Events

The hooks report through an ETW TraceLogging provider, Panoptes.Hook ({D9AA2FDD-2853-4A97-A42F-E77E42921704}), at warning level (PEBRead is informational) with the detection keyword 0x1. Every event carries the API, TargetProcessId and Remote; the process the hook ran in is the process ID in the event header, which the writer cannot change. RemoteViewMapped and ExecutableUnmapped also carry IsImage (a MEM_IMAGE view); it is not called Image because the service fills that in with the process's image path. The service consumes the provider through a real-time ETW session (Panoptes Hook Trace) and writes the events to the same log as the driver's events.

Writing an event is a single NtTraceEvent syscall, which is not hooked, so logging from inside a hook cannot recurse. Debug builds also send each event to OutputDebugString as a [Panoptes] message, which the tests check.

Nothing is reported from the thread placing or removing the hooks, since Detours' own protection changes to ntdll would otherwise show up as PEMadeExecutable.

If a process loads more than one copy of the DLL from different folders (the hook tests load their own build while the driver has already injected the installed one, and one test case loads a third), every copy places hooks and the hooks chain. Each copy takes a load generation, and only the newest, whose hooks are the outermost, reports: on load a copy silences the others through their PanoptesSetSilenced export, and on unload it hands reporting to the newest remaining copy. Copies also mark each other's Detours transactions (PanoptesSetTransactionThread), so no copy reports another's patching. Copies must unload newest first, as Detours requires; process exit does that.

Known gaps and noise

Building

The DLLs are built by the main project (BUILD_HOOK, on by default), or on their own with the hook preset, which only installs Detours:

cmake --preset hook
cmake --build build/hook --config Debug

The x64 DLL is built by the main project and the x86 DLL by a Win32 sub-build of this folder; both go to bin/<config>.

Tests

test/ has one test per hooked API (and its Ex variants), built for both architectures, plus Test_PanoptesDLL, which checks the hooks are placed and removed:

cmake --preset hook-tests
cmake --build build/hook --config Debug
.\bin\Debug\tests\Test_NtWriteVirtualMemoryx64.exe

Each test relaunches itself as a debuggee: the child loads the hook DLL and runs the cases, and the parent, as its debugger, captures the [Panoptes] messages and checks each case produced exactly the expected ones. The cases cover both sides of every check: what must be reported, and what must not (local targets, non-executable memory, failed calls). The messages only exist in Debug builds, so other configurations report SKIPPED.