forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crtsh.go
158 lines (132 loc) · 3.98 KB
/
crtsh.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
// 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"
"time"
"github.com/OWASP/Amass/v3/eventbus"
"github.com/OWASP/Amass/v3/net/dns"
"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"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq" // Need the postgres driver
)
// Crtsh is the Service that handles access to the Crtsh data source.
type Crtsh struct {
requests.BaseService
SourceType string
sys systems.System
db *sqlx.DB
haveConnection bool
}
// NewCrtsh returns he object initialized, but not yet started.
func NewCrtsh(sys systems.System) *Crtsh {
c := &Crtsh{
SourceType: requests.CERT,
sys: sys,
haveConnection: true,
}
c.BaseService = *requests.NewBaseService(c, "Crtsh")
return c
}
// Type implements the Service interface.
func (c *Crtsh) Type() string {
return c.SourceType
}
// OnStart implements the Service interface.
func (c *Crtsh) OnStart() error {
c.BaseService.OnStart()
var err error
c.db, err = sqlx.Connect("postgres", "host=crt.sh user=guest dbname=certwatch sslmode=disable")
if err != nil {
c.sys.Config().Log.Printf("%s: Failed to connect to the database server: %v", c.String(), err)
c.haveConnection = false
}
c.SetRateLimit(time.Second)
return nil
}
// OnDNSRequest implements the Service interface.
func (c *Crtsh) OnDNSRequest(ctx context.Context, req *requests.DNSRequest) {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return
}
c.CheckRateLimit()
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("Querying %s for %s subdomains", c.String(), req.Domain))
// Fall back to scraping the web page if the database connection failed
if !c.haveConnection {
c.scrape(ctx, req.Domain)
return
}
c.executeQuery(ctx, req.Domain)
}
func (c *Crtsh) executeQuery(ctx context.Context, domain string) {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return
}
var results []struct {
Domain string `db:"domain"`
}
pattern := "%." + domain
err := c.db.Select(&results,
`SELECT DISTINCT ci.NAME_VALUE as domain
FROM certificate_identity ci
WHERE reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower($1))
ORDER BY ci.NAME_VALUE`, pattern)
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("%s: Query pattern %s: %v", c.String(), pattern, err))
return
}
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, c.String())
// Extract the subdomain names from the results
names := stringset.New()
for _, result := range results {
names.Insert(dns.RemoveAsteriskLabel(result.Domain))
}
for name := range names {
bus.Publish(requests.NewNameTopic, eventbus.PriorityHigh, &requests.DNSRequest{
Name: name,
Domain: domain,
Tag: c.SourceType,
Source: c.String(),
})
}
}
func (c *Crtsh) scrape(ctx context.Context, domain string) {
bus := ctx.Value(requests.ContextEventBus).(*eventbus.EventBus)
if bus == nil {
return
}
url := c.getURL(domain)
page, err := http.RequestWebPage(url, nil, nil, "", "")
if err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", c.String(), url, err))
return
}
bus.Publish(requests.SetActiveTopic, eventbus.PriorityCritical, c.String())
// Extract the subdomain names from the results
var results []struct {
Name string `json:"name_value"`
}
if err := json.Unmarshal([]byte(page), &results); err != nil {
return
}
for _, line := range results {
bus.Publish(requests.NewNameTopic, eventbus.PriorityHigh, &requests.DNSRequest{
Name: line.Name,
Domain: domain,
Tag: c.SourceType,
Source: c.String(),
})
}
}
func (c *Crtsh) getURL(domain string) string {
return "https://crt.sh/?q=%25." + domain + "&output=json"
}