mirror of
https://github.com/volatilityfoundation/volatility3.git
synced 2026-09-24 02:24:51 +02:00
Rework how we handle import errors.
This commit is contained in:
@@ -108,10 +108,16 @@ class CommandLine(interfaces.plugins.FileConsumerInterface):
|
||||
file_logger.setFormatter(file_formatter)
|
||||
vollog.addHandler(file_logger)
|
||||
vollog.info("Logging started")
|
||||
if partial_args.verbosity < 3:
|
||||
console.setLevel(30 - (partial_args.verbosity * 10))
|
||||
else:
|
||||
console.setLevel(10 - (partial_args.verbosity - 2))
|
||||
|
||||
# Do the initialization
|
||||
ctx = contexts.Context() # Construct a blank context
|
||||
framework.import_files(volatility.plugins) # Will not log as console's default level is WARNING
|
||||
failures = framework.import_files(volatility.plugins,
|
||||
True) # Will not log as console's default level is WARNING
|
||||
vollog.info("Plugins could not be loaded: " + ", ".join(failures))
|
||||
automagics = automagic.available(ctx)
|
||||
|
||||
plugin_list = framework.list_plugins()
|
||||
@@ -141,10 +147,6 @@ class CommandLine(interfaces.plugins.FileConsumerInterface):
|
||||
args = parser.parse_args()
|
||||
if args.plugin is None:
|
||||
parser.error("Please select a plugin to run")
|
||||
if args.verbosity < 3:
|
||||
console.setLevel(30 - (args.verbosity * 10))
|
||||
else:
|
||||
console.setLevel(10 - (args.verbosity - 2))
|
||||
|
||||
vollog.log(constants.LOGLEVEL_VVV, "Cache directory used: {}".format(constants.CACHE_PATH))
|
||||
|
||||
|
||||
@@ -82,8 +82,9 @@ def class_subclasses(cls: T) -> typing.Generator[T, None, None]:
|
||||
yield return_value
|
||||
|
||||
|
||||
def import_files(base_module) -> None:
|
||||
def import_files(base_module, ignore_errors = False) -> typing.List[str]:
|
||||
"""Imports all plugins present under plugins path"""
|
||||
failures = []
|
||||
if not isinstance(base_module.__path__, list):
|
||||
raise TypeError("[base_module].__path__ must be a list of paths")
|
||||
for path in base_module.__path__:
|
||||
@@ -101,11 +102,13 @@ def import_files(base_module) -> None:
|
||||
__import__(base_module.__name__ + "." + module)
|
||||
except ImportError as e:
|
||||
vollog.debug(str(e))
|
||||
vollog.warning("Failed to import module {} based on file: {}".format(module, modpath))
|
||||
raise
|
||||
vollog.debug("Failed to import module {} based on file: {}".format(module, modpath))
|
||||
failures.append(base_module.__name__ + "." + module)
|
||||
if not ignore_errors:
|
||||
raise
|
||||
else:
|
||||
vollog.info("Skipping existing module: {}".format(module))
|
||||
return None
|
||||
return failures
|
||||
|
||||
|
||||
def list_plugins() -> typing.Dict[str, typing.Type[interfaces.plugins.PluginInterface]]:
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import logging
|
||||
import typing
|
||||
|
||||
try:
|
||||
import yara
|
||||
|
||||
has_yara = True
|
||||
except ImportError:
|
||||
has_yara = False
|
||||
|
||||
from volatility.framework import interfaces, layers, renderers
|
||||
from volatility.framework.renderers import format_hints
|
||||
from volatility.framework.symbols.windows import extensions
|
||||
@@ -16,6 +9,11 @@ from volatility.plugins.windows import pslist
|
||||
|
||||
vollog = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
import yara
|
||||
except ImportError:
|
||||
vollog.info("Python Yara module not found, plugin (and dependent plugins) not available")
|
||||
|
||||
|
||||
class VadYaraScan(interfaces.plugins.PluginInterface):
|
||||
|
||||
@@ -79,8 +77,5 @@ class VadYaraScan(interfaces.plugins.PluginInterface):
|
||||
return scan_iterator
|
||||
|
||||
def run(self):
|
||||
if not has_yara:
|
||||
vollog.error("Please install Yara from https://plusvic.github.io/yara/")
|
||||
|
||||
return renderers.TreeGrid([('Offset', format_hints.Hex),
|
||||
('Rule', str)], self._generator())
|
||||
|
||||
@@ -6,14 +6,13 @@ from volatility.framework.configuration import requirements
|
||||
from volatility.framework.interfaces import plugins
|
||||
from volatility.framework.renderers import format_hints
|
||||
|
||||
vollog = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
import yara
|
||||
|
||||
has_yara = True
|
||||
except:
|
||||
has_yara = False
|
||||
|
||||
vollog = logging.getLogger(__name__)
|
||||
except ImportError:
|
||||
vollog.info("Python Yara module not found, plugin (and dependent plugins) not available")
|
||||
raise
|
||||
|
||||
|
||||
class YaraScanner(interfaces.layers.ScannerInterface):
|
||||
@@ -87,8 +86,5 @@ class YaraScan(plugins.PluginInterface):
|
||||
yield format_hints.Hex(offset), name
|
||||
|
||||
def run(self):
|
||||
if not has_yara:
|
||||
vollog.error("Please install Yara from https://plusvic.github.io/yara/")
|
||||
|
||||
return renderers.TreeGrid([('Offset', format_hints.Hex),
|
||||
('Rule', str)], self._generator())
|
||||
|
||||
Reference in New Issue
Block a user