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

Script engine pseudo-registers implementations. More...

#include "pch.h"

Functions

UINT64 ScriptEnginePseudoRegGetTid ()
 Implementation of $tid pseudo-register.
UINT64 ScriptEnginePseudoRegGetCore ()
 Implementation of $core pseudo-register.
UINT64 ScriptEnginePseudoRegGetPid ()
 Implementation of $pid pseudo-register.
CHARScriptEnginePseudoRegGetPname ()
 Implementation of $pname pseudo-register.
UINT64 ScriptEnginePseudoRegGetProc ()
 Implementation of $proc pseudo-register.
UINT64 ScriptEnginePseudoRegGetThread ()
 Implementation of $thread pseudo-register.
UINT64 ScriptEnginePseudoRegGetPeb ()
 Implementation of $peb pseudo-register.
UINT64 ScriptEnginePseudoRegGetTeb ()
 Implementation of $teb pseudo-register.
UINT64 ScriptEnginePseudoRegGetIp ()
 Implementation of $ip pseudo-register.
UINT64 ScriptEnginePseudoRegGetBuffer (UINT64 *CorrespondingAction)
 Implementation of $buffer pseudo-register.
UINT64 ScriptEnginePseudoRegGetEventTag (PACTION_BUFFER ActionBuffer)
 Implementation of $tag pseudo-register.
UINT64 ScriptEnginePseudoRegGetEventId (PACTION_BUFFER ActionBuffer)
 Implementation of $id pseudo-register.
UINT64 ScriptEnginePseudoRegGetEventStage (PACTION_BUFFER ActionBuffer)
 Implementation of stage pseudo-register.
UINT64 ScriptEnginePseudoRegGetTime ()
 Implementation of time pseudo-register.
UINT64 ScriptEnginePseudoRegGetDate ()
 Implementation of date pseudo-register.

Detailed Description

Script engine pseudo-registers implementations.

Author
M.H. Gholamrezaei (mh@hy.nosp@m.perd.nosp@m.bg.or.nosp@m.g)
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.2
Date
2022-06-29

Function Documentation

◆ ScriptEnginePseudoRegGetBuffer()

UINT64 ScriptEnginePseudoRegGetBuffer ( UINT64 * CorrespondingAction)

Implementation of $buffer pseudo-register.

Parameters
CorrespondingAction
Returns
UINT64
313{
314#ifdef SCRIPT_ENGINE_USER_MODE
315 //
316 // $buffer doesn't mean anything in user-mode
317 //
318 return (UINT64)NULL;
319#endif // SCRIPT_ENGINE_USER_MODE
320
321#ifdef SCRIPT_ENGINE_KERNEL_MODE
323#endif // SCRIPT_ENGINE_KERNEL_MODE
324}
UINT64 ScriptEngineWrapperGetAddressOfReservedBuffer(PDEBUGGER_EVENT_ACTION Action)
Get the address of reserved buffer.
Definition ScriptEngine.c:46
struct _DEBUGGER_EVENT_ACTION DEBUGGER_EVENT_ACTION
The structure of actions in HyperDbg.
NULL()
Definition test-case-generator.py:530

◆ ScriptEnginePseudoRegGetCore()

UINT64 ScriptEnginePseudoRegGetCore ( )

Implementation of $core pseudo-register.

Returns
UINT64
43{
44#ifdef SCRIPT_ENGINE_USER_MODE
45 return (UINT64)PlatformGetCurrentProcessorNumber();
46#endif // SCRIPT_ENGINE_USER_MODE
47
48#ifdef SCRIPT_ENGINE_KERNEL_MODE
50#endif // SCRIPT_ENGINE_KERNEL_MODE
51}
ULONG PlatformCpuGetCurrentProcessorNumber(VOID)
Get the current logical processor number.
Definition PlatformCpu.c:47
UINT32 PlatformGetCurrentProcessorNumber(VOID)
Platform independent wrapper for GetCurrentProcessorNumber / sched_getcpu.
Definition platform-lib-calls.c:168

◆ ScriptEnginePseudoRegGetDate()

UINT64 ScriptEnginePseudoRegGetDate ( )

Implementation of date pseudo-register.

Returns
UINT64
408{
409#ifdef SCRIPT_ENGINE_USER_MODE
410 return NULL;
411#endif // SCRIPT_ENGINE_USER_MODE
412
413#ifdef SCRIPT_ENGINE_KERNEL_MODE
414
415 //
416 // Get the current core's date
417 //
419#endif // SCRIPT_ENGINE_KERNEL_MODE
420}
UINT64 ScriptEngineGetTargetCoreDate()
Update core's date time and return date.
Definition ScriptEngine.c:109

◆ ScriptEnginePseudoRegGetEventId()

UINT64 ScriptEnginePseudoRegGetEventId ( PACTION_BUFFER ActionBuffer)

Implementation of $id pseudo-register.

Parameters
ActionBuffer
Returns
UINT64
352{
353#ifdef SCRIPT_ENGINE_USER_MODE
354 return NULL;
355#endif // SCRIPT_ENGINE_USER_MODE
356
357#ifdef SCRIPT_ENGINE_KERNEL_MODE
358 return (ActionBuffer->Tag - DebuggerEventTagStartSeed);
359#endif // SCRIPT_ENGINE_KERNEL_MODE
360}
#define DebuggerEventTagStartSeed
The seeds that user-mode codes use as the starter of their events' tag.
Definition Constants.h:230
long long unsigned Tag
Definition ScriptEngineCommonDefinitions.h:36

◆ ScriptEnginePseudoRegGetEventStage()

UINT64 ScriptEnginePseudoRegGetEventStage ( PACTION_BUFFER ActionBuffer)

Implementation of stage pseudo-register.

Parameters
ActionBuffer
Returns
UINT64
370{
371#ifdef SCRIPT_ENGINE_USER_MODE
372 return NULL;
373#endif // SCRIPT_ENGINE_USER_MODE
374
375#ifdef SCRIPT_ENGINE_KERNEL_MODE
376 return ActionBuffer->CallingStage;
377#endif // SCRIPT_ENGINE_KERNEL_MODE
378}
char CallingStage
Definition ScriptEngineCommonDefinitions.h:40

◆ ScriptEnginePseudoRegGetEventTag()

UINT64 ScriptEnginePseudoRegGetEventTag ( PACTION_BUFFER ActionBuffer)

Implementation of $tag pseudo-register.

Parameters
ActionBuffer
Returns
UINT64
334{
335#ifdef SCRIPT_ENGINE_USER_MODE
336 return (UINT64)NULL;
337#endif // SCRIPT_ENGINE_USER_MODE
338
339#ifdef SCRIPT_ENGINE_KERNEL_MODE
340 return ActionBuffer->Tag;
341#endif // SCRIPT_ENGINE_KERNEL_MODE
342}

◆ ScriptEnginePseudoRegGetIp()

UINT64 ScriptEnginePseudoRegGetIp ( )

Implementation of $ip pseudo-register.

Returns
UINT64
292{
293#ifdef SCRIPT_ENGINE_USER_MODE
294 //
295 // $ip doesn't have meaning in user-moderds
296 //
297 return NULL;
298#endif // SCRIPT_ENGINE_USER_MODE
299
300#ifdef SCRIPT_ENGINE_KERNEL_MODE
302#endif // SCRIPT_ENGINE_KERNEL_MODE
303}
UINT64 ScriptEngineWrapperGetInstructionPointer()
Get current ip from the debugger frame.
Definition ScriptEngine.c:21

◆ ScriptEnginePseudoRegGetPeb()

UINT64 ScriptEnginePseudoRegGetPeb ( )

Implementation of $peb pseudo-register.

Returns
UINT64
128{
129#ifdef SCRIPT_ENGINE_USER_MODE
130# ifdef _WIN32
131 //
132 // Hand-rolled structs ( may cause conflict depending on your dev env )
133 //
134 struct PROCESS_BASIC_INFORMATION
135 {
136 PVOID Reserved1;
137 PVOID PebBaseAddress;
138 PVOID Reserved2[2];
139 ULONG_PTR UniqueProcessId;
140 PVOID Reserved3;
141 };
142
143 struct PEB_LDR_DATA
144 {
145 BYTE Reserved1[8];
146 PVOID Reserved2[3];
147 LIST_ENTRY InMemoryOrderModuleList;
148 };
149
150 struct PEB
151 {
152 BYTE Reserved1[2];
154 BYTE Reserved2[1];
155 PVOID Reserved3[2];
156 struct PEB_LDR_DATA * Ldr;
157 PVOID ProcessParameters; /* PRTL_USER_PROCESS_PARAMETERS */
158 BYTE Reserved4[104];
159 PVOID Reserved5[52];
160 PVOID PostProcessInitRoutine; /* PPS_POST_PROCESS_INIT_ROUTINE */
161 BYTE Reserved6[128];
162 PVOID Reserved7[1];
164 };
165
166 struct UNICODE_STRING
167 {
170 PWSTR Buffer;
171 };
172
173 struct LDR_MODULE
174 {
175 LIST_ENTRY InLoadOrderModuleList;
176 LIST_ENTRY InMemoryOrderModuleList;
177 LIST_ENTRY InInitializationOrderModuleList;
178 PVOID BaseAddress;
179 PVOID EntryPoint;
180 ULONG SizeOfImage;
181 struct UNICODE_STRING FullDllName;
182 struct UNICODE_STRING BaseDllName;
183 ULONG Flags;
184 SHORT LoadCount;
185 SHORT TlsIndex;
186 LIST_ENTRY HashTableEntry;
187 ULONG TimeDateStamp;
188 };
189
190 enum PROCESSINFOCLASS
191 {
192 ProcessBasicInformation = 0,
193 ProcessDebugPort = 7,
194 ProcessWow64Information = 26,
195 ProcessImageFileName = 27
196 };
197
198 LPCWSTR NTDLL_NAME = L"ntdll.dll";
199 LPCSTR NTQUERYINFO_NAME = "NtQueryInformationProcess";
200
201 HMODULE NtdllMod;
202 HANDLE ThisProcess;
203 NTSTATUS NtCallRet;
204 ULONG BytesReturned;
205
206 //
207 // function pointer to house result from GetProcAddress
208 //
209 NTSTATUS(WINAPI * QueryInfoProcPtr)
210 (HANDLE, enum PROCESSINFOCLASS, PVOID, ULONG, PULONG);
211
212 struct PROCESS_BASIC_INFORMATION BasicInfo;
213 struct PEB * PebPtr;
214
215 /* retrieve pseudo-handle */
216 ThisProcess = GetCurrentProcess();
217
218 //
219 // get address to already loaded module
220 //
221 NtdllMod = LoadLibraryW(NTDLL_NAME);
222
223 //
224 // get pointer to query function
225 //
226 QueryInfoProcPtr =
227 (NTSTATUS(WINAPI *)(HANDLE, enum PROCESSINFOCLASS, PVOID, ULONG, PULONG))GetProcAddress(NtdllMod, NTQUERYINFO_NAME);
228
229 //
230 // call function on self; introspect
231 //
232 NtCallRet = QueryInfoProcPtr(ThisProcess, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), &BytesReturned);
233
234 //
235 // get peb ptr and decode some if its fields
236 //
237 PebPtr = (struct PEB *)BasicInfo.PebBaseAddress;
238
239 return (UINT64)PebPtr;
240
241# else // !_WIN32
242
243 //
244 // The PEB (Process Environment Block) is a Windows NT-specific structure
245 // maintained by the kernel for every user-mode process. It holds loader
246 // data, heap pointers, the image path, and other process-wide state that
247 // has no direct equivalent in the Linux process model.
248 //
249 // TODO: investigate whether a meaningful substitute exists on Linux
250 // (e.g. /proc/self/maps, dl_iterate_phdr, or a custom auxv walk)
251 // and implement it here if HyperDbg user-mode on Linux ever needs $peb.
252 //
253 return 0;
254
255# endif // _WIN32
256
257#endif // SCRIPT_ENGINE_USER_MODE
258
259#ifdef SCRIPT_ENGINE_KERNEL_MODE
260
261 //
262 // PEB doesn't make sense in kernel-mode
263 //
264 return NULL64_ZERO;
265#endif // SCRIPT_ENGINE_KERNEL_MODE
266}
struct _PEB PEB
PEB 64-bit.
struct _PEB_LDR_DATA PEB_LDR_DATA
PEB LDR Data.
void * PVOID
Definition BasicTypes.h:56
unsigned char BYTE
Definition BasicTypes.h:40
unsigned short USHORT
Definition BasicTypes.h:30
#define NULL64_ZERO
Definition BasicTypes.h:111
short SHORT
Definition BasicTypes.h:27
unsigned long ULONG
Definition BasicTypes.h:31
struct _UNICODE_STRING UNICODE_STRING
ULONG Reserved6
Definition UserAccess.h:65
PPEB_LDR_DATA Ldr
Definition UserAccess.h:60
BYTE Reserved2[1]
Definition UserAccess.h:58
BYTE BeingDebugged
Definition UserAccess.h:57
ULONG SessionId
Definition UserAccess.h:74
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition UserAccess.h:61
PVOID Reserved5
Definition UserAccess.h:64
PVOID Reserved3[2]
Definition UserAccess.h:59
PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine
Definition UserAccess.h:71
PVOID Reserved7
Definition UserAccess.h:66
PVOID Reserved4[3]
Definition UserAccess.h:62
BYTE Reserved1[2]
Definition UserAccess.h:56
USHORT Length
Definition casting.cpp:26
USHORT MaximumLength
Definition casting.cpp:27
PWSTR Buffer
Definition casting.cpp:28

◆ ScriptEnginePseudoRegGetPid()

UINT64 ScriptEnginePseudoRegGetPid ( )

Implementation of $pid pseudo-register.

Returns
UINT64
60{
61#ifdef SCRIPT_ENGINE_USER_MODE
62 return (UINT64)PlatformGetCurrentProcessId();
63#endif // SCRIPT_ENGINE_USER_MODE
64
65#ifdef SCRIPT_ENGINE_KERNEL_MODE
67#endif // SCRIPT_ENGINE_KERNEL_MODE
68}
HANDLE PlatformProcessGetCurrentProcessId(VOID)
Get the current process ID.
Definition PlatformProcess.c:47
UINT32 PlatformGetCurrentProcessId(VOID)
Platform independent wrapper for GetCurrentProcessId / getpid.
Definition platform-lib-calls.c:185

◆ ScriptEnginePseudoRegGetPname()

CHAR * ScriptEnginePseudoRegGetPname ( )

Implementation of $pname pseudo-register.

Returns
CHAR*
77{
78#ifdef SCRIPT_ENGINE_USER_MODE
80#endif // SCRIPT_ENGINE_USER_MODE
81
82#ifdef SCRIPT_ENGINE_KERNEL_MODE
84#endif // SCRIPT_ENGINE_KERNEL_MODE
85}
PVOID PlatformProcessGetCurrentProcess(VOID)
Get the current process (PEPROCESS).
Definition PlatformProcess.c:70
PCHAR CommonGetProcessNameFromProcessControlBlock(PEPROCESS Eprocess)
Get process name by eprocess.
Definition Common.c:48
CHAR * PlatformGetCurrentProcessName(VOID)
Platform independent wrapper to get the current process name.
Definition platform-lib-calls.c:202

◆ ScriptEnginePseudoRegGetProc()

UINT64 ScriptEnginePseudoRegGetProc ( )

Implementation of $proc pseudo-register.

Returns
UINT64
94{
95#ifdef SCRIPT_ENGINE_USER_MODE
96 return (UINT64)NULL;
97#endif // SCRIPT_ENGINE_USER_MODE
98
99#ifdef SCRIPT_ENGINE_KERNEL_MODE
100 return (UINT64)PlatformProcessGetCurrentProcess();
101#endif // SCRIPT_ENGINE_KERNEL_MODE
102}

◆ ScriptEnginePseudoRegGetTeb()

UINT64 ScriptEnginePseudoRegGetTeb ( )

Implementation of $teb pseudo-register.

Returns
UINT64
275{
276#ifdef SCRIPT_ENGINE_USER_MODE
277 return (UINT64)NULL;
278#endif // SCRIPT_ENGINE_USER_MODE
279
280#ifdef SCRIPT_ENGINE_KERNEL_MODE
281 return (UINT64)PlatformProcessGetCurrentThreadTeb();
282#endif // SCRIPT_ENGINE_KERNEL_MODE
283}
PVOID PlatformProcessGetCurrentThreadTeb(VOID)
Get the TEB (Thread Environment Block) of the current thread.
Definition PlatformProcess.c:116

◆ ScriptEnginePseudoRegGetThread()

UINT64 ScriptEnginePseudoRegGetThread ( )

Implementation of $thread pseudo-register.

Returns
UINT64
111{
112#ifdef SCRIPT_ENGINE_USER_MODE
113 return (UINT64)NULL;
114#endif // SCRIPT_ENGINE_USER_MODE
115
116#ifdef SCRIPT_ENGINE_KERNEL_MODE
117 return (UINT64)PlatformProcessGetCurrentThread();
118#endif // SCRIPT_ENGINE_KERNEL_MODE
119}
PVOID PlatformProcessGetCurrentThread(VOID)
Get the current thread (PETHREAD).
Definition PlatformProcess.c:93

◆ ScriptEnginePseudoRegGetTid()

UINT64 ScriptEnginePseudoRegGetTid ( )

Implementation of $tid pseudo-register.

Returns
UINT64
26{
27#ifdef SCRIPT_ENGINE_USER_MODE
28 return (UINT64)PlatformGetCurrentThreadId();
29#endif // SCRIPT_ENGINE_USER_MODE
30
31#ifdef SCRIPT_ENGINE_KERNEL_MODE
32 return (UINT64)PlatformProcessGetCurrentThreadId();
33#endif // SCRIPT_ENGINE_KERNEL_MODE
34}
HANDLE PlatformProcessGetCurrentThreadId(VOID)
Get the current thread ID.
Definition PlatformProcess.c:24
UINT32 PlatformGetCurrentThreadId(VOID)
Platform independent wrapper for GetCurrentThreadId / gettid.
Definition platform-lib-calls.c:151

◆ ScriptEnginePseudoRegGetTime()

UINT64 ScriptEnginePseudoRegGetTime ( )

Implementation of time pseudo-register.

Returns
UINT64
387{
388#ifdef SCRIPT_ENGINE_USER_MODE
389 return NULL;
390#endif // SCRIPT_ENGINE_USER_MODE
391
392#ifdef SCRIPT_ENGINE_KERNEL_MODE
393
394 //
395 // Get the current core's time
396 //
398#endif // SCRIPT_ENGINE_KERNEL_MODE
399}
UINT64 ScriptEngineGetTargetCoreTime()
Update core's date time and return time.
Definition ScriptEngine.c:87