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

Header files for common functions. More...

Go to the source code of this file.

Classes

struct  _CPUID
 CPUID Registers. More...
union  _CR_FIXED
struct  _NT_KPROCESS
 KPROCESS Brief structure. More...

Macros

#define __CPU_INDEX__   KeGetCurrentProcessorNumberEx(NULL)
 Core Id.
#define ALIGNMENT_PAGE_SIZE   4096
 Alignment Size.
#define MAXIMUM_ADDRESS   0xffffffffffffffff
 Maximum x64 Address.
#define DPL_USER   3
 System and User ring definitions.
#define DPL_SYSTEM   0
#define RPL_MASK   3
 RPL Mask.
#define BITS_PER_LONG   (sizeof(ULONG) * 8)
#define ORDER_LONG   (sizeof(ULONG) == 4 ? 5 : 6)
#define BITMAP_ENTRY(_nr, _bmap)
#define BITMAP_SHIFT(_nr)
#define PAGE_OFFSET(Va)
 Offset from a page's 4096 bytes.
#define _XBEGIN_STARTED   (~0u)
 Intel TSX Constants.
#define _XABORT_EXPLICIT   (1 << 0)
#define _XABORT_RETRY   (1 << 1)
#define _XABORT_CONFLICT   (1 << 2)
#define _XABORT_CAPACITY   (1 << 3)
#define _XABORT_DEBUG   (1 << 4)
#define _XABORT_NESTED   (1 << 5)
#define _XABORT_CODE(x)

Typedefs

typedef SEGMENT_DESCRIPTOR_32 * PSEGMENT_DESCRIPTOR
typedef struct _CPUID CPUID
 CPUID Registers.
typedef struct _CPUIDPCPUID
typedef union _CR_FIXED CR_FIXED
typedef union _CR_FIXEDPCR_FIXED
typedef struct _NT_KPROCESS NT_KPROCESS
 KPROCESS Brief structure.
typedef struct _NT_KPROCESSPNT_KPROCESS
typedef VOID(* RunOnLogicalCoreFunc) (ULONG ProcessorId)
 Prototype to run a function on a logical core.

Functions

UCHARPsGetProcessImageFileName (IN PEPROCESS Process)
BOOLEAN CommonAffinityBroadcastToProcessors (_In_ ULONG ProcessorNumber, _In_ RunOnLogicalCoreFunc Routine)
BOOLEAN CommonIsStringStartsWith (const CHAR *pre, const CHAR *str)
 Detects whether the string starts with another string.
BOOLEAN CommonIsGuestOnUsermode32Bit ()
 determines if the guest was in 32-bit user-mode or 64-bit (long mode)
PCHAR CommonGetProcessNameFromProcessControlBlock (PEPROCESS eprocess)
 Get process name by eprocess.
VOID CommonCpuidInstruction (UINT32 Func, UINT32 SubFunc, INT *CpuInfo)
 Get cpuid results.
VOID CommonWriteDebugInformation (VIRTUAL_MACHINE_STATE *VCpu)
 Produce debug information from unrecoverable bugs.
BOOLEAN CommonIsXCr0Valid (XCR0 XCr0)
 Check correctness of the XCR0 register value.

Detailed Description

Header files for common functions.

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

Macro Definition Documentation

◆ __CPU_INDEX__

#define __CPU_INDEX__   KeGetCurrentProcessorNumberEx(NULL)

Core Id.

◆ _XABORT_CAPACITY

#define _XABORT_CAPACITY   (1 << 3)

◆ _XABORT_CODE

#define _XABORT_CODE ( x)
Value:
(((x) >> 24) & 0xFF)
x
Definition 01-expressions-correct.txt:2

◆ _XABORT_CONFLICT

#define _XABORT_CONFLICT   (1 << 2)

◆ _XABORT_DEBUG

#define _XABORT_DEBUG   (1 << 4)

◆ _XABORT_EXPLICIT

#define _XABORT_EXPLICIT   (1 << 0)

◆ _XABORT_NESTED

#define _XABORT_NESTED   (1 << 5)

◆ _XABORT_RETRY

#define _XABORT_RETRY   (1 << 1)

◆ _XBEGIN_STARTED

#define _XBEGIN_STARTED   (~0u)

Intel TSX Constants.

◆ ALIGNMENT_PAGE_SIZE

#define ALIGNMENT_PAGE_SIZE   4096

Alignment Size.

◆ BITMAP_ENTRY

#define BITMAP_ENTRY ( _nr,
_bmap )
Value:
((_bmap))[(_nr) / BITS_PER_LONG]
#define BITS_PER_LONG
Definition Common.h:45

◆ BITMAP_SHIFT

#define BITMAP_SHIFT ( _nr)
Value:
((_nr) % BITS_PER_LONG)

◆ BITS_PER_LONG

#define BITS_PER_LONG   (sizeof(ULONG) * 8)

◆ DPL_SYSTEM

#define DPL_SYSTEM   0

◆ DPL_USER

#define DPL_USER   3

System and User ring definitions.

◆ MAXIMUM_ADDRESS

#define MAXIMUM_ADDRESS   0xffffffffffffffff

Maximum x64 Address.

◆ ORDER_LONG

#define ORDER_LONG   (sizeof(ULONG) == 4 ? 5 : 6)

◆ PAGE_OFFSET

#define PAGE_OFFSET ( Va)
Value:
((PVOID)((ULONG_PTR)(Va) & (PAGE_SIZE - 1)))
void * PVOID
Definition BasicTypes.h:56
#define PAGE_SIZE
Size of each page (4096 bytes).
Definition common.h:80

Offset from a page's 4096 bytes.

◆ RPL_MASK

#define RPL_MASK   3

RPL Mask.

Typedef Documentation

◆ CPUID

typedef struct _CPUID CPUID

CPUID Registers.

◆ CR_FIXED

typedef union _CR_FIXED CR_FIXED

◆ NT_KPROCESS

typedef struct _NT_KPROCESS NT_KPROCESS

KPROCESS Brief structure.

◆ PCPUID

typedef struct _CPUID * PCPUID

◆ PCR_FIXED

typedef union _CR_FIXED * PCR_FIXED

◆ PNT_KPROCESS

typedef struct _NT_KPROCESS * PNT_KPROCESS

◆ PSEGMENT_DESCRIPTOR

typedef SEGMENT_DESCRIPTOR_32* PSEGMENT_DESCRIPTOR

◆ RunOnLogicalCoreFunc

typedef VOID(* RunOnLogicalCoreFunc) (ULONG ProcessorId)

Prototype to run a function on a logical core.

Function Documentation

◆ CommonAffinityBroadcastToProcessors()

BOOLEAN CommonAffinityBroadcastToProcessors ( _In_ ULONG ProcessorNumber,
_In_ RunOnLogicalCoreFunc Routine )

◆ CommonCpuidInstruction()

VOID CommonCpuidInstruction ( UINT32 Func,
UINT32 SubFunc,
INT * CpuInfo )

Get cpuid results.

Parameters
Func
SubFunc
CpuInfo
Returns
VOID
86{
87 CpuCpuIdEx(CpuInfo, Func, SubFunc);
88}
VOID CpuCpuIdEx(INT32 *CpuInfo, INT32 FunctionId, INT32 SubFunctionId)
Execute CPUID with sub-leaf.
Definition PlatformIntrinsics.c:274

◆ CommonGetProcessNameFromProcessControlBlock()

PCHAR CommonGetProcessNameFromProcessControlBlock ( PEPROCESS Eprocess)

Get process name by eprocess.

Parameters
EprocessProcess eprocess
Returns
PCHAR Returns a pointer to the process name
49{
50 PCHAR Result = 0;
51
52 //
53 // We can't use PsLookupProcessByProcessId as in pageable and not
54 // work on vmx-root
55 //
56 Result = (CHAR *)PsGetProcessImageFileName(Eprocess);
57
58 return Result;
59}
char CHAR
Definition BasicTypes.h:33
UCHAR * PsGetProcessImageFileName(IN PEPROCESS Process)

◆ CommonIsGuestOnUsermode32Bit()

BOOLEAN CommonIsGuestOnUsermode32Bit ( )

determines if the guest was in 32-bit user-mode or 64-bit (long mode)

this function should be called from vmx-root

Returns
BOOLEAN
98{
99 //
100 // Only 16 bit is needed however, VMWRITE might write on other bits
101 // and corrupt other variables, that's why we get 64bit
102 //
103 UINT64 CsSel = NULL64_ZERO;
104
105 //
106 // Read guest's cs selector
107 //
108 CsSel = HvGetCsSelector();
109
110 if (CsSel == KGDT64_R0_CODE)
111 {
112 //
113 // 64-bit kernel-mode
114 //
115 return FALSE;
116 }
117 else if ((CsSel & ~3) == KGDT64_R3_CODE)
118 {
119 //
120 // 64-bit user-mode
121 //
122 return FALSE;
123 }
124 else if ((CsSel & ~3) == KGDT64_R3_CMCODE)
125 {
126 //
127 // 32-bit user-mode
128 //
129 return TRUE;
130 }
131 else
132 {
133 LogError("Err, unknown value for cs, cannot determine wow64 mode");
134 }
135
136 //
137 // By default, 64-bit
138 //
139 return FALSE;
140}
UINT16 HvGetCsSelector()
Read CS selector.
Definition Hv.c:1178
#define NULL64_ZERO
Definition BasicTypes.h:111
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
#define KGDT64_R3_CODE
Definition Constants.h:659
#define KGDT64_R0_CODE
Definition Constants.h:655
#define KGDT64_R3_CMCODE
Definition Constants.h:657
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113

◆ CommonIsStringStartsWith()

BOOLEAN CommonIsStringStartsWith ( const CHAR * pre,
const CHAR * str )

Detects whether the string starts with another string.

Parameters
pre
str
Returns
BOOLEAN Returns true if it starts with and false if not strats with
70{
71 SIZE_T LenPre = strlen(pre),
72 LenStr = strlen(str);
73 return LenStr < LenPre ? FALSE : memcmp(pre, str, LenPre) == 0;
74}

◆ CommonIsXCr0Valid()

BOOLEAN CommonIsXCr0Valid ( XCR0 XCr0)

Check correctness of the XCR0 register value.

Parameters
XCr0The XCR0 register value.
Returns
BOOLEAN
203{
204 CPUID_EAX_0D_ECX_00 XCr0CpuidInfo;
205 UINT64 UnsupportedXCr0Bits;
206
207 //
208 // SDM Vol.3A 2.6 EXTENDED CONTROL REGISTERS (INCLUDING XCR0) - describes the invalid
209 // states of the XCR0 register.
210 //
211
212 CommonCpuidInstruction(CPUID_EXTENDED_STATE_INFORMATION, 0, (INT32 *)&XCr0CpuidInfo);
213 UnsupportedXCr0Bits = ~(XCr0CpuidInfo.Eax.AsUInt | (UINT64)XCr0CpuidInfo.Edx.AsUInt << 32);
214
215 if ((XCr0.AsUInt & UnsupportedXCr0Bits) != 0)
216 {
217 return FALSE;
218 }
219
220 if (XCr0.X87 != 1)
221 {
222 return FALSE;
223 }
224
225 if (XCr0.Sse == 0 && XCr0.Avx == 1)
226 {
227 return FALSE;
228 }
229
230 if (XCr0.Avx == 0)
231 {
232 if ((XCr0.Opmask | XCr0.ZmmHi256 | XCr0.ZmmHi16) == 1)
233 {
234 return FALSE;
235 }
236 }
237
238 if (XCr0.Bndcsr != XCr0.Bndreg)
239 {
240 return FALSE;
241 }
242
243 if ((XCr0.Opmask | XCr0.ZmmHi256 | XCr0.ZmmHi16) == 1)
244 {
245 if ((XCr0.Opmask & XCr0.ZmmHi256 & XCr0.ZmmHi16) != 1)
246 {
247 return FALSE;
248 }
249 }
250
251 return TRUE;
252}
signed int INT32
Definition BasicTypes.h:50
VOID CommonCpuidInstruction(UINT32 Func, UINT32 SubFunc, INT *CpuInfo)
Get cpuid results.
Definition Common.c:85

◆ CommonWriteDebugInformation()

VOID CommonWriteDebugInformation ( VIRTUAL_MACHINE_STATE * VCpu)

Produce debug information from unrecoverable bugs.

Parameters
VCpuThe virtual processor's state
Returns
VOID
150{
151 LogError("HyperDbg cannot recover from this error, please provide the following information through the Git issues");
152
153 LogInfo("Target RIP: %llx\n", VCpu->LastVmexitRip);
154
155 CHAR Instruction[MAXIMUM_INSTR_SIZE] = {0};
156
158
159 for (SIZE_T i = 0; i < MAXIMUM_INSTR_SIZE; i++)
160 {
161 Log("%02X ", Instruction[i] & 0xffU);
162 }
163
164 Log("\n");
167 Log("\n");
168
169 Log(
170 "RAX=%016llx RBX=%016llx RCX=%016llx\n"
171 "RDX=%016llx RSI=% 016llx RDI=%016llx\n"
172 "RIP=%016llx RSP=%016llx RBP=%016llx\n"
173 "R8 =%016llx R9 =%016llx R10=%016llx\n"
174 "R11=%016llx R12=%016llx R13=%016llx\n"
175 "R14=%016llx R15=%016llx\n",
176 VCpu->Regs->rax,
177 VCpu->Regs->rbx,
178 VCpu->Regs->rcx,
179 VCpu->Regs->rdx,
180 VCpu->Regs->rsi,
181 VCpu->Regs->rdi,
182 VCpu->LastVmexitRip,
183 VCpu->Regs->rsp,
184 VCpu->Regs->rbp,
185 VCpu->Regs->r8,
186 VCpu->Regs->r9,
187 VCpu->Regs->r10,
188 VCpu->Regs->r11,
189 VCpu->Regs->r12,
190 VCpu->Regs->r13,
191 VCpu->Regs->r14,
192 VCpu->Regs->r15);
193}
#define MAXIMUM_INSTR_SIZE
maximum instruction size in Intel
Definition Constants.h:470
#define Log(format,...)
Log without any prefix.
Definition HyperDbgHyperLogIntrinsics.h:129
#define LogInfo(format,...)
Define log variables.
Definition HyperDbgHyperLogIntrinsics.h:71
IMPORT_EXPORT_VMM UINT32 DisassemblerShowOneInstructionInVmxRootMode(PVOID Address, BOOLEAN Is32Bit)
Shows the disassembly of only one instruction.
Definition Disassembler.c:372
IMPORT_EXPORT_VMM BOOLEAN MemoryMapperReadMemorySafeOnTargetProcess(_In_ UINT64 VaAddressToRead, _Inout_ PVOID BufferToSaveMemory, _In_ SIZE_T SizeToRead)
BOOLEAN CommonIsGuestOnUsermode32Bit()
determines if the guest was in 32-bit user-mode or 64-bit (long mode)
Definition Common.c:97
GUEST_REGS * Regs
Definition State.h:326
UINT64 LastVmexitRip
Definition State.h:331
UINT64 rsp
Definition BasicTypes.h:145
UINT64 r14
Definition BasicTypes.h:155
UINT64 r15
Definition BasicTypes.h:156
UINT64 rdi
Definition BasicTypes.h:148
UINT64 rax
Definition BasicTypes.h:141
UINT64 r12
Definition BasicTypes.h:153
UINT64 r13
Definition BasicTypes.h:154
UINT64 r9
Definition BasicTypes.h:150
UINT64 r8
Definition BasicTypes.h:149
UINT64 rbp
Definition BasicTypes.h:146
UINT64 rbx
Definition BasicTypes.h:144
UINT64 r10
Definition BasicTypes.h:151
UINT64 rcx
Definition BasicTypes.h:142
UINT64 rsi
Definition BasicTypes.h:147
UINT64 r11
Definition BasicTypes.h:152
UINT64 rdx
Definition BasicTypes.h:143

◆ PsGetProcessImageFileName()

UCHAR * PsGetProcessImageFileName ( IN PEPROCESS Process)