Skip to content

Commit

Permalink
Land #16021, Add check for deleting directories to post/test/file
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 committed Jan 7, 2022
2 parents 6bd94dc + 6b64985 commit 1d3a6d5
Showing 1 changed file with 91 additions and 20 deletions.
111 changes: 91 additions & 20 deletions test/modules/post/test/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ def initialize(info = {})
'Name' => 'Testing Remote File Manipulation',
'Description' => %q{ This module will test Post::File API methods },
'License' => MSF_LICENSE,
'Author' => [ 'egypt'],
'Author' => [ 'egypt' ],
'Platform' => [ 'windows', 'linux', 'java' ],
'SessionTypes' => [ 'meterpreter', 'shell' ]
)
)

register_options(
[
OptString.new('BaseFileName', [true, 'File name to create', 'meterpreter-test'])
OptString.new('BaseDirectoryName', [true, 'Directory name to create', 'test-dir']),
OptString.new('BaseFileName', [true, 'File name to create', 'test-file'])
], self.class
)
end
Expand All @@ -46,6 +47,78 @@ def setup
super
end

def test_dir
fs_sep = session.platform == 'windows' ? '\\' : '/'

it 'should test for directory existence' do
ret = false
[
'c:\\',
'/etc/',
'/tmp'
].each do |path|
ret = true if directory?(path)
end

ret
end

it 'should create directories' do
mkdir(datastore['BaseDirectoryName'])
ret = directory?(datastore['BaseDirectoryName'])
ret &&= write_file([datastore['BaseDirectoryName'], 'file'].join(fs_sep), '')
ret &&= mkdir([datastore['BaseDirectoryName'], 'directory'].join(fs_sep))
ret &&= write_file([datastore['BaseDirectoryName'], 'directory', 'file'].join(fs_sep), '')
ret
end

it 'should list the directory we just made' do
dents = dir(datastore['BaseDirectoryName'])
dents.include?('file') && dents.include?('directory')
end

it 'should recursively delete the directory we just made' do
rm_rf(datastore['BaseDirectoryName'])
!directory?(datastore['BaseDirectoryName'])
end

unless (session.platform == 'windows') || (session.platform != 'windows' && command_exists?('ln'))
print_warning('skipping link related checks because the target is incompatible')
else
it 'should delete a symbolic link target' do
mkdir(datastore['BaseDirectoryName'])
ret = directory?(datastore['BaseDirectoryName'])
link = "#{datastore['BaseDirectoryName']}.lnk"
ret &&= write_file([datastore['BaseDirectoryName'], 'file'].join(fs_sep), '')
make_symlink(datastore['BaseDirectoryName'], link)
unless exists?(link)
print_error('failed to create the symbolic link')
end
rm_rf(link)
# the link should have been deleted
ret &&= !exists?(link)
# but the target directory and its contents should still be intact
ret &&= exists?("#{[datastore['BaseDirectoryName'], 'file'].join(fs_sep)}")
rm_rf(datastore['BaseDirectoryName'])
ret
end

it 'should not recurse into symbolic link directories' do
mkdir(datastore['BaseDirectoryName'] + '.1')
mkdir(datastore['BaseDirectoryName'] + '.2')
ret = directory?(datastore['BaseDirectoryName'] + '.1') && directory?(datastore['BaseDirectoryName'] + '.2')
ret &&= write_file([datastore['BaseDirectoryName'] + '.1', 'file'].join(fs_sep), '')
# make a symlink in dir.2 to dir.1 to ensure the deletion does not recurse into dir.1
make_symlink("#{datastore['BaseDirectoryName']}.1", "#{datastore['BaseDirectoryName']}.2/link")
rm_rf("#{datastore['BaseDirectoryName']}.2")
# check that dir.1's contests are still intact
ret &&= exists?([datastore['BaseDirectoryName'] + '.1', 'file'].join(fs_sep))
rm_rf("#{datastore['BaseDirectoryName']}.1")
ret
end
end
end

def test_file
it 'should test for file existence' do
ret = false
Expand All @@ -63,19 +136,6 @@ def test_file
ret
end

it 'should test for directory existence' do
ret = false
[
'c:\\',
'/etc/',
'/tmp'
].each do |path|
ret = true if directory?(path)
end

ret
end

it 'should create text files' do
rm_f(datastore['BaseFileName'])
ret = write_file(datastore['BaseFileName'], 'foo')
Expand Down Expand Up @@ -188,31 +248,31 @@ def test_binary_files

def test_path_expansion_nix
unless session.platform =~ /win/i
it "should expand home" do
it 'should expand home' do
home1 = expand_path('~')
home2 = expand_path('$HOME')
home1 == home2 && home1.length > 0
end

it "non-isolated tilde should not expand" do
it 'should not expand non-isolated tilde' do
s = '~a'
result = expand_path(s)
s == result
end

it "mid-string tilde should not expand" do
it 'should not expand mid-string tilde' do
s = '/home/~'
result = expand_path(s)
s == result
end

it "env vars with invalid naming should not expand" do
it 'should not expand env vars with invalid naming' do
s = 'no environment $ variables /here'
result = expand_path(s)
s == result
end

it "should expand multiple variables" do
it 'should expand multiple variables' do
result = expand_path('/blah/$HOME/test/$USER')
home = expand_path('$HOME')
user = expand_path('$USER')
Expand All @@ -228,4 +288,15 @@ def cleanup
super
end

def make_symlink(target, symlink)
if session.platform == 'windows'
cmd_exec("cmd.exe /c mklink #{directory?(target) ? '/D ' : ''}#{symlink} #{target}")
else
cmd_exec("ln -s $(pwd)/#{target} $(pwd)/#{symlink}")
end
end

def register_dir_for_cleanup(path)
end

end

0 comments on commit 1d3a6d5

Please sign in to comment.