Land #15492, Align support for negative session ids

This commit is contained in:
adfoster-r7 2021-07-30 11:45:22 +01:00 committed by GitHub
commit 18aef5e4db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 17 deletions

View File

@ -262,10 +262,9 @@ class SessionManager < Hash
session = nil
sid = sid.to_i
if sid > 0
session = self[sid]
elsif sid == -1
sid = self.keys.max
if sid < 0
session = self[self.keys.sort[sid]]
elsif sid > 0
session = self[sid]
end
@ -292,4 +291,3 @@ protected
end
end

View File

@ -93,21 +93,25 @@ module CommandDispatcher
#
# Generate an array of job or session IDs from a given range String.
# Always returns an Array.
# Always returns an Array unless an incorrect input is given.
# In that case, the result will always be nil, even if only one argument is incorrect.
#
# @param id_list [String] Range or list description such as 1-5 or 1,3,5 etc
# @return [Array<String>] Representing the range
# @return [Array<String>, nil] Representing the range
def build_range_array(id_list)
item_list = []
unless id_list.blank?
temp_list = id_list.split(',')
temp_list.each do |ele|
return if ele.count('-') > 1
return if ele.first == '-' || ele[-1] == '-'
return if ele.first == '.' || ele[-1] == '.'
return unless ele =~ (/^\d+((\.\.|-)\d+)?$/) # Not a number or range
return if ele.count('-') > 1 # Eg. 'sessions -u -1-,5', incorrect syntax
return if ele.last == '-' # Last item of array is a '-', resulting in an incomplete range
return if ele.first == '.' || ele.last == '.' #Eg. 'sessions -u .1..' or 'sessions -u ..
return unless ele =~ (/^\d+((\.\.|-)\d+)?$/) || ele =~ (/^-?\d+$/) # Not a number or range
if ele.include? '-'
# Check if the item is negative, as this will not always be a range
if ele =~ (/^-?\d+$/) && ele.to_i < 0 # if ele is a single negative number
item_list.push(ele.to_i)
elsif ele.include? '-'
temp_array = (ele.split("-").inject { |s, e| s.to_i..e.to_i }).to_a
item_list.concat(temp_array)
elsif ele.include? '..'

View File

@ -146,6 +146,10 @@ module Msf
job_id = val
when "-p"
job_list = build_range_array(val)
if job_list.blank?
print_error('Please specify valid job identifier(s)')
return
end
job_list.each do |job_id|
add_persist_job(job_id)
end
@ -223,12 +227,13 @@ module Msf
end
# Stop the job by job id.
job_list.map(&:to_s).each do |job|
if framework.jobs.key?(job)
print_status("Stopping job #{job}")
framework.jobs.stop_job(job)
job_list.map(&:to_s).each do |job_id|
job_id = job_id.to_i < 0 ? framework.jobs.keys[job_id.to_i] : job_id
if framework.jobs.key?(job_id)
print_status("Stopping job #{job_id}")
framework.jobs.stop_job(job_id)
else
print_error("Invalid job identifier: #{job}")
print_error("Invalid job identifier: #{job_id}")
end
end
end

View File

@ -0,0 +1,45 @@
require 'rspec'
RSpec.describe Msf::Ui::Console::CommandDispatcher do
include_context 'Msf::DBManager'
include_context 'Msf::UIDriver'
let(:subject) do
dummy_class = Class.new
dummy_class.include described_class
dummy_class.new(driver)
end
describe '#build_range_array' do
[
{ input: '1', expected: [1] },
{ input: '123', expected: [123] },
{ input: '-1', expected: [-1] },
{ input: '-123', expected: [-123] },
{ input: '1,2', expected: [1, 2] },
{ input: '-1,2', expected: [-1, 2] },
{ input: '2,-1', expected: [-1, 2] },
{ input: '-1,-2', expected: [-2, -1] },
{ input: '-1-', expected: nil },
{ input: '-1-,2', expected: nil },
{ input: '-1--,2', expected: nil },
{ input: '---1', expected: nil },
{ input: '1--', expected: nil },
{ input: '1-3', expected: [1, 2, 3] },
{ input: '-1-3', expected: nil },
{ input: '-1--4', expected: nil },
{ input: '1..4', expected: [1, 2, 3, 4] },
{ input: '1..-4', expected: nil },
{ input: '-1..4', expected: nil },
{ input: '-1..-4', expected: nil },
{ input: '-1,0-3', expected: [-1, 0, 1, 2, 3] },
{ input: '-1,0,1,2', expected: [-1, 0, 1, 2] },
{ input: '-1,-1', expected: [-1] },
{ input: '-1,1..2', expected: [-1, 1, 2] }
].each do |test|
it "returns #{test[:expected].inspect} for the input #{test[:input]}" do
expect(subject.build_range_array(test[:input])).to eq(test[:expected])
end
end
end
end