Skip to content

Commit

Permalink
Add full page screenshot support.
Browse files Browse the repository at this point in the history
Closes #19. Finally.
  • Loading branch information
leonjza committed Sep 17, 2020
1 parent c906191 commit 6cb4915
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
61 changes: 56 additions & 5 deletions chrome/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ 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"
Expand All @@ -20,6 +23,7 @@ type Chrome struct {
ResolutionY int
UserAgent string
Timeout int64
FullPage bool
}

// NewChrome returns a new initialised Chrome struct
Expand Down Expand Up @@ -125,11 +129,58 @@ func (chrome *Chrome) Screenshot(url *url.URL) ([]byte, error) {
defer cancel()

var buf []byte
if err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(url.String()),
chromedp.CaptureScreenshot(&buf),
}); err != nil {
return nil, 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.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.CaptureScreenshot(&buf),
}); err != nil {
return nil, err
}
}

return buf, nil
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ func init() {
rootCmd.PersistentFlags().IntVarP(&chrm.ResolutionY, "resolution-y", "Y", 900, "screenshot resolution y")
rootCmd.PersistentFlags().StringVar(&chrm.UserAgent, "user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36", "user agent string to use")
rootCmd.PersistentFlags().StringVarP(&options.ScreenshotPath, "screenshot-path", "P", "screenshots", "store path for screenshots (use . for pwd)")
rootCmd.PersistentFlags().BoolVarP(&chrm.FullPage, "fullpage", "F", false, "take fullpage screenshots")
rootCmd.PersistentFlags().Int64Var(&chrm.Timeout, "timeout", 10, "preflight check timeout")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/sensepost/gowitness
go 1.15

require (
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac
github.com/chromedp/chromedp v0.5.3
github.com/corona10/goimagehash v1.0.2
github.com/olekukonko/tablewriter v0.0.4
github.com/remeh/sizedwaitgroup v1.0.0
github.com/rs/zerolog v1.19.0
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect
github.com/spf13/cobra v1.0.0
github.com/tomsteele/go-nmap v0.0.0-20191202052157-3507e0b03523
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down
6 changes: 3 additions & 3 deletions web/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6cb4915

Please sign in to comment.