-
Notifications
You must be signed in to change notification settings - Fork 27
/
dns_blocker
executable file
·154 lines (140 loc) · 3.88 KB
/
dns_blocker
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
#!/usr/bin/perl
#Not working for subdomains
#TODO: use cache
#DO it parallely
use strict;
use warnings;
use 5.010;
use Net::DNS::Nameserver;
use Net::DNS ();
use Domain::PublicSuffix qw( );
sub reply_handler {
my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;
my ($rcode, @ans, @auth, @add);
print "Received query [ $qname ] from $peerhost\n";
#$query->print;
if ($qtype eq "A") {
my $res = Net::DNS::Resolver->new;
my $res_ip = "0.0.0.0";
##foreach my $name_server ('8.8.8.8', '208.67.222.222', '8.26.56.26', '199.85.126.30') { #TODO: move to a file
foreach my $name_server ('8.26.56.26', '199.85.126.30', '8.8.8.8') { #TODO: move to a file
print "\n --------- $name_server -------------\n";
$res->nameservers($name_server);
# 0) extract domain from subdomain (if any)
state $parser = Domain::PublicSuffix->new();
my $root = $parser->get_root_domain( $qname );
if($root) {
print "ROOT found: $root \n";
} else {
print "ERROR: Root Domain NOT found.\n";
$root = $qname;
}
# 1) get NS from domain:
#my $nserv = qx("dig +short -t ns $qname | head -n 1");
my $nserv;
$query = $res->query($root, "NS");
if($query) {
foreach my $rr (grep { $_->type eq 'NS' } $query->answer) {
$nserv = $rr->nsdname;
print "NS found: $nserv\n";
last;
}
}
if(!$nserv) {
print "ERROR: NameServer NOT found.\n";
next;
}
my $query = $res->search($qname);
my $result;
if ($query) {
foreach my $rr ($query->answer) {
if ($rr->type eq "A") {
$result = $rr->address;
print "FOUND: $result\n";
last;
}
}
}
if ($result) {
# = The returning IP address must match $result
# 2) get HOSTNAME of IP:
my $target = join('.', reverse split(/\./, $result)).".in-addr.arpa";
$query = $res->query($target, "PTR");
if($query) {
my $host;
foreach my $rr (grep { $_->type eq 'PTR' } $query->answer) {
$host = $rr->rdatastr;
print "HOST found: $host\n";
last;
}
if(!$host) {
print "ERROR: Host NOT found for $result .\n";
next;
}
# 3) query that NS for the specific host: (other way: host $qname $nserv)
#my $ip = qx("dig +short \@$nserv $qname");
# use the nameserver now
my $query = $res->search($nserv);
my $nserv_ip;
if ($query) {
foreach my $rr (grep { $_->type eq 'A' } $query->answer) {
$nserv_ip = $rr->address;
print "NS Address: $nserv_ip \n";
last;
}
}
if(!$nserv) {
print "ERROR: NS $nserv IP NOT found.\n";
next;
}
my $res2 = Net::DNS::Resolver->new;
$res2->nameservers($nserv_ip);
$query = $res2->search($host);
if ($query) {
foreach my $rr (grep { $_->type eq 'A' } $query->answer) {
$res_ip = $rr->address;
print "IP found: $res_ip\n";
last;
}
}
if($res_ip eq "0.0.0.0") {
print "ERROR: HOSTNAME $host has no IP address.\n";
next;
}
printf "%-20s", $name_server;
say $result;
if($res_ip ne $result) {
print "ERROR: $res_ip AND $result DON'T match.\n";
next;
}
} else {
print "ERROR: $result DON'T resolves to a host.\n";
next;
}
} else {
print "ERROR: Domain NOT found.\n";
next;
}
}
if ($res_ip ne "0.0.0.0") {
my ($ttl, $rdata) = (3600, $res_ip);
my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
push @ans, $rr;
print "NOERROR\n";
$rcode = "NOERROR";
} else {
print "NXDOMAIN\n";
$rcode = "NXDOMAIN";
}
} else {
$rcode = "NOTIMP";
}
# mark the answer as authoritive (by setting the 'aa' flag
return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
}
my $ns = new Net::DNS::Nameserver(
LocalPort => 53,
ReplyHandler => \&reply_handler,
Verbose => 0
) || die "couldn't create nameserver object\n";
$ns->main_loop;