From 10376a2687b8546df88bf6b3a3e11126f8816479 Mon Sep 17 00:00:00 2001 From: Davide Arcuri Date: Wed, 7 Aug 2024 10:38:29 +0200 Subject: [PATCH] use one class with flag --- README.md | 2 +- pyproject.toml | 2 +- volatility3/framework/__init__.py | 2 +- .../framework/plugins/windows/vadyarascan.py | 7 +- volatility3/framework/plugins/yarascan.py | 81 ++++++++----------- 5 files changed, 38 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 886790df8..1463c2bde 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ more details. ## Requirements -Volatility 3 requires Python 3.7.3 or later. To install the most minimal set of dependencies (some plugins will not work) use a command such as: +Volatility 3 requires Python 3.8.0 or later. To install the most minimal set of dependencies (some plugins will not work) use a command such as: ```shell pip3 install -r requirements-minimal.txt diff --git a/pyproject.toml b/pyproject.toml index 207f762cc..2e1636a43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ readme = "README.md" authors = [ { name = "Volatility Foundation", email = "volatility@volatilityfoundation.org" }, ] -requires-python = ">=3.7.3" +requires-python = ">=3.8.0" license = { text = "VSL" } dynamic = ["dependencies", "optional-dependencies", "version"] diff --git a/volatility3/framework/__init__.py b/volatility3/framework/__init__.py index 74db773cf..51310bfa2 100644 --- a/volatility3/framework/__init__.py +++ b/volatility3/framework/__init__.py @@ -7,7 +7,7 @@ import glob import sys import zipfile -required_python_version = (3, 7, 3) +required_python_version = (3, 8, 0) if ( sys.version_info.major != required_python_version[0] or sys.version_info.minor < required_python_version[1] diff --git a/volatility3/framework/plugins/windows/vadyarascan.py b/volatility3/framework/plugins/windows/vadyarascan.py index 4a84a1285..7bc3377c3 100644 --- a/volatility3/framework/plugins/windows/vadyarascan.py +++ b/volatility3/framework/plugins/windows/vadyarascan.py @@ -73,8 +73,9 @@ class VadYaraScan(interfaces.plugins.PluginInterface): ) continue + data = layer.read(start, size, True) if not yarascan.YaraScan._yara_x: - for match in rules.match(data=layer.read(start, size, True)): + for match in rules.match(data=data): if yarascan.YaraScan.yara_returns_instances(): for match_string in match.strings: for instance in match_string.instances: @@ -95,9 +96,7 @@ class VadYaraScan(interfaces.plugins.PluginInterface): value, ) else: - data = layer.read(start, size, True) - results = rules.scan(data) - for match in results.matching_rules: + for match in rules.scan(data).matching_rules: for match_string in match.patterns: for instance in match_string.matches: yield 0, ( diff --git a/volatility3/framework/plugins/yarascan.py b/volatility3/framework/plugins/yarascan.py index 6a4dd9251..310bbd072 100644 --- a/volatility3/framework/plugins/yarascan.py +++ b/volatility3/framework/plugins/yarascan.py @@ -36,7 +36,7 @@ except ImportError: raise -class BaseYaraScanner(interfaces.layers.ScannerInterface): +class YaraScanner(interfaces.layers.ScannerInterface): _version = (2, 1, 0) # yara.Rules isn't exposed, so we can't type this properly @@ -45,32 +45,44 @@ class BaseYaraScanner(interfaces.layers.ScannerInterface): if rules is None: raise ValueError("No rules provided to YaraScanner") self._rules = rules - - -class YaraPythonScanner(BaseYaraScanner): - def __init__(self, rules) -> None: - super().__init__(rules) - self.st_object = not tuple(int(x) for x in yara.__version__.split(".")) < (4, 3) + self.st_object = ( + None + if USE_YARA_X + else not tuple(int(x) for x in yara.__version__.split(".")) < (4, 3) + ) def __call__( self, data: bytes, data_offset: int ) -> Iterable[Tuple[int, str, str, bytes]]: - for match in self._rules.match(data=data): - if YaraScan.yara_returns_instances(): - for match_string in match.strings: - for instance in match_string.instances: + if USE_YARA_X: + for match in self._rules.scan(data).matching_rules: + for match_string in match.patterns: + for instance in match_string.matches: yield ( instance.offset + data_offset, - match.rule, + f"{match.namespace}.{match.identifier}", match_string.identifier, - instance.matched_data, + data[instance.offset : instance.offset + instance.length], ) - else: - for offset, name, value in match.strings: - yield (offset + data_offset, match.rule, name, value) + else: + for match in self._rules.match(data=data): + if YaraScan.yara_returns_instances(): + for match_string in match.strings: + for instance in match_string.instances: + yield ( + instance.offset + data_offset, + match.rule, + match_string.identifier, + instance.matched_data, + ) + else: + for offset, name, value in match.strings: + yield (offset + data_offset, match.rule, name, value) @staticmethod def get_rule(rule): + if USE_YARA_X: + return yara_x.compile(f"rule r1 {{strings: $a = {rule} condition: $a}}") return yara.compile( sources={"n": f"rule r1 {{strings: $a = {rule} condition: $a}}"} ) @@ -78,47 +90,18 @@ class YaraPythonScanner(BaseYaraScanner): @staticmethod def from_compiled_file(filepath): with resources.ResourceAccessor().open(filepath, "rb") as fp: + if USE_YARA_X: + return yara_x.Rules.deserialize_from(file=fp) return yara.load(file=fp) @staticmethod def from_file(filepath): with resources.ResourceAccessor().open(filepath, "rb") as fp: + if USE_YARA_X: + return yara_x.compile(fp.read().decode()) return yara.compile(file=fp) -class YaraXScanner(BaseYaraScanner): - def __call__( - self, data: bytes, data_offset: int - ) -> Iterable[Tuple[int, str, str, bytes]]: - results = self._rules.scan(data) - for match in results.matching_rules: - for match_string in match.patterns: - for instance in match_string.matches: - yield ( - instance.offset + data_offset, - f"{match.namespace}.{match.identifier}", - match_string.identifier, - data[instance.offset : instance.offset + instance.length], - ) - - @staticmethod - def get_rule(rule): - return yara_x.compile(f"rule r1 {{strings: $a = {rule} condition: $a}}") - - @staticmethod - def from_compiled_file(filepath): - with resources.ResourceAccessor().open(filepath, "rb") as fp: - return yara_x.Rules.deserialize_from(file=fp) - - @staticmethod - def from_file(filepath): - with resources.ResourceAccessor().open(filepath, "rb") as fp: - return yara_x.compile(fp.read().decode()) - - -YaraScanner = YaraXScanner if USE_YARA_X else YaraPythonScanner - - class YaraScan(plugins.PluginInterface): """Scans kernel memory using yara rules (string or file)."""