-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathfile.go
More file actions
223 lines (188 loc) · 4.54 KB
/
file.go
File metadata and controls
223 lines (188 loc) · 4.54 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
package readers
import (
"bufio"
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"github.com/sensepost/gowitness/internal/islazy"
)
// FileReader is a reader that expects a file with targets that
// is newline delimited.
type FileReader struct {
Options *FileReaderOptions
}
// FileReaderOptions are options for the file reader
type FileReaderOptions struct {
Source string
NoHTTP bool
NoHTTPS bool
Ports []int
PortsSmall bool
PortsMedium bool
PortsLarge bool
Random bool
}
// NewFileReader prepares a new file reader
func NewFileReader(opts *FileReaderOptions) *FileReader {
return &FileReader{
Options: opts,
}
}
// Read from a file that contains targets.
// FilePath can be "-" indicating that we should read from stdin.
func (fr *FileReader) Read(ch chan<- string) error {
defer close(ch)
var file *os.File
var err error
if fr.Options.Source == "-" {
file = os.Stdin
} else {
file, err = os.Open(fr.Options.Source)
if err != nil {
return err
}
defer file.Close()
}
// determine any ports
ports := fr.ports()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
candidate := scanner.Text()
if candidate == "" {
continue
}
for _, url := range fr.urlsFor(candidate, ports) {
ch <- url
}
}
return scanner.Err()
}
// urlsFor returns URLs for a scanning candidate.
//
// For candidates with no protocol, (and none of http/https is ignored), the
// method will return two urls.
// If any ports configuration exists, those will also be added as candidates.
func (fr *FileReader) urlsFor(candidate string, ports []int) []string {
var urls []string
// trim any spaces
candidate = strings.TrimSpace(candidate)
// check if we got a scheme, add
hasScheme := strings.Contains(candidate, "://")
if !hasScheme {
candidate = "http://" + candidate
}
parsedURL, err := url.Parse(candidate)
if err != nil {
// invalid url, return empty slice
return urls
}
hasPort := parsedURL.Port() != ""
hostname := parsedURL.Hostname()
// if hostname is not set we may have rubbish input. try and "fix" it
if hostname == "" {
// is it hostname/path?
if idx := strings.Index(candidate, "/"); idx != -1 {
parsedURL.Host = candidate[:idx]
parsedURL.Path = candidate[idx:]
hostname = parsedURL.Hostname()
} else {
// its just a hostname then?
parsedURL.Host = candidate
parsedURL.Path = ""
hostname = candidate
}
// at this point if hostname is still "", then just skip it entirely
if hostname == "" {
return urls
}
}
if hasScheme && hasPort {
// return the candidate as is
urls = append(urls, decodeLastHashtag(parsedURL.String()))
return urls
}
// determine schemes to apply
var schemes []string
if hasScheme {
schemes = append(schemes, parsedURL.Scheme)
} else {
if !fr.Options.NoHTTP {
schemes = append(schemes, "http")
}
if !fr.Options.NoHTTPS {
schemes = append(schemes, "https")
}
}
// determine ports to use
var targetPorts []int
if hasPort {
port, err := strconv.Atoi(parsedURL.Port())
if err == nil { // just ignore it
targetPorts = append(targetPorts, port)
}
} else {
// If no port is specified, use the provided ports
targetPorts = ports
}
// generate the urls
for _, scheme := range schemes {
for _, port := range targetPorts {
host := hostname
if port != 0 {
if isIPv6(hostname) {
host = fmt.Sprintf("[%s]:%d", hostname, port)
} else {
host = fmt.Sprintf("%s:%d", hostname, port)
}
}
fullURL := url.URL{
Scheme: scheme,
Host: host,
Path: parsedURL.Path,
Fragment: parsedURL.Fragment,
}
urls = append(urls, decodeLastHashtag(fullURL.String()))
}
}
return urls
}
// ports returns all of the ports to scan
func (fr *FileReader) ports() []int {
var ports = fr.Options.Ports
if fr.Options.PortsSmall {
ports = append(ports, small...)
}
if fr.Options.PortsMedium {
ports = append(ports, medium...)
}
if fr.Options.PortsLarge {
ports = append(ports, large...)
}
return islazy.UniqueIntSlice(ports)
}
func isIPv6(hostname string) bool {
return len(hostname) > 0 && hostname[0] == '[' && hostname[len(hostname)-1] == ']'
}
// it's for vhosts screenshooting. ## may be encoded
func decodeLastHashtag(url string) string {
i := strings.LastIndex(url, "%23%23")
pad := 0
if i == -1 {
i = strings.LastIndex(url, "#%23")
pad = 4
} else {
pad = 6
}
if i != -1 {
_url := url[:i] + "##" + url[i+pad:]
ipaddrIndex := strings.LastIndex(_url, "##") + 2
ipaddr := net.ParseIP(_url[ipaddrIndex:])
if ipaddr != nil {
return _url
}
}
return url
}