forked from berthubert/simplomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsmon.cc
259 lines (204 loc) · 7.92 KB
/
dnsmon.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "record-types.hh"
#include "sclasses.hh"
#include <thread>
#include <signal.h>
#include "fmt/format.h"
#include "fmt/ranges.h"
#include "fmt/chrono.h"
#include "simplomon.hh"
using namespace std;
DNSChecker::DNSChecker(sol::table data)
{
checkLuaTable(data, {"server", "name", "type", "acceptable"}, {"rd"});
d_nsip = ComboAddress(data.get<string>("server"), 53);
d_qname = makeDNSName(data.get<string>("name"));
d_qtype = makeDNSType(data.get<string>("type").c_str());
for(const auto& a : data.get<vector<string>>("acceptable"))
d_acceptable.insert(a);
d_rd = data.get_or("rd", true);
}
CheckResult DNSChecker::perform()
{
DNSMessageWriter dmw(d_qname, d_qtype);
dmw.dh.rd = true;
dmw.randomizeID();
dmw.setEDNS(4000, false);
Socket sock(d_nsip.sin4.sin_family, SOCK_DGRAM);
SConnect(sock, d_nsip);
SWrite(sock, dmw.serialize());
ComboAddress server;
double timeo=0.5;
if(!waitForData(sock, &timeo)) { // timeout
return fmt::format("Timeout asking DNS question for {}|{} to {}",
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
}
string resp = SRecvfrom(sock, 65535, server);
DNSMessageReader dmr(resp);
DNSSection rrsection;
uint32_t ttl;
DNSName dn;
DNSType dt;
dmr.getQuestion(dn, dt);
// cout<<"Received "<<resp.size()<<" byte response with RCode "<<(RCode)dmr.dh.rcode<<", qname " <<dn<<", qtype "<<dt<<endl;
if((RCode)dmr.dh.rcode != RCode::Noerror) {
return fmt::format("Got DNS response with RCode {} from {} for question {}|{}",
toString((RCode)dmr.dh.rcode), d_qname.toString(), d_nsip.toStringWithPort(), toString(d_qtype));
}
std::unique_ptr<RRGen> rr;
int matches = 0;
while(dmr.getRR(rrsection, dn, dt, ttl, rr)) {
if(rrsection == DNSSection::Answer && dt == d_qtype) {
// cout << rrsection<<" "<<dn<< " IN " << dt << " " << ttl << " " <<rr->toString()<<endl;
if(dt == DNSType::NS) {
set<DNSName> acc;
for(const auto& a : d_acceptable)
acc.insert(makeDNSName(a));
if(!acc.count(dynamic_cast<NSGen*>(rr.get())->d_name)) {
return fmt::format("Unacceptable DNS answer {} for question {} from {}. Acceptable: {}", rr->toString(), d_qname.toString(), d_nsip.toStringWithPort(), d_acceptable);
}
else matches++;
}
else if(!d_acceptable.count(rr->toString())) {
return fmt::format("Unacceptable DNS answer {} for question {} from {}. Acceptable: {}", rr->toString(), d_qname.toString(), d_nsip.toStringWithPort(), d_acceptable);
}
else matches++;
}
}
if(matches) {
return "";
}
else {
return fmt::format("No matching answer to question {}|{} to {} was received", d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
}
}
DNSSOAChecker::DNSSOAChecker(const std::string& domain,
const std::set<std::string>& servers)
{
d_domain = makeDNSName(domain);
for(const auto& s : servers)
d_servers.insert(ComboAddress(s, 53));
}
DNSSOAChecker::DNSSOAChecker(sol::table data)
{
checkLuaTable(data, {"domain", "servers"});
d_domain = makeDNSName(data.get<string>("domain"));
auto serv = data.get<vector<string>>("servers");
for(const auto& s: serv)
d_servers.insert(ComboAddress(s, 53));
}
CheckResult DNSSOAChecker::perform()
{
set<string> harvest;
for(const auto& s: d_servers) {
DNSMessageWriter dmw(d_domain, DNSType::SOA);
dmw.dh.rd = false;
dmw.randomizeID();
dmw.setEDNS(4000, false);
Socket sock(s.sin4.sin_family, SOCK_DGRAM);
SConnect(sock, s);
SWrite(sock, dmw.serialize());
ComboAddress server;
double timeo=0.5;
if(!waitForData(sock, &timeo)) { // timeout
return fmt::format("Timeout asking DNS question for {}|{} to {}",
d_domain.toString(), toString(DNSType::SOA), s.toStringWithPort());
}
string resp = SRecvfrom(sock, 65535, server);
DNSMessageReader dmr(resp);
DNSSection rrsection;
uint32_t ttl;
DNSName dn;
DNSType dt;
dmr.getQuestion(dn, dt);
// cout<<"Received "<<resp.size()<<" byte response with RCode "<<(RCode)dmr.dh.rcode<<", qname " <<dn<<", qtype "<<dt<<endl;
if((RCode)dmr.dh.rcode != RCode::Noerror) {
return fmt::format("Got DNS response with RCode {} for question {}|{}",
toString((RCode)dmr.dh.rcode), d_domain.toString(), toString(DNSType::SOA));
}
std::unique_ptr<RRGen> rr;
int matches = 0;
while(dmr.getRR(rrsection, dn, dt, ttl, rr)) {
if(dn == d_domain && rrsection == DNSSection::Answer && dt == DNSType::SOA) {
harvest.insert(rr->toString());
matches++;
}
}
if(!matches) {
return fmt::format("DNS server {} did not return a SOA for {}",
s.toStringWithPort(), d_domain.toString());
}
}
if(harvest.size() != 1) {
return fmt::format("Had different SOA records for {}: {}",
d_domain.toString(), harvest);
}
else {
return "";
}
}
RRSIGChecker::RRSIGChecker(sol::table data)
{
checkLuaTable(data, {"server", "name"}, {"minDays", "type"});
d_nsip = ComboAddress(data.get<string>("server"), 53);
d_qname = makeDNSName(data.get<string>("name"));
d_qtype = makeDNSType(data.get_or("type", string("SOA")).c_str());
d_minDays = data.get_or("minDays", 7);
}
CheckResult RRSIGChecker::perform()
{
DNSMessageWriter dmw(d_qname, d_qtype);
dmw.dh.rd = false;
dmw.randomizeID();
dmw.setEDNS(4000, true);
Socket sock(d_nsip.sin4.sin_family, SOCK_DGRAM);
SConnect(sock, d_nsip);
SWrite(sock, dmw.serialize());
ComboAddress server;
double timeo=1.0;
if(!waitForData(sock, &timeo)) { // timeout
return fmt::format("Timeout asking DNS question for {}|{} to {}",
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
}
string resp = SRecvfrom(sock, 65535, server);
DNSMessageReader dmr(resp);
DNSSection rrsection;
uint32_t ttl;
DNSName dn;
DNSType dt;
dmr.getQuestion(dn, dt);
if((RCode)dmr.dh.rcode != RCode::Noerror) {
return fmt::format("Got DNS response with RCode {} from {} for question {}|{}",
toString((RCode)dmr.dh.rcode), d_qname.toString(), d_nsip.toStringWithPort(), toString(d_qtype));
}
std::unique_ptr<RRGen> rr;
bool valid=false;
while(dmr.getRR(rrsection, dn, dt, ttl, rr)) {
if(rrsection == DNSSection::Answer && dt == DNSType::RRSIG && dn == d_qname) {
auto rrsig = dynamic_cast<RRSIGGen*>(rr.get());
if(rrsig->d_type != d_qtype) {
fmt::print("Skipping wrong type {}\n", toString(rrsig->d_type));
continue;
}
struct tm tmstart={}, tmend={};
time_t inception = rrsig->d_inception;
time_t expire = rrsig->d_expire;
gmtime_r(&inception, &tmstart);
gmtime_r(&expire, &tmend);
// fmt::print("Got active RRSIG for {}|{} from {:%Y-%m-%d %H:%M} to {:%Y-%m-%d %H:%M} UTC\n", d_qname.toString(), toString(d_qtype), tmstart, tmend);
time_t now = time(nullptr);
if(now + d_minDays * 86400 > expire)
return fmt::format("Got RRSIG that expires in {:.0f} days for {}|{} from {}, valid from {:%Y-%m-%d %H:%M} to {:%Y-%m-%d %H:%M} UTC",
(expire - now)/86400.0,
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort(), tmstart, tmend);
else if(now < inception) {
fmt::print("Got RRSIG that is not yet active for {}|{} from {}, valid from {:%Y-%m-%d %H:%M} to {:%Y-%m-%d %H:%M} UTC\n",
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort(), tmstart, tmend);
}
else
valid=true;
}
}
if(!valid)
return fmt::format("Did not find an active RRSIG for {}|{} over at server {}", d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
return "";
}