File: hellfire.go

package info (click to toggle)
hellfire 0.0~git20180708.bf3c390-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 96 kB
  • sloc: makefile: 2
file content (258 lines) | stat: -rw-r--r-- 5,929 bytes parent folder | download
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
package hellfire // import "pathspider.net/hellfire"

import (
	"encoding/json"
	"fmt"
	"net"
	"strings"
	"sync"
	"time"
)

type LookupQueryResult struct {
	attempts int
	result   []net.IP
}

func prepareTestList(testListOptions string) TestList {
	var testList TestList

	options := strings.Split(testListOptions, ";")

	if len(options) < 3 {
		return nil
	}

	if options[0] == "topsites" {
		testList = new(AlexaTopsitesList)
	} else if options[0] == "cisco" {
		testList = new(CiscoUmbrellaList)
	} else if options[0] == "citizenlab" {
		testList = new(CitizenLabCountryList)
		if options[1] != "" {
			testList.(*CitizenLabCountryList).SetCountry(options[1])
		}
	} else if options[0] == "opendns" {
		testList = new(OpenDNSList)
		if options[1] != "" {
			testList.(*OpenDNSList).SetListName(options[1])
		}
	}

	if options[2] != "" {
		if options[0] == "csv" {
			testList = CSVListFromFile(options[2])
		} else if options[0] == "txt" {
			testList = CSVListFromFile(options[2])
			testList.(*CSVList).SetHeader([]string{"domain"})
		} else if testList != nil {
			testList.SetFilename(options[2])
		}
	}

	if testList == nil {
		panic("Could not initialise a test list!")
	}

	return testList
}

func makeQuery(domain string, lookupType string) LookupQueryResult {
	result := []net.IP{}
	domains := []string{}
	lookupAttempt := 1

	//BUG(irl): Need to add support for MX lookups
	//BUG(irl): Need to add support for SRV lookups
	if lookupType == "host" {
		domains = append(domains, domain)
	} else if lookupType == "ns" {
		var nss []*net.NS
		for {
			nss, _ = net.LookupNS(domain)
			if len(nss) == 0 {
				time.Sleep(1)
			} else {
				break
			}
			lookupAttempt++
			if lookupAttempt == 4 {
				lookupAttempt = 3
				break
			}
		}
		for _, ns := range nss {
			domains = append(domains, ns.Host)
		}
	} else if lookupType == "mx" {
		var nss []*net.MX
		for {
			nss, _ = net.LookupMX(domain)
			if len(nss) == 0 {
				time.Sleep(1)
			} else {
				break
			}
			lookupAttempt++
			if lookupAttempt == 4 {
				lookupAttempt = 3
				break
			}
		}
		for _, ns := range nss {
			domains = append(domains, ns.Host)
		}
	}

	for _, d := range domains {
		var ips []net.IP
		for {
			ips, _ = net.LookupIP(d)
			if len(ips) == 0 {
				time.Sleep(1)
			} else {
				break
			}
			lookupAttempt++
			if lookupAttempt == 4 {
				lookupAttempt = 3
				break
			}
		}
		result = append(result, ips...)
	}
	return LookupQueryResult{lookupAttempt, result}
}

func lookupWorker(id int, lookupWaitGroup *sync.WaitGroup,
	jobs chan map[string]interface{},
	results chan map[string]interface{},
	lookupType string,
	canidAddress string,
	rateLimiter <-chan time.Time) {

	lookupWaitGroup.Add(1)

	go func(id int,
		lookupWaitGroup *sync.WaitGroup,
		jobs chan map[string]interface{},
		results chan map[string]interface{},
		lookupType string,
		canidAddress string) {
		defer lookupWaitGroup.Done()
		for job := range jobs {
			if job["domain"] == nil {
				jobs <- make(map[string]interface{})
				break
			}

			// wait for a tick
			<-rateLimiter

			lookupResult := makeQuery(job["domain"].(string),
				lookupType)
			job["hellfire_lookup_attempts"] = lookupResult.attempts
			job["hellfire_lookup_type"] = lookupType
			for _, ip := range lookupResult.result {
				thisResult := make(map[string]interface{})
				for key, value := range job {
					thisResult[key] = value
				}
				thisResult["ips"] = []net.IP{ip}
				if canidAddress != "" {
					thisResult["canid_info"] = GetAdditionalInfo(ip, canidAddress)
				}
				results <- thisResult
			}
		}
	}(id, lookupWaitGroup, jobs, results, lookupType, canidAddress)
}

func outputPrinter(outputWaitGroup *sync.WaitGroup, results chan map[string]interface{}, outputType string) {
	outputWaitGroup.Add(1)
	go func(results chan map[string]interface{}) {
		defer outputWaitGroup.Done()
		for {
			result := <-results
			if result["domain"] == nil {
				break
			}
			if outputType == "all" {
				b, _ := json.Marshal(result)
				fmt.Println(string(b))
			} else if outputType == "individual" {
				ips := result["ips"]
				if ips == nil {
					continue
				}
				delete(result, "ips")
				for _, ipo := range ips.([]net.IP) {
					ip := ipo.String()
					result["dip"] = ip
					b, _ := json.Marshal(result)
					fmt.Println(string(b))
					delete(result, "dip")
				}
			} else if outputType == "oneeach" {
				found4 := false
				found6 := false
				ips := result["ips"].([]net.IP)
				delete(result, "ips")
				for _, ipo := range ips {
					ip := ipo.String()
					if strings.Contains(ip, ".") {
						if found4 {
							continue
						} else {
							found4 = true
						}
					} else {
						if found6 {
							continue
						} else {
							found6 = true
						}
					}
					result["dip"] = ip
					b, _ := json.Marshal(result)
					fmt.Println(string(b))
					delete(result, "dip")
				}
			}
		}
	}(results)
}

func PerformLookups(testListOptions string, lookupType string, outputType string, canidAddress string, queriesPerSecond int) {
	var lookupWaitGroup sync.WaitGroup
	var outputWaitGroup sync.WaitGroup

	jobs := make(chan map[string]interface{}, 1)
	results := make(chan map[string]interface{})
	testList := prepareTestList(testListOptions)

	// Create a rate limiting ticker
	rateLimiter := time.Tick(time.Second / time.Duration(queriesPerSecond))

	// Spawn lookup workers
	for i := 0; i < 300; i++ {
		lookupWorker(i, &lookupWaitGroup, jobs, results, lookupType, canidAddress, rateLimiter)
	}

	// Spawn output printer
	outputPrinter(&outputWaitGroup, results, outputType)

	// Submit jobs
	testList.FeedJobs(jobs)
	jobs <- make(map[string]interface{})
	lookupWaitGroup.Wait()
	<-jobs // Read last shutdown sentinel from the queue left by the
	// final worker to exit
	// https://blog.golang.org/pipelines - This is a better way
	close(jobs)

	// Shutdown the output printer
	results <- make(map[string]interface{})
	outputWaitGroup.Wait()
	close(results)
}