Skip to content

Commit 0b8b049

Browse files
committed
- Added range support to sessions -c and sessions -s
- Added check for un-detach-able sessions - Added back the check for session.interactive? when detaching sessions - Collapse build_jobs_array and build_sessions_array to build_range_array - Added check for empty or invalid parameters to detach and kill [session | job] - Reworked session id sanity check around line 1660 - RuboCop/Style guide change: Array.new -> [] - Misc RuboCop/Style guide spacing changes
1 parent c6519d0 commit 0b8b049

File tree

1 file changed

+50
-61
lines changed
  • lib/msf/ui/console/command_dispatcher

1 file changed

+50
-61
lines changed

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

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,11 @@ def cmd_jobs(*args)
797797

798798
# Terminate the supplied job ID(s)
799799
when "-k"
800-
job_list = build_jobs_array(val)
800+
job_list = build_range_array(val)
801+
if job_list.blank?
802+
print_error("Please specify valid job identifier(s)")
803+
return false
804+
end
801805
print_status("Stopping the following job(s): #{job_list.join(', ')}")
802806
job_list.map(&:to_s).each do |job|
803807
if framework.jobs.has_key?(job)
@@ -1615,10 +1619,6 @@ def cmd_sessions(*args)
16151619
when "-k"
16161620
method = 'kill'
16171621
sid = val if val
1618-
if not sid
1619-
print_error("Specify a session to kill")
1620-
return false
1621-
end
16221622

16231623
when "-K"
16241624
method = 'killall'
@@ -1653,15 +1653,18 @@ def cmd_sessions(*args)
16531653
end
16541654
}
16551655

1656-
if sid and not framework.sessions.get(sid)
1657-
print_error("Invalid session id")
1658-
return false
1659-
end
1660-
16611656
if method.nil? and sid
16621657
method = 'interact'
16631658
end
16641659

1660+
unless sid.blank? || method == 'interact'
1661+
session_list = build_range_array(sid)
1662+
if session_list.blank?
1663+
print_error("Please specify valid session identifier(s)")
1664+
return false
1665+
end
1666+
end
1667+
16651668
# Now, perform the actual method
16661669
case method
16671670

@@ -1672,7 +1675,7 @@ def cmd_sessions(*args)
16721675
end
16731676
cmds.each do |cmd|
16741677
if sid
1675-
sessions = [ sid ]
1678+
sessions = session_list
16761679
else
16771680
sessions = framework.sessions.keys.sort
16781681
end
@@ -1712,7 +1715,6 @@ def cmd_sessions(*args)
17121715
end
17131716

17141717
when 'kill'
1715-
session_list = build_sessions_array(sid)
17161718
print_status("Killing the following session(s): #{session_list.join(', ')}")
17171719
session_list.each do |sess|
17181720
session = framework.sessions.get(sess)
@@ -1727,27 +1729,30 @@ def cmd_sessions(*args)
17271729
when 'killall'
17281730
print_status("Killing all sessions...")
17291731
framework.sessions.each_sorted do |s|
1730-
if ((session = framework.sessions.get(s)))
1731-
session.kill
1732-
end
1732+
session = framework.sessions.get(s)
1733+
session.kill if session
17331734
end
17341735

17351736
when 'detach'
1736-
session_list = build_sessions_array(sid)
17371737
print_status("Detaching the following session(s): #{session_list.join(', ')}")
17381738
session_list.each do |sess|
17391739
session = framework.sessions.get(sess)
1740-
if session
1740+
if session && session.interactive?
17411741
print_status("Detaching session #{sess}")
1742-
session.detach
1742+
begin
1743+
session.detach
1744+
rescue NoMethodError
1745+
print_error "#{sess} is not detachable"
1746+
end
17431747
else
17441748
print_error("Invalid session identifier: #{sess}")
17451749
end
17461750
end
17471751

17481752
when 'interact'
1749-
if ((session = framework.sessions.get(sid)))
1750-
if (session.interactive?)
1753+
session = framework.sessions.get(sid)
1754+
if session
1755+
if session.interactive?
17511756
print_status("Starting interaction with #{session.name}...\n") if (quiet == false)
17521757

17531758
self.active_session = session
@@ -1756,7 +1761,7 @@ def cmd_sessions(*args)
17561761

17571762
self.active_session = nil
17581763

1759-
if (driver.input.supports_readline)
1764+
if driver.input.supports_readline
17601765
driver.input.reset_tab_completion
17611766
end
17621767

@@ -1768,7 +1773,7 @@ def cmd_sessions(*args)
17681773
end
17691774

17701775
when 'scriptall'
1771-
if (script.nil?)
1776+
if script.nil?
17721777
print_error("No script specified!")
17731778
return false
17741779
end
@@ -1778,17 +1783,16 @@ def cmd_sessions(*args)
17781783
script_paths['shell'] = Msf::Sessions::CommandShell.find_script_path(script)
17791784

17801785
if sid
1781-
print_status("Running script #{script} on session #{sid}...")
1782-
sessions = [ sid ]
1786+
sessions = session_list
17831787
else
1784-
print_status("Running script #{script} on all sessions...")
17851788
sessions = framework.sessions.keys.sort
17861789
end
1787-
17881790
sessions.each do |s|
1789-
if ((session = framework.sessions.get(s)))
1790-
if (script_paths[session.type])
1791+
session = framework.sessions.get(s)
1792+
if session
1793+
if script_paths[session.type]
17911794
print_status("Session #{s} (#{session.session_host}):")
1795+
print_status("Running script #{script} on #{session.type} session #{s} (#{session.session_host})")
17921796
begin
17931797
session.execute_file(script_paths[session.type], extra)
17941798
rescue ::Exception => e
@@ -1799,12 +1803,12 @@ def cmd_sessions(*args)
17991803
end
18001804

18011805
when 'upexec'
1802-
session_list = build_sessions_array(sid)
18031806
print_status("Executing 'post/multi/manage/shell_to_meterpreter' on session(s): #{session_list}")
18041807
session_list.each do |sess|
1805-
if ((session = framework.sessions.get(sess)))
1806-
if (session.interactive?)
1807-
if (session.type == "shell")
1808+
session = framework.sessions.get(sess)
1809+
if session
1810+
if session.interactive?
1811+
if session.type == "shell"
18081812
session.init_ui(driver.input, driver.output)
18091813
session.execute_script('post/multi/manage/shell_to_meterpreter')
18101814
session.reset_ui
@@ -3367,44 +3371,29 @@ def retrieve_grep_lines(all_lines,line_num, before = nil, after = nil)
33673371
return all_lines.slice(start..finish)
33683372
end
33693373

3370-
# Generate an array of session IDs when presented with input such as '1' or '1,2,4-6,10' or '1,2,4..6,10'
3371-
def build_sessions_array(sid_list)
3372-
session_list = Array.new
3373-
temp_list = sid_list.split(",")
3374+
# Generate an array of job or session IDs when presented with input such as '1' or '1,2,4-6,10' or '1,2,4..6,10'
3375+
def build_range_array(id_list)
3376+
return if id_list.blank?
3377+
item_list = []
3378+
temp_list = id_list.split(",")
33743379

33753380
temp_list.each do |ele|
3376-
if ele.include? '-'
3377-
temp_array = (ele.split("-").inject {|s,e| s.to_i..e.to_i}).to_a
3378-
session_list.concat(temp_array)
3379-
elsif ele.include? '..'
3380-
temp_array = (ele.split("..").inject {|s,e| s.to_i..e.to_i}).to_a
3381-
session_list.concat(temp_array)
3382-
else
3383-
session_list.push(ele.to_i)
3384-
end
3385-
end
3381+
return if ele.count('-') > 1
3382+
return if ele[0] == '-' || ele[-1] == '-'
3383+
return if ele[0] == '.' || ele[-1] == '.'
33863384

3387-
return session_list.uniq.sort
3388-
end
3389-
3390-
# Generate an array of job IDs when presented with input such as '1' or '1,2,4-6,10' or '1,2,4..6,10'
3391-
def build_jobs_array(jid_list)
3392-
job_list = Array.new
3393-
temp_list = jid_list.split(",")
3394-
3395-
temp_list.each do |ele|
33963385
if ele.include? '-'
3397-
temp_array = (ele.split("-").inject {|s,e| s.to_i..e.to_i}).to_a
3398-
job_list.concat(temp_array)
3386+
temp_array = (ele.split("-").inject { |s, e| s.to_i..e.to_i }).to_a
3387+
item_list.concat(temp_array)
33993388
elsif ele.include? '..'
3400-
temp_array = (ele.split("..").inject {|s,e| s.to_i..e.to_i}).to_a
3401-
job_list.concat(temp_array)
3389+
temp_array = (ele.split("..").inject { |s, e| s.to_i..e.to_i }).to_a
3390+
item_list.concat(temp_array)
34023391
else
3403-
job_list.push(ele.to_i)
3392+
item_list.push(ele.to_i)
34043393
end
34053394
end
34063395

3407-
return job_list.uniq.sort
3396+
item_list.uniq.sort
34083397
end
34093398

34103399
end

0 commit comments

Comments
 (0)