@@ -93,22 +93,22 @@ class AzulPluginYara(BinaryPlugin):
9393 ]
9494 _cached_rules = None
9595
96- def __init__ (self , config : settings .Settings | dict = None ) -> None :
96+ def __init__ (self , config : settings .Settings | dict | None = None ) -> None :
9797 """Check correct config and load/cache rules."""
9898 super ().__init__ (config )
99- if not self .cfg .yara_rules_path or not self .cfg .name_suffix or not self .cfg .version_suffix :
99+ if not self .cfg .yara_rules_path or not self .cfg .name_suffix or not self .cfg .version_suffix : # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
100100 raise Exception ("Plugin requires 'yara_rules_path', 'name_suffix' and 'version_suffix' config to be set" )
101101
102102 if not self .cfg .security_override :
103103 raise Exception ("Plugin requires 'security_override' to be defined" )
104104
105105 # handle config override with env string
106- blacklist = self .cfg .yara_namespace_blacklist
106+ blacklist = self .cfg .yara_namespace_blacklist # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
107107 if isinstance (blacklist , str ):
108108 blacklist = blacklist .split ("," )
109109
110110 # Handle a list of regex to only load certain file types
111- yara_file_to_load_regex = self .cfg .yara_only_load_files_named
111+ yara_file_to_load_regex = self .cfg .yara_only_load_files_named # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
112112 if isinstance (yara_file_to_load_regex , list ) and len (yara_file_to_load_regex ) > 0 :
113113 yara_file_to_load_regex = [re .compile (expression ) for expression in yara_file_to_load_regex ]
114114 elif isinstance (yara_file_to_load_regex , str ):
@@ -117,10 +117,12 @@ def __init__(self, config: settings.Settings | dict = None) -> None:
117117 yara_file_to_load_regex = None
118118
119119 self .namespace_to_rule_path : dict [str , str ] = list_rules (
120- self .cfg .yara_rules_path , blacklist , yara_file_to_load_regex
120+ self .cfg .yara_rules_path , # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
121+ blacklist ,
122+ yara_file_to_load_regex ,
121123 )
122124 if not self .namespace_to_rule_path :
123- raise Exception ("No yara rules found in %s path" % self .cfg .yara_rules_path )
125+ raise Exception ("No yara rules found in %s path" % self .cfg .yara_rules_path ) # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
124126
125127 self .logger .info (f"Loaded { len (self .namespace_to_rule_path )} files containing yara rules." )
126128 if len (self .namespace_to_rule_path ) < 20 :
@@ -144,13 +146,15 @@ def execute(self, job: Job):
144146 if "." in fname :
145147 ext = fname .rsplit ("." , 1 )[- 1 ]
146148
149+ if self ._cached_rules is None :
150+ raise TypeError ("Expected self._cached_rules to be Rules. got None" )
147151 scanner = yara_x .Scanner (self ._cached_rules )
148152 scanner .set_global ("filename" , fname )
149153 scanner .set_global ("filepath" , fpath )
150154 scanner .set_global ("extension" , ext )
151155 scanner .set_global ("filetype" , ftype )
152156 # if binary over certain size, write to disk first
153- if job .event .entity .size > self .cfg .size_before_disk :
157+ if job .event .entity .size > self .cfg .size_before_disk : # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
154158 matches = scanner .scan_file (job .get_data ().get_filepath ()) # type: yara_x.ScanResults
155159 else :
156160 matches = scanner .scan (job .get_data ().read ()) # type: yara_x.ScanResults
@@ -182,7 +186,7 @@ def execute(self, job: Job):
182186 if new_rule not in seen_rules_md5s :
183187 seen_rules_md5s .append (new_rule )
184188 # Add the original yara rule that hit as an augmented stream. Stop at max allowed Augmented streams.
185- if yara_rule_streams_added < self .cfg .max_yara_hit_streams_to_keep :
189+ if yara_rule_streams_added < self .cfg .max_yara_hit_streams_to_keep : # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
186190 raw_rule_with_header = (
187191 f"// plugin: { self .NAME } , namespace_identifier: { rule } \n " .encode () + raw_rule
188192 )
@@ -228,7 +232,10 @@ def execute(self, job: Job):
228232 self .add_feature_values ("yararule_match_name" , names )
229233 self .add_feature_values (
230234 "yararule_match" ,
231- [FV (val , label = rule , offset = offset , size = len (val )) for rule , offset , _ , val in match_tuples ],
235+ [
236+ FV (val , label = rule , offset = offset , size = len (val ) if val is not None else 0 )
237+ for rule , offset , _ , val in match_tuples
238+ ],
232239 )
233240
234241 if not all (found_raw_rule .values ()):
@@ -299,7 +306,7 @@ def fetch_original_rule(self, rule_path: str, rule_identifier: str, logger: logg
299306 if not start_of_rule :
300307 # Ensure recursive yara includes don't end in an infinite loop
301308 self .yara_include_depth += 1
302- if self .yara_include_depth >= self .cfg .max_yara_include_depth :
309+ if self .yara_include_depth >= self .cfg .max_yara_include_depth : # ty: ignore[unresolved-attribute] ty doesn't understand add_settings
303310 return b""
304311 # Search included yara files.
305312 for included_path in included_yara_rule_paths :
0 commit comments