-
Notifications
You must be signed in to change notification settings - Fork 444
/
easyhook.h
452 lines (345 loc) · 12 KB
/
easyhook.h
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
EasyHook - The reinvention of Windows API hooking
Copyright (C) 2009 Christoph Husse
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Please visit http://www.codeplex.com/easyhook for more information
about the project and latest updates.
*/
#ifndef _EASYHOOK_H_
#define _EASYHOOK_H_
#ifdef DRIVER
#include <ntddk.h>
#include <ntstrsafe.h>
typedef int BOOL;
typedef void* HMODULE;
#else
// #define NTDDI_VERSION NTDDI_WIN2KSP4
// #define _WIN32_WINNT 0x500
#define _WIN32_IE_ _WIN32_IE_WIN2KSP4
#include <windows.h>
#include <winnt.h>
#include <winternl.h>
#endif
#ifdef __cplusplus
extern "C"{
#endif
#ifdef EASYHOOK_EXPORTS
#define EASYHOOK_API __declspec(dllexport) __stdcall
#define DRIVER_SHARED_API(type, decl) EXTERN_C type EASYHOOK_API decl
#else
#ifndef DRIVER
#ifdef STATIC_LIB
#define EASYHOOK_API __stdcall
#else
#define EASYHOOK_API __declspec(dllimport) __stdcall
#endif
#define DRIVER_SHARED_API(type, decl) EXTERN_C type EASYHOOK_API decl
#else
#define EASYHOOK_API __stdcall
#define DRIVER_SHARED_API(type, decl) typedef type EASYHOOK_API PROC_##decl; EXTERN_C type EASYHOOK_API decl
#endif
#endif
/*
This is the typical sign that a defined method is exported...
Methods marked with this attribute need special attention
during parameter validation and documentation.
*/
#define EASYHOOK_NT_EXPORT EXTERN_C NTSTATUS EASYHOOK_API
#define EASYHOOK_BOOL_EXPORT EXTERN_C BOOL EASYHOOK_API
#define MAX_HOOK_COUNT 128
#define MAX_ACE_COUNT 128
#define MAX_THREAD_COUNT 128
#define MAX_PASSTHRU_SIZE 1024 * 64
typedef struct _HOOK_ACL_
{
ULONG Count;
BOOL IsExclusive;
ULONG Entries[MAX_ACE_COUNT];
}HOOK_ACL;
typedef struct _LOCAL_HOOK_INFO_* PLOCAL_HOOK_INFO;
typedef struct _HOOK_TRACE_INFO_
{
PLOCAL_HOOK_INFO Link;
}HOOK_TRACE_INFO, *TRACED_HOOK_HANDLE;
typedef struct _LOCAL_HOOK_INFO_
{
PLOCAL_HOOK_INFO Next;
ULONG NativeSize;
UCHAR* TargetProc;
ULONGLONG TargetBackup;
ULONGLONG TargetBackup_x64;
ULONGLONG HookCopy;
ULONG EntrySize;
UCHAR* Trampoline;
ULONG HLSIndex;
ULONG HLSIdent;
void* Callback;
HOOK_ACL LocalACL;
ULONG Signature;
TRACED_HOOK_HANDLE Tracking;
void* RandomValue; // fixed
void* HookIntro; // fixed
UCHAR* OldProc; // fixed
UCHAR* HookProc; // fixed
void* HookOutro; // fixed
int* IsExecutedPtr; // fixed
}LOCAL_HOOK_INFO, *PLOCAL_HOOK_INFO;
DRIVER_SHARED_API(NTSTATUS, RtlGetLastError());
DRIVER_SHARED_API(PWCHAR, RtlGetLastErrorString());
DRIVER_SHARED_API(NTSTATUS, LhInstallHook(
void* InEntryPoint,
void* InHookProc,
void* InCallback,
TRACED_HOOK_HANDLE OutHandle));
DRIVER_SHARED_API(NTSTATUS, LhUninstallAllHooks());
DRIVER_SHARED_API(NTSTATUS, LhUninstallHook(TRACED_HOOK_HANDLE InHandle));
DRIVER_SHARED_API(NTSTATUS, LhWaitForPendingRemovals());
#ifdef STATIC_LIB
BOOL APIENTRY EasyHookDllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
#endif
/*
Setup the ACLs after hook installation. Please note that every
hook starts suspended. You will have to set a proper ACL to
make it active!
*/
#ifdef DRIVER
DRIVER_SHARED_API(NTSTATUS, LhSetInclusiveACL(
ULONG* InProcessIdList,
ULONG InProcessCount,
TRACED_HOOK_HANDLE InHandle));
DRIVER_SHARED_API(NTSTATUS, LhSetExclusiveACL(
ULONG* InProcessIdList,
ULONG InProcessCount,
TRACED_HOOK_HANDLE InHandle));
DRIVER_SHARED_API(NTSTATUS, LhSetGlobalInclusiveACL(
ULONG* InProcessIdList,
ULONG InProcessCount));
DRIVER_SHARED_API(NTSTATUS, LhSetGlobalExclusiveACL(
ULONG* InProcessIdList,
ULONG InProcessCount));
DRIVER_SHARED_API(NTSTATUS, LhIsProcessIntercepted(
TRACED_HOOK_HANDLE InHook,
ULONG InProcessID,
BOOL* OutResult));
#else
EASYHOOK_NT_EXPORT LhSetInclusiveACL(
ULONG* InThreadIdList,
ULONG InThreadCount,
TRACED_HOOK_HANDLE InHandle);
EASYHOOK_NT_EXPORT LhSetExclusiveACL(
ULONG* InThreadIdList,
ULONG InThreadCount,
TRACED_HOOK_HANDLE InHandle);
EASYHOOK_NT_EXPORT LhSetGlobalInclusiveACL(
ULONG* InThreadIdList,
ULONG InThreadCount);
EASYHOOK_NT_EXPORT LhSetGlobalExclusiveACL(
ULONG* InThreadIdList,
ULONG InThreadCount);
EASYHOOK_NT_EXPORT LhIsThreadIntercepted(
TRACED_HOOK_HANDLE InHook,
ULONG InThreadID,
BOOL* OutResult);
#endif // !DRIVER
/*
The following barrier methods are meant to be used in hook handlers only!
They will all fail with STATUS_NOT_SUPPORTED if called outside a
valid hook handler...
*/
DRIVER_SHARED_API(NTSTATUS, LhBarrierGetCallback(PVOID* OutValue));
DRIVER_SHARED_API(NTSTATUS, LhBarrierGetReturnAddress(PVOID* OutValue));
DRIVER_SHARED_API(NTSTATUS, LhBarrierGetAddressOfReturnAddress(PVOID** OutValue));
DRIVER_SHARED_API(NTSTATUS, LhBarrierBeginStackTrace(PVOID* OutBackup));
DRIVER_SHARED_API(NTSTATUS, LhBarrierEndStackTrace(PVOID InBackup));
typedef struct _MODULE_INFORMATION_* PMODULE_INFORMATION;
typedef struct _MODULE_INFORMATION_
{
PMODULE_INFORMATION Next;
UCHAR* BaseAddress;
ULONG ImageSize;
CHAR Path[256];
PCHAR ModuleName;
}MODULE_INFORMATION;
EASYHOOK_NT_EXPORT LhUpdateModuleInformation();
DRIVER_SHARED_API(NTSTATUS, LhBarrierPointerToModule(
PVOID InPointer,
MODULE_INFORMATION* OutModule));
DRIVER_SHARED_API(NTSTATUS, LhEnumModules(
HMODULE* OutModuleArray,
ULONG InMaxModuleCount,
ULONG* OutModuleCount));
DRIVER_SHARED_API(NTSTATUS, LhBarrierGetCallingModule(MODULE_INFORMATION* OutModule));
DRIVER_SHARED_API(NTSTATUS, LhBarrierCallStackTrace(
PVOID* OutMethodArray,
ULONG InMaxMethodCount,
ULONG* OutMethodCount));
#ifdef DRIVER
#define DRIVER_EXPORT(proc) PROC_##proc * proc
#define EASYHOOK_INTERFACE_v_1 0x0001
#define EASYHOOK_WIN32_DEVICE_NAME L"\\\\.\\EasyHook"
#define EASYHOOK_DEVICE_NAME L"\\Device\\EasyHook"
#define EASYHOOK_DOS_DEVICE_NAME L"\\DosDevices\\EasyHook"
#define FILE_DEVICE_EASYHOOK 0x893F
typedef struct _EASYHOOK_INTERFACE_API_v_1_
{
DRIVER_EXPORT(RtlGetLastError);
DRIVER_EXPORT(RtlGetLastErrorString);
DRIVER_EXPORT(LhInstallHook);
DRIVER_EXPORT(LhUninstallHook);
DRIVER_EXPORT(LhWaitForPendingRemovals);
DRIVER_EXPORT(LhBarrierGetCallback);
DRIVER_EXPORT(LhBarrierGetReturnAddress);
DRIVER_EXPORT(LhBarrierGetAddressOfReturnAddress);
DRIVER_EXPORT(LhBarrierBeginStackTrace);
DRIVER_EXPORT(LhBarrierEndStackTrace);
DRIVER_EXPORT(LhBarrierPointerToModule);
DRIVER_EXPORT(LhBarrierGetCallingModule);
DRIVER_EXPORT(LhBarrierCallStackTrace);
DRIVER_EXPORT(LhSetGlobalExclusiveACL);
DRIVER_EXPORT(LhSetGlobalInclusiveACL);
DRIVER_EXPORT(LhSetExclusiveACL);
DRIVER_EXPORT(LhSetInclusiveACL);
DRIVER_EXPORT(LhIsProcessIntercepted);
}EASYHOOK_INTERFACE_API_v_1, *PEASYHOOK_INTERFACE_API_v_1;
typedef struct _EASYHOOK_DEVICE_EXTENSION_
{
ULONG MaxVersion;
// enumeration of APIs
EASYHOOK_INTERFACE_API_v_1 API_v_1;
}EASYHOOK_DEVICE_EXTENSION, *PEASYHOOK_DEVICE_EXTENSION;
static NTSTATUS EasyHookQueryInterface(
ULONG InInterfaceVersion,
PVOID OutInterface,
PFILE_OBJECT* OutEasyHookDrv)
{
/*
Description:
Provides a convenient way to load the desired EasyHook interface.
The method will only work if the EasyHook support driver is loaded, of course.
If you don't need the interface anymore, you have to release the
file object with ObDereferenceObject().
Parameters:
- InInterfaceVersion
The desired interface version. Any future EasyHook driver will ALWAYS
be backward compatible. This is the reason why I provide such a flexible
interface mechanism.
- OutInterface
A pointer to the interface structure to be filled with data. If you specify
EASYHOOK_INTERFACE_v_1 as InInterfaceVersion, you will have to provide a
pointer to a EASYHOOK_INTERFACE_API_v_1 structure, for example...
- OutEasyHookDrv
A reference to the EasyHook driver. Make sure that you dereference it if
you don't need the interface any longer! As long as you keep this handle,
the EasyHook driver CAN'T be unloaded...
*/
UNICODE_STRING DeviceName;
PDEVICE_OBJECT hEasyHookDrv = NULL;
NTSTATUS NtStatus = STATUS_INTERNAL_ERROR;
EASYHOOK_DEVICE_EXTENSION* DevExt;
/*
Open log file...
*/
RtlInitUnicodeString(&DeviceName, EASYHOOK_DEVICE_NAME);
if(!NT_SUCCESS(NtStatus = IoGetDeviceObjectPointer(&DeviceName, FILE_READ_DATA, OutEasyHookDrv, &hEasyHookDrv)))
return NtStatus;
__try
{
DevExt = (EASYHOOK_DEVICE_EXTENSION*)hEasyHookDrv->DeviceExtension;
if(DevExt->MaxVersion < InInterfaceVersion)
return STATUS_NOT_SUPPORTED;
switch(InInterfaceVersion)
{
case EASYHOOK_INTERFACE_v_1: memcpy(OutInterface, &DevExt->API_v_1, sizeof(DevExt->API_v_1)); break;
default:
return STATUS_INVALID_PARAMETER_1;
}
return STATUS_SUCCESS;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
ObDereferenceObject(*OutEasyHookDrv);
return NtStatus;
}
}
#endif // DRIVER
#ifndef DRIVER
/*
Debug helper API.
*/
EASYHOOK_BOOL_EXPORT DbgIsAvailable();
EASYHOOK_BOOL_EXPORT DbgIsEnabled();
EASYHOOK_NT_EXPORT DbgAttachDebugger();
EASYHOOK_NT_EXPORT DbgDetachDebugger();
EASYHOOK_NT_EXPORT DbgGetThreadIdByHandle(
HANDLE InThreadHandle,
ULONG* OutThreadId);
EASYHOOK_NT_EXPORT DbgGetProcessIdByHandle(
HANDLE InProcessHandle,
ULONG* OutProcessId);
EASYHOOK_NT_EXPORT DbgHandleToObjectName(
HANDLE InNamedHandle,
UNICODE_STRING* OutNameBuffer,
ULONG InBufferSize,
ULONG* OutRequiredSize);
/*
Injection support API.
*/
typedef struct _REMOTE_ENTRY_INFO_
{
ULONG HostPID;
UCHAR* UserData;
ULONG UserDataSize;
}REMOTE_ENTRY_INFO;
typedef void __stdcall REMOTE_ENTRY_POINT(REMOTE_ENTRY_INFO* InRemoteInfo);
#define EASYHOOK_INJECT_DEFAULT 0x00000000
#define EASYHOOK_INJECT_STEALTH 0x10000000 // (experimental)
#define EASYHOOK_INJECT_NET_DEFIBRILLATOR 0x20000000 // USE THIS ONLY IN UNMANAGED CODE AND ONLY WITH CreateAndInject() FOR MANAGED PROCESSES!!
EASYHOOK_NT_EXPORT RhCreateStealthRemoteThread(
ULONG InTargetPID,
LPTHREAD_START_ROUTINE InRemoteRoutine,
PVOID InRemoteParam,
HANDLE* OutRemoteThread);
EASYHOOK_NT_EXPORT RhInjectLibrary(
ULONG InTargetPID,
ULONG InWakeUpTID,
ULONG InInjectionOptions,
WCHAR* InLibraryPath_x86,
WCHAR* InLibraryPath_x64,
PVOID InPassThruBuffer,
ULONG InPassThruSize);
EASYHOOK_NT_EXPORT RhCreateAndInject(
WCHAR* InEXEPath,
WCHAR* InCommandLine,
ULONG InProcessCreationFlags,
ULONG InInjectionOptions,
WCHAR* InLibraryPath_x86,
WCHAR* InLibraryPath_x64,
PVOID InPassThruBuffer,
ULONG InPassThruSize,
ULONG* OutProcessId);
EASYHOOK_BOOL_EXPORT RhIsX64System();
EASYHOOK_NT_EXPORT RhIsX64Process(
ULONG InProcessId,
BOOL* OutResult);
EASYHOOK_BOOL_EXPORT RhIsAdministrator();
EASYHOOK_NT_EXPORT RhWakeUpProcess();
EASYHOOK_NT_EXPORT RhInstallSupportDriver();
EASYHOOK_NT_EXPORT RhInstallDriver(
WCHAR* InDriverPath,
WCHAR* InDriverName);
typedef struct _GACUTIL_INFO_* HGACUTIL;
#endif // !DRIVER
#ifdef __cplusplus
};
#endif
#endif