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

Fix for Strelka Rule Duplication #585

Merged
merged 1 commit into from
Jul 19, 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
10 changes: 7 additions & 3 deletions html/js/routes/detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,14 @@ routes.push({ path: '/detection/:id', name: 'detection', component: {
}
},
async duplicateDetection() {
const response = await this.$root.papi.post('/detection/' + encodeURIComponent(this.$route.params.id) + '/duplicate');
this.extractDetection(response);
try {
const response = await this.$root.papi.post('/detection/' + encodeURIComponent(this.$route.params.id) + '/duplicate');
this.extractDetection(response);

this.$router.push({ name: 'detection', params: { id: response.data.id } });
this.$router.push({ name: 'detection', params: { id: response.data.id } });
} catch (error) {
this.$root.showError(error);
}
},
deleteDetection() {
this.confirmDeleteDialog = true;
Expand Down
29 changes: 26 additions & 3 deletions server/modules/strelka/strelka.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (
DEFAULT_INTEGRITY_CHECK_FREQUENCY_SECONDS = 600
)

var titleUpdater = regexp.MustCompile(`(?i)rule\s+(\w+)(\s+:(\s*[^{]+))?(\s+){`)
var titleUpdater = regexp.MustCompile(`(?im)rule\s+(\w+)(\s+:(\s*[^{]+))?(\s+)(//.*$)?(\n?){`)
var nameValidator = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]{0,127}$`) // alphanumeric + underscore, can't start with a number

type StrelkaEngine struct {
Expand Down Expand Up @@ -1074,9 +1074,32 @@ func (e *StrelkaEngine) DuplicateDetection(ctx context.Context, detection *model

rule := rules[0]

rule.Src = titleUpdater.ReplaceAllString(rule.Src, "rule ${1}_copy${2}${4}{")
newID := detection.PublicID + "_copy"

det := rule.ToDetection(detection.License, detections.RULESET_CUSTOM, false)
det, err := e.srv.Detectionstore.GetDetectionByPublicId(ctx, newID)
if err != nil {
return nil, err
}

if det != nil {
for i := 1; i < 10 && det != nil; i++ {
newID = fmt.Sprintf("%s_copy%d", detection.PublicID, i)

det, err = e.srv.Detectionstore.GetDetectionByPublicId(ctx, newID)
if err != nil {
return nil, err
}
}

if det != nil {
// never found a non-duplicate name, giving up
return nil, fmt.Errorf("exhausted hunt for new, unused publicId")
}
}

rule.Src = titleUpdater.ReplaceAllString(rule.Src, "rule "+newID+"${2}${4}${5}${6}{")

det = rule.ToDetection(detection.License, detections.RULESET_CUSTOM, false)

err = e.ExtractDetails(det)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions server/modules/strelka/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ func TestDuplicateDetection(t *testing.T) {
LastName: "Levinson",
}, nil)

detStore := mock.NewMockDetectionstore(ctrl)
detStore.EXPECT().GetDetectionByPublicId(ctx, "mimikatz_kirbi_ticket_copy").Return(det, nil)
detStore.EXPECT().GetDetectionByPublicId(ctx, "mimikatz_kirbi_ticket_copy1").Return(nil, nil)

eng := StrelkaEngine{
srv: &server.Server{
Userstore: mUser,
Userstore: mUser,
Detectionstore: detStore,
},
isRunning: true,
}
Expand All @@ -148,7 +153,7 @@ func TestDuplicateDetection(t *testing.T) {

// expected differences
assert.NotEqual(t, det.Title, dupe.Title)
assert.Equal(t, det.Title, dupe.Title[:len(dupe.Title)-len("_copy")])
assert.Equal(t, dupe.Title, "mimikatz_kirbi_ticket_copy1")
assert.NotEqual(t, det.PublicID, dupe.PublicID)
assert.NotEmpty(t, dupe.PublicID)
assert.NotEqual(t, det.IsCommunity, dupe.IsCommunity)
Expand Down
Loading