3#include "detours\detours.h"
7#pragma region Hooked Functions
27static BOOL ContainsDOSSignature(
const BYTE *Buffer, SIZE_T Size)
29 if (Buffer == NULL || Size <
sizeof(WORD))
36 return *(
const WORD *)Buffer == IMAGE_DOS_SIGNATURE;
38 __except (EXCEPTION_EXECUTE_HANDLER)
48static BOOL IsExecutableProtection(ULONG Protection)
50 return (Protection & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0;
58static BOOL IsExecutableMemory(HANDLE ProcessHandle, PVOID Address)
60 MEMORY_BASIC_INFORMATION mbi{};
61 if (VirtualQueryEx(ProcessHandle, Address, &mbi,
sizeof(mbi)) !=
sizeof(mbi))
65 return mbi.State == MEM_COMMIT && IsExecutableProtection(mbi.Protect);
76static BOOL ViewContainsExecutable(HANDLE ProcessHandle, PVOID Address, BOOL *IsImage)
80 MEMORY_BASIC_INFORMATION mbi{};
81 if (VirtualQueryEx(ProcessHandle, Address, &mbi,
sizeof(mbi)) !=
sizeof(mbi) || mbi.State == MEM_FREE)
85 *IsImage = (mbi.Type == MEM_IMAGE);
89 PVOID allocationBase = mbi.AllocationBase;
90 BYTE *next = (BYTE *)allocationBase;
91 while (VirtualQueryEx(ProcessHandle, next, &mbi,
sizeof(mbi)) ==
sizeof(mbi) &&
92 mbi.AllocationBase == allocationBase)
94 if (mbi.State == MEM_COMMIT && IsExecutableProtection(mbi.Protect))
98 next = (BYTE *)mbi.BaseAddress + mbi.RegionSize;
103#pragma region Handle identity
110typedef NTSTATUS(NTAPI *
pNtGetNextThread)(HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAccess,
111 ULONG HandleAttributes, ULONG Flags, PHANDLE NewThreadHandle);
120static HANDLE g_SelfProcess = NULL;
126static BOOL IsCurrentProcess(HANDLE ProcessHandle)
128 if (ProcessHandle == GetCurrentProcess())
134 const DWORD processId = GetProcessId(ProcessHandle);
137 return processId == GetCurrentProcessId();
140 return g_NtCompareObjects != NULL && g_SelfProcess != NULL &&
141 NT_SUCCESS(g_NtCompareObjects(ProcessHandle, g_SelfProcess));
150static BOOL IsThreadInCurrentProcess(HANDLE ThreadHandle)
152 if (ThreadHandle == GetCurrentThread())
158 const DWORD processId = GetProcessIdOfThread(ThreadHandle);
161 return processId == GetCurrentProcessId();
164 if (g_NtCompareObjects == NULL || g_NtGetNextThread == NULL)
170 HANDLE thread = NULL;
172 while (!found && NT_SUCCESS(g_NtGetNextThread(GetCurrentProcess(), thread, SYNCHRONIZE, 0, 0, &next)))
179 found = NT_SUCCESS(g_NtCompareObjects(ThreadHandle, thread));
192#define PANO_PEB_SPAN 0x1000
196static decltype(&NtQueryInformationProcess) g_NtQueryInformationProcess = NULL;
204static VOID GetPebAddresses(HANDLE ProcessHandle, ULONG_PTR Pebs[2])
206 Pebs[0] = Pebs[1] = 0;
208 if (ProcessHandle == GetCurrentProcess())
210 Pebs[0] = (ULONG_PTR)NtCurrentTeb()->ProcessEnvironmentBlock;
214 if (g_NtQueryInformationProcess == NULL)
219 PROCESS_BASIC_INFORMATION info{};
220 if (NT_SUCCESS(g_NtQueryInformationProcess(ProcessHandle, ProcessBasicInformation, &info,
sizeof(info), NULL)))
222 Pebs[0] = (ULONG_PTR)info.PebBaseAddress;
228 if (NT_SUCCESS(g_NtQueryInformationProcess(ProcessHandle, ProcessWow64Information, &peb32,
sizeof(peb32), NULL)))
241static BOOL RangeTouchesPEB(HANDLE ProcessHandle, PVOID Address, SIZE_T Size, ULONG_PTR *PebOffset)
244 GetPebAddresses(ProcessHandle, pebs);
246 const ULONG_PTR start = (ULONG_PTR)Address;
247 const ULONG_PTR end = (Size > MAXULONG_PTR - start) ? MAXULONG_PTR : start + Size;
249 for (ULONG_PTR peb : pebs)
251 if (peb != 0 && start < peb + PANO_PEB_SPAN && end > peb)
253 *PebOffset = (start > peb) ? start - peb : 0;
262static BOOL RangesOverlap(ULONG_PTR Start, SIZE_T Size, ULONG_PTR OtherStart, SIZE_T OtherSize)
264 const ULONG_PTR end = (Size > MAXULONG_PTR - Start) ? MAXULONG_PTR : Start + Size;
265 const ULONG_PTR otherEnd = (OtherSize > MAXULONG_PTR - OtherStart) ? MAXULONG_PTR : OtherStart + OtherSize;
266 return Start < otherEnd && end > OtherStart;
272static BOOL ReadTargetMemory(HANDLE ProcessHandle, ULONG_PTR Address, PVOID Buffer, SIZE_T Size)
282#define PANO_ENTRY_POINT_FIELD_OFFSET offsetof(IMAGE_NT_HEADERS32, OptionalHeader.AddressOfEntryPoint)
292static BOOL GetMainImageEntryPoint(HANDLE ProcessHandle, ULONG_PTR *EntryPoint, ULONG_PTR *EntryPointField)
295 GetPebAddresses(ProcessHandle, pebs);
302 ULONG_PTR imageBase = 0;
303 if (!ReadTargetMemory(ProcessHandle, pebs[0] + offsetof(PEB, Reserved3) +
sizeof(PVOID), &imageBase,
sizeof(imageBase)) ||
309 IMAGE_DOS_HEADER dos{};
310 if (!ReadTargetMemory(ProcessHandle, imageBase, &dos,
sizeof(dos)) || dos.e_magic != IMAGE_DOS_SIGNATURE ||
317 const ULONG_PTR ntHeaders = imageBase + (ULONG_PTR)dos.e_lfanew;
318 IMAGE_NT_HEADERS32 nt{};
320 nt.Signature != IMAGE_NT_SIGNATURE ||
321 (nt.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC && nt.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC))
326 *EntryPoint = (nt.OptionalHeader.AddressOfEntryPoint != 0) ? imageBase + nt.OptionalHeader.AddressOfEntryPoint : 0;
340 HANDLE ProcessHandle,
343 SIZE_T NumberOfBytesToWrite,
344 PSIZE_T NumberOfBytesWritten)
348 if (!NT_SUCCESS(status))
353 BOOL currentProcess = IsCurrentProcess(ProcessHandle);
354 ULONG_PTR pebOffset = 0;
355 if (RangeTouchesPEB(ProcessHandle, BaseAddress, NumberOfBytesToWrite, &pebOffset))
358 Log_PEBWritten(ProcessHandle, currentProcess, NumberOfBytesToWrite, pebOffset);
364 ULONG_PTR entryPoint = 0;
365 ULONG_PTR entryPointField = 0;
366 if (GetMainImageEntryPoint(ProcessHandle, &entryPoint, &entryPointField))
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)
374 (PVOID)entryPoint, codeWritten, fieldWritten);
378 if (ContainsDOSSignature((
const BYTE *)Buffer, NumberOfBytesToWrite))
380 Log_PEWritten(ProcessHandle, currentProcess, BaseAddress, IsExecutableMemory(ProcessHandle, BaseAddress));
394 HANDLE ProcessHandle,
398 PULONG OldProtection)
401 NewProtection, OldProtection);
403 if (!NT_SUCCESS(status))
408 if (IsExecutableProtection(NewProtection))
411 (OldProtection != NULL) ? *OldProtection : 0, NewProtection);
430 HANDLE SectionHandle,
431 HANDLE ProcessHandle,
435 PLARGE_INTEGER SectionOffset,
438 ULONG AllocationType,
442 ZeroBits, CommitSize, SectionOffset, ViewSize, InheritDisposition, AllocationType,
445 if (!NT_SUCCESS(status))
450 if (!IsCurrentProcess(ProcessHandle))
453 BOOL isImage = FALSE;
454 const BOOL executable = ViewContainsExecutable(ProcessHandle, *BaseAddress, &isImage);
456 Win32Protect, executable, isImage);
475 _In_ HANDLE ProcessHandle,
476 _Inout_ _At_(*BaseAddress, _Readable_bytes_(*ViewSize) _Writable_bytes_(*ViewSize) _Post_readable_byte_size_(*ViewSize)) PVOID *BaseAddress,
477 _Inout_opt_ PLARGE_INTEGER SectionOffset,
478 _Inout_ PSIZE_T ViewSize,
479 _In_ ULONG AllocationType,
480 _In_ ULONG PageProtection,
481 _Inout_updates_opt_(ExtendedParameterCount) PMEM_EXTENDED_PARAMETER ExtendedParameters,
482 _In_ ULONG ExtendedParameterCount)
485 SectionOffset, ViewSize, AllocationType, PageProtection, ExtendedParameters,
486 ExtendedParameterCount);
488 if (!NT_SUCCESS(status))
493 if (!IsCurrentProcess(ProcessHandle))
495 BOOL isImage = FALSE;
496 const BOOL executable = ViewContainsExecutable(ProcessHandle, *BaseAddress, &isImage);
498 PageProtection, executable, isImage);
509 HANDLE ProcessHandle,
513 BOOL isImage = FALSE;
514 const BOOL executable = ViewContainsExecutable(ProcessHandle, BaseAddress, &isImage);
518 if (!NT_SUCCESS(status))
526 BaseAddress, isImage);
538 HANDLE ProcessHandle,
543 BOOL isImage = FALSE;
544 const BOOL executable = ViewContainsExecutable(ProcessHandle, BaseAddress, &isImage);
548 if (!NT_SUCCESS(status))
556 BaseAddress, isImage);
571 HANDLE ProcessHandle,
575 ULONG AllocationType,
579 RegionSize, AllocationType, Protection);
581 if (!NT_SUCCESS(status))
587 if (IsExecutableProtection(Protection))
590 *BaseAddress, *RegionSize, AllocationType, Protection);
606 HANDLE ProcessHandle,
609 ULONG AllocationType,
610 ULONG PageProtection,
611 PMEM_EXTENDED_PARAMETER ExtendedParameters,
612 ULONG ExtendedParameterCount)
615 AllocationType, PageProtection, ExtendedParameters, ExtendedParameterCount);
617 if (!NT_SUCCESS(status))
622 if (IsExecutableProtection(PageProtection))
625 *BaseAddress, *RegionSize, AllocationType, PageProtection);
639 HANDLE ProcessHandle,
642 SIZE_T NumberOfBytesToRead,
643 PSIZE_T NumberOfBytesRead)
646 NumberOfBytesToRead, NumberOfBytesRead);
648 if (!NT_SUCCESS(status))
653 ULONG_PTR pebOffset = 0;
654 if (RangeTouchesPEB(ProcessHandle, BaseAddress, NumberOfBytesToRead, &pebOffset))
656 Log_PEBRead(ProcessHandle, IsCurrentProcess(ProcessHandle), NumberOfBytesToRead, pebOffset);
676 PHANDLE ThreadHandle,
677 ACCESS_MASK DesiredAccess,
678 POBJECT_ATTRIBUTES ObjectAttributes,
679 HANDLE ProcessHandle,
685 SIZE_T MaximumStackSize,
689 ProcessHandle, StartRoutine, Argument, CreateFlags, ZeroBits, StackSize, MaximumStackSize,
692 if (!NT_SUCCESS(status))
699 if (!IsCurrentProcess(ProcessHandle))
722 ApcArgument2, ApcArgument3);
724 if (!NT_SUCCESS(status))
729 if (!IsThreadInCurrentProcess(ThreadHandle))
731 Log_ApcQueued(ThreadHandle, FALSE,
"NtQueueApcThread", (PVOID)ApcRoutine, 0);
749 HANDLE ReserveHandle,
757 ApcArgument1, ApcArgument2, ApcArgument3);
759 if (!NT_SUCCESS(status))
764 if (!IsThreadInCurrentProcess(ThreadHandle))
766 Log_ApcQueued(ThreadHandle, FALSE,
"NtQueueApcThreadEx2", (PVOID)ApcRoutine, ApcFlags);
775#define PANO_PROCESS_PROTECTION_INFORMATION 61
784 HANDLE ProcessHandle,
785 PROCESSINFOCLASS ProcessInformationClass,
786 PVOID ProcessInformation,
787 ULONG ProcessInformationLength)
790 ProcessInformation, ProcessInformationLength);
797 if (!NT_SUCCESS(status))
833#pragma region Logging control
840static volatile DWORD g_TransactionThreadId = 0;
851static volatile LONG g_Silenced = 0;
854static LONG g_Generation = 0;
862 return g_Silenced == 0 && GetCurrentThreadId() != g_TransactionThreadId;
866extern "C" __declspec(dllexport) VOID __cdecl PanoptesSetSilenced(BOOL Silenced)
868 InterlockedExchange(&g_Silenced, Silenced ? 1 : 0);
873extern "C" __declspec(dllexport) VOID __cdecl PanoptesSetTransactionThread(DWORD ThreadId)
875 g_TransactionThreadId = ThreadId;
879extern "C" __declspec(dllexport) LONG __cdecl PanoptesGeneration()
885static const WCHAR *FileNamePart(
const UNICODE_STRING &Path, USHORT *Length)
887 USHORT start = Path.Length /
sizeof(WCHAR);
888 while (start > 0 && Path.Buffer[start - 1] != L
'\\' && Path.Buffer[start - 1] != L
'/')
892 *Length = (USHORT)(Path.Length /
sizeof(WCHAR) - start);
893 return Path.Buffer + start;
897#define PANO_MAX_OTHER_COPIES 8
914 PPEB_LDR_DATA ldr = NtCurrentTeb()->ProcessEnvironmentBlock->Ldr;
915 const LIST_ENTRY *head = &ldr->InMemoryOrderModuleList;
918 const WCHAR *selfName = NULL;
919 USHORT selfLength = 0;
920 for (
const LIST_ENTRY *link = head->Flink; link != head; link = link->Flink)
922 const LDR_DATA_TABLE_ENTRY *entry = CONTAINING_RECORD(link, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
925 selfName = FileNamePart(entry->FullDllName, &selfLength);
929 if (selfName == NULL)
935 for (
const LIST_ENTRY *link = head->Flink; link != head && count < PANO_MAX_OTHER_COPIES; link = link->Flink)
937 const LDR_DATA_TABLE_ENTRY *entry = CONTAINING_RECORD(link, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
938 if (entry->DllBase == &
__ImageBase || entry->DllBase == NULL)
944 const WCHAR *name = FileNamePart(entry->FullDllName, &length);
945 if (length != selfLength || CompareStringOrdinal(name, length, selfName, selfLength, TRUE) != CSTR_EQUAL)
956 Copies[count++] = copy;
963static VOID TakeOverReporting()
966 const int count = FindOtherCopies(copies);
969 for (
int i = 0; i < count; i++)
972 const LONG generation = copies[i].
Generation();
973 if (generation > newest)
978 g_Generation = newest + 1;
983static VOID HandOverReporting()
986 const int count = FindOtherCopies(copies);
989 for (
int i = 0; i < count; i++)
991 if (newest < 0 || copies[i].Generation() > copies[newest].Generation())
996 for (
int i = 0; i < count; i++)
1005static VOID SetTransactionThreadInOtherCopies(DWORD ThreadId)
1008 const int count = FindOtherCopies(copies);
1009 for (
int i = 0; i < count; i++)
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());
1029 TakeOverReporting();
1031 g_TransactionThreadId = GetCurrentThreadId();
1032 SetTransactionThreadInOtherCopies(g_TransactionThreadId);
1033 DetourTransactionBegin();
1034 DetourUpdateThread(GetCurrentThread());
1038 *hook.Original = (PVOID)GetProcAddress(ntdll, hook.Name);
1039 if (*hook.Original != NULL)
1041 DetourAttach(hook.Original, hook.Hook);
1045 DetourTransactionCommit();
1046 g_TransactionThreadId = 0;
1047 SetTransactionThreadInOtherCopies(0);
1054 g_TransactionThreadId = GetCurrentThreadId();
1055 SetTransactionThreadInOtherCopies(g_TransactionThreadId);
1056 DetourTransactionBegin();
1057 DetourUpdateThread(GetCurrentThread());
1061 if (*hook.Original != NULL)
1063 DetourDetach(hook.Original, hook.Hook);
1067 DetourTransactionCommit();
1070 SetTransactionThreadInOtherCopies(0);
1071 HandOverReporting();
1073 if (g_SelfProcess != NULL)
1075 CloseHandle(g_SelfProcess);
1076 g_SelfProcess = NULL;
enum _PANO_SECTION_INHERIT PANO_SECTION_INHERIT
How a mapped view is shared with child processes. Only defined in kernel-mode headers.
NTSTATUS(NTAPI * pNtCreateThreadEx)(_Out_ PHANDLE ThreadHandle, _In_ ACCESS_MASK DesiredAccess, _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes, _In_ HANDLE ProcessHandle, _In_ PVOID StartRoutine, _In_opt_ PVOID Argument, _In_ ULONG CreateFlags, _In_ SIZE_T ZeroBits, _In_ SIZE_T StackSize, _In_ SIZE_T MaximumStackSize, _In_opt_ PVOID AttributeList)
The NtCreateThreadEx function creates a thread in a specified process. StartRoutine is a PUSER_THREAD...
NTSTATUS(NTAPI * pNtWriteVirtualMemory)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToWrite, PSIZE_T NumberOfBytesWritten)
The NtWriteVirtualMemory function writes memory to a specified process.
NTSTATUS(NTAPI * pNtAllocateVirtualMemoryEx)(_In_ HANDLE ProcessHandle, _Inout_ PVOID *BaseAddress, _Inout_ PSIZE_T RegionSize, _In_ ULONG AllocationType, _In_ ULONG PageProtection, _Inout_updates_opt_(ExtendedParameterCount) PMEM_EXTENDED_PARAMETER ExtendedParameters, _In_ ULONG ExtendedParameterCount)
The NtAllocateVirtualMemoryEx function reserves and/or commits memory in a process,...
NTSTATUS(NTAPI * pNtQueueApcThread)(_In_ HANDLE ThreadHandle, _In_ PPANO_APC_ROUTINE ApcRoutine, _In_opt_ PVOID ApcArgument1, _In_opt_ PVOID ApcArgument2, _In_opt_ PVOID ApcArgument3)
The NtQueueApcThread function queues a user-mode APC to a thread.
NTSTATUS(NTAPI * pNtUnmapViewOfSection)(_In_ HANDLE ProcessHandle, _In_opt_ PVOID BaseAddress)
The NtUnmapViewOfSection function unmaps a view of a section from the address space of a process.
NTSTATUS(NTAPI * pNtMapViewOfSection)(_In_ HANDLE SectionHandle, _In_ HANDLE ProcessHandle, _Inout_ PVOID *BaseAddress, _In_ ULONG_PTR ZeroBits, _In_ SIZE_T CommitSize, _Inout_opt_ PLARGE_INTEGER SectionOffset, _Inout_ PSIZE_T ViewSize, _In_ PANO_SECTION_INHERIT InheritDisposition, _In_ ULONG AllocationType, _In_ ULONG Win32Protect)
The NtMapViewOfSection function maps a view of a section into the address space of a process.
NTSTATUS(NTAPI * pNtProtectVirtualMemory)(_In_ HANDLE ProcessHandle, _Inout_ PVOID *BaseAddress, _Inout_ PSIZE_T RegionSize, _In_ ULONG NewProtection, _Out_ PULONG OldProtection)
The NtProtectVirtualMemory function changes the protection of memory in a process.
NTSTATUS(NTAPI * pNtSetInformationProcess)(_In_ HANDLE ProcessHandle, _In_ PROCESSINFOCLASS ProcessInformationClass, _In_reads_bytes_(ProcessInformationLength) PVOID ProcessInformation, _In_ ULONG ProcessInformationLength)
The NtSetInformationProcess function sets information about a process.
NTSTATUS(NTAPI * pNtQueueApcThreadEx2)(_In_ HANDLE ThreadHandle, _In_opt_ HANDLE ReserveHandle, _In_ ULONG ApcFlags, _In_ PPANO_APC_ROUTINE ApcRoutine, _In_opt_ PVOID ApcArgument1, _In_opt_ PVOID ApcArgument2, _In_opt_ PVOID ApcArgument3)
The NtQueueApcThreadEx2 function queues a user-mode APC to a thread, optionally as a special user APC...
NTSTATUS(NTAPI * pNtAllocateVirtualMemory)(_In_ HANDLE ProcessHandle, _Inout_ PVOID *BaseAddress, _In_ ULONG_PTR ZeroBits, _Inout_ PSIZE_T RegionSize, _In_ ULONG AllocationType, _In_ ULONG Protection)
The NtAllocateVirtualMemory function reserves and/or commits memory in a process.
NTSTATUS(NTAPI * pNtReadVirtualMemory)(_In_ HANDLE ProcessHandle, _In_opt_ PVOID BaseAddress, _Out_writes_bytes_(NumberOfBytesToRead) PVOID Buffer, _In_ SIZE_T NumberOfBytesToRead, _Out_opt_ PSIZE_T NumberOfBytesRead)
The NtReadVirtualMemory function reads memory from a specified process.
NTSTATUS(NTAPI * pNtMapViewOfSectionEx)(_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 NtMapViewOfSectionEx function maps a view of a section into the address space of a process.
NTSTATUS(NTAPI * pNtUnmapViewOfSectionEx)(_In_ HANDLE ProcessHandle, _In_opt_ PVOID BaseAddress, _In_ ULONG Flags)
The NtUnmapViewOfSectionEx function unmaps a view of a section from the address space of a process,...
VOID(NTAPI * PPANO_APC_ROUTINE)(_In_opt_ PVOID ApcArgument1, _In_opt_ PVOID ApcArgument2, _In_opt_ PVOID ApcArgument3)
The routine an APC queued with NtQueueApcThread runs. Not in the SDK headers.
pNtSetInformationProcess pOriginal_NtSetInformationProcess
VOID UnHook()
The UnHook function removes the trampoline hooks from the NTDLL 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_NtSetInformationProcess(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength)
Hooks NtSetInformationProcess.
NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx(HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags)
Hooks NtUnmapViewOfSectionEx.
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.
VOID(__cdecl * pPanoptesSetTransactionThread)(DWORD ThreadId)
NTSTATUS NTAPI Hooked_NtReadVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToRead, PSIZE_T NumberOfBytesRead)
Hooks NtReadVirtualMemory.
#define PANO_PROCESS_PROTECTION_INFORMATION
NTSTATUS NTAPI Hooked_NtUnmapViewOfSection(HANDLE ProcessHandle, PVOID BaseAddress)
Hooks NtUnmapViewOfSection.
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_NtAllocateVirtualMemoryEx(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
Hooks NtAllocateVirtualMemoryEx.
LONG(__cdecl * pPanoptesGeneration)()
NTSTATUS(NTAPI * pNtCompareObjects)(HANDLE FirstObjectHandle, HANDLE SecondObjectHandle)
NTSTATUS NTAPI Hooked_NtQueueApcThreadEx2(HANDLE ThreadHandle, HANDLE ReserveHandle, ULONG ApcFlags, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
Hooks NtQueueApcThreadEx2.
pNtAllocateVirtualMemoryEx pOriginal_NtAllocateVirtualMemoryEx
#define PANO_MAX_OTHER_COPIES
The most other copies tracked; more than a couple in one process is not expected.
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
Hooks NtAllocateVirtualMemory.
#define PANO_ENTRY_POINT_FIELD_OFFSET
pNtCreateThreadEx pOriginal_NtCreateThreadEx
pNtReadVirtualMemory pOriginal_NtReadVirtualMemory
pNtQueueApcThreadEx2 pOriginal_NtQueueApcThreadEx2
VOID PlaceHooks()
The PlaceHooks function places the trampoline hooks on several NTDLL functions. Exports missing from ...
pNtUnmapViewOfSection pOriginal_NtUnmapViewOfSection
pNtQueueApcThread pOriginal_NtQueueApcThread
IMAGE_DOS_HEADER __ImageBase
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_NtQueueApcThread(HANDLE ThreadHandle, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
Hooks NtQueueApcThread.
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.
pNtProtectVirtualMemory pOriginal_NtProtectVirtualMemory
NTSTATUS NTAPI Hooked_NtProtectVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG NewProtection, PULONG OldProtection)
Hooks NtProtectVirtualMemory.
pNtMapViewOfSectionEx pOriginal_NtMapViewOfSectionEx
pNtMapViewOfSection pOriginal_NtMapViewOfSection
pNtUnmapViewOfSectionEx pOriginal_NtUnmapViewOfSectionEx
VOID(__cdecl * pPanoptesSetSilenced)(BOOL Silenced)
BOOL HookLoggingEnabled()
Whether the Log_* functions should write. FALSE on the thread running a Detours transaction (so the h...
pNtAllocateVirtualMemory pOriginal_NtAllocateVirtualMemory
__declspec(dllexport) VOID __cdecl PanoptesSetSilenced(BOOL Silenced)
Called by another copy of this DLL to stop (TRUE) or resume (FALSE) this copy's logging.
pNtWriteVirtualMemory pOriginal_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.
NTSTATUS NTAPI Hooked_NtSetInformationProcess(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength)
Hooks NtSetInformationProcess, which sets information about 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_NtReadVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToRead, PSIZE_T NumberOfBytesRead)
Hooks NtReadVirtualMemory, which reads memory from 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_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_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_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...
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_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_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_NtProtectVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG NewProtection, PULONG OldProtection)
Hooks NtProtectVirtualMemory, which changes the protection of memory in a process.
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"....
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 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 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".
An ntdll export and the hook placed on it. Original receives the export's address and,...
pPanoptesGeneration Generation
pPanoptesSetTransactionThread SetTransactionThread
pPanoptesSetSilenced SetSilenced