![]() |
Panoptes 1.0.0
Endpoint Detection and Response
|
#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. | |
| #define PANO_ENTRY_POINT_FIELD_OFFSET offsetof(IMAGE_NT_HEADERS32, OptionalHeader.AddressOfEntryPoint) |
| #define PANO_MAX_OTHER_COPIES 8 |
| #define PANO_PEB_SPAN 0x1000 |
| #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 NTSTATUS(NTAPI * pNtCompareObjects) (HANDLE FirstObjectHandle, HANDLE SecondObjectHandle) |
| typedef NTSTATUS(NTAPI * pNtGetNextThread) (HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAccess, ULONG HandleAttributes, ULONG Flags, PHANDLE NewThreadHandle) |
| typedef VOID(__cdecl * pPanoptesSetSilenced) (BOOL Silenced) |
| typedef VOID(__cdecl * pPanoptesSetTransactionThread) (DWORD ThreadId) |
| __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.
| 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.
| ProcessHandle | The handle to the process. |
| BaseAddress | The base address of the region. |
| ZeroBits | The number of high-order address bits that must be zero. |
| RegionSize | The size of the region. |
| AllocationType | MEM_* allocation flags. |
| Protection | The page protection. |
Definition at line 570 of file hook.cpp.
References Log_ExecutableAllocated(), and pOriginal_NtAllocateVirtualMemory.
| 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.
| ProcessHandle | The handle to the process. |
| BaseAddress | The base address of the region. |
| RegionSize | The size of the region. |
| AllocationType | MEM_* allocation flags. |
| PageProtection | The page protection. |
| ExtendedParameters | The extended parameters. |
| ExtendedParameterCount | The number of extended parameters. |
Definition at line 605 of file hook.cpp.
References Log_ExecutableAllocated(), and pOriginal_NtAllocateVirtualMemoryEx.
| 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.
| ThreadHandle | Receives the handle to the new thread. |
| DesiredAccess | The access requested for the thread handle. |
| ObjectAttributes | The attributes of the thread object. |
| ProcessHandle | The handle to the process the thread is created in. |
| StartRoutine | The address the thread starts executing at. |
| Argument | The argument passed to StartRoutine. |
| CreateFlags | THREAD_CREATE_FLAGS_* values. |
| ZeroBits | The number of high-order address bits of the stack that must be zero. |
| StackSize | The initial committed stack size. |
| MaximumStackSize | The maximum stack size. |
| AttributeList | The PS_ATTRIBUTE_LIST for the thread. |
Definition at line 675 of file hook.cpp.
References Log_RemoteThreadCreated(), and pOriginal_NtCreateThreadEx.
| 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.
| SectionHandle | The handle to the section. |
| ProcessHandle | The handle to the process the view is mapped into. |
| BaseAddress | The base address of the view. |
| ZeroBits | The number of high-order address bits that must be zero. |
| CommitSize | The number of bytes to commit initially. |
| SectionOffset | The offset of the view within the section. |
| ViewSize | The size of the view. |
| InheritDisposition | How the view is shared with child processes. |
| AllocationType | The allocation type. |
| Win32Protect | The page protection. |
Definition at line 429 of file hook.cpp.
References Log_RemoteViewMapped(), and pOriginal_NtMapViewOfSection.
| 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.
| SectionHandle | The handle to the section. |
| ProcessHandle | The handle to the process. |
| BaseAddress | The base address of the memory to map. |
| SectionOffset | The offset of the section. |
| ViewSize | The size of the view. |
| AllocationType | The allocation type. |
| PageProtection | The page protection. |
| ExtendedParameters | The extended parameters. |
| ExtendedParameterCount | The number of extended parameters. |
Definition at line 474 of file hook.cpp.
References Log_RemoteViewMapped(), and pOriginal_NtMapViewOfSectionEx.
| 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.
| ProcessHandle | The handle to the process. |
| BaseAddress | The base address of the region. |
| RegionSize | The size of the region. |
| NewProtection | The new page protection. |
| OldProtection | Receives the previous page protection. |
Definition at line 393 of file hook.cpp.
References Log_PEMadeExecutable(), and pOriginal_NtProtectVirtualMemory.
| 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.
| ThreadHandle | The handle to the thread the APC is queued to. |
| ApcRoutine | The routine the APC runs. |
| ApcArgument1 | The first argument passed to ApcRoutine. |
| ApcArgument2 | The second argument passed to ApcRoutine. |
| ApcArgument3 | The third argument passed to ApcRoutine. |
Definition at line 714 of file hook.cpp.
References Log_ApcQueued(), and pOriginal_NtQueueApcThread.
| 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.
| ThreadHandle | The handle to the thread the APC is queued to. |
| ReserveHandle | An optional APC reserve object. |
| ApcFlags | QUEUE_USER_APC_FLAGS_* values, e.g. 0x1 for a special user APC. |
| ApcRoutine | The routine the APC runs. For QueueUserAPC this is ntdll's dispatcher, which calls the caller's routine (passed in ApcArgument1). |
| ApcArgument1 | The first argument passed to ApcRoutine. |
| ApcArgument2 | The second argument passed to ApcRoutine. |
| ApcArgument3 | The third argument passed to ApcRoutine. |
Definition at line 747 of file hook.cpp.
References Log_ApcQueued(), and pOriginal_NtQueueApcThreadEx2.
| 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.
| ProcessHandle | The handle to the process. |
| BaseAddress | The base address of the memory to read. |
| Buffer | Receives the memory read. |
| NumberOfBytesToRead | The number of bytes to read. |
| NumberOfBytesRead | Receives the number of bytes read. |
Definition at line 638 of file hook.cpp.
References Log_PEBRead(), and pOriginal_NtReadVirtualMemory.
| NTSTATUS NTAPI Hooked_NtSetInformationProcess | ( | HANDLE | ProcessHandle, |
| PROCESSINFOCLASS | ProcessInformationClass, | ||
| PVOID | ProcessInformation, | ||
| ULONG | ProcessInformationLength ) |
Hooks NtSetInformationProcess.
Hooks NtSetInformationProcess, which sets information about a process.
| ProcessHandle | The handle to the process. |
| ProcessInformationClass | The kind of information being set. |
| ProcessInformation | The information to set. |
| ProcessInformationLength | The size of ProcessInformation in bytes. |
Definition at line 783 of file hook.cpp.
References Log_ProtectionSetAttempt(), PANO_PROCESS_PROTECTION_INFORMATION, and pOriginal_NtSetInformationProcess.
| NTSTATUS NTAPI Hooked_NtUnmapViewOfSection | ( | HANDLE | ProcessHandle, |
| PVOID | BaseAddress ) |
Hooks NtUnmapViewOfSection.
Hooks NtUnmapViewOfSection, which unmaps a view of a section from a process.
| ProcessHandle | The handle to the process the view is unmapped from. |
| BaseAddress | The base address of the view. |
Definition at line 508 of file hook.cpp.
References Log_ExecutableUnmapped(), and pOriginal_NtUnmapViewOfSection.
| NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx | ( | HANDLE | ProcessHandle, |
| PVOID | BaseAddress, | ||
| ULONG | Flags ) |
Hooks NtUnmapViewOfSectionEx.
Hooks NtUnmapViewOfSectionEx, which unmaps a view of a section from a process.
| ProcessHandle | The handle to the process the view is unmapped from. |
| BaseAddress | The base address of the view. |
| Flags | MEM_UNMAP_* flags. |
Definition at line 537 of file hook.cpp.
References Log_ExecutableUnmapped(), and pOriginal_NtUnmapViewOfSectionEx.
| 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.
| ProcessHandle | The handle to the process. |
| BaseAddress | The base address of the memory to write to. |
| Buffer | The buffer to write to the memory. |
| NumberOfBytesToWrite | The number of bytes to write to the memory. |
| NumberOfBytesWritten | The number of bytes written to the memory. |
Definition at line 339 of file hook.cpp.
References Log_EntryPointWritten(), Log_PEBWritten(), Log_PEWritten(), and pOriginal_NtWriteVirtualMemory.
| 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.
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().
| 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.
Referenced by DllMain().
| 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.
Referenced by DllMain().
| pNtAllocateVirtualMemory pOriginal_NtAllocateVirtualMemory = NULL |
Definition at line 15 of file hook.cpp.
Referenced by Hooked_NtAllocateVirtualMemory().
| pNtAllocateVirtualMemoryEx pOriginal_NtAllocateVirtualMemoryEx = NULL |
Definition at line 16 of file hook.cpp.
Referenced by Hooked_NtAllocateVirtualMemoryEx().
| pNtCreateThreadEx pOriginal_NtCreateThreadEx = NULL |
Definition at line 18 of file hook.cpp.
Referenced by Hooked_NtCreateThreadEx().
| pNtMapViewOfSection pOriginal_NtMapViewOfSection = NULL |
Definition at line 10 of file hook.cpp.
Referenced by Hooked_NtMapViewOfSection().
| pNtMapViewOfSectionEx pOriginal_NtMapViewOfSectionEx = NULL |
Definition at line 11 of file hook.cpp.
Referenced by Hooked_NtMapViewOfSectionEx().
| pNtProtectVirtualMemory pOriginal_NtProtectVirtualMemory = NULL |
Definition at line 14 of file hook.cpp.
Referenced by Hooked_NtProtectVirtualMemory().
| pNtQueueApcThread pOriginal_NtQueueApcThread = NULL |
Definition at line 19 of file hook.cpp.
Referenced by Hooked_NtQueueApcThread().
| pNtQueueApcThreadEx2 pOriginal_NtQueueApcThreadEx2 = NULL |
Definition at line 20 of file hook.cpp.
Referenced by Hooked_NtQueueApcThreadEx2().
| pNtReadVirtualMemory pOriginal_NtReadVirtualMemory = NULL |
Definition at line 17 of file hook.cpp.
Referenced by Hooked_NtReadVirtualMemory().
| pNtSetInformationProcess pOriginal_NtSetInformationProcess = NULL |
Definition at line 21 of file hook.cpp.
Referenced by Hooked_NtSetInformationProcess().
| pNtUnmapViewOfSection pOriginal_NtUnmapViewOfSection = NULL |
Definition at line 12 of file hook.cpp.
Referenced by Hooked_NtUnmapViewOfSection().
| pNtUnmapViewOfSectionEx pOriginal_NtUnmapViewOfSectionEx = NULL |
Definition at line 13 of file hook.cpp.
Referenced by Hooked_NtUnmapViewOfSectionEx().
| pNtWriteVirtualMemory pOriginal_NtWriteVirtualMemory = NULL |
Definition at line 9 of file hook.cpp.
Referenced by Hooked_NtWriteVirtualMemory().