-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
303 lines (260 loc) · 7.18 KB
/
main.go
File metadata and controls
303 lines (260 loc) · 7.18 KB
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
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"sort"
"strconv"
"strings"
"github.com/freb/go-nmap"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/stealth"
"github.com/integrii/flaggy"
"github.com/maruel/natural"
)
// TODO:
// - create unique id's for each element (image hash?), then leave the src
// blank for each, setting them in a script at the end?:
// https://stackoverflow.com/questions/35149719/html-image-src-call-javascript-variable
// The goal is to combine identical images
// - add text to the images!!!!! https://stackoverflow.com/questions/38299930/how-to-add-a-simple-text-label-to-an-image-in-go
var (
logErr = log.New(os.Stderr, "", log.Flags()&^(log.Ldate|log.Ltime))
logOut = log.New(os.Stdout, "", log.Flags()&^(log.Ldate|log.Ltime)) // todo, add more options and colorize?
)
type Config struct {
NmapXML []string
URLs []string
AllHostnames bool
Bin string
Headless bool
OutFile string
Overwrite bool
IgnoreCerts bool
Trace bool
DisableIncognito bool
Debug bool
}
var conf = &Config{
Headless: true,
OutFile: "http-sshots.html",
IgnoreCerts: true,
}
type Target struct {
Scheme string
Host string
Port int
}
type Targets []*Target
func (t *Targets) Add(targets ...*Target) {
empty := len(*t) == 0
// Only add unique host+port.
for _, target := range targets {
if empty {
*t = append(*t, target)
continue
}
skip := false
for _, tgt := range *t {
if tgt.Host == target.Host && tgt.Port == target.Port {
skip = true
}
}
if !skip {
*t = append(*t, target)
}
}
t.Sort()
}
func (t Targets) Sort() {
sort.SliceStable(t, func(i, j int) bool {
return natural.Less(
fmt.Sprintf("%s:%d", t[i].Host, t[i].Port),
fmt.Sprintf("%s:%d", t[j].Host, t[j].Port),
)
})
}
func (t Target) String() string {
return fmt.Sprintf("%s://%s:%d", t.Scheme, t.Host, t.Port)
}
func targetsFromNmapRun(nr nmap.NmapRun) Targets {
var targets Targets
for _, host := range nr.Hosts {
for _, port := range host.Ports {
if !strings.HasPrefix(port.Service.Name, "http") {
continue
}
scheme := "http"
if port.Service.Tunnel == "ssl" {
scheme = "https"
}
// TODO: Add an option to skip IP if a hostnames is found for it.
// Problem is we can't know if a user specified both a hostname and its
// IP address.
// Add all IPs.
for _, addr := range host.Addresses {
targets.Add(&Target{scheme, addr.Addr, port.PortId})
fmt.Println("JUST ADDED:", targets)
}
for _, hostname := range host.Hostnames {
// Type user indicates the hostname was explicity targeted, always add.
if hostname.Type == "user" {
targets.Add(&Target{scheme, hostname.Name, port.PortId})
continue
}
// Add other hostnames if AllHostnames set and the hostname resolves.
if conf.AllHostnames {
if _, err := net.LookupHost(hostname.Name); err == nil {
targets.Add(&Target{scheme, hostname.Name, port.PortId})
} else if conf.Debug {
fmt.Println("failed to resolve:", hostname.Name)
}
}
}
}
}
return targets
}
func targetsFromURLs(urls []string) Targets {
var targets Targets
for _, t := range urls {
u, err := url.Parse(t)
if err != nil {
logErr.Fatalf("error parsing URL target %s: %v\n", t, err)
}
port := 80
if u.Scheme == "https" {
port = 443
}
if p := u.Port(); p != "" {
if i, err := strconv.Atoi(p); err != nil {
port = i
}
}
targets.Add(&Target{
Scheme: u.Scheme,
Host: u.Hostname(),
Port: port,
})
}
return targets
}
var htmlIntro = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTTP Screenshots</title>
</head>
<body>
`
var htmlOutro = `
</body>
</html>
`
var htmlImage = `
<a href='%s' target=_blank style='font-size:x-large'>
%s
</a>
<br>
<img src='data:image/png;base64,%s' width=400 border=1 style='max-width:1024; margin-top:5px;'
onclick='this.setAttribute("width", this.getAttribute("width") === "400" ? "100%%" : "400")' />
<hr style="margin:20px 0 20px 0">
`
func main() {
flaggy.StringSlice(&conf.NmapXML, "n", "nmap-xml", "Nmap XML file")
flaggy.StringSlice(&conf.URLs, "u", "url", "URL to Screenshot")
flaggy.Bool(&conf.AllHostnames, "a", "all-hostnames", "Use all known hostnames")
flaggy.String(&conf.Bin, "b", "bin", "Path to Chrome binary")
flaggy.Bool(&conf.Headless, "", "headless", "Run Chrome in headless mode")
flaggy.String(&conf.OutFile, "o", "out", "Output file")
flaggy.Bool(&conf.Overwrite, "", "overwrite", "Overwrite output file if it exists")
flaggy.Bool(&conf.IgnoreCerts, "i", "ignore-certs", "Ignore certificate errors")
flaggy.Bool(&conf.Trace, "", "trace", "")
flaggy.Bool(&conf.DisableIncognito, "", "disable-incognito", "")
flaggy.Bool(&conf.Debug, "", "debug", "")
flaggy.Parse()
var targets Targets
if len(conf.NmapXML) > 0 {
for _, nxmlfile := range conf.NmapXML {
file, err := os.Open(nxmlfile)
if err != nil {
logErr.Fatalf("error opening file %s: %v\n", nxmlfile, err)
}
b, err := ioutil.ReadAll(file)
if err != nil {
logErr.Fatalf("error reading file %s: %v\n", nxmlfile, err)
}
nmapRun, err := nmap.Parse(b)
if err != nil {
logErr.Fatalf("error parsing file %s: %v\n", nxmlfile, err)
}
fmt.Println(nmapRun.Targets)
targets.Add(targetsFromNmapRun(*nmapRun)...)
fmt.Println("targets:", targets)
}
}
targets.Add(targetsFromURLs(conf.URLs)...)
if len(targets) == 0 {
logErr.Fatalln("error, no targets specified")
}
if _, err := os.Stat(conf.OutFile); err == nil && !conf.Overwrite {
logErr.Fatalln("file already exists, exiting:", conf.OutFile)
}
fmt.Println("List of targets:")
for _, t := range targets {
fmt.Println(t)
}
fmt.Println()
doc, err := os.OpenFile(conf.OutFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
doc.WriteString(htmlIntro)
defer func() {
doc.WriteString(htmlOutro)
doc.Close()
}()
l := launcher.New()
l = l.Headless(conf.Headless)
// If you don't specify .Bin(), it will download a browser to run for the platform.
if conf.Bin != "" {
l = l.Bin(conf.Bin)
}
u, err := l.Launch()
if err != nil {
logErr.Fatalf("error launching chrome: %v\n", err)
}
browser := rod.New().
ControlURL(u).
MustConnect().
Trace(conf.Trace).
MustIgnoreCertErrors(conf.IgnoreCerts)
if !conf.DisableIncognito {
browser = browser.MustIncognito()
}
defer browser.MustClose()
page := stealth.MustPage(browser)
defer page.MustClose()
for _, t := range targets {
logOut.Printf("capturing: %s", t)
if err := page.Navigate(t.String()); err != nil {
logErr.Printf("error navigating to %s: %v\n", t, err)
continue
}
if err := page.WaitLoad(); err != nil {
logErr.Printf("error waiting to load %s: %v\n", t, err)
continue
}
sshotb, err := page.Screenshot(true, nil)
if err != nil {
logErr.Printf("error capturing: %s: %v\n", t.String(), err)
continue
}
doc.WriteString(fmt.Sprintf(htmlImage, t.String(), t.String(), base64.StdEncoding.EncodeToString(sshotb)))
}
}