HyperDbg Debugger
Loading...
Searching...
No Matches
MemoryManager.c File Reference

Reading/Writing memory and all memory affairs. More...

#include "pch.h"

Functions

BOOLEAN ReadPhysicalMemoryUsingMapIoSpace (PVOID PhysicalAddress, PVOID Buffer, SIZE_T BufferSize)
 Read physical memory from the physical address (used in VMI mode).
BOOLEAN WritePhysicalMemoryUsingMapIoSpace (PVOID PhysicalAddress, PVOID Buffer, SIZE_T BufferSize)
 Write physical memory to the physical address (used in VMI mode).
BOOLEAN MemoryManagerReadProcessMemoryNormal (HANDLE PID, PVOID Address, DEBUGGER_READ_MEMORY_TYPE MemType, PVOID UserBuffer, SIZE_T Size, PSIZE_T ReturnSize)
 Read process memory.
BOOLEAN MemoryManagerWritePhysicalMemoryNormal (PVOID TargetAddress, PVOID UserBuffer, SIZE_T Size)
 Write process memory.

Detailed Description

Reading/Writing memory and all memory affairs.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-24

Function Documentation

◆ MemoryManagerReadProcessMemoryNormal()

BOOLEAN MemoryManagerReadProcessMemoryNormal ( HANDLE PID,
PVOID Address,
DEBUGGER_READ_MEMORY_TYPE MemType,
PVOID UserBuffer,
SIZE_T Size,
PSIZE_T ReturnSize )

Read process memory.

This function should not be called from vmx-root mode

Parameters
PIDTarget Process Id
AddressTarget Address
MemTypeType of memory
UserBufferBuffer to save to the user. This buffer must be in nonpageable memory.
SizeSize of read
ReturnSizeReturn Size
Returns
BOOLEAN
92{
93 PEPROCESS SourceProcess;
94 MM_COPY_ADDRESS CopyAddress = {0};
95 KAPC_STATE State = {0};
96 PHYSICAL_ADDRESS TempPhysicalAddress = {0};
97
98 //
99 // Check if we want another process memory, this way we attach to that process
100 // the find the physical address of the memory and read it from here using physical
101 // address
102
103 //
104 // The second thing that we consider here is reading a physical address doesn't
105 // need to attach to another process
106 //
107 if (PsGetCurrentProcessId() != PID && MemType == DEBUGGER_READ_VIRTUAL_ADDRESS)
108 {
109 //
110 // User needs another process memory
111 //
112
113 if (PsLookupProcessByProcessId(PID, &SourceProcess) != STATUS_SUCCESS)
114 {
115 //
116 // if the process not found
117 //
118 return FALSE;
119 }
120 __try
121 {
122 KeStackAttachProcess(SourceProcess, &State);
123
124 //
125 // We're in context of another process let's read the memory
126 //
127 TempPhysicalAddress = MmGetPhysicalAddress(Address);
128
129 KeUnstackDetachProcess(&State);
130
131 //
132 // Now we have to read the physical address
133 //
134 CopyAddress.PhysicalAddress.QuadPart = TempPhysicalAddress.QuadPart;
135 MmCopyMemory(UserBuffer, CopyAddress, Size, MM_COPY_MEMORY_PHYSICAL, ReturnSize);
136
137 ObDereferenceObject(SourceProcess);
138
139 return TRUE;
140 }
141 __except (EXCEPTION_EXECUTE_HANDLER)
142 {
143 KeUnstackDetachProcess(&State);
144
145 ObDereferenceObject(SourceProcess);
146
147 return FALSE;
148 }
149 }
150 else
151 {
152 //
153 // Process needs itself memory
154 //
155 __try
156 {
157 if (MemType == DEBUGGER_READ_VIRTUAL_ADDRESS)
158 {
159 CopyAddress.VirtualAddress = Address;
160 MmCopyMemory(UserBuffer, CopyAddress, Size, MM_COPY_MEMORY_VIRTUAL, ReturnSize);
161 }
162 else if (MemType == DEBUGGER_READ_PHYSICAL_ADDRESS)
163 {
164 //
165 // Check whether the physical memory is valid or not
166 //
167 if (!CheckAddressPhysical((UINT64)Address))
168 {
169 return FALSE;
170 }
171
172 CopyAddress.PhysicalAddress.QuadPart = (LONGLONG)Address;
173
174 if (MmCopyMemory(UserBuffer, CopyAddress, Size, MM_COPY_MEMORY_PHYSICAL, ReturnSize) != STATUS_SUCCESS && *ReturnSize == 0)
175 {
176 //
177 // If the memory is not readable, it might be an MMIO address
178 // Try again with another method
179 //
180
181 // LogInfo("reading using MMIO Map IO Space using VMCALL");
182
183 if (AsmVmxVmcall(VMCALL_READ_PHYSICAL_MEMORY, (UINT64)Address, (UINT64)UserBuffer, (UINT64)Size) == STATUS_SUCCESS)
184 {
185 *ReturnSize = Size;
186 return TRUE;
187 }
188 else
189 {
190 //
191 // unknown error
192 //
193 *ReturnSize = 0;
194 return FALSE;
195 }
196 }
197 }
198 else
199 {
200 //
201 // Type is not recognized
202 //
203 return FALSE;
204 }
205
206 //
207 // MmCopyVirtualMemory(SourceProcess, Address, TargetProcess, UserBuffer, Size, KernelMode, ReturnSize);
208 // memcpy(UserBuffer, Address, Size);
209 //
210
211 return TRUE;
212 }
213 __except (EXCEPTION_EXECUTE_HANDLER)
214 {
215 return FALSE;
216 }
217 }
218}
NTSTATUS AsmVmxVmcall(UINT64 VmcallNumber, UINT64 OptionalParam1, UINT64 OptionalParam2, UINT64 OptionalParam3)
Request Vmcall.
#define VMCALL_READ_PHYSICAL_MEMORY
VMCALL to read physical memory.
Definition Vmcall.h:315
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
@ DEBUGGER_READ_PHYSICAL_ADDRESS
Definition RequestStructures.h:255
@ DEBUGGER_READ_VIRTUAL_ADDRESS
Definition RequestStructures.h:256
IMPORT_EXPORT_VMM BOOLEAN CheckAddressPhysical(UINT64 PAddr)
Checks if the physical address is correct or not based on physical address width.
Definition AddressCheck.c:120

◆ MemoryManagerWritePhysicalMemoryNormal()

BOOLEAN MemoryManagerWritePhysicalMemoryNormal ( PVOID TargetAddress,
PVOID UserBuffer,
SIZE_T Size )

Write process memory.

This function should not be called from vmx-root mode

Parameters
TargetAddressTarget Address
UserBufferBuffer to write to the memory
SizeSize of the buffer
Returns
BOOLEAN
235{
236 if (AsmVmxVmcall(VMCALL_WRITE_PHYSICAL_MEMORY, (UINT64)TargetAddress, (UINT64)UserBuffer, (UINT64)Size) == STATUS_SUCCESS)
237 {
238 return TRUE;
239 }
240 else
241 {
242 return FALSE;
243 }
244}
#define VMCALL_WRITE_PHYSICAL_MEMORY
VMCALL to write physical memory.
Definition Vmcall.h:321

◆ ReadPhysicalMemoryUsingMapIoSpace()

BOOLEAN ReadPhysicalMemoryUsingMapIoSpace ( PVOID PhysicalAddress,
PVOID Buffer,
SIZE_T BufferSize )

Read physical memory from the physical address (used in VMI mode).

Parameters
PhysicalAddressPhysical Address
BufferBuffer to save the memory
BufferSizeSize of the buffer
Returns
BOOLEAN
25{
26 PHYSICAL_ADDRESS PhysicalAddressTemp = {0};
27 PhysicalAddressTemp.QuadPart = (LONGLONG)PhysicalAddress;
28
29 PVOID VirtualAddress = MmMapIoSpaceEx(PhysicalAddressTemp, BufferSize, PAGE_READWRITE | PAGE_NOCACHE);
30
31 if (VirtualAddress == NULL)
32 {
33 return FALSE;
34 }
35
36 RtlCopyMemory(Buffer, VirtualAddress, BufferSize);
37
38 MmUnmapIoSpace(VirtualAddress, BufferSize);
39
40 return TRUE;
41}
void * PVOID
Definition BasicTypes.h:56

◆ WritePhysicalMemoryUsingMapIoSpace()

BOOLEAN WritePhysicalMemoryUsingMapIoSpace ( PVOID PhysicalAddress,
PVOID Buffer,
SIZE_T BufferSize )

Write physical memory to the physical address (used in VMI mode).

Parameters
PhysicalAddressPhysical Address
BufferBuffer to write to the memory
BufferSizeSize of the buffer
Returns
BOOLEAN
54{
55 PHYSICAL_ADDRESS PhysicalAddressTemp = {0};
56 PhysicalAddressTemp.QuadPart = (LONGLONG)PhysicalAddress;
57
58 PVOID VirtualAddress = MmMapIoSpaceEx(PhysicalAddressTemp, BufferSize, PAGE_READWRITE | PAGE_NOCACHE);
59 if (VirtualAddress == NULL)
60 {
61 return FALSE;
62 }
63
64 RtlCopyMemory(VirtualAddress, Buffer, BufferSize);
65
66 MmUnmapIoSpace(VirtualAddress, BufferSize);
67
68 return TRUE;
69}