Skip to content

Commit 2b96f8e

Browse files
committed
Land rapid7#9353, Implement CommandShellCleanupCommand
2 parents 3516305 + a444bdb commit 2b96f8e

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/msf/base/sessions/command_shell.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ 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+
datastore = opts[:datastore]
55+
if datastore && !datastore["CommandShellCleanupCommand"].blank?
56+
@cleanup_command = opts[:datastore]["CommandShellCleanupCommand"]
57+
end
5458
super
5559
end
5660

@@ -193,12 +197,28 @@ def shell_write(buf)
193197
# :category: Msf::Session::Provider::SingleCommandShell implementors
194198
#
195199
# Closes the shell.
200+
# Note: parent's 'self.kill' method calls cleanup below.
196201
#
197202
def shell_close()
198-
rstream.close rescue nil
199203
self.kill
200204
end
201205

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

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('CommandShellCleanupCommand', "A command to run before the session is closed")
23+
]
24+
)
2325
end
2426

2527
def on_session(session)

0 commit comments

Comments
 (0)