Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
callbacks.cpp File Reference
#include "callbacks.h"
#include <ntstrsafe.h>
#include "shellcode.h"
#include "event_queue.h"
#include "inject_exclusions.h"

Go to the source code of this file.

Classes

struct  InjectArgs
struct  _PANO_RENAME_KEY_CONTEXT

Macros

#define PANO_REGISTRY_POOL_TAG   'gRoP'

Typedefs

typedef struct _PANO_RENAME_KEY_CONTEXT PANO_RENAME_KEY_CONTEXT
typedef struct _PANO_RENAME_KEY_CONTEXT * PPANO_RENAME_KEY_CONTEXT

Functions

PVOID NTAPI RtlxFindExportedRoutineByName (_In_ PVOID DllBase, _In_ PANSI_STRING ExportName)
RTL_GENERIC_COMPARE_RESULTS NTAPI ProcessInfoCompareRoutine (PRTL_AVL_TABLE Table, PVOID FirstStruct, PVOID SecondStruct)
PVOID NTAPI ProcessInfoAllocateRoutine (PRTL_AVL_TABLE Table, CLONG ByteSize)
VOID NTAPI ProcessInfoFreeRoutine (PRTL_AVL_TABLE Table, PVOID Buffer)
PPANO_PROCESS_INFO GetProcessInfo (HANDLE ProcessId)
VOID RemoveProcessInfo (HANDLE ProcessId)
void NTAPI KernelRoutine (PKAPC apc, PKNORMAL_ROUTINE *NormalRoutine, PVOID *NormalContext, PVOID *SystemArgument1, PVOID *SystemArgument2)
BOOLEAN CanInject (PPANO_PROCESS_INFO processInfo)
VOID InjectDllKernelApc (PVOID NormalContext, PVOID SystemArgument1, PVOID SystemArgument2)
NTSTATUS InstallKernelModeApcToInjectDll (HANDLE ProcessId)
BOOLEAN Is64BitProcess (PEPROCESS targetProcess)
NTSTATUS RegistryCallbackFunction (PVOID CallbackContext, PVOID Argument1, PVOID Argument2)
VOID LoadImageNotifyRoutine (PUNICODE_STRING FullImageName, HANDLE ProcessId, PIMAGE_INFO ImageInfo)
VOID ProcessCreateCallback (PEPROCESS Process, HANDLE ProcessId, PPS_CREATE_NOTIFY_INFO CreateInfo)
NTSTATUS InitializeKernelCallbacks ()
VOID RemoveCallbacks ()

Variables

RTL_AVL_TABLE g_ProcessList
LARGE_INTEGER g_RegistryCookie

Macro Definition Documentation

◆ PANO_REGISTRY_POOL_TAG

#define PANO_REGISTRY_POOL_TAG   'gRoP'

Definition at line 389 of file callbacks.cpp.

Typedef Documentation

◆ PANO_RENAME_KEY_CONTEXT

◆ PPANO_RENAME_KEY_CONTEXT

Function Documentation

◆ CanInject()

BOOLEAN CanInject ( PPANO_PROCESS_INFO processInfo)

Definition at line 237 of file callbacks.cpp.

238{
239 if (processInfo->ntdllLoaded && processInfo->kernel32Loaded && processInfo->kernelBaseLoaded)
240 {
241 return TRUE;
242 }
243 return FALSE;
244}
BOOLEAN ntdllLoaded
Definition structs.h:12
BOOLEAN kernel32Loaded
Definition structs.h:13
BOOLEAN kernelBaseLoaded
Definition structs.h:14

References _PANO_PROCESS_INFO::kernel32Loaded, _PANO_PROCESS_INFO::kernelBaseLoaded, and _PANO_PROCESS_INFO::ntdllLoaded.

Referenced by LoadImageNotifyRoutine().

◆ GetProcessInfo()

PPANO_PROCESS_INFO GetProcessInfo ( HANDLE ProcessId)

Definition at line 206 of file callbacks.cpp.

207{
208 PANO_PROCESS_INFO key{};
209 key.ProcessId = ProcessId;
210 return (PPANO_PROCESS_INFO)RtlLookupElementGenericTableAvl(&g_ProcessList, &key);
211}
RTL_AVL_TABLE g_ProcessList
Definition callbacks.cpp:7
HANDLE ProcessId
Definition structs.h:7
struct _PANO_PROCESS_INFO * PPANO_PROCESS_INFO
struct _PANO_PROCESS_INFO PANO_PROCESS_INFO

References g_ProcessList, and _PANO_PROCESS_INFO::ProcessId.

Referenced by InjectDllKernelApc(), and LoadImageNotifyRoutine().

◆ InitializeKernelCallbacks()

NTSTATUS InitializeKernelCallbacks ( )

Definition at line 1186 of file callbacks.cpp.

1187{
1188 PAGED_CODE();
1189
1191 NTSTATUS status = PsSetCreateProcessNotifyRoutineEx(ProcessCreateCallback, FALSE);
1192 if (!NT_SUCCESS(status))
1193 {
1194 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[!] Panoptes: Driver Failed to Set Process Creation Notify Routine Notify Routine - Ensure /INTEGRITYCHECK is added to the linker options\n");
1195 return status;
1196 }
1197 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[+] Panoptes: Set Process Creation Notify Callbacks\n");
1198
1199 status = PsSetLoadImageNotifyRoutine(LoadImageNotifyRoutine);
1200 if (!NT_SUCCESS(status))
1201 {
1202 NTSTATUS removeStatus = PsSetCreateProcessNotifyRoutineEx(ProcessCreateCallback, TRUE);
1203 if (!NT_SUCCESS(removeStatus))
1204 {
1205 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[!] Panoptes: Driver Failed to remove callback for Set Process Creation Notify Routine Notify Routine\n");
1206 return removeStatus;
1207 }
1208 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[!] Panoptes: Driver Failed to Set Process Load Image Notify Routine - Ensure /INTEGRITYCHECK is added to the linker options\n");
1209 return status;
1210 }
1211 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[+] Panoptes: Set Image Load Notify Callbacks\n");
1212
1213 PVOID RegistryContext = nullptr;
1214 status = CmRegisterCallback(RegistryCallbackFunction, RegistryContext, &g_RegistryCookie);
1215 if (!NT_SUCCESS(status))
1216 {
1217 NTSTATUS removeStatus = PsRemoveLoadImageNotifyRoutine(LoadImageNotifyRoutine);
1218 if (!NT_SUCCESS(removeStatus))
1219 {
1220 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[!] Panoptes: Driver Failed to remove callback for Set Image Load Notify Routine Notify Routine\n");
1221 return removeStatus;
1222 }
1223
1224 removeStatus = PsSetCreateProcessNotifyRoutineEx(ProcessCreateCallback, TRUE);
1225 if (!NT_SUCCESS(removeStatus))
1226 {
1227 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[!] Panoptes: Driver Failed to remove callback for Set Process Creation Notify Routine Notify Routine\n");
1228 return removeStatus;
1229 }
1230 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[!] Panoptes: Driver Failed to Register Registry Callback, status: 0x%X\n", status);
1231 return status;
1232 }
1233 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[+] Panoptes: Set Registry Callbacks\n");
1234
1235
1236 return STATUS_SUCCESS;
1237}
PVOID NTAPI ProcessInfoAllocateRoutine(PRTL_AVL_TABLE Table, CLONG ByteSize)
RTL_GENERIC_COMPARE_RESULTS NTAPI ProcessInfoCompareRoutine(PRTL_AVL_TABLE Table, PVOID FirstStruct, PVOID SecondStruct)
NTSTATUS RegistryCallbackFunction(PVOID CallbackContext, PVOID Argument1, PVOID Argument2)
VOID LoadImageNotifyRoutine(PUNICODE_STRING FullImageName, HANDLE ProcessId, PIMAGE_INFO ImageInfo)
VOID ProcessCreateCallback(PEPROCESS Process, HANDLE ProcessId, PPS_CREATE_NOTIFY_INFO CreateInfo)
LARGE_INTEGER g_RegistryCookie
Definition callbacks.cpp:8
VOID NTAPI ProcessInfoFreeRoutine(PRTL_AVL_TABLE Table, PVOID Buffer)

References g_ProcessList, g_RegistryCookie, LoadImageNotifyRoutine(), ProcessCreateCallback(), ProcessInfoAllocateRoutine(), ProcessInfoCompareRoutine(), ProcessInfoFreeRoutine(), and RegistryCallbackFunction().

Referenced by DriverEntry().

◆ InjectDllKernelApc()

VOID InjectDllKernelApc ( PVOID NormalContext,
PVOID SystemArgument1,
PVOID SystemArgument2 )

Definition at line 246 of file callbacks.cpp.

247{
248 DbgPrint("InjectDll Entered");
249 HANDLE pProcessId = (HANDLE)NormalContext;
250 UNREFERENCED_PARAMETER(SystemArgument1);
251 UNREFERENCED_PARAMETER(SystemArgument2);
252
253 if (nullptr == pProcessId)
254 {
255 DbgPrint("InjectDll - pProcessId is null");
256 return;
257 }
258
259 PPANO_PROCESS_INFO pi = GetProcessInfo(pProcessId);
260 if (pi == nullptr)
261 {
262 DbgPrint("InjectDll - process is not tracked");
263 return;
264 }
265
266 ANSI_STRING loadDllRoutineName = RTL_CONSTANT_STRING("LoadLibraryExW");
267 PVOID shellcodeAddress = RtlxFindExportedRoutineByName(pi->kernel32BaseAddress, &loadDllRoutineName);
268 if (shellcodeAddress == nullptr)
269 {
270 // Queuing an APC to a NULL routine would crash the process
271 DbgPrint("Error: LoadLibraryExW was not found in kernel32.");
272 return;
273 }
274
275 UNICODE_STRING dllToInject;
276 PVOID allocatedAddressContainingDllFullPath = nullptr;
277
278 if (pi->is64Bit)
279 {
280 RtlInitUnicodeString(&dllToInject, L"C:\\Program Files\\Panoptes\\PanoptesDLLx64.dll");
281 }
282 else
283 {
284 RtlInitUnicodeString(&dllToInject, L"C:\\Program Files\\Panoptes\\PanoptesDLLx86.dll");
285 }
286
287 // Room for the path and its terminator. ZwAllocateVirtualMemory takes and returns a SIZE_T,
288 // so it gets its own variable rather than a pointer to the USHORT dllToInject.Length.
289 // The allocation is rounded up to whole pages, which are zero-filled
290 SIZE_T regionSize = dllToInject.Length + sizeof(WCHAR);
291
292 // A 32-bit process's LoadLibraryExW takes a 32-bit pointer, so its copy of the path must be
293 // below 2 GB. A ZeroBits value above 32 is a mask the allocation's address must fit in
294 ULONG_PTR zeroBits = pi->is64Bit ? 0 : 0x7FFFFFFF;
295 NTSTATUS status = ZwAllocateVirtualMemory(NtCurrentProcess(), &allocatedAddressContainingDllFullPath, zeroBits, &regionSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
296 if (!NT_SUCCESS(status))
297 {
298 DbgPrint("Error: Unable to allocate memory in the target process.");
299 return;
300 }
301 RtlCopyMemory(allocatedAddressContainingDllFullPath, dllToInject.Buffer, dllToInject.Length);
302
303 // MEM_RELEASE frees the whole allocation and requires a size of 0
304 SIZE_T releaseSize = 0;
305
306 PKAPC pKapc = (PKAPC)ExAllocatePool2(POOL_FLAG_NON_PAGED, sizeof(KAPC), 'tikt'); // Allocate the APC object
307 if (!pKapc)
308 {
309 DbgPrint("Error: Unable to allocate the APC object.");
310 ZwFreeVirtualMemory(NtCurrentProcess(), &allocatedAddressContainingDllFullPath, &releaseSize, MEM_RELEASE); // Free the allocated memory
311 return;
312 }
313
314 // User-mode APCs are delivered by the native 64-bit dispatcher. For a 32-bit process the
315 // routine is the 32-bit kernel32's LoadLibraryExW, which must run in 32-bit mode: wrapping
316 // the APC makes WOW64 switch modes and call it. Without this the dispatcher runs 32-bit
317 // code as 64-bit code and the process crashes
318 PVOID apcRoutine = shellcodeAddress;
319 PVOID apcContext = allocatedAddressContainingDllFullPath;
320 if (!pi->is64Bit)
321 {
322 status = PsWrapApcWow64Thread(&apcContext, &apcRoutine);
323 if (!NT_SUCCESS(status))
324 {
325 DbgPrint("Error: Unable to wrap the APC for a WOW64 process.");
326 ZwFreeVirtualMemory(NtCurrentProcess(), &allocatedAddressContainingDllFullPath, &releaseSize, MEM_RELEASE);
327 ExFreePool(pKapc);
328 return;
329 }
330 }
331
332 KeInitializeApc(pKapc, KeGetCurrentThread(), OriginalApcEnvironment, KernelRoutine, NULL, (PKNORMAL_ROUTINE)apcRoutine, UserMode, apcContext); // Initialize the APC
333
334 DbgPrint("Inserting APC to target thread");
335 if (!KeInsertQueueApc(pKapc, NULL, NULL, IO_NO_INCREMENT))
336 {
337 DbgPrint("Error: Unable to insert APC to target thread.");
338 ZwFreeVirtualMemory(NtCurrentProcess(), &allocatedAddressContainingDllFullPath, &releaseSize, MEM_RELEASE); // Free the allocated memory
339 ExFreePool(pKapc); // Free the APC object
340 return;
341 }
342
343 KeTestAlertThread(UserMode);
344
345 DbgPrint("InjectDllKernelApc exiting");
346}
PPANO_PROCESS_INFO GetProcessInfo(HANDLE ProcessId)
void NTAPI KernelRoutine(PKAPC apc, PKNORMAL_ROUTINE *NormalRoutine, PVOID *NormalContext, PVOID *SystemArgument1, PVOID *SystemArgument2)
PVOID NTAPI RtlxFindExportedRoutineByName(_In_ PVOID DllBase, _In_ PANSI_STRING ExportName)
Definition callbacks.cpp:13
VOID(NTAPI * PKNORMAL_ROUTINE)(_In_ PVOID NormalContext, _In_ PVOID SystemArgument1, _In_ PVOID SystemArgument2)
Definition callbacks.h:59
BOOLEAN NTAPI KeInsertQueueApc(_Inout_ PRKAPC Apc, _In_opt_ PVOID SystemArgument1, _In_opt_ PVOID SystemArgument2, _In_ KPRIORITY Increment)
@ OriginalApcEnvironment
Definition callbacks.h:53
BOOLEAN NTAPI KeTestAlertThread(IN KPROCESSOR_MODE AlertMode)
VOID NTAPI KeInitializeApc(_Out_ PRKAPC Apc, _In_ PRKTHREAD Thread, _In_ KAPC_ENVIRONMENT Environment, _In_ PKKERNEL_ROUTINE KernelRoutine, _In_opt_ PKRUNDOWN_ROUTINE RundownRoutine, _In_opt_ PKNORMAL_ROUTINE NormalRoutine, _In_opt_ KPROCESSOR_MODE ProcessorMode, _In_opt_ PVOID NormalContext)
PVOID kernel32BaseAddress
Definition structs.h:15
BOOLEAN is64Bit
Definition structs.h:6

References GetProcessInfo(), _PANO_PROCESS_INFO::is64Bit, KeInitializeApc(), KeInsertQueueApc(), _PANO_PROCESS_INFO::kernel32BaseAddress, KernelRoutine(), KeTestAlertThread(), OriginalApcEnvironment, and RtlxFindExportedRoutineByName().

Referenced by InstallKernelModeApcToInjectDll().

◆ InstallKernelModeApcToInjectDll()

NTSTATUS InstallKernelModeApcToInjectDll ( HANDLE ProcessId)

Definition at line 348 of file callbacks.cpp.

349{
350 PRKAPC pKapc = nullptr;
351 PETHREAD pThread = nullptr;
352
353 pKapc = (PKAPC)ExAllocatePool2(POOL_FLAG_NON_PAGED, sizeof(KAPC), 'tiky'); // Allocate the APC object
354 if (nullptr == pKapc)
355 {
356 DbgPrint("InstallKernelModeApcToInjectDll Failed to allocate memory for the APC");
357 return STATUS_INSUFFICIENT_RESOURCES;
358 }
359
360 pThread = KeGetCurrentThread();
361 KeInitializeApc(pKapc, pThread,
363 KernelRoutine, NULL,
365 KernelMode, (PVOID)ProcessId);
366 if (!KeInsertQueueApc(pKapc, NULL, NULL, IO_NO_INCREMENT))
367 {
368 DbgPrint("InstallKernelModeApcToInjectDll Failed to insert APC");
369 ExFreePool(pKapc); // Free the APC object
370 return STATUS_UNSUCCESSFUL;
371 }
372 else
373 {
374 DbgPrint("InstallKernelModeApcToInjectDll APC delivered");
375 }
376
377 return STATUS_SUCCESS;
378}
VOID InjectDllKernelApc(PVOID NormalContext, PVOID SystemArgument1, PVOID SystemArgument2)

References InjectDllKernelApc(), KeInitializeApc(), KeInsertQueueApc(), KernelRoutine(), and OriginalApcEnvironment.

Referenced by LoadImageNotifyRoutine().

◆ Is64BitProcess()

BOOLEAN Is64BitProcess ( PEPROCESS targetProcess)

Definition at line 380 of file callbacks.cpp.

381{
382 // Rather than reading EPROCESS.WoW64Process at a fixed offset, which differs between
383 // Windows builds
384 return PsGetProcessWow64Process(targetProcess) == NULL;
385}
NTKERNELAPI PVOID NTAPI PsGetProcessWow64Process(_In_ PEPROCESS Process)
Exported by ntoskrnl but not declared in the WDK. Returns the process's WOW64 PEB (non-NULL) for a 32...

References PsGetProcessWow64Process().

Referenced by ProcessCreateCallback().

◆ KernelRoutine()

void NTAPI KernelRoutine ( PKAPC apc,
PKNORMAL_ROUTINE * NormalRoutine,
PVOID * NormalContext,
PVOID * SystemArgument1,
PVOID * SystemArgument2 )

Definition at line 220 of file callbacks.cpp.

221{
222 UNREFERENCED_PARAMETER(NormalRoutine);
223 UNREFERENCED_PARAMETER(NormalContext);
224 UNREFERENCED_PARAMETER(SystemArgument1);
225 UNREFERENCED_PARAMETER(SystemArgument2);
226
227 ExFreePool(apc);
228}

Referenced by InjectDllKernelApc(), InstallKernelModeApcToInjectDll(), and KeInitializeApc().

◆ LoadImageNotifyRoutine()

VOID LoadImageNotifyRoutine ( PUNICODE_STRING FullImageName,
HANDLE ProcessId,
PIMAGE_INFO ImageInfo )

Definition at line 1049 of file callbacks.cpp.

1050{
1051 PAGED_CODE();
1052
1053 PPANO_PROCESS_INFO processInfo = GetProcessInfo(ProcessId);
1054 if (!processInfo || processInfo->Injected || processInfo->ProtectedProcess || processInfo->SignedImagesOnly || processInfo->InjectionExcluded)
1055 {
1056 return;
1057 }
1058
1059 if (!CanInject(processInfo))
1060 {
1061 UNICODE_STRING ntdllLoadImage;
1062 RtlInitUnicodeString(&ntdllLoadImage, L"ntdll.dll");
1063 if (wcsstr(FullImageName->Buffer, ntdllLoadImage.Buffer) != NULL)
1064 {
1065 processInfo->ntdllLoaded = TRUE;
1066 return;
1067 }
1068
1069 UNICODE_STRING kernel32LoadImage;
1070 RtlInitUnicodeString(&kernel32LoadImage, L"kernel32.dll");
1071 if (wcsstr(FullImageName->Buffer, kernel32LoadImage.Buffer) != NULL)
1072 {
1073 processInfo->kernel32Loaded = TRUE;
1074 processInfo->kernel32BaseAddress = ImageInfo->ImageBase;
1075 return;
1076 }
1077
1078 UNICODE_STRING kernelbaseLoadImage;
1079 RtlInitUnicodeString(&kernelbaseLoadImage, L"KernelBase.dll");
1080 if (wcsstr(FullImageName->Buffer, kernelbaseLoadImage.Buffer) != NULL)
1081 {
1082 processInfo->kernelBaseLoaded = TRUE;
1083 return;
1084 }
1085 }
1086 else
1087 {
1088 PEPROCESS targetProcess = NULL;
1089 NTSTATUS status = PsLookupProcessByProcessId(ProcessId, &targetProcess);
1090 if (!NT_SUCCESS(status))
1091 {
1092 return;
1093 }
1094
1095 PUNICODE_STRING processPath{};
1096 status = SeLocateProcessImageName(targetProcess, &processPath);
1097 if (!NT_SUCCESS(status))
1098 {
1099 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[-] Panoptes: Failed to get process name, status: 0x%X\n", status);
1100 ObDereferenceObject(targetProcess);
1101 return;
1102 }
1103
1104 // Checked just before injecting, so a list the service sent after the process started
1105 // still applies. The result is kept so later image loads skip the process
1106 if (IsInjectionExcluded(processPath))
1107 {
1108 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[*] Panoptes: Not injecting into (%llu) %wZ: it is in the DontInject list\n", (ULONG64)ProcessId, processPath);
1109 processInfo->InjectionExcluded = TRUE;
1110 ExFreePool(processPath);
1111 ObDereferenceObject(targetProcess);
1112 return;
1113 }
1114
1115 // Checked here rather than at creation, so a policy turned on after creation but
1116 // before kernel32 loads is also seen. The result is kept so later image loads skip
1117 // the process without querying again
1118 if (RequiresMicrosoftSignedImages(targetProcess))
1119 {
1120 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[*] Panoptes: Not injecting into (%llu) %wZ: it only loads Microsoft-signed images\n", (ULONG64)ProcessId, processPath);
1121 processInfo->SignedImagesOnly = TRUE;
1122 ExFreePool(processPath);
1123 ObDereferenceObject(targetProcess);
1124 return;
1125 }
1126
1127 if (processInfo->is64Bit)
1128 {
1129 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[+] Panoptes: Injecting into x64: (%llu) %wZ\n", (ULONG64)ProcessId, processPath);
1130 }
1131 else
1132 {
1133 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[+] Panoptes: Injecting Into x86: (%llu) %wZ\n", (ULONG64)ProcessId, processPath);
1134 }
1135
1136 ExFreePool(processPath);
1137 ObDereferenceObject(targetProcess);
1138
1140 processInfo->Injected = TRUE;
1141 }
1142 return;
1143}
NTSTATUS InstallKernelModeApcToInjectDll(HANDLE ProcessId)
BOOLEAN CanInject(PPANO_PROCESS_INFO processInfo)
BOOLEAN IsInjectionExcluded(_In_ PCUNICODE_STRING ProcessImagePath)
Checks whether a process's image is in the list. PASSIVE_LEVEL only.
BOOLEAN Injected
Definition structs.h:8
BOOLEAN SignedImagesOnly
Definition structs.h:10
BOOLEAN InjectionExcluded
Definition structs.h:11
BOOLEAN ProtectedProcess
Definition structs.h:9

References CanInject(), GetProcessInfo(), _PANO_PROCESS_INFO::Injected, _PANO_PROCESS_INFO::InjectionExcluded, InstallKernelModeApcToInjectDll(), _PANO_PROCESS_INFO::is64Bit, IsInjectionExcluded(), _PANO_PROCESS_INFO::kernel32BaseAddress, _PANO_PROCESS_INFO::kernel32Loaded, _PANO_PROCESS_INFO::kernelBaseLoaded, _PANO_PROCESS_INFO::ntdllLoaded, _PANO_PROCESS_INFO::ProtectedProcess, and _PANO_PROCESS_INFO::SignedImagesOnly.

Referenced by InitializeKernelCallbacks(), and RemoveCallbacks().

◆ ProcessCreateCallback()

VOID ProcessCreateCallback ( PEPROCESS Process,
HANDLE ProcessId,
PPS_CREATE_NOTIFY_INFO CreateInfo )

Definition at line 1145 of file callbacks.cpp.

1146{
1147 if (CreateInfo != NULL)
1148 {
1149 // Protected processes are still tracked and reported; ProtectedProcess makes
1150 // LoadImageNotifyRoutine skip injecting into them
1151 PANO_PROCESS_INFO processInfo{};
1152 processInfo.ProcessId = ProcessId;
1153 processInfo.is64Bit = Is64BitProcess(Process);
1154 processInfo.ProtectedProcess = PsIsProtectedProcess(Process);
1155 // The signature policy is checked in LoadImageNotifyRoutine just before injecting
1156
1157 if (!RtlInsertElementGenericTableAvl(&g_ProcessList, &processInfo, sizeof(processInfo), NULL))
1158 {
1159 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[-] Panoptes: Failed to track process %llu\n", (ULONG64)ProcessId);
1160 }
1161
1162 // ImageFileName is only valid when FileOpenNameAvailable is set
1163 PCUNICODE_STRING imageName = CreateInfo->FileOpenNameAvailable ? CreateInfo->ImageFileName : NULL;
1164 // CommandLine can be NULL (e.g. minimal/pico processes)
1165 PCUNICODE_STRING commandLine = CreateInfo->CommandLine;
1168 (UINT64)ProcessId,
1169 (UINT64)CreateInfo->ParentProcessId,
1170 (UINT64)CreateInfo->CreatingThreadId.UniqueProcess,
1171 (UINT64)CreateInfo->CreatingThreadId.UniqueThread,
1172 imageName ? imageName->Buffer : NULL,
1173 imageName ? imageName->Length / sizeof(WCHAR) : 0,
1174 commandLine ? commandLine->Buffer : NULL,
1175 commandLine ? commandLine->Length / sizeof(WCHAR) : 0);
1176
1177
1178 }
1179 else
1180 {
1181 RemoveProcessInfo(ProcessId);
1182 PanoReportProcessEvent(PANO_EVENT_PROCESS_TERMINATED, (UINT64)ProcessId, 0, 0, 0, NULL, 0, NULL, 0);
1183 }
1184}
BOOLEAN Is64BitProcess(PEPROCESS targetProcess)
VOID RemoveProcessInfo(HANDLE ProcessId)
BOOLEAN NTAPI PsIsProtectedProcess(_In_ PEPROCESS Process)
NTSTATUS PanoReportProcessEvent(PANO_EVENT_TYPE Type, UINT64 ProcessId, UINT64 ParentProcessId, UINT64 CreatingProcessId, UINT64 CreatingThreadId, _In_reads_opt_(ImageNameLength) const WCHAR *ImageName, UINT32 ImageNameLength, _In_reads_opt_(CommandLineLength) const WCHAR *CommandLine, UINT32 CommandLineLength)
Convenience wrapper that builds a PANO_PROCESS_EVENT_DATA event. If the image name and command line t...
@ PANO_EVENT_PROCESS_TERMINATED
A process exited (a PANO_PROCESS_EVENT_DATA with only ProcessId set).
Definition pano_events.h:89
@ PANO_EVENT_PROCESS_CREATED
A process was created (a PANO_PROCESS_EVENT_DATA).
Definition pano_events.h:87

References g_ProcessList, _PANO_PROCESS_INFO::is64Bit, Is64BitProcess(), PANO_EVENT_PROCESS_CREATED, PANO_EVENT_PROCESS_TERMINATED, PanoReportProcessEvent(), _PANO_PROCESS_INFO::ProcessId, _PANO_PROCESS_INFO::ProtectedProcess, PsIsProtectedProcess(), and RemoveProcessInfo().

Referenced by InitializeKernelCallbacks(), and RemoveCallbacks().

◆ ProcessInfoAllocateRoutine()

PVOID NTAPI ProcessInfoAllocateRoutine ( PRTL_AVL_TABLE Table,
CLONG ByteSize )

Definition at line 194 of file callbacks.cpp.

195{
196 UNREFERENCED_PARAMETER(Table);
197 return ExAllocatePool2(POOL_FLAG_NON_PAGED, ByteSize, 'corP');
198}

Referenced by InitializeKernelCallbacks().

◆ ProcessInfoCompareRoutine()

RTL_GENERIC_COMPARE_RESULTS NTAPI ProcessInfoCompareRoutine ( PRTL_AVL_TABLE Table,
PVOID FirstStruct,
PVOID SecondStruct )

Definition at line 177 of file callbacks.cpp.

178{
179 UNREFERENCED_PARAMETER(Table);
180 ULONG_PTR first = (ULONG_PTR)((PPANO_PROCESS_INFO)FirstStruct)->ProcessId;
181 ULONG_PTR second = (ULONG_PTR)((PPANO_PROCESS_INFO)SecondStruct)->ProcessId;
182
183 if (first < second)
184 {
185 return GenericLessThan;
186 }
187 if (first > second)
188 {
189 return GenericGreaterThan;
190 }
191 return GenericEqual;
192}

Referenced by InitializeKernelCallbacks().

◆ ProcessInfoFreeRoutine()

VOID NTAPI ProcessInfoFreeRoutine ( PRTL_AVL_TABLE Table,
PVOID Buffer )

Definition at line 200 of file callbacks.cpp.

201{
202 UNREFERENCED_PARAMETER(Table);
203 ExFreePoolWithTag(Buffer, 'corP');
204}

Referenced by InitializeKernelCallbacks().

◆ RegistryCallbackFunction()

NTSTATUS RegistryCallbackFunction ( PVOID CallbackContext,
PVOID Argument1,
PVOID Argument2 )

Definition at line 997 of file callbacks.cpp.

998{
999 UNREFERENCED_PARAMETER(CallbackContext);
1000
1001 // Argument1 holds the REG_NOTIFY_CLASS value itself, not a pointer to it
1002 REG_NOTIFY_CLASS regClass = (REG_NOTIFY_CLASS)(ULONG_PTR)Argument1;
1003 switch (regClass)
1004 {
1005 case RegNtPreSetValueKey:
1006 ReportSetValueKey((PREG_SET_VALUE_KEY_INFORMATION)Argument2);
1007 break;
1008 case RegNtPreSaveKey:
1009 ReportSaveKey((PREG_SAVE_KEY_INFORMATION)Argument2);
1010 break;
1011 case RegNtPreSetKeySecurity:
1012 ReportSetKeySecurity((PREG_SET_KEY_SECURITY_INFORMATION)Argument2);
1013 break;
1014 case RegNtPreDeleteKey:
1015 {
1016 PREG_DELETE_KEY_INFORMATION deleteInfo = (PREG_DELETE_KEY_INFORMATION)Argument2;
1017 if (deleteInfo != NULL)
1018 {
1019 ReportRegistryKeyEvent(PANO_EVENT_REGISTRY_DELETE_KEY, deleteInfo->Object);
1020 }
1021 break;
1022 }
1023 case RegNtPostCreateKeyEx:
1024 ReportCreateKey((PREG_POST_OPERATION_INFORMATION)Argument2);
1025 break;
1026 case RegNtPostLoadKey:
1027 ReportLoadKey((PREG_POST_OPERATION_INFORMATION)Argument2);
1028 break;
1029 case RegNtPostReplaceKey:
1030 ReportReplaceKey((PREG_POST_OPERATION_INFORMATION)Argument2);
1031 break;
1032 case RegNtPreRenameKey:
1033 BeginRenameKey((PREG_RENAME_KEY_INFORMATION)Argument2);
1034 break;
1035 case RegNtPostRenameKey:
1036 ReportRenameKey((PREG_POST_OPERATION_INFORMATION)Argument2);
1037 break;
1038 case RegNtPostQueryValueKey:
1039 ReportQueryValueKey((PREG_POST_OPERATION_INFORMATION)Argument2);
1040 break;
1041 default:
1042 break;
1043 }
1044
1045 // STATUS_SUCCESS lets the registry operation proceed
1046 return STATUS_SUCCESS;
1047}
@ PANO_EVENT_REGISTRY_DELETE_KEY
A registry key is about to be deleted (a PANO_REGISTRY_EVENT_DATA with only the key name)....

References PANO_EVENT_REGISTRY_DELETE_KEY.

Referenced by InitializeKernelCallbacks().

◆ RemoveCallbacks()

VOID RemoveCallbacks ( )

Definition at line 1239 of file callbacks.cpp.

1240{
1241 PsSetCreateProcessNotifyRoutineEx(ProcessCreateCallback, TRUE);
1242 PsRemoveLoadImageNotifyRoutine(LoadImageNotifyRoutine);
1243 CmUnRegisterCallback(g_RegistryCookie);
1244
1245 PVOID entry;
1246 while ((entry = RtlGetElementGenericTableAvl(&g_ProcessList, 0)) != NULL)
1247 {
1248 RtlDeleteElementGenericTableAvl(&g_ProcessList, entry);
1249 }
1250 return;
1251}

References g_ProcessList, g_RegistryCookie, LoadImageNotifyRoutine(), and ProcessCreateCallback().

Referenced by UnloadPanoptes().

◆ RemoveProcessInfo()

VOID RemoveProcessInfo ( HANDLE ProcessId)

Definition at line 213 of file callbacks.cpp.

214{
215 PANO_PROCESS_INFO key{};
216 key.ProcessId = ProcessId;
217 RtlDeleteElementGenericTableAvl(&g_ProcessList, &key);
218}

References g_ProcessList, and _PANO_PROCESS_INFO::ProcessId.

Referenced by ProcessCreateCallback().

◆ RtlxFindExportedRoutineByName()

PVOID NTAPI RtlxFindExportedRoutineByName ( _In_ PVOID DllBase,
_In_ PANSI_STRING ExportName )

Definition at line 13 of file callbacks.cpp.

16{
17 //
18 // RtlFindExportedRoutineByName is not exported by ntoskrnl until Win10.
19 // Following code is borrowed from ReactOS.
20 //
21
22 PULONG NameTable;
23 PUSHORT OrdinalTable;
24 PIMAGE_EXPORT_DIRECTORY ExportDirectory;
25 LONG Low = 0, Mid = 0, High, Ret;
26 USHORT Ordinal;
27 PVOID Function;
28 ULONG ExportSize;
29 PULONG ExportTable;
30
31 //
32 // Get the export directory.
33 //
34
35 ExportDirectory = (PIMAGE_EXPORT_DIRECTORY)RtlImageDirectoryEntryToData(DllBase,
36 TRUE,
37 IMAGE_DIRECTORY_ENTRY_EXPORT,
38 &ExportSize);
39
40 if (!ExportDirectory)
41 {
42 return NULL;
43 }
44
45 //
46 // Setup name tables.
47 //
48
49 NameTable = (PULONG)((ULONG_PTR)DllBase + ExportDirectory->AddressOfNames);
50 OrdinalTable = (PUSHORT)((ULONG_PTR)DllBase + ExportDirectory->AddressOfNameOrdinals);
51
52 //
53 // Do a binary search.
54 //
55
56 High = ExportDirectory->NumberOfNames - 1;
57 while (High >= Low)
58 {
59 //
60 // Get new middle value.
61 //
62
63 Mid = (Low + High) >> 1;
64
65 //
66 // Compare name.
67 //
68
69 Ret = strcmp(ExportName->Buffer, (PCHAR)DllBase + NameTable[Mid]);
70 if (Ret < 0)
71 {
72 //
73 // Update high.
74 //
75 High = Mid - 1;
76 }
77 else if (Ret > 0)
78 {
79 //
80 // Update low.
81 //
82 Low = Mid + 1;
83 }
84 else
85 {
86 //
87 // We got it.
88 //
89 break;
90 }
91 }
92
93 //
94 // Check if we couldn't find it.
95 //
96
97 if (High < Low)
98 {
99 return NULL;
100 }
101
102 //
103 // Otherwise, this is the ordinal.
104 //
105
106 Ordinal = OrdinalTable[Mid];
107
108 //
109 // Validate the ordinal.
110 //
111
112 if (Ordinal >= ExportDirectory->NumberOfFunctions)
113 {
114 return NULL;
115 }
116
117 //
118 // Resolve the address and write it.
119 //
120
121 ExportTable = (PULONG)((ULONG_PTR)DllBase + ExportDirectory->AddressOfFunctions);
122 Function = (PVOID)((ULONG_PTR)DllBase + ExportTable[Ordinal]);
123
124 //
125 // We found it!
126 //
127
128 NT_ASSERT(
129 (Function < (PVOID)ExportDirectory) ||
130 (Function > (PVOID)((ULONG_PTR)ExportDirectory + ExportSize)));
131
132 return Function;
133}
PVOID NTAPI RtlImageDirectoryEntryToData(_In_ PVOID BaseOfImage, _In_ BOOLEAN MappedAsImage, _In_ USHORT DirectoryEntry, _Out_ PULONG Size)

References RtlImageDirectoryEntryToData().

Referenced by InjectDllKernelApc().

Variable Documentation

◆ g_ProcessList

RTL_AVL_TABLE g_ProcessList

◆ g_RegistryCookie

LARGE_INTEGER g_RegistryCookie

Definition at line 8 of file callbacks.cpp.

Referenced by InitializeKernelCallbacks(), and RemoveCallbacks().