2021-06-30 18:39:01 +05:30
|
|
|
package loader
|
|
|
|
|
|
|
|
|
|
import (
|
2021-07-12 17:20:01 +03:00
|
|
|
"github.com/projectdiscovery/goflags"
|
2021-07-07 19:03:14 +05:30
|
|
|
"strings"
|
|
|
|
|
|
2021-06-30 18:39:01 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog"
|
2021-07-05 04:35:53 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader/filter"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader/load"
|
2021-07-01 14:36:40 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-06-30 18:39:01 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Config contains the configuration options for the loader
|
|
|
|
|
type Config struct {
|
|
|
|
|
Templates []string
|
|
|
|
|
Workflows []string
|
|
|
|
|
ExcludeTemplates []string
|
|
|
|
|
IncludeTemplates []string
|
|
|
|
|
|
|
|
|
|
Tags []string
|
|
|
|
|
ExcludeTags []string
|
|
|
|
|
Authors []string
|
2021-07-12 17:20:01 +03:00
|
|
|
Severities goflags.Severities
|
2021-06-30 18:39:01 +05:30
|
|
|
IncludeTags []string
|
|
|
|
|
|
|
|
|
|
Catalog *catalog.Catalog
|
2021-07-01 14:36:40 +05:30
|
|
|
ExecutorOptions protocols.ExecuterOptions
|
2021-06-30 18:39:01 +05:30
|
|
|
TemplatesDirectory string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store is a storage for loaded nuclei templates
|
|
|
|
|
type Store struct {
|
2021-07-05 04:35:53 +05:30
|
|
|
tagFilter *filter.TagFilter
|
|
|
|
|
pathFilter *filter.PathFilter
|
2021-07-01 16:29:26 +05:30
|
|
|
config *Config
|
|
|
|
|
finalTemplates []string
|
2021-07-01 14:36:40 +05:30
|
|
|
|
|
|
|
|
templates []*templates.Template
|
|
|
|
|
workflows []*templates.Template
|
2021-06-30 18:39:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new template store based on provided configuration
|
|
|
|
|
func New(config *Config) (*Store, error) {
|
|
|
|
|
// Create a tag filter based on provided configuration
|
|
|
|
|
store := &Store{
|
2021-07-05 04:35:53 +05:30
|
|
|
config: config,
|
|
|
|
|
tagFilter: filter.New(&filter.Config{
|
|
|
|
|
Tags: config.Tags,
|
|
|
|
|
ExcludeTags: config.ExcludeTags,
|
|
|
|
|
Authors: config.Authors,
|
|
|
|
|
Severities: config.Severities,
|
|
|
|
|
IncludeTags: config.IncludeTags,
|
|
|
|
|
}),
|
|
|
|
|
pathFilter: filter.NewPathFilter(&filter.PathFilterConfig{
|
|
|
|
|
IncludedTemplates: config.IncludeTemplates,
|
|
|
|
|
ExcludedTemplates: config.ExcludeTemplates,
|
|
|
|
|
}, config.Catalog),
|
2021-06-30 18:39:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle a case with no templates or workflows, where we use base directory
|
|
|
|
|
if len(config.Templates) == 0 && len(config.Workflows) == 0 {
|
|
|
|
|
config.Templates = append(config.Templates, config.TemplatesDirectory)
|
|
|
|
|
}
|
|
|
|
|
store.finalTemplates = append(store.finalTemplates, config.Templates...)
|
|
|
|
|
return store, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 14:36:40 +05:30
|
|
|
// Templates returns all the templates in the store
|
|
|
|
|
func (s *Store) Templates() []*templates.Template {
|
|
|
|
|
return s.templates
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Workflows returns all the workflows in the store
|
|
|
|
|
func (s *Store) Workflows() []*templates.Template {
|
|
|
|
|
return s.workflows
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 18:39:01 +05:30
|
|
|
// Load loads all the templates from a store, performs filtering and returns
|
|
|
|
|
// the complete compiled templates for a nuclei execution configuration.
|
2021-07-01 14:36:40 +05:30
|
|
|
func (s *Store) Load() {
|
2021-07-05 04:35:53 +05:30
|
|
|
s.templates = s.LoadTemplates(s.finalTemplates)
|
|
|
|
|
s.workflows = s.LoadWorkflows(s.config.Workflows)
|
|
|
|
|
}
|
2021-06-30 18:39:01 +05:30
|
|
|
|
2021-07-07 19:03:14 +05:30
|
|
|
// ValidateTemplates takes a list of templates and validates them
|
|
|
|
|
// erroring out on discovering any faulty templates.
|
2021-07-07 19:16:48 +05:30
|
|
|
func (s *Store) ValidateTemplates(templatesList, workflowsList []string) bool {
|
2021-07-07 19:03:14 +05:30
|
|
|
includedTemplates := s.config.Catalog.GetTemplatesPath(templatesList)
|
2021-07-07 19:15:09 +05:30
|
|
|
includedWorkflows := s.config.Catalog.GetTemplatesPath(workflowsList)
|
2021-07-07 19:03:14 +05:30
|
|
|
templatesMap := s.pathFilter.Match(includedTemplates)
|
2021-07-07 19:15:09 +05:30
|
|
|
workflowsMap := s.pathFilter.Match(includedWorkflows)
|
2021-07-07 19:03:14 +05:30
|
|
|
|
|
|
|
|
notErrored := true
|
|
|
|
|
for k := range templatesMap {
|
|
|
|
|
_, err := s.loadTemplate(k, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if strings.Contains(err.Error(), "cannot create template executer") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err == filter.ErrExcluded {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
notErrored = false
|
2021-07-07 19:05:26 +05:30
|
|
|
gologger.Error().Msgf("Error occurred loading template %s: %s\n", k, err)
|
2021-07-07 19:56:20 +05:30
|
|
|
continue
|
2021-07-07 19:03:14 +05:30
|
|
|
}
|
|
|
|
|
_, err = templates.Parse(k, s.config.ExecutorOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if strings.Contains(err.Error(), "cannot create template executer") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err == filter.ErrExcluded {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
notErrored = false
|
2021-07-07 19:05:26 +05:30
|
|
|
gologger.Error().Msgf("Error occurred parsing template %s: %s\n", k, err)
|
2021-07-07 19:03:14 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-07-07 19:15:09 +05:30
|
|
|
for k := range workflowsMap {
|
|
|
|
|
_, err := s.loadTemplate(k, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if strings.Contains(err.Error(), "cannot create template executer") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err == filter.ErrExcluded {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
notErrored = false
|
|
|
|
|
gologger.Error().Msgf("Error occurred loading workflow %s: %s\n", k, err)
|
|
|
|
|
}
|
|
|
|
|
_, err = templates.Parse(k, s.config.ExecutorOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if strings.Contains(err.Error(), "cannot create template executer") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err == filter.ErrExcluded {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
notErrored = false
|
|
|
|
|
gologger.Error().Msgf("Error occurred parsing workflow %s: %s\n", k, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-07 19:03:14 +05:30
|
|
|
return notErrored
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 04:35:53 +05:30
|
|
|
// LoadTemplates takes a list of templates and returns paths for them
|
|
|
|
|
func (s *Store) LoadTemplates(templatesList []string) []*templates.Template {
|
|
|
|
|
includedTemplates := s.config.Catalog.GetTemplatesPath(templatesList)
|
|
|
|
|
templatesMap := s.pathFilter.Match(includedTemplates)
|
2021-06-30 18:39:01 +05:30
|
|
|
|
2021-07-05 04:35:53 +05:30
|
|
|
loadedTemplates := make([]*templates.Template, 0, len(templatesMap))
|
2021-06-30 18:39:01 +05:30
|
|
|
for k := range templatesMap {
|
2021-07-05 04:35:53 +05:30
|
|
|
loaded, err := s.loadTemplate(k, false)
|
2021-06-30 18:39:01 +05:30
|
|
|
if err != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not load template %s: %s\n", k, err)
|
|
|
|
|
}
|
|
|
|
|
if loaded {
|
2021-07-01 14:36:40 +05:30
|
|
|
parsed, err := templates.Parse(k, s.config.ExecutorOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not parse template %s: %s\n", k, err)
|
|
|
|
|
} else if parsed != nil {
|
2021-07-05 04:35:53 +05:30
|
|
|
loadedTemplates = append(loadedTemplates, parsed)
|
2021-07-01 14:36:40 +05:30
|
|
|
}
|
2021-06-30 18:39:01 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-07-05 04:35:53 +05:30
|
|
|
return loadedTemplates
|
|
|
|
|
}
|
2021-06-30 18:39:01 +05:30
|
|
|
|
2021-07-05 04:35:53 +05:30
|
|
|
// LoadWorkflows takes a list of workflows and returns paths for them
|
|
|
|
|
func (s *Store) LoadWorkflows(workflowsList []string) []*templates.Template {
|
|
|
|
|
includedWorkflows := s.config.Catalog.GetTemplatesPath(s.config.Workflows)
|
|
|
|
|
workflowsMap := s.pathFilter.Match(includedWorkflows)
|
|
|
|
|
|
|
|
|
|
loadedWorkflows := make([]*templates.Template, 0, len(workflowsMap))
|
2021-06-30 18:39:01 +05:30
|
|
|
for k := range workflowsMap {
|
2021-07-05 04:35:53 +05:30
|
|
|
loaded, err := s.loadTemplate(k, true)
|
2021-06-30 18:39:01 +05:30
|
|
|
if err != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not load workflow %s: %s\n", k, err)
|
|
|
|
|
}
|
|
|
|
|
if loaded {
|
2021-07-01 14:36:40 +05:30
|
|
|
parsed, err := templates.Parse(k, s.config.ExecutorOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not parse workflow %s: %s\n", k, err)
|
|
|
|
|
} else if parsed != nil {
|
2021-07-05 04:35:53 +05:30
|
|
|
loadedWorkflows = append(loadedWorkflows, parsed)
|
2021-07-01 14:36:40 +05:30
|
|
|
}
|
2021-06-30 18:39:01 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-07-05 04:35:53 +05:30
|
|
|
return loadedWorkflows
|
2021-06-30 18:39:01 +05:30
|
|
|
}
|
|
|
|
|
|
2021-07-05 04:35:53 +05:30
|
|
|
func (s *Store) loadTemplate(templatePath string, workflow bool) (bool, error) {
|
2021-07-05 14:25:42 +05:30
|
|
|
return load.Load(templatePath, workflow, nil, s.tagFilter)
|
2021-06-30 18:39:01 +05:30
|
|
|
}
|