Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook.cpp
Go to the documentation of this file.
1#include "hook.hpp"
2#include "hook_trace.hpp"
3#include "detours\detours.h"
4#include "winternl.h"
5#include <stddef.h> // offsetof
6
7#pragma region Hooked Functions
8
22
27static BOOL ContainsDOSSignature(const BYTE *Buffer, SIZE_T Size)
28{
29 if (Buffer == NULL || Size < sizeof(WORD))
30 {
31 return FALSE;
32 }
33
34 __try
35 {
36 return *(const WORD *)Buffer == IMAGE_DOS_SIGNATURE;
37 }
38 __except (EXCEPTION_EXECUTE_HANDLER)
39 {
40 return FALSE;
41 }
42}
43
48static BOOL IsExecutableProtection(ULONG Protection)
49{
50 return (Protection & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0;
51}
52
58static BOOL IsExecutableMemory(HANDLE ProcessHandle, PVOID Address)
59{
60 MEMORY_BASIC_INFORMATION mbi{};
61 if (VirtualQueryEx(ProcessHandle, Address, &mbi, sizeof(mbi)) != sizeof(mbi))
62 {
63 return FALSE;
64 }
65 return mbi.State == MEM_COMMIT && IsExecutableProtection(mbi.Protect);
66}
67
76static BOOL ViewContainsExecutable(HANDLE ProcessHandle, PVOID Address, BOOL *IsImage)
77{
78 *IsImage = FALSE;
79
80 MEMORY_BASIC_INFORMATION mbi{};
81 if (VirtualQueryEx(ProcessHandle, Address, &mbi, sizeof(mbi)) != sizeof(mbi) || mbi.State == MEM_FREE)
82 {
83 return FALSE;
84 }
85 *IsImage = (mbi.Type == MEM_IMAGE);
86
87 // Every region of the view shares its AllocationBase; the walk stops at the first one that
88 // does not, including the free region after the view
89 PVOID allocationBase = mbi.AllocationBase;
90 BYTE *next = (BYTE *)allocationBase;
91 while (VirtualQueryEx(ProcessHandle, next, &mbi, sizeof(mbi)) == sizeof(mbi) &&
92 mbi.AllocationBase == allocationBase)
93 {
94 if (mbi.State == MEM_COMMIT && IsExecutableProtection(mbi.Protect))
95 {
96 return TRUE;
97 }
98 next = (BYTE *)mbi.BaseAddress + mbi.RegionSize;
99 }
100 return FALSE;
101}
102
103#pragma region Handle identity
104
107typedef NTSTATUS(NTAPI *pNtCompareObjects)(HANDLE FirstObjectHandle, HANDLE SecondObjectHandle);
108
110typedef NTSTATUS(NTAPI *pNtGetNextThread)(HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAccess,
111 ULONG HandleAttributes, ULONG Flags, PHANDLE NewThreadHandle);
112
115static pNtCompareObjects g_NtCompareObjects = NULL;
116static pNtGetNextThread g_NtGetNextThread = NULL;
117
120static HANDLE g_SelfProcess = NULL;
121
126static BOOL IsCurrentProcess(HANDLE ProcessHandle)
127{
128 if (ProcessHandle == GetCurrentProcess())
129 {
130 return TRUE;
131 }
132
133 // GetProcessId returns 0 when the handle lacks query access
134 const DWORD processId = GetProcessId(ProcessHandle);
135 if (processId != 0)
136 {
137 return processId == GetCurrentProcessId();
138 }
139
140 return g_NtCompareObjects != NULL && g_SelfProcess != NULL &&
141 NT_SUCCESS(g_NtCompareObjects(ProcessHandle, g_SelfProcess));
142}
143
150static BOOL IsThreadInCurrentProcess(HANDLE ThreadHandle)
151{
152 if (ThreadHandle == GetCurrentThread())
153 {
154 return TRUE;
155 }
156
157 // GetProcessIdOfThread returns 0 when the handle lacks query access
158 const DWORD processId = GetProcessIdOfThread(ThreadHandle);
159 if (processId != 0)
160 {
161 return processId == GetCurrentProcessId();
162 }
163
164 if (g_NtCompareObjects == NULL || g_NtGetNextThread == NULL)
165 {
166 return FALSE;
167 }
168
169 BOOL found = FALSE;
170 HANDLE thread = NULL;
171 HANDLE next = NULL;
172 while (!found && NT_SUCCESS(g_NtGetNextThread(GetCurrentProcess(), thread, SYNCHRONIZE, 0, 0, &next)))
173 {
174 if (thread != NULL)
175 {
176 CloseHandle(thread);
177 }
178 thread = next;
179 found = NT_SUCCESS(g_NtCompareObjects(ThreadHandle, thread)); // STATUS_NOT_SAME_OBJECT otherwise
180 }
181 if (thread != NULL)
182 {
183 CloseHandle(thread);
184 }
185 return found;
186}
187
188#pragma endregion
189
192#define PANO_PEB_SPAN 0x1000
193
196static decltype(&NtQueryInformationProcess) g_NtQueryInformationProcess = NULL;
197
204static VOID GetPebAddresses(HANDLE ProcessHandle, ULONG_PTR Pebs[2])
205{
206 Pebs[0] = Pebs[1] = 0;
207
208 if (ProcessHandle == GetCurrentProcess())
209 {
210 Pebs[0] = (ULONG_PTR)NtCurrentTeb()->ProcessEnvironmentBlock;
211 return;
212 }
213
214 if (g_NtQueryInformationProcess == NULL)
215 {
216 return;
217 }
218
219 PROCESS_BASIC_INFORMATION info{};
220 if (NT_SUCCESS(g_NtQueryInformationProcess(ProcessHandle, ProcessBasicInformation, &info, sizeof(info), NULL)))
221 {
222 Pebs[0] = (ULONG_PTR)info.PebBaseAddress;
223 }
224
225#ifdef _WIN64
226 // A WOW64 target's 32-bit code uses its 32-bit PEB; this returns 0 for a native target
227 ULONG_PTR peb32 = 0;
228 if (NT_SUCCESS(g_NtQueryInformationProcess(ProcessHandle, ProcessWow64Information, &peb32, sizeof(peb32), NULL)))
229 {
230 Pebs[1] = peb32;
231 }
232#endif
233}
234
241static BOOL RangeTouchesPEB(HANDLE ProcessHandle, PVOID Address, SIZE_T Size, ULONG_PTR *PebOffset)
242{
243 ULONG_PTR pebs[2];
244 GetPebAddresses(ProcessHandle, pebs);
245
246 const ULONG_PTR start = (ULONG_PTR)Address;
247 const ULONG_PTR end = (Size > MAXULONG_PTR - start) ? MAXULONG_PTR : start + Size;
248
249 for (ULONG_PTR peb : pebs)
250 {
251 if (peb != 0 && start < peb + PANO_PEB_SPAN && end > peb)
252 {
253 *PebOffset = (start > peb) ? start - peb : 0;
254 return TRUE;
255 }
256 }
257 return FALSE;
258}
259
262static BOOL RangesOverlap(ULONG_PTR Start, SIZE_T Size, ULONG_PTR OtherStart, SIZE_T OtherSize)
263{
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;
267}
268
272static BOOL ReadTargetMemory(HANDLE ProcessHandle, ULONG_PTR Address, PVOID Buffer, SIZE_T Size)
273{
274 SIZE_T read = 0;
275 return pOriginal_NtReadVirtualMemory != NULL &&
276 NT_SUCCESS(pOriginal_NtReadVirtualMemory(ProcessHandle, (PVOID)Address, Buffer, Size, &read)) &&
277 read == Size;
278}
279
282#define PANO_ENTRY_POINT_FIELD_OFFSET offsetof(IMAGE_NT_HEADERS32, OptionalHeader.AddressOfEntryPoint)
283
292static BOOL GetMainImageEntryPoint(HANDLE ProcessHandle, ULONG_PTR *EntryPoint, ULONG_PTR *EntryPointField)
293{
294 ULONG_PTR pebs[2];
295 GetPebAddresses(ProcessHandle, pebs);
296 if (pebs[0] == 0)
297 {
298 return FALSE;
299 }
300
301 // PEB.ImageBaseAddress follows the first pointer-sized slot of Reserved3
302 ULONG_PTR imageBase = 0;
303 if (!ReadTargetMemory(ProcessHandle, pebs[0] + offsetof(PEB, Reserved3) + sizeof(PVOID), &imageBase, sizeof(imageBase)) ||
304 imageBase == 0)
305 {
306 return FALSE;
307 }
308
309 IMAGE_DOS_HEADER dos{};
310 if (!ReadTargetMemory(ProcessHandle, imageBase, &dos, sizeof(dos)) || dos.e_magic != IMAGE_DOS_SIGNATURE ||
311 dos.e_lfanew <= 0)
312 {
313 return FALSE;
314 }
315
316 // Only the fields up to and including AddressOfEntryPoint are read
317 const ULONG_PTR ntHeaders = imageBase + (ULONG_PTR)dos.e_lfanew;
318 IMAGE_NT_HEADERS32 nt{};
319 if (!ReadTargetMemory(ProcessHandle, ntHeaders, &nt, PANO_ENTRY_POINT_FIELD_OFFSET + sizeof(DWORD)) ||
320 nt.Signature != IMAGE_NT_SIGNATURE ||
321 (nt.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC && nt.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC))
322 {
323 return FALSE;
324 }
325
326 *EntryPoint = (nt.OptionalHeader.AddressOfEntryPoint != 0) ? imageBase + nt.OptionalHeader.AddressOfEntryPoint : 0;
327 *EntryPointField = ntHeaders + PANO_ENTRY_POINT_FIELD_OFFSET;
328 return TRUE;
329}
330
340 HANDLE ProcessHandle,
341 PVOID BaseAddress,
342 PVOID Buffer,
343 SIZE_T NumberOfBytesToWrite,
344 PSIZE_T NumberOfBytesWritten)
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}
385
394 HANDLE ProcessHandle,
395 PVOID *BaseAddress,
396 PSIZE_T RegionSize,
397 ULONG NewProtection,
398 PULONG OldProtection)
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}
416
430 HANDLE SectionHandle,
431 HANDLE ProcessHandle,
432 PVOID *BaseAddress,
433 ULONG_PTR ZeroBits,
434 SIZE_T CommitSize,
435 PLARGE_INTEGER SectionOffset,
436 PSIZE_T ViewSize,
437 PANO_SECTION_INHERIT InheritDisposition,
438 ULONG AllocationType,
439 ULONG Win32Protect)
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}
461
474NTSTATUS NTAPI Hooked_NtMapViewOfSectionEx(_In_ HANDLE SectionHandle,
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)
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}
503
509 HANDLE ProcessHandle,
510 PVOID BaseAddress)
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}
531
538 HANDLE ProcessHandle,
539 PVOID BaseAddress,
540 ULONG Flags)
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}
561
571 HANDLE ProcessHandle,
572 PVOID *BaseAddress,
573 ULONG_PTR ZeroBits,
574 PSIZE_T RegionSize,
575 ULONG AllocationType,
576 ULONG Protection)
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}
595
606 HANDLE ProcessHandle,
607 PVOID *BaseAddress,
608 PSIZE_T RegionSize,
609 ULONG AllocationType,
610 ULONG PageProtection,
611 PMEM_EXTENDED_PARAMETER ExtendedParameters,
612 ULONG ExtendedParameterCount)
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}
630
639 HANDLE ProcessHandle,
640 PVOID BaseAddress,
641 PVOID Buffer,
642 SIZE_T NumberOfBytesToRead,
643 PSIZE_T NumberOfBytesRead)
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}
661
676 PHANDLE ThreadHandle,
677 ACCESS_MASK DesiredAccess,
678 POBJECT_ATTRIBUTES ObjectAttributes,
679 HANDLE ProcessHandle,
680 PVOID StartRoutine,
681 PVOID Argument,
682 ULONG CreateFlags,
683 SIZE_T ZeroBits,
684 SIZE_T StackSize,
685 SIZE_T MaximumStackSize,
686 PVOID AttributeList)
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}
706
715 HANDLE ThreadHandle,
716 PPANO_APC_ROUTINE ApcRoutine,
717 PVOID ApcArgument1,
718 PVOID ApcArgument2,
719 PVOID ApcArgument3)
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}
736
748 HANDLE ThreadHandle,
749 HANDLE ReserveHandle,
750 ULONG ApcFlags,
751 PPANO_APC_ROUTINE ApcRoutine,
752 PVOID ApcArgument1,
753 PVOID ApcArgument2,
754 PVOID ApcArgument3)
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}
771
775#define PANO_PROCESS_PROTECTION_INFORMATION 61
776
784 HANDLE ProcessHandle,
785 PROCESSINFOCLASS ProcessInformationClass,
786 PVOID ProcessInformation,
787 ULONG ProcessInformationLength)
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}
804
805#pragma endregion
806
810{
811 const char *Name;
812 PVOID *Original;
813 PVOID Hook;
814};
815
816static HOOK_ENTRY g_Hooks[] = {
817 {"NtWriteVirtualMemory", (PVOID *)&pOriginal_NtWriteVirtualMemory, (PVOID)Hooked_NtWriteVirtualMemory},
818 {"NtMapViewOfSection", (PVOID *)&pOriginal_NtMapViewOfSection, (PVOID)Hooked_NtMapViewOfSection},
819 {"NtMapViewOfSectionEx", (PVOID *)&pOriginal_NtMapViewOfSectionEx, (PVOID)Hooked_NtMapViewOfSectionEx},
820 {"NtUnmapViewOfSection", (PVOID *)&pOriginal_NtUnmapViewOfSection, (PVOID)Hooked_NtUnmapViewOfSection},
821 {"NtUnmapViewOfSectionEx", (PVOID *)&pOriginal_NtUnmapViewOfSectionEx, (PVOID)Hooked_NtUnmapViewOfSectionEx},
822 {"NtProtectVirtualMemory", (PVOID *)&pOriginal_NtProtectVirtualMemory, (PVOID)Hooked_NtProtectVirtualMemory},
823 {"NtAllocateVirtualMemory", (PVOID *)&pOriginal_NtAllocateVirtualMemory, (PVOID)Hooked_NtAllocateVirtualMemory},
824 {"NtAllocateVirtualMemoryEx", (PVOID *)&pOriginal_NtAllocateVirtualMemoryEx, (PVOID)Hooked_NtAllocateVirtualMemoryEx},
825 {"NtReadVirtualMemory", (PVOID *)&pOriginal_NtReadVirtualMemory, (PVOID)Hooked_NtReadVirtualMemory},
826 {"NtCreateThreadEx", (PVOID *)&pOriginal_NtCreateThreadEx, (PVOID)Hooked_NtCreateThreadEx},
827 {"NtQueueApcThread", (PVOID *)&pOriginal_NtQueueApcThread, (PVOID)Hooked_NtQueueApcThread},
828 {"NtQueueApcThreadEx2", (PVOID *)&pOriginal_NtQueueApcThreadEx2, (PVOID)Hooked_NtQueueApcThreadEx2},
829 {"NtSetInformationProcess", (PVOID *)&pOriginal_NtSetInformationProcess, (PVOID)Hooked_NtSetInformationProcess},
830
831};
832
833#pragma region Logging control
834
835extern "C" IMAGE_DOS_HEADER __ImageBase;
836
840static volatile DWORD g_TransactionThreadId = 0;
841
842// More than one copy of this DLL can be loaded into a process when it is loaded from different
843// folders: the driver injects the installed copy, and the hook tests load their own build (and
844// one test case loads a third). Each copy places its own hooks and the hooks chain, so every call
845// would be reported once per copy. Only the newest copy, whose hooks are the outermost, reports;
846// the others are silenced. Copies are ordered by a generation number rather than by the module
847// list, and the list is walked afresh on every load and unload, so no copy keeps a pointer into
848// another that may since have unloaded. The exports are __cdecl so the x86 names are undecorated.
849
851static volatile LONG g_Silenced = 0;
852
854static LONG g_Generation = 0;
855
856typedef VOID(__cdecl *pPanoptesSetSilenced)(BOOL Silenced);
857typedef LONG(__cdecl *pPanoptesGeneration)();
858typedef VOID(__cdecl *pPanoptesSetTransactionThread)(DWORD ThreadId);
859
861{
862 return g_Silenced == 0 && GetCurrentThreadId() != g_TransactionThreadId;
863}
864
866extern "C" __declspec(dllexport) VOID __cdecl PanoptesSetSilenced(BOOL Silenced)
867{
868 InterlockedExchange(&g_Silenced, Silenced ? 1 : 0);
869}
870
873extern "C" __declspec(dllexport) VOID __cdecl PanoptesSetTransactionThread(DWORD ThreadId)
874{
875 g_TransactionThreadId = ThreadId;
876}
877
879extern "C" __declspec(dllexport) LONG __cdecl PanoptesGeneration()
880{
881 return g_Generation;
882}
883
885static const WCHAR *FileNamePart(const UNICODE_STRING &Path, USHORT *Length)
886{
887 USHORT start = Path.Length / sizeof(WCHAR);
888 while (start > 0 && Path.Buffer[start - 1] != L'\\' && Path.Buffer[start - 1] != L'/')
889 {
890 start--;
891 }
892 *Length = (USHORT)(Path.Length / sizeof(WCHAR) - start);
893 return Path.Buffer + start;
894}
895
897#define PANO_MAX_OTHER_COPIES 8
898
905
912static int FindOtherCopies(OTHER_COPY Copies[PANO_MAX_OTHER_COPIES])
913{
914 PPEB_LDR_DATA ldr = NtCurrentTeb()->ProcessEnvironmentBlock->Ldr;
915 const LIST_ENTRY *head = &ldr->InMemoryOrderModuleList;
916
917 // This copy's own file name
918 const WCHAR *selfName = NULL;
919 USHORT selfLength = 0;
920 for (const LIST_ENTRY *link = head->Flink; link != head; link = link->Flink)
921 {
922 const LDR_DATA_TABLE_ENTRY *entry = CONTAINING_RECORD(link, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
923 if (entry->DllBase == &__ImageBase)
924 {
925 selfName = FileNamePart(entry->FullDllName, &selfLength);
926 break;
927 }
928 }
929 if (selfName == NULL)
930 {
931 return 0;
932 }
933
934 int count = 0;
935 for (const LIST_ENTRY *link = head->Flink; link != head && count < PANO_MAX_OTHER_COPIES; link = link->Flink)
936 {
937 const LDR_DATA_TABLE_ENTRY *entry = CONTAINING_RECORD(link, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
938 if (entry->DllBase == &__ImageBase || entry->DllBase == NULL)
939 {
940 continue;
941 }
942
943 USHORT length = 0;
944 const WCHAR *name = FileNamePart(entry->FullDllName, &length);
945 if (length != selfLength || CompareStringOrdinal(name, length, selfName, selfLength, TRUE) != CSTR_EQUAL)
946 {
947 continue;
948 }
949
950 OTHER_COPY copy;
951 copy.SetSilenced = (pPanoptesSetSilenced)GetProcAddress((HMODULE)entry->DllBase, "PanoptesSetSilenced");
952 copy.Generation = (pPanoptesGeneration)GetProcAddress((HMODULE)entry->DllBase, "PanoptesGeneration");
953 copy.SetTransactionThread = (pPanoptesSetTransactionThread)GetProcAddress((HMODULE)entry->DllBase, "PanoptesSetTransactionThread");
954 if (copy.SetSilenced != NULL && copy.Generation != NULL && copy.SetTransactionThread != NULL)
955 {
956 Copies[count++] = copy;
957 }
958 }
959 return count;
960}
961
963static VOID TakeOverReporting()
964{
966 const int count = FindOtherCopies(copies);
967
968 LONG newest = 0;
969 for (int i = 0; i < count; i++)
970 {
971 copies[i].SetSilenced(TRUE);
972 const LONG generation = copies[i].Generation();
973 if (generation > newest)
974 {
975 newest = generation;
976 }
977 }
978 g_Generation = newest + 1;
979}
980
983static VOID HandOverReporting()
984{
986 const int count = FindOtherCopies(copies);
987
988 int newest = -1;
989 for (int i = 0; i < count; i++)
990 {
991 if (newest < 0 || copies[i].Generation() > copies[newest].Generation())
992 {
993 newest = i;
994 }
995 }
996 for (int i = 0; i < count; i++)
997 {
998 copies[i].SetSilenced(i != newest);
999 }
1000}
1001
1005static VOID SetTransactionThreadInOtherCopies(DWORD ThreadId)
1006{
1008 const int count = FindOtherCopies(copies);
1009 for (int i = 0; i < count; i++)
1010 {
1011 copies[i].SetTransactionThread(ThreadId);
1012 }
1013}
1014
1015#pragma endregion
1016
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}
1050
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}
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...
Definition def.h:128
NTSTATUS(NTAPI * pNtWriteVirtualMemory)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToWrite, PSIZE_T NumberOfBytesWritten)
The NtWriteVirtualMemory function writes memory to a specified process.
Definition def.h:21
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,...
Definition def.h:106
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.
Definition def.h:150
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.
Definition def.h:72
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.
Definition def.h:57
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.
Definition def.h:86
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.
Definition def.h:176
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...
Definition def.h:165
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.
Definition def.h:95
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.
Definition def.h:117
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.
Definition def.h:36
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,...
Definition def.h:79
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.
Definition def.h:143
pNtSetInformationProcess pOriginal_NtSetInformationProcess
Definition hook.cpp:21
VOID UnHook()
The UnHook function removes the trampoline hooks from the NTDLL functions.
Definition hook.cpp:1052
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.
Definition hook.cpp:339
NTSTATUS NTAPI Hooked_NtSetInformationProcess(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength)
Hooks NtSetInformationProcess.
Definition hook.cpp:783
NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx(HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags)
Hooks NtUnmapViewOfSectionEx.
Definition hook.cpp:537
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.
Definition hook.cpp:474
VOID(__cdecl * pPanoptesSetTransactionThread)(DWORD ThreadId)
Definition hook.cpp:858
NTSTATUS NTAPI Hooked_NtReadVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToRead, PSIZE_T NumberOfBytesRead)
Hooks NtReadVirtualMemory.
Definition hook.cpp:638
#define PANO_PROCESS_PROTECTION_INFORMATION
Definition hook.cpp:775
NTSTATUS NTAPI Hooked_NtUnmapViewOfSection(HANDLE ProcessHandle, PVOID BaseAddress)
Hooks NtUnmapViewOfSection.
Definition hook.cpp:508
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.
Definition hook.cpp:675
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemoryEx(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
Hooks NtAllocateVirtualMemoryEx.
Definition hook.cpp:605
LONG(__cdecl * pPanoptesGeneration)()
Definition hook.cpp:857
NTSTATUS(NTAPI * pNtCompareObjects)(HANDLE FirstObjectHandle, HANDLE SecondObjectHandle)
Definition hook.cpp:107
NTSTATUS NTAPI Hooked_NtQueueApcThreadEx2(HANDLE ThreadHandle, HANDLE ReserveHandle, ULONG ApcFlags, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
Hooks NtQueueApcThreadEx2.
Definition hook.cpp:747
pNtAllocateVirtualMemoryEx pOriginal_NtAllocateVirtualMemoryEx
Definition hook.cpp:16
#define PANO_MAX_OTHER_COPIES
The most other copies tracked; more than a couple in one process is not expected.
Definition hook.cpp:897
NTSTATUS NTAPI Hooked_NtAllocateVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protection)
Hooks NtAllocateVirtualMemory.
Definition hook.cpp:570
#define PANO_ENTRY_POINT_FIELD_OFFSET
Definition hook.cpp:282
pNtCreateThreadEx pOriginal_NtCreateThreadEx
Definition hook.cpp:18
pNtReadVirtualMemory pOriginal_NtReadVirtualMemory
Definition hook.cpp:17
pNtQueueApcThreadEx2 pOriginal_NtQueueApcThreadEx2
Definition hook.cpp:20
VOID PlaceHooks()
The PlaceHooks function places the trampoline hooks on several NTDLL functions. Exports missing from ...
Definition hook.cpp:1019
pNtUnmapViewOfSection pOriginal_NtUnmapViewOfSection
Definition hook.cpp:12
pNtQueueApcThread pOriginal_NtQueueApcThread
Definition hook.cpp:19
IMAGE_DOS_HEADER __ImageBase
Definition hook.cpp:835
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.
Definition hook.cpp:429
NTSTATUS NTAPI Hooked_NtQueueApcThread(HANDLE ThreadHandle, PPANO_APC_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
Hooks NtQueueApcThread.
Definition hook.cpp:714
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
pNtProtectVirtualMemory pOriginal_NtProtectVirtualMemory
Definition hook.cpp:14
NTSTATUS NTAPI Hooked_NtProtectVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG NewProtection, PULONG OldProtection)
Hooks NtProtectVirtualMemory.
Definition hook.cpp:393
pNtMapViewOfSectionEx pOriginal_NtMapViewOfSectionEx
Definition hook.cpp:11
pNtMapViewOfSection pOriginal_NtMapViewOfSection
Definition hook.cpp:10
pNtUnmapViewOfSectionEx pOriginal_NtUnmapViewOfSectionEx
Definition hook.cpp:13
VOID(__cdecl * pPanoptesSetSilenced)(BOOL Silenced)
Definition hook.cpp:856
BOOL HookLoggingEnabled()
Whether the Log_* functions should write. FALSE on the thread running a Detours transaction (so the h...
Definition hook.cpp:860
pNtAllocateVirtualMemory pOriginal_NtAllocateVirtualMemory
Definition hook.cpp:15
__declspec(dllexport) VOID __cdecl PanoptesSetSilenced(BOOL Silenced)
Called by another copy of this DLL to stop (TRUE) or resume (FALSE) this copy's logging.
Definition hook.cpp:866
pNtWriteVirtualMemory pOriginal_NtWriteVirtualMemory
Definition hook.cpp:9
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.
Definition hook.cpp:339
NTSTATUS NTAPI Hooked_NtSetInformationProcess(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength)
Hooks NtSetInformationProcess, which sets information about a process.
Definition hook.cpp:783
NTSTATUS NTAPI Hooked_NtUnmapViewOfSectionEx(HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags)
Hooks NtUnmapViewOfSectionEx, which unmaps a view of a section from a process.
Definition hook.cpp:537
NTSTATUS NTAPI Hooked_NtReadVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToRead, PSIZE_T NumberOfBytesRead)
Hooks NtReadVirtualMemory, which reads memory from a process.
Definition hook.cpp:638
NTSTATUS NTAPI Hooked_NtUnmapViewOfSection(HANDLE ProcessHandle, PVOID BaseAddress)
Hooks NtUnmapViewOfSection, which unmaps a view of a section from a process.
Definition hook.cpp:508
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.
Definition hook.cpp:675
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.
Definition hook.cpp:605
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...
Definition hook.cpp:747
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.
Definition hook.cpp:570
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.
Definition hook.cpp:429
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.
Definition hook.cpp:714
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.
Definition hook.cpp:393
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,...
Definition hook.cpp:810
PVOID * Original
Definition hook.cpp:812
const char * Name
Definition hook.cpp:811
PVOID Hook
Definition hook.cpp:813
pPanoptesGeneration Generation
Definition hook.cpp:902
pPanoptesSetTransactionThread SetTransactionThread
Definition hook.cpp:903
pPanoptesSetSilenced SetSilenced
Definition hook.cpp:901