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

Initial cut for auto enable sigma rules #406

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 17 additions & 1 deletion server/modules/elastalert/elastalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ElastAlertEngine struct {
elastAlertRulesFolder string
rulesFingerprintFile string
sigmaRulePackages []string
autoEnabledSigmaRules []string
rulesRepos []*module.RuleRepo
reposFolder string
isRunning bool
Expand All @@ -74,6 +75,19 @@ type ElastAlertEngine struct {
IOManager
}

func checkRulesetEnabled(e *ElastAlertEngine, det *model.Detection) {

det.IsEnabled = false
metaCombined := *det.Ruleset + "+" + string(det.Severity)
for _, rule := range e.autoEnabledSigmaRules {
if rule == metaCombined {
det.IsEnabled = true
break
}
}

}

func NewElastAlertEngine(srv *server.Server) *ElastAlertEngine {
return &ElastAlertEngine{
srv: srv,
Expand All @@ -94,6 +108,7 @@ func (e *ElastAlertEngine) Init(config module.ModuleConfig) (err error) {
e.elastAlertRulesFolder = module.GetStringDefault(config, "elastAlertRulesFolder", "/opt/sensoroni/elastalert")
e.rulesFingerprintFile = module.GetStringDefault(config, "rulesFingerprintFile", "/opt/sensoroni/fingerprints/sigma.fingerprint")
e.autoUpdateEnabled = module.GetBoolDefault(config, "autoUpdateEnabled", false)
e.autoEnabledSigmaRules = module.GetStringArrayDefault(config, "autoEnabledSigmaRules", []string{"securityonion-resources+critical", "securityonion-resources+high"})

pkgs := module.GetStringArrayDefault(config, "sigmaRulePackages", []string{"core", "emerging_threats_addon"})
e.parseSigmaPackages(pkgs)
Expand Down Expand Up @@ -836,7 +851,8 @@ func (e *ElastAlertEngine) syncCommunityDetections(ctx context.Context, detectio
results.Unchanged++
}
} else {
det.IsEnabled = false

checkRulesetEnabled(e, det)

_, err = e.srv.Detectionstore.CreateDetection(ctx, det)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions server/modules/elastalert/elastalert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ import (
"gopkg.in/yaml.v3"
)

func TestCheckAutoEnabledSigmaRule(t *testing.T) {
e := &ElastAlertEngine{
autoEnabledSigmaRules: []string{"securityonion-resources+high", "core+critical"},
}

tests := []struct {
name string
ruleset string
severity model.Severity
expected bool
}{
{"securityonion-resources rule with high severity, rule enabled", "securityonion-resources", model.SeverityHigh, true},
{"core rule with critical severity, rule enabled", "core", model.SeverityCritical, true},
{"core rule with high severity, rule not enabled", "core", model.SeverityHigh, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
det := &model.Detection{
Ruleset: util.Ptr(tt.ruleset),
Severity: tt.severity,
}
checkRulesetEnabled(e, det)
assert.Equal(t, tt.expected, det.IsEnabled)
})
}
}

func TestElastAlertModule(t *testing.T) {
srv := &server.Server{
DetectionEngines: map[model.EngineName]server.DetectionEngine{},
Expand Down
Loading