adding host with optional port type logic (#2815)

* adding host with optional port type logic

* adding comment support in test cases

* updating test cases with multiple input scenarios

* readding port condition
This commit is contained in:
Mzack9999 2022-11-08 15:19:45 +01:00 committed by GitHub
parent 185c95f549
commit 840028fa93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 40 deletions

View File

@ -66,6 +66,10 @@ func runTestCases(file *os.File, debug bool) (bool, []string) {
if testCase == "" {
continue
}
// skip comments
if strings.HasPrefix(testCase, "#") {
continue
}
if runTestCase(testCase, debug) {
errored = true
failedTestCases = append(failedTestCases, testCase)

View File

@ -1,4 +1,7 @@
# Simple binary invocation
{{binary}}
# Template tags filter
{{binary}} -tags cve -ntv 8.8.8,8.8.9
{{binary}} -tags cve
{{binary}} -tags cve,exposure
@ -49,7 +52,32 @@
{{binary}} -tags cve -author geeknik,pdteam -tc severity=='high'
{{binary}} -tc contains(authors,'pdteam')
{{binary}} -t cves/ -t exposures/ -tc contains(tags,'cve') -exclude-templates cves/2020/CVE-2020-9757.yaml
# Workflow Filters
{{binary}} -w workflows
{{binary}} -w workflows -author geeknik,pdteam
{{binary}} -w workflows -severity high,critical
{{binary}} -w workflows -author geeknik,pdteam -severity high,critical
# Input Types
# http protocol
# host
{{binary}} -id tech-detect -u scanme.sh
# host:port
{{binary}} -id tech-detect -u scanme.sh:80
# scheme://host:port
{{binary}} -id tech-detect -u http://scanme.sh:80
# scheme://host
{{binary}} -id tech-detect -u https://scanme.sh
# Network Protocol
# host
{{binary}} -id ftp-weak-credentials -u scanme.sh
# host:port
{{binary}} -id ftp-weak-credentials -u scanme.sh:21
# SSL Protocol
# host
{{binary}} -id tls-version -u scanme.sh
# host:port
{{binary}} -id tls-version -u scanme.sh:22

View File

@ -36,17 +36,17 @@ func (h *Helper) Close() error {
func (h *Helper) Transform(input string, protocol templateTypes.ProtocolType) string {
switch protocol {
case templateTypes.DNSProtocol, templateTypes.WHOISProtocol:
return h.convertInputToType(input, inputTypeHost, "")
return h.convertInputToType(input, typeHostOnly, "")
case templateTypes.FileProtocol, templateTypes.OfflineHTTPProtocol:
return h.convertInputToType(input, inputTypeFilepath, "")
return h.convertInputToType(input, typeFilepath, "")
case templateTypes.HTTPProtocol, templateTypes.HeadlessProtocol:
return h.convertInputToType(input, inputTypeURL, "")
return h.convertInputToType(input, typeURL, "")
case templateTypes.NetworkProtocol:
return h.convertInputToType(input, inputTypeHostPort, "")
return h.convertInputToType(input, typeHostWithOptionalPort, "")
case templateTypes.SSLProtocol:
return h.convertInputToType(input, inputTypeHostPort, "443")
return h.convertInputToType(input, typeHostWithPort, "443")
case templateTypes.WebsocketProtocol:
return h.convertInputToType(input, inputTypeWebsocket, "")
return h.convertInputToType(input, typeWebsocket, "")
}
return input
}
@ -54,18 +54,18 @@ func (h *Helper) Transform(input string, protocol templateTypes.ProtocolType) st
type inputType int
const (
inputTypeHost inputType = iota + 1
inputTypeURL
inputTypeFilepath
inputTypeHostPort
inputTypeWebsocket
typeHostOnly inputType = iota + 1
typeHostWithPort
typeHostWithOptionalPort
typeURL
typeFilepath
typeWebsocket
)
// convertInputToType converts an input based on an inputType.
// Various formats are supported for inputs and their transformation
func (h *Helper) convertInputToType(input string, inputType inputType, defaultPort string) string {
notURL := !strings.Contains(input, "://")
parsed, _ := url.Parse(input)
var host, port string
if !notURL {
@ -73,9 +73,11 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
} else {
host, port, _ = net.SplitHostPort(input)
}
hasPort := port != ""
if inputType == inputTypeFilepath {
if port != "" {
if inputType == typeFilepath {
// if it has ports most likely it's not a file
if hasPort {
return ""
}
if filepath.IsAbs(input) {
@ -87,7 +89,7 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && notURL {
return input
}
} else if inputType == inputTypeHost {
} else if inputType == typeHostOnly {
if host != "" {
return host
}
@ -96,7 +98,7 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
} else {
return input
}
} else if inputType == inputTypeURL {
} else if inputType == typeURL {
if parsed != nil && (parsed.Scheme == "http" || parsed.Scheme == "https") {
return input
}
@ -105,7 +107,7 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
return string(probed)
}
}
} else if inputType == inputTypeHostPort {
} else if inputType == typeHostWithPort {
if host != "" && port != "" {
return net.JoinHostPort(host, port)
}
@ -115,7 +117,18 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
if defaultPort != "" {
return net.JoinHostPort(input, defaultPort)
}
} else if inputType == inputTypeWebsocket {
} else if inputType == typeHostWithOptionalPort {
if host != "" && port != "" {
return net.JoinHostPort(host, port)
}
if parsed != nil && port == "" && parsed.Scheme == "https" {
return net.JoinHostPort(parsed.Host, "443")
}
if defaultPort != "" {
return net.JoinHostPort(input, defaultPort)
}
return input
} else if inputType == typeWebsocket {
if parsed != nil && (parsed.Scheme == "ws" || parsed.Scheme == "wss") {
return input
}

View File

@ -24,37 +24,37 @@ func TestConvertInputToType(t *testing.T) {
defaultPort string
}{
// host
{"google.com", inputTypeHost, "google.com", ""},
{"google.com:443", inputTypeHost, "google.com", ""},
{"https://google.com", inputTypeHost, "google.com", ""},
{"https://google.com:443", inputTypeHost, "google.com", ""},
{"google.com", typeHostOnly, "google.com", ""},
{"google.com:443", typeHostOnly, "google.com", ""},
{"https://google.com", typeHostOnly, "google.com", ""},
{"https://google.com:443", typeHostOnly, "google.com", ""},
// url
{"test.com", inputTypeURL, "", ""},
{"google.com", inputTypeURL, "https://google.com", ""},
{"https://google.com", inputTypeURL, "https://google.com", ""},
{"test.com", typeURL, "", ""},
{"google.com", typeURL, "https://google.com", ""},
{"https://google.com", typeURL, "https://google.com", ""},
// file
{"google.com:443", inputTypeFilepath, "", ""},
{"https://google.com:443", inputTypeFilepath, "", ""},
{"/example/path", inputTypeFilepath, "/example/path", ""},
{"input_test.go", inputTypeFilepath, "input_test.go", ""},
{"../input", inputTypeFilepath, "../input", ""},
{"input_test.*", inputTypeFilepath, "input_test.*", ""},
{"google.com:443", typeFilepath, "", ""},
{"https://google.com:443", typeFilepath, "", ""},
{"/example/path", typeFilepath, "/example/path", ""},
{"input_test.go", typeFilepath, "input_test.go", ""},
{"../input", typeFilepath, "../input", ""},
{"input_test.*", typeFilepath, "input_test.*", ""},
// host-port
{"google.com", inputTypeHostPort, "", ""},
{"google.com:443", inputTypeHostPort, "google.com:443", ""},
{"https://google.com", inputTypeHostPort, "google.com:443", ""},
{"https://google.com:443", inputTypeHostPort, "google.com:443", ""},
{"google.com", typeHostWithPort, "", ""},
{"google.com:443", typeHostWithPort, "google.com:443", ""},
{"https://google.com", typeHostWithPort, "google.com:443", ""},
{"https://google.com:443", typeHostWithPort, "google.com:443", ""},
// host-port with default port
{"google.com", inputTypeHostPort, "google.com:443", "443"},
{"google.com", typeHostWithPort, "google.com:443", "443"},
// websocket
{"google.com", inputTypeWebsocket, "", ""},
{"google.com:443", inputTypeWebsocket, "", ""},
{"https://google.com:443", inputTypeWebsocket, "", ""},
{"wss://google.com", inputTypeWebsocket, "wss://google.com", ""},
{"google.com", typeWebsocket, "", ""},
{"google.com:443", typeWebsocket, "", ""},
{"https://google.com:443", typeWebsocket, "", ""},
{"wss://google.com", typeWebsocket, "wss://google.com", ""},
}
for _, test := range tests {