diff --git a/lib/msf/ui/console/command_dispatcher/db.rb b/lib/msf/ui/console/command_dispatcher/db.rb index 1438936c1e..f1c921f60e 100644 --- a/lib/msf/ui/console/command_dispatcher/db.rb +++ b/lib/msf/ui/console/command_dispatcher/db.rb @@ -112,29 +112,6 @@ module Db end - def parse_port_range(desc) - res = [] - desc.split(",").each do |r| - s,e = r.split("-") - e ||= s - s = s.to_i - e = e.to_i - - if(e < s) - t = s - s = e - e = t - end - - s.to_i.upto(e.to_i) do |i| - next if i == 0 - res << i - end - end - - res - end - # # A shotgun approach to network-wide exploitation # @@ -181,9 +158,9 @@ module Db when '-X' targ_exc << OptAddressRange.new('TEMPRANGE', [ true, '' ]).normalize(args.shift) when '-PI' - port_inc = parse_port_range(args.shift) + port_inc = Rex::Socket.portspec_crack(args.shift) when '-PX' - port_exc = parse_port_range(args.shift) + port_exc = Rex::Socket.portspec_crack(args.shift) when '-m' regx = args.shift when '-h' diff --git a/lib/rex/socket.rb b/lib/rex/socket.rb index d2de699737..3bcf1ab192 100644 --- a/lib/rex/socket.rb +++ b/lib/rex/socket.rb @@ -351,6 +351,29 @@ module Socket [ (~((2 ** (32 - bitmask)) - 1)) & 0xffffffff ].pack('N').unpack('CCCC').join('.') end + # + # Converts a port specification like "80,21-23,443" into a sorted, + # unique array of valid port numbers like [21,22,23,80,443] + # + def self.portspec_crack(pspec) + ports = [] + + # Build ports array from port specification + pspec.split(/,/).each do |item| + start, stop = item.split(/-/).map { |p| p.to_i } + + start ||= 0 + stop ||= item.match(/-/) ? 65535 : start + + start, stop = stop, start if stop < start + + start.upto(stop) { |p| ports << p } + end + + # Sort, and remove dups and invalid ports + ports.sort.uniq.delete_if { |p| p < 0 or p > 65535 } + end + ## # # Utility class methods diff --git a/modules/auxiliary/scanner/portscan/tcp.rb b/modules/auxiliary/scanner/portscan/tcp.rb index 401226bc40..c8c09cdbb6 100644 --- a/modules/auxiliary/scanner/portscan/tcp.rb +++ b/modules/auxiliary/scanner/portscan/tcp.rb @@ -12,7 +12,6 @@ require 'msf/core' - class Metasploit3 < Msf::Auxiliary include Msf::Exploit::Remote::Tcp @@ -44,22 +43,8 @@ class Metasploit3 < Msf::Auxiliary def run_host(ip) timeout = datastore['TIMEOUT'].to_i - ports = [] - # Build ports array from port specification - datastore['PORTS'].split(/,/).each do |item| - start, stop = item.split(/-/).map { |p| p.to_i } - - start ||= 0 - stop ||= item.match(/-/) ? 65535 : start - - start, stop = stop, start if stop < start - - start.upto(stop) { |p| ports << p } - end - - # Sort, and remove dups and invalid ports - ports = ports.sort.uniq.delete_if { |p| p < 0 or p > 65535 } + ports = Rex::Socket.portspec_crack(datastore['PORTS']) if ports.empty? print_status("Error: No valid ports specified")