forked from pawal/dnssec-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnssec_monitor.pl
executable file
·200 lines (159 loc) · 5.55 KB
/
dnssec_monitor.pl
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
#!/usr/bin/perl
#
# Copyright (c) 2010 .SE (The Internet Infrastructure Foundation).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
######################################################################
require 5.8.0;
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use Net::DNS 0.49;
use Net::DNS::SEC 0.12;
use Crypt::OpenSSL::Random qw(random_bytes);
use Digest::SHA qw(sha1);
use Digest::BubbleBabble qw(bubblebabble);
use lib '.';
use dnssec_monitor;
use constant PROGNAME => "dnssec_monitor";
use constant VERSION => $dnssec_monitor::VERSION;
######################################################################
sub main {
my $help = 0;
my $version = 0;
my $debug = 0;
my $quiet = 0;
my $zone;
my $ksk_expire_critical = 7;
my $ksk_expire_warning = 14;
my $zsk_expire_critical = 1;
my $zsk_expire_warning = 3;
my $enable_wildcard = 0;
my $enable_nsec3 = 0;
GetOptions(
'help|?' => \$help,
'version|v' => \$version,
'zone=s' => \$zone,
'kskcritical=i' => \$ksk_expire_critical,
'kskwarning=i' => \$ksk_expire_warning,
'zskcritical=i' => \$zsk_expire_critical,
'zskwarning=i' => \$zsk_expire_warning,
'wildcard' => \$enable_wildcard,
'nsec3' => \$enable_nsec3,
'debug+' => \$debug,
'quiet|q' => \$quiet
) or pod2usage(2);
pod2usage(1) if ($help);
if ($version) {
printf("%s %s\n", PROGNAME, VERSION);
exit(-1);
}
pod2usage(1) unless ($zone);
# initialize NS list for zone
my @nameservers;
if ($#ARGV >= 0) {
@nameservers = @ARGV;
} else {
@nameservers = get_ns($zone);
}
# create nonexisting domain name
my @tmp = split(/-/, bubblebabble(Digest => sha1(random_bytes(64))));
my $nonexisting = sprintf("%s.%s", join("", @tmp[1 .. 6]), $zone);
my %args = (
quiet => $quiet,
debug => $debug,
ksk_expire_critical => $ksk_expire_critical,
ksk_expire_warning => $ksk_expire_warning,
zsk_expire_critical => $zsk_expire_critical,
zsk_expire_warning => $zsk_expire_warning,
);
my $errors = 0;
for my $ns (@nameservers) {
print "Checking $ns ...\n" unless ($quiet);
my $c = new dnssec_monitor($zone, $ns, %args);
$errors++ unless ($c->report($c->check_apex_rrsig()));
$errors++ unless ($c->report($c->check_exist($zone, "SOA")));
$errors++ unless ($c->report($c->check_exist($zone, "NS")));
$errors++
unless (
$c->report(
$c->check_nxdomain(
$nonexisting, "NS", $enable_wildcard, $enable_nsec3
)
)
);
}
exit($errors);
}
sub get_ns {
my $zone = shift;
my $res = Net::DNS::Resolver->new;
my @n = ();
my @a = ();
my $packet = $res->send($zone, "NS");
return () unless $packet;
foreach my $rr ($packet->answer) {
push @n, $rr->nsdname if ($rr->type eq "NS");
}
foreach my $ns (@n) {
$packet = $res->query($ns, "A");
next unless $packet;
foreach my $rr ($packet->answer) {
push @a, $rr->address if ($rr->type eq "A"
or $rr->type eq "AAAA");
}
}
return @a;
}
sub uniq {
my %u;
foreach my $x (@_) {
$u{$x} = "";
}
return keys %u;
}
main;
__END__
=head1 NAME
dnssec_monitor - simple DNSSEC monitor
=head1 SYNOPSIS
dnssec_monitor [options] [nameservers]
Options:
--help brief help message
--zone ZONE zone to check
--kskcritical N check for KSK expire within DAYS days
--kskwarning N check for KSK expire within DAYS days
--zskcritical N check for ZSK expire within DAYS days
--zskwarning N check for ZSK expire within DAYS days
--wildcard disable the nxdomain check to allow for wildcards
--nsec3 require NSEC3
--debug turn on debugging
--quiet be really quiet
--version display version and exit
If no nameservers are specified, all nameservers for ZONE are checked.
=head1 ABSTRACT
dnssec-monitor will check integrity of the zone DNSKEYs, SOA and NS.
It will also generate a random domain name and verify that a NSEC or
NSEC3 record is generated for negative answers.