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
41
53
55// Structures //
57
62typedef struct _PAGE_ENTRY
63{
64 union
65 {
66 UINT64 Flags;
67
68 PML4E_64 Pml4;
69 PDPTE_1GB_64 PdptLarge; // 1GB
70 PDPTE_64 Pdpt;
71 PDE_2MB_64 PdLarge; // 2MB
72 PDE_64 Pd;
73 PTE_64 Pt;
74
75 //
76 // Common fields.
77 //
78
79 struct
80 {
81 UINT64 Present : 1;
82 UINT64 Write : 1;
83 UINT64 Supervisor : 1;
86 UINT64 Accessed : 1;
87 UINT64 Dirty : 1;
88 UINT64 LargePage : 1;
89 UINT64 Global : 1;
90 UINT64 Ignored1 : 3;
91 UINT64 PageFrameNumber : 36;
92 UINT64 Reserved1 : 4;
93 UINT64 Ignored2 : 7;
94 UINT64 ProtectionKey : 4;
95 UINT64 ExecuteDisable : 1;
97 };
99
105{
106 UINT64 PteVirtualAddressForRead; // The virtual address of PTE for read operations
107 UINT64 VirualAddressForRead; // The actual kernel virtual address to read
108
109 UINT64 PteVirtualAddressForWrite; // The virtual address of PTE for write operations
110 UINT64 VirualAddressForWrite; // The actual kernel virtual address to write
112
114// Functions //
116
117// ----------------------------------------------------------------------------
118// Private Interfaces
119//
120
121static UINT64
123 _In_ UINT64 Va);
124
125static UINT32
127 _In_ UINT64 Va);
128
129static BOOLEAN
131 _In_ CR3_TYPE TargetCr3);
132
133static PVOID
134MemoryMapperMapReservedPageRange(_In_ SIZE_T Size);
135
136static VOID
137MemoryMapperUnmapReservedPageRange(_In_ PVOID VirtualAddress);
138
139static PVOID
140MemoryMapperGetPte(_In_ PVOID VirtualAddress);
141
142static PVOID
143MemoryMapperGetPteByCr3(_In_ PVOID VirtualAddress,
144 _In_ CR3_TYPE TargetCr3);
145
146static PVOID
147MemoryMapperMapPageAndGetPte(_Out_ PUINT64 PteAddress);
148
149static BOOLEAN
150MemoryMapperReadMemorySafeByPte(_In_ PHYSICAL_ADDRESS PaAddressToRead,
151 _Inout_ PVOID BufferToSaveMemory,
152 _In_ SIZE_T SizeToRead,
153 _In_ UINT64 PteVaAddress,
154 _In_ UINT64 MappingVa,
155 _In_ BOOLEAN InvalidateVpids);
156
157static BOOLEAN
159 _In_ PHYSICAL_ADDRESS PaAddressToWrite,
160 _In_ SIZE_T SizeToWrite,
161 _Inout_ UINT64 PteVaAddress,
162 _Inout_ UINT64 MappingVa,
163 _In_ BOOLEAN InvalidateVpids);
164
165static UINT64
168 _In_ UINT64 AddressToRead,
169 _In_ UINT32 TargetProcessId);
170
171static BOOLEAN
174 _In_ UINT64 AddressToRead,
175 _Inout_ UINT64 BufferToSaveMemory,
176 _In_ SIZE_T SizeToRead,
177 _In_ UINT32 TargetProcessId);
178
179static UINT64
181 _In_ UINT64 DestinationAddr,
182 _In_opt_ PCR3_TYPE TargetProcessCr3,
183 _In_opt_ UINT32 TargetProcessId);
184
185static BOOLEAN
187 _Inout_ UINT64 DestinationAddr,
188 _In_ UINT64 Source,
189 _In_ SIZE_T SizeToWrite,
190 _In_opt_ PCR3_TYPE TargetProcessCr3,
191 _In_opt_ UINT32 TargetProcessId);
192
193// ----------------------------------------------------------------------------
194// Public Interfaces
195//
196
197VOID
199
200VOID
202
205 _In_ CR3_TYPE TargetCr3);
206
207VOID
208MemoryMapperMapPhysicalAddressToPte(_In_ PHYSICAL_ADDRESS PhysicalAddress,
209 _In_ PVOID TargetProcessVirtualAddress,
210 _In_ CR3_TYPE TargetProcessKernelCr3);
_Use_decl_annotations_ BOOLEAN MemoryMapperReadMemorySafeWrapper(MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ TypeOfRead, UINT64 AddressToRead, UINT64 BufferToSaveMemory, SIZE_T SizeToRead, UINT32 TargetProcessId)
Wrapper to read the memory safely by mapping the buffer by physical address (It's a wrapper).
Definition MemoryMapper.c:989
_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_ UINT64 MemoryMapperReadMemorySafeByPhysicalAddressWrapperAddressMaker(MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ TypeOfRead, UINT64 AddressToRead, UINT32 TargetProcessId)
Wrapper to read the memory safely by mapping the buffer by physical address (It's a wrapper).
Definition MemoryMapper.c:931
_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:1257
_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_ 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:1321
_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_VIRTUAL_MEMORY_UNSAFE
Definition MemoryMapper.h:38
@ 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:47
@ MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE
Definition MemoryMapper.h:49
@ MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY
Definition MemoryMapper.h:48
@ MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE
Definition MemoryMapper.h:50
UCHAR BOOLEAN
Definition BasicTypes.h:35
void * PVOID
Definition BasicTypes.h:56
struct _CR3_TYPE CR3_TYPE
CR3 Structure.
unsigned int UINT32
Definition BasicTypes.h:54
struct _CR3_TYPE * PCR3_TYPE
enum _PAGING_LEVEL PAGING_LEVEL
Different levels of paging.
Memory mapper PTE and reserved virtual address.
Definition MemoryMapper.h:105
UINT64 PteVirtualAddressForRead
Definition MemoryMapper.h:106
UINT64 VirualAddressForRead
Definition MemoryMapper.h:107
UINT64 VirualAddressForWrite
Definition MemoryMapper.h:110
UINT64 PteVirtualAddressForWrite
Definition MemoryMapper.h:109
Page Entries.
Definition MemoryMapper.h:63
UINT64 Write
Definition MemoryMapper.h:82
UINT64 Dirty
Definition MemoryMapper.h:87
UINT64 LargePage
Definition MemoryMapper.h:88
UINT64 Ignored1
Definition MemoryMapper.h:90
UINT64 ExecuteDisable
Definition MemoryMapper.h:95
UINT64 Present
Definition MemoryMapper.h:81
UINT64 ProtectionKey
Definition MemoryMapper.h:94
UINT64 Global
Definition MemoryMapper.h:89
struct _PAGE_ENTRY::@376376202121063303120064260270315140365371076130::@103064255274005116234107103217043340214273361203 Fields
PDE_2MB_64 PdLarge
Definition MemoryMapper.h:71
PML4E_64 Pml4
Definition MemoryMapper.h:68
PDPTE_1GB_64 PdptLarge
Definition MemoryMapper.h:69
UINT64 PageLevelCacheDisable
Definition MemoryMapper.h:85
UINT64 PageFrameNumber
Definition MemoryMapper.h:91
PTE_64 Pt
Definition MemoryMapper.h:73
UINT64 PageLevelWriteThrough
Definition MemoryMapper.h:84
PDPTE_64 Pdpt
Definition MemoryMapper.h:70
UINT64 Supervisor
Definition MemoryMapper.h:83
UINT64 Flags
Definition MemoryMapper.h:66
PDE_64 Pd
Definition MemoryMapper.h:72
UINT64 Ignored2
Definition MemoryMapper.h:93
UINT64 Accessed
Definition MemoryMapper.h:86
UINT64 Reserved1
Definition MemoryMapper.h:92