Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
inject.cpp
Go to the documentation of this file.
1#include "structs.h"
2#include "inject.h"
3
4typedef NTSTATUS(NTAPI* PLDRLOADDLL)(
5 PWCHAR PathToFile,
6 ULONG Flags,
7 PUNICODE_STRING ModuleFileName,
8 PHANDLE ModuleHandle
9 );
10
11NTSTATUS WriteToTargetProcessMemory(PEPROCESS targetProcess, PVOID TargetAddress, SIZE_T Size, PVOID DataToWrite)
12{
13 KAPC_STATE apcState;
14 PMDL mdl = NULL;
15 PVOID kernelAddress = NULL;
16 NTSTATUS status = STATUS_SUCCESS;
17
18 // Attach to the target process
19 KeStackAttachProcess(targetProcess, &apcState);
20
21 __try {
22 // Create the MDL for the target process memory
23 mdl = IoAllocateMdl(TargetAddress, (ULONG)Size, FALSE, FALSE, NULL);
24 if (!mdl) {
25 status = STATUS_INSUFFICIENT_RESOURCES;
26 __leave;
27 }
28
29 __try {
30 // Probe and lock the pages
31 MmProbeAndLockPages(mdl, KernelMode, IoWriteAccess);
32
33 // Map the MDL to system address space
34 kernelAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
35 if (!kernelAddress) {
36 status = STATUS_INSUFFICIENT_RESOURCES;
37 __leave;
38 }
39
40 // Write to the mapped address
41 RtlCopyMemory(kernelAddress, DataToWrite, Size);
42 }
43 __except (EXCEPTION_EXECUTE_HANDLER) {
44 status = GetExceptionCode();
45 }
46 }
47 __finally {
48 // Cleanup
49 if (kernelAddress) {
50 MmUnmapLockedPages(kernelAddress, mdl);
51 }
52 if (mdl) {
53 if (mdl->MdlFlags & MDL_PAGES_LOCKED) {
54 MmUnlockPages(mdl);
55 }
56 IoFreeMdl(mdl);
57 }
58
59 // Detach from the target process
60 KeUnstackDetachProcess(&apcState);
61
62 // Dereference the process
63 ObDereferenceObject(targetProcess);
64 }
65
66 return status;
67}
68
69NTSTATUS AllocateMemoryInUserProcess(PEPROCESS targetProcess, SIZE_T Size, PVOID* AllocatedAddress)
70{
71 HANDLE processHandle = NULL;
72 NTSTATUS status;
73 SIZE_T allocSize = Size;
74 PVOID baseAddress = NULL;
75
76 // Open a handle to the process
77 status = ObOpenObjectByPointer(targetProcess,
78 OBJ_KERNEL_HANDLE,
79 NULL,
80 PROCESS_ALL_ACCESS,
81 *PsProcessType,
82 KernelMode,
83 &processHandle);
84
85 if (NT_SUCCESS(status)) {
86 // Allocate memory in the target process
87 status = ZwAllocateVirtualMemory(processHandle,
88 &baseAddress,
89 0,
90 &allocSize,
91 MEM_COMMIT | MEM_RESERVE,
92 PAGE_READWRITE);
93
94 if (NT_SUCCESS(status)) {
95 //RtlZeroMemory(baseAddress, Size);
96 *AllocatedAddress = baseAddress;
97 }
98
99 // Close the process handle
100 ZwClose(processHandle);
101 }
102
103 // Dereference the process
104 ObDereferenceObject(targetProcess);
105
106 return status;
107}
108
109//void kernel_free_kapc(PKAPC apc, PKNORMAL_ROUTINE*, PVOID*, PVOID*, PVOID*)
110//{
111// delete apc;
112// ExReleaseRundownProtection(&PendingOperations);
113//
114//}
115//
116//void rundown_free_kapc(PKAPC apc)
117//{
118// delete apc;
119// ExReleaseRundownProtection(&PendingOperations);
120//
121//}
122
123struct InjectArgs {
124 PCWSTR DLLPath;
125 PUNICODE_STRING DllName;
126 PVOID* DllHandle;
127};
128
129//
130//bool InjectDLL(HANDLE ProcessId, PVOID processInfo)
131//{
132// PPANO_PROCESS_INFO panoProcessInfo = (PPANO_PROCESS_INFO)processInfo;
133// InjectArgs args;
134// args.DllHandle = NULL;
135// args.DLLPath = L"C:\\Program Files\\Panoptes";
136// PVOID allocatedAddressContainingShellcode;
137// PVOID shellcodeAddress;
138// // Get the EPROCESS pointer for the target process
139// PEPROCESS targetProcess = NULL;
140// NTSTATUS status = PsLookupProcessByProcessId(ProcessId, &targetProcess);
141// if (!NT_SUCCESS(status)) {
142// return status;
143// }
144//
145// if (panoProcessInfo->Is64Bit)
146// {
147// UNICODE_STRING moduleName;
148// RtlInitUnicodeString(&moduleName, L"PanoptesDLLx64.dll");
149// args.DllName = &moduleName;
150// SIZE_T dllPathx64Size = sizeof(_code_rawx64);
151// status = AllocateMemoryInUserProcess(targetProcess, dllPathx64Size, &allocatedAddressContainingShellcode);
152// if (!NT_SUCCESS(status)) {
153// return false;
154// }
155// status = WriteToTargetProcessMemory(targetProcess, &allocatedAddressContainingShellcode, dllPathx64Size, _code_rawx64);
156// shellcodeAddress = (PVOID)((UCHAR*)allocatedAddressContainingShellcode + FUNCTION_OFFSETx64);
157// }
158// else
159// {
160// UNICODE_STRING moduleName;
161// RtlInitUnicodeString(&moduleName, L"PanoptesDLLx86.dll");
162// args.DllName = &moduleName;
163// SIZE_T dllPathx86Size = sizeof(_code_rawx86);
164// status = AllocateMemoryInUserProcess(targetProcess, dllPathx86Size, &allocatedAddressContainingShellcode);
165// if (!NT_SUCCESS(status)) {
166// return false;
167// }
168// status = WriteToTargetProcessMemory(targetProcess, &allocatedAddressContainingShellcode, dllPathx86Size, _code_rawx86);
169// shellcodeAddress = (PVOID)((UCHAR*)allocatedAddressContainingShellcode + FUNCTION_OFFSETx86);
170// }
171//
172// // Attach to the target process
173// KAPC_STATE ApcState;
174// KeStackAttachProcess(targetProcess, &ApcState);
175//
176// KAPC* apc = static_cast<KAPC*>(ExAllocatePool2(POOL_FLAG_NON_PAGED, sizeof(KAPC), 'ldll'));
177// if (nullptr == apc) {
178// return false;
179// }
180//
181// DbgBreakPoint();
182//
183// KeInitializeApc(
184// apc,
185// KeGetCurrentThread(),
186// OriginalApcEnvironment,
187// NULL,
188// NULL,
189// reinterpret_cast<PKNORMAL_ROUTINE>(shellcodeAddress),
190// UserMode,
191// &args
192// );
193//
194// if (!KeInsertQueueApc(
195// apc,
196// NULL,
197// NULL,
198// IO_NO_INCREMENT
199// ))
200// {
201// if (apc) {
202// ExFreePoolWithTag(apc, 0);
203// }
204// KeUnstackDetachProcess(&ApcState);
205// return false;
206// }
207//
208// KeUnstackDetachProcess(&ApcState);
209// //panoProcessInfo->Injected = true;
210// return true;
211//}
NTSTATUS(NTAPI * PLDRLOADDLL)(PWCHAR PathToFile, ULONG Flags, PUNICODE_STRING ModuleFileName, PHANDLE ModuleHandle)
Definition inject.cpp:4
NTSTATUS AllocateMemoryInUserProcess(PEPROCESS targetProcess, SIZE_T Size, PVOID *AllocatedAddress)
Definition inject.cpp:69
NTSTATUS WriteToTargetProcessMemory(PEPROCESS targetProcess, PVOID TargetAddress, SIZE_T Size, PVOID DataToWrite)
Definition inject.cpp:11
PVOID * DllHandle
Definition inject.cpp:126
PUNICODE_STRING DllName
PCWSTR DLLPath
Definition inject.cpp:124