mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-17 17:56:56 +00:00
* spelling: addresses Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: asynchronous Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: basic Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: brute force Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: constant Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: disables Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: engine Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: every time Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: execution Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: false positives Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: from Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: further Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: github Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: gitlab Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: highlight Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: hygiene Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: ignore Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: input Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: item Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: itself Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: latestxxx Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: navigation Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: negative Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: nonexistent Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: occurred Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: override Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: overrides Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: payload Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: performed Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: respective Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: retrieve Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: scanlist Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: separated Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: separator Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: severity Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: source Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: strategy Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: string Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: templates Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: terminal Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: timeout Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: trailing slash Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: trailing Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> * spelling: websocket Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> --------- Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
package input
|
|
|
|
import (
|
|
"net"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/projectdiscovery/hmap/store/hybrid"
|
|
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
|
"github.com/projectdiscovery/utils/ports"
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
|
urlutil "github.com/projectdiscovery/utils/url"
|
|
)
|
|
|
|
// Helper is a structure for helping with input transformation
|
|
type Helper struct {
|
|
InputsHTTP *hybrid.HybridMap
|
|
}
|
|
|
|
// NewHelper returns a new input helper instance
|
|
func NewHelper() *Helper {
|
|
helper := &Helper{}
|
|
return helper
|
|
}
|
|
|
|
// Close closes the resources associated with input helper
|
|
func (h *Helper) Close() error {
|
|
var err error
|
|
if h.InputsHTTP != nil {
|
|
err = h.InputsHTTP.Close()
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Transform transforms an input based on protocol type and returns
|
|
// appropriate input based on it.
|
|
func (h *Helper) Transform(input string, protocol templateTypes.ProtocolType) string {
|
|
switch protocol {
|
|
case templateTypes.DNSProtocol, templateTypes.WHOISProtocol:
|
|
return h.convertInputToType(input, typeHostOnly, "")
|
|
case templateTypes.FileProtocol, templateTypes.OfflineHTTPProtocol:
|
|
return h.convertInputToType(input, typeFilepath, "")
|
|
case templateTypes.HTTPProtocol, templateTypes.HeadlessProtocol:
|
|
return h.convertInputToType(input, typeURL, "")
|
|
case templateTypes.NetworkProtocol:
|
|
return h.convertInputToType(input, typeHostWithOptionalPort, "")
|
|
case templateTypes.WebsocketProtocol:
|
|
return h.convertInputToType(input, typeWebsocket, "")
|
|
}
|
|
return input
|
|
}
|
|
|
|
type inputType int
|
|
|
|
const (
|
|
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 {
|
|
isURL := strings.Contains(input, "://")
|
|
uri, _ := urlutil.Parse(input)
|
|
|
|
var host, port string
|
|
if isURL && uri != nil {
|
|
host, port, _ = net.SplitHostPort(uri.Host)
|
|
} else {
|
|
host, port, _ = net.SplitHostPort(input)
|
|
}
|
|
|
|
hasHost := host != ""
|
|
hasPort := ports.IsValid(port)
|
|
hasDefaultPort := ports.IsValid(defaultPort)
|
|
|
|
switch inputType {
|
|
case typeFilepath:
|
|
// if it has ports most likely it's not a file
|
|
if hasPort {
|
|
return ""
|
|
}
|
|
if filepath.IsAbs(input) {
|
|
return input
|
|
}
|
|
if absPath, _ := filepath.Abs(input); absPath != "" && fileutil.FileOrFolderExists(absPath) {
|
|
return input
|
|
}
|
|
if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && !isURL {
|
|
return input
|
|
}
|
|
case typeHostOnly:
|
|
if hasHost {
|
|
return host
|
|
}
|
|
if isURL && uri != nil {
|
|
return uri.Hostname()
|
|
}
|
|
return input
|
|
case typeURL:
|
|
if uri != nil && stringsutil.EqualFoldAny(uri.Scheme, "http", "https") {
|
|
return input
|
|
}
|
|
if h.InputsHTTP != nil {
|
|
if probed, ok := h.InputsHTTP.Get(input); ok {
|
|
return string(probed)
|
|
}
|
|
}
|
|
case typeHostWithPort, typeHostWithOptionalPort:
|
|
if hasHost && hasPort {
|
|
return net.JoinHostPort(host, port)
|
|
}
|
|
if uri != nil && !hasPort && uri.Scheme == "https" {
|
|
return net.JoinHostPort(uri.Host, "443")
|
|
}
|
|
if hasDefaultPort {
|
|
return net.JoinHostPort(input, defaultPort)
|
|
}
|
|
if inputType == typeHostWithOptionalPort {
|
|
return input
|
|
}
|
|
case typeWebsocket:
|
|
if uri != nil && stringsutil.EqualFoldAny(uri.Scheme, "ws", "wss") {
|
|
return input
|
|
}
|
|
}
|
|
return ""
|
|
}
|