-
Notifications
You must be signed in to change notification settings - Fork 3
/
collect.pl
executable file
·411 lines (368 loc) · 11.9 KB
/
collect.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/perl
# Copyright (c) 2012 The Internet Infrastructure Foundation (.SE). 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.
use warnings;
use strict;
use Data::Dumper;
use Thread;
use Thread::Queue;
use Term::ANSIColor;
use Pod::Usage;
use Getopt::Long;
use JSON -support_by_pp;
use Net::DNS;
use Net::DNS::SEC;
# global program parameters
my $config = 'collect.json';
my $DEBUG = 0; # set to true if you want some debug output
my $pretty = 0; # set to true to output pretty JSON
my $threads = 80; # default number of threads
my $outdir;
my $filename;
my $par = 0; # think of the parenthesis
my $rr = ""; # global rr var
my $queue = Thread::Queue->new();
# read configuration file for at least the resolver
open CONFIG, "$config" or warn "Cannot read config file $config";
my @CONFIG = <CONFIG>;
close CONFIG;
my $CONF = join '',@CONFIG;
my $c = from_json($CONF);
# set up params from config file
$threads = $c->{'threads'} if defined $c->{'threads'};
my $local_resolver = $c->{'resolver'};
my @parents = @{$c->{'parents'}};
# global resolver
my $res = Net::DNS::Resolver->new;
$res->nameservers($c->{'resolver'});
$res->recurse(1);
$res->dnssec(1);
$res->cdflag(0);
$res->udppacketsize(4096);
$res->tcp_timeout(10);
$res->udp_timeout(10);
# parent server for all delegations (ie a.ns.se for .se domains)
# (we could also use a non-validating recursive resolver)
my $fnsse = Net::DNS::Resolver->new;
$fnsse->nameservers(@parents);
$fnsse->recurse(0);
$fnsse->dnssec(1);
$fnsse->cdflag(0);
$fnsse->udppacketsize(4096);
$fnsse->tcp_timeout(5);
$fnsse->udp_timeout(5);
# fetch all data we need for a domain name, returns with a hash
sub readDNS
{
my $name = shift;
my $count; # retry counter
my $result; # resulting JSON stuffz
$result->{'domain'} = $name;
my $answer; # for DNS answers
print "Quering DS for $name\n" if $DEBUG;
$count = 0;
while (1) {
$count++;
last if $count > 5;
$answer = $res->query($name,'DS');
next if not defined $answer;
foreach my $data ($answer->answer)
{
if ($data->type eq 'DS') {
push @{$result->{'ds'}}, {
'digest' => $data->digest,
'digtype' => $data->digtype,
'algorithm' => $data->algorithm,
};
print "DS $name: ".$data->digest."\n" if $DEBUG;
print "DS $name: ".$data->digtype."\n" if $DEBUG;
}
}
last;
}
print "Quering DNSKEY for $name\n" if $DEBUG;
$count = 0;
while (1) {
$count++;
if ($count > 5) { $result->{'dnskey'}->{'rcode'} = 'TIMEOUT'; last; }
$answer = $res->send($name,'DNSKEY');
next if not defined $answer;
$result->{'dnskey'}->{'rcode'} = $answer->header->rcode;
if ($result->{'dnskey'}->{'rcode'} eq '') { print "FOO: $name\n"; next; }
foreach my $data ($answer->answer)
{
if ($data->type eq 'DNSKEY') {
push @{$result->{'dnskey'}->{'list'}}, {
'algorithm' => $data->algorithm,
'keylength' => $data->keylength,
'is_sep' => $data->sep,
'keytag' => $data->keytag,
'key' => $data->key,
};
print "DNSKEY $name: ".$data->key."\n" if $DEBUG;
} elsif ($data->type eq 'RRSIG') {
push @{$result->{'rrsig'}}, {
'siginception' => $data->siginception,
'sigexpiration' => $data->sigexpiration,
'typecovered' => $data->typecovered,
'algorithm' => $data->algorithm,
};
print "RRSIG $name: ".$data->keytag."\n" if $DEBUG;
}
}
last;
}
print "Quering NSEC3PARAM for $name\n" if $DEBUG;
$count = 0;
while (1) {
$count ++;
if ($count > 5) { $result->{'nsec3param'}->{'rcode'} = 'TIMEOUT'; last; }
$answer = $res->send($name,'NSEC3PARAM');
next if not defined $answer;
$result->{'nsec3param'}->{'rcode'} = $answer->header->rcode;
foreach my $data ($answer->answer)
{
if ($data->type eq 'NSEC3PARAM') {
$result->{'nsec3param'}->{'hashalgo'} = $data->hashalgo;
$result->{'nsec3param'}->{'flags'} = $data->flags;
$result->{'nsec3param'}->{'iterations'} = $data->iterations;
$result->{'nsec3param'}->{'salt'} = $data->salt;
print "NSEC3PARAM $name: ".$data->iterations."\n" if $DEBUG;
} elsif ($data->type eq 'RRSIG') {
push @{$result->{'rrsig'}}, {
'siginception' => $data->siginception,
'sigexpiration' => $data->sigexpiration,
'typecovered' => $data->typecovered,
'algorithm' => $data->algorithm,
};
print "RRSIG $name: ".$data->keytag."\n" if $DEBUG;
}
}
last;
}
print "Quering SOA for $name\n" if $DEBUG;
$count = 0;
while (1) {
$count++;
if ($count > 5) { $result->{'soa'}->{'rcode'} = 'TIMEOUT'; last; }
$answer = $res->send($name,'SOA');
next if not defined $answer;
$result->{'soa'}->{'rcode'} = $answer->header->rcode;
foreach my $data ($answer->answer)
{
if ($data->type eq 'SOA') {
$result->{'soa'}->{'mname'} = $data->mname;
$result->{'soa'}->{'rname'} = $data->rname;
$result->{'soa'}->{'serial'} = $data->serial;
$result->{'soa'}->{'refresh'} = $data->refresh;
$result->{'soa'}->{'retry'} = $data->retry;
$result->{'soa'}->{'expire'} = $data->expire;
$result->{'soa'}->{'minimum'} = $data->minimum;
print "SOA $name: ".$data->iterations."\n" if $DEBUG;
} elsif ($data->type eq 'RRSIG') {
push @{$result->{'rrsig'}}, {
'siginception' => $data->siginception,
'sigexpiration' => $data->sigexpiration,
'typecovered' => $data->typecovered,
'algorithm' => $data->algorithm,
};
print "RRSIG $name: ".$data->keytag."\n" if $DEBUG;
}
}
last;
}
print "Quering A for www.$name\n" if $DEBUG;
$count = 0;
while (1) {
$count++;
if ($count > 5) { $result->{'A'}->{'rcode'} = 'TIMEOUT'; last; }
$answer = $res->send($name,'A');
next if not defined $answer;
$result->{'A'}->{'rcode'} = $answer->header->rcode;
foreach my $data ($answer->answer)
{
if ($data->type eq 'A') {
push @{$result->{'A'}->{'list'}}, {
'address' => $data->address,
};
print "A www.$name: ".$data->address."\n" if $DEBUG;
} elsif ($data->type eq 'RRSIG') {
push @{$result->{'rrsig'}}, {
'siginception' => $data->siginception,
'sigexpiration' => $data->sigexpiration,
'typecovered' => $data->typecovered,
'algorithm' => $data->algorithm,
};
print "RRSIG www.$name: ".$data->keytag."\n" if $DEBUG;
}
}
last;
}
print "Quering MX for $name\n" if $DEBUG;
$count = 0;
while (1) {
$count++;
if ($count > 5) { $result->{'MX'}->{'rcode'} = 'TIMEOUT'; last; }
$answer = $res->send($name,'MX');
next if not defined $answer;
$result->{'MX'}->{'rcode'} = $answer->header->rcode;
foreach my $data ($answer->answer)
{
if ($data->type eq 'MX') {
push @{$result->{'MX'}->{'list'}}, {
'exchange' => $data->exchange,
'preference' => $data->preference,
};
print "MX $name: ".$data->exchange."\n" if $DEBUG;
} elsif ($data->type eq 'RRSIG') {
push @{$result->{'rrsig'}}, {
'siginception' => $data->siginception,
'sigexpiration' => $data->sigexpiration,
'typecovered' => $data->typecovered,
'algorithm' => $data->algorithm,
};
print "RRSIG www.$name: ".$data->keytag."\n" if $DEBUG;
}
}
last;
}
print "Quering ns for $name\n" if $DEBUG;
$count = 0;
while (1) {
$count++;
if ($count > 5) { $result->{'NS'}->{'rcode'} = 'TIMEOUT'; last; }
$answer = $fnsse->send($name,'NS');
next if not defined $answer;
$result->{'NS'}->{'rcode'} = $answer->header->rcode;
foreach my $data ($answer->authority)
{
if ($data->type eq 'NS') {
push @{$result->{'NS'}->{'list'}}, {
'nsdname' => $data->nsdname,
};
print "NS $name: ".$data->nsdname."\n" if $DEBUG;
} elsif ($data->type eq 'RRSIG') {
push @{$result->{'rrsig'}}, {
'siginception' => $data->siginception,
'sigexpiration' => $data->sigexpiration,
'typecovered' => $data->typecovered,
'algorithm' => $data->algorithm,
};
}
}
last;
}
return $result;
}
# find values in a hash by adressing it like this "key:key2:key3"
sub findValue {
my $hash = shift;
my $find = shift;
my $value;
my $defined;
my @keys = split (':',$find);
@keys = map { "{'$_'}" } @keys;
my $evalHash = '$hash->'.join('->',@keys);
eval '$defined = defined '.$evalHash;
return undef if $@;
return undef if $defined eq '';
eval '$value = '.$evalHash;
return undef if $@;
return $value if defined $value;
return 1;
}
sub processDomain {
while (defined(my $domain = $queue->dequeue_nb)) {
chomp $domain;
my $res = readDNS($domain);
print color 'reset';
if(findValue($res,'A:rcode') eq 'SERVFAIL' or
findValue($res,'MX:rcode') eq 'SERVFAIL' or
findValue($res,'soa:rcode') eq 'SERVFAIL' or
findValue($res,'nsec3param:rcode') eq 'SERVFAIL' or
findValue($res,'dnskey:rcode') eq 'SERVFAIL') {
print"$domain: "; print color 'red'; print "SERVFAIL\n"; print color 'reset';
} else {
print"$domain: "; print color 'green'; print "OK\n"; print color 'reset';
}
# output result
open(OUT, '>', "$outdir/$domain") or die $!;
print OUT to_json($res, { utf8 => 1 });
close(OUT);
}
threads->exit();
}
sub runQueue {
die "cannot create directory $outdir: $!" if not mkdir $outdir;
die 'no file for runQueue()' if not defined $filename;
die 'no outdir for runQueue()' if not defined $outdir;
open FILE, "$filename" or die "Cannot read file $filename: $!";
while ( <FILE> ) {
$queue->enqueue($_);
}
close FILE;
threads->create({'stack_size' => 32*4096}, "processDomain") for (1 .. $threads);
while(threads->list(threads::running) != 0) {
print color 'yellow';
print "Pending: ".$queue->pending()." - Running: ".threads->list(threads::running)."\n";
print color 'reset';
sleep(2);
}
}
sub main() {
# non-global program parameters
my $help = 0;
my $name;
GetOptions('help|?' => \$help,
'name|n=s' => \$name,
'file|f=s' => \$filename,
'outdir|d=s' => \$outdir,
'threads' => \$threads,
'debug' => \$DEBUG,
'pretty' => \$pretty,)
or pod2usage(2);
pod2usage(1) if($help);
pod2usage(1) if(not defined $name and not defined $filename);
if (defined $name) {
my $dnsData = readDNS($name);
print to_json($dnsData, { utf8 => 1, pretty => $pretty} );
} elsif (defined $filename) {
runQueue($filename,$outdir);
}
}
main;
=head1 NAME
collect
=head1 SYNOPSIS
collect.pl -n domain
-n domain specify name
-f file.txt read list of names from file
-d outdir create this directory and store result in if reading domains from file
--threads number of threads
--debug debug mode
--pretty print in pretty mode
=head1 DESCRIPTION
gets DNSSEC data for domain (outputs JSON)
=head1 AUTHOR
Patrik Wallstrom <[email protected]>
=cut