HyperDbg Debugger
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
193extern "C" {
194#endif
195
196extern BOOLEAN
198
199#ifdef __cplusplus
200}
201#endif
202
204// Spinlocks //
206
207VOID
208SpinlockLock(volatile LONG * Lock);
209
210VOID
211SpinlockLockWithCustomWait(volatile LONG * Lock, UINT32 MaximumWait);
212
213VOID
214SpinlockUnlock(volatile LONG * Lock);
215
217// Functions //
219
220VOID
221PrintBits(const UINT32 size, const VOID * ptr);
222
223BOOL
224Replace(std::string & str, const std::string & from, const std::string & to);
225
226VOID
227ReplaceAll(string & str, const string & from, const string & to);
228
229const vector<string>
230Split(const string & s, const CHAR & c);
231
233IsNumber(const string & str);
234
235UINT32
237
239IsHexNotation(const string & s);
240
241vector<CHAR>
242HexToBytes(const string & hex);
243
245ConvertStringToUInt64(string TextToConvert, PUINT64 Result);
246
248ConvertStringToUInt32(string TextToConvert, PUINT32 Result);
249
251ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result);
252
254ConvertTokenToUInt32(CommandToken TargetToken, PUINT32 Result);
255
256std::string
258
259std::string
261
263CompareLowerCaseStrings(CommandToken TargetToken, const CHAR * StringToCompare);
264
267
269HasEnding(string const & fullString, string const & ending);
270
272ValidateIP(const string & ip);
273
274BOOL
275SetPrivilege(HANDLE Token, // access token handle
276 LPCTSTR Privilege, // name of privilege to enable/disable
277 BOOL EnablePrivilege // to enable or disable privilege
278);
279
280VOID
281Trim(std::string & s);
282
283std::string
284RemoveSpaces(std::string str);
285
287IsFileExistA(const CHAR * FileName);
288
290IsFileExistW(const WCHAR * FileName);
291
292VOID
293GetConfigFilePath(PWCHAR ConfigPath);
294
295VOID
296StringToWString(std::wstring & ws, const std::string & s);
297
298SIZE_T
299FindCaseInsensitive(std::string Input, std::string ToSearch, SIZE_T Pos);
300
301SIZE_T
302FindCaseInsensitiveW(std::wstring Input, std::wstring ToSearch, SIZE_T Pos);
303
304CHAR *
305ConvertStringVectorToCharPointerArray(const std::string & s);
306
307std::vector<std::string>
308ListDirectory(const std::string & Directory, const std::string & Extension);
309
311IsEmptyString(CHAR * Text);
312
313VOID
314CommonCpuidInstruction(UINT32 Func, UINT32 SubFunc, INT * CpuInfo);
315
318
319UINT32
321
323CheckAccessValidityAndSafety(UINT64 TargetAddress, UINT32 Size);
324
int BOOL
Definition BasicTypes.h:25
UCHAR BOOLEAN
Definition BasicTypes.h:35
long LONG
Definition BasicTypes.h:28
int INT
Definition BasicTypes.h:43
unsigned int * PUINT32
Definition BasicTypes.h:54
unsigned int UINT32
Definition BasicTypes.h:54
char CHAR
Definition BasicTypes.h:33
std::tuple< CommandParsingTokenType, std::string, std::string > CommandToken
Command's parsing type.
Definition commands.h:177
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
VOID StringToWString(std::wstring &ws, const std::string &s)
convert std::string to std::wstring
Definition common.cpp:850
VOID GetConfigFilePath(PWCHAR ConfigPath)
Get config path.
Definition common.cpp:792
BOOLEAN CheckCpuSupportRtm()
Check whether the processor supports RTM or not.
Definition common.cpp:950
BOOLEAN IsNumber(const string &str)
BOOLEAN VmxSupportDetection()
Detect whether the VMX is supported or not.
Definition common.cpp:626
std::string GetLowerStringFromCommandToken(CommandToken TargetToken)
Get lower case string from command token.
Definition common.cpp:484
BOOLEAN ValidateIP(const string &ip)
Function to validate an IP address.
Definition common.cpp:590
VOID SpinlockLock(volatile LONG *Lock)
Tries to get the lock and won't return until successfully get the lock.
Definition Spinlock.c:53
BOOLEAN IsFileExistW(const WCHAR *FileName)
check if a file exist or not (wide-char)
Definition common.cpp:753
BOOL Replace(std::string &str, const std::string &from, const std::string &to)
BOOLEAN IsTokenBracketString(CommandToken TargetToken)
Is token bracket string.
Definition common.cpp:524
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
BOOLEAN ConvertStringToUInt64(string TextToConvert, PUINT64 Result)
BOOLEAN IsHexNotation(const string &s)
BOOLEAN ConvertStringToUInt32(string TextToConvert, PUINT32 Result)
VOID ReplaceAll(string &str, const string &from, const string &to)
BOOLEAN AsmVmxSupportDetection()
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
VOID Trim(std::string &s)
trim from both ends and start of a string (in place)
Definition common.cpp:715
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
VOID PrintBits(const UINT32 size, const VOID *ptr)
BOOL SetPrivilege(HANDLE Token, LPCTSTR Privilege, BOOL EnablePrivilege)
SetPrivilege enables/disables process token privilege.
Definition common.cpp:643
vector< CHAR > HexToBytes(const string &hex)
VOID SpinlockUnlock(volatile LONG *Lock)
Release the lock.
Definition Spinlock.c:162
const vector< string > Split(const string &s, const CHAR &c)
UINT32 Getx86VirtualAddressWidth()
Get virtual address width for x86 processors.
Definition common.cpp:932
BOOLEAN IsEmptyString(CHAR *Text)
Is empty character.
Definition common.cpp:765
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
BOOLEAN ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result)
add ` between 64 bit values and convert them to string
Definition common.cpp:447
BOOLEAN ConvertTokenToUInt32(CommandToken TargetToken, PUINT32 Result)
check and convert command token to a 32 bit unsigned integer
Definition common.cpp:546
CHAR * ConvertStringVectorToCharPointerArray(const std::string &s)
Convert vector<string> to char*.
Definition common.cpp:905
UINT32 Log2Ceil(UINT32 n)
Function to compute log2Ceil.
Definition common.cpp:1263
BOOLEAN IsFileExistA(const CHAR *FileName)
check if a file exist or not (ASCII)
Definition common.cpp:740
BOOLEAN CheckAccessValidityAndSafety(UINT64 TargetAddress, UINT32 Size)
Check the safety to access the memory.
Definition AddressCheck.c:318
BOOLEAN HasEnding(string const &fullString, string const &ending)
checks whether the string ends with a special string or not
Definition common.cpp:569
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
std::string RemoveSpaces(std::string str)
Remove all the spaces in a string.
Definition common.cpp:727
VOID CommonCpuidInstruction(UINT32 Func, UINT32 SubFunc, INT *CpuInfo)
Get cpuid results.
Definition Common.c:85