-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstopdnsrebind.go
More file actions
92 lines (73 loc) · 1.92 KB
/
stopdnsrebind.go
File metadata and controls
92 lines (73 loc) · 1.92 KB
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
package stopdnsrebind
import (
"context"
"net"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/nonwriter"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
type Stopdnsrebind struct {
Next plugin.Handler
AllowList []string
DenyList []net.IPNet
}
// ServeDNS implements the plugin.Handler interface.
func (a Stopdnsrebind) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
//ignore if on the allow list
for _, allowed := range a.AllowList {
if allowed == state.QName() {
return plugin.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
}
nw := nonwriter.New(w)
rcode, err := plugin.NextOrFailure(a.Name(), a.Next, ctx, nw, r)
if err != nil {
return rcode, err
}
for _, ans := range nw.Msg.Answer {
var ip net.IP
switch ans.Header().Rrtype {
case dns.TypeA:
ip = ans.(*dns.A).A
case dns.TypeAAAA:
ip = ans.(*dns.AAAA).AAAA
default:
//we only care about A and AAA
continue
}
/*
🚀 Default blocking rules:
🔒 Loopback Addresses: 127.0.0.1/8
🔒 Private Addresses:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
🔒 Link Local Addresses: 169.254.0.0/16
🔒 Unspecified: 0.0.0.0
🔒 Interface Local Multicast: 224.0.0.0/24
🔒 DenyList: Add your entries in the plugin configuration
// Keeping the network secure!
*/
if !ip.IsGlobalUnicast() || ip.IsInterfaceLocalMulticast() ||
ip.IsPrivate() || shouldDeny(ip, a.DenyList) {
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeRefused)
w.WriteMsg(m)
return dns.RcodeSuccess, nil
}
}
w.WriteMsg(nw.Msg)
return 0, nil
}
func shouldDeny(ip net.IP, denyList []net.IPNet) bool {
for _, ipNetDenied := range denyList {
if ipNetDenied.Contains(ip) {
return true
}
}
return false
}
// Name implements the Handler interface.
func (a Stopdnsrebind) Name() string { return "stopdnsrebind" }