Skip to content

Commit 129ed7f

Browse files
committed
Add yard documentation
1 parent 0fb21af commit 129ed7f

File tree

1 file changed

+76
-46
lines changed

1 file changed

+76
-46
lines changed

lib/msf/core/exploit/file_dropper.rb

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,20 @@ def initialize(info = {})
1414
], self.class)
1515
end
1616

17-
#
1817
# When a new session is created, attempt to delete any files that the
1918
# exploit created.
2019
#
2120
# @param (see Msf::Exploit#on_new_session)
2221
# @return [void]
23-
#
2422
def on_new_session(session)
2523
super
2624

2725
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}"
6029
end
30+
on_new_session_job(session)
6131
end
6232

6333
#
@@ -123,10 +93,76 @@ def cleanup
12393

12494
private
12595

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
126157
def file_dropper_win_file(file)
127158
file.gsub('/', "\\\\")
128159
end
129160

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
130166
def file_dropper_delete(file)
131167
puts "Deleting #{file}"
132168
win_file = file_dropper_win_file(file)
@@ -162,18 +198,12 @@ def file_dropper_delete(file)
162198
end
163199
end
164200

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
177207
def file_dropper_deleted?(file, exists_before)
178208
puts "Deleted? ... #{file}"
179209
if exists_before && file_dropper_check_file(file)

0 commit comments

Comments
 (0)