Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook.cpp File Reference
#include "hook.hpp"
#include "hook_trace.hpp"
#include "detours\detours.h"
#include "winternl.h"
#include <stddef.h>

Go to the source code of this file.

Classes

struct  HOOK_ENTRY
 An ntdll export and the hook placed on it. Original receives the export's address and, once attached, points at the Detours trampoline that calls the real function. More...
struct  OTHER_COPY

Macros

#define PANO_PEB_SPAN   0x1000
#define PANO_ENTRY_POINT_FIELD_OFFSET   offsetof(IMAGE_NT_HEADERS32, OptionalHeader.AddressOfEntryPoint)
#define PANO_PROCESS_PROTECTION_INFORMATION   61
#define PANO_MAX_OTHER_COPIES   8
 The most other copies tracked; more than a couple in one process is not expected.

Typedefs

typedef NTSTATUS(NTAPI * pNtCompareObjects) (HANDLE FirstObjectHandle, HANDLE SecondObjectHandle)
typedef NTSTATUS(NTAPI * pNtGetNextThread) (HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAccess, ULONG HandleAttributes, ULONG Flags, PHANDLE NewThreadHandle)
 NtGetNextThread enumerates the threads of a process, opening a handle to each.
typedef VOID(__cdecl * pPanoptesSetSilenced) (BOOL Silenced)
typedef LONG(__cdecl * pPanoptesGeneration) ()
typedef VOID(__cdecl * pPanoptesSetTransactionThread) (DWORD ThreadId)

Functions

NTSTATUS NTAPI Hooked_NtWriteVirtualMemory (HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToWrite, PSIZE_T NumberOfBytesWritten)
 The Hooked_NtWriteVirtualMemory function is a function that hooks the NtWriteVirtualMemory function.
NTSTATUS NTAPI Hooked_NtProtectVirtualMemory (HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG NewProtection, PULONG OldProtection)
 Hooks NtProtectVirtualMemory.
NTSTATUS NTAPI Hooked_NtMapViewOfSection (HANDLE SectionHandle, HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, PANO_SECTION_INHERIT InheritDisposition, ULONG AllocationType, ULONG Win32Protect)
 Hooks NtMapViewOfSection.
NTSTATUS NTAPI Hooked_NtMapViewOfSectionEx (_In_ HANDLE SectionHandle, _In_ HANDLE ProcessHandle, _Inout_ _At_(*BaseAddress, _Readable_bytes_(*ViewSize) _Writable_bytes_(*ViewSize) _Post_readable_byte_size_(*ViewSize)) PVOID *BaseAddress, _Inout_opt_ PLARGE_INTEGER SectionOffset, _Inout_ PSIZE_T ViewSize, _In_ ULONG AllocationType, _In_ ULONG PageProtection, _Inout_updates_opt_(ExtendedParameterCount) PMEM_EXTENDED_PARAMETER ExtendedParameters, _In_ ULONG ExtendedParameterCount)
 The Hooked_NtMapViewOfSectionEx function is a function that hooks the NtMapViewOfSectionEx function.
NTSTATUS NTAPI Hooked_NtUnmapViewOfSection (HANDLE ProcessHandle, PVOID BaseAddress)
 Hooks NtUnmapViewOfSection.
NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx (HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags)
 Hooks NtUnmapViewOfSectionEx.
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemory (HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
 Hooks NtAllocateVirtualMemory.
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemoryEx (HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
 Hooks NtAllocateVirtualMemoryEx.
NTSTATUS NTAPI Hooked_NtReadVirtualMemory (HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToRead, PSIZE_T NumberOfBytesRead)
 Hooks NtReadVirtualMemory.
NTSTATUS NTAPI Hooked_NtCreateThreadEx (PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, HANDLE ProcessHandle, PVOID StartRoutine, PVOID Argument, ULONG CreateFlags, SIZE_T ZeroBits, SIZE_T StackSize, SIZE_T MaximumStackSize, PVOID AttributeList)
 Hooks NtCreateThreadEx.
NTSTATUS NTAPI Hooked_NtQueueApcThread (HANDLE ThreadHandle, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
 Hooks NtQueueApcThread.
NTSTATUS NTAPI Hooked_NtQueueApcThreadEx2 (HANDLE ThreadHandle, HANDLE ReserveHandle, ULONG ApcFlags, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
 Hooks NtQueueApcThreadEx2.
NTSTATUS NTAPI Hooked_NtSetInformationProcess (HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength)
 Hooks NtSetInformationProcess.
BOOL HookLoggingEnabled ()
 Whether the Log_* functions should write. FALSE on the thread running a Detours transaction (so the hooks do not report Detours patching ntdll), and while a second instance of this DLL loaded in the process does the logging instead. Defined in hook.cpp.
 __declspec (dllexport) VOID __cdecl PanoptesSetSilenced(BOOL Silenced)
 Called by another copy of this DLL to stop (TRUE) or resume (FALSE) this copy's logging.
VOID PlaceHooks ()
 The PlaceHooks function places the trampoline hooks on several NTDLL functions. Exports missing from this version of ntdll (e.g. the Ex variants on older Windows) are skipped.
VOID UnHook ()
 The UnHook function removes the trampoline hooks from the NTDLL functions.

Variables

pNtWriteVirtualMemory pOriginal_NtWriteVirtualMemory = NULL
pNtMapViewOfSection pOriginal_NtMapViewOfSection = NULL
pNtMapViewOfSectionEx pOriginal_NtMapViewOfSectionEx = NULL
pNtUnmapViewOfSection pOriginal_NtUnmapViewOfSection = NULL
pNtUnmapViewOfSectionEx pOriginal_NtUnmapViewOfSectionEx = NULL
pNtProtectVirtualMemory pOriginal_NtProtectVirtualMemory = NULL
pNtAllocateVirtualMemory pOriginal_NtAllocateVirtualMemory = NULL
pNtAllocateVirtualMemoryEx pOriginal_NtAllocateVirtualMemoryEx = NULL
pNtReadVirtualMemory pOriginal_NtReadVirtualMemory = NULL
pNtCreateThreadEx pOriginal_NtCreateThreadEx = NULL
pNtQueueApcThread pOriginal_NtQueueApcThread = NULL
pNtQueueApcThreadEx2 pOriginal_NtQueueApcThreadEx2 = NULL
pNtSetInformationProcess pOriginal_NtSetInformationProcess = NULL
IMAGE_DOS_HEADER __ImageBase

Macro Definition Documentation

◆ PANO_ENTRY_POINT_FIELD_OFFSET

#define PANO_ENTRY_POINT_FIELD_OFFSET   offsetof(IMAGE_NT_HEADERS32, OptionalHeader.AddressOfEntryPoint)

The offset of the AddressOfEntryPoint field from the start of the NT headers. It is the same in IMAGE_NT_HEADERS32 and IMAGE_NT_HEADERS64.

Definition at line 282 of file hook.cpp.

◆ PANO_MAX_OTHER_COPIES

#define PANO_MAX_OTHER_COPIES   8

The most other copies tracked; more than a couple in one process is not expected.

Definition at line 897 of file hook.cpp.

◆ PANO_PEB_SPAN

#define PANO_PEB_SPAN   0x1000

The kernel gives each PEB its own page-aligned page, and the PEB is smaller than a page on every supported Windows version, so a write into that page is a write into the PEB.

Definition at line 192 of file hook.cpp.

◆ PANO_PROCESS_PROTECTION_INFORMATION

#define PANO_PROCESS_PROTECTION_INFORMATION   61

The native PROCESSINFOCLASS value for a process's PS_PROTECTION, which winternl.h does not define. Not to be confused with the Win32 ProcessProtectionLevelInfo, a different enum in which 7 means ProcessDebugPort here.

Definition at line 775 of file hook.cpp.

Referenced by Hooked_NtSetInformationProcess().

Typedef Documentation

◆ pNtCompareObjects

typedef NTSTATUS(NTAPI * pNtCompareObjects) (HANDLE FirstObjectHandle, HANDLE SecondObjectHandle)

NtCompareObjects (Windows 10 1607+) tells whether two handles refer to the same kernel object. It needs no access rights on either handle, so it identifies handles that cannot be queried.

Definition at line 107 of file hook.cpp.

◆ pNtGetNextThread

typedef NTSTATUS(NTAPI * pNtGetNextThread) (HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAccess, ULONG HandleAttributes, ULONG Flags, PHANDLE NewThreadHandle)

NtGetNextThread enumerates the threads of a process, opening a handle to each.

Definition at line 110 of file hook.cpp.

◆ pPanoptesGeneration

typedef LONG(__cdecl * pPanoptesGeneration) ()

Definition at line 857 of file hook.cpp.

◆ pPanoptesSetSilenced

typedef VOID(__cdecl * pPanoptesSetSilenced) (BOOL Silenced)

Definition at line 856 of file hook.cpp.

◆ pPanoptesSetTransactionThread

typedef VOID(__cdecl * pPanoptesSetTransactionThread) (DWORD ThreadId)

Definition at line 858 of file hook.cpp.

Function Documentation

◆ __declspec()

__declspec ( dllexport )

Called by another copy of this DLL to stop (TRUE) or resume (FALSE) this copy's logging.

This copy's load generation, for other copies deciding which of them reports.

Called by another copy around its Detours transactions: its patching goes through this copy's hooks too, and must not be reported by whichever copy is reporting. 0 clears it.

Definition at line 866 of file hook.cpp.

867{
868 InterlockedExchange(&g_Silenced, Silenced ? 1 : 0);
869}

◆ Hooked_NtAllocateVirtualMemory()

NTSTATUS NTAPI Hooked_NtAllocateVirtualMemory ( HANDLE ProcessHandle,
PVOID * BaseAddress,
ULONG_PTR ZeroBits,
PSIZE_T RegionSize,
ULONG AllocationType,
ULONG Protection )

Hooks NtAllocateVirtualMemory.

Hooks NtAllocateVirtualMemory, which reserves and/or commits memory in a process.

Parameters
ProcessHandleThe handle to the process.
BaseAddressThe base address of the region.
ZeroBitsThe number of high-order address bits that must be zero.
RegionSizeThe size of the region.
AllocationTypeMEM_* allocation flags.
ProtectionThe page protection.
Returns
The status of the function.

Definition at line 570 of file hook.cpp.

577{
578 NTSTATUS status = pOriginal_NtAllocateVirtualMemory(ProcessHandle, BaseAddress, ZeroBits,
579 RegionSize, AllocationType, Protection);
580
581 if (!NT_SUCCESS(status))
582 {
583 return status;
584 }
585
586 // On success the kernel has written the actual base and rounded-up size back to the caller
587 if (IsExecutableProtection(Protection))
588 {
589 Log_ExecutableAllocated(ProcessHandle, IsCurrentProcess(ProcessHandle), "NtAllocateVirtualMemory",
590 *BaseAddress, *RegionSize, AllocationType, Protection);
591 }
592
593 return status;
594}
pNtAllocateVirtualMemory pOriginal_NtAllocateVirtualMemory
Definition hook.cpp:15
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...

References Log_ExecutableAllocated(), and pOriginal_NtAllocateVirtualMemory.

◆ Hooked_NtAllocateVirtualMemoryEx()

NTSTATUS NTAPI Hooked_NtAllocateVirtualMemoryEx ( HANDLE ProcessHandle,
PVOID * BaseAddress,
PSIZE_T RegionSize,
ULONG AllocationType,
ULONG PageProtection,
PMEM_EXTENDED_PARAMETER ExtendedParameters,
ULONG ExtendedParameterCount )

Hooks NtAllocateVirtualMemoryEx.

Hooks NtAllocateVirtualMemoryEx, which reserves and/or commits memory in a process.

Parameters
ProcessHandleThe handle to the process.
BaseAddressThe base address of the region.
RegionSizeThe size of the region.
AllocationTypeMEM_* allocation flags.
PageProtectionThe page protection.
ExtendedParametersThe extended parameters.
ExtendedParameterCountThe number of extended parameters.
Returns
The status of the function.

Definition at line 605 of file hook.cpp.

613{
614 NTSTATUS status = pOriginal_NtAllocateVirtualMemoryEx(ProcessHandle, BaseAddress, RegionSize,
615 AllocationType, PageProtection, ExtendedParameters, ExtendedParameterCount);
616
617 if (!NT_SUCCESS(status))
618 {
619 return status;
620 }
621
622 if (IsExecutableProtection(PageProtection))
623 {
624 Log_ExecutableAllocated(ProcessHandle, IsCurrentProcess(ProcessHandle), "NtAllocateVirtualMemoryEx",
625 *BaseAddress, *RegionSize, AllocationType, PageProtection);
626 }
627
628 return status;
629}
pNtAllocateVirtualMemoryEx pOriginal_NtAllocateVirtualMemoryEx
Definition hook.cpp:16

References Log_ExecutableAllocated(), and pOriginal_NtAllocateVirtualMemoryEx.

◆ Hooked_NtCreateThreadEx()

NTSTATUS NTAPI Hooked_NtCreateThreadEx ( PHANDLE ThreadHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
HANDLE ProcessHandle,
PVOID StartRoutine,
PVOID Argument,
ULONG CreateFlags,
SIZE_T ZeroBits,
SIZE_T StackSize,
SIZE_T MaximumStackSize,
PVOID AttributeList )

Hooks NtCreateThreadEx.

Hooks NtCreateThreadEx, which creates a thread in a process.

Parameters
ThreadHandleReceives the handle to the new thread.
DesiredAccessThe access requested for the thread handle.
ObjectAttributesThe attributes of the thread object.
ProcessHandleThe handle to the process the thread is created in.
StartRoutineThe address the thread starts executing at.
ArgumentThe argument passed to StartRoutine.
CreateFlagsTHREAD_CREATE_FLAGS_* values.
ZeroBitsThe number of high-order address bits of the stack that must be zero.
StackSizeThe initial committed stack size.
MaximumStackSizeThe maximum stack size.
AttributeListThe PS_ATTRIBUTE_LIST for the thread.
Returns
The status of the function.

Definition at line 675 of file hook.cpp.

687{
688 NTSTATUS status = pOriginal_NtCreateThreadEx(ThreadHandle, DesiredAccess, ObjectAttributes,
689 ProcessHandle, StartRoutine, Argument, CreateFlags, ZeroBits, StackSize, MaximumStackSize,
690 AttributeList);
691
692 if (!NT_SUCCESS(status))
693 {
694 return status;
695 }
696
697 // ThreadHandle is an out-parameter (PHANDLE), and the new handle may lack query access, so the
698 // target is taken from ProcessHandle
699 if (!IsCurrentProcess(ProcessHandle))
700 {
701 Log_RemoteThreadCreated(ProcessHandle, StartRoutine, CreateFlags);
702 }
703
704 return status;
705}
pNtCreateThreadEx pOriginal_NtCreateThreadEx
Definition hook.cpp:18
VOID Log_RemoteThreadCreated(HANDLE ProcessHandle, PVOID StartRoutine, ULONG CreateFlags)
NtCreateThreadEx created a thread in another process. Event "RemoteThreadCreated".

References Log_RemoteThreadCreated(), and pOriginal_NtCreateThreadEx.

◆ Hooked_NtMapViewOfSection()

NTSTATUS NTAPI Hooked_NtMapViewOfSection ( HANDLE SectionHandle,
HANDLE ProcessHandle,
PVOID * BaseAddress,
ULONG_PTR ZeroBits,
SIZE_T CommitSize,
PLARGE_INTEGER SectionOffset,
PSIZE_T ViewSize,
PANO_SECTION_INHERIT InheritDisposition,
ULONG AllocationType,
ULONG Win32Protect )

Hooks NtMapViewOfSection.

Hooks NtMapViewOfSection, which maps a view of a section into a process.

Parameters
SectionHandleThe handle to the section.
ProcessHandleThe handle to the process the view is mapped into.
BaseAddressThe base address of the view.
ZeroBitsThe number of high-order address bits that must be zero.
CommitSizeThe number of bytes to commit initially.
SectionOffsetThe offset of the view within the section.
ViewSizeThe size of the view.
InheritDispositionHow the view is shared with child processes.
AllocationTypeThe allocation type.
Win32ProtectThe page protection.
Returns
The status of the function.

Definition at line 429 of file hook.cpp.

440{
441 NTSTATUS status = pOriginal_NtMapViewOfSection(SectionHandle, ProcessHandle, BaseAddress,
442 ZeroBits, CommitSize, SectionOffset, ViewSize, InheritDisposition, AllocationType,
443 Win32Protect);
444
445 if (!NT_SUCCESS(status))
446 {
447 return status;
448 }
449
450 if (!IsCurrentProcess(ProcessHandle))
451 {
452 // On success the kernel has written the view's actual base and size back to the caller
453 BOOL isImage = FALSE;
454 const BOOL executable = ViewContainsExecutable(ProcessHandle, *BaseAddress, &isImage);
455 Log_RemoteViewMapped(ProcessHandle, "NtMapViewOfSection", *BaseAddress, *ViewSize,
456 Win32Protect, executable, isImage);
457 }
458
459 return status;
460}
pNtMapViewOfSection pOriginal_NtMapViewOfSection
Definition hook.cpp:10
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...

References Log_RemoteViewMapped(), and pOriginal_NtMapViewOfSection.

◆ Hooked_NtMapViewOfSectionEx()

NTSTATUS NTAPI Hooked_NtMapViewOfSectionEx ( _In_ HANDLE SectionHandle,
_In_ HANDLE ProcessHandle,
_Inout_ _At_(*, *ViewSize _Writable_bytes_ *ViewSize _Post_readable_byte_size_ * ViewSize )

The Hooked_NtMapViewOfSectionEx function is a function that hooks the NtMapViewOfSectionEx function.

Parameters
SectionHandleThe handle to the section.
ProcessHandleThe handle to the process.
BaseAddressThe base address of the memory to map.
SectionOffsetThe offset of the section.
ViewSizeThe size of the view.
AllocationTypeThe allocation type.
PageProtectionThe page protection.
ExtendedParametersThe extended parameters.
ExtendedParameterCountThe number of extended parameters.
Returns
The status of the function.

Definition at line 474 of file hook.cpp.

483{
484 NTSTATUS status = pOriginal_NtMapViewOfSectionEx(SectionHandle, ProcessHandle, BaseAddress,
485 SectionOffset, ViewSize, AllocationType, PageProtection, ExtendedParameters,
486 ExtendedParameterCount);
487
488 if (!NT_SUCCESS(status))
489 {
490 return status;
491 }
492
493 if (!IsCurrentProcess(ProcessHandle))
494 {
495 BOOL isImage = FALSE;
496 const BOOL executable = ViewContainsExecutable(ProcessHandle, *BaseAddress, &isImage);
497 Log_RemoteViewMapped(ProcessHandle, "NtMapViewOfSectionEx", *BaseAddress, *ViewSize,
498 PageProtection, executable, isImage);
499 }
500
501 return status;
502}
pNtMapViewOfSectionEx pOriginal_NtMapViewOfSectionEx
Definition hook.cpp:11

References Log_RemoteViewMapped(), and pOriginal_NtMapViewOfSectionEx.

◆ Hooked_NtProtectVirtualMemory()

NTSTATUS NTAPI Hooked_NtProtectVirtualMemory ( HANDLE ProcessHandle,
PVOID * BaseAddress,
PSIZE_T RegionSize,
ULONG NewProtection,
PULONG OldProtection )

Hooks NtProtectVirtualMemory.

Hooks NtProtectVirtualMemory, which changes the protection of memory in a process.

Parameters
ProcessHandleThe handle to the process.
BaseAddressThe base address of the region.
RegionSizeThe size of the region.
NewProtectionThe new page protection.
OldProtectionReceives the previous page protection.
Returns
The status of the function.

Definition at line 393 of file hook.cpp.

399{
400 NTSTATUS status = pOriginal_NtProtectVirtualMemory(ProcessHandle, BaseAddress, RegionSize,
401 NewProtection, OldProtection);
402
403 if (!NT_SUCCESS(status))
404 {
405 return status;
406 }
407
408 if (IsExecutableProtection(NewProtection))
409 {
410 Log_PEMadeExecutable(ProcessHandle, IsCurrentProcess(ProcessHandle), *BaseAddress,
411 (OldProtection != NULL) ? *OldProtection : 0, NewProtection);
412 }
413
414 return status;
415}
pNtProtectVirtualMemory pOriginal_NtProtectVirtualMemory
Definition hook.cpp:14
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".

References Log_PEMadeExecutable(), and pOriginal_NtProtectVirtualMemory.

◆ Hooked_NtQueueApcThread()

NTSTATUS NTAPI Hooked_NtQueueApcThread ( HANDLE ThreadHandle,
PPANO_APC_ROUTINE ApcRoutine,
PVOID ApcArgument1,
PVOID ApcArgument2,
PVOID ApcArgument3 )

Hooks NtQueueApcThread.

Hooks NtQueueApcThread, which queues a user-mode APC to a thread.

Parameters
ThreadHandleThe handle to the thread the APC is queued to.
ApcRoutineThe routine the APC runs.
ApcArgument1The first argument passed to ApcRoutine.
ApcArgument2The second argument passed to ApcRoutine.
ApcArgument3The third argument passed to ApcRoutine.
Returns
The status of the function.

Definition at line 714 of file hook.cpp.

720{
721 NTSTATUS status = pOriginal_NtQueueApcThread(ThreadHandle, ApcRoutine, ApcArgument1,
722 ApcArgument2, ApcArgument3);
723
724 if (!NT_SUCCESS(status))
725 {
726 return status;
727 }
728
729 if (!IsThreadInCurrentProcess(ThreadHandle))
730 {
731 Log_ApcQueued(ThreadHandle, FALSE, "NtQueueApcThread", (PVOID)ApcRoutine, 0);
732 }
733
734 return status;
735}
pNtQueueApcThread pOriginal_NtQueueApcThread
Definition hook.cpp:19
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"....

References Log_ApcQueued(), and pOriginal_NtQueueApcThread.

◆ Hooked_NtQueueApcThreadEx2()

NTSTATUS NTAPI Hooked_NtQueueApcThreadEx2 ( HANDLE ThreadHandle,
HANDLE ReserveHandle,
ULONG ApcFlags,
PPANO_APC_ROUTINE ApcRoutine,
PVOID ApcArgument1,
PVOID ApcArgument2,
PVOID ApcArgument3 )

Hooks NtQueueApcThreadEx2.

Hooks NtQueueApcThreadEx2, which queues a user-mode APC to a thread, optionally as a special user APC. QueueUserAPC and QueueUserAPC2 go through it.

Parameters
ThreadHandleThe handle to the thread the APC is queued to.
ReserveHandleAn optional APC reserve object.
ApcFlagsQUEUE_USER_APC_FLAGS_* values, e.g. 0x1 for a special user APC.
ApcRoutineThe routine the APC runs. For QueueUserAPC this is ntdll's dispatcher, which calls the caller's routine (passed in ApcArgument1).
ApcArgument1The first argument passed to ApcRoutine.
ApcArgument2The second argument passed to ApcRoutine.
ApcArgument3The third argument passed to ApcRoutine.
Returns
The status of the function.

Definition at line 747 of file hook.cpp.

755{
756 NTSTATUS status = pOriginal_NtQueueApcThreadEx2(ThreadHandle, ReserveHandle, ApcFlags, ApcRoutine,
757 ApcArgument1, ApcArgument2, ApcArgument3);
758
759 if (!NT_SUCCESS(status))
760 {
761 return status;
762 }
763
764 if (!IsThreadInCurrentProcess(ThreadHandle))
765 {
766 Log_ApcQueued(ThreadHandle, FALSE, "NtQueueApcThreadEx2", (PVOID)ApcRoutine, ApcFlags);
767 }
768
769 return status;
770}
pNtQueueApcThreadEx2 pOriginal_NtQueueApcThreadEx2
Definition hook.cpp:20

References Log_ApcQueued(), and pOriginal_NtQueueApcThreadEx2.

◆ Hooked_NtReadVirtualMemory()

NTSTATUS NTAPI Hooked_NtReadVirtualMemory ( HANDLE ProcessHandle,
PVOID BaseAddress,
PVOID Buffer,
SIZE_T NumberOfBytesToRead,
PSIZE_T NumberOfBytesRead )

Hooks NtReadVirtualMemory.

Hooks NtReadVirtualMemory, which reads memory from a process.

Parameters
ProcessHandleThe handle to the process.
BaseAddressThe base address of the memory to read.
BufferReceives the memory read.
NumberOfBytesToReadThe number of bytes to read.
NumberOfBytesReadReceives the number of bytes read.
Returns
The status of the function.

Definition at line 638 of file hook.cpp.

644{
645 NTSTATUS status = pOriginal_NtReadVirtualMemory(ProcessHandle, BaseAddress, Buffer,
646 NumberOfBytesToRead, NumberOfBytesRead);
647
648 if (!NT_SUCCESS(status))
649 {
650 return status;
651 }
652
653 ULONG_PTR pebOffset = 0;
654 if (RangeTouchesPEB(ProcessHandle, BaseAddress, NumberOfBytesToRead, &pebOffset))
655 {
656 Log_PEBRead(ProcessHandle, IsCurrentProcess(ProcessHandle), NumberOfBytesToRead, pebOffset);
657 }
658
659 return status;
660}
pNtReadVirtualMemory pOriginal_NtReadVirtualMemory
Definition hook.cpp:17
VOID Log_PEBRead(HANDLE ProcessHandle, BOOL CurrentProcess, SIZE_T Size, ULONG_PTR PebOffset)
NtReadVirtualMemory read from a process's PEB. Event "PEBRead".

References Log_PEBRead(), and pOriginal_NtReadVirtualMemory.

◆ Hooked_NtSetInformationProcess()

NTSTATUS NTAPI Hooked_NtSetInformationProcess ( HANDLE ProcessHandle,
PROCESSINFOCLASS ProcessInformationClass,
PVOID ProcessInformation,
ULONG ProcessInformationLength )

Hooks NtSetInformationProcess.

Hooks NtSetInformationProcess, which sets information about a process.

Parameters
ProcessHandleThe handle to the process.
ProcessInformationClassThe kind of information being set.
ProcessInformationThe information to set.
ProcessInformationLengthThe size of ProcessInformation in bytes.
Returns
The status of the function.

Definition at line 783 of file hook.cpp.

788{
789 NTSTATUS status = pOriginal_NtSetInformationProcess(ProcessHandle, ProcessInformationClass,
790 ProcessInformation, ProcessInformationLength);
791
792 if (ProcessInformationClass == (PROCESSINFOCLASS)PANO_PROCESS_PROTECTION_INFORMATION)
793 {
794 Log_ProtectionSetAttempt(ProcessHandle, IsCurrentProcess(ProcessHandle), status);
795 }
796
797 if (!NT_SUCCESS(status))
798 {
799 return status;
800 }
801
802 return status;
803}
pNtSetInformationProcess pOriginal_NtSetInformationProcess
Definition hook.cpp:21
#define PANO_PROCESS_PROTECTION_INFORMATION
Definition hook.cpp:775
VOID Log_ProtectionSetAttempt(HANDLE ProcessHandle, BOOL CurrentProcess, NTSTATUS Status)
NtSetInformationProcess was called to set a process's protection level (ProcessProtectionInformation)...

References Log_ProtectionSetAttempt(), PANO_PROCESS_PROTECTION_INFORMATION, and pOriginal_NtSetInformationProcess.

◆ Hooked_NtUnmapViewOfSection()

NTSTATUS NTAPI Hooked_NtUnmapViewOfSection ( HANDLE ProcessHandle,
PVOID BaseAddress )

Hooks NtUnmapViewOfSection.

Hooks NtUnmapViewOfSection, which unmaps a view of a section from a process.

Parameters
ProcessHandleThe handle to the process the view is unmapped from.
BaseAddressThe base address of the view.
Returns
The status of the function.

Definition at line 508 of file hook.cpp.

511{
512 // Queried before the call: once unmapped, the view's protection is gone
513 BOOL isImage = FALSE;
514 const BOOL executable = ViewContainsExecutable(ProcessHandle, BaseAddress, &isImage);
515
516 NTSTATUS status = pOriginal_NtUnmapViewOfSection(ProcessHandle, BaseAddress);
517
518 if (!NT_SUCCESS(status))
519 {
520 return status;
521 }
522
523 if (executable)
524 {
525 Log_ExecutableUnmapped(ProcessHandle, IsCurrentProcess(ProcessHandle), "NtUnmapViewOfSection",
526 BaseAddress, isImage);
527 }
528
529 return status;
530}
pNtUnmapViewOfSection pOriginal_NtUnmapViewOfSection
Definition hook.cpp:12
VOID Log_ExecutableUnmapped(HANDLE ProcessHandle, BOOL CurrentProcess, const char *Api, PVOID BaseAddress, BOOL IsImage)
NtUnmapViewOfSection or NtUnmapViewOfSectionEx unmapped a view with executable pages....

References Log_ExecutableUnmapped(), and pOriginal_NtUnmapViewOfSection.

◆ Hooked_NtUnmapViewOfSectionEx()

NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx ( HANDLE ProcessHandle,
PVOID BaseAddress,
ULONG Flags )

Hooks NtUnmapViewOfSectionEx.

Hooks NtUnmapViewOfSectionEx, which unmaps a view of a section from a process.

Parameters
ProcessHandleThe handle to the process the view is unmapped from.
BaseAddressThe base address of the view.
FlagsMEM_UNMAP_* flags.
Returns
The status of the function.

Definition at line 537 of file hook.cpp.

541{
542 // Queried before the call: once unmapped, the view's protection is gone
543 BOOL isImage = FALSE;
544 const BOOL executable = ViewContainsExecutable(ProcessHandle, BaseAddress, &isImage);
545
546 NTSTATUS status = pOriginal_NtUnmapViewOfSectionEx(ProcessHandle, BaseAddress, Flags);
547
548 if (!NT_SUCCESS(status))
549 {
550 return status;
551 }
552
553 if (executable)
554 {
555 Log_ExecutableUnmapped(ProcessHandle, IsCurrentProcess(ProcessHandle), "NtUnmapViewOfSectionEx",
556 BaseAddress, isImage);
557 }
558
559 return status;
560}
pNtUnmapViewOfSectionEx pOriginal_NtUnmapViewOfSectionEx
Definition hook.cpp:13

References Log_ExecutableUnmapped(), and pOriginal_NtUnmapViewOfSectionEx.

◆ Hooked_NtWriteVirtualMemory()

NTSTATUS NTAPI Hooked_NtWriteVirtualMemory ( HANDLE ProcessHandle,
PVOID BaseAddress,
PVOID Buffer,
SIZE_T NumberOfBytesToWrite,
PSIZE_T NumberOfBytesWritten )

The Hooked_NtWriteVirtualMemory function is a function that hooks the NtWriteVirtualMemory function.

Parameters
ProcessHandleThe handle to the process.
BaseAddressThe base address of the memory to write to.
BufferThe buffer to write to the memory.
NumberOfBytesToWriteThe number of bytes to write to the memory.
NumberOfBytesWrittenThe number of bytes written to the memory.
Returns
The status of the function.

Definition at line 339 of file hook.cpp.

345{
346 NTSTATUS status = pOriginal_NtWriteVirtualMemory(ProcessHandle, BaseAddress, Buffer, NumberOfBytesToWrite, NumberOfBytesWritten);
347
348 if (!NT_SUCCESS(status))
349 {
350 return status;
351 }
352
353 BOOL currentProcess = IsCurrentProcess(ProcessHandle);
354 ULONG_PTR pebOffset = 0;
355 if (RangeTouchesPEB(ProcessHandle, BaseAddress, NumberOfBytesToWrite, &pebOffset))
356 {
357
358 Log_PEBWritten(ProcessHandle, currentProcess, NumberOfBytesToWrite, pebOffset);
359 }
360
361 // Checked after the write, so a write that replaces AddressOfEntryPoint reports the new entry
362 // point. Patching the first byte of the entry point code, or redirecting the field, makes the
363 // process run injected code when its main thread starts.
364 ULONG_PTR entryPoint = 0;
365 ULONG_PTR entryPointField = 0;
366 if (GetMainImageEntryPoint(ProcessHandle, &entryPoint, &entryPointField))
367 {
368 const BOOL codeWritten = entryPoint != 0 &&
369 RangesOverlap((ULONG_PTR)BaseAddress, NumberOfBytesToWrite, entryPoint, 1);
370 const BOOL fieldWritten = RangesOverlap((ULONG_PTR)BaseAddress, NumberOfBytesToWrite, entryPointField, sizeof(DWORD));
371 if (codeWritten || fieldWritten)
372 {
373 Log_EntryPointWritten(ProcessHandle, currentProcess, BaseAddress, NumberOfBytesToWrite,
374 (PVOID)entryPoint, codeWritten, fieldWritten);
375 }
376 }
377
378 if (ContainsDOSSignature((const BYTE *)Buffer, NumberOfBytesToWrite))
379 {
380 Log_PEWritten(ProcessHandle, currentProcess, BaseAddress, IsExecutableMemory(ProcessHandle, BaseAddress));
381 }
382
383 return status;
384}
pNtWriteVirtualMemory pOriginal_NtWriteVirtualMemory
Definition hook.cpp:9
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 Log_PEWritten(HANDLE ProcessHandle, BOOL CurrentProcess, PVOID BaseAddress, BOOL Executable)
NtWriteVirtualMemory wrote a PE image. Event "PEWritten".

References Log_EntryPointWritten(), Log_PEBWritten(), Log_PEWritten(), and pOriginal_NtWriteVirtualMemory.

◆ HookLoggingEnabled()

BOOL HookLoggingEnabled ( )

Whether the Log_* functions should write. FALSE on the thread running a Detours transaction (so the hooks do not report Detours patching ntdll), and while a second instance of this DLL loaded in the process does the logging instead. Defined in hook.cpp.

Definition at line 860 of file hook.cpp.

861{
862 return g_Silenced == 0 && GetCurrentThreadId() != g_TransactionThreadId;
863}

Referenced by Log_ApcQueued(), Log_EntryPointWritten(), Log_ExecutableAllocated(), Log_ExecutableUnmapped(), Log_PEBRead(), Log_PEBWritten(), Log_PEMadeExecutable(), Log_PEWritten(), Log_ProtectionSetAttempt(), Log_RemoteThreadCreated(), and Log_RemoteViewMapped().

◆ PlaceHooks()

VOID PlaceHooks ( )

The PlaceHooks function places the trampoline hooks on several NTDLL functions. Exports missing from this version of ntdll (e.g. the Ex variants on older Windows) are skipped.

The PlaceHooks function places the hooks on the NTDLL functions.

Definition at line 1019 of file hook.cpp.

1020{
1021 HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
1022 g_NtQueryInformationProcess = (decltype(&NtQueryInformationProcess))GetProcAddress(ntdll, "NtQueryInformationProcess");
1023 g_NtCompareObjects = (pNtCompareObjects)GetProcAddress(ntdll, "NtCompareObjects");
1024 g_NtGetNextThread = (pNtGetNextThread)GetProcAddress(ntdll, "NtGetNextThread");
1025 g_SelfProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
1026
1027 // This copy's hooks will wrap every other copy's, so this copy reports and the others go
1028 // quiet. Done before the transaction, so the others do not report Detours' patching either
1029 TakeOverReporting();
1030
1031 g_TransactionThreadId = GetCurrentThreadId();
1032 SetTransactionThreadInOtherCopies(g_TransactionThreadId);
1033 DetourTransactionBegin();
1034 DetourUpdateThread(GetCurrentThread());
1035
1036 for (HOOK_ENTRY &hook : g_Hooks)
1037 {
1038 *hook.Original = (PVOID)GetProcAddress(ntdll, hook.Name);
1039 if (*hook.Original != NULL)
1040 {
1041 DetourAttach(hook.Original, hook.Hook);
1042 }
1043 }
1044
1045 DetourTransactionCommit();
1046 g_TransactionThreadId = 0;
1047 SetTransactionThreadInOtherCopies(0);
1048 return;
1049}
NTSTATUS(NTAPI * pNtCompareObjects)(HANDLE FirstObjectHandle, HANDLE SecondObjectHandle)
Definition hook.cpp:107
NTSTATUS(NTAPI * pNtGetNextThread)(HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAccess, ULONG HandleAttributes, ULONG Flags, PHANDLE NewThreadHandle)
NtGetNextThread enumerates the threads of a process, opening a handle to each.
Definition hook.cpp:110
An ntdll export and the hook placed on it. Original receives the export's address and,...
Definition hook.cpp:810

Referenced by DllMain().

◆ UnHook()

VOID UnHook ( )

The UnHook function removes the trampoline hooks from the NTDLL functions.

The UnHook function removes the hooks from the NTDLL functions.

Definition at line 1052 of file hook.cpp.

1053{
1054 g_TransactionThreadId = GetCurrentThreadId();
1055 SetTransactionThreadInOtherCopies(g_TransactionThreadId);
1056 DetourTransactionBegin();
1057 DetourUpdateThread(GetCurrentThread());
1058
1059 for (HOOK_ENTRY &hook : g_Hooks)
1060 {
1061 if (*hook.Original != NULL)
1062 {
1063 DetourDetach(hook.Original, hook.Hook);
1064 }
1065 }
1066
1067 DetourTransactionCommit();
1068
1069 // After the transaction, so the next copy does not report Detours removing these hooks
1070 SetTransactionThreadInOtherCopies(0);
1071 HandOverReporting();
1072
1073 if (g_SelfProcess != NULL)
1074 {
1075 CloseHandle(g_SelfProcess);
1076 g_SelfProcess = NULL;
1077 }
1078 // Left set: the process is exiting or the DLL is unloading, and nothing this thread does from
1079 // here on should be reported by this copy
1080 return;
1081}

Referenced by DllMain().

Variable Documentation

◆ __ImageBase

IMAGE_DOS_HEADER __ImageBase

Definition at line 835 of file hook.cpp.

◆ pOriginal_NtAllocateVirtualMemory

pNtAllocateVirtualMemory pOriginal_NtAllocateVirtualMemory = NULL

Definition at line 15 of file hook.cpp.

Referenced by Hooked_NtAllocateVirtualMemory().

◆ pOriginal_NtAllocateVirtualMemoryEx

pNtAllocateVirtualMemoryEx pOriginal_NtAllocateVirtualMemoryEx = NULL

Definition at line 16 of file hook.cpp.

Referenced by Hooked_NtAllocateVirtualMemoryEx().

◆ pOriginal_NtCreateThreadEx

pNtCreateThreadEx pOriginal_NtCreateThreadEx = NULL

Definition at line 18 of file hook.cpp.

Referenced by Hooked_NtCreateThreadEx().

◆ pOriginal_NtMapViewOfSection

pNtMapViewOfSection pOriginal_NtMapViewOfSection = NULL

Definition at line 10 of file hook.cpp.

Referenced by Hooked_NtMapViewOfSection().

◆ pOriginal_NtMapViewOfSectionEx

pNtMapViewOfSectionEx pOriginal_NtMapViewOfSectionEx = NULL

Definition at line 11 of file hook.cpp.

Referenced by Hooked_NtMapViewOfSectionEx().

◆ pOriginal_NtProtectVirtualMemory

pNtProtectVirtualMemory pOriginal_NtProtectVirtualMemory = NULL

Definition at line 14 of file hook.cpp.

Referenced by Hooked_NtProtectVirtualMemory().

◆ pOriginal_NtQueueApcThread

pNtQueueApcThread pOriginal_NtQueueApcThread = NULL

Definition at line 19 of file hook.cpp.

Referenced by Hooked_NtQueueApcThread().

◆ pOriginal_NtQueueApcThreadEx2

pNtQueueApcThreadEx2 pOriginal_NtQueueApcThreadEx2 = NULL

Definition at line 20 of file hook.cpp.

Referenced by Hooked_NtQueueApcThreadEx2().

◆ pOriginal_NtReadVirtualMemory

pNtReadVirtualMemory pOriginal_NtReadVirtualMemory = NULL

Definition at line 17 of file hook.cpp.

Referenced by Hooked_NtReadVirtualMemory().

◆ pOriginal_NtSetInformationProcess

pNtSetInformationProcess pOriginal_NtSetInformationProcess = NULL

Definition at line 21 of file hook.cpp.

Referenced by Hooked_NtSetInformationProcess().

◆ pOriginal_NtUnmapViewOfSection

pNtUnmapViewOfSection pOriginal_NtUnmapViewOfSection = NULL

Definition at line 12 of file hook.cpp.

Referenced by Hooked_NtUnmapViewOfSection().

◆ pOriginal_NtUnmapViewOfSectionEx

pNtUnmapViewOfSectionEx pOriginal_NtUnmapViewOfSectionEx = NULL

Definition at line 13 of file hook.cpp.

Referenced by Hooked_NtUnmapViewOfSectionEx().

◆ pOriginal_NtWriteVirtualMemory

pNtWriteVirtualMemory pOriginal_NtWriteVirtualMemory = NULL

Definition at line 9 of file hook.cpp.

Referenced by Hooked_NtWriteVirtualMemory().