Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/internal/runner/inputs.go
2070 views
1
package runner
2
3
import (
4
"context"
5
"fmt"
6
"sync/atomic"
7
"time"
8
9
"github.com/pkg/errors"
10
"github.com/projectdiscovery/hmap/store/hybrid"
11
"github.com/projectdiscovery/httpx/common/httpx"
12
"github.com/projectdiscovery/nuclei/v3/pkg/input/provider"
13
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
14
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
15
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
16
stringsutil "github.com/projectdiscovery/utils/strings"
17
syncutil "github.com/projectdiscovery/utils/sync"
18
)
19
20
// initializeTemplatesHTTPInput initializes the http form of input
21
// for any loaded http templates if input is in non-standard format.
22
func (r *Runner) initializeTemplatesHTTPInput() (*hybrid.HybridMap, error) {
23
hm, err := hybrid.New(hybrid.DefaultDiskOptions)
24
if err != nil {
25
return nil, errors.Wrap(err, "could not create temporary input file")
26
}
27
if r.inputProvider.InputType() == provider.MultiFormatInputProvider {
28
// currently http probing for input mode types is not supported
29
return hm, nil
30
}
31
r.Logger.Info().Msgf("Running httpx on input host")
32
33
httpxOptions := httpx.DefaultOptions
34
if r.options.AliveHttpProxy != "" {
35
httpxOptions.Proxy = r.options.AliveHttpProxy
36
} else if r.options.AliveSocksProxy != "" {
37
httpxOptions.Proxy = r.options.AliveSocksProxy
38
}
39
httpxOptions.RetryMax = r.options.Retries
40
httpxOptions.Timeout = time.Duration(r.options.Timeout) * time.Second
41
42
dialers := protocolstate.GetDialersWithId(r.options.ExecutionId)
43
if dialers == nil {
44
return nil, fmt.Errorf("dialers not initialized for %s", r.options.ExecutionId)
45
}
46
47
httpxOptions.NetworkPolicy = dialers.NetworkPolicy
48
httpxClient, err := httpx.New(&httpxOptions)
49
if err != nil {
50
return nil, errors.Wrap(err, "could not create httpx client")
51
}
52
53
// Probe the non-standard URLs and store them in cache
54
swg, err := syncutil.New(syncutil.WithSize(r.options.BulkSize))
55
if err != nil {
56
return nil, errors.Wrap(err, "could not create adaptive group")
57
}
58
var count atomic.Int32
59
r.inputProvider.Iterate(func(value *contextargs.MetaInput) bool {
60
if stringsutil.HasPrefixAny(value.Input, "http://", "https://") {
61
return true
62
}
63
64
if r.options.ProbeConcurrency > 0 && swg.Size != r.options.ProbeConcurrency {
65
if err := swg.Resize(context.Background(), r.options.ProbeConcurrency); err != nil {
66
r.Logger.Error().Msgf("Could not resize workpool: %s\n", err)
67
}
68
}
69
70
swg.Add()
71
go func(input *contextargs.MetaInput) {
72
defer swg.Done()
73
74
if result := utils.ProbeURL(input.Input, httpxClient); result != "" {
75
count.Add(1)
76
_ = hm.Set(input.Input, []byte(result))
77
}
78
}(value)
79
return true
80
})
81
swg.Wait()
82
83
r.Logger.Info().Msgf("Found %d URL from httpx", count.Load())
84
return hm, nil
85
}
86
87