Skip to content

Commit 66eec62

Browse files
committed
(fix) allow file parser to parse <host>:<port> targets
1 parent 4f4cbe4 commit 66eec62

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ PLATFORMS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 linux/arm windows
1313
CGO := CGO_ENABLED=0
1414

1515
# Default target
16-
default: clean frontend build integrity
16+
default: clean test frontend build integrity
1717

1818
# Clean up build artifacts
1919
clean:
@@ -29,6 +29,10 @@ frontend: check-npm
2929
check-npm:
3030
@command -v npm >/dev/null 2>&1 || { echo >&2 "npm is not installed. Please install npm first."; exit 1; }
3131

32+
# Run any tests
33+
test:
34+
@echo "Running tests..."
35+
go test ./...
3236

3337
# Build for all platforms
3438
build: $(PLATFORMS)

pkg/readers/file.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/sensepost/gowitness/internal/islazy"
12-
"github.com/sensepost/gowitness/pkg/log"
1312
)
1413

1514
// FileReader is a reader that expects a file with targets that
@@ -80,14 +79,18 @@ func (fr *FileReader) Read(ch chan<- string) error {
8079
// If any ports configuration exists, those will also be added as candidates.
8180
func (fr *FileReader) urlsFor(candidate string, ports []int) []string {
8281
var urls []string
82+
// check if we got a scheme, add
83+
hasScheme := strings.Contains(candidate, "://")
84+
if !hasScheme {
85+
candidate = "http://" + candidate
86+
}
8387

8488
parsedURL, err := url.Parse(candidate)
8589
if err != nil {
8690
// invalid url, return empty slice
8791
return urls
8892
}
8993

90-
hasScheme := parsedURL.Scheme != ""
9194
hasPort := parsedURL.Port() != ""
9295
hostname := parsedURL.Hostname()
9396

@@ -107,7 +110,6 @@ func (fr *FileReader) urlsFor(candidate string, ports []int) []string {
107110

108111
// at this point if hostname is still "", then just skip it entirely
109112
if hostname == "" {
110-
log.Debug("could not parse candidate to something usable", "candidate", candidate)
111113
return urls
112114
}
113115
}

pkg/readers/file_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package readers
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestUrlsFor(t *testing.T) {
9+
fr := FileReader{
10+
Options: &FileReaderOptions{},
11+
}
12+
13+
tests := []struct {
14+
name string
15+
candidate string
16+
ports []int
17+
want []string
18+
}{
19+
{
20+
name: "Test with IP",
21+
candidate: "192.168.1.1",
22+
ports: []int{80, 443, 8443},
23+
want: []string{
24+
"http://192.168.1.1:80",
25+
"http://192.168.1.1:443",
26+
"http://192.168.1.1:8443",
27+
"https://192.168.1.1:80",
28+
"https://192.168.1.1:443",
29+
"https://192.168.1.1:8443",
30+
},
31+
},
32+
{
33+
name: "Test with IP and port",
34+
candidate: "192.168.1.1:8080",
35+
ports: []int{80, 443, 8443},
36+
want: []string{
37+
"http://192.168.1.1:8080",
38+
"https://192.168.1.1:8080",
39+
},
40+
},
41+
{
42+
name: "Test with scheme, IP and port",
43+
candidate: "http://192.168.1.1:8080",
44+
ports: []int{80, 443, 8443},
45+
want: []string{
46+
"http://192.168.1.1:8080",
47+
},
48+
},
49+
{
50+
name: "Test with scheme and IP",
51+
candidate: "https://192.168.1.1",
52+
ports: []int{80, 443, 8443},
53+
want: []string{
54+
"https://192.168.1.1:80",
55+
"https://192.168.1.1:443",
56+
"https://192.168.1.1:8443",
57+
},
58+
},
59+
{
60+
name: "Test with IP and path",
61+
candidate: "192.168.1.1/path",
62+
ports: []int{80, 443, 8443},
63+
want: []string{
64+
"http://192.168.1.1:80/path",
65+
"http://192.168.1.1:443/path",
66+
"http://192.168.1.1:8443/path",
67+
"https://192.168.1.1:80/path",
68+
"https://192.168.1.1:443/path",
69+
"https://192.168.1.1:8443/path",
70+
},
71+
},
72+
{
73+
name: "Test with scheme, IP, port and path",
74+
candidate: "http://192.168.1.1:8080/path",
75+
ports: []int{80, 443, 8443},
76+
want: []string{
77+
"http://192.168.1.1:8080/path",
78+
},
79+
},
80+
{
81+
name: "Test with IP, port and path",
82+
candidate: "192.168.1.1:8080/path",
83+
ports: []int{80, 443, 8443},
84+
want: []string{
85+
"http://192.168.1.1:8080/path",
86+
"https://192.168.1.1:8080/path",
87+
},
88+
},
89+
}
90+
91+
for _, tt := range tests {
92+
t.Run(tt.name, func(t *testing.T) {
93+
got := fr.urlsFor(tt.candidate, tt.ports)
94+
if !reflect.DeepEqual(got, tt.want) {
95+
t.Errorf("urlsFor() =>\n\nhave: %v\nwant %v", got, tt.want)
96+
}
97+
})
98+
}
99+
}

0 commit comments

Comments
 (0)