2021-10-14 23:30:37 +02:00
|
|
|
package loader
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
2022-01-12 18:33:17 +05:30
|
|
|
"net/url"
|
2021-10-14 23:30:37 +02:00
|
|
|
"strings"
|
2025-07-09 14:47:26 -05:00
|
|
|
"sync"
|
2021-11-05 17:24:23 +05:30
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-02-07 16:41:55 +02:00
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/templates/extensions"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
|
2023-03-02 14:54:01 +01:00
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2025-07-09 14:47:26 -05:00
|
|
|
sliceutil "github.com/projectdiscovery/utils/slice"
|
2023-03-16 09:03:59 +01:00
|
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
2025-07-09 14:47:26 -05:00
|
|
|
syncutil "github.com/projectdiscovery/utils/sync"
|
2021-10-14 23:30:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ContentType string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
Template ContentType = "Template"
|
|
|
|
|
Workflow ContentType = "Workflow"
|
|
|
|
|
)
|
|
|
|
|
|
2022-01-24 16:48:12 +05:30
|
|
|
type RemoteContent struct {
|
2021-10-14 23:30:37 +02:00
|
|
|
Content []string
|
|
|
|
|
Type ContentType
|
|
|
|
|
Error error
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 20:14:50 +05:30
|
|
|
func getRemoteTemplatesAndWorkflows(templateURLs, workflowURLs, remoteTemplateDomainList []string) ([]string, []string, error) {
|
2025-07-09 14:47:26 -05:00
|
|
|
var (
|
|
|
|
|
err error
|
|
|
|
|
muErr sync.Mutex
|
|
|
|
|
)
|
|
|
|
|
remoteTemplateList := sliceutil.NewSyncSlice[string]()
|
|
|
|
|
remoteWorkFlowList := sliceutil.NewSyncSlice[string]()
|
2021-10-14 23:30:37 +02:00
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
awg, errAwg := syncutil.New(syncutil.WithSize(50))
|
|
|
|
|
if errAwg != nil {
|
|
|
|
|
return nil, nil, errAwg
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
loadItem := func(URL string, contentType ContentType) {
|
|
|
|
|
defer awg.Done()
|
|
|
|
|
|
|
|
|
|
remoteContent := getRemoteContent(URL, remoteTemplateDomainList, contentType)
|
2022-01-24 16:48:12 +05:30
|
|
|
if remoteContent.Error != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
muErr.Lock()
|
2021-10-14 23:30:37 +02:00
|
|
|
if err != nil {
|
2022-01-24 16:48:12 +05:30
|
|
|
err = errors.New(remoteContent.Error.Error() + ": " + err.Error())
|
2021-10-14 23:30:37 +02:00
|
|
|
} else {
|
2022-01-24 16:48:12 +05:30
|
|
|
err = remoteContent.Error
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
muErr.Unlock()
|
2021-10-14 23:30:37 +02:00
|
|
|
} else {
|
2025-07-01 00:40:44 +07:00
|
|
|
switch remoteContent.Type {
|
|
|
|
|
case Template:
|
2025-07-09 14:47:26 -05:00
|
|
|
remoteTemplateList.Append(remoteContent.Content...)
|
2025-07-01 00:40:44 +07:00
|
|
|
case Workflow:
|
2025-07-09 14:47:26 -05:00
|
|
|
remoteWorkFlowList.Append(remoteContent.Content...)
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
|
|
|
|
|
for _, templateURL := range templateURLs {
|
|
|
|
|
awg.Add()
|
|
|
|
|
go loadItem(templateURL, Template)
|
|
|
|
|
}
|
|
|
|
|
for _, workflowURL := range workflowURLs {
|
|
|
|
|
awg.Add()
|
|
|
|
|
go loadItem(workflowURL, Workflow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
awg.Wait()
|
|
|
|
|
|
|
|
|
|
return remoteTemplateList.Slice, remoteWorkFlowList.Slice, err
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
func getRemoteContent(URL string, remoteTemplateDomainList []string, contentType ContentType) RemoteContent {
|
2022-02-07 16:41:55 +02:00
|
|
|
if err := validateRemoteTemplateURL(URL, remoteTemplateDomainList); err != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{Error: err}
|
2022-01-27 13:45:03 +05:30
|
|
|
}
|
2023-05-26 22:10:18 +02:00
|
|
|
if strings.HasPrefix(URL, "http") && stringsutil.HasSuffixAny(URL, extensions.YAML) {
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{
|
2022-01-12 18:33:17 +05:30
|
|
|
Content: []string{URL},
|
|
|
|
|
Type: contentType,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 14:54:01 +01:00
|
|
|
response, err := retryablehttp.DefaultClient().Get(URL)
|
2021-10-14 23:30:37 +02:00
|
|
|
if err != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{Error: err}
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = response.Body.Close()
|
|
|
|
|
}()
|
2021-10-14 23:30:37 +02:00
|
|
|
if response.StatusCode < 200 || response.StatusCode > 299 {
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{Error: fmt.Errorf("get \"%s\": unexpect status %d", URL, response.StatusCode)}
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(response.Body)
|
|
|
|
|
var templateList []string
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
text := strings.TrimSpace(scanner.Text())
|
|
|
|
|
if text == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-01-27 13:45:03 +05:30
|
|
|
if utils.IsURL(text) {
|
2022-02-07 16:41:55 +02:00
|
|
|
if err := validateRemoteTemplateURL(text, remoteTemplateDomainList); err != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{Error: err}
|
2022-01-27 13:45:03 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-10-14 23:30:37 +02:00
|
|
|
templateList = append(templateList, text)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{Error: errors.Wrap(err, "get \"%s\"")}
|
2021-10-14 23:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
return RemoteContent{
|
2021-10-14 23:30:37 +02:00
|
|
|
Content: templateList,
|
|
|
|
|
Type: contentType,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-27 13:45:03 +05:30
|
|
|
|
2022-02-07 16:41:55 +02:00
|
|
|
func validateRemoteTemplateURL(inputURL string, remoteTemplateDomainList []string) error {
|
2022-01-27 13:45:03 +05:30
|
|
|
parsedURL, err := url.Parse(inputURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !utils.StringSliceContains(remoteTemplateDomainList, parsedURL.Host) {
|
|
|
|
|
return errors.Errorf("Remote template URL host (%s) is not present in the `remote-template-domain` list in nuclei config", parsedURL.Host)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|