Skip to content

Commit cd86a69

Browse files
committed
Have Post::File use shiny new session.fs.file.mv
Also adds a quick and dirty test. Verified working on Linux shell, Linux meterpreter, and Windows x86 and x64 meterpreter.
1 parent 7fbe477 commit cd86a69

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/msf/core/post/file.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,18 @@ def rm_f(*remote_files)
299299
end
300300

301301
#
302-
# Rename a remote file. This is a stopgap until a proper API version is added:
303-
# http://dev.metasploit.com/redmine/issues/7288
302+
# Rename a remote file.
304303
#
305-
def rename_file(new_file, old_file)
306-
#TODO: this is not ideal as the file contents are sent to meterp server and back to the client
307-
write_file(new_file, read_file(old_file))
308-
rm_f(old_file)
304+
def rename_file(old_file, new_file)
305+
if session.respond_to? :commands and session.commands.include?("stdapi_fs_file_move")
306+
session.fs.file.mv(old_file, new_file)
307+
else
308+
if session.platform =~ /win/
309+
cmd_exec(%Q|move /y "#{old_file}" "#{new_file}"|)
310+
else
311+
cmd_exec(%Q|mv -f "#{old_file}" "#{new_file}"|)
312+
end
313+
end
309314
end
310315
alias :move_file :rename_file
311316
alias :mv_file :rename_file

test/modules/post/test/file.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ class Metasploit4 < Msf::Post
1313

1414
def initialize(info={})
1515
super( update_info( info,
16-
'Name' => 'Testing remote file manipulation',
16+
'Name' => 'Testing Remote File Manipulation',
1717
'Description' => %q{ This module will test Post::File API methods },
1818
'License' => MSF_LICENSE,
1919
'Author' => [ 'egypt'],
20-
'Version' => '$Revision$',
2120
'Platform' => [ 'windows', 'linux', 'java' ],
2221
'SessionTypes' => [ 'meterpreter', 'shell' ]
2322
))
@@ -102,6 +101,23 @@ def test_file
102101
not file_exist?("pwned")
103102
end
104103

104+
it "should move files" do
105+
# Make sure we don't have leftovers from a previous run
106+
file_rm("meterpreter-test") rescue nil
107+
file_rm("meterpreter-test-moved") rescue nil
108+
109+
# touch a new file
110+
write_file("meterpreter-test", "")
111+
112+
rename_file("meterpreter-test", "meterpreter-test-moved")
113+
res &&= exist?("meterpreter-test-moved")
114+
res &&= !exist?("meterpreter-test")
115+
116+
# clean up
117+
file_rm("meterpreter-test") rescue nil
118+
file_rm("meterpreter-test-moved") rescue nil
119+
end
120+
105121
end
106122

107123
def test_binary_files

0 commit comments

Comments
 (0)