forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umbrella.go
542 lines (461 loc) · 14.1 KB
/
umbrella.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// 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 (
"context"
"encoding/json"
"fmt"
"net"
"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/resolvers"
"github.com/OWASP/Amass/v3/stringset"
"github.com/OWASP/Amass/v3/systems"
)
// Umbrella is the Service that handles access to the Umbrella data source.
type Umbrella struct {
requests.BaseService
API *config.APIKey
SourceType string
sys systems.System
}
// NewUmbrella returns he object initialized, but not yet started.
func NewUmbrella(sys systems.System) *Umbrella {
u := &Umbrella{
SourceType: requests.API,
sys: sys,
}
u.BaseService = *requests.NewBaseService(u, "Umbrella")
return u
}
// Type implements the Service interface.
func (u *Umbrella) Type() string {
return u.SourceType
}
// OnStart implements the Service interface.
func (u *Umbrella) OnStart() error {
u.BaseService.OnStart()
u.API = u.sys.Config().GetAPIKey(u.String())
if u.API == nil || u.API.Key == "" {
u.sys.Config().Log.Printf("%s: API key data was not provided", u.String())
}
u.SetRateLimit(500 * time.Millisecond)
return nil
}
// OnDNSRequest implements the Service interface.
func (u *Umbrella) 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 u.API == nil || u.API.Key == "" {
return
}
if !cfg.IsDomainInScope(req.Domain) {
return
}
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("Querying %s for %s subdomains", u.String(), req.Domain))
headers := u.restHeaders()
url := u.restDNSURL(req.Domain)
page, err := http.RequestWebPage(url, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), url, err))
return
}
// Extract the subdomain names from the REST API results
var subs struct {
Matches []struct {
Name string `json:"name"`
} `json:"matches"`
}
if err := json.Unmarshal([]byte(page), &subs); err != nil {
return
}
for _, m := range subs.Matches {
if d := cfg.WhichDomain(m.Name); d != "" {
bus.Publish(requests.NewNameTopic, eventbus.PriorityHigh, &requests.DNSRequest{
Name: m.Name,
Domain: d,
Tag: u.SourceType,
Source: u.String(),
})
}
}
}
// OnAddrRequest implements the Service interface.
func (u *Umbrella) OnAddrRequest(ctx context.Context, req *requests.AddrRequest) {
cfg := ctx.Value(requests.ContextConfig).(*config.Config)
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if cfg == nil || bus == nil {
return
}
if u.API == nil || u.API.Key == "" {
return
}
if req.Address == "" {
return
}
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
headers := u.restHeaders()
url := u.restAddrURL(req.Address)
page, err := http.RequestWebPage(url, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), url, err))
return
}
// Extract the subdomain names from the REST API results
var ip struct {
Records []struct {
Data string `json:"rr"`
} `json:"records"`
}
if err := json.Unmarshal([]byte(page), &ip); err != nil {
return
}
for _, record := range ip.Records {
if name := resolvers.RemoveLastDot(record.Data); name != "" {
if domain := cfg.WhichDomain(name); domain != "" {
bus.Publish(requests.NewNameTopic, eventbus.PriorityHigh, &requests.DNSRequest{
Name: name,
Domain: req.Domain,
Tag: u.SourceType,
Source: u.String(),
})
}
}
}
}
// OnASNRequest implements the Service interface.
func (u *Umbrella) OnASNRequest(ctx context.Context, req *requests.ASNRequest) {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return
}
if u.API == nil || u.API.Key == "" {
return
}
if req.Address == "" && req.ASN == 0 {
return
}
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
if req.Address != "" {
u.executeASNAddrQuery(ctx, req)
return
}
u.executeASNQuery(ctx, req)
}
func (u *Umbrella) executeASNAddrQuery(ctx context.Context, req *requests.ASNRequest) {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return
}
headers := u.restHeaders()
url := u.restAddrToASNURL(req.Address)
page, err := http.RequestWebPage(url, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), url, err))
return
}
// Extract the AS information from the REST API results
var as []struct {
Date string `json:"creation_date"`
Registry int `json:"ir"`
Description string `json:"description"`
ASN int `json:"asn"`
CIDR string `json:"cidr"`
}
if err := json.Unmarshal([]byte(page), &as); err != nil || len(as) == 0 {
return
}
created, err := time.Parse("2006-01-02", as[0].Date)
if err != nil {
return
}
var registry string
switch as[0].Registry {
case 1:
registry = "AfriNIC"
case 2:
registry = "APNIC"
case 3:
registry = "ARIN"
case 4:
registry = "LACNIC"
case 5:
registry = "RIPE NCC"
default:
registry = "N/A"
}
req.ASN = as[0].ASN
req.Prefix = as[0].CIDR
req.Registry = registry
req.AllocationDate = created
req.Description = as[0].Description
req.Tag = u.SourceType
req.Source = u.String()
if req.Netblocks == nil {
req.Netblocks = stringset.New()
req.Netblocks.Insert(strings.TrimSpace(req.Prefix))
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
u.executeASNQuery(ctx, req)
}
bus.Publish(requests.NewASNTopic, eventbus.PriorityHigh, req)
}
func (u *Umbrella) executeASNQuery(ctx context.Context, req *requests.ASNRequest) {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return
}
headers := u.restHeaders()
url := u.restASNToCIDRsURL(req.ASN)
page, err := http.RequestWebPage(url, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), url, err))
return
}
// Extract the netblock information from the REST API results
var netblock []struct {
CIDR string `json:"cidr"`
Geo struct {
CountryName string `json:"country_name"`
CountryCode string `json:"country_code"`
} `json:"geo"`
}
if err := json.Unmarshal([]byte(page), &netblock); err != nil || len(netblock) == 0 {
return
}
if req.Netblocks == nil {
req.Netblocks = stringset.New()
}
for _, nb := range netblock {
req.Netblocks.Insert(strings.TrimSpace(nb.CIDR))
if nb.CIDR == req.Prefix {
req.CC = nb.Geo.CountryCode
}
}
// If no basic AS info exists, then obtain an IP and query
if req.Prefix == "" {
addr, _, err := net.ParseCIDR(netblock[0].CIDR)
if err == nil {
req.Address = addr.String()
req.CC = netblock[0].Geo.CountryCode
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
u.executeASNAddrQuery(ctx, req)
return
}
}
// Finish populating the AS info in the request
for _, nb := range netblock {
if nb.CIDR == req.Prefix {
req.CC = nb.Geo.CountryCode
break
}
}
}
// Umbrella provides much more than this, but we're only interested in these
// fields
type whoisRecord struct {
NameServers []string `json:"nameServers"`
AdminContactEmail string `json:"administrativeContactEmail"`
BillingContactEmail string `json:"billingContactEmail"`
RegistrantEmail string `json:"registrantEmail"`
TechContactEmail string `json:"technicalContactEmail"`
ZoneContactEmail string `json:"zoneContactEmail"`
}
// Umbrella provides the same response for email and ns reverse records. Makes
// the json parsing logic simple since we can use the same structs for both
type rWhoisDomain struct {
Domain string `json:"domain"`
Current bool `json:"current"`
}
type rWhoisResponse struct {
TotalResults int `json:"totalResults"`
MoreData bool `json:"moreDataAvailable"`
Limit int `json:"limit"`
Domains []rWhoisDomain `json:"domains"`
}
func (u *Umbrella) collateEmails(ctx context.Context, record *whoisRecord) []string {
emails := stringset.New()
if u.validateScope(ctx, record.AdminContactEmail) {
emails.InsertMany(record.AdminContactEmail)
}
if u.validateScope(ctx, record.BillingContactEmail) {
emails.InsertMany(record.BillingContactEmail)
}
if u.validateScope(ctx, record.RegistrantEmail) {
emails.InsertMany(record.RegistrantEmail)
}
if u.validateScope(ctx, record.TechContactEmail) {
emails.InsertMany(record.TechContactEmail)
}
if u.validateScope(ctx, record.ZoneContactEmail) {
emails.InsertMany(record.ZoneContactEmail)
}
return emails.Slice()
}
func (u *Umbrella) queryWhois(ctx context.Context, domain string) *whoisRecord {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return nil
}
var whois whoisRecord
headers := u.restHeaders()
whoisURL := u.whoisRecordURL(domain)
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
record, err := http.RequestWebPage(whoisURL, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), whoisURL, err))
return nil
}
err = json.Unmarshal([]byte(record), &whois)
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), whoisURL, err))
return nil
}
return &whois
}
func (u *Umbrella) queryReverseWhois(ctx context.Context, apiURL string) []string {
domains := stringset.New()
headers := u.restHeaders()
var whois map[string]rWhoisResponse
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return domains.Slice()
}
// Umbrella provides data in 500 piece chunks
for count, more := 0, true; more; count = count + 500 {
u.CheckRateLimit()
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, u.String())
fullAPIURL := fmt.Sprintf("%s&offset=%d", apiURL, count)
record, err := http.RequestWebPage(fullAPIURL, nil, headers, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), apiURL, err))
return domains.Slice()
}
err = json.Unmarshal([]byte(record), &whois)
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", u.String(), apiURL, err))
return domains.Slice()
}
more = false
for _, result := range whois {
if result.TotalResults > 0 {
for _, domain := range result.Domains {
if domain.Current {
domains.Insert(domain.Domain)
}
}
}
if result.MoreData && more == false {
more = true
}
}
}
return domains.Slice()
}
func (u *Umbrella) validateScope(ctx context.Context, input string) bool {
cfg := ctx.Value(requests.ContextConfig).(*config.Config)
if cfg == nil {
return false
}
if input != "" && cfg.IsDomainInScope(input) {
return true
}
return false
}
// OnWhoisRequest implements the Service interface.
func (u *Umbrella) OnWhoisRequest(ctx context.Context, req *requests.WhoisRequest) {
cfg := ctx.Value(requests.ContextConfig).(*config.Config)
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if cfg == nil || bus == nil {
return
}
if u.API == nil || u.API.Key == "" {
return
}
if !cfg.IsDomainInScope(req.Domain) {
return
}
whoisRecord := u.queryWhois(ctx, req.Domain)
if whoisRecord == nil {
return
}
domains := stringset.New()
emails := u.collateEmails(ctx, whoisRecord)
if len(emails) > 0 {
emailURL := u.reverseWhoisByEmailURL(emails...)
for _, d := range u.queryReverseWhois(ctx, emailURL) {
if !cfg.IsDomainInScope(d) {
domains.Insert(d)
}
}
}
var nameservers []string
for _, ns := range whoisRecord.NameServers {
if u.validateScope(ctx, ns) {
nameservers = append(nameservers, ns)
}
}
if len(nameservers) > 0 {
nsURL := u.reverseWhoisByNSURL(nameservers...)
for _, d := range u.queryReverseWhois(ctx, nsURL) {
if !cfg.IsDomainInScope(d) {
domains.Insert(d)
}
}
}
if len(domains) > 0 {
bus.Publish(requests.NewWhoisTopic, eventbus.PriorityHigh, &requests.WhoisRequest{
Domain: req.Domain,
NewDomains: domains.Slice(),
Tag: u.SourceType,
Source: u.String(),
})
}
}
func (u *Umbrella) restHeaders() map[string]string {
headers := map[string]string{"Content-Type": "application/json"}
if u.API != nil && u.API.Key != "" {
headers["Authorization"] = "Bearer " + u.API.Key
}
return headers
}
func (u *Umbrella) whoisBaseURL() string {
return `https://investigate.api.umbrella.com/whois/`
}
func (u *Umbrella) whoisRecordURL(domain string) string {
return u.whoisBaseURL() + domain
}
func (u *Umbrella) reverseWhoisByNSURL(ns ...string) string {
nameservers := strings.Join(ns, ",")
return u.whoisBaseURL() + `nameservers?nameServerList=` + nameservers
}
func (u *Umbrella) reverseWhoisByEmailURL(emails ...string) string {
emailQuery := strings.Join(emails, ",")
return u.whoisBaseURL() + `emails?emailList=` + emailQuery
}
func (u *Umbrella) restDNSURL(domain string) string {
return `https://investigate.api.umbrella.com/search/.*[.]` + domain + "?start=-30days&limit=1000"
}
func (u *Umbrella) restAddrURL(addr string) string {
return "https://investigate.api.umbrella.com/pdns/ip/" + addr + "?recordType=A,AAAA"
}
func (u *Umbrella) restAddrToASNURL(addr string) string {
return fmt.Sprintf("https://investigate.api.umbrella.com/bgp_routes/ip/%s/as_for_ip.json", addr)
}
func (u *Umbrella) restASNToCIDRsURL(asn int) string {
return fmt.Sprintf("https://investigate.api.umbrella.com/bgp_routes/asn/%d/prefixes_for_asn.json", asn)
}