-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
disable non default templates as default #5370
Open
dogancanbakir
wants to merge
5
commits into
dev
Choose a base branch
from
disable_non_default_templates
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -5,17 +5,20 @@ import ( | |
"io" | ||
"net/url" | ||
"os" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/Knetic/govaluate" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/pkg/errors" | ||
"github.com/projectdiscovery/gologger" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/catalog" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/loader/filter" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/operators/common/dsl" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/protocols" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/templates" | ||
templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types" | ||
|
@@ -24,9 +27,11 @@ import ( | |
"github.com/projectdiscovery/nuclei/v3/pkg/workflows" | ||
"github.com/projectdiscovery/retryablehttp-go" | ||
errorutil "github.com/projectdiscovery/utils/errors" | ||
fileutil "github.com/projectdiscovery/utils/file" | ||
sliceutil "github.com/projectdiscovery/utils/slice" | ||
stringsutil "github.com/projectdiscovery/utils/strings" | ||
urlutil "github.com/projectdiscovery/utils/url" | ||
"github.com/samber/lo" | ||
) | ||
|
||
const ( | ||
|
@@ -465,6 +470,22 @@ func (store *Store) LoadTemplatesWithTags(templatesList, tags []string) []*templ | |
stats.Increment(templates.SkippedUnsignedStats) | ||
return | ||
} | ||
|
||
if parsed.SelfContained && | ||
store.config.ExecutorOptions.Options.Vars.IsEmpty() && !store.config.ExecutorOptions.Options.EnvironmentVariables && | ||
templateContainsUnresolvedVariables(templatePath) { | ||
stats.Increment(templates.SkippedSelfContainedStats) | ||
return | ||
} | ||
|
||
if parsed.HasFileProtocol() && | ||
lo.NoneBy(store.config.ExecutorOptions.Options.Targets, func(target string) bool { | ||
return fileutil.FileOrFolderExists(target) | ||
}) { | ||
stats.Increment(templates.SkippedFileStats) | ||
return | ||
} | ||
|
||
// if template has request signature like aws then only signed and verified templates are allowed | ||
if parsed.UsesRequestSignature() && !parsed.Verified { | ||
stats.Increment(templates.SkippedRequestSignatureStats) | ||
|
@@ -528,6 +549,55 @@ func (store *Store) LoadTemplatesWithTags(templatesList, tags []string) []*templ | |
return loadedTemplates.Slice | ||
} | ||
|
||
var ( | ||
numericalExpressionRegex = regexp.MustCompile(`^[0-9+\-/\W]+$`) | ||
unresolvedVariablesRegex = regexp.MustCompile(`(?:%7[B|b]|\{){2}([^}]+)(?:%7[D|d]|\}){2}["'\)\}]*`) | ||
) | ||
|
||
// copy of the original function from pkg/protocols/common/expressions/variables.go:ContainsUnresolvedVariables | ||
func templateContainsUnresolvedVariables(templatePath string) bool { | ||
data, err := os.ReadFile(templatePath) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
matches := unresolvedVariablesRegex.FindAllStringSubmatch(string(data), -1) | ||
if len(matches) == 0 { | ||
return false | ||
} | ||
|
||
var unresolvedVariables []string | ||
for _, match := range matches { | ||
if len(match) < 2 { | ||
continue | ||
} | ||
|
||
// Skip if the match is an expression | ||
if numericalExpressionRegex.MatchString(match[1]) { | ||
continue | ||
} | ||
// or if it contains only literals (can be solved from expression engine) | ||
if hasLiteralsOnly(match[1]) { | ||
continue | ||
} | ||
unresolvedVariables = append(unresolvedVariables, match[1]) | ||
} | ||
|
||
return len(unresolvedVariables) > 0 | ||
} | ||
|
||
func hasLiteralsOnly(data string) bool { | ||
expr, err := govaluate.NewEvaluableExpressionWithFunctions(data, dsl.HelperFunctions) | ||
if err != nil { | ||
return false | ||
} | ||
if expr != nil { | ||
_, err = expr.Evaluate(nil) | ||
return err == nil | ||
} | ||
Comment on lines
+552
to
+597
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this is duplicated i think we can move it to pkg/utils because to avoid any inconsistencies in future and we can reuse it for other purpose as well |
||
return true | ||
} | ||
|
||
// IsHTTPBasedProtocolUsed returns true if http/headless protocol is being used for | ||
// any templates. | ||
func IsHTTPBasedProtocolUsed(store *Store) bool { | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we should compute this check here , the stats definetly belong here but checking everytime if target is a file for every template might increase latency on small hosts/boxes . i think it would be better if we precalcuate and put it in types.Options ( we do this for headless,DAST and other filtering ) so we could also add 2 options
another reason for this is that nuclei supports target from various flags
-u , -l
and even input mode and this logic currently only checks for values of-u
and not-l