forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_probe.go
More file actions
90 lines (77 loc) · 2.2 KB
/
http_probe.go
File metadata and controls
90 lines (77 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package utils
import (
"fmt"
"net"
"net/http"
"strconv"
"github.com/projectdiscovery/httpx/common/httpx"
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
"github.com/projectdiscovery/useragent"
sliceutil "github.com/projectdiscovery/utils/slice"
)
var commonHttpPorts = []string{
"80",
"8080",
}
var defaultHttpSchemes = []string{
"https",
"http",
}
var httpFirstSchemes = []string{
"http",
"https",
}
// determineSchemeOrder for the input
func determineSchemeOrder(input string) []string {
if _, port, err := net.SplitHostPort(input); err == nil {
// if input has port that is commonly used for HTTP, return http then https
if sliceutil.Contains(commonHttpPorts, port) {
return httpFirstSchemes
}
// As of 10/2025 shodan shows that ports > 1024 are more likely to expose HTTP
// hence we test first http then https on higher ports
// if input has port > 1024, return http then https
if port, err := strconv.Atoi(port); err == nil && port > 1024 {
return httpFirstSchemes
}
}
return defaultHttpSchemes
}
// ProbeURL probes the scheme for a URL.
// http schemes are selected with heuristics
// If none succeeds, probing is abandoned for such URLs.
func ProbeURL(input string, httpxclient *httpx.HTTPX) string {
schemes := determineSchemeOrder(input)
for _, scheme := range schemes {
formedURL := fmt.Sprintf("%s://%s", scheme, input)
req, err := httpxclient.NewRequest(http.MethodHead, formedURL)
if err != nil {
continue
}
userAgent := useragent.PickRandom()
req.Header.Set("User-Agent", userAgent.Raw)
if _, err = httpxclient.Do(req, httpx.UnsafeOptions{}); err != nil {
continue
}
return formedURL
}
return ""
}
type inputLivenessChecker struct {
client *httpx.HTTPX
}
// ProbeURL probes the scheme for a URL.
func (i *inputLivenessChecker) ProbeURL(input string) (string, error) {
return ProbeURL(input, i.client), nil
}
func (i *inputLivenessChecker) Close() error {
if i.client.Dialer != nil {
i.client.Dialer.Close()
}
return nil
}
// GetInputLivenessChecker returns a new input liveness checker using provided httpx client
func GetInputLivenessChecker(client *httpx.HTTPX) types.InputLivenessProbe {
x := &inputLivenessChecker{client: client}
return x
}