Commit c297fbc3 by Craig Heffner

Added load_file method support for plugins

parent 1e0491ee
__all__ = ['scan', 'execute', 'ModuleException'] __all__ = ['scan', 'execute', 'ModuleException']
from binwalk.core.module import Modules, ModuleException from binwalk.core.module import Modules
from binwalk.core.exceptions import ModuleException
# Convenience functions # Convenience functions
def scan(*args, **kwargs): def scan(*args, **kwargs):
......
...@@ -9,12 +9,7 @@ import struct ...@@ -9,12 +9,7 @@ import struct
import datetime import datetime
import binwalk.core.common import binwalk.core.common
import binwalk.core.compat import binwalk.core.compat
from binwalk.core.exceptions import ParserException
class ParserException(Exception):
'''
Exception thrown specifically for signature file parsing errors.
'''
pass
class SignatureResult(binwalk.core.module.Result): class SignatureResult(binwalk.core.module.Result):
''' '''
......
...@@ -17,6 +17,7 @@ import binwalk.core.settings ...@@ -17,6 +17,7 @@ import binwalk.core.settings
import binwalk.core.plugin import binwalk.core.plugin
from threading import Thread from threading import Thread
from binwalk.core.compat import * from binwalk.core.compat import *
from binwalk.core.exceptions import *
class Option(object): class Option(object):
''' '''
...@@ -319,6 +320,13 @@ class Module(object): ...@@ -319,6 +320,13 @@ class Module(object):
def _plugins_pre_scan(self): def _plugins_pre_scan(self):
self.plugins.pre_scan_callbacks(self) self.plugins.pre_scan_callbacks(self)
def _plugins_load_file(self, fp):
try:
self.plugins.load_file_callbacks(fp)
return True
except IgnoreFileException:
return False
def _plugins_new_file(self, fp): def _plugins_new_file(self, fp):
self.plugins.new_file_callbacks(fp) self.plugins.new_file_callbacks(fp)
...@@ -398,7 +406,8 @@ class Module(object): ...@@ -398,7 +406,8 @@ class Module(object):
if not fp: if not fp:
break break
else: else:
if self.config.file_name_filter(fp) == False: if (self.config.file_name_filter(fp) == False or
self._plugins_load_file(fp) == False):
fp.close() fp.close()
fp = None fp = None
continue continue
...@@ -588,13 +597,6 @@ class Status(object): ...@@ -588,13 +597,6 @@ class Status(object):
for (k,v) in iterator(self.kwargs): for (k,v) in iterator(self.kwargs):
setattr(self, k, v) setattr(self, k, v)
class ModuleException(Exception):
'''
Module exception class.
Nothing special here except the name.
'''
pass
class Modules(object): class Modules(object):
''' '''
Main class used for running and managing modules. Main class used for running and managing modules.
......
...@@ -7,6 +7,7 @@ import inspect ...@@ -7,6 +7,7 @@ import inspect
import binwalk.core.common import binwalk.core.common
import binwalk.core.settings import binwalk.core.settings
from binwalk.core.compat import * from binwalk.core.compat import *
from binwalk.core.exceptions import IgnoreFileException
class Plugin(object): class Plugin(object):
''' '''
...@@ -80,6 +81,7 @@ class Plugins(object): ...@@ -80,6 +81,7 @@ class Plugins(object):
SCAN = 'scan' SCAN = 'scan'
NEWFILE = 'new_file' NEWFILE = 'new_file'
LOADFILE = 'load_file'
PRESCAN = 'pre_scan' PRESCAN = 'pre_scan'
POSTSCAN = 'post_scan' POSTSCAN = 'post_scan'
MODULE_EXTENSION = '.py' MODULE_EXTENSION = '.py'
...@@ -88,6 +90,7 @@ class Plugins(object): ...@@ -88,6 +90,7 @@ class Plugins(object):
self.scan = [] self.scan = []
self.pre_scan = [] self.pre_scan = []
self.new_file = [] self.new_file = []
self.load_file = []
self.post_scan = [] self.post_scan = []
self.parent = parent self.parent = parent
self.settings = binwalk.core.settings.Settings() self.settings = binwalk.core.settings.Settings()
...@@ -108,6 +111,8 @@ class Plugins(object): ...@@ -108,6 +111,8 @@ class Plugins(object):
callback(obj) callback(obj)
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
raise e raise e
except IgnoreFileException as e:
raise e
except Exception as e: except Exception as e:
binwalk.core.common.warning("%s.%s failed: %s" % (callback.__module__, callback.__name__, e)) binwalk.core.common.warning("%s.%s failed: %s" % (callback.__module__, callback.__name__, e))
...@@ -215,6 +220,13 @@ class Plugins(object): ...@@ -215,6 +220,13 @@ class Plugins(object):
pass pass
try: try:
self.load_file.append(getattr(class_instance, self.LOADFILE))
except KeyboardInterrupt as e:
raise e
except Exception as e:
pass
try:
self.pre_scan.append(getattr(class_instance, self.PRESCAN)) self.pre_scan.append(getattr(class_instance, self.PRESCAN))
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
raise e raise e
...@@ -243,6 +255,9 @@ class Plugins(object): ...@@ -243,6 +255,9 @@ class Plugins(object):
def pre_scan_callbacks(self, obj): def pre_scan_callbacks(self, obj):
return self._call_plugins(self.pre_scan) return self._call_plugins(self.pre_scan)
def load_file_callbacks(self, fp):
return self._call_plugins(self.load_file, fp)
def new_file_callbacks(self, fp): def new_file_callbacks(self, fp):
return self._call_plugins(self.new_file, fp) return self._call_plugins(self.new_file, fp)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment