@@ -1690,33 +1690,39 @@ def cmd_sessions(*args)
1690
1690
session = verify_session ( s )
1691
1691
next unless session
1692
1692
print_status ( "Running '#{ cmd } ' on #{ session . type } session #{ s } (#{ session . session_host } )" )
1693
+ last_known_timeout = session . response_timeout
1693
1694
session . response_timeout = response_timeout
1694
1695
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
1711
1702
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
1716
1722
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
1720
1726
end
1721
1727
# If the session isn't a meterpreter or shell type, it
1722
1728
# could be a VNC session (which can't run commands) or
@@ -1729,9 +1735,14 @@ def cmd_sessions(*args)
1729
1735
session_list . each do |sess_id |
1730
1736
session = framework . sessions . get ( sess_id )
1731
1737
if session
1738
+ last_known_timeout = session . response_timeout
1732
1739
session . response_timeout = response_timeout
1733
1740
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
1735
1746
else
1736
1747
print_error ( "Invalid session identifier: #{ sess_id } " )
1737
1748
end
@@ -1741,8 +1752,13 @@ def cmd_sessions(*args)
1741
1752
framework . sessions . each_sorted do |s |
1742
1753
session = framework . sessions . get ( s )
1743
1754
if session
1755
+ last_known_timeout = session . response_timeout
1744
1756
session . response_timeout = response_timeout
1745
- session . kill
1757
+ begin
1758
+ session . kill
1759
+ ensure
1760
+ session . response_timeout = last_known_timeout
1761
+ end
1746
1762
end
1747
1763
end
1748
1764
when 'detach'
@@ -1751,20 +1767,30 @@ def cmd_sessions(*args)
1751
1767
session = verify_session ( sess_id )
1752
1768
# if session is interactive, it's detachable
1753
1769
if session
1770
+ last_known_timeout = session . response_timeout
1754
1771
session . response_timeout = response_timeout
1755
1772
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
1757
1778
end
1758
1779
end
1759
1780
when 'interact'
1760
1781
session = verify_session ( sid )
1761
1782
if session
1783
+ last_known_timeout = session . response_timeout
1762
1784
session . response_timeout = response_timeout
1763
1785
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
1768
1794
end
1769
1795
when 'scriptall'
1770
1796
unless script
@@ -1785,16 +1811,21 @@ def cmd_sessions(*args)
1785
1811
session = framework . sessions . get ( sess_id )
1786
1812
end
1787
1813
if session
1814
+ last_known_timeout = session . response_timeout
1788
1815
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
1797
1826
end
1827
+ ensure
1828
+ session . response_timeout = last_known_timeout
1798
1829
end
1799
1830
else
1800
1831
print_error ( "Invalid session identifier: #{ sess_id } " )
@@ -1806,14 +1837,19 @@ def cmd_sessions(*args)
1806
1837
session_list . each do |sess_id |
1807
1838
session = verify_session ( sess_id )
1808
1839
if session
1840
+ last_known_timeout = session . response_timeout
1809
1841
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
1817
1853
end
1818
1854
end
1819
1855
0 commit comments