Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
inject.cpp File Reference
#include "structs.h"
#include "inject.h"

Go to the source code of this file.

Classes

struct  InjectArgs
 

Typedefs

typedef NTSTATUS(NTAPI * PLDRLOADDLL) (PWCHAR PathToFile, ULONG Flags, PUNICODE_STRING ModuleFileName, PHANDLE ModuleHandle)
 

Functions

NTSTATUS WriteToTargetProcessMemory (PEPROCESS targetProcess, PVOID TargetAddress, SIZE_T Size, PVOID DataToWrite)
 
NTSTATUS AllocateMemoryInUserProcess (PEPROCESS targetProcess, SIZE_T Size, PVOID *AllocatedAddress)
 

Typedef Documentation

◆ PLDRLOADDLL

typedef NTSTATUS(NTAPI * PLDRLOADDLL) (PWCHAR PathToFile, ULONG Flags, PUNICODE_STRING ModuleFileName, PHANDLE ModuleHandle)

Definition at line 4 of file inject.cpp.

Function Documentation

◆ AllocateMemoryInUserProcess()

NTSTATUS AllocateMemoryInUserProcess ( PEPROCESS  targetProcess,
SIZE_T  Size,
PVOID *  AllocatedAddress 
)

Definition at line 69 of file inject.cpp.

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}

◆ WriteToTargetProcessMemory()

NTSTATUS WriteToTargetProcessMemory ( PEPROCESS  targetProcess,
PVOID  TargetAddress,
SIZE_T  Size,
PVOID  DataToWrite 
)

Definition at line 11 of file inject.cpp.

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}