-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathutility.go
More file actions
417 lines (379 loc) · 12.8 KB
/
Copy pathutility.go
File metadata and controls
417 lines (379 loc) · 12.8 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package zgrab2
import (
"context"
"errors"
"fmt"
"net"
"regexp"
"strconv"
"strings"
"time"
"runtime/debug"
"github.com/sirupsen/logrus"
)
// ReadAvaiable reads what it can without blocking for more than
// defaultReadTimeout per read, or defaultTotalTimeout for the whole session.
// Reads at most defaultMaxReadSize bytes.
func ReadAvailable(conn net.Conn) ([]byte, error) {
const defaultReadTimeout = 10 * time.Millisecond
const defaultMaxReadSize = 1024 * 512
// if the buffer size exactly matches the number of bytes returned, we hit
// a corner case where we attempt to read even though there is nothing
// available. Otherwise we should be able to return without blocking at all.
// So -- it's better to be large than small, but the worst case is getting
// the exact right number of bytes.
const defaultBufferSize = 8209
return ReadAvailableWithOptions(conn, defaultBufferSize, defaultReadTimeout, 0, defaultMaxReadSize)
}
// Make this implement the net.Error interface so that err.(net.Error).SessionTimeout() works.
type errTotalTimeout string
const (
ErrTotalTimeout = errTotalTimeout("timeout")
)
func (err errTotalTimeout) Error() string {
return string(err)
}
func (err errTotalTimeout) Timeout() bool {
return true
}
func (err errTotalTimeout) Temporary() bool {
return false
}
// ReadAvailableWithOptions reads whatever can be read (up to maxReadSize) from
// conn without blocking for longer than readTimeout per read, or totalTimeout
// for the entire session. A totalTimeout of 0 means attempt to use the
// connection's timeout (or, failing that, DefaultSessionTimeout).
// On failure, returns anything it was able to read along with the error.
func ReadAvailableWithOptions(conn net.Conn, bufferSize int, readTimeout time.Duration, totalTimeout time.Duration, maxReadSize int) ([]byte, error) {
// A 0 totalTimeout falls back to the conn's session timeout, or
// DefaultSessionTimeout when the conn carries none.
if totalTimeout == 0 {
totalTimeout = DefaultSessionTimeout
tc, ok := conn.(*TimeoutConnection)
if ok && tc.SessionTimeout > 0 {
totalTimeout = tc.SessionTimeout
}
}
// The first read must wait for the peer's reply, so bound it by
// totalTimeout; readTimeout applies only to the drain reads below.
err := conn.SetReadDeadline(time.Now().Add(totalTimeout))
if err != nil {
return nil, err
}
totalDeadline := time.Now().Add(totalTimeout)
buf := make([]byte, bufferSize)
ret := make([]byte, 0)
n, err := conn.Read(buf[0:min(bufferSize, maxReadSize)])
ret = append(ret, buf[0:n]...)
if err != nil || n >= maxReadSize {
return ret, err
}
maxReadSize -= n
// Drain any remaining buffered data, not blocking longer than readTimeout
// per read, and treat those per-read timeouts as "done", not errors. If
// we hit the totalTimeout, that still counts as a timeout error.
for totalDeadline.After(time.Now()) {
deadline := time.Now().Add(readTimeout)
err = conn.SetReadDeadline(deadline)
if err != nil {
return ret, fmt.Errorf("could not set read deadline on conn: %w", err)
}
n, err := conn.Read(buf[0:min(maxReadSize, bufferSize)])
maxReadSize -= n
ret = append(ret, buf[0:n]...)
if err != nil {
if IsTimeoutError(err) {
err = nil
}
return ret, err
}
if n >= maxReadSize {
return ret, err
}
}
return ret, ErrTotalTimeout
}
var ErrInsufficientBuffer = errors.New("not enough buffer space")
// ReadUntilRegex calls connection.Read() until it returns an error, or the cumulatively-read data matches the given regexp
func ReadUntilRegex(connection net.Conn, res []byte, expr *regexp.Regexp) (int, error) {
buf := res[0:]
length := 0
for finished := false; !finished; {
n, err := connection.Read(buf)
length += n
if err != nil {
return length, err
}
if expr.Match(res[0:length]) {
finished = true
}
if length == len(res) {
return length, ErrInsufficientBuffer
}
buf = res[length:]
}
return length, nil
}
// TLDMatches checks for a strict TLD match
func TLDMatches(host1 string, host2 string) bool {
splitStr1 := strings.Split(stripPortNumber(host1), ".")
splitStr2 := strings.Split(stripPortNumber(host2), ".")
tld1 := splitStr1[len(splitStr1)-1]
tld2 := splitStr2[len(splitStr2)-1]
return tld1 == tld2
}
func stripPortNumber(host string) string {
return strings.Split(host, ":")[0]
}
type timeoutError interface {
Timeout() bool
}
// IsTimeoutError checks if the given error corresponds to a timeout (of any type).
func IsTimeoutError(err error) bool {
if err == nil {
return false
}
if cast, ok := err.(timeoutError); ok {
return cast.Timeout()
}
if cast, ok := err.(*ScanError); ok {
return cast.Status == SCAN_IO_TIMEOUT || cast.Status == SCAN_CONNECTION_TIMEOUT
}
return false
}
// LogPanic is intended to be called from within defer -- if there was no panic, it returns without
// doing anything. Otherwise, it logs the stacktrace, the panic error, and the provided message
// before re-raising the original panic.
// Example:
//
// defer zgrab2.LogPanic("Error decoding body '%x'", body)
func LogPanic(format string, args ...any) {
err := recover()
if err == nil {
return
}
logrus.Errorf("Uncaught panic at %s: %v", string(debug.Stack()), err)
logrus.Errorf(format, args...)
panic(err)
}
// addDefaultPortToDNSServerName validates that the input DNS server address is correct and appends the default DNS port 53 if no port is specified
func addDefaultPortToDNSServerName(inAddr string) (string, error) {
// Try to split host and port to see if the port is already specified.
host, port, err := net.SplitHostPort(inAddr)
if err != nil {
// might mean there's no port specified
host = inAddr
}
// Validate the host part as an IP address.
ip := net.ParseIP(host)
if ip == nil {
return "", errors.New("invalid IP address")
}
// If the original input does not have a port, specify port 53 as the default
if port == "" {
port = defaultDNSPort
}
return net.JoinHostPort(ip.String(), port), nil
}
func parseCustomDNSString(customDNS string) ([]string, error) {
nameservers := make([]string, 0)
customDNS = strings.TrimSpace(customDNS)
if customDNS == "" {
return nil, nil
}
for _, ns := range strings.Split(customDNS, ",") {
ns = strings.TrimSpace(ns)
if ns == "" {
continue
}
nsWithPort, err := addDefaultPortToDNSServerName(ns)
if err != nil {
return nil, fmt.Errorf("invalid DNS server address: %s", ns)
}
nameservers = append(nameservers, nsWithPort)
}
return nameservers, nil
}
// CloseConnAndHandleError closes the connection and logs an error if it fails. Convenience function for code-reuse.
func CloseConnAndHandleError(conn net.Conn) {
conn.Close()
}
// HasCtxExpired checks if the context has expired. Common function used in various places.
func HasCtxExpired(ctx context.Context) bool {
select {
case <-(ctx).Done():
return true
default:
return false
}
}
// extractIPAddresses takes in a slice containing strings of IP addresses, ranges, or CIDR blocks and returns a de-duped
// list of IP addresses, or an error if the string is invalid. Whitespace is trimmed from each address string and the
// ranges are inclusive.
// See config_test.go for examples of valid and invalid strings
func extractIPAddresses(input []string) ([]net.IP, error) {
ipNets, err := extractCIDRRanges(input)
if err != nil {
return nil, fmt.Errorf("could not parse IP address string %v: %w", input, err)
}
ips := make([]net.IP, 0, len(ipNets))
// need to expand the CIDR ranges into IP addresses
for _, ipnet := range ipNets {
for currentIP := ipnet.IP.Mask(ipnet.Mask); ipnet.Contains(currentIP); {
tempIP := duplicateIP(currentIP)
ips = append(ips, tempIP)
incrementIP(currentIP)
if currentIP.Equal(tempIP) {
// our IP is the largest IPv4 or IPv6 addr possible, and has saturated
break
}
}
}
// de-dupe
lookupMap := make(map[string]struct{})
dedupedIPs := make([]net.IP, 0, len(ips))
for _, i := range ips {
ipString := i.String()
if _, ok := lookupMap[ipString]; !ok {
lookupMap[ipString] = struct{}{}
dedupedIPs = append(dedupedIPs, i)
}
}
return dedupedIPs, nil
}
// extractCIDRRanges takes in a slice containing strings of IP addresses, ranges, or CIDR blocks and returns a de-duped
// list of CIDR ranges, or an error if the string is invalid. Whitespace is trimmed from each address string
func extractCIDRRanges(inputs []string) ([]net.IPNet, error) {
ipNets := make([]net.IPNet, 0, len(inputs))
for _, addr := range inputs {
addr = strings.TrimSpace(addr) // remove whitespace
if len(addr) == 0 {
continue // skip empty strings
}
// this addr is either an IP address, ip address range, or a CIDR range
_, ipnet, err := net.ParseCIDR(addr)
if err == nil {
ipNets = append(ipNets, *ipnet)
continue
}
if strings.Contains(addr, "-") {
// IP range
parts := strings.Split(addr, "-")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid IP range %s", addr)
}
parts[0] = strings.TrimSpace(parts[0])
parts[1] = strings.TrimSpace(parts[1])
startIP := net.ParseIP(parts[0])
endIP := net.ParseIP(parts[1])
if startIP == nil {
return nil, fmt.Errorf("invalid start IP %s of IP range", parts[0])
}
if endIP == nil {
return nil, fmt.Errorf("invalid end IP %s of IP range", parts[1])
}
if compareIPs(startIP, endIP) > 0 {
return nil, fmt.Errorf("start IP %s is greater than end IP %s of IP range", startIP.String(), endIP.String())
}
if (startIP.To4() == nil) != (endIP.To4() == nil) {
return nil, fmt.Errorf("start IP %s and end IP %s of IP range are not the same type", startIP.String(), endIP.String())
}
isIPv4 := startIP.To4() != nil
for currentIP := startIP; compareIPs(currentIP, endIP) <= 0; {
tempIP := duplicateIP(currentIP)
if isIPv4 {
ipNets = append(ipNets, net.IPNet{IP: tempIP, Mask: net.CIDRMask(32, 32)})
} else {
ipNets = append(ipNets, net.IPNet{IP: tempIP, Mask: net.CIDRMask(128, 128)})
}
incrementIP(currentIP)
if currentIP.Equal(tempIP) {
// our IP is the largest IPv4 or IPv6 addr possible, and has saturated
break
}
}
continue
}
// single IP
castIP := net.ParseIP(addr)
if castIP == nil {
return nil, fmt.Errorf("could not parse IP address %s", addr)
}
if castIP.To4() != nil {
ipNets = append(ipNets, net.IPNet{IP: castIP, Mask: net.CIDRMask(32, 32)})
} else {
ipNets = append(ipNets, net.IPNet{IP: castIP, Mask: net.CIDRMask(128, 128)})
}
}
// de-dupe
lookupMap := make(map[string]struct{})
dedupedIPNets := make([]net.IPNet, 0, len(ipNets))
for _, i := range ipNets {
str := i.String()
if _, ok := lookupMap[str]; !ok {
lookupMap[str] = struct{}{}
dedupedIPNets = append(dedupedIPNets, i)
}
}
return dedupedIPNets, nil
}
// extractPorts takes in a string of comma-separated ports or port ranges (80-443) and returns a de-duped list of ports
// Whitespace is trimmed from each port string, and the port range is inclusive.
func extractPorts(portString string) ([]uint16, error) {
portMap := make(map[uint16]struct{})
for _, portStr := range strings.Split(portString, ",") {
portStr = strings.TrimSpace(portStr)
if strings.Contains(portStr, "-") {
// port range
parts := strings.Split(portStr, "-")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid port range %s, valid range ex: '80-443'", portStr)
}
startPort, err := parsePortString(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid start port %s of port range: %w", parts[0], err)
}
endPort, err := parsePortString(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid end port %s of port range: %w", parts[1], err)
}
if startPort >= endPort {
return nil, fmt.Errorf("start port %d must be less than end port %d", startPort, endPort)
}
// validation complete, add all ports in range
for i := startPort; i <= endPort; i++ {
portMap[i] = struct{}{}
}
} else {
// single port
port, err := parsePortString(portStr)
if err != nil {
return nil, fmt.Errorf("invalid port %s: %w", portStr, err)
}
portMap[port] = struct{}{}
}
}
// build list from de-duped map
ports := make([]uint16, 0, len(portMap))
for port := range portMap {
ports = append(ports, port)
}
return ports, nil
}
// parsePortString converts a string to a uint16 port number after removing whitespace
// Checks for validity of the port number and returns an error if invalid
func parsePortString(portStr string) (uint16, error) {
minimumPort := uint64(1) // inclusive
maximumPort := uint64(65535) // inclusive
port, err := strconv.ParseUint(strings.TrimSpace(portStr), 10, 16)
if err != nil {
return 0, fmt.Errorf("invalid port %s: %w", portStr, err)
}
if port < minimumPort {
return 0, fmt.Errorf("port %s must be in the range [%d,%d]", portStr, minimumPort, maximumPort)
}
if port > maximumPort {
return 0, fmt.Errorf("port %s must be in the range [%d,%d]", portStr, minimumPort, maximumPort)
}
return uint16(port), nil
}