@@ -14,50 +14,20 @@ def initialize(info = {})
14
14
] , self . class )
15
15
end
16
16
17
- #
18
17
# When a new session is created, attempt to delete any files that the
19
18
# exploit created.
20
19
#
21
20
# @param (see Msf::Exploit#on_new_session)
22
21
# @return [void]
23
- #
24
22
def on_new_session ( session )
25
23
super
26
24
27
25
print_status ( "new session..." )
28
- on_new_session_job ( session )
29
- end
30
-
31
- def on_new_session_job ( new_session )
32
- if session
33
- session_orig = session
34
- end
35
-
36
- self . session = new_session
37
-
38
- if session . type == 'meterpreter'
39
- session . core . use ( 'stdapi' ) unless session . ext . aliases . include? ( 'stdapi' )
40
- end
41
-
42
- if not @dropped_files or @dropped_files . empty?
43
- return true
44
- end
45
-
46
- @dropped_files . delete_if do |file |
47
- puts "Deleting #{ file } "
48
- exists_before = file_dropper_check_file ( file )
49
- if file_dropper_delete ( file )
50
- file_dropper_deleted? ( file , exists_before )
51
- else
52
- false
53
- end
54
- end
55
-
56
- if session_orig
57
- self . session = session_orig
58
- else
59
- self . session = nil
26
+ puts "#{ self . session } "
27
+ if self . payload
28
+ puts "#{ self . payload . session } "
60
29
end
30
+ on_new_session_job ( session )
61
31
end
62
32
63
33
#
@@ -123,10 +93,76 @@ def cleanup
123
93
124
94
private
125
95
96
+ # Takes a new session and makes the drop files task
97
+ #
98
+ # @param (see Msf::Exploit#on_new_session)
99
+ # @return [void]
100
+ # @note This methods needs to overwrite *and restore* the original
101
+ # `session` attribute
102
+ def on_new_session_job ( new_session )
103
+ session_orig = session
104
+ self . session = new_session
105
+
106
+ begin
107
+ file_dropper_delete_files
108
+ ensure
109
+ self . session = session_orig
110
+ end
111
+ end
112
+
113
+ # Uses the exploit `session` to delete files on the `dropped_files` list
114
+ #
115
+ # @return [void]
116
+ def file_dropper_delete_files
117
+ if session . type == 'meterpreter'
118
+ session . core . use ( 'stdapi' ) unless session . ext . aliases . include? ( 'stdapi' )
119
+ end
120
+
121
+ unless @dropped_files && @dropped_files . length > 0
122
+ return
123
+ end
124
+
125
+ @dropped_files . delete_if do |file |
126
+ puts "Deleting #{ file } "
127
+ exists_before = file_dropper_check_file ( file )
128
+ if file_dropper_delete ( file )
129
+ file_dropper_deleted? ( file , exists_before )
130
+ else
131
+ false
132
+ end
133
+ end
134
+ end
135
+
136
+ # Check if a file exists in the `session` file system
137
+ #
138
+ # @param [String] file The file to check
139
+ # @return [TrueClass] If the file exists
140
+ # @return [FalseClass] If the file doesn't exist
141
+ def file_dropper_check_file ( file )
142
+ puts "Checking file... #{ file } "
143
+ if session . platform =~ /win/
144
+ normalized = file_dropper_win_file ( file )
145
+ else
146
+ normalized = file
147
+ end
148
+
149
+ puts "Checking normalized file... #{ file } "
150
+ Msf ::Post ::File . file_exist? ( normalized )
151
+ end
152
+
153
+ # Converts a file path to use the windows separator '\'
154
+ #
155
+ # @param [String] file The file path to convert
156
+ # @return [String] The file path converted
126
157
def file_dropper_win_file ( file )
127
158
file . gsub ( '/' , "\\ \\ " )
128
159
end
129
160
161
+ # Sends a file deletion command to the remote `session`
162
+ #
163
+ # @param [String] file The file to delete
164
+ # @return [TrueClass] If the delete command has been executed in the remote machine
165
+ # @return [FalseClass] Otherwise
130
166
def file_dropper_delete ( file )
131
167
puts "Deleting #{ file } "
132
168
win_file = file_dropper_win_file ( file )
@@ -162,18 +198,12 @@ def file_dropper_delete(file)
162
198
end
163
199
end
164
200
165
- def file_dropper_check_file ( file )
166
- puts "Checking file... #{ file } "
167
- if session . platform =~ /win/
168
- normalized = file_dropper_win_file ( file )
169
- else
170
- normalized = file
171
- end
172
-
173
- puts "Checking normalized file... #{ file } "
174
- Msf ::Post ::File . file_exist? ( normalized )
175
- end
176
-
201
+ # Checks if a file has been deleted by the current job
202
+ #
203
+ # @param [String] file The file to check
204
+ # @param [TrueClass] exists_before Indicates if the file existed before the cleanup job
205
+ # @return [TrueClass] if the file has been deleted or it cannot resolve
206
+ # @return [FalseClass] if the file hasn't been deleted
177
207
def file_dropper_deleted? ( file , exists_before )
178
208
puts "Deleted? ... #{ file } "
179
209
if exists_before && file_dropper_check_file ( file )
0 commit comments