forked from 0xrawsec/whids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
221 lines (185 loc) · 5.26 KB
/
config.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package sysmon
import (
"encoding/json"
"encoding/xml"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/0xrawsec/golang-utils/crypto/data"
"github.com/0xrawsec/sod"
"github.com/0xrawsec/whids/los"
)
var (
ValidOnMatch = []string{
"include",
"exclude",
"",
}
ValidGroupRelation = []string{
"and",
"or",
"",
}
ValidHashAlgorithm = []string{
"IMPHASH",
"MD5",
"SHA1",
"SHA256",
"*",
"",
}
ErrUnknownOS = fmt.Errorf("unknown OS")
ErrInvalidSchemaVersion = fmt.Errorf("invalid schema version")
ErrInvalidGroupRelation = fmt.Errorf("invalid group relation")
ErrInvalidCondition = fmt.Errorf("invalid condition")
ErrInvalidOnMatch = fmt.Errorf("invalid onmatch")
ErrInvalidHashAlgorithm = fmt.Errorf("invalid hash algorithm")
schemaVersionRe = regexp.MustCompile(`\d+\.\d+`)
)
func validate(value string, in ...string) bool {
for _, check := range in {
if value == check {
return true
}
}
return false
}
type EventFilter struct {
OnMatch string `xml:"onmatch,attr,omitempty" json:"onmatch,omitempty"`
}
func (e *EventFilter) Validate() error {
if !validate(e.OnMatch, ValidOnMatch...) {
return fmt.Errorf("%w %s", ErrInvalidOnMatch, e.OnMatch)
}
return nil
}
type Filter struct {
Name string `xml:"name,attr,omitempty" json:"name,omitempty"`
Condition string `xml:"condition,attr,omitempty" json:"condition,omitempty"`
Value string `xml:",innerxml" json:"value"`
}
func (f *Filter) Validate() error {
if !validate(f.Condition, Conditions...) {
return fmt.Errorf("%w %s on value %s", ErrInvalidCondition, f.Condition, f.Value)
}
return nil
}
// Method of Filters structure not wanted to be overwritten by auto generation
func (f *Filters) Validate() error {
// we get the value of the struct and not of the pointer
v := reflect.ValueOf(f).Elem()
for i := 0; i < v.NumField(); i++ {
evtFilter := v.Field(i)
if evtFilter.IsNil() {
continue
}
filterName := v.Type().Field(i).Name
method := evtFilter.MethodByName("Validate")
res := method.Call([]reflect.Value{})
if !res[0].IsNil() {
return fmt.Errorf("%s %w", filterName, res[0].Interface().(error))
}
evtFilter = evtFilter.Elem()
for k := 0; k < evtFilter.NumField(); k++ {
field := evtFilter.Field(k)
i := field.Interface()
if filters, ok := i.([]Filter); ok {
for _, f := range filters {
if err := f.Validate(); err != nil {
return fmt.Errorf("%s bad %s filter: %w", filterName, evtFilter.Type().Field(k).Name, err)
}
}
}
}
}
return nil
}
type RuleGroup struct {
Filters
Name string `xml:"name,attr,omitempty" json:",omitempty"`
Relation string `xml:"groupRelation,attr,omitempty" json:"groupRelation,omitempty"`
}
func (g *RuleGroup) Validate() error {
if !validate(g.Relation, ValidGroupRelation...) {
return fmt.Errorf("%w %s", ErrInvalidGroupRelation, g.Relation)
}
return g.Filters.Validate()
}
type EventFiltering struct {
Filters
RuleGroup []RuleGroup
}
type InnerConfig struct {
XMLName xml.Name `xml:"Sysmon" json:"-"`
SchemaVersion string `xml:"schemaversion,attr" json:"schemaversion"`
ArchiveDirectory string `xml:",omitempty" json:",omitempty"`
CheckRevocation bool `xml:",omitempty"`
CopyOnDeletePE bool `xml:",omitempty"`
CopyOnDeleteSIDs csstrings `xml:",omitempty" json:",omitempty"`
CopyOnDeleteExtensions csstrings `xml:",omitempty" json:",omitempty"`
CopyOnDeleteProcesses csstrings `xml:",omitempty" json:",omitempty"`
DriverName string `xml:",omitempty" json:",omitempty"`
DnsLookup bool `xml:",omitempty"`
HashAlgorithms csstrings `xml:",omitempty" json:",omitempty"`
EventFiltering EventFiltering
// Don't validate Sysmon XML DTD
XmlSha256 string `xml:"-"`
OS string `xml:"-"`
}
type Config struct {
sod.Item
InnerConfig
}
func (c Config) MarshalJSON() (b []byte, err error) {
if c.XmlSha256, err = c.Sha256(); err != nil {
return
}
return json.MarshalIndent(c.InnerConfig, "", " ")
}
func (c *Config) Validate() (err error) {
if !schemaVersionRe.MatchString(c.SchemaVersion) {
return fmt.Errorf("%w %s", ErrInvalidSchemaVersion, c.SchemaVersion)
}
if !los.IsKnownOS(c.OS) {
return fmt.Errorf("%w %s", ErrUnknownOS, c.OS)
}
for _, ha := range c.HashAlgorithms {
if !validate(ha, ValidHashAlgorithm...) {
return fmt.Errorf("%w %s", ErrInvalidHashAlgorithm, ha)
}
}
if err = c.EventFiltering.Validate(); err != nil {
return
}
for _, rg := range c.EventFiltering.RuleGroup {
if err = rg.Validate(); err != nil {
return
}
}
return
}
func (c *Config) XML() (b []byte, err error) {
return xml.MarshalIndent(c, "", " ")
}
func (c *Config) Sha256() (sha256 string, err error) {
var b []byte
if b, err = c.XML(); err != nil {
return
}
sha256 = data.Sha256(b)
return
}
// to manage comma separated strings used by some Sysmon config fields
type csstrings []string
func (s csstrings) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(strings.Join(s, ","), start)
}
func (s *csstrings) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
var csstring string
if err = d.DecodeElement(&csstring, &start); err != nil {
return
}
*s = strings.Split(csstring, ",")
return
}