Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook.hpp File Reference
#include <Windows.h>
#include "def.h"
#include <detours/detours.h>

Go to the source code of this file.

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_NtModifyBootEntry (PBOOT_ENTRY BootEntry)
 The Hooked_NtModifyBootEntry function is a function that hooks the NtModifyBootEntry function.
NTSTATUS NTAPI Hooked_NtMapViewOfSectionEx (HANDLE SectionHandle, HANDLE ProcessHandle, PVOID *BaseAddress, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
 The Hooked_NtMapViewOfSectionEx function is a function that hooks the NtMapViewOfSectionEx function.
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, which maps a view of a section into a process.
NTSTATUS NTAPI Hooked_NtUnmapViewOfSection (HANDLE ProcessHandle, PVOID BaseAddress)
 Hooks NtUnmapViewOfSection, which unmaps a view of a section from a process.
NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx (HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags)
 Hooks NtUnmapViewOfSectionEx, which unmaps a view of a section from a process.
NTSTATUS NTAPI Hooked_NtProtectVirtualMemory (HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG NewProtection, PULONG OldProtection)
 Hooks NtProtectVirtualMemory, which changes the protection of memory in a process.
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemory (HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
 Hooks NtAllocateVirtualMemory, which reserves and/or commits memory in a process.
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemoryEx (HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
 Hooks NtAllocateVirtualMemoryEx, which reserves and/or commits memory in a process.
NTSTATUS NTAPI Hooked_NtReadVirtualMemory (HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToRead, PSIZE_T NumberOfBytesRead)
 Hooks NtReadVirtualMemory, which reads memory from a process.
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, which creates a thread in a process.
NTSTATUS NTAPI Hooked_NtQueueApcThread (HANDLE ThreadHandle, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
 Hooks NtQueueApcThread, which queues a user-mode APC to a thread.
NTSTATUS NTAPI Hooked_NtQueueApcThreadEx2 (HANDLE ThreadHandle, HANDLE ReserveHandle, ULONG ApcFlags, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
 Hooks NtQueueApcThreadEx2, which queues a user-mode APC to a thread, optionally as a special user APC. QueueUserAPC and QueueUserAPC2 go through it.
NTSTATUS NTAPI Hooked_NtSetInformationProcess (HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength)
 Hooks NtSetInformationProcess, which sets information about a process.
VOID PlaceHooks ()
 The PlaceHooks function places the hooks on the NTDLL functions.
VOID UnHook ()
 The UnHook function removes the hooks from the NTDLL functions.

Function Documentation

◆ Hooked_NtAllocateVirtualMemory()

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

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

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, which reserves and/or commits memory in a process.

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, which creates a thread in a process.

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, which maps a view of a section into a process.

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 ( HANDLE SectionHandle,
HANDLE ProcessHandle,
PVOID * BaseAddress,
PLARGE_INTEGER SectionOffset,
PSIZE_T ViewSize,
ULONG AllocationType,
ULONG PageProtection,
PMEM_EXTENDED_PARAMETER ExtendedParameters,
ULONG ExtendedParameterCount )

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 type of allocation.
PageProtectionThe protection of the page.
ExtendedParametersThe extended parameters.
ExtendedParameterCountThe number of extended parameters.
Returns
The status of the function.

◆ Hooked_NtModifyBootEntry()

NTSTATUS NTAPI Hooked_NtModifyBootEntry ( PBOOT_ENTRY BootEntry)

The Hooked_NtModifyBootEntry function is a function that hooks the NtModifyBootEntry function.

Parameters
BootEntryThe boot entry to modify.
Returns
The status of the function.

◆ Hooked_NtProtectVirtualMemory()

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

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

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, which queues a user-mode APC to a thread.

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, which queues a user-mode APC to a thread, optionally as a special user APC. QueueUserAPC and QueueUserAPC2 go through it.

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, which reads memory from a process.

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, which sets information about a process.

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, which unmaps a view of a section from a process.

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, which unmaps a view of a section from a process.

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.
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.

◆ PlaceHooks()

VOID PlaceHooks ( )

The PlaceHooks function places the hooks on the NTDLL functions.

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 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().