Skip to content

Commit

Permalink
misc changes to implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ice3man543 committed May 4, 2024
1 parent 53d07a6 commit 429e59e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
27 changes: 17 additions & 10 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,23 @@ func (r *Runner) RunEnumeration() error {
return errors.Wrap(err, "Could not create loader.")
}

// list all templates or tags as specified by user.
// This uses a separate parser to reduce time taken as
// normally nuclei does a lot of compilation and stuff
// for templates, which we don't want for these simp
if r.options.TemplateList || r.options.TemplateDisplay || r.options.TagList {
if err := store.LoadTemplatesOnlyMetadata(); err != nil {
return err
}

if r.options.TagList {
r.listAvailableStoreTags(store)
} else {
r.listAvailableStoreTemplates(store)
}
os.Exit(0)
}

if r.options.Validate {
if err := store.ValidateTemplates(); err != nil {
return err
Expand Down Expand Up @@ -540,16 +557,6 @@ func (r *Runner) RunEnumeration() error {
_ = r.inputProvider.SetWithExclusions(host)
}
}
// list all templates
if r.options.TemplateList || r.options.TemplateDisplay {
r.listAvailableStoreTemplates(store)
os.Exit(0)
}
// list all tags
if r.options.TagList {
r.listAvailableStoreTags(store)
os.Exit(0)
}
// display execution info like version , templates used etc
r.displayExecutionInfo(store)

Expand Down
32 changes: 22 additions & 10 deletions internal/runner/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package runner
import (
"bytes"
"path/filepath"
"sort"
"strings"

"github.com/alecthomas/chroma/quick"
jsoniter "github.com/json-iterator/go"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/loader"
Expand Down Expand Up @@ -77,21 +79,31 @@ func (r *Runner) listAvailableStoreTags(store *loader.Store) {
config.DefaultConfig.TemplateVersion,
config.DefaultConfig.TemplatesDirectory,
)
tags_set := make(map[string]int)
tagsMap := make(map[string]int)
for _, tpl := range store.Templates() {
for _, tag := range tpl.Info.Tags.ToSlice() {
prev_count, ok := tags_set[tag]

if !ok {
prev_count = 0
}

tags_set[tag] = prev_count + 1
tagsMap[tag]++
}
}
type kv struct {
Key string `json:"key"`
Value int `json:"value"`
}
var tagsList []kv
for k, v := range tagsMap {
tagsList = append(tagsList, kv{k, v})
}
sort.Slice(tagsList, func(i, j int) bool {
return tagsList[i].Value > tagsList[j].Value
})

for tag, count := range tags_set {
gologger.Silent().Msgf("%s\t%d\n", tag, count)
for _, tag := range tagsList {
if r.options.JSONL {
marshalled, _ := jsoniter.Marshal(tag)
gologger.Silent().Msgf("%s\n", string(marshalled))
} else {
gologger.Silent().Msgf("%s (%d)\n", tag.Key, tag.Value)
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/catalog/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,45 @@ func init() {
templateIDPathMap = make(map[string]string)
}

// LoadTemplatesOnlyMetadata loads only the metadata of the templates
func (store *Store) LoadTemplatesOnlyMetadata() error {
templatePaths, errs := store.config.Catalog.GetTemplatesPath(store.finalTemplates)
store.logErroredTemplates(errs)

filteredTemplatePaths := store.pathFilter.Match(templatePaths)

validPaths := make(map[string]struct{})
for templatePath := range filteredTemplatePaths {
loaded, err := store.config.ExecutorOptions.Parser.LoadTemplate(templatePath, store.tagFilter, nil, store.config.Catalog)
if loaded || store.pathFilter.MatchIncluded(templatePath) {
validPaths[templatePath] = struct{}{}
}
if err != nil {
if strings.Contains(err.Error(), templates.ErrExcluded.Error()) {
stats.Increment(templates.TemplatesExcludedStats)
if config.DefaultConfig.LogAllEvents {
gologger.Print().Msgf("[%v] %v\n", aurora.Yellow("WRN").String(), err.Error())
}
continue
}
gologger.Warning().Msg(err.Error())
}
}
parserItem, ok := store.config.ExecutorOptions.Parser.(*templates.Parser)
if !ok {
return errors.New("invalid parser")
}
templatesCache := parserItem.Cache()

for templatePath := range validPaths {
template, _, _ := templatesCache.Has(templatePath)
if template != nil {
store.templates = append(store.templates, template)
}
}
return nil
}

// ValidateTemplates takes a list of templates and validates them
// erroring out on discovering any faulty templates.
func (store *Store) ValidateTemplates() error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/templates/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func NewParser() *Parser {
return p
}

// Cache returns the parsed templates cache
func (p *Parser) Cache() *Cache {
return p.parsedTemplatesCache
}

// LoadTemplate returns true if the template is valid and matches the filtering criteria.
func (p *Parser) LoadTemplate(templatePath string, t any, extraTags []string, catalog catalog.Catalog) (bool, error) {
tagFilter, ok := t.(*TagFilter)
Expand Down

0 comments on commit 429e59e

Please sign in to comment.