Skip to content

Commit 88d093e

Browse files
committed
Merge pull request #1 from wvu-r7/pr/4063
Add support for jobs -k ranges from @wvu
2 parents 13b6f1c + 5547890 commit 88d093e

File tree

1 file changed

+41
-14
lines changed
  • lib/msf/ui/console/command_dispatcher

1 file changed

+41
-14
lines changed

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

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Core
4949

5050
@@jobs_opts = Rex::Parser::Arguments.new(
5151
"-h" => [ false, "Help banner." ],
52-
"-k" => [ true, "Terminate the specified job name." ],
52+
"-k" => [ true, "Terminate jobs by job ID and/or range." ],
5353
"-K" => [ false, "Terminate all running jobs." ],
5454
"-i" => [ true, "Lists detailed information about a running job."],
5555
"-l" => [ false, "List all running jobs." ],
@@ -795,13 +795,17 @@ def cmd_jobs(*args)
795795
when "-l"
796796
dump_list = true
797797

798-
# Terminate the supplied job name
798+
# Terminate the supplied job ID(s)
799799
when "-k"
800-
if (not framework.jobs.has_key?(val))
801-
print_error("No such job")
802-
else
803-
print_line("Stopping job: #{val}...")
804-
framework.jobs.stop_job(val)
800+
job_list = build_jobs_array(val)
801+
print_status("Killing the following job(s): #{job_list.join(', ')}")
802+
job_list.map(&:to_s).each do |job|
803+
if framework.jobs.has_key?(job)
804+
print_status("Killing job #{job}")
805+
framework.jobs.stop_job(job)
806+
else
807+
print_error("Invalid job identifier: #{job}")
808+
end
805809
end
806810
when "-K"
807811
print_line("Stopping all jobs...")
@@ -1709,7 +1713,7 @@ def cmd_sessions(*args)
17091713

17101714
when 'kill'
17111715
session_list = build_sessions_array(sid)
1712-
print_status("Killing the following session(s): #{session_list}")
1716+
print_status("Killing the following session(s): #{session_list.join(', ')}")
17131717
session_list.each do |sess|
17141718
session = framework.sessions.get(sess)
17151719
if session
@@ -1729,13 +1733,16 @@ def cmd_sessions(*args)
17291733
end
17301734

17311735
when 'detach'
1732-
if ((session = framework.sessions.get(sid)))
1733-
print_status("Detaching session #{sid}")
1734-
if (session.interactive?)
1735-
session.detach()
1736+
session_list = build_sessions_array(sid)
1737+
print_status("Detaching the following session(s): #{session_list.join(', ')}")
1738+
session_list.each do |sess|
1739+
session = framework.sessions.get(sess)
1740+
if session
1741+
print_status("Detaching session #{sess}")
1742+
session.detach
1743+
else
1744+
print_error("Invalid session identifier: #{sess}")
17361745
end
1737-
else
1738-
print_error("Invalid session identifier: #{sid}")
17391746
end
17401747

17411748
when 'interact'
@@ -3380,6 +3387,26 @@ def build_sessions_array(sid_list)
33803387
return session_list.uniq.sort
33813388
end
33823389

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|
3396+
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)
3399+
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)
3402+
else
3403+
job_list.push(ele.to_i)
3404+
end
3405+
end
3406+
3407+
return job_list.uniq.sort
3408+
end
3409+
33833410
end
33843411

33853412

0 commit comments

Comments
 (0)