-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.py
73 lines (58 loc) · 1.8 KB
/
importer.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
import sys
import re
from signature import Signature
# Regular expression pattern for matching signature rules
REGEX = re.compile(r""" ^
(\d{,99999}:\s)? #! sID Range badaio
([A-Z]{,4}\s) # PROTO
(!?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:)|any:) # IP
(!?[0-9]{,6}\s|(any)\s|!?\[[0-9]{,6}-[0-9]{,6}\]\s) # PORT
(<>\s|->\s) # DIR
(!?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:)|any:) # IP
(!?[0-9]{,6}\s|(any)\s|!?\[[0-9]{,6}-[0-9]{,6}]\s) # PORT
(\*) # PAYLOAD
$ """, re.VERBOSE)
# Default path for the rule file
DEFAULT_RULEPATH = 'eval.rules'
def load_rules(rule_path="test.rules"):
"""
Load signature rules from a file.
Args:
rule_path (str): Path to the rule file.
Returns:
list: List of Signature objects.
"""
try:
with open(rule_path) as file:
rules = file.readlines()
except FileNotFoundError:
sys.exit(f"Error: Rule file '{rule_path}' not found.")
else:
return [Signature(rule.strip()) for rule in rules if rule.strip() and not rule.startswith('#')]
#
# try:
# RULES = load_rules(RULEPATH)
# print('[*] parsed rules')
# except ValueError as err:
# exit(f"[@] {err}")
def main():
"""
Main function for loading and parsing signature rules.
"""
# Determine the rule file path
try:
rule_path = sys.argv[1]
except IndexError:
rule_path = DEFAULT_RULEPATH
print(f"[*] Loading rules from '{rule_path}'")
# Load and parse rules
try:
rules = load_rules(rule_path)
except ValueError as err:
sys.exit(f"Error: {err}")
print(f"[*] Successfully loaded {len(rules)} rules.")
print("[*] Rules:")
for rule in rules:
print(rule)
if __name__ == "__main__":
main()