|
1 | 1 | from dataclasses import InitVar, dataclass, field |
2 | 2 | from functools import reduce |
3 | 3 | from pathlib import Path |
4 | | -from typing import Any, Callable, Dict, Iterable, List, Optional, Union, IO, TYPE_CHECKING |
| 4 | +from typing import Any, Callable, Dict, Iterable, List, Optional, Union, IO, TYPE_CHECKING, cast |
5 | 5 | from uuid import UUID |
6 | 6 |
|
7 | 7 | import yaml |
@@ -88,7 +88,9 @@ def resolve_rule_references(self) -> None: |
88 | 88 | rule.resolve_rule_references(self) |
89 | 89 |
|
90 | 90 | # Extract all filters from the rules |
91 | | - filters: List[SigmaFilter] = [rule for rule in self.rules if isinstance(rule, SigmaFilter)] |
| 91 | + filters: List[SigmaFilter] = [ |
| 92 | + cast(SigmaFilter, rule) for rule in self.rules if isinstance(rule, SigmaFilter) |
| 93 | + ] |
92 | 94 | self.rules = [rule for rule in self.rules if not isinstance(rule, SigmaFilter)] |
93 | 95 |
|
94 | 96 | # Apply filters on each rule and replace the rule with the filtered rule |
@@ -126,34 +128,33 @@ def from_dicts( |
126 | 128 | if isinstance( |
127 | 129 | rule, SigmaRule |
128 | 130 | ): # Included rules are already parsed, skip collection action processing |
129 | | - parsed_rule = rule |
130 | | - parsed_rules.append(parsed_rule) |
131 | | - parsed_rule.source = source |
| 131 | + parsed_rules.append(rule) |
| 132 | + rule.source = source |
132 | 133 | else: |
133 | 134 | action = rule.get("action") |
134 | 135 | if action is None: # no action defined |
135 | 136 | if "correlation" in rule: # correlation rule - no global rule merge |
136 | | - parsed_rule = SigmaCorrelationRule.from_dict( |
| 137 | + parsed_correlation_rule = SigmaCorrelationRule.from_dict( |
137 | 138 | rule, |
138 | 139 | collect_errors, |
139 | 140 | source, |
140 | 141 | ) |
141 | | - parsed_rules.append(parsed_rule) |
142 | | - errors.extend(parsed_rule.errors) # Propagate errors from rule |
| 142 | + parsed_rules.append(parsed_correlation_rule) |
| 143 | + errors.extend(parsed_correlation_rule.errors) # Propagate errors from rule |
143 | 144 | elif "filter" in rule: # correlation rule - no global rule merge |
144 | | - parsed_rule = SigmaFilter.from_dict( |
| 145 | + parsed_filter_rule = SigmaFilter.from_dict( |
145 | 146 | rule, |
146 | 147 | collect_errors, |
147 | 148 | source, |
148 | 149 | ) |
149 | | - parsed_rules.append(parsed_rule) |
150 | | - errors.extend(parsed_rule.errors) # Propagate errors from rule |
| 150 | + parsed_rules.append(parsed_filter_rule) |
| 151 | + errors.extend(parsed_filter_rule.errors) # Propagate errors from rule |
151 | 152 | else: # merge with global rule and parse as simple rule |
152 | | - parsed_rule = SigmaRule.from_dict( |
| 153 | + parsed_merged_rule = SigmaRule.from_dict( |
153 | 154 | deep_dict_update(rule, global_rule), collect_errors, source |
154 | 155 | ) |
155 | | - parsed_rules.append(parsed_rule) |
156 | | - errors.extend(parsed_rule.errors) # Propagate errors from rule |
| 156 | + parsed_rules.append(parsed_merged_rule) |
| 157 | + errors.extend(parsed_merged_rule.errors) # Propagate errors from rule |
157 | 158 | prev_rule = rule |
158 | 159 | elif action == "global": # set global rule template |
159 | 160 | del rule["action"] |
@@ -245,6 +246,7 @@ def load_ruleset( |
245 | 246 | :param recursion_pattern: Pattern used to recurse into directories, defaults to ``**/*.yml``. |
246 | 247 |
|
247 | 248 | :return: :class:`SigmaCollection` of all sigma rules contained in given paths. |
| 249 | +
|
248 | 250 | """ |
249 | 251 | if not isinstance(inputs, Iterable) or isinstance(inputs, str): |
250 | 252 | raise TypeError( |
|
0 commit comments