Skip to content

Commit 48465a4

Browse files
committed
feat(filter): Add file/image path stem field
file.path.stem and image.path.stem fields return the file/image path but without the file extension.
1 parent cc7973d commit 48465a4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

pkg/filter/accessor_windows.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ package filter
2121
import (
2222
"errors"
2323
"expvar"
24-
"github.com/rabbitstack/fibratus/pkg/fs"
25-
"github.com/rabbitstack/fibratus/pkg/network"
26-
psnap "github.com/rabbitstack/fibratus/pkg/ps"
27-
"github.com/rabbitstack/fibratus/pkg/util/cmdline"
28-
"github.com/rabbitstack/fibratus/pkg/util/signature"
2924
"net"
3025
"path/filepath"
3126
"strconv"
3227
"strings"
3328
"time"
3429

30+
"github.com/rabbitstack/fibratus/pkg/fs"
31+
"github.com/rabbitstack/fibratus/pkg/network"
32+
psnap "github.com/rabbitstack/fibratus/pkg/ps"
33+
"github.com/rabbitstack/fibratus/pkg/util/cmdline"
34+
"github.com/rabbitstack/fibratus/pkg/util/signature"
35+
3536
"github.com/rabbitstack/fibratus/pkg/event"
3637
"github.com/rabbitstack/fibratus/pkg/event/params"
3738
"github.com/rabbitstack/fibratus/pkg/filter/fields"
@@ -777,6 +778,13 @@ func (l *fileAccessor) Get(f Field, e *event.Event) (params.Value, error) {
777778
switch f.Name {
778779
case fields.FilePath:
779780
return e.GetParamAsString(params.FilePath), nil
781+
case fields.FilePathStem:
782+
path := e.GetParamAsString(params.FilePath)
783+
n := strings.LastIndexByte(path, '.')
784+
if n == -1 {
785+
return path, nil
786+
}
787+
return path[:n], nil
780788
case fields.FileName:
781789
return filepath.Base(e.GetParamAsString(params.FilePath)), nil
782790
case fields.FileExtension:
@@ -925,6 +933,13 @@ func (i *imageAccessor) Get(f Field, e *event.Event) (params.Value, error) {
925933
switch f.Name {
926934
case fields.ImagePath:
927935
return e.GetParamAsString(params.ImagePath), nil
936+
case fields.ImagePathStem:
937+
path := e.GetParamAsString(params.ImagePath)
938+
n := strings.LastIndexByte(path, '.')
939+
if n == -1 {
940+
return path, nil
941+
}
942+
return path[:n], nil
928943
case fields.ImageName:
929944
return filepath.Base(e.GetParamAsString(params.ImagePath)), nil
930945
case fields.ImageDefaultAddress:

pkg/filter/fields/fields_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ const (
450450
FileName Field = "file.name"
451451
// FilePath represents the file full path (e.g. C:\Windows\System32\cmd.exe)
452452
FilePath Field = "file.path"
453+
// FilePathStem represents the full file path without extension (e.g. C:\Windows\System32\cmd)
454+
FilePathStem Field = "file.path.stem"
453455
// FileExtension represents the file extension (e.g. .exe or .dll)
454456
FileExtension Field = "file.extension"
455457
// FileOperation represents the file operation (e.g. create)
@@ -522,6 +524,8 @@ const (
522524
ImageDefaultAddress Field = "image.default.address"
523525
// ImagePath is the module full path
524526
ImagePath Field = "image.path"
527+
// ImagePathStem represents the full module path without extension
528+
ImagePathStem Field = "image.path.stem"
525529
// ImageName is the module name
526530
ImageName Field = "image.name"
527531
// ImagePID is the pid of the process where the image was loaded
@@ -997,6 +1001,7 @@ var fields = map[Field]FieldInfo{
9971001
ThreadCallstackFinalUserModuleSignatureCertSubject: {ThreadCallstackFinalUserModuleSignatureCertSubject, "final user space stack frame module signature certificate subject", params.UnicodeString, []string{"thread.callstack.final_user_module.signature.cert.subject imatches '*Microsoft Windows*'"}, nil, nil},
9981002

9991003
ImagePath: {ImagePath, "full image path", params.UnicodeString, []string{"image.patj = 'C:\\Windows\\System32\\advapi32.dll'"}, nil, nil},
1004+
ImagePathStem: {ImagePathStem, "full image path without extension", params.UnicodeString, []string{"image.path.stem = 'C:\\Windows\\System32\\advapi32'"}, nil, nil},
10001005
ImageName: {ImageName, "image name", params.UnicodeString, []string{"image.name = 'advapi32.dll'"}, nil, nil},
10011006
ImageBase: {ImageBase, "the base address of process in which the image is loaded", params.Address, []string{"image.base.address = 'a65d800000'"}, nil, nil},
10021007
ImageChecksum: {ImageChecksum, "image checksum", params.Uint32, []string{"image.checksum = 746424"}, nil, nil},
@@ -1019,6 +1024,7 @@ var fields = map[Field]FieldInfo{
10191024

10201025
FileObject: {FileObject, "file object address", params.Uint64, []string{"file.object = 18446738026482168384"}, nil, nil},
10211026
FilePath: {FilePath, "full file path", params.UnicodeString, []string{"file.path = 'C:\\Windows\\System32'"}, nil, nil},
1027+
FilePathStem: {FilePathStem, "full file path without extension", params.UnicodeString, []string{"file.path.stem = 'C:\\Windows\\System32\\cmd'"}, nil, nil},
10221028
FileName: {FileName, "full file name", params.UnicodeString, []string{"file.name contains 'mimikatz'"}, nil, nil},
10231029
FileOperation: {FileOperation, "file operation", params.AnsiString, []string{"file.operation = 'open'"}, nil, nil},
10241030
FileShareMask: {FileShareMask, "file share mask", params.AnsiString, []string{"file.share.mask = 'rw-'"}, nil, nil},

pkg/filter/filter_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ func TestFileFilter(t *testing.T) {
629629
{`file.path ifuzzy 'C:\\WINDOWS\\sYS\\32dll'`, true},
630630
{`file.path fuzzy ('C:\\Windows\\system32\\kernel', 'C:\\Windows\\system32\\ser3ll')`, true},
631631
{`file.path ifuzzynorm 'C:\\WINDOWS\\sÝS\\32dll'`, true},
632+
{`file.path.stem = 'C:\\Windows\\system32\\user32'`, true},
632633
{`base(file.path) = 'user32.dll'`, true},
633634
{`ext(base(file.path)) = '.dll'`, true},
634635
{`base(file.path, false) = 'user32'`, true},
@@ -972,6 +973,7 @@ func TestImageFilter(t *testing.T) {
972973
{`image.signature.level = 'AUTHENTICODE'`, true},
973974
{`image.pid = 1023`, true},
974975
{`image.path endswith 'System32\\kernel32.dll'`, true},
976+
{`image.path.stem endswith 'System32\\kernel32'`, true},
975977
{`image.name = 'kernel32.dll'`, true},
976978
{`image.checksum = 2323432`, true},
977979
{`image.base.address = '7ffb313833a3'`, true},

0 commit comments

Comments
 (0)