2022-10-20 17:23:00 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2022-12-04 22:16:55 +05:30
|
|
|
"net/http"
|
2022-10-20 17:23:00 +05:30
|
|
|
"strings"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
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"
|
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/projectdiscovery/nuclei/v2/pkg/protocols/http/httpclientpool"
|
|
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
|
|
|
|
"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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpclient, err := httpclientpool.Get(r.options, &httpclientpool.Configuration{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not get http client")
|
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
// 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()
|
|
|
|
|
|
2022-11-09 14:18:56 +01:00
|
|
|
if result := probeURL(input.Input, httpclient); 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 (
|
|
|
|
|
drainReqSize = int64(8 * 1024)
|
|
|
|
|
httpSchemes = []string{"https", "http"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
func probeURL(input string, httpclient *retryablehttp.Client) string {
|
|
|
|
|
for _, scheme := range httpSchemes {
|
|
|
|
|
formedURL := fmt.Sprintf("%s://%s", scheme, input)
|
2022-12-04 22:16:55 +05:30
|
|
|
req, err := retryablehttp.NewRequest(http.MethodGet, formedURL, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("User-Agent", uarand.GetRandom())
|
|
|
|
|
|
|
|
|
|
resp, err := httpclient.Do(req)
|
2022-10-20 17:23:00 +05:30
|
|
|
if resp != nil {
|
|
|
|
|
_, _ = io.CopyN(io.Discard, resp.Body, drainReqSize)
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return formedURL
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|