Skip to content

Commit bcf6597

Browse files
committed
Restore original timeout
1 parent ad8bbf4 commit bcf6597

File tree

1 file changed

+81
-45
lines changed
  • lib/msf/ui/console/command_dispatcher

1 file changed

+81
-45
lines changed

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

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,33 +1690,39 @@ def cmd_sessions(*args)
16901690
session = verify_session(s)
16911691
next unless session
16921692
print_status("Running '#{cmd}' on #{session.type} session #{s} (#{session.session_host})")
1693+
last_known_timeout = session.response_timeout
16931694
session.response_timeout = response_timeout
16941695

1695-
if session.type == 'meterpreter'
1696-
# If session.sys is nil, dont even try..
1697-
unless session.sys
1698-
print_error("Session #{s} does not have stdapi loaded, skipping...")
1699-
next
1700-
end
1701-
c, c_args = cmd.split(' ', 2)
1702-
begin
1703-
process = session.sys.process.execute(c, c_args,
1704-
{
1705-
'Channelized' => true,
1706-
'Hidden' => true
1707-
})
1708-
if process && process.channel
1709-
data = process.channel.read
1710-
print_line(data) if data
1696+
begin
1697+
if session.type == 'meterpreter'
1698+
# If session.sys is nil, dont even try..
1699+
unless session.sys
1700+
print_error("Session #{s} does not have stdapi loaded, skipping...")
1701+
next
17111702
end
1712-
rescue ::Rex::Post::Meterpreter::RequestError
1713-
print_error("Failed: #{$!.class} #{$!}")
1714-
rescue Rex::TimeoutError
1715-
print_error("Operation timed out")
1703+
c, c_args = cmd.split(' ', 2)
1704+
begin
1705+
process = session.sys.process.execute(c, c_args,
1706+
{
1707+
'Channelized' => true,
1708+
'Hidden' => true
1709+
})
1710+
if process && process.channel
1711+
data = process.channel.read
1712+
print_line(data) if data
1713+
end
1714+
rescue ::Rex::Post::Meterpreter::RequestError
1715+
print_error("Failed: #{$!.class} #{$!}")
1716+
rescue Rex::TimeoutError
1717+
print_error("Operation timed out")
1718+
end
1719+
elsif session.type == 'shell'
1720+
output = session.shell_command(cmd)
1721+
print_line(output) if output
17161722
end
1717-
elsif session.type == 'shell'
1718-
output = session.shell_command(cmd)
1719-
print_line(output) if output
1723+
ensure
1724+
# Restore timeout for each session
1725+
session.response_timeout = last_known_timeout
17201726
end
17211727
# If the session isn't a meterpreter or shell type, it
17221728
# could be a VNC session (which can't run commands) or
@@ -1729,9 +1735,14 @@ def cmd_sessions(*args)
17291735
session_list.each do |sess_id|
17301736
session = framework.sessions.get(sess_id)
17311737
if session
1738+
last_known_timeout = session.response_timeout
17321739
session.response_timeout = response_timeout
17331740
print_status("Killing session #{sess_id}")
1734-
session.kill
1741+
begin
1742+
session.kill
1743+
ensure
1744+
session.response_timeout = last_known_timeout
1745+
end
17351746
else
17361747
print_error("Invalid session identifier: #{sess_id}")
17371748
end
@@ -1741,8 +1752,13 @@ def cmd_sessions(*args)
17411752
framework.sessions.each_sorted do |s|
17421753
session = framework.sessions.get(s)
17431754
if session
1755+
last_known_timeout = session.response_timeout
17441756
session.response_timeout = response_timeout
1745-
session.kill
1757+
begin
1758+
session.kill
1759+
ensure
1760+
session.response_timeout = last_known_timeout
1761+
end
17461762
end
17471763
end
17481764
when 'detach'
@@ -1751,20 +1767,30 @@ def cmd_sessions(*args)
17511767
session = verify_session(sess_id)
17521768
# if session is interactive, it's detachable
17531769
if session
1770+
last_known_timeout = session.response_timeout
17541771
session.response_timeout = response_timeout
17551772
print_status("Detaching session #{sess_id}")
1756-
session.detach
1773+
begin
1774+
session.detach
1775+
ensure
1776+
session.response_timeout = last_known_timeout
1777+
end
17571778
end
17581779
end
17591780
when 'interact'
17601781
session = verify_session(sid)
17611782
if session
1783+
last_known_timeout = session.response_timeout
17621784
session.response_timeout = response_timeout
17631785
print_status("Starting interaction with #{session.name}...\n") unless quiet
1764-
self.active_session = session
1765-
session.interact(driver.input.dup, driver.output)
1766-
self.active_session = nil
1767-
driver.input.reset_tab_completion if driver.input.supports_readline
1786+
begin
1787+
self.active_session = session
1788+
session.interact(driver.input.dup, driver.output)
1789+
self.active_session = nil
1790+
driver.input.reset_tab_completion if driver.input.supports_readline
1791+
ensure
1792+
session.response_timeout = last_known_timeout
1793+
end
17681794
end
17691795
when 'scriptall'
17701796
unless script
@@ -1785,16 +1811,21 @@ def cmd_sessions(*args)
17851811
session = framework.sessions.get(sess_id)
17861812
end
17871813
if session
1814+
last_known_timeout = session.response_timeout
17881815
session.response_timeout = response_timeout
1789-
if script_paths[session.type]
1790-
print_status("Session #{sess_id} (#{session.session_host}):")
1791-
print_status("Running script #{script} on #{session.type} session" +
1792-
" #{sess_id} (#{session.session_host})")
1793-
begin
1794-
session.execute_file(script_paths[session.type], extra)
1795-
rescue ::Exception => e
1796-
log_error("Error executing script: #{e.class} #{e}")
1816+
begin
1817+
if script_paths[session.type]
1818+
print_status("Session #{sess_id} (#{session.session_host}):")
1819+
print_status("Running script #{script} on #{session.type} session" +
1820+
" #{sess_id} (#{session.session_host})")
1821+
begin
1822+
session.execute_file(script_paths[session.type], extra)
1823+
rescue ::Exception => e
1824+
log_error("Error executing script: #{e.class} #{e}")
1825+
end
17971826
end
1827+
ensure
1828+
session.response_timeout = last_known_timeout
17981829
end
17991830
else
18001831
print_error("Invalid session identifier: #{sess_id}")
@@ -1806,14 +1837,19 @@ def cmd_sessions(*args)
18061837
session_list.each do |sess_id|
18071838
session = verify_session(sess_id)
18081839
if session
1840+
last_known_timeout = session.response_timeout
18091841
session.response_timeout = response_timeout
1810-
if session.type == 'shell'
1811-
session.init_ui(driver.input, driver.output)
1812-
session.execute_script('post/multi/manage/shell_to_meterpreter')
1813-
session.reset_ui
1814-
else
1815-
print_error("Session #{sess_id} is not a command shell session, skipping...")
1816-
next
1842+
begin
1843+
if session.type == 'shell'
1844+
session.init_ui(driver.input, driver.output)
1845+
session.execute_script('post/multi/manage/shell_to_meterpreter')
1846+
session.reset_ui
1847+
else
1848+
print_error("Session #{sess_id} is not a command shell session, skipping...")
1849+
next
1850+
end
1851+
ensure
1852+
session.response_timeout = last_known_timeout
18171853
end
18181854
end
18191855

0 commit comments

Comments
 (0)