forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsdb.go
140 lines (116 loc) · 3.17 KB
/
dnsdb.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
// Copyright 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package datasrcs
import (
"bufio"
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/OWASP/Amass/v3/config"
"github.com/OWASP/Amass/v3/eventbus"
"github.com/OWASP/Amass/v3/net/http"
"github.com/OWASP/Amass/v3/requests"
"github.com/OWASP/Amass/v3/stringset"
"github.com/OWASP/Amass/v3/systems"
)
// DNSDB is the Service that handles access to the DNSDB data source.
type DNSDB struct {
requests.BaseService
API *config.APIKey
SourceType string
sys systems.System
}
// NewDNSDB returns he object initialized, but not yet started.
func NewDNSDB(sys systems.System) *DNSDB {
d := &DNSDB{
SourceType: requests.SCRAPE,
sys: sys,
}
d.BaseService = *requests.NewBaseService(d, "DNSDB")
return d
}
// Type implements the Service interface.
func (d *DNSDB) Type() string {
return d.SourceType
}
// OnStart implements the Service interface.
func (d *DNSDB) OnStart() error {
d.BaseService.OnStart()
d.API = d.sys.Config().GetAPIKey(d.String())
if d.API == nil || d.API.Key == "" {
d.sys.Config().Log.Printf("%s: API key data was not provided", d.String())
}
d.SetRateLimit(2 * time.Minute)
return nil
}
// OnDNSRequest implements the Service interface.
func (d *DNSDB) OnDNSRequest(ctx context.Context, req *requests.DNSRequest) {
cfg := ctx.Value(requests.ContextConfig).(*config.Config)
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if cfg == nil || bus == nil {
return
}
if !cfg.IsDomainInScope(req.Domain) {
return
}
if d.API == nil || d.API.Key == "" {
return
}
d.CheckRateLimit()
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("Querying %s for %s subdomains", d.String(), req.Domain))
headers := map[string]string{
"X-API-Key": d.API.Key,
"Accept": "application/json",
"Content-Type": "application/json",
}
url := d.getURL(req.Domain)
page, err := http.RequestWebPage(url, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", d.String(), url, err))
return
}
for _, name := range d.parse(ctx, page, req.Domain) {
bus.Publish(requests.NewNameTopic, eventbus.PriorityHigh, &requests.DNSRequest{
Name: name,
Domain: req.Domain,
Tag: requests.API,
Source: d.String(),
})
}
}
func (d *DNSDB) getURL(domain string) string {
return fmt.Sprintf("https://api.dnsdb.info/lookup/rrset/name/*.%s", domain)
}
func (d *DNSDB) parse(ctx context.Context, page, domain string) []string {
cfg := ctx.Value(requests.ContextConfig).(*config.Config)
if cfg == nil {
return []string{}
}
re := cfg.DomainRegex(domain)
if re == nil {
return []string{}
}
unique := stringset.New()
scanner := bufio.NewScanner(strings.NewReader(page))
for scanner.Scan() {
// Get the next line of JSON
line := scanner.Text()
if line == "" {
continue
}
var j struct {
Name string `json:"rrname"`
}
err := json.Unmarshal([]byte(line), &j)
if err != nil {
continue
}
if re.MatchString(j.Name) {
unique.Insert(j.Name)
}
}
return unique.Slice()
}