-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workflows restructured to work with filters + tag support
- Loading branch information
1 parent
d45d1c9
commit 0726acc
Showing
12 changed files
with
461 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package filter | ||
|
||
import "github.com/projectdiscovery/nuclei/v2/pkg/catalog" | ||
|
||
// PathFilter is a path based template filter | ||
type PathFilter struct { | ||
excludedTemplates []string | ||
alwaysIncludedTemplatesMap map[string]struct{} | ||
} | ||
|
||
// PathFilterConfig contains configuraton options for Path based templates Filter | ||
type PathFilterConfig struct { | ||
IncludedTemplates []string | ||
ExcludedTemplates []string | ||
} | ||
|
||
// NewPathFilter creates a new path filter from provided config | ||
func NewPathFilter(config *PathFilterConfig, catalog *catalog.Catalog) *PathFilter { | ||
filter := &PathFilter{ | ||
excludedTemplates: catalog.GetTemplatesPath(config.ExcludedTemplates), | ||
alwaysIncludedTemplatesMap: make(map[string]struct{}), | ||
} | ||
|
||
alwaysIncludeTemplates := catalog.GetTemplatesPath(config.IncludedTemplates) | ||
for _, tpl := range alwaysIncludeTemplates { | ||
filter.alwaysIncludedTemplatesMap[tpl] = struct{}{} | ||
} | ||
return filter | ||
} | ||
|
||
// Match performs match for path filter on templates and returns final list | ||
func (p *PathFilter) Match(templates []string) map[string]struct{} { | ||
templatesMap := make(map[string]struct{}) | ||
for _, tpl := range templates { | ||
templatesMap[tpl] = struct{}{} | ||
} | ||
for _, template := range p.excludedTemplates { | ||
if _, ok := p.alwaysIncludedTemplatesMap[template]; ok { | ||
continue | ||
} else { | ||
delete(templatesMap, template) | ||
} | ||
} | ||
return templatesMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package load | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader/filter" | ||
"github.com/projectdiscovery/nuclei/v2/pkg/types" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Load loads a template by parsing metadata and running | ||
// all tag and path based filters on the template. | ||
func Load(templatePath string, workflow bool, customTags []string, tagFilter *filter.TagFilter) (bool, error) { | ||
f, err := os.Open(templatePath) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
data, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
template := make(map[string]interface{}) | ||
err = yaml.NewDecoder(bytes.NewReader(data)).Decode(template) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
info, ok := template["info"] | ||
if !ok { | ||
return false, errors.New("no template info field provided") | ||
} | ||
infoMap := info.(map[interface{}]interface{}) | ||
|
||
if _, ok := infoMap["name"]; !ok { | ||
return false, errors.New("no template name field provided") | ||
} | ||
author, ok := infoMap["author"] | ||
if !ok { | ||
return false, errors.New("no template author field provided") | ||
} | ||
severity, ok := infoMap["severity"] | ||
if !ok { | ||
severity = "" | ||
} | ||
|
||
templateTags, ok := infoMap["tags"] | ||
if !ok { | ||
templateTags = "" | ||
} | ||
tagStr := types.ToString(templateTags) | ||
|
||
tags := strings.Split(tagStr, ",") | ||
severityStr := types.ToString(severity) | ||
authors := strings.Split(types.ToString(author), ",") | ||
|
||
matched := false | ||
|
||
for _, tag := range tags { | ||
for _, author := range authors { | ||
var match bool | ||
var err error | ||
|
||
if len(customTags) > 0 { | ||
match, err = tagFilter.Match(strings.TrimSpace(tag), strings.TrimSpace(author), severityStr) | ||
} else { | ||
match, err = tagFilter.MatchWithAllowedTags(customTags, strings.TrimSpace(tag), strings.TrimSpace(author), severityStr) | ||
} | ||
if err == filter.ErrExcluded { | ||
return false, filter.ErrExcluded | ||
} | ||
if !matched && match && err == nil { | ||
matched = true | ||
} | ||
} | ||
} | ||
if !matched { | ||
return false, nil | ||
} | ||
_, workflowsFound := template["workflows"] | ||
|
||
if !workflowsFound && workflow { | ||
return false, nil | ||
} | ||
if workflowsFound && !workflow { | ||
return false, nil | ||
} | ||
return true, nil | ||
} |
Oops, something went wrong.