-
Notifications
You must be signed in to change notification settings - Fork 621
feat: detect potential incremental routes and bruteforce them #388
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
Draft
Ullaakut
wants to merge
9
commits into
master
Choose a base branch
from
263-incremental-route-detection
base: master
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.
Draft
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
510a9af
feat: detect potential incremental routes and bruteforce them
Ullaakut 10bf1b5
fix: add max attempts to incremental routes to prevent infinite loop
Ullaakut cb83217
fix: improve attackRoute methods
Ullaakut dc4d6c9
fix: overflow fallback when detecting incremental route, regression test
Ullaakut 77a2eac
docs: fix erroneous function comment
Ullaakut a867a60
fix: prevent incorrect binding
Ullaakut fb7b3fd
test: add test cases for more edge cases
Ullaakut 1f06a07
fix: incremental attacks always use credentials
Ullaakut d589b61
fix: increment limit, added integration tests
Ullaakut 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| package attack | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| type incrementalRoute struct { | ||
| prefix string | ||
| suffix string | ||
| number int | ||
| width int | ||
| isChannel bool | ||
| } | ||
|
|
||
| // detectIncrementalRoute identifies routes that can be incremented. | ||
| // It prioritizes channel-like patterns to enable sequential scanning when possible. | ||
| // | ||
| // Examples of supported patterns: | ||
| // - /StreamingSetting?ChannelID=01&other=params -> /StreamingSetting?ChannelID=02&other=params | ||
| // - /path/to/channel2/stream -> /path/to/channel3/stream | ||
| // - /foo/bar12/baz -> /foo/bar13/baz | ||
| // | ||
| // It returns false if no incrementable pattern is found. | ||
| func detectIncrementalRoute(route string) (incrementalRoute, bool) { | ||
| if strings.TrimSpace(route) == "" { | ||
| return incrementalRoute{}, false | ||
| } | ||
|
|
||
| if match, ok := findChannelIncrement(route); ok { | ||
| match.isChannel = true | ||
| return match, true | ||
| } | ||
|
|
||
| match, ok := findLastNumber(route) | ||
| if !ok { | ||
| return incrementalRoute{}, false | ||
| } | ||
| return match, true | ||
| } | ||
|
|
||
| // findChannelIncrement locates a numeric segment tied to channel-like keywords. | ||
| // It returns the last match for the first keyword that yields a hit. | ||
| // | ||
| // Supported keywords include: channel_id, channelid, channelno, channel, channelname. | ||
| func findChannelIncrement(route string) (incrementalRoute, bool) { | ||
| patterns := []string{"channel_id", "channelid", "channelno", "channel", "channelname"} | ||
| lower := strings.ToLower(route) | ||
|
Ullaakut marked this conversation as resolved.
Ullaakut marked this conversation as resolved.
|
||
|
|
||
| for _, pattern := range patterns { | ||
| var lastMatch incrementalRoute | ||
| found := false | ||
| index := 0 | ||
|
|
||
| for { | ||
| pos := strings.Index(lower[index:], pattern) | ||
| if pos == -1 { | ||
| break | ||
| } | ||
| pos += index | ||
|
|
||
| start, end, ok := firstNumberAfterKey(route, pos+len(pattern)) | ||
| if ok { | ||
| num, width, parseOK := parseNumber(route, start, end) | ||
| if parseOK { | ||
| lastMatch = incrementalRoute{ | ||
| prefix: route[:start], | ||
| suffix: route[end:], | ||
| number: num, | ||
| width: width, | ||
| } | ||
| found = true | ||
| } | ||
| } | ||
| index = pos + len(pattern) | ||
| } | ||
| if found { | ||
| return lastMatch, true | ||
| } | ||
| } | ||
|
|
||
| return incrementalRoute{}, false | ||
| } | ||
|
|
||
| // findLastNumber finds the last numeric token in the route so it can be incremented. | ||
| // This supports routes where the channel number is not the final component. | ||
| func findLastNumber(route string) (incrementalRoute, bool) { | ||
| for i := len(route) - 1; i >= 0; { | ||
| if !isDigit(route[i]) { | ||
| i-- | ||
| continue | ||
| } | ||
|
|
||
| end := i + 1 | ||
| start := i | ||
| for start >= 0 && isDigit(route[start]) { | ||
| start-- | ||
| } | ||
| start++ | ||
|
|
||
| num, width, ok := parseNumber(route, start, end) | ||
| if !ok { | ||
| i = start - 1 | ||
| continue | ||
| } | ||
|
|
||
| return incrementalRoute{ | ||
| prefix: route[:start], | ||
| suffix: route[end:], | ||
| number: num, | ||
| width: width, | ||
| }, true | ||
| } | ||
|
|
||
| return incrementalRoute{}, false | ||
| } | ||
|
|
||
| // parseNumber reads the numeric token and returns its integer value and width. | ||
| func parseNumber(route string, start, end int) (int, int, bool) { | ||
| if start < 0 || end > len(route) || start >= end { | ||
| return 0, 0, false | ||
| } | ||
|
|
||
| value := route[start:end] | ||
| num, err := strconv.Atoi(value) | ||
| if err != nil { | ||
| return 0, 0, false | ||
| } | ||
|
|
||
| return num, len(value), true | ||
| } | ||
|
|
||
| // firstNumberAfterKey returns the first numeric token after a keyword, limited to | ||
| // the current token and requiring an '=' delimiter (query param or path segment). | ||
| func firstNumberAfterKey(route string, after int) (start, end int, ok bool) { | ||
| if after < 0 { | ||
| after = 0 | ||
| } | ||
|
|
||
| tokenEnd := len(route) | ||
| for i := after; i < len(route); i++ { | ||
| if isTokenDelimiter(route[i]) { | ||
| tokenEnd = i | ||
| break | ||
| } | ||
| } | ||
|
|
||
| relEq := strings.IndexByte(route[after:tokenEnd], '=') | ||
| searchStart := after | ||
| if relEq != -1 { | ||
| searchStart = after + relEq + 1 | ||
| } | ||
| for i := searchStart; i < tokenEnd; i++ { | ||
| if !isDigit(route[i]) { | ||
| if relEq == -1 { | ||
| break | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| end := i + 1 | ||
| for end < tokenEnd && isDigit(route[end]) { | ||
| end++ | ||
| } | ||
| return i, end, true | ||
| } | ||
|
|
||
| return 0, 0, false | ||
| } | ||
|
|
||
| // buildIncrementedRoute formats the route with the new numeric value. | ||
| // It preserves zero padding when the original token had a fixed width. | ||
| func buildIncrementedRoute(match incrementalRoute, number int) string { | ||
| if match.width <= 0 { | ||
| return match.prefix + strconv.Itoa(number) + match.suffix | ||
| } | ||
| return match.prefix + fmt.Sprintf("%0*d", match.width, number) + match.suffix | ||
| } | ||
|
|
||
| func isDigit(b byte) bool { | ||
| return b >= '0' && b <= '9' | ||
| } | ||
|
|
||
| func isTokenDelimiter(b byte) bool { | ||
| switch b { | ||
| case '&', '/', '?', '#': | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
Oops, something went wrong.
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.