Skip to content

Commit 3d67d2e

Browse files
committed
Land rapid7#9443, Add warning to FileDropper for deleting CWD
2 parents 25d1642 + da23432 commit 3d67d2e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/msf/core/exploit/file_dropper.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ def on_new_session(session)
6161

6262
@dropped_files.delete_if do |file|
6363
exists_before = file_dropper_exist?(session, file)
64+
6465
if file_dropper_delete_file(session, file)
6566
file_dropper_deleted?(session, file, exists_before)
6667
end
6768
end
6869

6970
@dropped_dirs.delete_if do |dir|
71+
if file_dropper_check_cwd?(session, dir)
72+
print_warning("Attempting to delete working directory #{dir}")
73+
end
74+
7075
exists_before = file_dropper_exist?(session, dir)
76+
7177
if file_dropper_delete_dir(session, dir)
7278
file_dropper_deleted?(session, dir, exists_before)
7379
end
@@ -110,6 +116,10 @@ def cleanup
110116
# Check if dir_rm method is available (local exploit, mixin support, module support)
111117
if respond_to?(:dir_rm)
112118
@dropped_dirs.delete_if do |dir|
119+
if respond_to?(:pwd) && pwd.include?(dir)
120+
print_warning("Attempting to delete working directory #{dir}")
121+
end
122+
113123
begin
114124
dir_rm(dir)
115125
rescue ::Exception => e
@@ -250,6 +260,28 @@ def file_dropper_deleted?(session, path, exists_before)
250260
end
251261
end
252262

263+
# Check if the path being removed is the same as the working directory
264+
#
265+
# @param [String] path The path to check
266+
# @return [Boolean] true if the path is the same, otherwise false
267+
def file_dropper_check_cwd?(session, path)
268+
if session.type == 'meterpreter'
269+
return true if path == session.fs.dir.pwd
270+
else
271+
pwd =
272+
if session.platform == 'windows'
273+
session.shell_command_token('echo %cd%')
274+
else
275+
session.shell_command_token('pwd')
276+
end
277+
278+
# Check for subdirectories and relative paths
279+
return true if pwd.include?(path)
280+
end
281+
282+
false
283+
end
284+
253285
# Converts a path to use the windows separator '\'
254286
#
255287
# @param [String] path The path to convert

0 commit comments

Comments
 (0)