-
Notifications
You must be signed in to change notification settings - Fork 3
/
activedns.pl
401 lines (331 loc) · 10.7 KB
/
activedns.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
#!/usr/bin/perl
# ----------------------------------------------------------------------
# ActiveDNS
# Copyright (C) 2013, Edward Fjellskål <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# ----------------------------------------------------------------------
use strict;
use warnings;
use POSIX qw(setsid strftime);
use DateTime;
use Getopt::Long qw/:config auto_version auto_help/;
use Net::DNS::Async;
use Net::DNS::Packet;
use Time::HiRes qw(usleep);
use Switch;
#use Data::Dumper;
=head1 NAME
activedns.pl - Active resolve Domains.
=head1 VERSION
0.22
=head1 SYNOPSIS
$ activedns.pl [options]
OPTIONS:
--file <file> : the file to load domains from (default /etc/activedns/domains.txt)
--log <file> : the file to log dns results in (default /var/log/activedns.log)
--nolog : turn off logging to --log <file> (default on)
--batch : run through all domains once then exit
--usleep : sleep in microseconds between each request (default 100000 ms)
--ttl-min : minimum TTL for a domain before we query again (default 60)
--ttl-mul : TTL multiplier to extend the time between queries (default 1)
--persistance : will try to load query state on startup, and flush state when exiting (default 1)
--persfile : the file to load and save persistance from (default /var/lib/activedns/activedns-state.log)
--daemon : enables daemon mode
--verbose : enables some verboseness
--debug <int> : enable debug messages (default: 0 (disabled))
--help : this help message
--version : show version
=cut
my $VERSION = 0.22;
my $DEBUG = 0;
my $VERBOSE = 0;
my $DAEMON = 0;
my $TIMEOUT = 1;
my $BATCH = 0;
my $SLEEP = 100000;
my $TTL_MUL = 1;
my $TTL_MIN = 180;
my $dkey = {};
my $LIP = q(192.168.0.1);
my $NOLOG = 0;
my $PERSISTANCE = 1;
my $PERSFILE = q(/var/lib/activedns/activedns-state.log);
my $ADNSFILE = q(/etc/activedns/domains.txt);
my $ADNSLOG = q(/var/log/activedns.log);
my $LOGFILE = q(/var/log/activedns-run.log);
my $PIDFILE = q(/var/run/activedns.pid);
GetOptions(
'file=s' => \$ADNSFILE,
'log=s' => \$ADNSLOG,
'batch' => \$BATCH,
'usleep' => \$SLEEP,
'ttl-mul=s' => \$TTL_MUL,
'ttl-min=s' => \$TTL_MIN,
'nolog' => \$NOLOG,
'persistance=s' => \$PERSISTANCE,
'persfile=s' => \$PERSFILE,
'debug=s' => \$DEBUG,
'daemon' => \$DAEMON,
'verbose' => \$VERBOSE,
);
# Dont multiply with 0 etc!
$TTL_MUL = 1 if ( $TTL_MUL < 1 );
# Set a saine minimum default...
$TTL_MIN = 60 if ( $TTL_MUL < 60 );
# Signal handlers
use vars qw(%sources);
$SIG{"HUP"} = sub { reload_domain_file() };
$SIG{"INT"} = sub { gameover() };
$SIG{"TERM"} = sub { gameover() };
$SIG{"QUIT"} = sub { gameover() };
$SIG{"KILL"} = sub { gameover() };
#$SIG{"ALRM"} = sub { print_stats(); alarm $TIMEOUT; };
logger("[*] Starting...\n");
my @domains = load_domain_list($ADNSFILE);
my $LFH;
if ( $ADNSLOG ne "" ) {
open($LFH, ">>", $ADNSLOG) or die "[E] Could not open '$ADNSLOG': $!";
}
$dkey = load_persistance($PERSFILE) if ($PERSISTANCE != 0);
# Prepare to meet the world of Daemons
if ( $DAEMON ) {
logger("[*] Daemonizing...\n");
chdir ("/") or die "chdir /: $!\n";
open (STDIN, "/dev/null") or die "open /dev/null: $!\n";
open (STDOUT, "> $LOGFILE") or die "open > $LOGFILE: $!\n";
defined (my $dpid = fork) or die "fork: $!\n";
if ($dpid) {
# Write PID file
open (PID, "> $PIDFILE") or die "open($PIDFILE): $!\n";
print PID $dpid, "\n";
close (PID);
exit 0;
}
setsid ();
open (STDERR, ">&STDOUT");
}
# Run for ever....
while (1) {
my $a_res = new Net::DNS::Async(QueueSize => 10, Retries => 3, Timeout => 3);
foreach my $domain (@domains) {
my $epoch = time;
$domain = lc($domain);
if (not defined $dkey->{$domain}) {
$dkey->{$domain} = 0;
}
if ($dkey->{$domain} <= $epoch) {
logger("[D] Processing domain: $domain\n") if $DEBUG;
my $packet = new Net::DNS::Packet($domain);
$a_res->add({ Nameservers => [ qw(8.8.8.8 8.8.4.4) ],
Callback => \&callback,
Query => [ $packet ]
});
} else {
my $tleft = $dkey->{$domain} - $epoch;
logger("[D] Time left for recheck of $domain : $tleft\n") if $DEBUG;
}
usleep($SLEEP);
}
$a_res->await();
if ( $BATCH == 1 ) {
logger("[*] Batch run finished...\n");
exit;
}
sleep 5;
logger("[D] Looping\n") if $DEBUG;
}
=head1 FUNCTIONS
=head2 load_domain_list
Callback rutine for the DNS response.
=cut
sub callback {
my $packet = shift;
my $query = q();
my $rr = q();
my $type = q();
my $answer = q();
my $ttl = q();
my $epoch = time;
my $serverip = q();
if ( defined($packet) ) {
$serverip = $packet->answerfrom;
if ( $packet->header->rcode eq "NOERROR") {
#print Dumper $packet;
foreach my $r ( $packet->answer ) {
#print Dumper $r;
$query = lc($r->name);
$rr = $r->class;
$type = $r->type;
switch ($type) {
case /^(A|AAAA)/ {
$answer= $r->address;
}
case "CNAME" {
$answer= $r->cname;
}
}
$ttl = $r->ttl;
$ttl = $TTL_MIN if ($ttl < $TTL_MIN);
$dkey->{$query} = $epoch + ($ttl * $TTL_MUL);
log_adns($epoch, $serverip, $rr, $query, $type, $answer, $r->ttl);
}
} else {
my @question = $packet->question;
$query = lc($question[0]->qname);
$rr = $question[0]->qclass;
$type = $question[0]->qtype;
$answer= $packet->header->rcode;
$ttl = 43200;
$dkey->{$query} = $epoch + ($ttl * $TTL_MUL);
log_adns($epoch, $serverip, $rr, $query, $type, $answer, $ttl);
}
}
}
=head2 reload_domain_file
Reloads the domain file.
=cut
sub reload_domain_file {
my $bcnt = @domains;
@domains = ();
@domains = load_domain_list($ADNSFILE);
my $acnt = @domains;
my $ncnt = ($acnt - $bcnt);
if ( $ncnt == 0 ) {
logger("[*] Reload of domain file: No new domains\n");
} elsif ( $ncnt > 0 ) {
logger("[*] Reload of domain file: $ncnt new domains\n");
} else {
logger("[*] Reload of domain file: ". abs($ncnt)." less domains\n");
}
}
=head2 log_adns
logs dns answers in PassiveDNS style...
=cut
sub log_adns {
my($epoch, $serverip, $rr, $query, $type, $answer, $ttl) = @_;
my $count = 1;
return if not defined $LFH;
if ( ($NOLOG == 0) && ($ADNSLOG ne "") ) {
print $LFH "$epoch.123456||$LIP||$serverip||$rr||$query.||$type||$answer||$ttl||$count\n";
}
if ( $DAEMON == 0 && $VERBOSE == 1) {
logger("[*] $epoch.123456||$LIP||$serverip||$rr||$query.||$type||$answer||$ttl||$count\n");
}
}
=head2 load_domain_list
Reads a file and pushes domains into an array.
Input $file - Output @domains
=cut
sub load_domain_list {
my $file = shift;
my @domain = ();
my $cnt = 0;
open(my $FH, "<", $file) or die "[E] Could not open '$file': $!";
LINE:
while (my $line = readline $FH) {
chomp $line;
$line =~ s/\#.*//;
$line =~ s/\t//g;
$line =~ s/ //g;
next LINE unless($line); # empty line
# One should check for a more or less sane domaine.
logger("[D] $line\n") if $DEBUG > 3;
push(@domains, $line);
$cnt++;
}
if ($cnt > 1) {
logger("[*] Loaded $cnt domains from file: $file\n") if $VERBOSE;
} else {
logger("[*] Loaded $cnt domain from file: $file\n") if $VERBOSE;
}
return @domains;
}
=head2 logger
Adds time prefix to logg output. Takes $msg as input.
=cut
sub logger {
my $msg = shift;
print strftime('%F %H:%M:%S', localtime), " $msg";
}
=head2 load_persistance
Loads query state from a saved persistance file
=cut
sub load_persistance {
my $file = shift;
my $cnt = 0;
my $key = {};
if ( $PERSISTANCE != 0 ) {
open(my $FH, "<", $file) or die "[E] Could not open '$file': $!";
LINE:
while (my $line = readline $FH) {
chomp $line;
$line =~ s/\#.*//;
$line =~ s/\t//g;
$line =~ s/ //g;
next LINE unless($line); # empty line
logger("[D] $line\n") if $DEBUG > 3;
my ($domain,$epoch) = split(/,/,$line);
$key->{$domain} = $epoch;
$cnt++;
}
}
logger("[*] Loaded $cnt enteries from persistance: $file\n") if $VERBOSE;
return $key;
}
=head2 save_persistance
Saves the domain hash to a persistance file
=cut
sub save_persistance {
my $hash = shift;
my $cnt = 0;
if ( $PERSISTANCE != 0 ) {
if (open(my $PFH, ">", $PERSFILE)) {
for my $domain ( keys %{$hash} ) {
my $epoch = $hash->{$domain};
print $PFH "$domain,$epoch\n";
$cnt++;
}
} else {
logger("[E] Could not open '$PERSFILE': $!");
}
}
logger("[*] Saved $cnt entries to persistance: $PERSFILE\n") if $VERBOSE;
}
=head2 gameover
Terminates the program in a sainfull way.
=cut
sub gameover {
save_persistance($dkey) if ($PERSISTANCE != 0);
unlink ($PIDFILE) if $DAEMON;
logger("[*] Terminating...\n");
exit 0;
}
=head1 AUTHOR
Edward Fjellskaal <[email protected]>
=head1 COPYRIGHT
Copyright (C) 2013, Edward Fjellskål <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=cut