Path: blob/master/modules/auxiliary/scanner/portscan/tcp.rb
21367 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::Tcp78include Msf::Auxiliary::Report9include Msf::Auxiliary::Scanner1011def initialize12super(13'Name' => 'TCP Port Scanner',14'Description' => %q{15Enumerate open TCP services by performing a full TCP connect on each port.16This does not need administrative privileges on the source machine, which17may be useful if pivoting.18},19'Author' => [ 'hdm', 'kris katterjohn' ],20'License' => MSF_LICENSE21)2223register_options(24[25OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),26OptInt.new('TIMEOUT', [true, "The socket connect timeout in milliseconds", 1000]),27OptInt.new('CONCURRENCY', [true, "The number of concurrent ports to check per host", 10]),28OptInt.new('DELAY', [true, "The delay between connections, per thread, in milliseconds", 0]),29OptInt.new('JITTER', [true, "The delay jitter factor (maximum value by which to +/- DELAY) in milliseconds.", 0]),30]31)3233deregister_options('RPORT')34end3536def run_host(ip)37timeout = datastore['TIMEOUT'].to_i3839ports = Rex::Socket.portspec_crack(datastore['PORTS'])4041if ports.empty?42raise Msf::OptionValidateError.new(['PORTS'])43end4445jitter_value = datastore['JITTER'].to_i46if jitter_value < 047raise Msf::OptionValidateError.new(['JITTER'])48end4950delay_value = datastore['DELAY'].to_i51if delay_value < 052raise Msf::OptionValidateError.new(['DELAY'])53end5455while (ports.length > 0)56t = []57r = []58begin591.upto(datastore['CONCURRENCY']) do60this_port = ports.shift61break if not this_port6263t << framework.threads.spawn("Module(#{self.refname})-#{ip}:#{this_port}", false, this_port) do |port|64begin65# Add the delay based on JITTER and DELAY if needs be66add_delay_jitter(delay_value, jitter_value)6768# Actually perform the TCP connection69s = connect(false,70{71'RPORT' => port,72'RHOST' => ip,73'ConnectTimeout' => (timeout / 1000.0)74})75if s76print_good("#{ip}:#{port} - TCP OPEN")77r << [ip, port, "open"]78end79rescue ::Rex::ConnectionRefused80vprint_status("#{ip}:#{port} - TCP closed")81rescue ::Rex::ConnectionError, ::IOError, ::Timeout::Error82rescue ::Rex::Post::Meterpreter::RequestError83rescue ::Interrupt84raise $!85rescue ::Exception => e86print_error("#{ip}:#{port} exception #{e.class} #{e} #{e.backtrace}")87ensure88if s89disconnect(s) rescue nil90end91end92end93end94t.each { |x| x.join }95rescue ::Timeout::Error96ensure97t.each { |x| x.kill rescue nil }98end99100r.each do |res|101report_service(:host => res[0], :port => res[1], :state => res[2])102end103end104end105end106107108