-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook-direct.cpp
More file actions
398 lines (343 loc) · 11.1 KB
/
Copy pathhook-direct.cpp
File metadata and controls
398 lines (343 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* @file hook-direct.cpp
* @brief Direct ioctl-based hook registration for HyperDbg
*
* Bypasses the command parser (InterpretGeneralEventAndActionsFields)
* to directly construct event/action structures and send ioctls.
* This removes limitations like "!epthook stage post" rejection
* and script variable scoping issues.
*/
#include <Windows.h>
#include <cstdio>
#include <cstring>
#include <string>
#include "SDK/HyperDbgSdk.h"
#include "SDK/imports/user/HyperDbgLibImports.h"
#include "SDK/imports/user/HyperDbgScriptImports.h"
#include "hook-direct.h"
// ====================== Internal globals ======================
static HANDLE g_HookDeviceHandle = NULL;
static UINT64 g_HookEventTag = 0x1000; // Start from a safe value
// ====================== Internal helpers ======================
static UINT64 GetNextEventTag()
{
return g_HookEventTag++;
}
/**
* @brief Compile a script string into a symbol buffer
* @param scriptBody The script text
* @param[out] outBufferAddress Address of compiled script buffer
* @param[out] outBufferLength Length of compiled script buffer
* @param[out] outBufferPointer Pointer count in script buffer
* @param[out] outCodeBuffer The code buffer handle (to free later)
* @return true on success
*/
static bool CompileScript(const char* scriptBody,
UINT64& outBufferAddress,
UINT32& outBufferLength,
UINT32& outBufferPointer,
UINT64& outCodeBuffer)
{
if (!scriptBody || scriptBody[0] == '\0')
return false;
PVOID codeBuffer = ScriptEngineParse((CHAR*)scriptBody);
if (!codeBuffer)
{
printf("[hook-direct] ScriptEngineParse returned NULL\n");
return false;
}
PSYMBOL_BUFFER symBuf = (PSYMBOL_BUFFER)codeBuffer;
// Check for syntax errors
if (symBuf->Message != NULL)
{
printf("[hook-direct] Script syntax error: %s\n", symBuf->Message);
RemoveSymbolBuffer(symBuf);
return false;
}
// Extract buffer info
outBufferAddress = (UINT64)symBuf->Head;
outBufferLength = (UINT32)(symBuf->Pointer * sizeof(SYMBOL));
outBufferPointer = (UINT32)symBuf->Pointer;
outCodeBuffer = (UINT64)codeBuffer;
return true;
}
/**
* @brief Send an event to the kernel via ioctl
*/
static bool SendEventIoctl(PDEBUGGER_GENERAL_EVENT_DETAIL event, UINT32 eventLength)
{
if (!g_HookDeviceHandle || g_HookDeviceHandle == INVALID_HANDLE_VALUE)
{
printf("[hook-direct] Device handle not open\n");
return false;
}
DEBUGGER_EVENT_AND_ACTION_RESULT result = {0};
DWORD returnedLength = 0;
BOOL status = DeviceIoControl(
g_HookDeviceHandle,
IOCTL_DEBUGGER_REGISTER_EVENT,
event,
eventLength,
&result,
sizeof(result),
&returnedLength,
NULL
);
if (!status)
{
printf("[hook-direct] RegisterEvent ioctl failed, err=0x%x\n", GetLastError());
return false;
}
if (!result.IsSuccessful || result.Error != 0)
{
printf("[hook-direct] RegisterEvent failed, error=0x%x\n", result.Error);
return false;
}
return true;
}
/**
* @brief Send an action to the kernel via ioctl
*/
static bool SendActionIoctl(PDEBUGGER_GENERAL_ACTION action, UINT32 actionLength)
{
if (!g_HookDeviceHandle || g_HookDeviceHandle == INVALID_HANDLE_VALUE)
{
printf("[hook-direct] Device handle not open\n");
return false;
}
DEBUGGER_EVENT_AND_ACTION_RESULT result = {0};
DWORD returnedLength = 0;
BOOL status = DeviceIoControl(
g_HookDeviceHandle,
IOCTL_DEBUGGER_ADD_ACTION_TO_EVENT,
action,
actionLength,
&result,
sizeof(result),
&returnedLength,
NULL
);
if (!status)
{
printf("[hook-direct] AddAction ioctl failed, err=0x%x\n", GetLastError());
return false;
}
if (!result.IsSuccessful || result.Error != 0)
{
printf("[hook-direct] AddAction failed, error=0x%x\n", result.Error);
return false;
}
return true;
}
/**
* @brief Core function: register an event with a script action
* @param eventType VMM_EVENT_TYPE_ENUM value
* @param stage Calling stage
* @param optionalParam1 Event-specific optional param 1
* @param optionalParam2 Event-specific optional param 2
* @param scriptBody The script to execute
* @param commandStr Command string for tracing (can be empty)
* @return true on success
*/
static bool RegisterEventWithScript(
VMM_EVENT_TYPE_ENUM eventType,
VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE stage,
UINT64 optionalParam1,
UINT64 optionalParam2,
const char* scriptBody,
const char* commandStr)
{
// 1. Compile the script
UINT64 scriptBufAddr = 0;
UINT32 scriptBufLen = 0;
UINT32 scriptBufPtr = 0;
UINT64 codeBufHandle = 0;
if (!CompileScript(scriptBody, scriptBufAddr, scriptBufLen, scriptBufPtr, codeBufHandle))
{
return false;
}
// 2. Allocate and fill the Event structure
UINT32 eventLength = sizeof(DEBUGGER_GENERAL_EVENT_DETAIL);
PDEBUGGER_GENERAL_EVENT_DETAIL event = (PDEBUGGER_GENERAL_EVENT_DETAIL)malloc(eventLength);
if (!event)
{
RemoveSymbolBuffer((PSYMBOL_BUFFER)codeBufHandle);
return false;
}
memset(event, 0, eventLength);
event->Tag = GetNextEventTag();
event->CoreId = DEBUGGER_EVENT_APPLY_TO_ALL_CORES;
event->ProcessId = DEBUGGER_EVENT_APPLY_TO_ALL_PROCESSES;
event->IsEnabled = TRUE;
event->EventStage = stage;
event->EventType = eventType;
event->CountOfActions = 1; // We'll add one script action
event->Options.OptionalParam1 = optionalParam1;
event->Options.OptionalParam2 = optionalParam2;
// CommandStringBuffer is user-mode only (for tracing), can be NULL
event->CommandStringBuffer = NULL;
event->ConditionBufferSize = 0;
// 3. Send the event to kernel
bool ok = SendEventIoctl(event, eventLength);
if (!ok)
{
printf("[hook-direct] Failed to register event (type=%d, tag=0x%llx)\n",
eventType, event->Tag);
free(event);
RemoveSymbolBuffer((PSYMBOL_BUFFER)codeBufHandle);
return false;
}
UINT64 eventTag = event->Tag;
free(event);
// 4. Allocate and fill the Action structure (script action)
UINT32 actionLength = sizeof(DEBUGGER_GENERAL_ACTION) + scriptBufLen;
PDEBUGGER_GENERAL_ACTION action = (PDEBUGGER_GENERAL_ACTION)malloc(actionLength);
if (!action)
{
RemoveSymbolBuffer((PSYMBOL_BUFFER)codeBufHandle);
return false;
}
memset(action, 0, actionLength);
action->EventTag = eventTag;
action->ActionType = RUN_SCRIPT;
action->ImmediateMessagePassing = TRUE;
action->PreAllocatedBuffer = 0;
action->CustomCodeBufferSize = 0;
action->ScriptBufferSize = scriptBufLen;
action->ScriptBufferPointer = scriptBufPtr;
// Copy script buffer after the action header
if (scriptBufLen > 0 && scriptBufAddr != 0)
{
memcpy((PVOID)((UINT64)action + sizeof(DEBUGGER_GENERAL_ACTION)),
(PVOID)scriptBufAddr,
scriptBufLen);
}
// 5. Send the action to kernel
ok = SendActionIoctl(action, actionLength);
if (!ok)
{
printf("[hook-direct] Failed to add script action (tag=0x%llx)\n", eventTag);
}
// 6. Cleanup
free(action);
RemoveSymbolBuffer((PSYMBOL_BUFFER)codeBufHandle);
return ok;
}
// ====================== Public API ======================
bool HookDeviceOpen()
{
if (g_HookDeviceHandle && g_HookDeviceHandle != INVALID_HANDLE_VALUE)
return true; // Already open
g_HookDeviceHandle = CreateFileA(
"\\\\.\\HyperDbgDebuggerDevice",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (g_HookDeviceHandle == INVALID_HANDLE_VALUE)
{
printf("[hook-direct] Failed to open HyperDbg device, err=0x%x\n", GetLastError());
g_HookDeviceHandle = NULL;
return false;
}
printf("[hook-direct] Device opened successfully\n");
return true;
}
void HookDeviceClose()
{
if (g_HookDeviceHandle && g_HookDeviceHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(g_HookDeviceHandle);
g_HookDeviceHandle = NULL;
}
}
UINT64 HookResolveSymbol(const char* symbolName)
{
BOOLEAN wasFound = FALSE;
UINT64 addr = ScriptEngineConvertNameToAddress(symbolName, &wasFound);
if (!wasFound || addr == 0)
{
printf("[hook-direct] Failed to resolve symbol: %s\n", symbolName);
return 0;
}
return addr;
}
bool HookCpuid(const char* scriptBody, HookCallingStage stage)
{
VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE vmmStage =
(VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE)stage;
return RegisterEventWithScript(
CPUID_INSTRUCTION_EXECUTION,
vmmStage,
0, // OptionalParam1: 0 = all CPUID leaves
0, // OptionalParam2: not used when OptionalParam1=0
scriptBody,
"!cpuid script { ... }"
);
}
bool HookSyscall(UINT64 syscallNumber, const char* scriptBody, HookCallingStage stage)
{
VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE vmmStage =
(VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE)stage;
// OptionalParam1 = syscall number (0xFFFFFFFF = all)
// OptionalParam2 = 0 = SAFE_ACCESS_MEMORY mode
return RegisterEventWithScript(
SYSCALL_HOOK_EFER_SYSCALL,
vmmStage,
syscallNumber,
0, // DEBUGGER_EVENT_SYSCALL_SYSRET_SAFE_ACCESS_MEMORY
scriptBody,
"!syscall script { ... }"
);
}
bool HookSysret(const char* scriptBody, HookCallingStage stage)
{
VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE vmmStage =
(VMM_CALLBACK_EVENT_CALLING_STAGE_TYPE)stage;
// OptionalParam2 = 0 = SAFE_ACCESS_MEMORY mode
return RegisterEventWithScript(
SYSCALL_HOOK_EFER_SYSRET,
vmmStage,
0, // No optional param for sysret
0, // DEBUGGER_EVENT_SYSCALL_SYSRET_SAFE_ACCESS_MEMORY
scriptBody,
"!sysret script { ... }"
);
}
bool HookEptHiddenBp(UINT64 targetAddress, const char* scriptBody)
{
if (targetAddress == 0)
{
printf("[hook-direct] HookEptHiddenBp: targetAddress is NULL\n");
return false;
}
// !epthook only supports stage pre
return RegisterEventWithScript(
HIDDEN_HOOK_EXEC_CC,
VMM_CALLBACK_CALLING_STAGE_PRE_EVENT_EMULATION,
targetAddress, // OptionalParam1 = hook address
0,
scriptBody,
"!epthook script { ... }"
);
}
bool HookEptDetours(UINT64 targetAddress, const char* scriptBody)
{
if (targetAddress == 0)
{
printf("[hook-direct] HookEptDetours: targetAddress is NULL\n");
return false;
}
// !epthook2 only supports stage pre
return RegisterEventWithScript(
HIDDEN_HOOK_EXEC_DETOURS,
VMM_CALLBACK_CALLING_STAGE_PRE_EVENT_EMULATION,
targetAddress, // OptionalParam1 = hook address
0,
scriptBody,
"!epthook2 script { ... }"
);
}