@@ -61,13 +61,19 @@ def on_new_session(session)
61
61
62
62
@dropped_files . delete_if do |file |
63
63
exists_before = file_dropper_exist? ( session , file )
64
+
64
65
if file_dropper_delete_file ( session , file )
65
66
file_dropper_deleted? ( session , file , exists_before )
66
67
end
67
68
end
68
69
69
70
@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
+
70
75
exists_before = file_dropper_exist? ( session , dir )
76
+
71
77
if file_dropper_delete_dir ( session , dir )
72
78
file_dropper_deleted? ( session , dir , exists_before )
73
79
end
@@ -110,6 +116,10 @@ def cleanup
110
116
# Check if dir_rm method is available (local exploit, mixin support, module support)
111
117
if respond_to? ( :dir_rm )
112
118
@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
+
113
123
begin
114
124
dir_rm ( dir )
115
125
rescue ::Exception => e
@@ -250,6 +260,28 @@ def file_dropper_deleted?(session, path, exists_before)
250
260
end
251
261
end
252
262
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
+
253
285
# Converts a path to use the windows separator '\'
254
286
#
255
287
# @param [String] path The path to convert
0 commit comments