Skip to content

Commit 917ecbf

Browse files
authored
Refactor YARA syntax validation handler
1 parent 4d044d1 commit 917ecbf

1 file changed

Lines changed: 0 additions & 15 deletions

File tree

misp_modules/modules/expansion/yara_syntax_validator.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def insert_import_module(rule_text, module_name):
4444
return f'import "{module_name}"\n' + rule_text
4545
return rule_text
4646

47-
4847
# -------------------------------
4948
# HANDLER NOW DOES VALIDATION LOGIC
5049
# -------------------------------
@@ -69,59 +68,45 @@ def insert_import_module(rule_text, module_name):
6968
def handler(q=False):
7069
if q is False:
7170
return False
72-
7371
request = json.loads(q)
7472
rule_content = request.get("yara")
75-
7673
if not rule_content:
7774
misperrors["error"] = "Yara rule missing"
7875
return misperrors
79-
8076
externals = {}
8177
attempts = 0
8278
max_attempts = 10 # to prevent from infinite loop
8379
current_rule_text = rule_content
84-
8580
while attempts < max_attempts:
8681
try:
8782
yara.compile(source=current_rule_text, externals=externals) # some times the compilator needs externals variables
8883
summary = "Syntax valid"
8984
break
9085
except yara.SyntaxError as e:
9186
error_msg = str(e)
92-
9387
# try to catch modules or externals variables errors to auto correct it
9488
match_id = re.search(r'undefined identifier "(\w+)"', error_msg)
9589
if match_id:
9690
var_name = match_id.group(1)
97-
9891
# Auto-import YARA modules
9992
if var_name in YARA_MODULES:
10093
current_rule_text = insert_import_module(current_rule_text, var_name)
10194
else:
10295
# Treat as external variable
10396
externals[var_name] = "example.txt" # a random value so that the compiler does not make an error (most of the time the external variable are in other configs files)
104-
10597
attempts += 1
10698
continue
107-
10899
# Other syntax errors
109100
summary = "Syntax error: " + error_msg
110101
break
111-
112102
else:
113103
# Max attempts exceeded
114104
summary = "Syntax error: Max validation attempts exceeded"
115-
116105
return {"results": [{"types": mispattributes["output"], "values": summary}]}
117106

118-
119107
def introspection():
120108
return mispattributes
121109

122-
123110
def version():
124111
moduleinfo["config"] = moduleconfig
125112
return moduleinfo
126-
127-

0 commit comments

Comments
 (0)