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

Bugfix: Sigma plugin expand details did not handle non-string values #3232

Merged
merged 1 commit into from
Jan 21, 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
23 changes: 20 additions & 3 deletions vql/sigma/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sigma

import (
"context"
"encoding/json"
"regexp"

"github.com/Velocidex/ordereddict"
Expand Down Expand Up @@ -46,10 +47,26 @@ func (self *SigmaContext) AddDetail(
return in
}

res, ok := resolved[0].(string)
if ok {
return res
if len(resolved) == 1 {
res, ok := resolved[0].(string)
if ok {
return res
}

// If it is not a string, serialize to json and
// interpolate instead.
serialized, err := json.Marshal(resolved[0])
if err == nil {
return string(serialized)
}
}

// Handle lists and dicts
serialized, err := json.Marshal(resolved)
if err == nil {
return string(serialized)
}

return in
})
}
Expand Down
70 changes: 68 additions & 2 deletions vql/sigma/fixtures/TestSigma.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
"Match single field": [
{
"Foo": "Bar",
"Integer": 4,
"List": [
1,
2,
3
],
"Dict": {
"X": 1,
"Y": 2
},
"Baz": "Hello",
"Details": null,
"_Match": {
Expand Down Expand Up @@ -48,8 +58,18 @@
"Rule With Details": [
{
"Foo": "Bar",
"Integer": 4,
"List": [
1,
2,
3
],
"Dict": {
"X": 1,
"Y": 2
},
"Baz": "Hello",
"Details": "This is column Foo=Bar",
"Details": "This is column Foo=Bar Int=4 List=[1,2,3] Dict={\"X\":1,\"Y\":2}",
"_Match": {
"Match": true,
"SearchResults": {
Expand All @@ -75,6 +95,12 @@
"Values": [
"Bar"
]
},
{
"Field": "Integer",
"Values": [
4
]
}
]
]
Expand All @@ -89,14 +115,24 @@
]
},
"AdditionalFields": {
"details": "This is column Foo=%Foo%"
"details": "This is column Foo=%Foo% Int=%Integer% List=%List% Dict=%Dict%"
}
}
}
],
"Default Details in callback": [
{
"Foo": "Bar",
"Integer": 4,
"List": [
1,
2,
3
],
"Dict": {
"X": 1,
"Y": 2
},
"Baz": "Hello",
"Details": "I'm a scope var:Default Detail Foo=Bar",
"_Match": {
Expand Down Expand Up @@ -143,6 +179,16 @@
"Match field with regex": [
{
"Foo": "Bar",
"Integer": 4,
"List": [
1,
2,
3
],
"Dict": {
"X": 1,
"Y": 2
},
"Baz": "Hello",
"Details": null,
"_Match": {
Expand Down Expand Up @@ -192,6 +238,16 @@
"Match field with logical operators": [
{
"Foo": "Bar",
"Integer": 4,
"List": [
1,
2,
3
],
"Dict": {
"X": 1,
"Y": 2
},
"Baz": "Hello",
"Details": null,
"_Match": {
Expand Down Expand Up @@ -262,6 +318,16 @@
"Match field with logical or operator": [
{
"Foo": "Bar",
"Integer": 4,
"List": [
1,
2,
3
],
"Dict": {
"X": 1,
"Y": 2
},
"Baz": "Hello",
"Details": null,
"_Match": {
Expand Down
8 changes: 7 additions & 1 deletion vql/sigma/sigma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ detection:
testRows = []*ordereddict.Dict{
ordereddict.NewDict().
Set("Foo", "Bar").
Set("Integer", 4).
Set("List", []int64{1, 2, 3}).
Set("Dict", map[string]interface{}{
"X": 1, "Y": 2,
}).
Set("Baz", "Hello"),
ordereddict.NewDict().
Set("System", ordereddict.NewDict().
Expand Down Expand Up @@ -94,14 +99,15 @@ detection:
description: "Rule With Details",
rule: `
title: RuleWithDetails
details: This is column Foo=%Foo%
details: This is column Foo=%Foo% Int=%Integer% List=%List% Dict=%Dict%
logsource:
product: windows
service: application

detection:
selection:
Foo: Bar
Integer: 4
condition: selection
`,
fieldmappings: ordereddict.NewDict(),
Expand Down