-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdnsbl_resolver.go
More file actions
166 lines (148 loc) · 3.81 KB
/
Copy pathdnsbl_resolver.go
File metadata and controls
166 lines (148 loc) · 3.81 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
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
package udig
import (
"fmt"
"net"
"sync"
"time"
"github.com/miekg/dns"
)
var defaultDNSBLZones = []string{
"b.barracudacentral.org",
"dnsbl-2.uceprotect.net",
"dnsbl-3.uceprotect.net",
"dnsbl.dronebl.org",
}
/////////////////////////////////////////
// DNSBL RESOLVER
/////////////////////////////////////////
// DNSBLResolver checks IPs against DNS-based blocklists.
type DNSBLResolver struct {
Client *dns.Client
Zones []string
}
// NewDNSBLResolver creates a new DNSBLResolver with sensible defaults.
func NewDNSBLResolver(timeout time.Duration) *DNSBLResolver {
return &DNSBLResolver{
Client: &dns.Client{ReadTimeout: timeout},
Zones: defaultDNSBLZones,
}
}
// ResolveIP checks an IP against all configured DNSBL zones in parallel.
// Returns one Resolution per zone that lists the IP.
func (r *DNSBLResolver) ResolveIP(ip string) []Resolution {
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
LogErr("%s: IP %s is invalid.", TypeDNSBL, ip)
return nil
}
var reverseIP string
if ipAddr.To4() != nil {
reverseIP = reverseIPv4(ipAddr)
} else {
reverseIP = reverseIPv6(ipAddr)
}
var mux sync.Mutex
var wg sync.WaitGroup
var results []Resolution
wg.Add(len(r.Zones))
for _, zone := range r.Zones {
go func(zone string) {
defer wg.Done()
query := fmt.Sprintf("%s.%s", reverseIP, zone)
msg, err := queryOneCallback(query, dns.TypeA, localNameServer, r.Client)
if err != nil {
if err.Error() != "NXDOMAIN" {
LogErr("%s: DNSBL query %s failed: %s", TypeDNSBL, query, err.Error())
}
return
}
for _, rr := range msg.Answer {
if rr.Header().Rrtype != dns.TypeA {
continue
}
returnCode := rr.(*dns.A).A.String()
if isDNSBLMetaCode(returnCode) {
LogWarn("%s: %s returned meta code %s for %s — query limit exceeded or service error", TypeDNSBL, zone, returnCode, ip)
continue
}
mux.Lock()
results = append(results, &DNSBLResolution{
ResolutionBase: &ResolutionBase{query: ip},
Record: DNSBLRecord{
Zone: zone,
ReturnCode: returnCode,
Listed: true,
Meaning: decodeDNSBLMeaning(zone, returnCode),
},
})
mux.Unlock()
}
}(zone)
}
wg.Wait()
return results
}
// Type returns "DNSBL".
func (r *DNSBLResolver) Type() ResolutionType {
return TypeDNSBL
}
// isDNSBLMetaCode reports whether a DNSBL return code is a service meta/error
// response rather than a real listing. DNSBL services use the 127.255.255.0/24
// range for operational signals (must NOT be interpreted as reputation data).
func isDNSBLMetaCode(returnCode string) bool {
ip := net.ParseIP(returnCode)
if ip == nil {
return false
}
ip4 := ip.To4()
return ip4 != nil && ip4[1] == 255 && ip4[2] == 255
}
// decodeDNSBLMeaning decodes the meaning of a DNSBL return code for known zones.
func decodeDNSBLMeaning(zone, returnCode string) string {
switch zone {
case "b.barracudacentral.org":
if returnCode == "127.0.0.2" {
return "listed"
}
case "dnsbl-2.uceprotect.net":
if returnCode == "127.0.0.2" {
return "spam subnet"
}
case "dnsbl-3.uceprotect.net":
if returnCode == "127.0.0.2" {
return "spam ASN"
}
case "dnsbl.dronebl.org":
switch returnCode {
case "127.0.0.3":
return "IRC drone"
case "127.0.0.5":
return "bottler"
case "127.0.0.6":
return "unknown spambot"
case "127.0.0.7":
return "DDoS drone"
case "127.0.0.8":
return "SOCKS proxy"
case "127.0.0.9":
return "HTTP proxy"
case "127.0.0.10":
return "proxy chain"
case "127.0.0.11":
return "web page proxy"
case "127.0.0.13":
return "brute force"
case "127.0.0.14":
return "open Wingate proxy"
case "127.0.0.15":
return "compromised router"
case "127.0.0.16":
return "autorooting worm"
case "127.0.0.17":
return "botnet"
case "127.0.0.18":
return "open resolver"
}
}
return returnCode
}