HyperDbg Debugger
Loading...
Searching...
No Matches
MemoryMapper.h File Reference

Go to the source code of this file.

Classes

struct  _PAGE_ENTRY
 Page Entries. More...
struct  _MEMORY_MAPPER_ADDRESSES
 Memory mapper PTE and reserved virtual address. More...

Macros

#define PAGE_4KB_OFFSET   ((UINT64)(1 << 12) - 1)
#define PAGE_2MB_OFFSET   ((UINT64)(1 << 21) - 1)
#define PAGE_4MB_OFFSET   ((UINT64)(1 << 22) - 1)
#define PAGE_1GB_OFFSET   ((UINT64)(1 << 30) - 1)

Typedefs

typedef enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ
 Memory wrapper for reading safe from the memory.
typedef enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE
 Memory wrapper for writing safe into the memory.
typedef struct _PAGE_ENTRY PAGE_ENTRY
 Page Entries.
typedef struct _PAGE_ENTRYPPAGE_ENTRY
typedef struct _MEMORY_MAPPER_ADDRESSES MEMORY_MAPPER_ADDRESSES
 Memory mapper PTE and reserved virtual address.
typedef struct _MEMORY_MAPPER_ADDRESSESPMEMORY_MAPPER_ADDRESSES

Enumerations

enum  _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ { MEMORY_MAPPER_WRAPPER_READ_PHYSICAL_MEMORY , MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY , MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY_UNSAFE }
 Memory wrapper for reading safe from the memory. More...
enum  _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE { MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY , MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE , MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE }
 Memory wrapper for writing safe into the memory. More...

Functions

VOID MemoryMapperInitialize ()
 Initialize the Memory Mapper.
VOID MemoryMapperUninitialize ()
 uninitialize the Memory Mapper
BOOLEAN MemoryMapperCheckIfPageIsPresentByCr3 (_In_ PVOID Va, _In_ CR3_TYPE TargetCr3)
VOID MemoryMapperMapPhysicalAddressToPte (_In_ PHYSICAL_ADDRESS PhysicalAddress, _In_ PVOID TargetProcessVirtualAddress, _In_ CR3_TYPE TargetProcessKernelCr3)

Macro Definition Documentation

◆ PAGE_1GB_OFFSET

#define PAGE_1GB_OFFSET   ((UINT64)(1 << 30) - 1)

◆ PAGE_2MB_OFFSET

#define PAGE_2MB_OFFSET   ((UINT64)(1 << 21) - 1)

◆ PAGE_4KB_OFFSET

#define PAGE_4KB_OFFSET   ((UINT64)(1 << 12) - 1)

◆ PAGE_4MB_OFFSET

#define PAGE_4MB_OFFSET   ((UINT64)(1 << 22) - 1)

Typedef Documentation

◆ MEMORY_MAPPER_ADDRESSES

Memory mapper PTE and reserved virtual address.

Memory mapper details for each core, contains PTE Virtual Address, Actual Kernel Virtual Address

◆ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ

Memory wrapper for reading safe from the memory.

◆ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE

Memory wrapper for writing safe into the memory.

◆ PAGE_ENTRY

typedef struct _PAGE_ENTRY PAGE_ENTRY

Page Entries.

◆ PMEMORY_MAPPER_ADDRESSES

◆ PPAGE_ENTRY

typedef struct _PAGE_ENTRY * PPAGE_ENTRY

Enumeration Type Documentation

◆ _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ

Memory wrapper for reading safe from the memory.

Enumerator
MEMORY_MAPPER_WRAPPER_READ_PHYSICAL_MEMORY 
MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY 
MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY_UNSAFE 
35{
39
enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ
Memory wrapper for reading safe from the memory.
@ 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

◆ _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE

Memory wrapper for writing safe into the memory.

Enumerator
MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY 
MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE 
MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE 
47{
51
enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE
Memory wrapper for writing safe into the memory.
@ 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

Function Documentation

◆ MemoryMapperCheckIfPageIsPresentByCr3()

BOOLEAN MemoryMapperCheckIfPageIsPresentByCr3 ( _In_ PVOID Va,
_In_ CR3_TYPE TargetCr3 )

◆ MemoryMapperInitialize()

VOID MemoryMapperInitialize ( )

Initialize the Memory Mapper.

This function should be called in vmx non-root in a IRQL <= APC_LEVEL

Returns
VOID
662{
663 UINT64 TempPte;
664 ULONG ProcessorsCount;
665
666 ProcessorsCount = KeQueryActiveProcessorCount(0);
667
668 //
669 // *** Reserve the address for all cores (read pte and va) ***
670 //
671
672 if (g_MemoryMapper != NULL)
673 {
674 //
675 // It's already initialized
676 //
677 return;
678 }
679
680 //
681 // Allocate the memory buffer structure
682 //
684
685 //
686 // Set the core's id and initialize memory mapper
687 //
688 for (SIZE_T i = 0; i < ProcessorsCount; i++)
689 {
690 //
691 // *** Initialize memory mapper for each core ***
692 //
693
694 //
695 // Initial and reserve for read operations
696 //
697 g_MemoryMapper[i].VirualAddressForRead = (UINT64)MemoryMapperMapPageAndGetPte(&TempPte);
698 g_MemoryMapper[i].PteVirtualAddressForRead = TempPte;
699
700 //
701 // Initial and reserve for write operations
702 //
703 g_MemoryMapper[i].VirualAddressForWrite = (UINT64)MemoryMapperMapPageAndGetPte(&TempPte);
704 g_MemoryMapper[i].PteVirtualAddressForWrite = TempPte;
705 }
706}
_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
struct _MEMORY_MAPPER_ADDRESSES MEMORY_MAPPER_ADDRESSES
Memory mapper PTE and reserved virtual address.
PVOID PlatformMemAllocateZeroedNonPagedPool(SIZE_T NumberOfBytes)
Allocates zeroed non-paged pool memory.
Definition PlatformMem.c:248
unsigned long ULONG
Definition BasicTypes.h:31
MEMORY_MAPPER_ADDRESSES * g_MemoryMapper
Save the state of memory mapper.
Definition GlobalVariables.h:44

◆ MemoryMapperMapPhysicalAddressToPte()

VOID MemoryMapperMapPhysicalAddressToPte ( _In_ PHYSICAL_ADDRESS PhysicalAddress,
_In_ PVOID TargetProcessVirtualAddress,
_In_ CR3_TYPE TargetProcessKernelCr3 )

◆ MemoryMapperUninitialize()

VOID MemoryMapperUninitialize ( )

uninitialize the Memory Mapper

This function should be called in vmx non-root in a IRQL <= APC_LEVEL

Returns
VOID
717{
718 ULONG ProcessorsCount = KeQueryActiveProcessorCount(0);
719
720 for (SIZE_T i = 0; i < ProcessorsCount; i++)
721 {
722 //
723 // Unmap and free the reserved buffer
724 //
725 if (g_MemoryMapper[i].VirualAddressForRead != NULL64_ZERO)
726 {
728 }
729
730 if (g_MemoryMapper[i].VirualAddressForWrite != NULL64_ZERO)
731 {
733 }
734
735 g_MemoryMapper[i].VirualAddressForRead = NULL64_ZERO;
736 g_MemoryMapper[i].PteVirtualAddressForRead = NULL64_ZERO;
737
738 g_MemoryMapper[i].VirualAddressForWrite = NULL64_ZERO;
739 g_MemoryMapper[i].PteVirtualAddressForWrite = NULL64_ZERO;
740 }
741
742 //
743 // Set the g_MemoryMapper to null
744 //
746}
_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
void * PVOID
Definition BasicTypes.h:56
#define NULL64_ZERO
Definition BasicTypes.h:111
NULL()
Definition test-case-generator.py:530