Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/auxiliary/scanner/portscan/syn.rb
21367 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Auxiliary
7
include Msf::Exploit::Capture
8
include Msf::Auxiliary::Report
9
include Msf::Auxiliary::Scanner
10
11
def initialize
12
super(
13
'Name' => 'TCP SYN Port Scanner',
14
'Description' => %q{
15
Enumerate open TCP services using a raw SYN scan.
16
},
17
'Author' => 'kris katterjohn',
18
'License' => MSF_LICENSE
19
)
20
21
register_options([
22
OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),
23
OptInt.new('TIMEOUT', [true, "The reply read timeout in milliseconds", 500]),
24
OptInt.new('BATCHSIZE', [true, "The number of hosts to scan per set", 256]),
25
OptInt.new('DELAY', [true, "The delay between connections, per thread, in milliseconds", 0]),
26
OptInt.new('JITTER', [true, "The delay jitter factor (maximum value by which to +/- DELAY) in milliseconds.", 0]),
27
OptString.new('INTERFACE', [false, 'The name of the interface'])
28
])
29
30
deregister_options('FILTER', 'PCAPFILE')
31
end
32
33
# No IPv6 support yet
34
def support_ipv6?
35
false
36
end
37
38
def run_batch_size
39
datastore['BATCHSIZE'] || 256
40
end
41
42
def run_batch(hosts)
43
ports = Rex::Socket.portspec_crack(datastore['PORTS'])
44
if ports.empty?
45
raise Msf::OptionValidateError.new(['PORTS'])
46
end
47
48
jitter_value = datastore['JITTER'].to_i
49
if jitter_value < 0
50
raise Msf::OptionValidateError.new(['JITTER'])
51
end
52
53
delay_value = datastore['DELAY'].to_i
54
if delay_value < 0
55
raise Msf::OptionValidateError.new(['DELAY'])
56
end
57
58
open_pcap
59
pcap = self.capture
60
61
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
62
63
# we copy the hosts because some may not be reachable and need to be ejected
64
host_queue = hosts.dup
65
# Spread the load across the hosts
66
ports.each do |dport|
67
host_queue.each do |dhost|
68
shost, sport = getsource(dhost)
69
70
self.capture.setfilter(getfilter(shost, sport, dhost, dport))
71
72
# Add the delay based on JITTER and DELAY if needs be
73
add_delay_jitter(delay_value, jitter_value)
74
75
begin
76
probe = buildprobe(shost, sport, dhost, dport)
77
78
unless capture_sendto(probe, dhost)
79
host_queue.delete(dhost)
80
next
81
end
82
83
reply = probereply(self.capture, to)
84
85
next if not reply
86
87
if (reply.is_tcp? and reply.tcp_flags.syn == 1 and reply.tcp_flags.ack == 1)
88
print_good(" TCP OPEN #{dhost}:#{dport}")
89
report_service(:host => dhost, :port => dport)
90
end
91
rescue ::Exception
92
print_error("Error: #{$!.class} #{$!}")
93
end
94
end
95
end
96
97
close_pcap
98
end
99
100
def getfilter(shost, sport, dhost, dport)
101
# Look for associated SYN/ACKs and RSTs
102
"tcp and (tcp[13] == 0x12 or (tcp[13] & 0x04) != 0) and " +
103
"src host #{dhost} and src port #{dport} and " +
104
"dst host #{shost} and dst port #{sport}"
105
end
106
107
def getsource(dhost)
108
# srcip, srcport
109
[ Rex::Socket.source_address(dhost), rand(0xffff - 1025) + 1025 ]
110
end
111
112
def buildprobe(shost, sport, dhost, dport)
113
p = PacketFu::TCPPacket.new
114
p.ip_saddr = shost
115
p.ip_daddr = dhost
116
p.tcp_sport = sport
117
p.tcp_flags.ack = 0
118
p.tcp_flags.syn = 1
119
p.tcp_dport = dport
120
p.tcp_win = 3072
121
p.recalc
122
p
123
end
124
125
def probereply(pcap, to)
126
reply = nil
127
begin
128
Timeout.timeout(to) do
129
pcap.each do |r|
130
pkt = PacketFu::Packet.parse(r)
131
next unless pkt.is_tcp?
132
133
reply = pkt
134
break
135
end
136
end
137
rescue Timeout::Error
138
end
139
return reply
140
end
141
end
142
143