metasploit-framework/modules/auxiliary/scanner/portscan/tcp.rb

117 lines
3.3 KiB
Ruby
Raw Normal View History

##
# This module requires Metasploit: http://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
2016-03-08 13:02:44 +00:00
class MetasploitModule < Msf::Auxiliary
2013-08-30 21:28:54 +00:00
include Msf::Exploit::Remote::Tcp
include Msf::Auxiliary::Report
include Msf::Auxiliary::Scanner
def initialize
super(
'Name' => 'TCP Port Scanner',
2015-12-05 15:33:04 +00:00
'Description' => %q{
Enumerate open TCP services by performing a full TCP connect on each port.
2015-12-05 15:12:40 +00:00
This does not need administrative privileges on the source machine, which
may be useful if pivoting.
},
2013-08-30 21:28:54 +00:00
'Author' => [ 'hdm', 'kris katterjohn' ],
'License' => MSF_LICENSE
)
register_options(
[
OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),
OptInt.new('TIMEOUT', [true, "The socket connect timeout in milliseconds", 1000]),
OptInt.new('CONCURRENCY', [true, "The number of concurrent ports to check per host", 10]),
OptInt.new('DELAY', [true, "The delay between connections, per thread, in milliseconds", 0]),
OptInt.new('JITTER', [true, "The delay jitter factor (maximum value by which to +/- DELAY) in milliseconds.", 0]),
2013-08-30 21:28:54 +00:00
], self.class)
deregister_options('RPORT')
end
def run_host(ip)
timeout = datastore['TIMEOUT'].to_i
ports = Rex::Socket.portspec_crack(datastore['PORTS'])
if ports.empty?
raise Msf::OptionValidateError.new(['PORTS'])
2013-08-30 21:28:54 +00:00
end
jitter_value = datastore['JITTER'].to_i
2015-12-05 15:33:04 +00:00
if jitter_value < 0
raise Msf::OptionValidateError.new(['JITTER'])
end
2015-12-05 15:12:40 +00:00
delay_value = datastore['DELAY'].to_i
2015-12-05 15:33:04 +00:00
if delay_value < 0
2015-12-05 15:12:40 +00:00
raise Msf::OptionValidateError.new(['DELAY'])
end
2013-08-30 21:28:54 +00:00
while(ports.length > 0)
t = []
r = []
begin
1.upto(datastore['CONCURRENCY']) do
this_port = ports.shift
break if not this_port
t << framework.threads.spawn("Module(#{self.refname})-#{ip}:#{this_port}", false, this_port) do |port|
begin
2015-12-05 14:59:33 +00:00
# Add the delay based on JITTER and DELAY if needs be
2015-12-05 15:12:40 +00:00
add_delay_jitter(delay_value,jitter_value)
2015-12-05 14:59:33 +00:00
# Actually perform the TCP connection
2013-08-30 21:28:54 +00:00
s = connect(false,
{
'RPORT' => port,
'RHOST' => ip,
'ConnectTimeout' => (timeout / 1000.0)
}
)
if s
print_status("#{ip}:#{port} - TCP OPEN")
r << [ip,port,"open"]
end
2013-08-30 21:28:54 +00:00
rescue ::Rex::ConnectionRefused
vprint_status("#{ip}:#{port} - TCP closed")
r << [ip,port,"closed"]
rescue ::Rex::ConnectionError, ::IOError, ::Timeout::Error
rescue ::Rex::Post::Meterpreter::RequestError
rescue ::Interrupt
raise $!
rescue ::Exception => e
print_error("#{ip}:#{port} exception #{e.class} #{e} #{e.backtrace}")
ensure
if s
disconnect(s) rescue nil
end
2013-08-30 21:28:54 +00:00
end
end
end
t.each {|x| x.join }
rescue ::Timeout::Error
ensure
t.each {|x| x.kill rescue nil }
end
r.each do |res|
report_service(:host => res[0], :port => res[1], :state => res[2])
end
end
end
end