-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat: added initial fuzz input url deduplication implementation #5594
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Package dedupe implements a duplicate URL deduplication mechanism | ||
// for Nuclei DAST or Fuzzing inputs. | ||
// | ||
// It is used to remove similar or non-relevant inputs from fuzzing | ||
// or DAST scans to reduce the number of requests made. | ||
package dedupe | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
"slices" | ||
"strings" | ||
|
||
mapsutil "github.com/projectdiscovery/utils/maps" | ||
) | ||
|
||
// FuzzingDeduper is a deduper for fuzzing inputs | ||
// | ||
// The normalization works as follows: | ||
// | ||
// - The path is normalized to remove any trailing slashes | ||
// - The query is normalized by templating the query parameters with their names | ||
// TODO: Doesn't handle different values, everything is stripped. Maybe make it more flexible? | ||
// - Numeric IDs in the path are replaced with {numeric_id} | ||
// | ||
// This allows us to deduplicate URLs with different query parameters | ||
// or orders but the same structure or key names. | ||
type FuzzingDeduper struct { | ||
items *mapsutil.SyncLockMap[string, struct{}] | ||
} | ||
|
||
// NewFuzzingDeduper creates a new fuzzing deduper | ||
func NewFuzzingDeduper() *FuzzingDeduper { | ||
return &FuzzingDeduper{ | ||
items: mapsutil.NewSyncLockMap[string, struct{}](), | ||
} | ||
} | ||
|
||
// Add adds a new URL to the deduper | ||
func (d *FuzzingDeduper) Add(URL string) bool { | ||
generatedPattern, err := generatePattern(URL) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
_, found := d.items.Get(generatedPattern) | ||
if found { | ||
return false | ||
} | ||
d.items.Set(generatedPattern, struct{}{}) | ||
return true | ||
} | ||
|
||
func generatePattern(urlStr string) (string, error) { | ||
parsedURL, err := url.ParseRequestURI(urlStr) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
path := normalizePath(parsedURL.Path) | ||
query := extractQuery(parsedURL.Query()) | ||
|
||
var builder strings.Builder | ||
builder.Grow(len(urlStr)) | ||
builder.WriteString(parsedURL.Scheme) | ||
builder.WriteString("://") | ||
builder.WriteString(parsedURL.Host) | ||
builder.WriteString(path) | ||
if query != "" { | ||
builder.WriteString("?") | ||
builder.WriteString(query) | ||
} | ||
pattern := builder.String() | ||
return pattern, nil | ||
} | ||
|
||
var ( | ||
numericIDPathRegex = regexp.MustCompile(`/(\d+)(?:/|$)`) | ||
) | ||
|
||
func normalizePath(path string) string { | ||
subMatches := numericIDPathRegex.FindAllStringSubmatch(path, -1) | ||
for _, match := range subMatches { | ||
path = strings.ReplaceAll(path, match[0], "/{numeric_id}") | ||
} | ||
return path | ||
} | ||
|
||
func extractQuery(query url.Values) string { | ||
normalizedParams := make([]string, 0, len(query)) | ||
|
||
for k, v := range query { | ||
if len(v) == 0 { | ||
normalizedParams = append(normalizedParams, k) | ||
} else { | ||
normalizedParams = append(normalizedParams, fmt.Sprintf("%s={%s}", k, k)) | ||
} | ||
} | ||
slices.Sort(normalizedParams) | ||
return strings.Join(normalizedParams, "&") | ||
} |
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,137 @@ | ||
package dedupe | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFuzzingDeduper(t *testing.T) { | ||
t.Run("Basic URL Deduplication", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
urls []string | ||
expected []bool | ||
}{ | ||
{ | ||
name: "Simple unique URLs", | ||
urls: []string{"http://example.com/page1", "http://example.com/page2"}, | ||
expected: []bool{true, true}, | ||
}, | ||
{ | ||
name: "Duplicate URLs", | ||
urls: []string{"http://example.com/page1", "http://example.com/page1"}, | ||
expected: []bool{true, false}, | ||
}, | ||
{ | ||
name: "URLs with different query param values", | ||
urls: []string{"http://example.com/page?id=1", "http://example.com/page?id=2"}, | ||
expected: []bool{true, false}, | ||
}, | ||
{ | ||
name: "URLs with different query param orders", | ||
urls: []string{"http://example.com/page?a=1&b=2", "http://example.com/page?b=2&a=1"}, | ||
expected: []bool{true, false}, | ||
}, | ||
{ | ||
name: "URLs with and without trailing slash", | ||
urls: []string{"http://example.com/page/", "http://example.com/page"}, | ||
expected: []bool{true, true}, | ||
}, | ||
{ | ||
name: "URLs with different schemes", | ||
urls: []string{"http://example.com", "https://example.com"}, | ||
expected: []bool{true, true}, | ||
}, | ||
{ | ||
name: "URLs with query params and without", | ||
urls: []string{"http://example.com/page", "http://example.com/page?param=value"}, | ||
expected: []bool{true, true}, | ||
}, | ||
{ | ||
name: "Invalid URLs", | ||
urls: []string{"http://example.com/page", "not a valid url"}, | ||
expected: []bool{true, false}, | ||
}, | ||
{ | ||
name: "URLs with empty query params", | ||
urls: []string{"http://example.com/page?param1=¶m2=", "http://example.com/page?param2=¶m1="}, | ||
expected: []bool{true, false}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
deduper := NewFuzzingDeduper() | ||
for i, url := range tt.urls { | ||
result := deduper.Add(url) | ||
require.Equal(t, tt.expected[i], result, "Add(%q) = %v, want %v", url, result, tt.expected[i]) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("Large Set Deduplication", func(t *testing.T) { | ||
deduper := NewFuzzingDeduper() | ||
baseURL := "http://example.com/page?id=%d¶m=%s" | ||
|
||
for i := 0; i < 1000; i++ { | ||
url := fmt.Sprintf(baseURL, i, "value") | ||
result := deduper.Add(url) | ||
if i == 0 { | ||
require.True(t, result, "First URL should be added") | ||
} else { | ||
require.False(t, result, "Duplicate URL pattern should not be added: %s", url) | ||
} | ||
} | ||
|
||
allItems := deduper.items.GetAll() | ||
require.Len(t, allItems, 1, "Expected 1 unique URL pattern, got %d", len(allItems)) | ||
}) | ||
|
||
t.Run("Path Parameters", func(t *testing.T) { | ||
deduper := NewFuzzingDeduper() | ||
|
||
require.True(t, deduper.Add("https://example.com/page/1337")) | ||
require.False(t, deduper.Add("https://example.com/page/1332")) | ||
}) | ||
|
||
t.Run("TestPHP Vulnweb URLs", func(t *testing.T) { | ||
urls := []string{ | ||
"http://testphp.vulnweb.com/hpp/?pp=12", | ||
"http://testphp.vulnweb.com/hpp/params.php?p=valid&pp=12", | ||
"http://testphp.vulnweb.com/artists.php?artist=3", | ||
"http://testphp.vulnweb.com/artists.php?artist=1", | ||
"http://testphp.vulnweb.com/artists.php?artist=2", | ||
"http://testphp.vulnweb.com/listproducts.php?artist=3", | ||
"http://testphp.vulnweb.com/listproducts.php?cat=4", | ||
"http://testphp.vulnweb.com/listproducts.php?cat=3", | ||
"http://testphp.vulnweb.com/listproducts.php?cat=2", | ||
"http://testphp.vulnweb.com/listproducts.php?artist=2", | ||
"http://testphp.vulnweb.com/listproducts.php?artist=1", | ||
"http://testphp.vulnweb.com/listproducts.php?cat=1", | ||
"http://testphp.vulnweb.com/showimage.php?file=./pictures/6.jpg", | ||
"http://testphp.vulnweb.com/product.php?pic=6", | ||
"http://testphp.vulnweb.com/showimage.php?file=./pictures/6.jpg&size=160", | ||
} | ||
|
||
expectedUnique := 8 | ||
|
||
deduper := NewFuzzingDeduper() | ||
uniqueCount := 0 | ||
|
||
for _, url := range urls { | ||
if deduper.Add(url) { | ||
uniqueCount++ | ||
} | ||
} | ||
|
||
require.Equal(t, expectedUnique, uniqueCount, "Expected %d unique URLs, but got %d", expectedUnique, uniqueCount) | ||
|
||
// Test for duplicates | ||
for _, url := range urls { | ||
require.False(t, deduper.Add(url), "URL should have been identified as duplicate: %s", url) | ||
} | ||
}) | ||
} |
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.
We can consider using https://github.com/projectdiscovery/utils/blob/main/dedupe/dedupe_test.go