Files
metasploit-gs/modules/post/windows/gather/enum_dirperms.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

151 lines
3.8 KiB
Ruby
Raw Normal View History

##
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
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Post
2013-09-05 20:36:52 +01:00
include Msf::Post::Windows::Accounts
2013-08-30 16:28:54 -05:00
2021-09-10 12:53:39 +01:00
def initialize(info = {})
super(
update_info(
info,
2023-02-08 13:47:34 +00:00
'Name' => 'Windows Gather Directory Permissions Enumeration',
2021-09-10 12:53:39 +01:00
'Description' => %q{
This module enumerates directories and lists the permissions set
on found directories. Please note: if the PATH option isn't specified,
then the module will start enumerate whatever is in the target machine's
%PATH% variable.
},
'License' => MSF_LICENSE,
'Platform' => ['win'],
'SessionTypes' => ['meterpreter'],
'Author' => [
2013-09-05 13:41:25 -05:00
'Kx499',
'Ben Campbell',
2013-09-05 13:41:25 -05:00
'sinn3r'
2021-10-06 13:43:31 +01:00
],
'Compat' => {
'Meterpreter' => {
'Commands' => %w[
stdapi_fs_stat
]
}
}
2021-09-10 12:53:39 +01:00
)
)
2013-09-05 13:41:25 -05:00
register_options(
[
OptString.new('PATH', [ false, 'Directory to begin search from', '']),
OptEnum.new('FILTER', [ false, 'Filter to limit results by', 'NA', [ 'NA', 'R', 'W', 'RW' ]]),
2021-09-10 12:53:39 +01:00
OptInt.new('DEPTH', [ true, 'Depth to drill down into subdirs, O = no limit', 0]),
]
)
2013-09-05 13:41:25 -05:00
end
def enum_subdirs(perm_filter, dpath, maxdepth, token)
filter = datastore['FILTER']
filter = nil if datastore['FILTER'] == 'NA'
begin
dirs = session.fs.dir.foreach(dpath)
rescue Rex::Post::Meterpreter::RequestError
# Sometimes we cannot see the dir
dirs = []
end
2023-02-08 13:47:34 +00:00
if (maxdepth >= 1) || (maxdepth < 0)
2021-09-10 12:53:39 +01:00
dirs.each do |d|
2013-09-05 13:41:25 -05:00
next if d =~ /^(\.|\.\.)$/
2021-09-10 12:53:39 +01:00
2013-09-05 13:41:25 -05:00
realpath = dpath + '\\' + d
2023-02-08 13:47:34 +00:00
next unless session.fs.file.stat(realpath).directory?
perm = check_dir_perms(realpath, token)
if perm_filter && perm && perm.include?(perm_filter)
print_status(perm + "\t" + realpath)
2013-09-05 13:41:25 -05:00
end
2023-02-08 13:47:34 +00:00
enum_subdirs(perm_filter, realpath, maxdepth - 1, token)
2013-09-05 13:41:25 -05:00
end
end
end
def get_paths
p = datastore['PATH']
2023-02-08 13:47:34 +00:00
return [p] if !p.nil? && !p.empty?
2013-09-05 13:41:25 -05:00
begin
2023-02-08 13:47:34 +00:00
p = cmd_exec('cmd.exe', '/c echo %PATH%')
2013-09-05 13:41:25 -05:00
rescue Rex::Post::Meterpreter::RequestError => e
vprint_error(e.message)
return []
end
print_status("Option 'PATH' isn't specified. Using system %PATH%")
if p.include?(';')
return p.split(';')
else
return [p]
end
end
def get_token
2023-02-08 13:47:34 +00:00
print_status('Getting impersonation token...')
2013-09-05 13:41:25 -05:00
begin
2023-02-08 13:47:34 +00:00
t = get_imperstoken
2013-09-05 13:41:25 -05:00
rescue ::Exception => e
# Failure due to timeout, access denied, etc.
2013-09-05 20:36:52 +01:00
t = nil
2013-09-05 13:41:25 -05:00
vprint_error("Error #{e.message} while using get_imperstoken()")
vprint_error(e.backtrace)
end
return t
end
def enum_perms(perm_filter, token, depth, paths)
paths.each do |path|
next if path.empty?
2021-09-10 12:53:39 +01:00
2013-09-05 13:41:25 -05:00
path = path.strip
print_status("Checking directory permissions from: #{path}")
2013-09-05 20:36:52 +01:00
perm = check_dir_perms(path, token)
2023-02-08 13:47:34 +00:00
next if perm.nil?
2013-09-05 13:41:25 -05:00
2023-02-08 13:47:34 +00:00
# Show the permission of the parent directory
if perm_filter && perm.include?(perm_filter)
print_status(perm + "\t" + path)
2013-09-05 13:41:25 -05:00
end
2023-02-08 13:47:34 +00:00
# call recursive function to loop through and check all sub directories
enum_subdirs(perm_filter, path, depth, token)
2013-09-05 13:41:25 -05:00
end
end
def run
perm_filter = datastore['FILTER']
perm_filter = nil if datastore['FILTER'] == 'NA'
paths = get_paths
if paths.empty?
2023-02-08 13:47:34 +00:00
print_error('Unable to get the path')
2013-09-05 13:41:25 -05:00
return
end
depth = -1
if datastore['DEPTH'] > 0
depth = datastore['DEPTH']
end
t = get_token
2023-02-08 13:47:34 +00:00
if t
print_status("Got token: #{t}...")
2013-09-05 13:41:25 -05:00
enum_perms(perm_filter, t, depth, paths)
2023-02-08 13:47:34 +00:00
else
print_error('Getting impersonation token failed')
2013-09-05 13:41:25 -05:00
end
end
end