Skip to content

Commit e546598

Browse files
committed
Implement a method for command shells to register a post-session cleanup command
1 parent 8de760f commit e546598

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/msf/base/sessions/command_shell.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ def self.type
4747
"shell"
4848
end
4949

50-
def initialize(*args)
50+
def initialize(conn, opts = {})
5151
self.platform ||= ""
5252
self.arch ||= ""
5353
self.max_threads = 1
54+
if !opts[:datastore]["CommandShellCleanupCommand"].blank?
55+
@cleanup_command = opts[:datastore]["CommandShellCleanupCommand"]
56+
end
5457
super
5558
end
5659

@@ -193,12 +196,28 @@ def shell_write(buf)
193196
# :category: Msf::Session::Provider::SingleCommandShell implementors
194197
#
195198
# Closes the shell.
199+
# Note: parent's 'self.kill' method calls cleanup below.
196200
#
197201
def shell_close()
198-
rstream.close rescue nil
199202
self.kill
200203
end
201204

205+
##
206+
# :category: Msf::Session implementors
207+
#
208+
# Closes the shell.
209+
#
210+
def cleanup
211+
if rstream
212+
if !@cleanup_command.blank?
213+
shell_command_token(@cleanup_command, 0)
214+
end
215+
rstream.close
216+
rstream = nil
217+
end
218+
super
219+
end
220+
202221
#
203222
# Execute any specified auto-run scripts for this session
204223
#

lib/msf/base/sessions/command_shell_options.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ def initialize(info = {})
1717

1818
register_advanced_options(
1919
[
20-
OptString.new('InitialAutoRunScript', [false, "An initial script to run on session creation (before AutoRunScript)", '']),
21-
OptString.new('AutoRunScript', [false, "A script to run automatically on session creation.", ''])
22-
], self.class)
20+
OptString.new('InitialAutoRunScript', "An initial script to run on session creation (before AutoRunScript)"),
21+
OptString.new('AutoRunScript', "A script to run automatically on session creation."),
22+
OptString.new('ShellCleanupCommand', "A command to run before the session is closed")
23+
]
24+
)
2325
end
2426

2527
def on_session(session)

0 commit comments

Comments
 (0)