Skip to content

Commit

Permalink
Bugfix: Sigma plugin expand details did not handle non-string values (#…
Browse files Browse the repository at this point in the history
…3232)

Fixes: #3231
  • Loading branch information
scudette authored Jan 21, 2024
1 parent e86901d commit ddf3f37
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
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

0 comments on commit ddf3f37

Please sign in to comment.