-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
189 lines (159 loc) · 3.54 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
package main
import (
"flag"
"fmt"
"github.com/miekg/dns"
"os"
"strings"
"time"
)
var (
client = dns.Client{
Timeout: 4 * time.Second,
}
address = "1.1.1.1"
zone = ""
flagSet = flag.NewFlagSet("walk", flag.ExitOnError)
fullWalk = flagSet.Bool("f", false, "Do a full zone walk")
start = flagSet.String("s", "", "Start walk with this owner name")
port = flagSet.String("p", "53", "Specify port number")
usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "Usage: %s [@nameserver] [options] zone\n", os.Args[0])
fmt.Fprintf(w, "[options]:\n")
flagSet.PrintDefaults()
}
)
func main() {
parseArgs(os.Args[1:])
zone := dns.Fqdn(zone)
next := nextName(zone, true)
if *start != "" {
next = nextName(*start, false)
}
for {
res, err := query(next, dns.TypeDS)
if err != nil {
fmt.Fprintf(os.Stderr, "query: %s %s\n", next, dns.TypeToString[dns.TypeDS])
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
nsecRecords := extractRRSet(res.Ns, dns.TypeNSEC)
if len(nsecRecords) == 0 {
fmt.Fprintf(os.Stderr, "No NSEC records found in authority section:\n%v", res)
os.Exit(1)
}
nsec := nsecRecords[0].(*dns.NSEC)
// likely using NSEC black lies
// https://blog.cloudflare.com/black-lies/
if strings.HasPrefix(nsec.NextDomain, "\\000.\\000") {
fmt.Fprintf(os.Stderr, "This zone is likely using online signing\n\n%v\n", res)
os.Exit(1)
}
if *fullWalk {
lookupBitmap(nsec)
} else {
fmt.Printf("%s %s\n", nsec.NextDomain, bitmapToString(nsec))
}
if nsec.NextDomain == zone {
break
}
next = nextName(nsec.NextDomain, false)
}
}
func lookupBitmap(nsec *dns.NSEC) {
for _, t := range nsec.TypeBitMap {
r, err := query(nsec.Header().Name, t)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
for _, rr := range r.Answer {
fmt.Println(rr)
}
// glue records
if t == dns.TypeNS && len(r.Answer) == 0 && r.Rcode == dns.RcodeSuccess {
for _, rr := range r.Ns {
fmt.Println(rr)
}
for _, rr := range r.Extra {
if rr.Header().Rrtype != dns.TypeOPT {
fmt.Println(rr)
}
}
}
}
}
func bitmapToString(rr *dns.NSEC) string {
s := ""
for _, t := range rr.TypeBitMap {
s += " " + dns.Type(t).String()
}
return s
}
func nextName(qname string, first bool) string {
if qname == "." {
return "\\000."
}
if first {
return "\\000." + qname
}
labels := dns.SplitDomainName(qname)
labels[0] = labels[0] + "\\000"
return dns.Fqdn(strings.Join(labels, "."))
}
func query(qname string, qtype uint16) (r *dns.Msg, err error) {
retry := 0
for {
m := new(dns.Msg)
m.RecursionDesired = true
m.AuthenticatedData = true
m.SetQuestion(dns.Fqdn(qname), qtype)
m.SetEdns0(4096, true)
r, _, err = client.Exchange(m, address+":"+*port)
if err == nil || retry > 3 {
return
}
time.Sleep(time.Second * 2)
retry++
}
}
func extractRRSet(rrs []dns.RR, qtype uint16) []dns.RR {
var set []dns.RR
for _, r := range rrs {
if r.Header().Rrtype == qtype {
set = append(set, r)
}
}
return set
}
func parseArgs(args []string) {
flagSet.Usage = usage
var flags []string
needVal := false
for _, arg := range args {
if len(arg) < 2 {
continue
}
switch arg[0] {
case '@':
address = arg[1:]
case '-':
// -f is the only flag that doesn't need a value
needVal = arg[1] != 'f'
flags = append(flags, arg)
default:
if needVal {
flags = append(flags, arg)
needVal = false
continue
}
zone = arg
}
}
if zone == "" {
flagSet.Usage()
os.Exit(1)
}
flagSet.Parse(flags)
}