Skip to content

Commit be36083

Browse files
committed
Make PATH optional, also correct a filtering bug
If the PATH option is not specified, the module will try to enumerate from %PATH%. Also, this commit fixes a bug in the filtering routine (basically the filtering routine didn't really work).
1 parent 72f0a56 commit be36083

File tree

1 file changed

+68
-26
lines changed

1 file changed

+68
-26
lines changed

modules/post/windows/gather/enum_dirperms.rb

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
##
1111

1212
require 'msf/core'
13-
require 'rex'
13+
require 'msf/core/post/common'
1414

1515
class Metasploit3 < Msf::Post
1616

17+
include Msf::Post::Common
18+
1719
def initialize(info={})
1820
super(update_info(info,
1921
'Name' => "Windows Gather Directory Permissions Enumeration",
2022
'Description' => %q{
2123
This module enumerates directories and lists the permissions set
22-
on found directories.
24+
on found directories. Please note: if the PATH option isn't specified,
25+
then the module will start enumerate whatever is in the target machine's
26+
%PATH% variable.
2327
},
2428
'License' => MSF_LICENSE,
2529
'Version' => '$Revision$',
@@ -30,7 +34,7 @@ def initialize(info={})
3034

3135
register_options(
3236
[
33-
OptString.new('PATH', [ true, 'Directory to begin search from', '']),
37+
OptString.new('PATH', [ false, 'Directory to begin search from', '']),
3438
OptEnum.new('FILTER', [ false, 'Filter to limit results by', 'NA', [ 'NA', 'R', 'W', 'RW' ]]),
3539
OptInt.new('DEPTH', [ true, 'Depth to drill down into subdirs, O = no limit',0]),
3640
], self.class)
@@ -90,40 +94,46 @@ def check_dir(dir, token)
9094
if w["GrantedAccess"] > 0 then result << "W" end
9195
end
9296

93-
def enum_subdirs(dpath, maxdepth, token)
97+
def enum_subdirs(perm_filter, dpath, maxdepth, token)
9498
filter = datastore['FILTER']
9599
filter = nil if datastore['FILTER'] == 'NA'
100+
96101
dirs = session.fs.dir.foreach(dpath)
102+
97103
if maxdepth >= 1 or maxdepth < 0
98104
dirs.each do|d|
99105
next if d =~ /^(\.|\.\.)$/
100106
realpath = dpath + '\\' + d
101107
if session.fs.file.stat(realpath).directory?
102108
perm = check_dir(realpath, token)
103-
if !filter or perm.include? filter
109+
if perm_filter and perm.include?(perm_filter)
104110
print_status(perm + "\t" + realpath)
105111
end
106-
enum_subdirs(realpath, maxdepth - 1,token)
112+
enum_subdirs(perm_filter, realpath, maxdepth - 1,token)
107113
end
108114
end
109115
end
110116
end
111117

112-
def run
113-
t = 0 #holds impers token
118+
def get_paths
119+
p = datastore['PATH']
120+
return [p] if not p.nil? and not p.empty?
114121

115-
#check and set vars
116-
if not datastore['PATH'].empty?
117-
path = datastore['PATH']
122+
begin
123+
p = cmd_exec("cmd.exe", "/c echo %PATH%")
124+
rescue Rex::Post::Meterpreter::RequestError => e
125+
vprint_error(e.message)
126+
return []
118127
end
119-
120-
depth = -1
121-
122-
if datastore['DEPTH'] > 0
123-
depth = datastore['DEPTH']
128+
print_status("Option 'PATH' isn't specified. Using system %PATH%")
129+
if p.include?(';')
130+
return p.split(';')
131+
else
132+
return [p]
124133
end
134+
end
125135

126-
#get impersonation token
136+
def get_token
127137
print_status("Getting impersonation token...")
128138
begin
129139
t = get_imperstoken()
@@ -133,19 +143,51 @@ def run
133143
vprint_error("Error #{e.message} while using get_imperstoken()")
134144
vprint_error(e.backtrace)
135145
end
146+
return t
147+
end
136148

137-
#loop through sub dirs if we have an impers token..else error
138-
if t == 0
139-
print_error("Getting impersonation token failed")
140-
else
141-
print_status("Got token...")
142-
print_status("Checking directory permissions from: " + path)
149+
def enum_perms(perm_filter, token, depth, paths)
150+
paths.each do |path|
151+
next if path.empty?
152+
path = path.strip
153+
154+
print_status("Checking directory permissions from: #{path}")
155+
156+
perm = check_dir(path, token)
157+
if not perm.nil?
158+
# Show the permission of the parent directory
159+
if perm_filter and perm.include?(perm_filter)
160+
print_status(perm + "\t" + path)
161+
end
143162

144-
is_path_valid = check_dir(path, t)
145-
if not is_path_valid.nil?
146163
#call recursive function to loop through and check all sub directories
147-
enum_subdirs(path, depth, t)
164+
enum_subdirs(perm_filter, path, depth, token)
148165
end
149166
end
150167
end
168+
169+
def run
170+
perm_filter = datastore['FILTER']
171+
perm_filter = nil if datastore['FILTER'] == 'NA'
172+
173+
paths = get_paths
174+
if paths.empty?
175+
print_error("Unable to get the path")
176+
return
177+
end
178+
179+
depth = -1
180+
if datastore['DEPTH'] > 0
181+
depth = datastore['DEPTH']
182+
end
183+
184+
t = get_token
185+
186+
if t == 0
187+
print_error("Getting impersonation token failed")
188+
else
189+
print_status("Got token: #{t.to_s}...")
190+
enum_perms(perm_filter, t, depth, paths)
191+
end
192+
end
151193
end

0 commit comments

Comments
 (0)