-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
323 lines (273 loc) · 8.51 KB
/
main.go
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
package main
import (
"fmt"
"net"
"os"
"strings"
"sync"
valid "github.com/asaskevich/govalidator"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("dnsbl_checker", "All-in-one DNSBL checker written in Go using every publicly known DNSBL.")
ip4Cmd = app.Command("ip", "checks IPv4 address against DNSBLs")
cfgWhitelist = app.Flag("whitelist", "Check whitelists instead of blacklists").Bool()
cfgVerbose = app.Flag("verbose", "More verbose output. Output will include misses, timeouts and failures.").Bool()
cfgExclude = app.Flag("exclude", "List of DNSBLs to exclude from the check. This flag can be specified multiple times.").PlaceHolder("bl.example.com").Strings()
cfgThreads = app.Flag("threads", "number of concurrent checks between 1 (min) and 1000 (max)").Default("10").Int()
cfgIP4 = ip4Cmd.Arg("ip", "IP address to check").Required().String()
// ip6Cmd = app.Command("ip6", "checks IPv6 address against DNSBLs")
// cfgIP6 = ip6Cmd.Arg("ip", "IP address to check").Required().String()
domainCmd = app.Command("domain", "checks a domain against DNSBLs")
cfgDomain = domainCmd.Arg("domain", "domain name to check").Required().String()
version = "0.2"
ErrWrongResponse = fmt.Errorf("RBL returned a response outside of 127.0.0.0/8 subnet")
ErrRBLPositiveFail = fmt.Errorf("RBL failed positive check")
ErrRBLNegativeFail = fmt.Errorf("RBL failed negative check")
ErrRBLFail = fmt.Errorf("RBL failed both checks")
)
// ListItem is a struct with list details
type ListItem struct {
// Name is the name of the list
Name string
// Address is the hostname used for checking
Address string
// IP4 is true if this list is used for checking IP4 addresses
IP4 bool
// IP6 is true if this list is used for checking IP4 addresses
IP6 bool
// Domain is true if this list is used for checking domains
Domain bool
// Blacklist is true if this list is a blacklist
Blacklist bool
// Whitelist is true if this list is a whitelist
Whitelist bool
}
type workUnit struct {
// address is the hostname used for checking
address string
// listItem is the ListItem
listItem *ListItem
counterHits chan bool
counterMisses chan bool
counterTimeouts chan bool
counterFailures chan bool
lookupFunc func(string, *ListItem) (bool, error)
}
func main() {
app.Version(version)
ks := kingpin.MustParse(app.Parse(os.Args[1:]))
allLists := parseCVS()
filteredLists := []*ListItem{}
// create filteredLists by removing excluded lists from allLists
if len(*cfgExclude) >= 1 {
for _, vAll := range allLists {
if isStringInSlice(vAll.Address, *cfgExclude) {
continue
}
filteredLists = append(filteredLists, vAll)
}
} else {
filteredLists = allLists
}
switch ks {
case ip4Cmd.FullCommand():
if !valid.IsIPv4(*cfgIP4) {
app.FatalUsage("You have not supplied a valid IP4 address.")
}
CheckIP4(*cfgWhitelist, *cfgIP4, filteredLists)
// case ip6Cmd.FullCommand():
// if !valid.IsIPv6(*cfgIP6) {
// app.FatalUsage("You have not supplied a valid IP6 address.")
// }
// CheckIP6(*cfgWhitelist, *cfgIP6, filteredLists)
case domainCmd.FullCommand():
if !valid.IsDNSName(*cfgDomain) {
app.FatalUsage("You have not supplied a valid domain name.")
}
CheckDomain(*cfgWhitelist, *cfgDomain, filteredLists)
}
}
// CheckIP4 .
func CheckIP4(whitelist bool, ip string, allLists []*ListItem) {
lists := []*ListItem{}
for _, v := range allLists {
if v.IP4 && v.Whitelist == whitelist {
lists = append(lists, v)
}
}
runChecks(ip, lists, lookupIP4)
}
// CheckDomain .
func CheckDomain(whitelist bool, domain string, allLists []*ListItem) {
lists := []*ListItem{}
for _, v := range allLists {
if v.Domain && v.Whitelist == whitelist {
lists = append(lists, v)
}
}
runChecks(domain, lists, lookupDomain)
}
// lookupIP4 returns true if `ip` is listed, false otherwise
func lookupIP4(ip string, list *ListItem) (bool, error) {
// check RBL health before using it
if err := checkIP4Health(list.Address); err != nil {
return false, err
}
stringyIP := strings.Split(ip, ".")
addr := stringyIP[3] + "." + stringyIP[2] + "." + stringyIP[1] + "." + stringyIP[0] + "." + list.Address
ips, err := net.LookupIP(addr)
if err != nil {
return false, err
}
if len(ips) > 0 {
_, subNet, _ := net.ParseCIDR("127.0.0.0/8")
if !subNet.Contains(ips[0]) {
return false, ErrWrongResponse
}
return true, nil
}
return false, nil
}
// lookupDomain returns true if `domain` is listed, false otherwise
func lookupDomain(domain string, list *ListItem) (bool, error) {
// check RBL health before using it
if !checkDomainHealth(list.Address) {
fmt.Printf("%v : ERROR\n", list.Address)
return false, fmt.Errorf("RBL is not working properly")
}
addrs, err := net.LookupHost(domain + "." + list.Address)
if err != nil {
return false, err
}
if len(addrs) > 0 {
return true, nil
}
return false, nil
}
func worker(wg *sync.WaitGroup, ch chan *workUnit, done chan bool) {
wg.Add(1)
for {
select {
case <-done:
wg.Done()
return
case wu := <-ch:
result, err := wu.lookupFunc(wu.address, wu.listItem)
if err != nil {
var out string
if strings.HasSuffix(err.Error(), "no such host") {
out = fmt.Sprintf("%v : MISS\n", wu.listItem.Address)
wu.counterMisses <- true
} else if strings.HasSuffix(err.Error(), "i/o timeout") {
out = fmt.Sprintf("%v : TIMEOUT\n", wu.listItem.Address)
wu.counterTimeouts <- true
} else {
out = fmt.Sprintf("%v : FAILURE: %v\n", wu.listItem.Address, err)
wu.counterFailures <- true
}
if *cfgVerbose {
fmt.Printf("%v", out)
}
}
if result {
fmt.Printf("%v : HIT\n", wu.listItem.Address)
wu.counterHits <- true
}
}
}
}
func runChecks(address string, lists []*ListItem, lookupFunc func(string, *ListItem) (bool, error)) {
wg := &sync.WaitGroup{}
counterHits := make(chan bool, len(lists))
counterMisses := make(chan bool, len(lists))
counterTimeouts := make(chan bool, len(lists))
counterFailures := make(chan bool, len(lists))
counterChecks := 0
workChan := make(chan *workUnit)
workDone := make(chan bool)
for i := 1; i <= *cfgThreads; i++ {
go worker(wg, workChan, workDone)
}
for _, listItem := range lists {
counterChecks++
wu := &workUnit{
address: address,
listItem: listItem,
counterFailures: counterFailures,
counterHits: counterHits,
counterMisses: counterMisses,
counterTimeouts: counterTimeouts,
lookupFunc: lookupFunc,
}
workChan <- wu
}
close(workDone)
wg.Wait()
numListed := len(counterHits)
fmt.Printf("------------------------------------------------\n")
fmt.Printf("Result: %v checks performed. %v hits, %v misses, %v timeouts, %v failures\n", counterChecks, len(counterHits), len(counterMisses), len(counterTimeouts), len(counterFailures))
if numListed > 0 && !*cfgWhitelist {
os.Exit(2)
}
os.Exit(0)
}
// isStringInSlice returns true if `needle` is in `haystrack`
func isStringInSlice(needle string, haystrack []string) bool {
for _, v := range haystrack {
if needle == v {
return true
}
}
return false
}
// checkIP4Health returns true if `list` is healthy. Returns false otherwise.
// Test specification: https://tools.ietf.org/html/rfc5782#page-7
func checkIP4Health(list string) error {
testNegative := func(list string) bool {
ips, err := net.LookupHost("1.0.0.127" + "." + list)
if len(ips) == 0 && strings.HasSuffix(err.Error(), "no such host") {
return true
}
return false
}
testPositive := func(list string) bool {
ips, err := net.LookupHost("2.0.0.127" + "." + list)
if len(ips) > 0 && err == nil {
return true
}
return false
}
negResult := testNegative(list)
posResult := testPositive(list)
if !negResult && !posResult {
return ErrRBLFail
} else if negResult && !posResult {
return ErrRBLPositiveFail
} else if !negResult && posResult {
return ErrRBLNegativeFail
}
return nil
}
// checkDomainHealth returns true if `list` is healthy. Returns false otherwise.
// Test specification: https://tools.ietf.org/html/rfc5782#page-7
func checkDomainHealth(list string) bool {
testNegative := func(list string) bool {
ips, err := net.LookupHost("INVALID" + "." + list)
if len(ips) == 0 && strings.HasSuffix(err.Error(), "no such host") {
return true
}
return false
}
testPositive := func(list string) bool {
ips, err := net.LookupHost("TEST" + "." + list)
if len(ips) > 0 && err == nil {
return true
}
return false
}
if testNegative(list) && testPositive(list) {
return true
}
return false
}