Path: blob/master/modules/auxiliary/scanner/portscan/syn.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::Capture7include Msf::Auxiliary::Report8include Msf::Auxiliary::Scanner910def initialize11super(12'Name' => 'TCP SYN Port Scanner',13'Description' => %q{14Enumerate open TCP services using a raw SYN scan.15},16'Author' => 'kris katterjohn',17'License' => MSF_LICENSE18)1920register_options([21OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),22OptInt.new('TIMEOUT', [true, "The reply read timeout in milliseconds", 500]),23OptInt.new('BATCHSIZE', [true, "The number of hosts to scan per set", 256]),24OptInt.new('DELAY', [true, "The delay between connections, per thread, in milliseconds", 0]),25OptInt.new('JITTER', [true, "The delay jitter factor (maximum value by which to +/- DELAY) in milliseconds.", 0]),26OptString.new('INTERFACE', [false, 'The name of the interface'])27])2829deregister_options('FILTER', 'PCAPFILE')30end3132# No IPv6 support yet33def support_ipv6?34false35end3637def run_batch_size38datastore['BATCHSIZE'] || 25639end4041def run_batch(hosts)42ports = Rex::Socket.portspec_crack(datastore['PORTS'])43if ports.empty?44raise Msf::OptionValidateError.new(['PORTS'])45end4647jitter_value = datastore['JITTER'].to_i48if jitter_value < 049raise Msf::OptionValidateError.new(['JITTER'])50end5152delay_value = datastore['DELAY'].to_i53if delay_value < 054raise Msf::OptionValidateError.new(['DELAY'])55end5657open_pcap58pcap = self.capture5960to = (datastore['TIMEOUT'] || 500).to_f / 1000.06162# we copy the hosts because some may not be reachable and need to be ejected63host_queue = hosts.dup64# Spread the load across the hosts65ports.each do |dport|66host_queue.each do |dhost|67shost, sport = getsource(dhost)6869self.capture.setfilter(getfilter(shost, sport, dhost, dport))7071# Add the delay based on JITTER and DELAY if needs be72add_delay_jitter(delay_value, jitter_value)7374begin75probe = buildprobe(shost, sport, dhost, dport)7677unless capture_sendto(probe, dhost)78host_queue.delete(dhost)79next80end8182reply = probereply(self.capture, to)8384next if not reply8586if (reply.is_tcp? and reply.tcp_flags.syn == 1 and reply.tcp_flags.ack == 1)87print_good(" TCP OPEN #{dhost}:#{dport}")88report_service(:host => dhost, :port => dport)89end90rescue ::Exception91print_error("Error: #{$!.class} #{$!}")92end93end94end9596close_pcap97end9899def getfilter(shost, sport, dhost, dport)100# Look for associated SYN/ACKs and RSTs101"tcp and (tcp[13] == 0x12 or (tcp[13] & 0x04) != 0) and " +102"src host #{dhost} and src port #{dport} and " +103"dst host #{shost} and dst port #{sport}"104end105106def getsource(dhost)107# srcip, srcport108[ Rex::Socket.source_address(dhost), rand(0xffff - 1025) + 1025 ]109end110111def buildprobe(shost, sport, dhost, dport)112p = PacketFu::TCPPacket.new113p.ip_saddr = shost114p.ip_daddr = dhost115p.tcp_sport = sport116p.tcp_flags.ack = 0117p.tcp_flags.syn = 1118p.tcp_dport = dport119p.tcp_win = 3072120p.recalc121p122end123124def probereply(pcap, to)125reply = nil126begin127Timeout.timeout(to) do128pcap.each do |r|129pkt = PacketFu::Packet.parse(r)130next unless pkt.is_tcp?131132reply = pkt133break134end135end136rescue Timeout::Error137end138return reply139end140end141142143