-
Notifications
You must be signed in to change notification settings - Fork 2
/
rule.go
144 lines (125 loc) · 3.09 KB
/
rule.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
package suricataparser
import (
"fmt"
"strconv"
"strings"
)
// Rule stores parsed suricata rule - https://suricata.readthedocs.io/en/latest/rules/intro.html#rules-format
type Rule struct {
Enabled bool
action string
header string
Options []*Option
Metadata *Metadata
References []*Reference
sid int64
gid int64
rev int64
classtype string
msg string
// raw rule representation
raw string
}
func (r *Rule) String() string {
ruleStr := ""
if !r.Enabled {
ruleStr += "# "
}
ruleStr += r.raw
return ruleStr
}
// Sid - rule ID - https://suricata.readthedocs.io/en/latest/rules/meta.html#sid-signature-id
func (r *Rule) Sid() int64 {
return r.sid
}
// Gid - group ID - https://suricata.readthedocs.io/en/latest/rules/meta.html#gid-group-id
func (r *Rule) Gid() int64 {
return r.gid
}
// Msg - rule description - https://suricata.readthedocs.io/en/latest/rules/meta.html#msg-message
func (r *Rule) Msg() string {
return r.msg
}
// Rev rule revision (version) - https://suricata.readthedocs.io/en/latest/rules/meta.html#rev-revision
func (r *Rule) Rev() int64 {
return r.rev
}
// ClassType rule class type - https://docs.suricata.io/en/latest/rules/meta.html#classtype
func (r *Rule) ClassType() string {
return r.classtype
}
// Header defines the protocol, IP addresses, ports and direction of the rule
func (r *Rule) Header() string {
return r.header
}
// Action from rule - https://suricata.readthedocs.io/en/latest/rules/intro.html#action
func (r *Rule) Action() string {
return r.action
}
func (r *Rule) GetOptions(name string) []*Option {
opts := make([]*Option, 0)
for _, opt := range r.Options {
if opt.Name == name {
opts = append(opts, opt)
}
}
return opts
}
func (r *Rule) buildRawRule() {
rawRule := fmt.Sprintf("%s %s ", r.action, r.header)
var opts []string
for _, opt := range r.Options {
opts = append(opts, opt.String())
}
rawRule += fmt.Sprintf("(%s)", strings.Join(opts, " "))
r.raw = rawRule
}
func (r *Rule) fillFromOptions() {
for _, opt := range r.Options {
if opt.Name == OptMsg {
r.msg = strings.Trim(opt.Value, "\"")
}
if opt.Name == OptSid {
r.sid, _ = strconv.ParseInt(opt.Value, 10, 64)
}
if opt.Name == OptGid {
r.gid, _ = strconv.ParseInt(opt.Value, 10, 64)
}
if opt.Name == OptRev {
r.rev, _ = strconv.ParseInt(opt.Value, 10, 64)
}
if opt.Name == OptClasstype {
r.classtype = opt.Value
}
if opt.Name == OptMetadata {
r.fillMetadata(opt.Value)
}
if opt.Name == OptReference {
r.addReference(opt.Value)
}
}
}
func (r *Rule) fillMetadata(rawMetadata string) {
parsed, _ := ParseMetadata(rawMetadata)
r.Metadata.Merge(*parsed)
}
func (r *Rule) addReference(rawReference string) {
parsed, _ := ParseReference(rawReference)
r.References = append(r.References, parsed)
}
func NewRule(enabled bool, action, header, raw string, options []*Option) *Rule {
rule := Rule{
Enabled: enabled,
action: action,
header: header,
Options: options,
raw: raw,
Metadata: NewMetadata(),
}
if raw != "" {
rule.fillFromOptions()
} else {
rule.buildRawRule()
}
return &rule
}