forked from yanivagman/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature_test.go
155 lines (138 loc) · 3.83 KB
/
signature_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/aquasecurity/tracee/types/detect"
"github.com/open-policy-agent/opa/compile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
// FIXME Don't relay on Git project structure to run unit tests.
exampleRulesDir = "../../signatures/rego"
)
func Test_getSignatures(t *testing.T) {
sigs, err := getSignatures(compile.TargetRego, false, exampleRulesDir, []string{"TRC-2"}, false)
require.NoError(t, err)
require.Equal(t, 1, len(sigs))
gotMetadata, err := sigs[0].GetMetadata()
require.NoError(t, err)
assert.Equal(t, detect.SignatureMetadata{
ID: "TRC-2",
Version: "0.1.0",
Name: "Anti-Debugging",
Description: "Process uses anti-debugging technique to block debugger",
Tags: []string{"linux", "container"},
Properties: map[string]interface{}{
"MITRE ATT&CK": "Defense Evasion: Execution Guardrails",
"Severity": json.Number("3"),
},
}, gotMetadata)
}
func Test_isHelper(t *testing.T) {
testCases := []struct {
input string
expected bool
}{
{"helpers.rego", true},
{"test_helpers.rego", true},
{"new_helpers.rego", true},
{"test.rego", false},
{"new.rego", false},
{"test_new.rego", false},
{"helpers.go", false},
{"test_helpers.go", false},
{"aaa.go", false},
{"helpers", false},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
actual := isHelper(tc.input)
assert.Equal(t, tc.expected, actual)
})
}
}
func Test_findRegoSigs(t *testing.T) {
// create test signature directory, clean it up after the test
testRoot, err := ioutil.TempDir(os.TempDir(), "")
require.NoError(t, err)
defer os.RemoveAll(testRoot)
// create directory to test nested rule directories
testDir := filepath.Join(testRoot, "test")
err = os.Mkdir(testDir, 0777)
require.NoError(t, err)
// copy example go signature to test dir and it's child dir
err = copyExampleSig("anti_debugging_ptraceme.rego", testRoot)
require.NoError(t, err)
err = copyExampleSig("anti_debugging_ptraceme.rego", testDir)
require.NoError(t, err)
// find rego signatures
sigs, err := findRegoSigs(compile.TargetRego, false, testRoot, false)
require.NoError(t, err)
assert.Equal(t, len(sigs), 2)
for _, sig := range sigs {
gotMetadata, err := sig.GetMetadata()
require.NoError(t, err)
assert.Equal(t, detect.SignatureMetadata{
ID: "TRC-2",
Version: "0.1.0",
Name: "Anti-Debugging",
Description: "Process uses anti-debugging technique to block debugger",
Tags: []string{"linux", "container"},
Properties: map[string]interface{}{
"MITRE ATT&CK": "Defense Evasion: Execution Guardrails",
"Severity": json.Number("3"),
},
}, gotMetadata)
}
}
func copyExampleSig(exampleName, destDir string) error {
var exampleDir string
extension := filepath.Ext(exampleName)
if extension == ".rego" {
exampleDir = exampleRulesDir + "/%s"
} else {
return errors.New("unsupported signature type")
}
// copy example signature
exampleFile := fmt.Sprintf(exampleDir, exampleName)
_, err := os.Stat(exampleFile)
if err != nil {
return err
}
file, err := ioutil.ReadFile(exampleFile)
if err != nil {
return err
}
// write example sig to directory
testSig := "test" + extension
err = ioutil.WriteFile(filepath.Join(destDir, testSig), file, 0777)
if err != nil {
return err
}
return nil
}
func Test_isRegoFile(t *testing.T) {
testCases := []struct {
input string
expected bool
}{
{"helpers.rego", true},
{"test_helpers.rego", true},
{"helpers.go", false},
{"builtin.so", false},
{"helpers", false},
{"helpers.txt", false},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
actual := isHelper(tc.input)
assert.Equal(t, tc.expected, actual)
})
}
}