10
10
##
11
11
12
12
require 'msf/core'
13
- require 'rex '
13
+ require 'msf/core/post/common '
14
14
15
15
class Metasploit3 < Msf ::Post
16
16
17
+ include Msf ::Post ::Common
18
+
17
19
def initialize ( info = { } )
18
20
super ( update_info ( info ,
19
21
'Name' => "Windows Gather Directory Permissions Enumeration" ,
20
22
'Description' => %q{
21
23
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.
23
27
} ,
24
28
'License' => MSF_LICENSE ,
25
29
'Version' => '$Revision$' ,
@@ -30,7 +34,7 @@ def initialize(info={})
30
34
31
35
register_options (
32
36
[
33
- OptString . new ( 'PATH' , [ true , 'Directory to begin search from' , '' ] ) ,
37
+ OptString . new ( 'PATH' , [ false , 'Directory to begin search from' , '' ] ) ,
34
38
OptEnum . new ( 'FILTER' , [ false , 'Filter to limit results by' , 'NA' , [ 'NA' , 'R' , 'W' , 'RW' ] ] ) ,
35
39
OptInt . new ( 'DEPTH' , [ true , 'Depth to drill down into subdirs, O = no limit' , 0 ] ) ,
36
40
] , self . class )
@@ -90,40 +94,46 @@ def check_dir(dir, token)
90
94
if w [ "GrantedAccess" ] > 0 then result << "W" end
91
95
end
92
96
93
- def enum_subdirs ( dpath , maxdepth , token )
97
+ def enum_subdirs ( perm_filter , dpath , maxdepth , token )
94
98
filter = datastore [ 'FILTER' ]
95
99
filter = nil if datastore [ 'FILTER' ] == 'NA'
100
+
96
101
dirs = session . fs . dir . foreach ( dpath )
102
+
97
103
if maxdepth >= 1 or maxdepth < 0
98
104
dirs . each do |d |
99
105
next if d =~ /^(\. |\. \. )$/
100
106
realpath = dpath + '\\' + d
101
107
if session . fs . file . stat ( realpath ) . directory?
102
108
perm = check_dir ( realpath , token )
103
- if ! filter or perm . include? filter
109
+ if perm_filter and perm . include? ( perm_filter )
104
110
print_status ( perm + "\t " + realpath )
105
111
end
106
- enum_subdirs ( realpath , maxdepth - 1 , token )
112
+ enum_subdirs ( perm_filter , realpath , maxdepth - 1 , token )
107
113
end
108
114
end
109
115
end
110
116
end
111
117
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?
114
121
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 [ ]
118
127
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 ]
124
133
end
134
+ end
125
135
126
- #get impersonation token
136
+ def get_token
127
137
print_status ( "Getting impersonation token..." )
128
138
begin
129
139
t = get_imperstoken ( )
@@ -133,19 +143,51 @@ def run
133
143
vprint_error ( "Error #{ e . message } while using get_imperstoken()" )
134
144
vprint_error ( e . backtrace )
135
145
end
146
+ return t
147
+ end
136
148
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
143
162
144
- is_path_valid = check_dir ( path , t )
145
- if not is_path_valid . nil?
146
163
#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 )
148
165
end
149
166
end
150
167
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
151
193
end
0 commit comments