-
Notifications
You must be signed in to change notification settings - Fork 84
/
rules_manager.go
405 lines (349 loc) · 12.1 KB
/
rules_manager.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* This file is part of caronte (https://github.com/eciavatta/caronte).
* Copyright (c) 2020 Emiliano Ciavatta.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/flier/gohs/hyperscan"
"github.com/go-playground/validator/v10"
log "github.com/sirupsen/logrus"
)
const DirectionBoth = 0
const DirectionToServer = 1
const DirectionToClient = 2
type RegexFlags struct {
Caseless bool `json:"caseless" bson:"caseless,omitempty"` // Set case-insensitive matching.
DotAll bool `json:"dot_all" bson:"dot_all,omitempty"` // Matching a `.` will not exclude newlines.
MultiLine bool `json:"multi_line" bson:"multi_line,omitempty"` // Set multi-line anchoring.
Utf8Mode bool `json:"utf_8_mode" bson:"utf_8_mode,omitempty"` // Enable UTF-8 mode for this expression.
UnicodeProperty bool `json:"unicode_property" bson:"unicode_property,omitempty"` // Enable Unicode property support for this expression
}
type Pattern struct {
Regex string `json:"regex" binding:"required,min=1" bson:"regex"`
Flags RegexFlags `json:"flags" bson:"flags,omitempty"`
MinOccurrences uint `json:"min_occurrences" bson:"min_occurrences,omitempty"`
MaxOccurrences uint `json:"max_occurrences" binding:"omitempty,gtefield=MinOccurrences" bson:"max_occurrences,omitempty"`
Direction uint8 `json:"direction" binding:"omitempty,max=2" bson:"direction,omitempty"`
internalID uint
}
type Filter struct {
ServicePort uint16 `json:"service_port" bson:"service_port,omitempty"`
ClientAddress string `json:"client_address" binding:"omitempty,ip" bson:"client_address,omitempty"`
ClientPort uint16 `json:"client_port" bson:"client_port,omitempty"`
MinDuration uint `json:"min_duration" bson:"min_duration,omitempty"`
MaxDuration uint `json:"max_duration" binding:"omitempty,gtefield=MinDuration" bson:"max_duration,omitempty"`
MinBytes uint `json:"min_bytes" bson:"min_bytes,omitempty"`
MaxBytes uint `json:"max_bytes" binding:"omitempty,gtefield=MinBytes" bson:"max_bytes,omitempty"`
}
type Rule struct {
ID RowID `json:"id" bson:"_id,omitempty"`
Name string `json:"name" binding:"min=3" bson:"name"`
Color string `json:"color" binding:"hexcolor" bson:"color"`
Notes string `json:"notes" bson:"notes,omitempty"`
Enabled bool `json:"enabled" bson:"enabled"`
Patterns []Pattern `json:"patterns" bson:"patterns"`
Filter Filter `json:"filter" bson:"filter,omitempty"`
Version int64 `json:"version" bson:"version"`
}
type RulesDatabase struct {
database hyperscan.StreamDatabase
databaseSize int
version RowID
}
type RulesManager interface {
AddRule(context context.Context, rule Rule) (RowID, error)
GetRule(id RowID) (Rule, bool)
UpdateRule(context context.Context, id RowID, rule Rule) (bool, error)
GetRules() []Rule
FillWithMatchedRules(connection *Connection, clientMatches map[uint][]PatternSlice, serverMatches map[uint][]PatternSlice)
DatabaseUpdateChannel() chan RulesDatabase
}
type rulesManagerImpl struct {
storage Storage
rules map[RowID]Rule
rulesByName map[string]Rule
patterns []*hyperscan.Pattern
patternsIds map[string]uint
mutex sync.Mutex
databaseUpdated chan RulesDatabase
validate *validator.Validate
}
func LoadRulesManager(storage Storage, flagRegex string) (RulesManager, error) {
var rules []Rule
if err := storage.Find(Rules).Sort("_id", true).All(&rules); err != nil {
return nil, err
}
rulesManager := rulesManagerImpl{
storage: storage,
rules: make(map[RowID]Rule),
rulesByName: make(map[string]Rule),
patterns: make([]*hyperscan.Pattern, 0),
patternsIds: make(map[string]uint),
mutex: sync.Mutex{},
databaseUpdated: make(chan RulesDatabase, 1),
validate: validator.New(),
}
for _, rule := range rules {
if err := rulesManager.validateAndAddRuleLocal(&rule); err != nil {
return nil, err
}
}
// if there are no rules in database (e.g. first run), set flagRegex as first rule
if len(rulesManager.rules) == 0 {
_, _ = rulesManager.AddRule(context.Background(), Rule{
Name: "flag_out",
Color: "#e53935",
Notes: "Mark connections where the flags are stolen",
Patterns: []Pattern{
{Regex: flagRegex, Direction: DirectionToClient, Flags: RegexFlags{Utf8Mode: true}},
},
})
_, _ = rulesManager.AddRule(context.Background(), Rule{
Name: "flag_in",
Color: "#43A047",
Notes: "Mark connections where the flags are placed",
Patterns: []Pattern{
{Regex: flagRegex, Direction: DirectionToServer, Flags: RegexFlags{Utf8Mode: true}},
},
})
} else {
if err := rulesManager.generateDatabase(rules[len(rules)-1].ID); err != nil {
return nil, err
}
}
return &rulesManager, nil
}
func (rm *rulesManagerImpl) AddRule(context context.Context, rule Rule) (RowID, error) {
rm.mutex.Lock()
rule.ID = CustomRowID(uint64(len(rm.rules)), time.Now())
rule.Enabled = true
if err := rm.validateAndAddRuleLocal(&rule); err != nil {
rm.mutex.Unlock()
return EmptyRowID(), err
}
if err := rm.generateDatabase(rule.ID); err != nil {
rm.mutex.Unlock()
log.WithError(err).WithField("rule", rule).Panic("failed to generate database")
}
rm.mutex.Unlock()
if _, err := rm.storage.Insert(Rules).Context(context).One(rule); err != nil {
log.WithError(err).WithField("rule", rule).Panic("failed to insert rule on database")
}
return rule.ID, nil
}
func (rm *rulesManagerImpl) GetRule(id RowID) (Rule, bool) {
rule, isPresent := rm.rules[id]
return rule, isPresent
}
func (rm *rulesManagerImpl) UpdateRule(context context.Context, id RowID, rule Rule) (bool, error) {
newRule, isPresent := rm.rules[id]
if !isPresent {
return false, nil
}
sameName, isPresent := rm.rulesByName[rule.Name]
if isPresent && sameName.ID != id {
return false, errors.New("already exists another rule with the same name")
}
updated, err := rm.storage.Update(Rules).Context(context).Filter(OrderedDocument{{"_id", id}}).
One(UnorderedDocument{"name": rule.Name, "color": rule.Color})
if err != nil {
log.WithError(err).WithField("rule", rule).Panic("failed to update rule on database")
}
if updated {
rm.mutex.Lock()
newRule.Name = rule.Name
newRule.Color = rule.Color
delete(rm.rulesByName, newRule.Name)
rm.rulesByName[newRule.Name] = newRule
rm.rules[id] = newRule
rm.mutex.Unlock()
}
return updated, nil
}
func (rm *rulesManagerImpl) GetRules() []Rule {
rules := make([]Rule, 0, len(rm.rules))
for _, rule := range rm.rules {
rules = append(rules, rule)
}
sort.Slice(rules, func(i, j int) bool {
return rules[i].ID.Timestamp().Before(rules[j].ID.Timestamp())
})
return rules
}
func (rm *rulesManagerImpl) FillWithMatchedRules(connection *Connection, clientMatches map[uint][]PatternSlice,
serverMatches map[uint][]PatternSlice) {
rm.mutex.Lock()
filterFunctions := []func(rule Rule) bool{
func(rule Rule) bool {
return rule.Filter.ClientAddress == "" || connection.SourceIP == rule.Filter.ClientAddress
},
func(rule Rule) bool {
return rule.Filter.ClientPort == 0 || connection.SourcePort == rule.Filter.ClientPort
},
func(rule Rule) bool {
return rule.Filter.ServicePort == 0 || connection.DestinationPort == rule.Filter.ServicePort
},
func(rule Rule) bool {
return rule.Filter.MinDuration == 0 || uint(connection.ClosedAt.Sub(connection.StartedAt).Milliseconds()) >=
rule.Filter.MinDuration
},
func(rule Rule) bool {
return rule.Filter.MaxDuration == 0 || uint(connection.ClosedAt.Sub(connection.StartedAt).Milliseconds()) <=
rule.Filter.MaxDuration
},
func(rule Rule) bool {
return rule.Filter.MinBytes == 0 || uint(connection.ClientBytes+connection.ServerBytes) >=
rule.Filter.MinBytes
},
func(rule Rule) bool {
return rule.Filter.MaxBytes == 0 || uint(connection.ClientBytes+connection.ServerBytes) <=
rule.Filter.MinBytes
},
}
connection.MatchedRules = make([]RowID, 0)
for _, rule := range rm.rules {
matching := true
for _, f := range filterFunctions {
if !f(rule) {
matching = false
break
}
}
for _, p := range rule.Patterns {
checkOccurrences := func(occurrences []PatternSlice) bool {
return (p.MinOccurrences == 0 || uint(len(occurrences)) >= p.MinOccurrences) &&
(p.MaxOccurrences == 0 || uint(len(occurrences)) <= p.MaxOccurrences)
}
clientOccurrences, clientPresent := clientMatches[p.internalID]
serverOccurrences, serverPresent := serverMatches[p.internalID]
if p.Direction == DirectionToServer {
if !clientPresent || !checkOccurrences(clientOccurrences) {
matching = false
break
}
} else if p.Direction == DirectionToClient {
if !serverPresent || !checkOccurrences(serverOccurrences) {
matching = false
break
}
} else {
if !(clientPresent || serverPresent) || !checkOccurrences(append(clientOccurrences, serverOccurrences...)) {
matching = false
break
}
}
}
if matching {
connection.MatchedRules = append(connection.MatchedRules, rule.ID)
}
}
rm.mutex.Unlock()
}
func (rm *rulesManagerImpl) DatabaseUpdateChannel() chan RulesDatabase {
return rm.databaseUpdated
}
func (rm *rulesManagerImpl) validateAndAddRuleLocal(rule *Rule) error {
if _, alreadyPresent := rm.rulesByName[rule.Name]; alreadyPresent {
return errors.New("rule name must be unique")
}
newPatterns := make([]*hyperscan.Pattern, 0, len(rule.Patterns))
duplicatePatterns := make(map[string]bool)
for i, pattern := range rule.Patterns {
if err := rm.validate.Struct(pattern); err != nil {
return err
}
regex := pattern.Regex
if !strings.HasPrefix(regex, "/") {
regex = fmt.Sprintf("/%s", regex)
}
if !strings.HasSuffix(regex, "/") {
regex = fmt.Sprintf("%s/", regex)
}
rule.Patterns[i].Regex = regex
compiledPattern, err := pattern.BuildPattern()
if err != nil {
return err
}
regex = compiledPattern.String()
if _, isPresent := duplicatePatterns[regex]; isPresent {
return errors.New("duplicate pattern")
}
if existingPattern, isPresent := rm.patternsIds[regex]; isPresent {
rule.Patterns[i].internalID = existingPattern
continue
}
id := len(rm.patternsIds) + len(newPatterns)
rule.Patterns[i].internalID = uint(id)
compiledPattern.Id = id
newPatterns = append(newPatterns, compiledPattern)
duplicatePatterns[regex] = true
}
startID := len(rm.patterns)
for id, pattern := range newPatterns {
rm.patterns = append(rm.patterns, pattern)
regex := pattern.String()
rm.patternsIds[regex[strings.IndexByte(regex, ':')+1:]] = uint(startID + id)
}
rm.rules[rule.ID] = *rule
rm.rulesByName[rule.Name] = *rule
return nil
}
func (rm *rulesManagerImpl) generateDatabase(version RowID) error {
database, err := hyperscan.NewStreamDatabase(rm.patterns...)
if err != nil {
return err
}
go func() {
rm.databaseUpdated <- RulesDatabase{
database: database,
databaseSize: len(rm.patterns),
version: version,
}
}()
return nil
}
func (p *Pattern) BuildPattern() (*hyperscan.Pattern, error) {
hp, err := hyperscan.ParsePattern(p.Regex)
if err != nil {
return nil, err
}
hp.Flags |= hyperscan.SomLeftMost
if p.Flags.Caseless {
hp.Flags |= hyperscan.Caseless
}
if p.Flags.DotAll {
hp.Flags |= hyperscan.DotAll
}
if p.Flags.MultiLine {
hp.Flags |= hyperscan.MultiLine
}
if p.Flags.Utf8Mode {
hp.Flags |= hyperscan.Utf8Mode
}
if p.Flags.UnicodeProperty {
hp.Flags |= hyperscan.UnicodeProperty
}
if !hp.IsValid() {
return nil, errors.New("can't validate the pattern")
}
return hp, nil
}