Skip to content
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 time based delay analyzer to fuzzing implementation #5781

Merged
merged 18 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions pkg/fuzz/analyzers/time/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
DefaultRequestsLimit = int(4)
DefaultTimeCorrelationErrorRange = float64(0.15)
DefaultTimeSlopeErrorRange = float64(0.30)
DefaultTimeUnit = "seconds"

defaultSleepTimeDuration = 5 * time.Second
)
Expand Down Expand Up @@ -55,28 +56,45 @@ func (a *Analyzer) ApplyInitialTransformation(data string, params map[string]int
gologger.Warning().Msgf("Invalid sleep_duration parameter type, using default value: %d", duration)
}
}
// Default unit is second. If we get passed milliseconds, multiply
if unit, ok := params["time_unit"]; ok {
duration = a.handleCustomTimeUnit(unit.(string), duration)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more reliable to use time.ParseDuration directly and skip having a custom handler for it? So we could remove the "time_unit" param entirely and make "sleep_duration" a string that gets parsed into time.Duration. What do you think, @Ice3man543?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, will make it like so.

}
data = strings.ReplaceAll(data, "[SLEEPTIME]", strconv.Itoa(duration))
data = analyzers.ApplyPayloadTransformations(data)

// Also support [INFERENCE] for the time delay analyzer
randInt := analyzers.GetRandomInteger()
data = strings.ReplaceAll(data, "[INFERENCE]", fmt.Sprintf("%d=%d", randInt, randInt))
if strings.Contains(data, "[INFERENCE]") {
randInt := analyzers.GetRandomInteger()
data = strings.ReplaceAll(data, "[INFERENCE]", fmt.Sprintf("%d=%d", randInt, randInt))
}
return data
}

func (a *Analyzer) parseAnalyzerParameters(params map[string]interface{}) (int, int, float64, float64, error) {
func (a *Analyzer) handleCustomTimeUnit(unit string, duration int) int {
switch unit {
case "milliseconds":
return duration * 1000
}
return duration
}

func (a *Analyzer) parseAnalyzerParameters(params map[string]interface{}) (int, int, float64, float64, string, error) {
requestsLimit := DefaultRequestsLimit
sleepDuration := DefaultSleepDuration
timeCorrelationErrorRange := DefaultTimeCorrelationErrorRange
timeSlopeErrorRange := DefaultTimeSlopeErrorRange
timeUnit := DefaultTimeUnit

if len(params) == 0 {
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, nil
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, timeUnit, nil
}
var ok bool
for k, v := range params {
switch k {
case "time_unit":
timeUnit, ok = v.(string)
case "sleep_duration":
sleepDuration, ok = v.(int)
case "requests_limit":
Expand All @@ -87,10 +105,10 @@ func (a *Analyzer) parseAnalyzerParameters(params map[string]interface{}) (int,
timeSlopeErrorRange, ok = v.(float64)
}
if !ok {
return 0, 0, 0, 0, errors.Errorf("invalid parameter type for %s", k)
return 0, 0, 0, 0, "", errors.Errorf("invalid parameter type for %s", k)
}
}
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, nil
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, timeUnit, nil
}

// Analyze is the main function for the analyzer
Expand All @@ -100,13 +118,23 @@ func (a *Analyzer) Analyze(options *analyzers.Options) (bool, string, error) {
}

// Parse parameters for this analyzer if any or use default values
requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, err :=
requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, customUnit, err :=
a.parseAnalyzerParameters(options.AnalyzerParameters)
if err != nil {
return false, "", err
}

// If custom unit is passed, handle it
if customUnit != DefaultTimeUnit {
sleepDuration = a.handleCustomTimeUnit(customUnit, sleepDuration)
}

reqSender := func(delay int) (float64, error) {
// If custom unit is passed, handle it
if customUnit != DefaultTimeUnit {
delay = a.handleCustomTimeUnit(customUnit, delay)
}

gr := options.FuzzGenerated
replaced := strings.ReplaceAll(gr.OriginalPayload, "[SLEEPTIME]", strconv.Itoa(delay))
replaced = a.ApplyInitialTransformation(replaced, options.AnalyzerParameters)
Expand Down
2 changes: 1 addition & 1 deletion pkg/fuzz/analyzers/time/time_delay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Test_should_giveup_slow_non_injectable_realworld(t *testing.T) {
matched, _, err := checkTimingDependency(4, 15, correlationErrorRange, slopeErrorRange, reqSender)
require.NoError(t, err)
require.False(t, matched)
require.LessOrEqual(t, timesCalled, 3)
require.LessOrEqual(t, timesCalled, 4)
}

func Test_should_detect_dependence_with_small_error(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions pkg/fuzz/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,6 @@ func (rule *Rule) executeRuleValues(input *ExecuteRuleInput, ruleComponent compo
for _, value := range rule.Fuzz.Value {
originalPayload := value

if input.ApplyPayloadInitialTransformation != nil {
value = input.ApplyPayloadInitialTransformation(value, input.AnalyzerParams)
}
if err := rule.executePartRule(input, ValueOrKeyValue{Value: value, OriginalPayload: originalPayload}, ruleComponent); err != nil {
if component.IsErrSetValue(err) {
// this are errors due to format restrictions
Expand Down
2 changes: 2 additions & 0 deletions pkg/fuzz/parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func (rule *Rule) executePartComponentOnValues(input *ExecuteRuleInput, payloadS
var evaluated, originalEvaluated string
evaluated, input.InteractURLs = rule.executeEvaluate(input, key, valueStr, payloadStr, input.InteractURLs)
if input.ApplyPayloadInitialTransformation != nil {
evaluated = input.ApplyPayloadInitialTransformation(evaluated, input.AnalyzerParams)
originalEvaluated, _ = rule.executeEvaluate(input, key, valueStr, originalPayload, input.InteractURLs)
}

if err := ruleComponent.SetValue(key, evaluated); err != nil {
// gologger.Warning().Msgf("could not set value due to format restriction original(%s, %s[%T]) , new(%s,%s[%T])", key, valueStr, value, key, evaluated, evaluated)
return nil
Expand Down
Loading