Skip to content

Commit ce454a3

Browse files
committed
chore(yara): Log rule loading and check view section size
Log the loading of the YARA rule and check the size of the view of section. Small sections should not be candidates for scanning.
1 parent c66f028 commit ce454a3

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

pkg/yara/scanner.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func NewScanner(psnap ps.Snapshotter, config config.Config) (Scanner, error) {
103103
return nil
104104
}
105105
rulesInCompiler.Add(1)
106+
log.Infof("loading yara rule(s) from %s", filepath.Join(path, fi.Name()))
106107

107108
return nil
108109
})
@@ -277,7 +278,8 @@ func (s scanner) Scan(e *kevent.Kevent) (bool, error) {
277278
// scan process mapping a suspicious RX/RWX section view
278279
pid := e.Kparams.MustGetPid()
279280
prot := e.Kparams.MustGetUint32(kparams.MemProtect)
280-
if e.PID != 4 && ((prot&kevent.SectionRX) != 0 && (prot&kevent.SectionRWX) != 0) {
281+
size := e.Kparams.MustGetUint64(kparams.FileViewSize)
282+
if e.PID != 4 && size >= 4096 && ((prot&kevent.SectionRX) != 0 && (prot&kevent.SectionRWX) != 0) {
281283
filename := e.GetParamAsString(kparams.FileName)
282284
// skip mappings of signed images
283285
addr := e.Kparams.MustGetUint64(kparams.FileViewBase)

pkg/yara/scanner_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ func TestScan(t *testing.T) {
725725
Kparams: kevent.Kparams{
726726
kparams.ProcessID: {Name: kparams.ProcessID, Type: kparams.PID, Value: pid},
727727
kparams.FileViewBase: {Name: kparams.FileViewBase, Type: kparams.Address, Value: uint64(0x7ffe0000)},
728+
kparams.FileViewSize: {Name: kparams.FileViewSize, Type: kparams.Uint64, Value: uint64(12333)},
728729
kparams.MemProtect: {Name: kparams.MemProtect, Type: kparams.Flags, Value: uint32(kevent.SectionRX), Flags: kevent.ViewProtectionFlags},
729730
},
730731
Metadata: make(map[kevent.MetadataKey]any),
@@ -780,6 +781,7 @@ func TestScan(t *testing.T) {
780781
Kparams: kevent.Kparams{
781782
kparams.ProcessID: {Name: kparams.ProcessID, Type: kparams.PID, Value: uint32(1123)},
782783
kparams.FileViewBase: {Name: kparams.FileViewBase, Type: kparams.Address, Value: uint64(0x7f3e1000)},
784+
kparams.FileViewSize: {Name: kparams.FileViewSize, Type: kparams.Uint64, Value: uint64(12333)},
783785
kparams.MemProtect: {Name: kparams.MemProtect, Type: kparams.Flags, Value: uint32(kevent.SectionRX), Flags: kevent.ViewProtectionFlags},
784786
},
785787
Metadata: make(map[kevent.MetadataKey]any),
@@ -828,6 +830,7 @@ func TestScan(t *testing.T) {
828830
Kparams: kevent.Kparams{
829831
kparams.ProcessID: {Name: kparams.ProcessID, Type: kparams.PID, Value: uint32(321321)},
830832
kparams.FileViewBase: {Name: kparams.FileViewBase, Type: kparams.Address, Value: uint64(0x7ffe0000)},
833+
kparams.FileViewSize: {Name: kparams.FileViewSize, Type: kparams.Uint64, Value: uint64(12333)},
831834
kparams.MemProtect: {Name: kparams.MemProtect, Type: kparams.Flags, Value: uint32(0x10000), Flags: kevent.ViewProtectionFlags},
832835
},
833836
Metadata: make(map[kevent.MetadataKey]any),
@@ -877,6 +880,7 @@ func TestScan(t *testing.T) {
877880
kparams.ProcessID: {Name: kparams.ProcessID, Type: kparams.PID, Value: uint32(1123)},
878881
kparams.FileName: {Name: kparams.FileName, Type: kparams.UnicodeString, Value: filepath.Join(os.Getenv("windir"), "regedit.exe")},
879882
kparams.FileViewBase: {Name: kparams.FileViewBase, Type: kparams.Address, Value: uint64(0x7ffe0000)},
883+
kparams.FileViewSize: {Name: kparams.FileViewSize, Type: kparams.Uint64, Value: uint64(12333)},
880884
kparams.MemProtect: {Name: kparams.MemProtect, Type: kparams.Flags, Value: uint32(kevent.SectionRWX), Flags: kevent.ViewProtectionFlags},
881885
},
882886
Metadata: make(map[kevent.MetadataKey]any),

0 commit comments

Comments
 (0)