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

Common functions that needs to be used in all source code files. More...

#include "pch.h"

Functions

_Use_decl_annotations_ BOOLEAN CommonAffinityBroadcastToProcessors (ULONG ProcessorNumber, RunOnLogicalCoreFunc Routine)
 Broadcast a function to all logical cores.
PCHAR CommonGetProcessNameFromProcessControlBlock (PEPROCESS Eprocess)
 Get process name by eprocess.
BOOLEAN CommonIsStringStartsWith (const CHAR *pre, const CHAR *str)
 Detects whether the string starts with another string.
VOID CommonCpuidInstruction (UINT32 Func, UINT32 SubFunc, INT *CpuInfo)
 Get cpuid results.
BOOLEAN CommonIsGuestOnUsermode32Bit ()
 determines if the guest was in 32-bit user-mode or 64-bit (long mode)
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

Common functions that needs to be used in all source code files.

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

Function Documentation

◆ CommonAffinityBroadcastToProcessors()

_Use_decl_annotations_ BOOLEAN CommonAffinityBroadcastToProcessors ( ULONG ProcessorNumber,
RunOnLogicalCoreFunc Routine )

Broadcast a function to all logical cores.

This function is deprecated as we want to supporrt more than 32 processors

Parameters
ProcessorNumberThe logical core number to execute routine on it
RoutineThe function that should be executed on the target core
Returns
BOOLEAN Returns true if it was successful
25{
26 KIRQL OldIrql;
27
28 KeSetSystemAffinityThread((KAFFINITY)(1ULL << ProcessorNumber));
29
30 OldIrql = KeRaiseIrqlToDpcLevel();
31
32 Routine(ProcessorNumber);
33
34 KeLowerIrql(OldIrql);
35
36 KeRevertToUserAffinityThread();
37
38 return TRUE;
39}
#define TRUE
Definition BasicTypes.h:114

◆ 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 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}
void * PVOID
Definition BasicTypes.h:56
#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