2009-03-26 14:55:53 +00:00
|
|
|
##
|
|
|
|
# $Id$
|
|
|
|
##
|
|
|
|
|
|
|
|
##
|
2009-11-02 18:16:51 +00:00
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
2009-03-26 14:55:53 +00:00
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# Framework web site for more information on licensing and terms of use.
|
2009-04-13 14:33:26 +00:00
|
|
|
# http://metasploit.com/framework/
|
2009-03-26 14:55:53 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
2009-07-17 20:36:40 +00:00
|
|
|
require 'racket'
|
2009-03-26 14:55:53 +00:00
|
|
|
|
|
|
|
class Metasploit3 < Msf::Auxiliary
|
|
|
|
|
2010-01-26 22:16:03 +00:00
|
|
|
include Msf::Exploit::Capture
|
2009-03-26 14:55:53 +00:00
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
include Msf::Auxiliary::Scanner
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'TCP SYN Port Scanner',
|
|
|
|
'Description' => %q{
|
|
|
|
Enumerate open TCP services using a raw SYN scan.
|
|
|
|
},
|
2009-04-03 15:05:35 +00:00
|
|
|
'Author' => 'kris katterjohn',
|
2009-03-26 14:55:53 +00:00
|
|
|
'Version' => '$Revision$', # 03/26/2009
|
|
|
|
'License' => MSF_LICENSE
|
|
|
|
)
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'pcaprub'
|
|
|
|
@@havepcap = true
|
|
|
|
rescue ::LoadError
|
|
|
|
@@havepcap = false
|
|
|
|
end
|
|
|
|
|
|
|
|
register_options([
|
|
|
|
OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),
|
|
|
|
OptInt.new('TIMEOUT', [true, "The reply read timeout in milliseconds", 500]),
|
2009-09-24 03:34:04 +00:00
|
|
|
OptInt.new('BATCHSIZE', [true, "The number of hosts to scan per set", 256]),
|
|
|
|
OptString.new('INTERFACE', [false, 'The name of the interface'])
|
2009-03-26 14:55:53 +00:00
|
|
|
], self.class)
|
2010-01-26 22:16:03 +00:00
|
|
|
|
|
|
|
deregister_options('FILTER','PCAPFILE')
|
2009-03-26 14:55:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_batch_size
|
|
|
|
datastore['BATCHSIZE'] || 256
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_batch(hosts)
|
2010-01-27 18:34:35 +00:00
|
|
|
open_pcap
|
2009-03-26 14:55:53 +00:00
|
|
|
|
|
|
|
raise "Pcaprub is not available" if not @@havepcap
|
|
|
|
|
2010-01-27 18:34:35 +00:00
|
|
|
pcap = self.capture
|
2009-03-26 14:55:53 +00:00
|
|
|
|
|
|
|
ports = Rex::Socket.portspec_crack(datastore['PORTS'])
|
|
|
|
|
|
|
|
if ports.empty?
|
|
|
|
print_error("Error: No valid ports specified")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
|
|
|
|
|
|
|
# Spread the load across the hosts
|
|
|
|
ports.each do |dport|
|
|
|
|
hosts.each do |dhost|
|
|
|
|
shost, sport = getsource(dhost)
|
|
|
|
|
2010-01-26 22:16:03 +00:00
|
|
|
self.capture.setfilter(getfilter(shost, sport, dhost, dport))
|
2009-03-26 14:55:53 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
probe = buildprobe(shost, sport, dhost, dport)
|
|
|
|
|
2010-01-27 18:34:35 +00:00
|
|
|
capture_sendto(probe, dhost)
|
2009-03-26 14:55:53 +00:00
|
|
|
|
2010-01-26 22:16:03 +00:00
|
|
|
reply = probereply(self.capture, to)
|
2009-03-26 14:55:53 +00:00
|
|
|
|
|
|
|
next if not reply
|
|
|
|
|
2009-07-17 20:36:40 +00:00
|
|
|
if (reply[:tcp] and reply[:tcp].flag_syn == 1 and reply[:tcp].flag_ack == 1)
|
2009-03-26 14:55:53 +00:00
|
|
|
print_status(" TCP OPEN #{dhost}:#{dport}")
|
|
|
|
report_service(:host => dhost, :port => dport)
|
|
|
|
end
|
|
|
|
rescue ::Exception
|
|
|
|
print_error("Error: #{$!.class} #{$!}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-26 22:16:03 +00:00
|
|
|
close_pcap
|
2009-03-26 14:55:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def getfilter(shost, sport, dhost, dport)
|
|
|
|
# Look for associated SYN/ACKs and RSTs
|
|
|
|
"tcp and (tcp[13] == 0x12 or (tcp[13] & 0x04) != 0) and " +
|
|
|
|
"src host #{dhost} and src port #{dport} and " +
|
|
|
|
"dst host #{shost} and dst port #{sport}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def getsource(dhost)
|
|
|
|
# srcip, srcport
|
|
|
|
[ Rex::Socket.source_address(dhost), rand(0xffff - 1025) + 1025 ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def buildprobe(shost, sport, dhost, dport)
|
2009-07-17 20:36:40 +00:00
|
|
|
n = Racket::Racket.new
|
|
|
|
|
2009-12-29 23:32:50 +00:00
|
|
|
n.l3 = Racket::L3::IPv4.new
|
2009-07-17 20:36:40 +00:00
|
|
|
n.l3.src_ip = shost
|
|
|
|
n.l3.dst_ip = dhost
|
|
|
|
n.l3.protocol = 0x6
|
|
|
|
n.l3.id = rand(0x10000)
|
2009-11-07 03:17:19 +00:00
|
|
|
n.l3.ttl = 255
|
2009-11-02 18:16:51 +00:00
|
|
|
|
2009-12-29 23:32:50 +00:00
|
|
|
n.l4 = Racket::L4::TCP.new
|
2009-07-17 20:36:40 +00:00
|
|
|
n.l4.src_port = sport
|
|
|
|
n.l4.seq = rand(0x100000000)
|
|
|
|
n.l4.ack = 0
|
|
|
|
n.l4.flag_syn = 1
|
|
|
|
n.l4.dst_port = dport
|
|
|
|
n.l4.window = 3072
|
2009-11-02 18:16:51 +00:00
|
|
|
|
|
|
|
n.l4.fix!(n.l3.src_ip, n.l3.dst_ip, "")
|
|
|
|
|
2009-07-17 20:36:40 +00:00
|
|
|
n.pack
|
2009-03-26 14:55:53 +00:00
|
|
|
end
|
|
|
|
|
2010-01-26 22:16:03 +00:00
|
|
|
def probereply(pcap, to)
|
2009-03-26 14:55:53 +00:00
|
|
|
reply = nil
|
|
|
|
|
|
|
|
begin
|
2009-11-02 18:16:51 +00:00
|
|
|
Timeout.timeout(to) do
|
2009-03-26 14:55:53 +00:00
|
|
|
pcap.each do |r|
|
2009-12-29 23:32:50 +00:00
|
|
|
eth = Racket::L2::Ethernet.new(r)
|
2009-07-17 20:36:40 +00:00
|
|
|
next if not eth.ethertype == 0x0800
|
2009-11-02 18:16:51 +00:00
|
|
|
|
2009-12-29 23:32:50 +00:00
|
|
|
ip = Racket::L3::IPv4.new(eth.payload)
|
2009-07-17 20:36:40 +00:00
|
|
|
next if not ip.protocol == 6
|
2009-11-02 18:16:51 +00:00
|
|
|
|
2009-12-29 23:32:50 +00:00
|
|
|
tcp = Racket::L4::TCP.new(ip.payload)
|
2009-11-02 18:16:51 +00:00
|
|
|
|
2009-07-17 20:36:40 +00:00
|
|
|
reply = {:raw => r, :eth => eth, :ip => ip, :tcp => tcp}
|
2009-11-02 18:16:51 +00:00
|
|
|
|
2009-03-26 14:55:53 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:22:50 +00:00
|
|
|
rescue Timeout::Error
|
2009-03-26 14:55:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return reply
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|