-
Notifications
You must be signed in to change notification settings - Fork 43
/
visitor_param.go
505 lines (492 loc) · 14.8 KB
/
visitor_param.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package rule
import (
"fmt"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/suricata/data/modifier"
"github.com/yaklang/yaklang/common/suricata/data/numrange"
"github.com/yaklang/yaklang/common/suricata/parser"
"github.com/yaklang/yaklang/common/suricata/pcre"
"github.com/yaklang/yaklang/common/utils"
"strconv"
"strings"
)
func modifierMapping(str string) modifier.Modifier {
switch str {
case "file_data", "file.data":
return modifier.FileData
case "http_content_type", "http.content_type":
return modifier.HTTPContentType
case "http_content_len", "http.content_len":
return modifier.HTTPContentLen
case "http_start", "http.start":
return modifier.HTTPStart
case "http_protocol", "http.protocol":
return modifier.HTTPProtocol
case "http_header_names", "http.header_names":
return modifier.HTTPHeaderNames
case "http_request_line", "http.request_line":
return modifier.HTTPRequestLine
case "http_accept", "http.accept":
return modifier.HTTPAccept
case "http_accept_enc", "http.accept_enc":
return modifier.HTTPAcceptEnc
case "http_referer", "http.referer":
return modifier.HTTPReferer
case "http_connection", "http.connection":
return modifier.HTTPConnection
case "http_response_line", "http.response_line":
return modifier.HTTPResponseLine
case "dns_query", "dns.query":
return modifier.DNSQuery
case "http_header", "http.header":
return modifier.HTTPHeader
case "http_raw_header", "http.raw_header":
return modifier.HTTPHeaderRaw
case "http_cookie", "http.cookie":
return modifier.HTTPCookie
case "http_uri", "http.uri":
return modifier.HTTPUri
case "http_raw_uri", "http.uri.raw":
return modifier.HTTPUriRaw
case "http_method", "http.method":
return modifier.HTTPMethod
case "http_user_agent", "http.user_agent":
return modifier.HTTPUserAgent
case "http_host", "http.host":
return modifier.HTTPHost
case "http_raw_host", `http.raw_host`:
return modifier.HTTPHostRaw
case "http_stat_msg", "http.stat_msg":
return modifier.HTTPStatMsg
case "http_stat_code", "http.stat_code":
return modifier.HTTPStatCode
case "http_client_body", "http.client_body", "http_request_body", "http.request_body":
return modifier.HTTPRequestBody
case "http_server_body", "http.server_body":
return modifier.HTTPResponseBody
case "http_server", "http.server":
return modifier.HTTPServer
case "http_location", "http.location":
return modifier.HTTPLocation
case "ipv4.hdr", "ipv4_hdr":
return modifier.IPv4HDR
case "ipv6.hdr", "ipv6_hdr":
return modifier.IPv6HDR
case "tcp.hdr", "tcp_hdr":
return modifier.TCPHDR
}
return modifier.Default
}
func mustSoloSingleSetting(ssts []parser.ISingleSettingContext) (bool, string) {
if len(ssts) != 1 {
return false, ""
}
ctx := ssts[0].(*parser.SingleSettingContext)
data := ctx.GetText()
return ctx.Negative() != nil, data
}
type MultipleBufferMatching struct {
last modifier.Modifier
}
func (m *MultipleBufferMatching) transfer(mdf modifier.Modifier) modifier.Modifier {
switch mdf {
case modifier.DNSQuery, modifier.FileData, modifier.HTTPHeader:
m.last = mdf
case modifier.Default:
mdf = m.last
default:
m.last = modifier.Default
}
return mdf
}
func (v *RuleSyntaxVisitor) VisitParams(i *parser.ParamsContext, rule *Rule) {
const (
HasNone = iota
HasContent
HasModif
ModifContent
ContentModif
SavingPCRE
)
STATUS := HasNone
var contents []*ContentRule
multipleBufferMatching := new(MultipleBufferMatching)
contentRule := new(ContentRule)
params := i.AllParam()
for i := 0; i < len(params); i++ {
if params[i] == nil {
continue
}
paramctx := params[i].(*parser.ParamContext)
key := paramctx.Keyword().GetText()
key = strings.TrimSpace(key)
if key == "" {
continue
}
var setting *parser.SettingContext
var ssts []parser.ISingleSettingContext
var vStr string
vParams := make(map[string]interface{})
if st := paramctx.Setting(); st != nil {
setting = paramctx.Setting().(*parser.SettingContext)
vStr = strings.TrimSpace(setting.GetText())
ssts = setting.AllSingleSetting()
}
switch STATUS {
case HasNone:
if modifierMapping(key) != modifier.Default {
STATUS = HasModif
} else if key == "content" {
STATUS = HasContent
} else if key == "pcre" {
STATUS = SavingPCRE
} else {
STATUS = HasNone
}
case HasContent:
if modifierMapping(key) != modifier.Default {
STATUS = ContentModif
} else if key == "content" {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
contentRule = new(ContentRule)
STATUS = HasContent
} else if key == "pcre" {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
contentRule = new(ContentRule)
STATUS = SavingPCRE
} else {
STATUS = HasContent
}
case SavingPCRE:
contents = append(contents, contentRule)
contentRule = new(ContentRule)
if modifierMapping(key) != modifier.Default {
STATUS = ContentModif
} else if key == "content" {
STATUS = HasContent
} else if key == "pcre" {
STATUS = SavingPCRE
} else {
STATUS = HasNone
}
case HasModif:
if modifierMapping(key) != modifier.Default {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
contentRule = new(ContentRule)
STATUS = HasModif
} else if key == "content" {
STATUS = ModifContent
}
case ModifContent, ContentModif:
if modifierMapping(key) != modifier.Default {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
contentRule = new(ContentRule)
STATUS = HasModif
} else if key == "content" {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
contentRule = new(ContentRule)
STATUS = HasContent
} else if key == "pcre" {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
contentRule = new(ContentRule)
STATUS = SavingPCRE
}
}
log.Debugf("key: %#v value: %#v", key, vStr)
switch key {
// meta keywords
case "sid":
rule.Sid, _ = strconv.Atoi(vStr)
case "rev":
rule.Rev, _ = strconv.Atoi(vStr)
case "gid":
rule.Gid, _ = strconv.Atoi(vStr)
case "classtype":
rule.ClassType = vStr
case "reference":
if rule.Reference == nil {
rule.Reference = map[string]string{}
}
if len(ssts) == 2 {
rule.Reference[ssts[0].GetText()] = ssts[1].GetText()
} else {
rule.Reference[vStr] = ""
}
case "msg":
rule.Message, _ = strconv.Unquote(vStr)
case "priority":
rule.Priority, _ = strconv.Atoi(vStr)
case "metadata":
for _, v := range ssts {
rule.Metadata = append(rule.Metadata, v.GetText())
}
case "content":
neg, content := mustSoloSingleSetting(ssts)
contentRule.Content = []byte(unquoteAndParseHex(content))
contentRule.Negative = neg
case "dns.opcode", "dns_opcode":
neg, content := mustSoloSingleSetting(ssts)
rule.ContentRuleConfig.DNS = &DNSRule{
OpcodeNegative: neg,
Opcode: atoi(content),
}
case "flow":
if rule.ContentRuleConfig.Flow == nil {
lvstr := strings.ToLower(vStr)
rule.ContentRuleConfig.Flow = &FlowRule{
ToClient: strings.Contains(lvstr, "to_client"),
Established: strings.Contains(lvstr, "established"),
ToServer: strings.Contains(lvstr, "to_server"),
}
}
case "ttl":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.TTL = atoi(vStr)
case "sameip":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.Sameip = true
case "ipopts":
/*
rr Record Route
eol End of List
nop No Op
ts Time Stamp
sec IP Security
esec IP Extended Security
lsrr Loose Source Routing
ssrr Strict Source Routing
satid Stream Identifier
any any IP options are set
*/
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.IPOpts = vStr
case "ip_proto":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.IPProto = vStr //number or name
case "id":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.Id = atoi(vStr)
case "geoip":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.Geoip = vStr
case "fragbits":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.FragBits = vStr
case "fragoffset":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.FragOffset = vStr
case "tos":
if rule.ContentRuleConfig.IPConfig == nil {
rule.ContentRuleConfig.IPConfig = &IPLayerRule{}
}
rule.ContentRuleConfig.IPConfig.Tos = vStr
case "flags":
/*
S: 匹配TCP SYN标志位
F: 匹配TCP FIN标志位
R: 匹配TCP RST标志位
P: 匹配TCP PUSH标志位
U: 匹配TCP URG标志位
E: 匹配TCP ECE标志位
C: 匹配TCP CWR标志位
*/
if rule.ContentRuleConfig.TcpConfig == nil {
rule.ContentRuleConfig.TcpConfig = &TCPLayerRule{}
}
rule.ContentRuleConfig.TcpConfig.Flags = vStr
case "seq":
if rule.ContentRuleConfig.TcpConfig == nil {
rule.ContentRuleConfig.TcpConfig = &TCPLayerRule{}
}
seq := atoi(vStr)
rule.ContentRuleConfig.TcpConfig.Seq = &seq
case "ack":
if rule.ContentRuleConfig.TcpConfig == nil {
rule.ContentRuleConfig.TcpConfig = &TCPLayerRule{}
}
ack := atoi(vStr)
rule.ContentRuleConfig.TcpConfig.Ack = &ack
case "window":
if rule.ContentRuleConfig.TcpConfig == nil {
rule.ContentRuleConfig.TcpConfig = &TCPLayerRule{}
}
neg, content := mustSoloSingleSetting(ssts)
window := atoi(content)
rule.ContentRuleConfig.TcpConfig.NegativeWindow, rule.ContentRuleConfig.TcpConfig.Window = neg, &window
case "threshold":
config := &ThresholdingConfig{}
config.Count = atoi(utils.MapGetString(vParams, "count"))
config.Track = utils.MapGetString(vParams, "track")
switch utils.MapGetString(vParams, "type") {
case "both":
config.ThresholdMode = true
config.LimitMode = true
case "threshold":
config.ThresholdMode = true
case "limit":
config.LimitMode = true
}
config.Seconds = atoi(utils.MapGetString(vParams, "seconds"))
rule.ContentRuleConfig.Thresholding = config
case "icode":
/*
icode:min<>max;
icode:[<|>]<number>;
*/
if rule.ContentRuleConfig.IcmpConfig == nil {
rule.ContentRuleConfig.IcmpConfig = &ICMPLayerRule{}
}
numRange, err := numrange.ParseNumRange(vStr)
if err != nil {
log.Errorf("parse icmp icode err:%v", err)
continue
}
rule.ContentRuleConfig.IcmpConfig.ICode = numRange
case "itype":
/*
itype:min<>max;
itype:[<|>]<number>;
*/
if rule.ContentRuleConfig.IcmpConfig == nil {
rule.ContentRuleConfig.IcmpConfig = &ICMPLayerRule{}
}
numRange, err := numrange.ParseNumRange(vStr)
if err != nil {
log.Errorf("parse icmp itype err:%v", err)
continue
}
rule.ContentRuleConfig.IcmpConfig.IType = numRange
case "icmp_id":
// icmp_id:<number>
if rule.ContentRuleConfig.IcmpConfig == nil {
rule.ContentRuleConfig.IcmpConfig = &ICMPLayerRule{}
}
num := atoi(vStr)
rule.ContentRuleConfig.IcmpConfig.ICMPId = &num
case "icmp_seq":
// icmp_seq:<number>;
if rule.ContentRuleConfig.IcmpConfig == nil {
rule.ContentRuleConfig.IcmpConfig = &ICMPLayerRule{}
}
num := atoi(vStr)
rule.ContentRuleConfig.IcmpConfig.ICMPSeq = &num
case "urilen":
if rule.ContentRuleConfig.HTTPConfig == nil {
rule.ContentRuleConfig.HTTPConfig = &HTTPConfig{}
}
numRange, err := numrange.ParseNumRange(vStr)
if err != nil {
log.Errorf("parse urilen err:%v", err)
continue
}
rule.ContentRuleConfig.HTTPConfig.Urilen = numRange
case "nocase":
contentRule.Nocase = true
case "depth":
contentRule.Depth = atoistar(vStr)
case "offset":
contentRule.Offset = atoistar(vStr)
case "startswith":
contentRule.StartsWith = true
case "endswith":
contentRule.EndsWith = true
case "distance":
contentRule.Distance = atoistar(vStr)
case "within":
contentRule.Within = atoistar(vStr)
case "rawbytes":
contentRule.RawBytes = true
case "isdataat":
contentRule.IsDataAt = vStr
case "bsize":
contentRule.BSize = vStr
case "dsize":
contentRule.DSize = vStr
case "byte_test":
contentRule.ByteTest = vStr
case "byte_math":
contentRule.ByteMath = vStr
case "byte_extract":
contentRule.ByteExtract = vStr
case "byte_jump":
contentRule.ByteJump = vStr
case "rpc":
contentRule.RPC = vStr
case "replace":
contentRule.RPC = unquoteAndParseHex(vStr)
case "pcre":
contentRule.PCRE = unquoteString(vStr)
parsed, err := pcre.ParsePCREStr(contentRule.PCRE)
if err != nil {
log.Errorf("parsePCREStr err:%v", err)
}
contentRule.PCREParsed = parsed
contentRule.Modifier = parsed.Modifier()
case "tcp.mss":
if rule.ContentRuleConfig.TcpConfig == nil {
rule.ContentRuleConfig.TcpConfig = &TCPLayerRule{}
}
numRange, err := numrange.ParseNumRange(vStr)
if err != nil {
log.Errorf("parse tcp.mss err:%v", err)
continue
}
rule.ContentRuleConfig.TcpConfig.TCPMss = numRange
case "fast_pattern":
contentRule.FastPattern = true
case "flowbits":
contentRule.FlowBits = vStr
case "noalert":
contentRule.NoAlert = true
case "base64_decode":
contentRule.Base64Decode = vStr
case "base64_data":
contentRule.Base64Data = true
case "flowint":
contentRule.FlowInt = vStr
case "xbits":
contentRule.XBits = vStr
case "app-layer-event":
contentRule.ExtraFlags = append(contentRule.ExtraFlags, fmt.Sprintf("%v:%v", key, vStr))
default:
if modifierMapping(key) != modifier.Default {
contentRule.Modifier = modifierMapping(key)
break
}
// fallback
contentRule.ExtraFlags = append(contentRule.ExtraFlags, fmt.Sprintf("%v:%v", key, vStr))
if key != "metad" {
log.Debugf("unknown content rule params: %s\n%v\n\n", key, rule.Raw)
} else {
log.Warnf("BAD RULE:\n\n%v\n\n", rule.Raw)
}
}
}
if STATUS != HasNone {
contentRule.Modifier = multipleBufferMatching.transfer(contentRule.Modifier)
contents = append(contents, contentRule)
}
rule.ContentRuleConfig.ContentRules = append(rule.ContentRuleConfig.ContentRules, contents...)
}