-
Notifications
You must be signed in to change notification settings - Fork 72
/
module.h
339 lines (267 loc) · 6.63 KB
/
module.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
/*
* Copyright (c) 2008 Arab Team 4 Reverse Engineering. All rights reserved.
*
* Module Name:
*
* module.c
*
* Abstract:
*
* This module defines various routines used to deal with kernel-mode drivers.
*
* Author:
*
* GamingMasteR
*
*/
#include "KeDetective.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MI_UNLOADED_DRIVERS 50
typedef struct _UNLOADED_DRIVERS {
UNICODE_STRING Name;
PVOID StartAddress;
PVOID EndAddress;
LARGE_INTEGER CurrentTime;
} UNLOADED_DRIVERS, *PUNLOADED_DRIVERS;
typedef struct _CDRIVER_OBJECT {
PVOID ImageBase;
PVOID DriverObject;
struct _KLDR_DATA_TABLE_ENTRY *LdrEntry;
PVOID Unload;
PVOID EntryPoint;
ULONG ImageSize;
UNICODE_STRING ImagePath;
} CDRIVER_OBJECT, *PCDRIVER_OBJECT;
//
// Loader Data Table Entry Flags
//
#define LDRP_STATIC_LINK 0x00000002
#define LDRP_IMAGE_DLL 0x00000004
#define LDRP_LOAD_IN_PROGRESS 0x00001000
#define LDRP_UNLOAD_IN_PROGRESS 0x00002000
#define LDRP_ENTRY_PROCESSED 0x00004000
#define LDRP_ENTRY_INSERTED 0x00008000
#define LDRP_CURRENT_LOAD 0x00010000
#define LDRP_FAILED_BUILTIN_LOAD 0x00020000
#define LDRP_DONT_CALL_FOR_THREADS 0x00040000
#define LDRP_PROCESS_ATTACH_CALLED 0x00080000
#define LDRP_DEBUG_SYMBOLS_LOADED 0x00100000
#define LDRP_IMAGE_NOT_AT_BASE 0x00200000
#define LDRP_COR_IMAGE 0x00400000
#define LDR_COR_OWNS_UNMAP 0x00800000
#define LDRP_SYSTEM_MAPPED 0x01000000
#define LDRP_IMAGE_VERIFYING 0x02000000
#define LDRP_DRIVER_DEPENDENT_DLL 0x04000000
#define LDRP_ENTRY_NATIVE 0x08800000
#define LDRP_REDIRECTED 0x10000000
#define LDRP_NON_PAGED_DEBUG_INFO 0x20000000
#define LDRP_MM_LOADED 0x40000000
#define LDRP_COMPAT_DATABASE_PROCESSED 0x80000000
//
// Loader Data Table Entry
//
typedef struct _LDR_DATA_TABLE_ENTRY
{
LIST_ENTRY InLoadOrderLinks;
LIST_ENTRY InMemoryOrderModuleList;
LIST_ENTRY InInitializationOrderModuleList;
PVOID DllBase;
PVOID EntryPoint;
ULONG SizeOfImage;
UNICODE_STRING FullDllName;
UNICODE_STRING BaseDllName;
ULONG Flags;
USHORT LoadCount;
USHORT TlsIndex;
union
{
LIST_ENTRY HashLinks;
PVOID SectionPointer;
};
ULONG CheckSum;
union
{
ULONG TimeDateStamp;
PVOID LoadedImports;
};
PVOID EntryPointActivationContext;
PVOID PatchInformation;
} LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;
typedef struct _KLDR_DATA_TABLE_ENTRY {
LIST_ENTRY InLoadOrderLinks;
PVOID ExceptionTable;
ULONG ExceptionTableSize;
// ULONG padding on IA64
PVOID GpValue;
PNON_PAGED_DEBUG_INFO NonPagedDebugInfo;
PVOID DllBase;
PVOID EntryPoint;
ULONG SizeOfImage;
UNICODE_STRING FullDllName;
UNICODE_STRING BaseDllName;
ULONG Flags;
USHORT LoadCount;
USHORT __Unused5;
PVOID SectionPointer;
ULONG CheckSum;
// ULONG padding on IA64
PVOID LoadedImports;
PVOID PatchInformation;
} KLDR_DATA_TABLE_ENTRY, *PKLDR_DATA_TABLE_ENTRY;
#define NUMBER_HASH_BUCKETS 37
typedef struct _OBJECT_DIRECTORY_ENTRY {
struct _OBJECT_DIRECTORY_ENTRY *ChainLink;
PVOID Object;
} OBJECT_DIRECTORY_ENTRY, *POBJECT_DIRECTORY_ENTRY;
typedef struct _OBJECT_DIRECTORY {
struct _OBJECT_DIRECTORY_ENTRY *HashBuckets[ NUMBER_HASH_BUCKETS ];
struct _OBJECT_DIRECTORY_ENTRY **LookupBucket;
BOOLEAN LookupFound;
USHORT SymbolicLinkUsageCount;
struct _DEVICE_MAP *DeviceMap;
} OBJECT_DIRECTORY, *POBJECT_DIRECTORY;
VOID
GetDriverInfo(
PCDRIVER_OBJECT DriverObject,
PDRIVER_ENTRY lpBuffer
);
VOID
GetModuleRange(
PVOID* Base,
ULONG* Size,
PCWSTR FileName
);
PWCHAR
ModuleFromAddress(
PVOID Addr,
BOOL GetSymbol,
BOOL GetExport,
PWCHAR Module,
SIZE_T Size
);
PVOID
GetModuleHandle(
LPSTR lpModuleName
);
PVOID
GetModuleHandleW(PWCHAR lpModuleName);
PVOID
KdGetSystemRoutineAddress(
PCWSTR SystemRoutineName
);
PVOID
GetSystemRoutineAddress(
PCWSTR SystemRoutineName
);
PVOID
GetProcAddress(
PVOID DllBase,
PCHAR RoutineName
);
PCHAR
GetAddressProc(
PVOID DllBase,
PVOID RoutineAddress
);
PIMAGE_SECTION_HEADER
ImageRvaToSection(
PVOID Base,
ULONG Rva
);
PIMAGE_SECTION_HEADER
ImageVaToSection(
PVOID Base,
ULONG Va
);
PVOID
GetRealProcAddress(
PVOID hModule,
PVOID file,
LPSTR lpProcName
);
VOID
WalkDirectory(
POBJECT_DIRECTORY Directory,
POBJECT_TYPE Type,
PVOID **ObjectArray
);
PIMAGE_SECTION_HEADER RtlImageRvaToSection(PVOID, ULONG);
ULONG EnumUnloadedDrivers(PDRIVER_ENTRY *);
class CDriver
{
public:
CDriver() {
this->Objects = 0;
this->DriverCount = 0;
};
~CDriver() {
MmFree(this->Objects);
};
VOID ScanModuleList();
VOID ScanDriverType();
VOID ScanDeviceType();
VOID ScanDriverDirectory();
VOID ScanDeviceDirectory();
VOID ScanPhysicalMemory();
VOID GrabDriver(PKLDR_DATA_TABLE_ENTRY Driver);
VOID GrabDriver(PDRIVER_OBJECT DriverObject);
VOID Scan() {
this->ScanDriverType();
this->ScanDeviceType();
this->ScanDriverDirectory();
this->ScanDeviceDirectory();
this->ScanModuleList();
//this->ScanPhysicalMemory();
};
void * __cdecl operator new(unsigned int count)
{
return MmAlloc(count);
};
void __cdecl operator delete(void * ptr)
{
MmFree(ptr);
};
private:
public:
PCDRIVER_OBJECT Objects;
ULONG DriverCount;
};
class CDevice
{
public:
CDevice() {
this->DeviceArray = 0;
this->DeviceCount = 0;
};
~CDevice() {
MmFree(this->DeviceArray);
};
VOID ScanDeviceType();
VOID ScanDeviceDirectory();
VOID GrabDevice(PDEVICE_OBJECT DeviceObject);
VOID Scan() {
this->ScanDeviceType();
this->ScanDeviceDirectory();
};
void * __cdecl operator new(unsigned int count)
{
return MmAlloc(count);
};
void __cdecl operator delete(void * ptr)
{
MmFree(ptr);
};
private:
BOOLEAN IsFound(PDEVICE_OBJECT DeviceObject);
public:
PDEVICE_OBJECT *DeviceArray;
ULONG DeviceCount;
};
extern PLIST_ENTRY PsLoadedModuleList;
extern PLIST_ENTRY MmLoadedUserImageList;
extern CDriver *gDrivers;
#ifdef __cplusplus
}
#endif