/
ctl.go
429 lines (405 loc) · 11.3 KB
/
ctl.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
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package actions
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/corazawaf/coraza/v3/debuglog"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/collections"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
utils "github.com/corazawaf/coraza/v3/internal/strings"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
type ctlFunctionType int
const (
ctlUnknown ctlFunctionType = iota
ctlRuleRemoveTargetByID ctlFunctionType = iota
ctlRuleRemoveTargetByTag ctlFunctionType = iota
ctlRuleRemoveTargetByMsg ctlFunctionType = iota
ctlAuditEngine ctlFunctionType = iota
ctlAuditLogParts ctlFunctionType = iota
ctlForceRequestBodyVariable ctlFunctionType = iota
ctlRequestBodyAccess ctlFunctionType = iota
ctlRequestBodyLimit ctlFunctionType = iota
ctlRuleEngine ctlFunctionType = iota
ctlRuleRemoveByID ctlFunctionType = iota
ctlRuleRemoveByMsg ctlFunctionType = iota
ctlRuleRemoveByTag ctlFunctionType = iota
ctlHashEngine ctlFunctionType = iota
ctlHashEnforcement ctlFunctionType = iota
ctlRequestBodyProcessor ctlFunctionType = iota
ctlForceResponseBodyVariable ctlFunctionType = iota
ctlResponseBodyProcessor ctlFunctionType = iota
ctlResponseBodyAccess ctlFunctionType = iota
ctlResponseBodyLimit ctlFunctionType = iota
ctlDebugLogLevel ctlFunctionType = iota
)
type ctlFn struct {
action ctlFunctionType
value string
collection variables.RuleVariable
colKey string
}
func (a *ctlFn) Init(_ plugintypes.RuleMetadata, data string) error {
var err error
a.action, a.value, a.collection, a.colKey, err = parseCtl(data)
return err
}
// parseOnOff turns a string value into a boolean equivalent on/off into true/false
func parseOnOff(s string) (bool, bool) {
val := strings.ToLower(s)
switch val {
case "on":
return true, true
case "off":
return false, true
default:
return false, false
}
}
func (a *ctlFn) Evaluate(_ plugintypes.RuleMetadata, txS plugintypes.TransactionState) {
tx := txS.(*corazawaf.Transaction)
switch a.action {
case ctlRuleRemoveTargetByID:
ran, err := rangeToInts(tx.WAF.Rules.GetRules(), a.value)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "RuleRemoveTargetByID").
Err(err).
Msg("Invalid range")
return
}
for _, id := range ran {
tx.RemoveRuleTargetByID(id, a.collection, a.colKey)
}
case ctlRuleRemoveTargetByTag:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if utils.InSlice(a.value, r.Tags_) {
tx.RemoveRuleTargetByID(r.ID(), a.collection, a.colKey)
}
}
case ctlRuleRemoveTargetByMsg:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if r.Msg != nil && r.Msg.String() == a.value {
tx.RemoveRuleTargetByID(r.ID(), a.collection, a.colKey)
}
}
case ctlAuditEngine:
ae, err := types.ParseAuditEngineStatus(a.value)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "AuditEngine").
Str("value", a.value).
Err(err).
Msg("Invalid status")
return
}
tx.AuditEngine = ae
case ctlAuditLogParts:
AuditLogParts, err := types.ParseAuditLogParts(a.value)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "AuditLogParts").
Str("value", a.value).
Err(err).
Msg("Invalid audit log part")
return
}
tx.AuditLogParts = AuditLogParts
case ctlForceRequestBodyVariable:
val, ok := parseOnOff(a.value)
if !ok {
tx.DebugLogger().Error().
Str("ctl", "ForceRequestBodyVariable").
Str("value", a.value).
Msg("Unknown toggle")
return
}
tx.ForceRequestBodyVariable = val
tx.DebugLogger().Debug().
Str("ctl", "ForceRequestBodyVariable").
Bool("value", val).
Msg("Forcing request body var")
case ctlRequestBodyAccess:
if tx.LastPhase() <= types.PhaseRequestHeaders {
val, ok := parseOnOff(a.value)
if !ok {
tx.DebugLogger().Error().
Str("ctl", "RequestBodyAccess").
Str("value", a.value).
Msg("Unknown toggle")
return
}
tx.RequestBodyAccess = val
} else {
tx.DebugLogger().Warn().
Str("ctl", "RequestBodyAccess").
Msg("Cannot change request body access after request headers phase")
return
}
case ctlRequestBodyLimit:
if tx.LastPhase() <= types.PhaseRequestHeaders {
limit, err := strconv.ParseInt(a.value, 10, 64)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "RequestBodyLimit").
Str("value", a.value).
Err(err).
Msg("Invalid limit")
return
}
tx.RequestBodyLimit = limit
} else {
tx.DebugLogger().Warn().
Str("ctl", "RequestBodyLimit").
Msg("Cannot change request body limit after request headers phase")
return
}
case ctlRequestBodyProcessor:
if tx.LastPhase() <= types.PhaseRequestHeaders {
tx.Variables().RequestBodyProcessor().(*collections.Single).Set(strings.ToUpper(a.value))
} else {
tx.DebugLogger().Warn().
Str("ctl", "RequestBodyProcessor").
Msg("Cannot change request body processor after request headers phase")
}
case ctlRuleEngine:
re, err := types.ParseRuleEngineStatus(a.value)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "RuleEngine").
Str("value", a.value).
Err(err).
Msg("Invalid status")
return
}
tx.RuleEngine = re
case ctlRuleRemoveByID:
if idx := strings.Index(a.value, "-"); idx == -1 {
id, err := strconv.Atoi(a.value)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "RuleRemoveByID").
Str("value", a.value).
Err(err).
Msg("Invalid rule ID")
return
}
tx.RemoveRuleByID(id)
} else {
ran, err := rangeToInts(tx.WAF.Rules.GetRules(), a.value)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "RuleRemoveByID").
Err(err).
Msg("Invalid range")
return
}
for _, id := range ran {
tx.RemoveRuleByID(id)
}
}
case ctlRuleRemoveByMsg:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if r.Msg != nil && r.Msg.String() == a.value {
tx.RemoveRuleByID(r.ID_)
}
}
case ctlRuleRemoveByTag:
rules := tx.WAF.Rules.GetRules()
for _, r := range rules {
if utils.InSlice(a.value, r.Tags_) {
tx.RemoveRuleByID(r.ID_)
}
}
case ctlResponseBodyAccess:
if tx.LastPhase() <= types.PhaseResponseHeaders {
val, ok := parseOnOff(a.value)
if !ok {
tx.DebugLogger().Error().
Str("ctl", "ResponseBodyAccess").
Str("value", a.value).
Msg("Unknown toggle")
return
}
tx.ResponseBodyAccess = val
} else {
tx.DebugLogger().Warn().
Str("ctl", "ResponseBodyAccess").
Msg("Cannot change response body access after response headers phase")
return
}
case ctlResponseBodyLimit:
if tx.LastPhase() <= types.PhaseResponseHeaders {
limit, err := strconv.ParseInt(a.value, 10, 64)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "ResponseBodyLimit").
Str("value", a.value).
Err(err).
Msg("Invalid limit")
return
}
tx.ResponseBodyLimit = limit
} else {
tx.DebugLogger().Warn().
Str("ctl", "ResponseBodyLimit").
Msg("Cannot change response body access after response headers phase")
return
}
case ctlForceResponseBodyVariable:
val, ok := parseOnOff(a.value)
if !ok {
tx.DebugLogger().Error().
Str("ctl", "ForceResponseBodyVariable").
Str("value", a.value).
Msg("Unknown toggle")
return
}
tx.ForceResponseBodyVariable = val
tx.WAF.Logger.Debug().
Str("ctl", "ForceResponseBodyVariable").
Bool("value", val).
Msg("Forcing response body var")
case ctlResponseBodyProcessor:
if tx.LastPhase() <= types.PhaseResponseHeaders {
// We are still in time to set the response body processor
// TODO(jcchavezs): Who should hold this knowledge?
// TODO(jcchavezs): Shall we validate such body processor exists or is it
// too ambitious as plugins might register their own at some point in the
// lifecycle which does not have to happen before this.
tx.Variables().ResponseBodyProcessor().(*collections.Single).Set(strings.ToUpper(a.value))
} else {
tx.DebugLogger().Warn().
Str("ctl", "ResponseBodyLimit").
Msg("Cannot change response body access after response headers phase")
return
}
case ctlHashEngine:
// Not supported yet
case ctlHashEnforcement:
// Not supported yet
case ctlDebugLogLevel:
lvl, err := strconv.ParseInt(a.value, 10, 8)
if err != nil {
tx.DebugLogger().Error().
Str("ctl", "DebugLogLevel").
Str("value", a.value).
Err(err).
Msg("Invalid log level")
return
}
tx.SetDebugLogLevel(debuglog.Level(lvl))
}
}
func (a *ctlFn) Type() plugintypes.ActionType {
return plugintypes.ActionTypeNondisruptive
}
func parseCtl(data string) (ctlFunctionType, string, variables.RuleVariable, string, error) {
action, ctlVal, ok := strings.Cut(data, "=")
if !ok {
return ctlUnknown, "", 0, "", errors.New("invalid syntax")
}
value, col, ok := strings.Cut(ctlVal, ";")
var colkey, colname string
if ok {
colname, colkey, _ = strings.Cut(col, ":")
}
collection, _ := variables.Parse(strings.TrimSpace(colname))
colkey = strings.ToLower(colkey)
var act ctlFunctionType
switch action {
case "auditEngine":
act = ctlAuditEngine
case "auditLogParts":
act = ctlAuditLogParts
case "requestBodyAccess":
act = ctlRequestBodyAccess
case "requestBodyLimit":
act = ctlRequestBodyLimit
case "requestBodyProcessor":
act = ctlRequestBodyProcessor
case "forceRequestBodyVariable":
act = ctlForceRequestBodyVariable
case "responseBodyProcessor":
act = ctlResponseBodyProcessor
case "responseBodyAccess":
act = ctlResponseBodyAccess
case "responseBodyLimit":
act = ctlResponseBodyLimit
case "forceResponseBodyVariable":
act = ctlForceResponseBodyVariable
case "ruleEngine":
act = ctlRuleEngine
case "ruleRemoveById":
act = ctlRuleRemoveByID
case "ruleRemoveByMsg":
act = ctlRuleRemoveByMsg
case "ruleRemoveByTag":
act = ctlRuleRemoveByTag
case "ruleRemoveTargetById":
act = ctlRuleRemoveTargetByID
case "ruleRemoveTargetByMsg":
act = ctlRuleRemoveTargetByMsg
case "ruleRemoveTargetByTag":
act = ctlRuleRemoveTargetByTag
case "hashEngine":
act = ctlHashEngine
case "hashEnforcement":
act = ctlHashEnforcement
case "debugLogLevel":
act = ctlDebugLogLevel
default:
return ctlUnknown, "", 0x00, "", fmt.Errorf("unknown ctl action %q", action)
}
return act, value, collection, strings.TrimSpace(colkey), nil
}
func rangeToInts(rules []corazawaf.Rule, input string) ([]int, error) {
if len(input) == 0 {
return nil, errors.New("empty input")
}
var (
ids []int
start, end int
err error
)
if in0, in1, ok := strings.Cut(input, "-"); ok {
start, err = strconv.Atoi(in0)
if err != nil {
return nil, err
}
end, err = strconv.Atoi(in1)
if err != nil {
return nil, err
}
if start > end {
return nil, errors.New("invalid range, start > end")
}
} else {
id, err := strconv.Atoi(input)
if err != nil {
return nil, err
}
start, end = id, id
}
for _, r := range rules {
if r.ID_ >= start && r.ID_ <= end {
ids = append(ids, r.ID_)
}
}
return ids, nil
}
func ctl() plugintypes.Action {
return &ctlFn{}
}
var (
_ plugintypes.Action = &ctlFn{}
_ ruleActionWrapper = ctl
)