-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
262 lines (241 loc) · 6.13 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
. "github.com/truekonrads/danglingcname/dnsdb"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
)
import "errors"
var DNSSERVER string = "8.8.8.8:53"
func lookupCNAME(target string, server string) (result []string, ok bool) {
return lookupRecord(target, server, dns.TypeCNAME)
}
func lookupA(target string, server string) (result []string, ok bool) {
return lookupRecord(target, server, dns.TypeA)
}
func lookupRecord(target string, server string, qtype uint16) (result []string, ok bool) {
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(target, qtype)
m.RecursionDesired = true
r, _, err := c.Exchange(m, server)
if err != nil {
log.Errorf("Unble to resolve: %s, %v", target, err)
return nil, false
}
if r.Rcode != dns.RcodeSuccess {
return nil, true
}
for _, a := range r.Answer {
h := a.Header()
if h.Rrtype == qtype {
if x, ok := a.(*dns.CNAME); ok {
result = append(result, x.Target)
}
if x, ok := a.(*dns.A); ok {
result = append(result, x.A.String())
}
}
}
return result, true
}
type CRTRecord struct {
Issuer_ca_id uint
Issuer_name string
Name_value string
Id uint
Entry_timestamp string
Not_before string
Not_after string
}
type ProcessingResult struct {
Name string
CNAMEPointsTo []string
Error error
ARecords []string
}
func processname(name string) ProcessingResult {
var res ProcessingResult
res.Name = name
if !strings.HasSuffix(name, ".") {
name = name + "."
}
cname, ok := lookupCNAME(name, DNSSERVER)
//fmt.Println(cname, ok)
if ok == false {
s := fmt.Sprintf("Can't process CNAME lookup for %v\n", name)
res.Error = errors.New(s)
return res
//panic(err)
}
if len(cname) > 0 {
// it's a CNAME that points to something
res.CNAMEPointsTo = cname
a, ok := lookupA(name, DNSSERVER)
if ok {
res.ARecords = a
} else {
s := fmt.Sprintf("Can't process A lookup for %v\n", name)
res.Error = errors.New(s)
return res
}
}
return res
}
func worker(in chan string, out chan ProcessingResult) {
for name := range in {
out <- processname(name)
}
}
func main() {
targetDomain := flag.String("domain", "", "Target domain.")
dnsServer := flag.String("server", DNSSERVER, "DNS Server. (Optional)")
numWorkers := flag.Int("workers", 5, "Number of workers (Optional)")
useDNSDB := flag.Bool("dnsdb", false, "Use DNSDB (set DNSDB_KEY env var) (Optional)")
usecrt := flag.Bool("usecrtsh", false, "Use crt.sh as source")
sourcefile := flag.String("sourcefile", "", "Specify a source file to read DNS records, one per line (Optional)")
debug := flag.Bool("debug", false, "Debug mode")
// jsonFile := flag.String("jsonfile", "", "JSON file from which to read results (Optional)")
flag.Parse()
// if *targetDomain == "" {
// flag.PrintDefaults()
// os.Exit(1)
// }
if *debug {
log.SetLevel(log.DebugLevel)
log.Debug("Debugging on")
}
targetMap := make(map[string]bool)
DNSSERVER = *dnsServer
var body []byte
var err error
var dnsdb_records []RRSetAnswer
var records []CRTRecord
var wg sync.WaitGroup
var rwg sync.WaitGroup
//log.Debug(fmt.Sprintf("Targeting %v\n", *targetDomain))
//fmt.Println("useDNSDB: ", *useDNSDB)
//fmt.Println("usecrt: ", *usecrt)
if *useDNSDB {
if *targetDomain == "" {
fmt.Println("Please specify a domain!")
flag.PrintDefaults()
os.Exit(1)
}
log.Debug(fmt.Sprintf("Loading data from DNSDB\n"))
key := os.Getenv("DNSDB_KEY")
if len(key) == 0 {
fmt.Println("No DNSDB_KEY found")
return
}
dnsc := DNSDBClient{ApiKey: key}
dnsdb_records, err = dnsc.Lookup(fmt.Sprintf("*.%s", *targetDomain), "CNAME")
if err != nil {
fmt.Println("DNSDB", err)
return
}
for _, r := range dnsdb_records {
var rrname string
if strings.HasSuffix(r.RRName, ".") {
rrname = r.RRName[:len(r.RRName)-1]
} else {
rrname = r.RRName
}
targetMap[rrname] = true
}
log.Debug(fmt.Sprintf("Total %v records received from dnsdb\n", len(dnsdb_records)))
}
if *usecrt {
if *targetDomain == "" {
log.Println("Please specify a domain!")
flag.PrintDefaults()
os.Exit(1)
}
url := fmt.Sprintf("https://crt.sh/?q=%s&output=json", *targetDomain)
log.Debugf("Sucesfully fetched from crt.sh %s\n", *targetDomain)
var doc *http.Response
if doc, err = http.Get(url); err != nil {
fmt.Println(err)
return
}
body, err = ioutil.ReadAll(doc.Body)
records = make([]CRTRecord, 0)
err = json.Unmarshal(body, &records)
if err != nil {
fmt.Println(err)
return
}
for i := 0; i < len(records); i++ {
nv := strings.Split(records[i].Name_value, " ")
// fmt.Println(nv)
for _, cert_values := range nv {
for _, s := range strings.Split(cert_values, "\n") {
targetMap[s] = true
}
}
}
log.Debug(fmt.Sprintf("Total %v records received from crt.sh\n", len(records)))
}
if len(*sourcefile) > 0 {
var fh *os.File
if fh, err = os.Open(*sourcefile); err != nil {
log.Errorf("Can't read/open file '%s': %v", *sourcefile, err)
return
}
i := 0
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
targetMap[scanner.Text()] = true
i += 1
}
log.Debugf("Total %v records received from %s\n", i, *sourcefile)
}
//fmt.Println(records)
queue := make(chan string)
results := make(chan ProcessingResult)
// recmap := make(map[string]bool)
// counter := 0
for i := 0; i < *numWorkers; i++ {
go func() {
wg.Add(1)
worker(queue, results)
wg.Done()
}()
}
go func() {
rwg.Add(1)
defer rwg.Done()
for res := range results {
if len(res.CNAMEPointsTo) > 0 {
if len(res.ARecords) > 0 {
log.Debugf("%v: is a CNAME and points to %v and resolves to %v\n",
res.Name,
strings.Join(res.CNAMEPointsTo, ", "),
strings.Join(res.ARecords, ", "),
)
} else {
log.Infof("%v: is a CNAME and points to %v and resolves to nothing (%v)\n",
res.Name,
strings.Join(res.CNAMEPointsTo, ", "),
strings.Join(res.ARecords, ", "),
)
}
}
}
}()
for key, _ := range targetMap {
queue <- key
}
close(queue)
wg.Wait()
close(results)
rwg.Wait()
}