Skip to content

Commit

Permalink
decrement confidence in time-based when duration is lower than the ex…
Browse files Browse the repository at this point in the history
…pected delay
  • Loading branch information
pyneda committed May 27, 2024
1 parent 2ad9e2c commit 3b860e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions pkg/payloads/generation/detection_methods.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package generation

import (
"github.com/rs/zerolog/log"
"strconv"
"time"

"github.com/rs/zerolog/log"
)

type DetectionMethod struct {
Expand Down Expand Up @@ -78,24 +79,25 @@ type TimeBasedDetectionMethod struct {
Confidence int `yaml:"confidence,omitempty"`
}

func (t *TimeBasedDetectionMethod) CheckIfResultDurationIsHigher(resultDuration time.Duration) bool {
sleepInt, err := strconv.Atoi(t.Sleep)
func (t *TimeBasedDetectionMethod) ParseSleepDuration(sleep string) time.Duration {
sleepInt, err := strconv.Atoi(sleep)
if err != nil {
log.Error().Err(err).Str("sleep", t.Sleep).Msg("Error converting sleep string to int")
return false
log.Error().Err(err).Str("sleep", sleep).Msg("Error converting sleep string to int")
return 0
}
// TODO: Improve this, the units should probably be defined in the templates
var sleepDuration time.Duration
// var unit string
if sleepInt >= 1000 {
sleepDuration = time.Duration(sleepInt) * time.Millisecond
// unit = "ms"
} else {
sleepDuration = time.Duration(sleepInt) * time.Second
// unit = "s"
}
return sleepDuration
}

func (t *TimeBasedDetectionMethod) CheckIfResultDurationIsHigher(resultDuration time.Duration) bool {
sleepDuration := t.ParseSleepDuration(t.Sleep)

if resultDuration >= sleepDuration {
if sleepDuration != 0 && resultDuration >= sleepDuration {
return true
}
return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (f *TemplateScanner) EvaluateDetectionMethod(result TemplateScannerResult,
sb.WriteString(fmt.Sprintf("Response took %s, which is greater than the sleep time injected in the payload of %s\n\n", result.Duration, m.Sleep))
// var originalResults []bool
// var payloadResults []bool

expectedSleepDuratoin := m.ParseSleepDuration(m.Sleep)
sb.WriteString("Revalidation results:\n")
sb.WriteString("=============================\n")

Expand Down Expand Up @@ -462,7 +462,7 @@ func (f *TemplateScanner) EvaluateDetectionMethod(result TemplateScannerResult,
finalConfidence += confidenceIncrement
}

if originalResult.duration > withPayloadResult.duration {
if originalResult.duration > withPayloadResult.duration || withPayloadResult.duration < expectedSleepDuratoin {
finalConfidence -= confidenceDecrement
}
// originalResults = append(originalResults, originalIsHigher)
Expand Down

0 comments on commit 3b860e2

Please sign in to comment.