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

Functions for switching memory layouts. More...

#include "pch.h"

Functions

_Use_decl_annotations_ CR3_TYPE SwitchToProcessMemoryLayout (UINT32 ProcessId)
 Switch to another process's cr3.
 
CR3_TYPE SwitchToCurrentProcessMemoryLayout ()
 Switch to guest's running process's cr3.
 
_Use_decl_annotations_ CR3_TYPE SwitchToProcessMemoryLayoutByCr3 (CR3_TYPE TargetCr3)
 Switch to another process's cr3.
 
_Use_decl_annotations_ VOID SwitchToPreviousProcess (CR3_TYPE PreviousProcess)
 Switch to previous process's cr3.
 

Detailed Description

Functions for switching memory layouts.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.2
Date
2023-04-27

Function Documentation

◆ SwitchToCurrentProcessMemoryLayout()

CR3_TYPE SwitchToCurrentProcessMemoryLayout ( )

Switch to guest's running process's cr3.

this function can be called from vmx-root mode

Returns
CR3_TYPE The cr3 of current process which can be used by SwitchToPreviousProcess function
71{
72 CR3_TYPE GuestCr3;
73 CR3_TYPE CurrentProcessCr3 = {0};
74
76
77 //
78 // Read the current cr3
79 //
80 CurrentProcessCr3.Flags = __readcr3();
81
82 //
83 // Change to a new cr3 (of target process)
84 //
85 __writecr3(GuestCr3.Flags);
86
87 return CurrentProcessCr3;
88}
CR3_TYPE LayoutGetCurrentProcessCr3()
Get cr3 of the target running process.
Definition Layout.c:55
CR3 Structure.
Definition BasicTypes.h:130
UINT64 Flags
Definition BasicTypes.h:133

◆ SwitchToPreviousProcess()

_Use_decl_annotations_ VOID SwitchToPreviousProcess ( CR3_TYPE PreviousProcess)

Switch to previous process's cr3.

Parameters
PreviousProcessCr3 of previous process which is returned by SwitchToProcessMemoryLayout
Returns
VOID
126{
127 //
128 // Restore the original cr3
129 //
130 __writecr3(PreviousProcess.Flags);
131}

◆ SwitchToProcessMemoryLayout()

_Use_decl_annotations_ CR3_TYPE SwitchToProcessMemoryLayout ( UINT32 ProcessId)

Switch to another process's cr3.

this function should NOT be called from vmx-root mode

Parameters
ProcessIdProcessId to switch
Returns
CR3_TYPE The cr3 of current process which can be used by SwitchToPreviousProcess function
26{
27 UINT64 GuestCr3;
28 PEPROCESS TargetEprocess;
29 CR3_TYPE CurrentProcessCr3 = {0};
30
31 if (PsLookupProcessByProcessId((HANDLE)ProcessId, &TargetEprocess) != STATUS_SUCCESS)
32 {
33 //
34 // There was an error, probably the process id was not found
35 //
36 return CurrentProcessCr3;
37 }
38
39 //
40 // Due to KVA Shadowing, we need to switch to a different directory table base
41 // if the PCID indicates this is a user mode directory table base.
42 //
43 NT_KPROCESS * CurrentProcess = (NT_KPROCESS *)(TargetEprocess);
44 GuestCr3 = CurrentProcess->DirectoryTableBase;
45
46 //
47 // Read the current cr3
48 //
49 CurrentProcessCr3.Flags = __readcr3();
50
51 //
52 // Change to a new cr3 (of target process)
53 //
54 __writecr3(GuestCr3);
55
56 ObDereferenceObject(TargetEprocess);
57
58 return CurrentProcessCr3;
59}
unsigned __int64 UINT64
Definition BasicTypes.h:21
KPROCESS Brief structure.
Definition Common.h:265
ULONG_PTR DirectoryTableBase
Definition Common.h:268

◆ SwitchToProcessMemoryLayoutByCr3()

_Use_decl_annotations_ CR3_TYPE SwitchToProcessMemoryLayoutByCr3 ( CR3_TYPE TargetCr3)

Switch to another process's cr3.

Parameters
TargetCr3cr3 to switch
Returns
CR3_TYPE The cr3 of current process which can be used by SwitchToPreviousProcess function
100{
101 CR3_TYPE CurrentProcessCr3 = {0};
102
103 //
104 // Read the current cr3
105 //
106 CurrentProcessCr3.Flags = __readcr3();
107
108 //
109 // Change to a new cr3 (of target process)
110 //
111 __writecr3(TargetCr3.Flags);
112
113 return CurrentProcessCr3;
114}