2022-10-20 17:23:00 +05:30
|
|
|
package input
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net"
|
|
|
|
|
"net/url"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/hmap/store/hybrid"
|
|
|
|
|
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
2022-12-20 21:59:28 +01:00
|
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
|
|
|
|
"github.com/projectdiscovery/utils/ports"
|
2022-12-13 13:43:54 +01:00
|
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
2022-10-20 17:23:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Helper is a structure for helping with input transformation
|
|
|
|
|
type Helper struct {
|
|
|
|
|
InputsHTTP *hybrid.HybridMap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHelper returns a new inpt 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:
|
2022-11-08 15:19:45 +01:00
|
|
|
return h.convertInputToType(input, typeHostOnly, "")
|
2022-10-20 17:23:00 +05:30
|
|
|
case templateTypes.FileProtocol, templateTypes.OfflineHTTPProtocol:
|
2022-11-08 15:19:45 +01:00
|
|
|
return h.convertInputToType(input, typeFilepath, "")
|
2022-10-20 17:23:00 +05:30
|
|
|
case templateTypes.HTTPProtocol, templateTypes.HeadlessProtocol:
|
2022-11-08 15:19:45 +01:00
|
|
|
return h.convertInputToType(input, typeURL, "")
|
2022-10-20 17:23:00 +05:30
|
|
|
case templateTypes.NetworkProtocol:
|
2022-11-08 15:19:45 +01:00
|
|
|
return h.convertInputToType(input, typeHostWithOptionalPort, "")
|
2022-10-20 17:23:00 +05:30
|
|
|
case templateTypes.SSLProtocol:
|
2022-11-08 15:19:45 +01:00
|
|
|
return h.convertInputToType(input, typeHostWithPort, "443")
|
2022-10-20 17:23:00 +05:30
|
|
|
case templateTypes.WebsocketProtocol:
|
2022-11-08 15:19:45 +01:00
|
|
|
return h.convertInputToType(input, typeWebsocket, "")
|
2022-10-20 17:23:00 +05:30
|
|
|
}
|
|
|
|
|
return input
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type inputType int
|
|
|
|
|
|
|
|
|
|
const (
|
2022-11-08 15:19:45 +01:00
|
|
|
typeHostOnly inputType = iota + 1
|
|
|
|
|
typeHostWithPort
|
|
|
|
|
typeHostWithOptionalPort
|
|
|
|
|
typeURL
|
|
|
|
|
typeFilepath
|
|
|
|
|
typeWebsocket
|
2022-10-20 17:23:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2022-12-08 21:00:55 +01:00
|
|
|
isURL := strings.Contains(input, "://")
|
|
|
|
|
uri, _ := url.Parse(input)
|
|
|
|
|
|
2022-10-20 17:23:00 +05:30
|
|
|
var host, port string
|
2022-12-08 21:00:55 +01:00
|
|
|
if isURL && uri != nil {
|
|
|
|
|
host, port, _ = net.SplitHostPort(uri.Host)
|
2022-10-20 17:23:00 +05:30
|
|
|
} else {
|
|
|
|
|
host, port, _ = net.SplitHostPort(input)
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
|
|
|
|
|
hasHost := host != ""
|
2022-12-20 21:59:28 +01:00
|
|
|
hasPort := ports.IsValid(port)
|
|
|
|
|
hasDefaultPort := ports.IsValid(defaultPort)
|
2022-10-20 17:23:00 +05:30
|
|
|
|
2022-12-08 21:00:55 +01:00
|
|
|
switch inputType {
|
|
|
|
|
case typeFilepath:
|
2022-11-08 15:19:45 +01:00
|
|
|
// if it has ports most likely it's not a file
|
|
|
|
|
if hasPort {
|
2022-10-20 17:23:00 +05:30
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
if filepath.IsAbs(input) {
|
|
|
|
|
return input
|
|
|
|
|
}
|
2022-12-20 21:59:28 +01:00
|
|
|
if absPath, _ := filepath.Abs(input); absPath != "" && fileutil.FileOrFolderExists(absPath) {
|
2022-10-20 17:23:00 +05:30
|
|
|
return input
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && !isURL {
|
2022-10-20 17:23:00 +05:30
|
|
|
return input
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
case typeHostOnly:
|
|
|
|
|
if hasHost {
|
2022-10-20 17:23:00 +05:30
|
|
|
return host
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
if isURL {
|
|
|
|
|
return uri.Hostname()
|
2022-10-20 17:23:00 +05:30
|
|
|
}
|
2022-12-13 09:07:52 +01:00
|
|
|
return input
|
2022-12-08 21:00:55 +01:00
|
|
|
case typeURL:
|
2022-12-13 13:43:54 +01:00
|
|
|
if uri != nil && stringsutil.EqualFoldAny(uri.Scheme, "http", "https") {
|
2022-10-20 17:23:00 +05:30
|
|
|
return input
|
|
|
|
|
}
|
|
|
|
|
if h.InputsHTTP != nil {
|
|
|
|
|
if probed, ok := h.InputsHTTP.Get(input); ok {
|
|
|
|
|
return string(probed)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
case typeHostWithPort, typeHostWithOptionalPort:
|
|
|
|
|
if hasHost && hasPort {
|
2022-10-20 17:23:00 +05:30
|
|
|
return net.JoinHostPort(host, port)
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
if uri != nil && !hasPort && uri.Scheme == "https" {
|
|
|
|
|
return net.JoinHostPort(uri.Host, "443")
|
2022-10-20 17:23:00 +05:30
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
if hasDefaultPort {
|
2022-10-20 17:23:00 +05:30
|
|
|
return net.JoinHostPort(input, defaultPort)
|
|
|
|
|
}
|
2022-12-13 09:07:52 +01:00
|
|
|
if inputType == typeHostWithOptionalPort {
|
|
|
|
|
return input
|
|
|
|
|
}
|
2022-12-08 21:00:55 +01:00
|
|
|
case typeWebsocket:
|
2022-12-13 13:43:54 +01:00
|
|
|
if uri != nil && stringsutil.EqualFoldAny(uri.Scheme, "ws", "wss") {
|
2022-10-20 17:23:00 +05:30
|
|
|
return input
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|