forked from coreruleset/secrules_parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
117 lines (101 loc) · 4.1 KB
/
test_api.py
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
# -*- coding: utf-8 -*-
import glob
import os
import re
from secrules_parsing import parser
def test_api() -> None:
"""Test that usage as API works"""
# Extract all of our pathing
files = glob.glob("../../rules/*.conf")
# Pass absolute paths because of module location
files = [os.path.abspath(path) for path in files]
models = parser.process_rules(files)
def test_model_parse() -> None:
"""Test that we can parse the model correctly"""
rule_text = """
SecRule ARGS "@rx found" "id:1,log,noauditlog,t:lowercase,block"
SecRule FILES:pluginzip "@endsWith .zip" "id:2,phase:2,pass,t:none,ctl:ruleRemoveTargetById=944110;REQUEST_BODY,ctl:ruleRemoveTargetById=944250;REQUEST_BODY"
"""
parsed_rule = parser.process_from_str(rule_text)
# print(ppretty(parsed_rule, depth=10))
for rule in parsed_rule.rules:
assert (rule.__class__.__name__) == "SecRule"
for var in rule.variables:
assert var.collection in ["ARGS", "FILES"]
def test_operator_contains_works_with_greater_than() -> None:
"""Test that the contains operator works correctly using greater than"""
rule_text = """
SecRule REQUEST_COOKIES|!REQUEST_COOKIES:/__utm/|REQUEST_COOKIES_NAMES|ARGS_NAMES|ARGS|XML:/* "@contains -->" \
"id:941181,\
phase:2,\
block,\
capture,\
t:none,t:utf8toUnicode,t:urlDecodeUni,t:htmlEntityDecode,t:jsDecode,t:cssDecode,t:lowercase,t:removeNulls,\
msg:'Node-Validator Deny List Keywords',\
logdata:'Matched Data: %{TX.0} found within %{MATCHED_VAR_NAME}: %{MATCHED_VAR}',\
tag:'application-multi',\
tag:'language-multi',\
tag:'platform-multi',\
tag:'attack-xss',\
tag:'paranoia-level/2',\
tag:'OWASP_CRS',\
tag:'capec/1000/152/242',\
ctl:auditLogParts=+E,\
ver:'OWASP_CRS/4.0.0-rc1',\
severity:'CRITICAL',\
setvar:'tx.xss_score=+%{tx.critical_anomaly_score}',\
setvar:'tx.inbound_anomaly_score_pl2=+%{tx.critical_anomaly_score}'"
"""
parsed_rule = parser.process_from_str(rule_text)
for rule in parsed_rule.rules:
assert rule.__class__.__name__ == "SecRule"
assert rule.operator.contains == "-->"
def test_collection_argument_with_dollar() -> None:
"""Test that a collection argument can contain `$` (e.g., a key in a JSON document)"""
rule_text = """
SecRule REQUEST_FILENAME "@rx /apps/mail/api/messages/[0-9]+/flags$" \
"id:9508978,\
phase:1,\
pass,\
t:none,\
nolog,\
ver:'nextcloud-rule-exclusions-plugin/1.0.0',\
ctl:ruleRemoveTargetById=942290;ARGS_NAMES:json.flags.$notjunk,\
setvar:'tx.allowed_methods=%{tx.allowed_methods} PUT'"
"""
parsed_rule = parser.process_from_str(rule_text)
matched = False
for rule in parsed_rule.rules:
assert rule.__class__.__name__ == "SecRule"
for action in rule.actions:
if action.ctl:
matched = True
assert action.ctl.ruleRemoveTargetById == 942290
assert action.ctl.removeVariableName == "json.flags.$notjunk"
assert matched
def test_lowercase_and_uppercase_in_argument() -> None:
""" Example test showing how to find if a rule has a lowercase transformation, then see if the target
of the rule has an uppercase regex. """
rule_text = """
SecRule REQUEST_FILENAME "@rx /[ABCD]+/$" \
"id:1234,\
phase:1,\
pass,\
t:lowercase,\
nolog
"""
matched = False
uppercase_regex = re.compile(r"[A-Z]")
parsed_rule = parser.process_from_str(rule_text)
for rule in parsed_rule.rules:
assert rule.__class__.__name__ == "SecRule"
for action in rule.actions:
if action.transformations:
for t in action.transformations:
if t == "lowercase":
if uppercase_regex.search(rule.operator.rx):
matched = True
assert True, ("Regex tries to match uppercase, "
"but you are transforming into lowercase so it will "
"never match")
assert matched