Skip to content

Commit

Permalink
(fix) allow file parser to parse <host>:<port> targets
Browse files Browse the repository at this point in the history
  • Loading branch information
leonjza committed Sep 19, 2024
1 parent 4f4cbe4 commit 66eec62
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PLATFORMS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 linux/arm windows
CGO := CGO_ENABLED=0

# Default target
default: clean frontend build integrity
default: clean test frontend build integrity

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

# Run any tests
test:
@echo "Running tests..."
go test ./...

# Build for all platforms
build: $(PLATFORMS)
Expand Down
8 changes: 5 additions & 3 deletions pkg/readers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/sensepost/gowitness/internal/islazy"
"github.com/sensepost/gowitness/pkg/log"
)

// FileReader is a reader that expects a file with targets that
Expand Down Expand Up @@ -80,14 +79,18 @@ func (fr *FileReader) Read(ch chan<- string) error {
// If any ports configuration exists, those will also be added as candidates.
func (fr *FileReader) urlsFor(candidate string, ports []int) []string {
var urls []string
// 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
}

hasScheme := parsedURL.Scheme != ""
hasPort := parsedURL.Port() != ""
hostname := parsedURL.Hostname()

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

// at this point if hostname is still "", then just skip it entirely
if hostname == "" {
log.Debug("could not parse candidate to something usable", "candidate", candidate)
return urls
}
}
Expand Down
99 changes: 99 additions & 0 deletions pkg/readers/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package readers

import (
"reflect"
"testing"
)

func TestUrlsFor(t *testing.T) {
fr := FileReader{
Options: &FileReaderOptions{},
}

tests := []struct {
name string
candidate string
ports []int
want []string
}{
{
name: "Test with IP",
candidate: "192.168.1.1",
ports: []int{80, 443, 8443},
want: []string{
"http://192.168.1.1:80",
"http://192.168.1.1:443",
"http://192.168.1.1:8443",
"https://192.168.1.1:80",
"https://192.168.1.1:443",
"https://192.168.1.1:8443",
},
},
{
name: "Test with IP and port",
candidate: "192.168.1.1:8080",
ports: []int{80, 443, 8443},
want: []string{
"http://192.168.1.1:8080",
"https://192.168.1.1:8080",
},
},
{
name: "Test with scheme, IP and port",
candidate: "http://192.168.1.1:8080",
ports: []int{80, 443, 8443},
want: []string{
"http://192.168.1.1:8080",
},
},
{
name: "Test with scheme and IP",
candidate: "https://192.168.1.1",
ports: []int{80, 443, 8443},
want: []string{
"https://192.168.1.1:80",
"https://192.168.1.1:443",
"https://192.168.1.1:8443",
},
},
{
name: "Test with IP and path",
candidate: "192.168.1.1/path",
ports: []int{80, 443, 8443},
want: []string{
"http://192.168.1.1:80/path",
"http://192.168.1.1:443/path",
"http://192.168.1.1:8443/path",
"https://192.168.1.1:80/path",
"https://192.168.1.1:443/path",
"https://192.168.1.1:8443/path",
},
},
{
name: "Test with scheme, IP, port and path",
candidate: "http://192.168.1.1:8080/path",
ports: []int{80, 443, 8443},
want: []string{
"http://192.168.1.1:8080/path",
},
},
{
name: "Test with IP, port and path",
candidate: "192.168.1.1:8080/path",
ports: []int{80, 443, 8443},
want: []string{
"http://192.168.1.1:8080/path",
"https://192.168.1.1:8080/path",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := fr.urlsFor(tt.candidate, tt.ports)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("urlsFor() =>\n\nhave: %v\nwant %v", got, tt.want)
}
})
}
}

0 comments on commit 66eec62

Please sign in to comment.