2022-10-20 17:23:00 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2022-12-04 22:16:55 +05:30
|
|
|
"net/http"
|
2022-10-20 17:23:00 +05:30
|
|
|
"strings"
|
|
|
|
|
"sync/atomic"
|
2023-02-06 23:34:33 +05:30
|
|
|
"time"
|
2022-10-20 17:23:00 +05:30
|
|
|
|
2022-12-04 22:16:55 +05:30
|
|
|
"github.com/corpix/uarand"
|
2022-10-20 17:23:00 +05:30
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/hmap/store/hybrid"
|
2023-02-06 23:34:33 +05:30
|
|
|
"github.com/projectdiscovery/httpx/common/httpx"
|
2022-11-09 14:18:56 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/contextargs"
|
2022-10-20 17:23:00 +05:30
|
|
|
"github.com/remeh/sizedwaitgroup"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const probeBulkSize = 50
|
|
|
|
|
|
|
|
|
|
// initializeTemplatesHTTPInput initializes the http form of input
|
|
|
|
|
// for any loaded http templates if input is in non-standard format.
|
|
|
|
|
func (r *Runner) initializeTemplatesHTTPInput() (*hybrid.HybridMap, error) {
|
|
|
|
|
hm, err := hybrid.New(hybrid.DefaultDiskOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not create temporary input file")
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 20:51:33 +05:30
|
|
|
gologger.Info().Msgf("Running httpx on input host")
|
2022-10-20 17:23:00 +05:30
|
|
|
|
|
|
|
|
var bulkSize = probeBulkSize
|
|
|
|
|
if r.options.BulkSize > probeBulkSize {
|
|
|
|
|
bulkSize = r.options.BulkSize
|
|
|
|
|
}
|
2023-02-06 23:34:33 +05:30
|
|
|
|
|
|
|
|
httpxOptions := httpx.DefaultOptions
|
|
|
|
|
httpxOptions.RetryMax = r.options.Retries
|
|
|
|
|
httpxOptions.Timeout = time.Duration(r.options.Timeout) * time.Second
|
|
|
|
|
httpxClient, err := httpx.New(&httpxOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not create httpx client")
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 17:23:00 +05:30
|
|
|
// Probe the non-standard URLs and store them in cache
|
|
|
|
|
swg := sizedwaitgroup.New(bulkSize)
|
|
|
|
|
count := int32(0)
|
2022-11-09 14:18:56 +01:00
|
|
|
r.hmapInputProvider.Scan(func(value *contextargs.MetaInput) bool {
|
|
|
|
|
if strings.HasPrefix(value.Input, "http://") || strings.HasPrefix(value.Input, "https://") {
|
2022-10-20 17:23:00 +05:30
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swg.Add()
|
2022-11-09 14:18:56 +01:00
|
|
|
go func(input *contextargs.MetaInput) {
|
2022-10-20 17:23:00 +05:30
|
|
|
defer swg.Done()
|
|
|
|
|
|
2023-02-06 23:34:33 +05:30
|
|
|
if result := probeURL(input.Input, httpxClient); result != "" {
|
2022-10-20 17:23:00 +05:30
|
|
|
atomic.AddInt32(&count, 1)
|
2022-11-09 14:18:56 +01:00
|
|
|
_ = hm.Set(input.Input, []byte(result))
|
2022-10-20 17:23:00 +05:30
|
|
|
}
|
|
|
|
|
}(value)
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
swg.Wait()
|
|
|
|
|
|
2022-12-04 20:51:33 +05:30
|
|
|
gologger.Info().Msgf("Found %d URL from httpx", atomic.LoadInt32(&count))
|
2022-10-20 17:23:00 +05:30
|
|
|
return hm, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2023-02-06 23:34:33 +05:30
|
|
|
httpSchemes = []string{"https", "http"}
|
2022-10-20 17:23:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// probeURL probes the scheme for a URL. first HTTPS is tried
|
|
|
|
|
// and if any errors occur http is tried. If none succeeds, probing
|
|
|
|
|
// is abandoned for such URLs.
|
2023-02-06 23:34:33 +05:30
|
|
|
func probeURL(input string, httpxclient *httpx.HTTPX) string {
|
2022-10-20 17:23:00 +05:30
|
|
|
for _, scheme := range httpSchemes {
|
|
|
|
|
formedURL := fmt.Sprintf("%s://%s", scheme, input)
|
2023-02-06 23:34:33 +05:30
|
|
|
req, err := httpxclient.NewRequest(http.MethodHead, formedURL)
|
2022-12-04 22:16:55 +05:30
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("User-Agent", uarand.GetRandom())
|
|
|
|
|
|
2023-02-06 23:34:33 +05:30
|
|
|
if _, err = httpxclient.Do(req, httpx.UnsafeOptions{}); err != nil {
|
2022-10-20 17:23:00 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return formedURL
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|