Skip to content

Commit 36c85b7

Browse files
committed
Add support for jobs -k ranges
1 parent 13b6f1c commit 36c85b7

File tree

1 file changed

+32
-8
lines changed
  • lib/msf/ui/console/command_dispatcher

1 file changed

+32
-8
lines changed

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

Lines changed: 32 additions & 8 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
@@ -3380,6 +3384,26 @@ def build_sessions_array(sid_list)
33803384
return session_list.uniq.sort
33813385
end
33823386

3387+
# 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'
3388+
def build_jobs_array(jid_list)
3389+
job_list = Array.new
3390+
temp_list = jid_list.split(",")
3391+
3392+
temp_list.each do |ele|
3393+
if ele.include? '-'
3394+
temp_array = (ele.split("-").inject {|s,e| s.to_i..e.to_i}).to_a
3395+
job_list.concat(temp_array)
3396+
elsif 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+
else
3400+
job_list.push(ele.to_i)
3401+
end
3402+
end
3403+
3404+
return job_list.uniq.sort
3405+
end
3406+
33833407
end
33843408

33853409

0 commit comments

Comments
 (0)