forked from sensepost/gowitness
/
chrome.go
227 lines (192 loc) 路 5.65 KB
/
chrome.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
package chrome
import (
"context"
"crypto/tls"
"math"
"net/http"
"net/url"
"strings"
"time"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/sensepost/gowitness/storage"
"gorm.io/gorm"
)
// Chrome contains information about a Google Chrome
// instance, with methods to run on it.
type Chrome struct {
ResolutionX int
ResolutionY int
UserAgent string
Timeout int64
Delay int
FullPage bool
ChromePath string
Proxy string
}
// NewChrome returns a new initialised Chrome struct
func NewChrome() *Chrome {
return &Chrome{}
}
// Preflight will preflight a url
func (chrome *Chrome) Preflight(url *url.URL) (resp *http.Response, title string, err error) {
// purposefully ignore bad certs
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
}
if chrome.Proxy != "" {
var erri error
proxyURL, erri := url.Parse(chrome.Proxy)
if erri != nil {
return nil, "", erri
}
transport.Proxy = http.ProxyURL(proxyURL)
}
// purposefully ignore bad certs
client := http.Client{
Transport: transport,
}
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return
}
req.Header.Set("User-Agent", chrome.UserAgent)
req.Close = true
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(chrome.Timeout)*time.Second)
defer cancel()
req = req.WithContext(ctx)
resp, err = client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
title, _ = GetHTMLTitle(resp.Body)
return
}
// StorePreflight will store preflight info to a DB
func (chrome *Chrome) StorePreflight(url *url.URL, db *gorm.DB, resp *http.Response, title string, filename string) (uint, error) {
record := &storage.URL{
URL: url.String(),
FinalURL: resp.Request.URL.String(),
ResponseCode: resp.StatusCode,
ResponseReason: resp.Status,
Proto: resp.Proto,
ContentLength: resp.ContentLength,
Filename: filename,
Title: title,
}
// append headers
for k, v := range resp.Header {
hv := strings.Join(v, ", ")
record.AddHeader(k, hv)
}
// get TLS info, if any
if resp.TLS != nil {
record.TLS = storage.TLS{
Version: resp.TLS.Version,
ServerName: resp.TLS.ServerName,
}
for _, cert := range resp.TLS.PeerCertificates {
tlsCert := &storage.TLSCertificate{
SubjectCommonName: cert.Subject.CommonName,
IssuerCommonName: cert.Issuer.CommonName,
SignatureAlgorithm: cert.SignatureAlgorithm.String(),
PubkeyAlgorithm: cert.PublicKeyAlgorithm.String(),
}
for _, name := range cert.DNSNames {
tlsCert.AddDNSName(name)
}
record.TLS.TLSCertificates = append(record.TLS.TLSCertificates, *tlsCert)
}
}
db.Create(record)
return record.ID, nil
}
// Screenshot takes a screenshot of a URL and saves it to destination
// Ref:
// https://github.com/chromedp/examples/blob/255873ca0d76b00e0af8a951a689df3eb4f224c3/screenshot/main.go
func (chrome *Chrome) Screenshot(url *url.URL) ([]byte, error) {
// setup chromedp default options
options := []chromedp.ExecAllocatorOption{}
options = append(options, chromedp.DefaultExecAllocatorOptions[:]...)
options = append(options, chromedp.UserAgent(chrome.UserAgent))
options = append(options, chromedp.DisableGPU)
options = append(options, chromedp.Flag("ignore-certificate-errors", true)) // RIP shittyproxy.go
options = append(options, chromedp.WindowSize(chrome.ResolutionX, chrome.ResolutionY))
if chrome.ChromePath != "" {
options = append(options, chromedp.ExecPath(chrome.ChromePath))
}
if chrome.Proxy != "" {
options = append(options, chromedp.ProxyServer(chrome.Proxy))
}
actx, acancel := chromedp.NewExecAllocator(context.Background(), options...)
ctx, cancel := chromedp.NewContext(actx)
defer acancel()
defer cancel()
var buf []byte
// squash JavaScript dialog boxes such as alert();
chromedp.ListenTarget(ctx, func(ev interface{}) {
if _, ok := ev.(*page.EventJavascriptDialogOpening); ok {
go func() {
if err := chromedp.Run(ctx,
page.HandleJavaScriptDialog(true),
); err != nil {
panic(err)
}
}()
}
})
if chrome.FullPage {
// straight from: https://github.com/chromedp/examples/blob/255873ca0d76b00e0af8a951a689df3eb4f224c3/screenshot/main.go#L54
if err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(url.String()),
chromedp.Sleep(time.Duration(chrome.Delay) * time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
// get layout metrics
_, _, contentSize, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
width, height := int64(math.Ceil(contentSize.Width)),
int64(math.Ceil(contentSize.Height))
// force viewport emulation
err = emulation.SetDeviceMetricsOverride(width, height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).Do(ctx)
if err != nil {
return err
}
// capture screenshot
buf, err = page.CaptureScreenshot().
WithQuality(100).
WithClip(&page.Viewport{
X: contentSize.X,
Y: contentSize.Y,
Width: contentSize.Width,
Height: contentSize.Height,
Scale: 2,
}).Do(ctx)
if err != nil {
return err
}
return nil
}),
}); err != nil {
return nil, err
}
} else {
// normal viewport screenshot
if err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(url.String()),
chromedp.Sleep(time.Duration(chrome.Delay) * time.Second),
chromedp.CaptureScreenshot(&buf),
}); err != nil {
return nil, err
}
}
return buf, nil
}