Skip to content

Commit 4b7ad6e

Browse files
committed
rules: fix parsing of address lists
The previous parser just looked for the next "]" to find the end of a list without respect for list depth. Instead step through the array tracking the depth of the nested lists. Ticket: #7799
1 parent 8d9170d commit 4b7ad6e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

suricata/update/rule.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,16 @@ def parse(buf, group=None):
212212
if not rem:
213213
return None
214214
if rem[0] == "[":
215-
end = rem.find("]")
216-
if end < 0:
217-
return
215+
depth = 1
216+
end = 0
217+
while depth > 0:
218+
end += 1
219+
if end >= len(rem):
220+
return
221+
if rem[end] == "[":
222+
depth += 1
223+
elif rem[end] == "]":
224+
depth -= 1
218225
end += 1
219226
token = rem[:end].strip()
220227
rem = rem[end:].strip()

tests/test_rule.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,11 @@ def test_parse_feature_ja3(self):
254254
rule = suricata.update.rule.parse(rule_string)
255255
self.assertIsNotNone(rule)
256256
self.assertTrue("ja3" in rule["features"])
257+
258+
def test_parse_var_lists(self):
259+
rule_string = u"""alert http [any,![$EXTERNAL_IP,$REVERSE_PROXY_HOSTS,$ODD_HTTP_HOSTS]] any -> [any,![$EXTERNAL_IP,$REVERSE_PROXY_HOSTS,$ODD_HTTP_HOSTS]] 80 (msg:"TEST Unknown var"; sid: 99000003; rev: 1;)"""
260+
rule = suricata.update.rule.parse(rule_string)
261+
self.assertEqual(rule["source_addr"], "[any,![$EXTERNAL_IP,$REVERSE_PROXY_HOSTS,$ODD_HTTP_HOSTS]]")
262+
self.assertEqual(rule["source_port"], "any")
263+
self.assertEqual(rule["dest_addr"], "[any,![$EXTERNAL_IP,$REVERSE_PROXY_HOSTS,$ODD_HTTP_HOSTS]]")
264+
self.assertEqual(rule["dest_port"], "80")

0 commit comments

Comments
 (0)