@@ -45,7 +45,8 @@ class Core
45
45
"-K" => [ false , "Terminate all sessions" ] ,
46
46
"-s" => [ true , "Run a script on the session given with -i, or all" ] ,
47
47
"-r" => [ false , "Reset the ring buffer for the session given with -i, or all" ] ,
48
- "-u" => [ true , "Upgrade a shell to a meterpreter session on many platforms" ] )
48
+ "-u" => [ true , "Upgrade a shell to a meterpreter session on many platforms" ] ,
49
+ "-t" => [ true , "Set a response timeout (default: 15)" ] )
49
50
50
51
@@jobs_opts = Rex ::Parser ::Arguments . new (
51
52
"-h" => [ false , "Help banner." ] ,
@@ -1222,7 +1223,7 @@ def cmd_route(*args)
1222
1223
Rex ::Socket ::SwitchBoard . flush_routes
1223
1224
1224
1225
when "print"
1225
- tbl = Table . new (
1226
+ tbl = Table . new (
1226
1227
Table ::Style ::Default ,
1227
1228
'Header' => "Active Routing Table" ,
1228
1229
'Prefix' => "\n " ,
@@ -1597,6 +1598,7 @@ def cmd_sessions(*args)
1597
1598
cmds = [ ]
1598
1599
script = nil
1599
1600
reset_ring = false
1601
+ response_timeout = 15
1600
1602
1601
1603
# any arguments that don't correspond to an option or option arg will
1602
1604
# be put in here
@@ -1646,6 +1648,10 @@ def cmd_sessions(*args)
1646
1648
when "-h"
1647
1649
cmd_sessions_help
1648
1650
return false
1651
+ when "-t"
1652
+ if val . to_s =~ /^\d +$/
1653
+ response_timeout = val . to_i
1654
+ end
1649
1655
else
1650
1656
extra << val
1651
1657
end
@@ -1684,30 +1690,39 @@ def cmd_sessions(*args)
1684
1690
session = verify_session ( s )
1685
1691
next unless session
1686
1692
print_status ( "Running '#{ cmd } ' on #{ session . type } session #{ s } (#{ session . session_host } )" )
1693
+ last_known_timeout = session . response_timeout
1694
+ session . response_timeout = response_timeout
1687
1695
1688
- if session . type == 'meterpreter'
1689
- # If session.sys is nil, dont even try..
1690
- unless session . sys
1691
- print_error ( "Session #{ s } does not have stdapi loaded, skipping..." )
1692
- next
1693
- end
1694
- c , c_args = cmd . split ( ' ' , 2 )
1695
- begin
1696
- process = session . sys . process . execute ( c , c_args ,
1697
- {
1698
- 'Channelized' => true ,
1699
- 'Hidden' => true
1700
- } )
1701
- rescue ::Rex ::Post ::Meterpreter ::RequestError
1702
- print_error ( "Failed: #{ $!. class } #{ $!} " )
1703
- end
1704
- if process && process . channel
1705
- data = process . channel . read
1706
- 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
1702
+ end
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
1707
1722
end
1708
- elsif session . type == 'shell'
1709
- output = session . shell_command ( cmd )
1710
- print_line ( output ) if output
1723
+ ensure
1724
+ # Restore timeout for each session
1725
+ session . response_timeout = last_known_timeout
1711
1726
end
1712
1727
# If the session isn't a meterpreter or shell type, it
1713
1728
# could be a VNC session (which can't run commands) or
@@ -1720,8 +1735,14 @@ def cmd_sessions(*args)
1720
1735
session_list . each do |sess_id |
1721
1736
session = framework . sessions . get ( sess_id )
1722
1737
if session
1738
+ last_known_timeout = session . response_timeout
1739
+ session . response_timeout = response_timeout
1723
1740
print_status ( "Killing session #{ sess_id } " )
1724
- session . kill
1741
+ begin
1742
+ session . kill
1743
+ ensure
1744
+ session . response_timeout = last_known_timeout
1745
+ end
1725
1746
else
1726
1747
print_error ( "Invalid session identifier: #{ sess_id } " )
1727
1748
end
@@ -1730,26 +1751,46 @@ def cmd_sessions(*args)
1730
1751
print_status ( "Killing all sessions..." )
1731
1752
framework . sessions . each_sorted do |s |
1732
1753
session = framework . sessions . get ( s )
1733
- session . kill if session
1754
+ if session
1755
+ last_known_timeout = session . response_timeout
1756
+ session . response_timeout = response_timeout
1757
+ begin
1758
+ session . kill
1759
+ ensure
1760
+ session . response_timeout = last_known_timeout
1761
+ end
1762
+ end
1734
1763
end
1735
1764
when 'detach'
1736
1765
print_status ( "Detaching the following session(s): #{ session_list . join ( ', ' ) } " )
1737
1766
session_list . each do |sess_id |
1738
1767
session = verify_session ( sess_id )
1739
1768
# if session is interactive, it's detachable
1740
1769
if session
1770
+ last_known_timeout = session . response_timeout
1771
+ session . response_timeout = response_timeout
1741
1772
print_status ( "Detaching session #{ sess_id } " )
1742
- session . detach
1773
+ begin
1774
+ session . detach
1775
+ ensure
1776
+ session . response_timeout = last_known_timeout
1777
+ end
1743
1778
end
1744
1779
end
1745
1780
when 'interact'
1746
1781
session = verify_session ( sid )
1747
1782
if session
1783
+ last_known_timeout = session . response_timeout
1784
+ session . response_timeout = response_timeout
1748
1785
print_status ( "Starting interaction with #{ session . name } ...\n " ) unless quiet
1749
- self . active_session = session
1750
- session . interact ( driver . input . dup , driver . output )
1751
- self . active_session = nil
1752
- 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
1753
1794
end
1754
1795
when 'scriptall'
1755
1796
unless script
@@ -1770,15 +1811,21 @@ def cmd_sessions(*args)
1770
1811
session = framework . sessions . get ( sess_id )
1771
1812
end
1772
1813
if session
1773
- if script_paths [ session . type ]
1774
- print_status ( "Session #{ sess_id } (#{ session . session_host } ):" )
1775
- print_status ( "Running script #{ script } on #{ session . type } session" +
1776
- " #{ sess_id } (#{ session . session_host } )" )
1777
- begin
1778
- session . execute_file ( script_paths [ session . type ] , extra )
1779
- rescue ::Exception => e
1780
- log_error ( "Error executing script: #{ e . class } #{ e } " )
1814
+ last_known_timeout = session . response_timeout
1815
+ session . response_timeout = response_timeout
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
1781
1826
end
1827
+ ensure
1828
+ session . response_timeout = last_known_timeout
1782
1829
end
1783
1830
else
1784
1831
print_error ( "Invalid session identifier: #{ sess_id } " )
@@ -1790,13 +1837,19 @@ def cmd_sessions(*args)
1790
1837
session_list . each do |sess_id |
1791
1838
session = verify_session ( sess_id )
1792
1839
if session
1793
- if session . type == 'shell'
1794
- session . init_ui ( driver . input , driver . output )
1795
- session . execute_script ( 'post/multi/manage/shell_to_meterpreter' )
1796
- session . reset_ui
1797
- else
1798
- print_error ( "Session #{ sess_id } is not a command shell session, skipping..." )
1799
- next
1840
+ last_known_timeout = session . response_timeout
1841
+ session . response_timeout = response_timeout
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
1800
1853
end
1801
1854
end
1802
1855
0 commit comments