-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add XSS context analyzer with AST-based detection #7063
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
Open
odvcencio
wants to merge
4
commits into
projectdiscovery:dev
Choose a base branch
from
odvcencio:feat/xss-context-analyzer
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.
+1,025
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
538753f
add(fuzz): XSS context analyzer with AST-based detection
odvcencio 02b04d1
add(fuzz/xss): add FindReflections benchmark for all context types
odvcencio 1884b13
test(xss): add real-world HTML reflection tests
odvcencio ffb78c0
update(XSS analyzer (#confused)): fix XSS context analyzer edge cases…
odvcencio 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 hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| package xss | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/projectdiscovery/nuclei/v3/pkg/fuzz/analyzers" | ||
| ) | ||
|
|
||
| // Analyzer is an XSS context analyzer for the fuzzer. | ||
| // It injects a canary value and uses gotreesitter AST parsing to determine | ||
| // the precise HTML/JS/CSS context of each reflection point. | ||
| type Analyzer struct{} | ||
|
|
||
| var _ analyzers.Analyzer = &Analyzer{} | ||
|
|
||
| const defaultMaxResponseBodySize = 10 * 1024 * 1024 // 10 MB | ||
|
|
||
| func init() { | ||
| analyzers.RegisterAnalyzer("xss_context", &Analyzer{}) | ||
| } | ||
|
|
||
| // Name returns the name of this analyzer. | ||
| func (a *Analyzer) Name() string { | ||
| return "xss_context" | ||
| } | ||
|
|
||
| // ApplyInitialTransformation applies payload transformations. | ||
| // For XSS context analysis, we don't transform the initial payload since | ||
| // we generate our own canary in Analyze(). | ||
| func (a *Analyzer) ApplyInitialTransformation(data string, params map[string]interface{}) string { | ||
| return analyzers.ApplyPayloadTransformations(data) | ||
| } | ||
|
|
||
| // Analyze sends a request with a unique canary value and determines the | ||
| // XSS context of all reflection points using AST parsing. | ||
| func (a *Analyzer) Analyze(options *analyzers.Options) (bool, string, error) { | ||
| canary := generateCanary() | ||
|
|
||
| gr := options.FuzzGenerated | ||
| if err := gr.Component.SetValue(gr.Key, canary); err != nil { | ||
| return false, "", errors.Wrap(err, "could not set canary value") | ||
| } | ||
|
|
||
| rebuilt, err := gr.Component.Rebuild() | ||
| if err != nil { | ||
| return false, "", errors.Wrap(err, "could not rebuild request with canary") | ||
| } | ||
|
|
||
| resp, err := options.HttpClient.Do(rebuilt) | ||
| if err != nil { | ||
| return false, "", errors.Wrap(err, "could not send canary request") | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(io.LimitReader(resp.Body, defaultMaxResponseBodySize)) | ||
| if err != nil { | ||
| return false, "", errors.Wrap(err, "could not read response body") | ||
| } | ||
|
|
||
| // Quick check: if canary is not reflected at all, no XSS | ||
| if !strings.Contains(string(body), canary) { | ||
| return false, "", nil | ||
| } | ||
|
|
||
| // Parse HTML and find all reflection contexts | ||
| points := findReflections(body, canary) | ||
| if len(points) == 0 { | ||
| return false, "", nil | ||
| } | ||
|
|
||
| // Build details string listing all unique contexts | ||
| seen := make(map[XSSContext]bool) | ||
| var details []string | ||
| for _, p := range points { | ||
| if !seen[p.Context] { | ||
| seen[p.Context] = true | ||
| details = append(details, p.Context.String()) | ||
| } | ||
| } | ||
|
|
||
| return true, strings.Join(details, ", "), nil | ||
| } | ||
|
|
||
| // generateCanary creates a unique canary string unlikely to appear in natural content. | ||
| // Format: "gtss" + 8 random alphanumeric characters. | ||
| func generateCanary() string { | ||
| const charset = "abcdefghijklmnopqrstuvwxyz0123456789" | ||
| b := make([]byte, 8) | ||
| if _, err := rand.Read(b); err != nil { | ||
| // Fallback to a fixed canary if crypto/rand fails (extremely unlikely) | ||
| return "gtss00000000" | ||
| } | ||
| for i := range b { | ||
| b[i] = charset[b[i]%byte(len(charset))] | ||
| } | ||
| return "gtss" + string(b) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.