Path: blob/master/modules/auxiliary/scanner/portscan/ack.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::Scanner8include Msf::Auxiliary::Report910def initialize11super(12'Name' => 'TCP ACK Firewall Scanner',13'Description' => %q{14Map out firewall rulesets with a raw ACK scan. Any15unfiltered ports found means a stateful firewall is16not in place for them.17},18'Author' => 'kris katterjohn',19'License' => MSF_LICENSE20)2122register_options([23OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),24OptInt.new('TIMEOUT', [true, "The reply read timeout in milliseconds", 500]),25OptInt.new('BATCHSIZE', [true, "The number of hosts to scan per set", 256]),26OptInt.new('DELAY', [true, "The delay between connections, per thread, in milliseconds", 0]),27OptInt.new('JITTER', [true, "The delay jitter factor (maximum value by which to +/- DELAY) in milliseconds.", 0]),28OptString.new('INTERFACE', [false, 'The name of the interface'])29])3031deregister_options('FILTER', 'PCAPFILE')32end3334# No IPv6 support yet35def support_ipv6?36false37end3839def run_batch_size40datastore['BATCHSIZE'] || 25641end4243def run_batch(hosts)44ports = Rex::Socket.portspec_crack(datastore['PORTS'])45if ports.empty?46raise Msf::OptionValidateError.new(['PORTS'])47end4849jitter_value = datastore['JITTER'].to_i50if jitter_value < 051raise Msf::OptionValidateError.new(['JITTER'])52end5354delay_value = datastore['DELAY'].to_i55if delay_value < 056raise Msf::OptionValidateError.new(['DELAY'])57end5859open_pcap6061pcap = self.capture6263to = (datastore['TIMEOUT'] || 500).to_f / 1000.06465# we copy the hosts because some may not be reachable and need to be ejected66host_queue = hosts.dup67# Spread the load across the hosts68ports.each do |dport|69host_queue.each do |dhost|70shost, sport = getsource(dhost)7172pcap.setfilter(getfilter(shost, sport, dhost, dport))7374# Add the delay based on JITTER and DELAY if needs be75add_delay_jitter(delay_value, jitter_value)7677begin78probe = buildprobe(shost, sport, dhost, dport)7980unless capture_sendto(probe, dhost)81host_queue.delete(dhost)82next83end8485reply = probereply(pcap, to)8687next if not reply8889print_status(" TCP UNFILTERED #{dhost}:#{dport}")9091# Add Report92report_note(93:host => dhost,94:proto => 'tcp',95:port => dport,96:type => "TCP UNFILTERED #{dhost}:#{dport}",97:data => {98:host => dhost,99:port => dport100}101)102rescue ::Exception103print_error("Error: #{$!.class} #{$!}")104end105end106end107108close_pcap109end110111def getfilter(shost, sport, dhost, dport)112# Look for associated RSTs113"tcp and (tcp[13] & 0x04) != 0 and " +114"src host #{dhost} and src port #{dport} and " +115"dst host #{shost} and dst port #{sport}"116end117118def getsource(dhost)119# srcip, srcport120[ Rex::Socket.source_address(dhost), rand(0xffff - 1025) + 1025 ]121end122123def buildprobe(shost, sport, dhost, dport)124p = PacketFu::TCPPacket.new125p.ip_saddr = shost126p.ip_daddr = dhost127p.tcp_sport = sport128p.tcp_ack = rand(0x100000000)129p.tcp_flags.ack = 1130p.tcp_dport = dport131p.tcp_win = 3072132p.recalc133p134end135136def probereply(pcap, to)137reply = nil138begin139Timeout.timeout(to) do140pcap.each do |r|141pkt = PacketFu::Packet.parse(r)142next unless pkt.is_tcp?143144reply = pkt145break146end147end148rescue Timeout::Error149end150return reply151end152end153154155