Skip to content

Commit

Permalink
misc changes to implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ice3man543 committed Nov 13, 2024
1 parent f6926b5 commit 311198f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 35 deletions.
36 changes: 5 additions & 31 deletions pkg/fuzz/analyzers/time/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const (
DefaultRequestsLimit = int(4)
DefaultTimeCorrelationErrorRange = float64(0.15)
DefaultTimeSlopeErrorRange = float64(0.30)
DefaultTimeUnit = "seconds"

defaultSleepTimeDuration = 5 * time.Second
)
Expand Down Expand Up @@ -56,10 +55,6 @@ 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)
}
}
data = strings.ReplaceAll(data, "[SLEEPTIME]", strconv.Itoa(duration))
data = analyzers.ApplyPayloadTransformations(data)
Expand All @@ -72,29 +67,18 @@ func (a *Analyzer) ApplyInitialTransformation(data string, params map[string]int
return data
}

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) {
func (a *Analyzer) parseAnalyzerParameters(params map[string]interface{}) (int, int, float64, float64, error) {
requestsLimit := DefaultRequestsLimit
sleepDuration := DefaultSleepDuration
timeCorrelationErrorRange := DefaultTimeCorrelationErrorRange
timeSlopeErrorRange := DefaultTimeSlopeErrorRange
timeUnit := DefaultTimeUnit

if len(params) == 0 {
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, timeUnit, nil
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, 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 @@ -105,10 +89,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, timeUnit, nil
return requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, nil
}

// Analyze is the main function for the analyzer
Expand All @@ -118,23 +102,13 @@ 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, customUnit, err :=
requestsLimit, sleepDuration, timeCorrelationErrorRange, timeSlopeErrorRange, 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
3 changes: 1 addition & 2 deletions pkg/protocols/http/build_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ type generatedRequest struct {
// ex: {{BaseURL}}/api/exp?param={{randstr}}
requestURLPattern string

fuzzGeneratedRequest fuzz.GeneratedRequest
analyzerPreReqResponse map[string]interface{}
fuzzGeneratedRequest fuzz.GeneratedRequest
}

// setReqURLPattern sets the url request pattern for the generated request
Expand Down
6 changes: 5 additions & 1 deletion pkg/protocols/http/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
if value, ok := wrapped.InternalEvent["global-matchers"]; ok {
isGlobalMatchers = value.(bool)
}
var analyzerDetails string
if value, ok := wrapped.InternalEvent["analyzer_details"]; ok {
analyzerDetails = value.(string)
}
data := &output.ResultEvent{
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
Expand All @@ -193,7 +197,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
CURLCommand: types.ToString(wrapped.InternalEvent["curl-command"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
AnalyzerDetails: types.ToString(wrapped.InternalEvent["analyzer_details"]),
AnalyzerDetails: analyzerDetails,
}
return data
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocols/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ
}
if analysisMatched {
finalEvent["analyzer_details"] = analysisDetails
finalEvent["analyzer_matched"] = true
finalEvent["analyzer"] = true
}
}

Expand Down

0 comments on commit 311198f

Please sign in to comment.