Skip to content

Commit 1628b93

Browse files
authored
Add nil checks for slice pointers; filter invalid overrides from reports (#1031)
* Add nil checks for slice pointers; filter invalid overrides from reports Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> * Re-word comment Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> --------- Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 75df672 commit 1628b93

18 files changed

Lines changed: 78 additions & 78 deletions

pkg/report/report.go

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcon
448448

449449
b := buildBehavior(m, matchedStrings, key, ruleURL, risk)
450450

451-
handleMetadata(m, b, fr, override, mrsMap, &pledges, &caps, &syscalls)
451+
// if the rule has an override tag but is not overriding a valid rule,
452+
// ignore this match rule so that we don't show errant false positive rules in reports
453+
if !parseMetadata(m, b, fr, override, mrsMap, &pledges, &caps, &syscalls) {
454+
continue
455+
}
452456

453457
// Fix YARA Forge rules that record their author URL as reference URLs
454458
if strings.HasPrefix(b.RuleURL, b.ReferenceURL) {
@@ -476,8 +480,6 @@ func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcon
476480
}
477481

478482
updateBehavior(fr, b, key)
479-
480-
// TODO: If we match multiple rules within a single namespace, merge matchstrings
481483
}
482484

483485
// Update the behaviors to account for overrides
@@ -589,10 +591,14 @@ func buildBehavior(m *yarax.Rule, matchedStrings []string, key string, ruleURL s
589591
}
590592
}
591593

592-
func handleMetadata(m *yarax.Rule, b *malcontent.Behavior, fr *malcontent.FileReport, override bool, mrsMap map[string]*yarax.Rule, pledges *[]string, caps *[]string, syscalls *[]string) {
594+
func parseMetadata(m *yarax.Rule, b *malcontent.Behavior, fr *malcontent.FileReport, override bool, mrsMap map[string]*yarax.Rule, pledges *[]string, caps *[]string, syscalls *[]string) bool {
593595
k := ""
594596
v := ""
595597

598+
// valid represents whether a rule's metadata contains a legitimate override
599+
// or is otherwise valid for the matching rule
600+
valid := true
601+
596602
for _, meta := range m.Metadata() {
597603
k = meta.Identifier()
598604
v = fmt.Sprintf("%s", meta.Value())
@@ -601,25 +607,6 @@ func handleMetadata(m *yarax.Rule, b *malcontent.Behavior, fr *malcontent.FileRe
601607
continue
602608
}
603609

604-
// If we find a match in the map for the metadata key, that's the rule to override
605-
// Store this rule (the override) in the fr.Overrides behavior slice
606-
// If an override rule is not overriding a valid rule, log an error
607-
_, exists := mrsMap[k]
608-
switch {
609-
case exists && override:
610-
var overrideSev int
611-
if sev, ok := Levels[v]; ok {
612-
overrideSev = sev
613-
}
614-
b.RiskLevel = RiskLevels[overrideSev]
615-
b.RiskScore = overrideSev
616-
b.Override = append(b.Override, k)
617-
fr.Overrides = append(fr.Overrides, b)
618-
case !exists && override:
619-
// TODO: return error if override references an unknown rule name
620-
continue
621-
}
622-
623610
switch k {
624611
case "author":
625612
b.RuleAuthor = v
@@ -656,14 +643,47 @@ func handleMetadata(m *yarax.Rule, b *malcontent.Behavior, fr *malcontent.FileRe
656643
// YARAforge forgets to encode spaces
657644
b.RuleURL = fixURL(v)
658645
case "pledge":
659-
*pledges = append(*pledges, v)
646+
// pledges should not be nil when we get here, but guard against it
647+
if pledges != nil {
648+
*pledges = append(*pledges, v)
649+
}
660650
case "syscall":
661-
sy := strings.Split(v, ",")
662-
*syscalls = append(*syscalls, sy...)
651+
// syscalls should not be nil when we get here, but guard against it
652+
if syscalls != nil {
653+
calls := strings.Split(v, ",")
654+
*syscalls = append(*syscalls, calls...)
655+
}
663656
case "cap":
664-
*caps = append(*caps, v)
657+
// caps should not be nil when we get here, but guard against it
658+
if caps != nil {
659+
*caps = append(*caps, v)
660+
}
661+
case "filetypes":
662+
continue
663+
// If we find a match in the map for the metadata key after exhausting known keys, that's the rule to override
664+
// Store this rule (the override) in the fr.Overrides behavior slice
665+
// If an override rule is not overriding a valid rule, set `valid` to false so we can
666+
// skip the parent rule match in the report
667+
default:
668+
_, exists := mrsMap[k]
669+
switch {
670+
case exists && override:
671+
var overrideSev int
672+
if sev, ok := Levels[v]; ok {
673+
overrideSev = sev
674+
}
675+
b.RiskLevel = RiskLevels[overrideSev]
676+
b.RiskScore = overrideSev
677+
b.Override = append(b.Override, k)
678+
fr.Overrides = append(fr.Overrides, b)
679+
case !exists && override:
680+
valid = false
681+
continue
682+
}
665683
}
666684
}
685+
686+
return valid
667687
}
668688

669689
func updateBehavior(fr *malcontent.FileReport, b *malcontent.Behavior, key string) {

rules/false_positives/lslogins.yara

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
rule lastlogins: override linux {
22
meta:
3-
description = "lastlogins"
4-
login_records = "low"
3+
description = "lastlogins"
4+
current_logins = "low"
55

66
strings:
77
$lastlogin = "LAST-LOGIN"

rules/false_positives/slirp.yara

Lines changed: 0 additions & 13 deletions
This file was deleted.

rules/false_positives/snapd.yara

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ rule snapd: override linux {
22
meta:
33
description = "snapd"
44
nohup = "medium"
5-
login_records = "medium"
65
dev_mem = "medium"
76
dev_mmc = "medium"
87
busybox_runner = "medium"

rules/false_positives/ssh.yara

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
rule ignore_sshd: override {
22
meta:
3-
description = "sshd"
4-
login_records = "medium"
5-
id_rsa = "low"
6-
sshd = "low"
3+
description = "sshd"
4+
id_rsa = "low"
5+
sshd = "low"
76

87
strings:
98
$auth = "SSH_USER_AUTH"

rules/false_positives/vmtools.yara

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
rule vmtools: override {
22
meta:
3-
description = "vmtools"
4-
backdoor = "medium"
5-
linux_critical_system_paths_high = "medium"
6-
proc_net_route_high = "medium"
7-
proc_s_exe = "medium"
8-
sys_net_recon_exfil = "medium"
9-
proc_s_cmdline = "medium"
3+
description = "vmtools"
4+
backdoor = "medium"
5+
proc_net_route_high = "medium"
6+
proc_s_exe = "medium"
7+
sys_net_recon_exfil = "medium"
8+
proc_s_cmdline = "medium"
109

1110
strings:
1211
$vmtools = "VMTools" fullword

rules/persist/kernel_module/symbol-lookup.yara

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rule kallsyms_lookup: high linux {
1+
rule kallsyms_lookup: high {
22
meta:
33
description = "access unexported kernel symbols"
44
ref = "https://lwn.net/Articles/813350/"
@@ -15,9 +15,10 @@ rule kallsyms_lookup: high linux {
1515
filesize < 1MB and $ref and none of ($not*)
1616
}
1717

18-
rule kallsyms: medium linux {
18+
rule kallsyms: medium {
1919
meta:
2020
description = "access kernel symbols"
21+
filetypes = "c,elf,so"
2122

2223
strings:
2324
$kallsyms = "/proc/kallsyms"
@@ -26,11 +27,10 @@ rule kallsyms: medium linux {
2627
any of them
2728
}
2829

29-
rule bpftrace: override linux {
30+
rule bpftrace: medium {
3031
meta:
3132
description = "bpftrace"
3233
filetypes = "c,elf,so"
33-
kallsyms = "medium"
3434

3535
strings:
3636
$ref2 = "BPFTRACE" fullword
@@ -39,7 +39,7 @@ rule bpftrace: override linux {
3939
filesize < 2MB and any of them
4040
}
4141

42-
rule bpf: override linux {
42+
rule bpf: override {
4343
meta:
4444
description = "libbpf"
4545
filetypes = "c,so,elf"

rules/privesc/sudoers.yara

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
rule sudo_editor: medium {
22
meta:
33
description = "references /etc/sudoers"
4+
filetypes = "elf,macho,so"
45

56
strings:
67
$etc_sudoers = "/etc/sudoers"
@@ -15,6 +16,7 @@ rule sudo_editor: medium {
1516
rule small_elf_sudoer: high {
1617
meta:
1718
description = "references /etc/sudoers"
19+
filetypes = "elf,macho,so"
1820

1921
condition:
2022
uint32(0) == 1179403647 and filesize < 10MB and sudo_editor
@@ -23,6 +25,7 @@ rule small_elf_sudoer: high {
2325
rule sudo_parser: override {
2426
meta:
2527
small_elf_sudoer = "medium"
28+
filetypes = "elf,macho,so"
2629

2730
strings:
2831
$parse = "sudo_parse"

tests/c/clean/falco/ppm_events.c.simple

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ net/http/post: medium
77
net/socket/connect: medium
88
net/socket/send: low
99
net/url/embedded: low
10-
persist/kernel_module/symbol_lookup: low

tests/javascript/clean/powershell.js.simple

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# javascript/clean/powershell.js: medium
22
c2/tool_transfer/os: low
33
exec/shell/power: medium
4-
false-positives/opensearch_dashboard: low
54
fs/directory/remove: low
65
fs/file/copy: medium
76
fs/file/delete: medium

0 commit comments

Comments
 (0)