-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
215 lines (196 loc) · 5.11 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
package main
import (
"fmt"
flags "github.com/jessevdk/go-flags"
"github.com/miekg/dns"
"go.uber.org/atomic"
"go.uber.org/ratelimit"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
)
type queryEngine struct {
limiter ratelimit.Limiter
nameServers []string
client dns.Client
ip net.IP
subnet net.IPNet
output chan *dns.PTR
waitGroup *sync.WaitGroup
attempts int
}
const (
IPv4 = iota
IPv6 = iota
)
var opts struct {
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Rate int `short:"r" default:"600" long:"rate" description:"Queries per second"`
Servers []string `short:"s" long:"server" description:"Nameserver to query"`
Attempts int `short:"a" long:"attempts" default:"5" description:"Number of attempts if timeout"`
}
var stats struct {
queries *atomic.Uint64
results *atomic.Uint64
errored *atomic.Uint64
}
func main() {
args, err := flags.Parse(&opts)
if err != nil {
panic(err)
}
// TODO this could be a lot better
for i, server := range opts.Servers {
if strings.Index(server, ":") == -1 {
opts.Servers[i] = server + ":53"
} else {
opts.Servers[i] = "[" + server + "]" + ":53"
}
}
// Init stat counters
stats.queries = atomic.NewUint64(0)
stats.results = atomic.NewUint64(0)
stats.errored = atomic.NewUint64(0)
var engines []*queryEngine = make([]*queryEngine, len(args))
for i, prefix := range args {
engines[i] = new(queryEngine)
engines[i].initEngine(prefix)
queryString, err := subnetToQuery(&engines[i].subnet)
if err != nil {
panic(err)
}
engines[i].scanNextDivision(queryString)
go printResults(engines[i].output)
}
stderrStat, _ := os.Stderr.Stat()
stdoutStat, _ := os.Stdout.Stat()
if (stderrStat.Mode()&os.ModeCharDevice) != 0 && (stdoutStat.Mode()&os.ModeCharDevice) == 0 {
go printStatus()
}
for _, engine := range engines {
engine.waitGroup.Wait()
}
}
func printStatus() {
for {
fmt.Fprintf(os.Stderr, "\r%d queries, %d found", stats.queries.Load(), stats.results.Load())
time.Sleep(1 * time.Second)
}
}
func printResults(input chan *dns.PTR) {
for {
fmt.Println(<-input)
}
}
func subnetToQuery(subnet *net.IPNet) (string, error) {
var end string
var chunks []string
mask, _ := subnet.Mask.Size()
if subnet.IP.To4() != nil {
end = ".in-addr.arpa."
if mask%8 != 0 {
return "", fmt.Errorf("Subnet must be at the byte level for IPv4\n")
}
chunks = make([]string, 4)
for index, element := range subnet.IP {
chunks[len(chunks)-index-1] = strconv.FormatUint(uint64(element), 10)
}
chunks = chunks[len(chunks)-mask/8 : len(chunks)]
} else {
end = ".ip6.arpa."
if mask%4 != 0 {
return "", fmt.Errorf("Subnet must be at the nibble level for IPv6\n")
}
chunks = make([]string, 32)
for index, element := range subnet.IP {
chunks[len(chunks)-2*index-1] = strconv.FormatUint(uint64((element&0xF0)>>4), 16)
chunks[len(chunks)-2*index-2] = strconv.FormatUint(uint64(element&0x0F), 16)
}
// Now we shorten it to the appropriate mask
chunks = chunks[len(chunks)-mask/4 : len(chunks)]
}
return strings.Join(chunks, ".") + end, nil
}
/*func ptrToIP(ptr string) string {
var output *strings.Builder = new(strings.Builder)
if ptr[len(ptr)-10:] == ".ip6.arpa." {
//IPv6
} else { //IPv4
}
}*/
func (engine *queryEngine) initEngine(prefix string) {
// parse prefix
ip, subnet, err := net.ParseCIDR(prefix)
if err != nil {
panic(err)
}
engine.ip = ip
engine.subnet = *subnet
engine.limiter = ratelimit.New(opts.Rate)
engine.nameServers = opts.Servers
engine.output = make(chan *dns.PTR, 10)
engine.attempts = opts.Attempts
engine.waitGroup = new(sync.WaitGroup)
engine.waitGroup.Add(1)
}
func (engine *queryEngine) selectServer() string {
return engine.nameServers[rand.Intn(len(engine.nameServers))]
}
func (engine *queryEngine) scanNextDivision(query string) {
var err error
engine.limiter.Take()
message := new(dns.Msg)
message.SetQuestion(query, dns.TypePTR)
message.RecursionDesired = true
var msg *dns.Msg
// TODO randomize nameserver
for i := 0; i < engine.attempts; i++ {
msg, err = dns.Exchange(message, engine.selectServer())
stats.queries.Inc()
if err == nil {
break
}
}
if err != nil {
panic(err)
}
if msg.Rcode == dns.RcodeSuccess {
// Did we find something here?
if len(msg.Answer) > 0 {
for _, answer := range msg.Answer {
switch ptrAnswer := answer.(type) {
case *dns.PTR:
stats.results.Inc()
engine.output <- ptrAnswer
}
}
// Now leave
} else {
// Nothing here, but there is a record further down
if engine.subnet.IP.To4() != nil {
engine.waitGroup.Add(256)
// Use 0-255
for i := 0; i <= 255; i++ {
go engine.scanNextDivision(strconv.FormatUint(uint64(i), 10) + "." + query)
}
} else {
engine.waitGroup.Add(16)
// use 0-f
for i := 0; i <= 0xf; i++ {
go engine.scanNextDivision(strconv.FormatUint(uint64(i), 16) + "." + query)
}
}
}
} else if msg.Rcode == dns.RcodeNameError {
// Nothing further down this path, abort
} else {
// This was not expected
log.Println(msg.String())
}
engine.waitGroup.Done()
}