-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
202 lines (168 loc) · 4.46 KB
/
lib.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
package crtsher
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"io"
"math/rand"
"net/http"
"net/url"
"strings"
"time"
"dario.cat/mergo"
"github.com/root4loot/goutils/log"
)
type Runner struct {
Options *Options
Results chan Result
Visited map[string]bool
}
type Options struct {
Concurrency int
Timeout int
Delay int
DelayJitter int
UserAgent string
Debug bool
HTTPClient *http.Client
}
type Results struct {
Results []Result
}
type Result struct {
Query string `json:"query"`
Error error `json:"error"`
IssuerCaID int `json:"issuer_ca_id"`
IssuerName string `json:"issuer_name"`
CommonName string `json:"common_name"`
NameValue string `json:"name_value"`
ID int64 `json:"id"`
EntryTimestamp string `json:"entry_timestamp"`
NotBefore string `json:"not_before"`
NotAfter string `json:"not_after"`
SerialNumber string `json:"serial_number"`
}
var seen map[string]bool
func init() {
log.Init("crtsher")
}
func DefaultOptions() *Options {
const timeout = 90 * time.Second
return &Options{
Concurrency: 3,
Timeout: int(timeout.Seconds()),
Delay: 2,
UserAgent: "crtsher",
Debug: false,
HTTPClient: &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
ResponseHeaderTimeout: timeout,
},
Timeout: timeout,
},
}
}
func NewRunner() *Runner {
return newRunner(nil)
}
func NewRunnerWithOptions(options *Options) *Runner {
return newRunner(options)
}
func NewRunnerWithDefaultOptions() *Runner {
return newRunner(DefaultOptions())
}
func newRunner(options *Options) *Runner {
defaultOptions := DefaultOptions()
if options != nil {
err := mergo.Merge(options, defaultOptions)
if err != nil {
log.Fatal(err)
}
} else {
options = defaultOptions
}
if options.Debug {
log.SetLevel(log.DebugLevel)
}
return &Runner{
Results: make(chan Result),
Visited: make(map[string]bool),
Options: options,
}
}
func (r *Result) GetCommonName() (domain string) {
domain = strings.Trim(r.CommonName, "*.")
u, _ := url.Parse("http://" + domain)
return u.Hostname()
}
func (r *Result) GetMatchingIdentity() (domain string) {
domain = strings.Trim(r.NameValue, "*.")
u, _ := url.Parse("http://" + domain)
return u.Hostname()
}
func (r *Runner) Query(target string) (results []Result) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(r.Options.Timeout)*time.Second)
defer cancel()
log.Infof("Querying %s", target)
endpoint := "https://crt.sh/?q=" + url.QueryEscape(target) + "&output=json"
seen = make(map[string]bool)
maxRetries := 5
retryDelay := time.Duration(r.Options.Timeout) * time.Second
for attempt := 0; attempt < maxRetries; attempt++ {
log.Debugf("Attempting request to endpoint: %s (attempt %d)", endpoint, attempt+1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
log.Errorf("%v", err.Error())
return nil
}
if r.Options.UserAgent != "" {
req.Header.Add("User-Agent", r.Options.UserAgent)
}
log.Debug("Sending HTTP request")
resp, err := r.Options.HTTPClient.Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Errorf("timeout exceeded (%s) - retrying in %v", endpoint, retryDelay)
time.Sleep(retryDelay)
continue
} else {
log.Warnf("%v - %s", err.Error(), target)
return nil
}
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusTooManyRequests {
log.Errorf("too many requests - retrying in %v (consider lowering concurrency)", retryDelay)
time.Sleep(retryDelay)
continue
}
if resp.StatusCode == http.StatusBadGateway {
log.Errorf("bad gateway (%s) - retrying in %v", endpoint, retryDelay)
time.Sleep(retryDelay)
continue
}
log.Debug("Reading response body")
bodyBytes, _ := io.ReadAll(resp.Body)
_ = json.Unmarshal(bodyBytes, &results)
for i := range results {
if !seen[results[i].CommonName] {
seen[results[i].CommonName] = true
results[i].Query = target
results = append(results, results[i])
}
}
time.Sleep(r.getDelay())
return results
}
log.Errorf("failed to get a successful response after %d attempts", maxRetries)
return nil
}
func (r *Runner) getDelay() time.Duration {
if r.Options.DelayJitter != 0 {
return time.Duration(r.Options.Delay + rand.Intn(r.Options.DelayJitter))
}
return time.Duration(r.Options.Delay)
}