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.
 

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:55

◆ CommonCpuidInstruction()

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

Get cpuid results.

Parameters
UINT32Func
UINT32SubFunc
int* CpuInfo
Returns
VOID
86{
87 __cpuidex(CpuInfo, Func, SubFunc);
88}

◆ 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:31
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}
#define NULL64_ZERO
Definition BasicTypes.h:52
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
UINT16 HvGetCsSelector()
Read CS selector.
Definition Hv.c:1132
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113
#define KGDT64_R3_CODE
Definition Common.h:127
#define KGDT64_R0_CODE
Definition Common.h:123
#define KGDT64_R3_CMCODE
Definition Common.h:125

◆ CommonIsStringStartsWith()

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

Detects whether the string starts with another string.

Parameters
constchar * pre
constchar * 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}

◆ 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:468
UINT32 DisassemblerShowOneInstructionInVmxRootMode(PVOID Address, BOOLEAN Is32Bit)
Shows the disassembly of only one instruction.
Definition Disassembler.c:328
#define Log(format,...)
Log without any prefix.
Definition HyperDbgHyperLogIntrinsics.h:129
#define LogInfo(format,...)
Define log variables.
Definition HyperDbgHyperLogIntrinsics.h:71
_Use_decl_annotations_ BOOLEAN MemoryMapperReadMemorySafeOnTargetProcess(UINT64 VaAddressToRead, PVOID BufferToSaveMemory, SIZE_T SizeToRead)
Read memory safely by mapping the buffer on the target process memory (It's a wrapper)
Definition MemoryMapper.c:1120
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:305
UINT64 LastVmexitRip
Definition State.h:309
UINT64 rsp
Definition BasicTypes.h:79
UINT64 r14
Definition BasicTypes.h:89
UINT64 r15
Definition BasicTypes.h:90
UINT64 rdi
Definition BasicTypes.h:82
UINT64 rax
Definition BasicTypes.h:75
UINT64 r12
Definition BasicTypes.h:87
UINT64 r13
Definition BasicTypes.h:88
UINT64 r9
Definition BasicTypes.h:84
UINT64 r8
Definition BasicTypes.h:83
UINT64 rbp
Definition BasicTypes.h:80
UINT64 rbx
Definition BasicTypes.h:78
UINT64 r10
Definition BasicTypes.h:85
UINT64 rcx
Definition BasicTypes.h:76
UINT64 rsi
Definition BasicTypes.h:81
UINT64 r11
Definition BasicTypes.h:86
UINT64 rdx
Definition BasicTypes.h:77