2020-11-30 09:37:17 +00:00
|
|
|
package csprofiles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-22 09:29:52 +00:00
|
|
|
"time"
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
"github.com/antonmedv/expr"
|
2022-06-22 09:29:52 +00:00
|
|
|
"github.com/antonmedv/expr/vm"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-06-22 09:29:52 +00:00
|
|
|
type Runtime struct {
|
|
|
|
RuntimeFilters []*vm.Program `json:"-" yaml:"-"`
|
|
|
|
DebugFilters []*exprhelpers.ExprDebugger `json:"-" yaml:"-"`
|
|
|
|
RuntimeDurationExpr *vm.Program `json:"-" yaml:"-"`
|
|
|
|
DebugDurationExpr *exprhelpers.ExprDebugger `json:"-" yaml:"-"`
|
|
|
|
Cfg *csconfig.ProfileCfg `json:"-" yaml:"-"`
|
|
|
|
Logger *log.Entry `json:"-" yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultDuration = "4h"
|
|
|
|
|
|
|
|
func NewProfile(profilesCfg []*csconfig.ProfileCfg) ([]*Runtime, error) {
|
|
|
|
var err error
|
|
|
|
profilesRuntime := make([]*Runtime, 0)
|
|
|
|
|
|
|
|
for _, profile := range profilesCfg {
|
|
|
|
var runtimeFilter, runtimeDurationExpr *vm.Program
|
|
|
|
var debugFilter, debugDurationExpr *exprhelpers.ExprDebugger
|
|
|
|
runtime := &Runtime{}
|
|
|
|
xlog := log.New()
|
|
|
|
if err := types.ConfigureLogger(xlog); err != nil {
|
|
|
|
log.Fatalf("While creating profiles-specific logger : %s", err)
|
|
|
|
}
|
|
|
|
xlog.SetLevel(log.InfoLevel)
|
|
|
|
runtime.Logger = xlog.WithFields(log.Fields{
|
|
|
|
"type": "profile",
|
|
|
|
"name": profile.Name,
|
|
|
|
})
|
|
|
|
|
|
|
|
runtime.RuntimeFilters = make([]*vm.Program, len(profile.Filters))
|
|
|
|
runtime.DebugFilters = make([]*exprhelpers.ExprDebugger, len(profile.Filters))
|
|
|
|
runtime.Cfg = profile
|
2023-01-19 07:45:50 +00:00
|
|
|
if runtime.Cfg.OnSuccess != "" && runtime.Cfg.OnSuccess != "continue" && runtime.Cfg.OnSuccess != "break" {
|
|
|
|
return []*Runtime{}, errors.Wrapf(err, "invalid 'on_success' for '%s' : %s", profile.Name, runtime.Cfg.OnSuccess)
|
|
|
|
}
|
|
|
|
if runtime.Cfg.OnFailure != "" && runtime.Cfg.OnFailure != "continue" && runtime.Cfg.OnFailure != "break" && runtime.Cfg.OnFailure != "apply" {
|
|
|
|
return []*Runtime{}, errors.Wrapf(err, "invalid 'on_failure' for '%s' : %s", profile.Name, runtime.Cfg.OnFailure)
|
|
|
|
}
|
2022-06-22 09:29:52 +00:00
|
|
|
for fIdx, filter := range profile.Filters {
|
|
|
|
if runtimeFilter, err = expr.Compile(filter, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"Alert": &models.Alert{}}))); err != nil {
|
|
|
|
return []*Runtime{}, errors.Wrapf(err, "error compiling filter of '%s'", profile.Name)
|
|
|
|
}
|
|
|
|
runtime.RuntimeFilters[fIdx] = runtimeFilter
|
|
|
|
if profile.Debug != nil && *profile.Debug {
|
|
|
|
if debugFilter, err = exprhelpers.NewDebugger(filter, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"Alert": &models.Alert{}}))); err != nil {
|
|
|
|
log.Debugf("Error compiling debug filter of %s : %s", profile.Name, err)
|
|
|
|
// Don't fail if we can't compile the filter - for now
|
|
|
|
// return errors.Wrapf(err, "Error compiling debug filter of %s", profile.Name)
|
|
|
|
}
|
|
|
|
runtime.DebugFilters[fIdx] = debugFilter
|
|
|
|
runtime.Logger.Logger.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile.DurationExpr != "" {
|
|
|
|
if runtimeDurationExpr, err = expr.Compile(profile.DurationExpr, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"Alert": &models.Alert{}}))); err != nil {
|
|
|
|
return []*Runtime{}, errors.Wrapf(err, "error compiling duration_expr of %s", profile.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.RuntimeDurationExpr = runtimeDurationExpr
|
|
|
|
if profile.Debug != nil && *profile.Debug {
|
|
|
|
if debugDurationExpr, err = exprhelpers.NewDebugger(profile.DurationExpr, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"Alert": &models.Alert{}}))); err != nil {
|
|
|
|
log.Debugf("Error compiling debug duration_expr of %s : %s", profile.Name, err)
|
|
|
|
}
|
|
|
|
runtime.DebugDurationExpr = debugDurationExpr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, decision := range profile.Decisions {
|
|
|
|
if runtime.RuntimeDurationExpr == nil {
|
|
|
|
if _, err := time.ParseDuration(*decision.Duration); err != nil {
|
|
|
|
return []*Runtime{}, errors.Wrapf(err, "error parsing duration '%s' of %s", *decision.Duration, profile.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
profilesRuntime = append(profilesRuntime, runtime)
|
|
|
|
}
|
|
|
|
return profilesRuntime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Profile *Runtime) GenerateDecisionFromProfile(Alert *models.Alert) ([]*models.Decision, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
var decisions []*models.Decision
|
|
|
|
|
2022-06-22 09:29:52 +00:00
|
|
|
for _, refDecision := range Profile.Cfg.Decisions {
|
2020-11-30 09:37:17 +00:00
|
|
|
decision := models.Decision{}
|
|
|
|
/*the reference decision from profile is in sumulated mode */
|
|
|
|
if refDecision.Simulated != nil && *refDecision.Simulated {
|
|
|
|
decision.Simulated = new(bool)
|
|
|
|
*decision.Simulated = true
|
|
|
|
/*the event is already in simulation mode */
|
|
|
|
} else if Alert.Simulated != nil && *Alert.Simulated {
|
|
|
|
decision.Simulated = new(bool)
|
|
|
|
*decision.Simulated = true
|
|
|
|
}
|
|
|
|
/*If the profile specifies a scope, this will prevail.
|
|
|
|
If not, we're going to get the scope from the source itself*/
|
|
|
|
decision.Scope = new(string)
|
|
|
|
if refDecision.Scope != nil && *refDecision.Scope != "" {
|
|
|
|
*decision.Scope = *refDecision.Scope
|
|
|
|
} else {
|
|
|
|
*decision.Scope = *Alert.Source.Scope
|
|
|
|
}
|
|
|
|
/*some fields are populated from the reference object : duration, scope, type*/
|
|
|
|
decision.Duration = new(string)
|
2022-06-22 09:29:52 +00:00
|
|
|
if Profile.Cfg.DurationExpr != "" && Profile.RuntimeDurationExpr != nil {
|
|
|
|
duration, err := expr.Run(Profile.RuntimeDurationExpr, exprhelpers.GetExprEnv(map[string]interface{}{"Alert": Alert}))
|
|
|
|
if err != nil {
|
|
|
|
Profile.Logger.Warningf("Failed to run duration_expr : %v", err)
|
|
|
|
*decision.Duration = *refDecision.Duration
|
|
|
|
} else {
|
|
|
|
durationStr := fmt.Sprint(duration)
|
|
|
|
if _, err := time.ParseDuration(durationStr); err != nil {
|
|
|
|
Profile.Logger.Warningf("Failed to parse expr duration result '%s'", duration)
|
|
|
|
*decision.Duration = *refDecision.Duration
|
|
|
|
} else {
|
|
|
|
*decision.Duration = durationStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if refDecision.Duration == nil {
|
|
|
|
*decision.Duration = defaultDuration
|
|
|
|
}
|
|
|
|
*decision.Duration = *refDecision.Duration
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
decision.Type = new(string)
|
|
|
|
*decision.Type = *refDecision.Type
|
|
|
|
|
|
|
|
/*for the others, let's populate it from the alert and its source*/
|
|
|
|
decision.Value = new(string)
|
|
|
|
*decision.Value = *Alert.Source.Value
|
|
|
|
decision.Origin = new(string)
|
2023-01-31 13:47:44 +00:00
|
|
|
*decision.Origin = types.CrowdSecOrigin
|
2020-11-30 09:37:17 +00:00
|
|
|
if refDecision.Origin != nil {
|
|
|
|
*decision.Origin = fmt.Sprintf("%s/%s", *decision.Origin, *refDecision.Origin)
|
|
|
|
}
|
|
|
|
decision.Scenario = new(string)
|
|
|
|
*decision.Scenario = *Alert.Scenario
|
|
|
|
decisions = append(decisions, &decision)
|
|
|
|
}
|
|
|
|
return decisions, nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
// EvaluateProfile is going to evaluate an Alert against a profile to generate Decisions
|
2022-06-22 09:29:52 +00:00
|
|
|
func (Profile *Runtime) EvaluateProfile(Alert *models.Alert) ([]*models.Decision, bool, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
var decisions []*models.Decision
|
2022-06-22 09:29:52 +00:00
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
matched := false
|
2022-06-22 09:29:52 +00:00
|
|
|
for eIdx, expression := range Profile.RuntimeFilters {
|
2021-08-25 09:43:29 +00:00
|
|
|
output, err := expr.Run(expression, exprhelpers.GetExprEnv(map[string]interface{}{"Alert": Alert}))
|
|
|
|
if err != nil {
|
2022-06-22 09:29:52 +00:00
|
|
|
Profile.Logger.Warningf("failed to run whitelist expr : %v", err)
|
|
|
|
return nil, matched, errors.Wrapf(err, "while running expression %s", Profile.Cfg.Filters[eIdx])
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
switch out := output.(type) {
|
|
|
|
case bool:
|
2022-06-22 09:29:52 +00:00
|
|
|
if Profile.Cfg.Debug != nil && *Profile.Cfg.Debug {
|
|
|
|
Profile.DebugFilters[eIdx].Run(Profile.Logger, out, exprhelpers.GetExprEnv(map[string]interface{}{"Alert": Alert}))
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-08-25 09:43:29 +00:00
|
|
|
if out {
|
|
|
|
matched = true
|
|
|
|
/*the expression matched, create the associated decision*/
|
2022-06-22 09:29:52 +00:00
|
|
|
subdecisions, err := Profile.GenerateDecisionFromProfile(Alert)
|
2021-08-25 09:43:29 +00:00
|
|
|
if err != nil {
|
2022-06-22 09:29:52 +00:00
|
|
|
return nil, matched, errors.Wrapf(err, "while generating decision from profile %s", Profile.Cfg.Name)
|
2021-08-17 14:50:16 +00:00
|
|
|
}
|
2020-12-02 16:15:48 +00:00
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
decisions = append(decisions, subdecisions...)
|
|
|
|
} else {
|
2022-06-22 09:29:52 +00:00
|
|
|
Profile.Logger.Debugf("Profile %s filter is unsuccessful", Profile.Cfg.Name)
|
|
|
|
if Profile.Cfg.OnFailure == "break" {
|
2021-08-25 09:43:29 +00:00
|
|
|
break
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-02 16:15:48 +00:00
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
default:
|
2022-06-22 09:29:52 +00:00
|
|
|
return nil, matched, fmt.Errorf("unexpected type %t (%v) while running '%s'", output, output, Profile.Cfg.Filters[eIdx])
|
2021-08-25 09:43:29 +00:00
|
|
|
|
2020-12-02 16:15:48 +00:00
|
|
|
}
|
2021-08-25 09:43:29 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2022-06-22 09:29:52 +00:00
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
return decisions, matched, nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|