Skip to content

Commit bebff78

Browse files
committed
Add timestamping to downloaded files
1 parent 71a2446 commit bebff78

File tree

3 files changed

+26
-9
lines changed
  • lib/rex/post/meterpreter

3 files changed

+26
-9
lines changed

lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def Dir.unlink(path)
195195
# Downloads the contents of a remote directory a
196196
# local directory, optionally in a recursive fashion.
197197
#
198-
def Dir.download(dst, src, recursive = false, force = true, glob = nil, &stat)
198+
def Dir.download(dst, src, recursive = false, force = true, glob = nil, timestamp = nil, &stat)
199199

200200
self.entries(src, glob).each { |src_sub|
201201
dst_item = dst + ::File::SEPARATOR + client.unicode_filter_encode(src_sub)
@@ -208,7 +208,12 @@ def Dir.download(dst, src, recursive = false, force = true, glob = nil, &stat)
208208
src_stat = client.fs.filestat.new(src_item)
209209

210210
if (src_stat.file?)
211+
if timestamp
212+
dst_item << timestamp
213+
end
214+
211215
stat.call('downloading', src_item, dst_item) if (stat)
216+
212217
begin
213218
result = client.fs.file.download_file(dst_item, src_item)
214219
stat.call(result, src_item, dst_item) if (stat)
@@ -231,7 +236,7 @@ def Dir.download(dst, src, recursive = false, force = true, glob = nil, &stat)
231236
end
232237

233238
stat.call('mirroring', src_item, dst_item) if (stat)
234-
download(dst_item, src_item, recursive, force, glob, &stat)
239+
download(dst_item, src_item, recursive, force, glob, timestamp, &stat)
235240
stat.call('mirrored', src_item, dst_item) if (stat)
236241
end
237242
}

lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,19 @@ def File.is_glob?(name)
280280
# If a block is given, it will be called before each file is downloaded and
281281
# again when each download is complete.
282282
#
283-
def File.download(dest, *src_files, &stat)
284-
src_files.each { |src|
283+
def File.download(dest, src_files, timestamp = nil, &stat)
284+
[*src_files].each { |src|
285285
if (::File.basename(dest) != File.basename(src))
286286
# The destination when downloading is a local file so use this
287287
# system's separator
288288
dest += ::File::SEPARATOR + File.basename(src)
289289
end
290290

291+
# XXX: dest can be the same object as src, so we use += instead of <<
292+
if timestamp
293+
dest += timestamp
294+
end
295+
291296
stat.call('downloading', src, dest) if (stat)
292297
result = download_file(dest, src)
293298
stat.call(result, src, dest) if (stat)

lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Console::CommandDispatcher::Stdapi::Fs
2424
#
2525
@@download_opts = Rex::Parser::Arguments.new(
2626
"-h" => [ false, "Help banner." ],
27-
"-r" => [ false, "Download recursively." ])
27+
"-r" => [ false, "Download recursively." ],
28+
"-t" => [ false, "Timestamp downloaded files." ])
2829
#
2930
# Options for the upload command.
3031
#
@@ -332,6 +333,7 @@ def cmd_download(*args)
332333
end
333334

334335
recursive = false
336+
timestamp = false
335337
src_items = []
336338
last = nil
337339
dest = nil
@@ -340,6 +342,8 @@ def cmd_download(*args)
340342
case opt
341343
when "-r"
342344
recursive = true
345+
when "-t"
346+
timestamp = true
343347
when nil
344348
src_items << last if (last)
345349
last = val
@@ -367,6 +371,10 @@ def cmd_download(*args)
367371
dest = ::File.dirname(dest)
368372
end
369373

374+
if timestamp
375+
ts = '_' + Time.now.iso8601
376+
end
377+
370378
# Go through each source item and download them
371379
src_items.each { |src|
372380
glob = nil
@@ -389,8 +397,7 @@ def cmd_download(*args)
389397
src_path = file['path'] + client.fs.file.separator + file['name']
390398
dest_path = src_path.tr(src_separator, ::File::SEPARATOR)
391399

392-
client.fs.file.download(dest_path, src_path) do |step, src, dst|
393-
puts step
400+
client.fs.file.download(dest_path, src_path, ts) do |step, src, dst|
394401
print_status("#{step.ljust(11)}: #{src} -> #{dst}")
395402
client.framework.events.on_session_download(client, src, dest) if msf_loaded?
396403
end
@@ -404,12 +411,12 @@ def cmd_download(*args)
404411
# Perform direct matching
405412
stat = client.fs.file.stat(src)
406413
if (stat.directory?)
407-
client.fs.dir.download(dest, src, recursive, true, glob) do |step, src, dst|
414+
client.fs.dir.download(dest, src, recursive, true, glob, ts) do |step, src, dst|
408415
print_status("#{step.ljust(11)}: #{src} -> #{dst}")
409416
client.framework.events.on_session_download(client, src, dest) if msf_loaded?
410417
end
411418
elsif (stat.file?)
412-
client.fs.file.download(dest, src) do |step, src, dst|
419+
client.fs.file.download(dest, src, ts) do |step, src, dst|
413420
print_status("#{step.ljust(11)}: #{src} -> #{dst}")
414421
client.framework.events.on_session_download(client, src, dest) if msf_loaded?
415422
end

0 commit comments

Comments
 (0)