HyperDbg Debugger
Loading...
Searching...
No Matches
MemoryMapper.h
Go to the documentation of this file.
1
15#pragma once
16
18// Definitions //
20
21#define PAGE_4KB_OFFSET ((UINT64)(1 << 12) - 1)
22#define PAGE_2MB_OFFSET ((UINT64)(1 << 21) - 1)
23#define PAGE_4MB_OFFSET ((UINT64)(1 << 22) - 1)
24#define PAGE_1GB_OFFSET ((UINT64)(1 << 30) - 1)
25
27// Enums //
29
39
51
53// Structures //
55
60typedef struct _PAGE_ENTRY
61{
62 union
63 {
65
66 PML4E_64 Pml4;
67 PDPTE_1GB_64 PdptLarge; // 1GB
68 PDPTE_64 Pdpt;
69 PDE_2MB_64 PdLarge; // 2MB
70 PDE_64 Pd;
71 PTE_64 Pt;
72
73 //
74 // Common fields.
75 //
76
77 struct
78 {
95 };
97
103{
104 UINT64 PteVirtualAddressForRead; // The virtual address of PTE for read operations
105 UINT64 VirualAddressForRead; // The actual kernel virtual address to read
106
107 UINT64 PteVirtualAddressForWrite; // The virtual address of PTE for write operations
108 UINT64 VirualAddressForWrite; // The actual kernel virtual address to write
110
112// Functions //
114
115// ----------------------------------------------------------------------------
116// Private Interfaces
117//
118
119static UINT64
121 _In_ UINT64 Va);
122
123static UINT32
125 _In_ UINT64 Va);
126
127static BOOLEAN
129 _In_ CR3_TYPE TargetCr3);
130
131static PVOID
132MemoryMapperMapReservedPageRange(_In_ SIZE_T Size);
133
134static VOID
135MemoryMapperUnmapReservedPageRange(_In_ PVOID VirtualAddress);
136
137static PVOID
138MemoryMapperGetPte(_In_ PVOID VirtualAddress);
139
140static PVOID
141MemoryMapperGetPteByCr3(_In_ PVOID VirtualAddress,
142 _In_ CR3_TYPE TargetCr3);
143
144static PVOID
145MemoryMapperMapPageAndGetPte(_Out_ PUINT64 PteAddress);
146
147static BOOLEAN
148MemoryMapperReadMemorySafeByPte(_In_ PHYSICAL_ADDRESS PaAddressToRead,
149 _Inout_ PVOID BufferToSaveMemory,
150 _In_ SIZE_T SizeToRead,
151 _In_ UINT64 PteVaAddress,
152 _In_ UINT64 MappingVa,
153 _In_ BOOLEAN InvalidateVpids);
154
155static BOOLEAN
156MemoryMapperWriteMemorySafeByPte(_In_ PVOID SourceVA,
157 _In_ PHYSICAL_ADDRESS PaAddressToWrite,
158 _In_ SIZE_T SizeToWrite,
159 _Inout_ UINT64 PteVaAddress,
160 _Inout_ UINT64 MappingVa,
161 _In_ BOOLEAN InvalidateVpids);
162
163static UINT64
166 _In_ UINT64 AddressToRead);
167
168static BOOLEAN
171 _In_ UINT64 AddressToRead,
172 _Inout_ UINT64 BufferToSaveMemory,
173 _In_ SIZE_T SizeToRead);
174
175static UINT64
177 _In_ UINT64 DestinationAddr,
178 _In_opt_ PCR3_TYPE TargetProcessCr3,
179 _In_opt_ UINT32 TargetProcessId);
180
181static BOOLEAN
183 _Inout_ UINT64 DestinationAddr,
184 _In_ UINT64 Source,
185 _In_ SIZE_T SizeToWrite,
186 _In_opt_ PCR3_TYPE TargetProcessCr3,
187 _In_opt_ UINT32 TargetProcessId);
188
189// ----------------------------------------------------------------------------
190// Public Interfaces
191//
192
193VOID
195
196VOID
198
201 _In_ CR3_TYPE TargetCr3);
202
203VOID
204MemoryMapperMapPhysicalAddressToPte(_In_ PHYSICAL_ADDRESS PhysicalAddress,
205 _In_ PVOID TargetProcessVirtualAddress,
206 _In_ CR3_TYPE TargetProcessKernelCr3);
UCHAR BOOLEAN
Definition BasicTypes.h:39
#define VOID
Definition BasicTypes.h:33
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned __int64 * PUINT64
Definition BasicTypes.h:21
unsigned int UINT32
Definition BasicTypes.h:48
enum _PAGING_LEVEL PAGING_LEVEL
Different levels of paging.
_Use_decl_annotations_ BOOLEAN MemoryMapperReadMemorySafeByPte(PHYSICAL_ADDRESS PaAddressToRead, PVOID BufferToSaveMemory, SIZE_T SizeToRead, UINT64 PteVaAddress, UINT64 MappingVa, BOOLEAN InvalidateVpids)
Read memory safely by mapping the buffer using PTE.
Definition MemoryMapper.c:761
_Use_decl_annotations_ UINT64 MemoryMapperGetIndex(PAGING_LEVEL Level, UINT64 Va)
Get Index of VA on PMLx.
Definition MemoryMapper.c:25
_Use_decl_annotations_ BOOLEAN MemoryMapperReadMemorySafeByPhysicalAddressWrapper(MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ TypeOfRead, UINT64 AddressToRead, UINT64 BufferToSaveMemory, SIZE_T SizeToRead)
Wrapper to read the memory safely by mapping the buffer by physical address (It's a wrapper)
Definition MemoryMapper.c:971
_Use_decl_annotations_ UINT64 MemoryMapperWriteMemorySafeWrapperAddressMaker(MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE TypeOfWrite, UINT64 DestinationAddr, PCR3_TYPE TargetProcessCr3, UINT32 TargetProcessId)
Decides about making the address and converting the address to physical address based on the passed p...
Definition MemoryMapper.c:1212
_Use_decl_annotations_ PVOID MemoryMapperMapPageAndGetPte(PUINT64 PteAddress)
This function MAPs one resreved page (4096) and returns its virtual adrresss and also PTE virtual add...
Definition MemoryMapper.c:633
_Use_decl_annotations_ UINT64 MemoryMapperReadMemorySafeByPhysicalAddressWrapperAddressMaker(MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ TypeOfRead, UINT64 AddressToRead)
Wrapper to read the memory safely by mapping the buffer by physical address (It's a wrapper)
Definition MemoryMapper.c:929
_Use_decl_annotations_ BOOLEAN MemoryMapperCheckIfPageIsNxBitSetByCr3(PVOID Va, CR3_TYPE TargetCr3)
This function checks if the page has NX bit or not.
Definition MemoryMapper.c:446
_Use_decl_annotations_ PVOID MemoryMapperGetPte(PVOID VirtualAddress)
This function gets virtual address and returns its PTE (Pml4e) virtual address.
Definition MemoryMapper.c:604
_Use_decl_annotations_ PVOID MemoryMapperGetPteByCr3(PVOID VirtualAddress, CR3_TYPE TargetCr3)
This function gets virtual address and returns its PTE (Pml4e) virtual address based on a specific Cr...
Definition MemoryMapper.c:619
_Use_decl_annotations_ VOID MemoryMapperUnmapReservedPageRange(PVOID VirtualAddress)
This function frees the memory that was previously allocated from system range (without physically al...
Definition MemoryMapper.c:591
_Use_decl_annotations_ PVOID MemoryMapperMapReservedPageRange(SIZE_T Size)
This function reserve memory from system range (without physically allocating them)
Definition MemoryMapper.c:573
_Use_decl_annotations_ BOOLEAN MemoryMapperWriteMemorySafeByPte(PVOID SourceVA, PHYSICAL_ADDRESS PaAddressToWrite, SIZE_T SizeToWrite, UINT64 PteVaAddress, UINT64 MappingVa, BOOLEAN InvalidateVpids)
Write memory safely by mapping the buffer using PTE.
Definition MemoryMapper.c:848
_Use_decl_annotations_ BOOLEAN MemoryMapperWriteMemorySafeWrapper(MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE TypeOfWrite, UINT64 DestinationAddr, UINT64 Source, SIZE_T SizeToWrite, PCR3_TYPE TargetProcessCr3, UINT32 TargetProcessId)
Write memory safely by mapping the buffer (It's a wrapper)
Definition MemoryMapper.c:1276
_Use_decl_annotations_ UINT32 MemoryMapperGetOffset(PAGING_LEVEL Level, UINT64 Va)
Get page offset.
Definition MemoryMapper.c:42
enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ
Memory wrapper for reading safe from the memory.
struct _MEMORY_MAPPER_ADDRESSES * PMEMORY_MAPPER_ADDRESSES
VOID MemoryMapperMapPhysicalAddressToPte(_In_ PHYSICAL_ADDRESS PhysicalAddress, _In_ PVOID TargetProcessVirtualAddress, _In_ CR3_TYPE TargetProcessKernelCr3)
VOID MemoryMapperUninitialize()
uninitialize the Memory Mapper
Definition MemoryMapper.c:716
VOID MemoryMapperInitialize()
Initialize the Memory Mapper.
Definition MemoryMapper.c:661
_MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ
Memory wrapper for reading safe from the memory.
Definition MemoryMapper.h:35
@ MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY
Definition MemoryMapper.h:37
@ MEMORY_MAPPER_WRAPPER_READ_PHYSICAL_MEMORY
Definition MemoryMapper.h:36
struct _PAGE_ENTRY PAGE_ENTRY
Page Entries.
struct _MEMORY_MAPPER_ADDRESSES MEMORY_MAPPER_ADDRESSES
Memory mapper PTE and reserved virtual address.
struct _PAGE_ENTRY * PPAGE_ENTRY
enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE
Memory wrapper for writing safe into the memory.
BOOLEAN MemoryMapperCheckIfPageIsPresentByCr3(_In_ PVOID Va, _In_ CR3_TYPE TargetCr3)
_MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE
Memory wrapper for writing safe into the memory.
Definition MemoryMapper.h:45
@ MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE
Definition MemoryMapper.h:47
@ MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY
Definition MemoryMapper.h:46
@ MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE
Definition MemoryMapper.h:48
CR3 Structure.
Definition BasicTypes.h:130
Memory mapper PTE and reserved virtual address.
Definition MemoryMapper.h:103
UINT64 PteVirtualAddressForRead
Definition MemoryMapper.h:104
UINT64 VirualAddressForRead
Definition MemoryMapper.h:105
UINT64 VirualAddressForWrite
Definition MemoryMapper.h:108
UINT64 PteVirtualAddressForWrite
Definition MemoryMapper.h:107
Page Entries.
Definition MemoryMapper.h:61
UINT64 Write
Definition MemoryMapper.h:80
UINT64 Dirty
Definition MemoryMapper.h:85
UINT64 LargePage
Definition MemoryMapper.h:86
struct _PAGE_ENTRY::@2::@4 Fields
UINT64 Ignored1
Definition MemoryMapper.h:88
UINT64 ExecuteDisable
Definition MemoryMapper.h:93
UINT64 Present
Definition MemoryMapper.h:79
UINT64 ProtectionKey
Definition MemoryMapper.h:92
UINT64 Global
Definition MemoryMapper.h:87
PDE_2MB_64 PdLarge
Definition MemoryMapper.h:69
PML4E_64 Pml4
Definition MemoryMapper.h:66
PDPTE_1GB_64 PdptLarge
Definition MemoryMapper.h:67
UINT64 PageLevelCacheDisable
Definition MemoryMapper.h:83
UINT64 PageFrameNumber
Definition MemoryMapper.h:89
PTE_64 Pt
Definition MemoryMapper.h:71
UINT64 PageLevelWriteThrough
Definition MemoryMapper.h:82
PDPTE_64 Pdpt
Definition MemoryMapper.h:68
UINT64 Supervisor
Definition MemoryMapper.h:81
UINT64 Flags
Definition MemoryMapper.h:64
PDE_64 Pd
Definition MemoryMapper.h:70
UINT64 Ignored2
Definition MemoryMapper.h:91
UINT64 Accessed
Definition MemoryMapper.h:84
UINT64 Reserved1
Definition MemoryMapper.h:90