Skip to content

Commit 45c4624

Browse files
committed
Use a separate func for matching for filesystem signatures
1 parent cc71cd2 commit 45c4624

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

components/ee/agent-smith/pkg/classifier/filesystem_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
package classifier
66

77
import (
8-
"io/ioutil"
98
"os"
109
"path/filepath"
1110
"testing"
1211
)
1312

1413
func TestSignatureMatchClassifier_MatchesFile(t *testing.T) {
1514
// Create temporary directory for test files
16-
tempDir, err := ioutil.TempDir("", "agent-smith-test")
15+
tempDir, err := os.MkdirTemp("", "agent-smith-test")
1716
if err != nil {
1817
t.Fatalf("failed to create temp dir: %v", err)
1918
}
@@ -30,7 +29,7 @@ func TestSignatureMatchClassifier_MatchesFile(t *testing.T) {
3029

3130
for filename, content := range testFiles {
3231
filePath := filepath.Join(tempDir, filename)
33-
if err := ioutil.WriteFile(filePath, content, 0644); err != nil {
32+
if err := os.WriteFile(filePath, content, 0644); err != nil {
3433
t.Fatalf("failed to create test file %s: %v", filename, err)
3534
}
3635
}
@@ -251,14 +250,14 @@ func TestSignatureMatchClassifier_ContentMatching(t *testing.T) {
251250
for _, tt := range tests {
252251
t.Run(tt.name, func(t *testing.T) {
253252
// Create a temporary file for testing
254-
tempDir, err := ioutil.TempDir("", "agent-smith-content-test")
253+
tempDir, err := os.MkdirTemp("", "agent-smith-content-test")
255254
if err != nil {
256255
t.Fatalf("failed to create temp dir: %v", err)
257256
}
258257
defer os.RemoveAll(tempDir)
259258

260259
filePath := filepath.Join(tempDir, tt.filename)
261-
if err := ioutil.WriteFile(filePath, []byte(tt.content), 0644); err != nil {
260+
if err := os.WriteFile(filePath, []byte(tt.content), 0644); err != nil {
262261
t.Fatalf("failed to create test file: %v", err)
263262
}
264263

components/ee/agent-smith/pkg/classifier/sinature.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ type Signature struct {
4343
// Name is a description of the signature
4444
Name string `json:"name,omitempty"`
4545

46-
// Domain describe where to look for the file to search for the signature (default: "filesystem")
46+
// Domain describe where to look for the file to search for the signature
47+
// "process" is dominant
48+
// if domain is empty, we set "filesystem"
4749
Domain Domain `json:"domain,omitempty"`
4850

4951
// The kind of file this signature can apply to
@@ -149,6 +151,11 @@ func (s *Signature) Matches(in *SignatureReadCache) (bool, error) {
149151
}
150152
}
151153

154+
// necessary to do a string match for text files
155+
if s.Domain == DomainFileSystem {
156+
return s.matchAnyFile(in)
157+
}
158+
152159
// match the specific kind
153160
switch s.Kind {
154161
case ObjectELFSymbols:
@@ -282,6 +289,33 @@ func (s *Signature) matchAny(in *SignatureReadCache) (bool, error) {
282289
pos += int64(n)
283290

284291
// TODO: deal with buffer edges (i.e. pattern wrapping around the buffer edge)
292+
if bytes.Contains(sub, s.Pattern) {
293+
return true, nil
294+
}
295+
296+
if err == io.EOF {
297+
break
298+
}
299+
if err != nil {
300+
return false, xerrors.Errorf("cannot read stream: %w", err)
301+
}
302+
if s.Slice.End > 0 && pos >= s.Slice.End {
303+
break
304+
}
305+
}
306+
307+
return false, nil
308+
}
309+
310+
// matchAny matches a signature against a text file
311+
func (s *Signature) matchAnyFile(in *SignatureReadCache) (bool, error) {
312+
buffer := make([]byte, 8096)
313+
pos := s.Slice.Start
314+
for {
315+
n, err := in.Reader.ReadAt(buffer, pos)
316+
sub := buffer[0:n]
317+
pos += int64(n)
318+
285319
match, matchErr := s.matches(sub)
286320
if matchErr != nil {
287321
return false, matchErr

0 commit comments

Comments
 (0)