Skip to content

Commit 6241e48

Browse files
author
Brent Cook
committed
Land rapid7#7350, add 'sess' command for direct session switching support
2 parents de94348 + 438ba0e commit 6241e48

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

features/commands/help.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Feature: Help command
4242
route Route traffic through a session
4343
save Saves the active datastores
4444
search Searches module names and descriptions
45+
sess Interact with a given session
4546
sessions Dump session listings and display information about sessions
4647
set Sets a context-specific variable to a value
4748
setg Sets a global variable to a value

lib/msf/ui/console/command_dispatcher/core.rb

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def commands
136136
"route" => "Route traffic through a session",
137137
"save" => "Saves the active datastores",
138138
"search" => "Searches module names and descriptions",
139+
"sess" => "Interact with a given session",
139140
"sessions" => "Dump session listings and display information about sessions",
140141
"set" => "Sets a context-specific variable to a value",
141142
"setg" => "Sets a global variable to a value",
@@ -1753,6 +1754,25 @@ def cmd_spool(*args)
17531754
return
17541755
end
17551756

1757+
def cmd_sess_help
1758+
print_line('Usage: sess <session id>')
1759+
print_line
1760+
print_line('Interact with the given session ID.')
1761+
print_line('This works the same as: sessions -i <session id>')
1762+
print_line
1763+
end
1764+
1765+
#
1766+
# Helper function to quickly select a session
1767+
#
1768+
def cmd_sess(*args)
1769+
if args.length == 0 || args[0].to_i == 0
1770+
cmd_sess_help
1771+
else
1772+
cmd_sessions('-i', args[0])
1773+
end
1774+
end
1775+
17561776
def cmd_sessions_help
17571777
print_line "Usage: sessions [options]"
17581778
print_line
@@ -1954,22 +1974,26 @@ def cmd_sessions(*args)
19541974
end
19551975
end
19561976
when 'interact'
1957-
session = verify_session(sid)
1958-
if session
1959-
if session.respond_to?(:response_timeout)
1960-
last_known_timeout = session.response_timeout
1961-
session.response_timeout = response_timeout
1962-
end
1963-
print_status("Starting interaction with #{session.name}...\n") unless quiet
1964-
begin
1965-
self.active_session = session
1966-
session.interact(driver.input.dup, driver.output)
1967-
self.active_session = nil
1968-
driver.input.reset_tab_completion if driver.input.supports_readline
1969-
ensure
1970-
if session.respond_to?(:response_timeout) && last_known_timeout
1971-
session.response_timeout = last_known_timeout
1977+
while sid
1978+
session = verify_session(sid)
1979+
if session
1980+
if session.respond_to?(:response_timeout)
1981+
last_known_timeout = session.response_timeout
1982+
session.response_timeout = response_timeout
1983+
end
1984+
print_status("Starting interaction with #{session.name}...\n") unless quiet
1985+
begin
1986+
self.active_session = session
1987+
sid = session.interact(driver.input.dup, driver.output)
1988+
self.active_session = nil
1989+
driver.input.reset_tab_completion if driver.input.supports_readline
1990+
ensure
1991+
if session.respond_to?(:response_timeout) && last_known_timeout
1992+
session.response_timeout = last_known_timeout
1993+
end
19721994
end
1995+
else
1996+
sid = nil
19731997
end
19741998
end
19751999
when 'scriptall'

lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def commands
6565
"bgkill" => "Kills a background meterpreter script",
6666
"get_timeouts" => "Get the current session timeout values",
6767
"set_timeouts" => "Set the current session timeout values",
68+
"sess" => "Quickly switch to another session",
6869
"bglist" => "Lists running background scripts",
6970
"write" => "Writes data to a channel",
7071
"enable_unicode_encoding" => "Enables encoding of unicode strings",
@@ -111,6 +112,28 @@ def name
111112
"Core"
112113
end
113114

115+
def cmd_sess_help
116+
print_line('Usage: sess <session id>')
117+
print_line
118+
print_line('Interact with a different session Id.')
119+
print_line('This works the same as calling this from the MSF shell: sessions -i <session id>')
120+
print_line
121+
end
122+
123+
def cmd_sess(*args)
124+
if args.length == 0 || args[0].to_i == 0
125+
cmd_sess_help
126+
elsif args[0].to_s == client.name.to_s
127+
print_status("Session #{client.name} is already interactive.")
128+
else
129+
print_status("Backgrounding session #{client.name}...")
130+
# store the next session id so that it can be referenced as soon
131+
# as this session is no longer interacting
132+
client.next_session = args[0]
133+
client.interacting = false
134+
end
135+
end
136+
114137
def cmd_background_help
115138
print_line "Usage: background"
116139
print_line

lib/rex/ui/interactive.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ def interact(user_input, user_output)
8383
self.completed = true
8484
end
8585

86-
# Return whether or not EOF was reached
87-
return eof
86+
# if another session was requested, store it
87+
next_session = self.next_session
88+
# clear the value from the object
89+
self.next_session = nil
90+
91+
# return this session id
92+
return next_session
8893
end
8994

9095
#
@@ -104,6 +109,11 @@ def detach
104109
#
105110
attr_accessor :interacting
106111

112+
#
113+
# If another session needs interaction, this is where it goes
114+
#
115+
attr_accessor :next_session
116+
107117
#
108118
# Whether or not the session has completed interaction
109119
#

0 commit comments

Comments
 (0)