CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/auxiliary/scanner/ntp/ntp_monlist.rb
Views: 1904
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::Auxiliary::Report
8
include Msf::Exploit::Remote::Udp
9
include Msf::Auxiliary::UDPScanner
10
include Msf::Auxiliary::NTP
11
include Msf::Auxiliary::DRDoS
12
13
def initialize
14
super(
15
'Name' => 'NTP Monitor List Scanner',
16
'Description' => %q{
17
This module identifies NTP servers which permit "monlist" queries and
18
obtains the recent clients list. The monlist feature allows remote
19
attackers to cause a denial of service (traffic amplification)
20
via spoofed requests. The more clients there are in the list, the
21
greater the amplification.
22
},
23
'References' =>
24
[
25
['CVE', '2013-5211'],
26
['URL', 'https://www.cisa.gov/uscert/ncas/alerts/TA14-013A'],
27
['URL', 'https://support.ntp.org/bin/view/Main/SecurityNotice'],
28
['URL', 'https://nmap.org/nsedoc/scripts/ntp-monlist.html'],
29
],
30
'Author' => 'hdm',
31
'License' => MSF_LICENSE
32
)
33
34
register_options(
35
[
36
OptInt.new('RETRY', [false, "Number of tries to query the NTP server", 3]),
37
OptBool.new('SHOW_LIST', [false, 'Show the recent clients list', false])
38
])
39
40
register_advanced_options(
41
[
42
OptBool.new('StoreNTPClients', [true, 'Store NTP clients as host records in the database', false])
43
])
44
end
45
46
# Called for each response packet
47
def scanner_process(data, shost, sport)
48
@results[shost] ||= { messages: [], peers: [] }
49
@results[shost][:messages] << Rex::Proto::NTP::NTPPrivate.new.read(data).to_binary_s
50
@results[shost][:peers] << extract_peer_tuples(data)
51
end
52
53
# Called before the scan block
54
def scanner_prescan(batch)
55
@results = {}
56
@aliases = {}
57
@probe = Rex::Proto::NTP.ntp_private(datastore['VERSION'], datastore['IMPLEMENTATION'], 42, "\0" * 40).to_binary_s
58
end
59
60
# Called after the scan block
61
def scanner_postscan(batch)
62
@results.keys.each do |k|
63
response_map = { @probe => @results[k][:messages] }
64
peer = "#{k}:#{rport}"
65
66
# TODO: check to see if any of the responses are actually NTP before reporting
67
report_service(
68
:host => k,
69
:proto => 'udp',
70
:port => rport,
71
:name => 'ntp'
72
)
73
74
peers = @results[k][:peers].flatten(1)
75
unless peers.empty?
76
print_good("#{peer} NTP monlist request permitted (#{peers.length} entries)")
77
# store the peers found from the monlist
78
report_note(
79
:host => k,
80
:proto => 'udp',
81
:port => rport,
82
:type => 'ntp.monlist',
83
:data => {:monlist => peers}
84
)
85
# print out peers if desired
86
if datastore['SHOW_LIST']
87
peers.each do |ntp_peer|
88
print_status("#{peer} #{ntp_peer}")
89
end
90
end
91
# store any aliases for our target
92
report_note(
93
:host => k,
94
:proto => 'udp',
95
:port => rport,
96
:type => 'ntp.addresses',
97
:data => {:addresses => peers.map { |p| p.last }.sort.uniq }
98
)
99
100
if (datastore['StoreNTPClients'])
101
print_status("#{peer} Storing #{peers.length} NTP client hosts in the database...")
102
peers.each do |r|
103
maddr,mport,mserv = r
104
next if maddr == '127.0.0.1' # some NTP servers peer with themselves..., but we can't store loopback
105
report_note(
106
:host => maddr,
107
:type => 'ntp.client.history',
108
:data => {
109
:address => maddr,
110
:port => mport,
111
:server => mserv
112
}
113
)
114
end
115
end
116
end
117
118
vulnerable, proof = prove_amplification(response_map)
119
what = 'NTP Mode 7 monlist DRDoS (CVE-2013-5211)'
120
if vulnerable
121
print_good("#{peer} - Vulnerable to #{what}: #{proof}")
122
report_vuln({
123
:host => k,
124
:port => rport,
125
:proto => 'udp',
126
:name => what,
127
:refs => self.references
128
})
129
else
130
vprint_status("#{peer} - Not vulnerable to #{what}: #{proof}")
131
end
132
end
133
134
end
135
136
# Examine the monlist response +data+ and extract all peer tuples (saddd, dport, daddr)
137
def extract_peer_tuples(data)
138
return [] if data.length < 76
139
140
# NTP headers 8 bytes
141
ntp_flags, ntp_auth, ntp_vers, ntp_code = data.slice!(0,4).unpack('C*')
142
pcnt, plen = data.slice!(0,4).unpack('nn')
143
return [] if plen != 72
144
145
idx = 0
146
peer_tuples = []
147
1.upto(pcnt) do
148
# u_int32 firsttime; /* first time we received a packet */
149
# u_int32 lasttime; /* last packet from this host */
150
# u_int32 restr; /* restrict bits (was named lastdrop) */
151
# u_int32 count; /* count of packets received */
152
# u_int32 addr; /* host address V4 style */
153
# u_int32 daddr; /* destination host address */
154
# u_int32 flags; /* flags about destination */
155
# u_short port; /* port number of last reception */
156
157
_,_,_,_,saddr,daddr,_,dport = data[idx, 30].unpack("NNNNNNNn")
158
159
peer_tuples << [ Rex::Socket.addr_itoa(saddr), dport, Rex::Socket.addr_itoa(daddr) ]
160
idx += plen
161
end
162
peer_tuples
163
end
164
end
165
166