forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources.go
257 lines (221 loc) · 6.07 KB
/
sources.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
// 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 sources
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/OWASP/Amass/amass/internal/utils"
"github.com/PuerkitoBio/fetchbot"
"github.com/PuerkitoBio/goquery"
)
const (
ARCHIVE = "archive"
API = "api"
CERT = "cert"
SCRAPE = "scrape"
)
// All data sources are handled through this interface in amass
type DataSource interface {
// Returns subdomain names from the data source
Query(domain, sub string) []string
// Sets the logger to be used by this data source
SetLogger(l *log.Logger)
// Returns the data source's associated organization
String() string
// Returns true if the data source supports subdomain name searches
Subdomains() bool
// Returns one of the types defined above in the constants
Type() string
}
// The common functionalities and default behaviors for all data sources
// Most of the base methods are not implemented by each data source
type BaseDataSource struct {
SourceType string
Organization string
logger *log.Logger
}
func NewBaseDataSource(stype, org string) *BaseDataSource {
return &BaseDataSource{
SourceType: stype,
Organization: org,
}
}
// Place holder that get implemented by each data source
func (bds *BaseDataSource) Query(domain, sub string) []string {
return []string{}
}
func (bds *BaseDataSource) Type() string {
return bds.SourceType
}
// If a data source supports searching on subdomains,
// this get implemented by the data source and returns true
func (bds *BaseDataSource) Subdomains() bool {
return false
}
func (bds *BaseDataSource) SetLogger(l *log.Logger) {
bds.logger = l
}
func (bds *BaseDataSource) String() string {
return bds.Organization
}
// All data sources send log messages through this method
func (bds *BaseDataSource) log(msg string) {
if bds.logger == nil {
return
}
bds.logger.Printf("%s: %s", bds.Organization, msg)
}
//-------------------------------------------------------------------------------------------------
// Web archive crawler implementation
//-------------------------------------------------------------------------------------------------
func (bds *BaseDataSource) crawl(base, domain, sub string) ([]string, error) {
var results []string
var filterMutex sync.Mutex
filter := make(map[string]struct{})
year := strconv.Itoa(time.Now().Year())
mux := fetchbot.NewMux()
links := make(chan string, 50)
names := make(chan string, 50)
linksFilter := make(map[string]struct{})
mux.HandleErrors(fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) {
bds.log(fmt.Sprintf("Crawler error: %s %s - %v", ctx.Cmd.Method(), ctx.Cmd.URL(), err))
}))
mux.Response().Method("GET").ContentType("text/html").Handler(fetchbot.HandlerFunc(
func(ctx *fetchbot.Context, res *http.Response, err error) {
filterMutex.Lock()
defer filterMutex.Unlock()
u := res.Request.URL.String()
if _, found := filter[u]; found {
return
}
filter[u] = struct{}{}
bds.linksAndNames(domain, ctx, res, links, names)
}))
f := fetchbot.New(fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) {
mux.Handle(ctx, res, err)
}))
setFetcherConfig(f)
q := f.Start()
u := fmt.Sprintf("%s/%s/%s", base, year, sub)
if _, err := q.SendStringGet(u); err != nil {
return results, fmt.Errorf("Crawler error: GET %s - %v", u, err)
}
t := time.NewTimer(10 * time.Second)
loop:
for {
select {
case l := <-links:
if _, ok := linksFilter[l]; ok {
continue
}
linksFilter[l] = struct{}{}
q.SendStringGet(l)
case n := <-names:
results = utils.UniqueAppend(results, n)
case <-t.C:
go func() {
q.Cancel()
}()
case <-q.Done():
close(names)
break loop
}
}
// Makes sure all the names were collected
for name := range names {
results = utils.UniqueAppend(results, name)
}
return results, nil
}
func (bds *BaseDataSource) linksAndNames(domain string, ctx *fetchbot.Context, res *http.Response, links, names chan string) {
// Process the body to find the links
doc, err := goquery.NewDocumentFromResponse(res)
if err != nil {
bds.log(fmt.Sprintf("Crawler error: %s %s - %s\n", ctx.Cmd.Method(), ctx.Cmd.URL(), err))
return
}
re := utils.SubdomainRegex(domain)
doc.Find("a[href]").Each(func(i int, s *goquery.Selection) {
val, _ := s.Attr("href")
// Resolve address
u, err := ctx.Cmd.URL().Parse(val)
if err != nil {
bds.log(fmt.Sprintf("Crawler failed to parse: %s - %v\n", val, err))
return
}
if sub := re.FindString(u.String()); sub != "" {
names <- sub
links <- u.String()
}
})
}
func setFetcherConfig(f *fetchbot.Fetcher) {
f.HttpClient = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
DialContext: utils.DialContext,
MaxIdleConns: 200,
IdleConnTimeout: 5 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
ExpectContinueTimeout: 5 * time.Second,
},
}
f.CrawlDelay = 1 * time.Second
f.DisablePoliteness = true
f.UserAgent = utils.USER_AGENT
}
//-------------------------------------------------------------------------------------------------
func GetAllSources() []DataSource {
return []DataSource{
NewArchiveIt(),
NewArchiveToday(),
NewArquivo(),
NewAsk(),
NewBaidu(),
NewCensys(),
NewCertDB(),
NewCertSpotter(),
NewCommonCrawl(),
NewCrtsh(),
NewDNSDB(),
NewDNSDumpster(),
NewDogpile(),
NewEntrust(),
NewExalead(),
NewFindSubdomains(),
NewGoogle(),
NewHackerTarget(),
NewIPv4Info(),
NewLoCArchive(),
NewNetcraft(),
NewOpenUKArchive(),
NewPTRArchive(),
NewRiddler(),
NewRobtex(),
NewSiteDossier(),
NewThreatCrowd(),
NewUKGovArchive(),
NewVirusTotal(),
NewWaybackMachine(),
NewYahoo(),
}
}
func removeAsteriskLabel(s string) string {
var index int
labels := strings.Split(s, ".")
for i := len(labels) - 1; i >= 0; i-- {
if strings.TrimSpace(labels[i]) == "*" {
break
}
index = i
}
if index == len(labels)-1 {
return ""
}
return strings.Join(labels[index:], ".")
}