HyperDbg Debugger
Toggle main menu visibility
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1
13
#pragma once
14
16
// Definitions //
18
19
#define AssertReturn return;
20
21
#define AssertReturnFalse return FALSE;
22
23
#define AssertReturnOne return 1;
24
25
#define PAUSE_MESSAGE_IN_USER_DEBUGGER "\nTo pause the target process, run the 'pause' command"
26
27
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED "handle of the driver not found, probably the driver is not loaded. Did you use 'load' command?\n"
28
29
#define ASSERT_MESSAGE_KD_NOT_LOADED "the kd (kernel debugger) module is not loaded. Did you use 'load kd' command?\n"
30
31
#define ASSERT_MESSAGE_VMM_NOT_LOADED "the vmm (virtualization) module is not loaded. Did you use 'load vmm' command?\n"
32
33
#define ASSERT_MESSAGE_HYPERTRACE_NOT_LOADED "the trace (hypertrace) module is not loaded. Did you use 'load trace' command?\n"
34
35
#define ASSERT_MESSAGE_BUILD_SIGNATURE_DOESNT_MATCH "the handshaking process was successful; however, there is a mismatch between " \
36
"the version/build of the debuggee and the debugger. please use the same " \
37
"version/build for both the debuggee and debugger\n"
38
39
#define ASSERT_MESSAGE_CANNOT_SPECIFY_PID "err, since HyperDbg won't context-switch to keep the system in a halted state, " \
40
"you cannot specify 'pid' for this command in the debugger mode. You can switch to the target process " \
41
"memory layout using the '.process' or the '.thread' command. After that, you can use " \
42
"this command without specifying the process ID. Alternatively, you can modify the current " \
43
"CR3 register to achieve the same functionality\n"
44
45
#define AssertReturnStmt(expr, stmt, rc) \
46
do \
47
{ \
48
if (expr) \
49
{ \
50
/* likely */
\
51
} \
52
else \
53
{ \
54
stmt; \
55
rc; \
56
} \
57
} while (0)
58
59
#define AssertShowMessageReturnStmt(expr1, expr2, message1, message2, rc) \
60
do \
61
{ \
62
if (expr1 && expr2) \
63
{ \
64
/* likely */
\
65
} \
66
else \
67
{ \
68
if (!expr1) \
69
ShowMessages(message1); \
70
else if (!expr2) \
71
ShowMessages(message2); \
72
rc; \
73
} \
74
} while (0)
75
80
#define PAGE_SIZE 0x1000
81
86
#define PAGE_ALIGN(Va) ((PVOID)((ULONG_PTR)(Va) & ~(PAGE_SIZE - 1)))
87
92
#define CPUID_ADDR_WIDTH 0x80000008
93
95
// Kernel & User Synchronization //
97
98
#define DbgWaitForKernelResponse(KernelSyncObjectId) \
99
do \
100
{ \
101
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
102
&g_KernelSyncronizationObjectsHandleTable[KernelSyncObjectId]; \
103
\
104
SyncronizationObject->IsOnWaitingState = TRUE; \
105
PlatformWaitForSingleObject(SyncronizationObject->EventHandle, INFINITE); \
106
\
107
} while (FALSE);
108
109
#define DbgWaitForUserResponse(UserSyncObjectId) \
110
do \
111
{ \
112
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
113
&g_UserSyncronizationObjectsHandleTable[UserSyncObjectId]; \
114
\
115
SyncronizationObject->IsOnWaitingState = TRUE; \
116
PlatformWaitForSingleObject(SyncronizationObject->EventHandle, INFINITE); \
117
\
118
} while (FALSE);
119
120
#define DbgWaitSetKernelRequestData(KernelSyncObjectId, ReqData, ReqSize) \
121
do \
122
{ \
123
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
124
&g_KernelSyncronizationObjectsHandleTable[KernelSyncObjectId]; \
125
\
126
SyncronizationObject->RequestData = (PVOID)ReqData; \
127
SyncronizationObject->RequestSize = (UINT32)ReqSize; \
128
\
129
} while (FALSE);
130
131
#define DbgWaitSetUserRequestData(UserSyncObjectId, ReqData, ReqSize) \
132
do \
133
{ \
134
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
135
&g_UserSyncronizationObjectsHandleTable[UserSyncObjectId]; \
136
\
137
SyncronizationObject->RequestData = (PVOID)ReqData; \
138
SyncronizationObject->RequestSize = (UINT32)ReqSize; \
139
\
140
} while (FALSE);
141
142
#define DbgWaitGetKernelRequestData(KernelSyncObjectId, ReqData, ReqSize) \
143
do \
144
{ \
145
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
146
&g_KernelSyncronizationObjectsHandleTable[KernelSyncObjectId]; \
147
\
148
*ReqData = SyncronizationObject->RequestData; \
149
*ReqSize = SyncronizationObject->RequestSize; \
150
SyncronizationObject->RequestData = NULL; \
151
SyncronizationObject->RequestSize = NULL_ZERO; \
152
\
153
} while (FALSE);
154
155
#define DbgWaitGetUserRequestData(UserSyncObjectId, ReqData, ReqSize) \
156
do \
157
{ \
158
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
159
&g_UserSyncronizationObjectsHandleTable[UserSyncObjectId]; \
160
\
161
*ReqData = SyncronizationObject->RequestData; \
162
*ReqSize = SyncronizationObject->RequestSize; \
163
SyncronizationObject->RequestData = NULL; \
164
SyncronizationObject->RequestSize = NULL_ZERO; \
165
\
166
} while (FALSE);
167
168
#define DbgReceivedKernelResponse(KernelSyncObjectId) \
169
do \
170
{ \
171
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
172
&g_KernelSyncronizationObjectsHandleTable[KernelSyncObjectId]; \
173
\
174
SyncronizationObject->IsOnWaitingState = FALSE; \
175
SetEvent(SyncronizationObject->EventHandle); \
176
} while (FALSE);
177
178
#define DbgReceivedUserResponse(UserSyncObjectId) \
179
do \
180
{ \
181
DEBUGGER_SYNCRONIZATION_EVENTS_STATE * SyncronizationObject = \
182
&g_UserSyncronizationObjectsHandleTable[UserSyncObjectId]; \
183
\
184
SyncronizationObject->IsOnWaitingState = FALSE; \
185
SetEvent(SyncronizationObject->EventHandle); \
186
} while (FALSE);
187
189
// Assembly Functions //
191
192
#ifdef __cplusplus
193
extern
"C"
{
194
#endif
195
196
extern
BOOLEAN
197
AsmVmxSupportDetection
();
198
199
#ifdef __cplusplus
200
}
201
#endif
202
204
// Spinlocks //
206
207
VOID
208
SpinlockLock
(
volatile
LONG
* Lock);
209
210
VOID
211
SpinlockLockWithCustomWait
(
volatile
LONG
* Lock,
UINT32
MaximumWait);
212
213
VOID
214
SpinlockUnlock
(
volatile
LONG
* Lock);
215
217
// Functions //
219
220
VOID
221
PrintBits
(
const
UINT32
size,
const
VOID * ptr);
222
223
BOOL
224
Replace
(std::string & str,
const
std::string & from,
const
std::string & to);
225
226
VOID
227
ReplaceAll
(
string
& str,
const
string
& from,
const
string
& to);
228
229
const
vector<string>
230
Split
(
const
string
& s,
const
CHAR
& c);
231
232
BOOLEAN
233
IsNumber
(
const
string
& str);
234
235
UINT32
236
Log2Ceil
(
UINT32
n);
237
238
BOOLEAN
239
IsHexNotation
(
const
string
& s);
240
241
vector<CHAR>
242
HexToBytes
(
const
string
& hex);
243
244
BOOLEAN
245
ConvertStringToUInt64
(
string
TextToConvert, PUINT64 Result);
246
247
BOOLEAN
248
ConvertStringToUInt32
(
string
TextToConvert,
PUINT32
Result);
249
250
BOOLEAN
251
ConvertTokenToUInt64
(
CommandToken
TargetToken, PUINT64 Result);
252
253
BOOLEAN
254
ConvertTokenToUInt32
(
CommandToken
TargetToken,
PUINT32
Result);
255
256
std::string
257
GetCaseSensitiveStringFromCommandToken
(
CommandToken
TargetToken);
258
259
std::string
260
GetLowerStringFromCommandToken
(
CommandToken
TargetToken);
261
262
BOOLEAN
263
CompareLowerCaseStrings
(
CommandToken
TargetToken,
const
CHAR
* StringToCompare);
264
265
BOOLEAN
266
IsTokenBracketString
(
CommandToken
TargetToken);
267
268
BOOLEAN
269
HasEnding
(
string
const
& fullString,
string
const
& ending);
270
271
BOOLEAN
272
ValidateIP
(
const
string
& ip);
273
274
BOOL
275
SetPrivilege
(HANDLE Token,
// access token handle
276
LPCTSTR Privilege,
// name of privilege to enable/disable
277
BOOL
EnablePrivilege
// to enable or disable privilege
278
);
279
280
VOID
281
Trim
(std::string & s);
282
283
std::string
284
RemoveSpaces
(std::string str);
285
286
BOOLEAN
287
IsFileExistA
(
const
CHAR
* FileName);
288
289
BOOLEAN
290
IsFileExistW
(
const
WCHAR * FileName);
291
292
VOID
293
GetConfigFilePath
(PWCHAR ConfigPath);
294
295
VOID
296
StringToWString
(std::wstring & ws,
const
std::string & s);
297
298
SIZE_T
299
FindCaseInsensitive
(std::string Input, std::string ToSearch, SIZE_T Pos);
300
301
SIZE_T
302
FindCaseInsensitiveW
(std::wstring Input, std::wstring ToSearch, SIZE_T Pos);
303
304
CHAR
*
305
ConvertStringVectorToCharPointerArray
(
const
std::string & s);
306
307
std::vector<std::string>
308
ListDirectory
(
const
std::string & Directory,
const
std::string & Extension);
309
310
BOOLEAN
311
IsEmptyString
(
CHAR
* Text);
312
313
VOID
314
CommonCpuidInstruction
(
UINT32
Func,
UINT32
SubFunc,
INT
* CpuInfo);
315
316
BOOLEAN
317
CheckCpuSupportRtm
();
318
319
UINT32
320
Getx86VirtualAddressWidth
();
321
322
BOOLEAN
323
CheckAccessValidityAndSafety
(UINT64 TargetAddress,
UINT32
Size);
324
325
BOOLEAN
326
VmxSupportDetection
();
BOOL
int BOOL
Definition
BasicTypes.h:25
BOOLEAN
UCHAR BOOLEAN
Definition
BasicTypes.h:35
LONG
long LONG
Definition
BasicTypes.h:28
INT
int INT
Definition
BasicTypes.h:43
PUINT32
unsigned int * PUINT32
Definition
BasicTypes.h:54
UINT32
unsigned int UINT32
Definition
BasicTypes.h:54
CHAR
char CHAR
Definition
BasicTypes.h:33
CommandToken
std::tuple< CommandParsingTokenType, std::string, std::string > CommandToken
Command's parsing type.
Definition
commands.h:177
GetCaseSensitiveStringFromCommandToken
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition
common.cpp:467
StringToWString
VOID StringToWString(std::wstring &ws, const std::string &s)
convert std::string to std::wstring
Definition
common.cpp:850
GetConfigFilePath
VOID GetConfigFilePath(PWCHAR ConfigPath)
Get config path.
Definition
common.cpp:792
CheckCpuSupportRtm
BOOLEAN CheckCpuSupportRtm()
Check whether the processor supports RTM or not.
Definition
common.cpp:950
IsNumber
BOOLEAN IsNumber(const string &str)
VmxSupportDetection
BOOLEAN VmxSupportDetection()
Detect whether the VMX is supported or not.
Definition
common.cpp:626
GetLowerStringFromCommandToken
std::string GetLowerStringFromCommandToken(CommandToken TargetToken)
Get lower case string from command token.
Definition
common.cpp:484
ValidateIP
BOOLEAN ValidateIP(const string &ip)
Function to validate an IP address.
Definition
common.cpp:590
SpinlockLock
VOID SpinlockLock(volatile LONG *Lock)
Tries to get the lock and won't return until successfully get the lock.
Definition
Spinlock.c:53
IsFileExistW
BOOLEAN IsFileExistW(const WCHAR *FileName)
check if a file exist or not (wide-char)
Definition
common.cpp:753
Replace
BOOL Replace(std::string &str, const std::string &from, const std::string &to)
IsTokenBracketString
BOOLEAN IsTokenBracketString(CommandToken TargetToken)
Is token bracket string.
Definition
common.cpp:524
FindCaseInsensitive
SIZE_T FindCaseInsensitive(std::string Input, std::string ToSearch, SIZE_T Pos)
Find case insensitive sub string in a given substring.
Definition
common.cpp:866
ConvertStringToUInt64
BOOLEAN ConvertStringToUInt64(string TextToConvert, PUINT64 Result)
IsHexNotation
BOOLEAN IsHexNotation(const string &s)
ConvertStringToUInt32
BOOLEAN ConvertStringToUInt32(string TextToConvert, PUINT32 Result)
ReplaceAll
VOID ReplaceAll(string &str, const string &from, const string &to)
AsmVmxSupportDetection
BOOLEAN AsmVmxSupportDetection()
CompareLowerCaseStrings
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition
common.cpp:503
Trim
VOID Trim(std::string &s)
trim from both ends and start of a string (in place)
Definition
common.cpp:715
ListDirectory
std::vector< std::string > ListDirectory(const std::string &Directory, const std::string &Extension)
Create a list of special files in a directory.
Definition
common.cpp:820
PrintBits
VOID PrintBits(const UINT32 size, const VOID *ptr)
SetPrivilege
BOOL SetPrivilege(HANDLE Token, LPCTSTR Privilege, BOOL EnablePrivilege)
SetPrivilege enables/disables process token privilege.
Definition
common.cpp:643
HexToBytes
vector< CHAR > HexToBytes(const string &hex)
SpinlockUnlock
VOID SpinlockUnlock(volatile LONG *Lock)
Release the lock.
Definition
Spinlock.c:162
Split
const vector< string > Split(const string &s, const CHAR &c)
Getx86VirtualAddressWidth
UINT32 Getx86VirtualAddressWidth()
Get virtual address width for x86 processors.
Definition
common.cpp:932
IsEmptyString
BOOLEAN IsEmptyString(CHAR *Text)
Is empty character.
Definition
common.cpp:765
SpinlockLockWithCustomWait
VOID SpinlockLockWithCustomWait(volatile LONG *Lock, UINT32 MaximumWait)
Tries to get the lock and won't return until successfully get the lock.
Definition
Spinlock.c:128
ConvertTokenToUInt64
BOOLEAN ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result)
add ` between 64 bit values and convert them to string
Definition
common.cpp:447
ConvertTokenToUInt32
BOOLEAN ConvertTokenToUInt32(CommandToken TargetToken, PUINT32 Result)
check and convert command token to a 32 bit unsigned integer
Definition
common.cpp:546
ConvertStringVectorToCharPointerArray
CHAR * ConvertStringVectorToCharPointerArray(const std::string &s)
Convert vector<string> to char*.
Definition
common.cpp:905
Log2Ceil
UINT32 Log2Ceil(UINT32 n)
Function to compute log2Ceil.
Definition
common.cpp:1263
IsFileExistA
BOOLEAN IsFileExistA(const CHAR *FileName)
check if a file exist or not (ASCII)
Definition
common.cpp:740
CheckAccessValidityAndSafety
BOOLEAN CheckAccessValidityAndSafety(UINT64 TargetAddress, UINT32 Size)
Check the safety to access the memory.
Definition
AddressCheck.c:318
HasEnding
BOOLEAN HasEnding(string const &fullString, string const &ending)
checks whether the string ends with a special string or not
Definition
common.cpp:569
FindCaseInsensitiveW
SIZE_T FindCaseInsensitiveW(std::wstring Input, std::wstring ToSearch, SIZE_T Pos)
Find case insensitive sub string in a given substring (unicode).
Definition
common.cpp:885
RemoveSpaces
std::string RemoveSpaces(std::string str)
Remove all the spaces in a string.
Definition
common.cpp:727
CommonCpuidInstruction
VOID CommonCpuidInstruction(UINT32 Func, UINT32 SubFunc, INT *CpuInfo)
Get cpuid results.
Definition
Common.c:85
hyperdbg
libhyperdbg
header
common.h
Generated by
1.17.0