Commit 556feb56 by devttys0

Added new_file plugin callback method

parent 46f87000
...@@ -307,6 +307,9 @@ class Module(object): ...@@ -307,6 +307,9 @@ 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_new_file(self, fp):
self.plugins.new_file_callbacks(fp)
def _plugins_post_scan(self): def _plugins_post_scan(self):
self.plugins.post_scan_callbacks(self) self.plugins.post_scan_callbacks(self)
...@@ -387,6 +390,9 @@ class Module(object): ...@@ -387,6 +390,9 @@ class Module(object):
self.current_target_file_name = None self.current_target_file_name = None
self.previous_next_file_fp = fp self.previous_next_file_fp = fp
self._plugins_new_file(fp)
return fp return fp
def clear(self, results=True, errors=True): def clear(self, results=True, errors=True):
......
...@@ -45,6 +45,12 @@ class Plugin(object): ...@@ -45,6 +45,12 @@ class Plugin(object):
''' '''
pass pass
def new_file(self, fp):
'''
Child class should override this if needed.
'''
pass
def scan(self, result): def scan(self, result):
''' '''
Child class should override this if needed. Child class should override this if needed.
...@@ -67,36 +73,13 @@ class Plugins(object): ...@@ -67,36 +73,13 @@ class Plugins(object):
The Plugin class constructor is called once prior to scanning a file or set of files. The Plugin class constructor is called once prior to scanning a file or set of files.
The Plugin class destructor (__del__) is called once after scanning all files. The Plugin class destructor (__del__) is called once after scanning all files.
The Plugin class can define one or all of the following callback methods:
o pre_scan(self, fd)
This method is called prior to running a scan against a file. It is passed the file object of
the file about to be scanned.
o pre_parser(self, result)
This method is called every time any result - valid or invalid - is found in the file being scanned.
It is passed a dictionary with one key ('description'), which contains the raw string returned by libmagic.
The contents of this dictionary key may be modified as necessary by the plugin.
o callback(self, results)
This method is called every time a valid result is found in the file being scanned. It is passed a
dictionary of results. This dictionary is identical to that passed to Binwalk.single_scan's callback
function, and its contents may be modified as necessary by the plugin.
o post_scan(self, fd)
This method is called after running a scan against a file, but before the file has been closed.
It is passed the file object of the scanned file.
Values returned by pre_scan affect all results during the scan of that particular file.
Values returned by callback affect only that specific scan result.
Values returned by post_scan are ignored since the scan of that file has already been completed.
By default, all plugins are loaded during binwalk signature scans. Plugins that wish to be disabled by By default, all plugins are loaded during binwalk signature scans. Plugins that wish to be disabled by
default may create a class variable named 'ENABLED' and set it to False. If ENABLED is set to False, the default may create a class variable named 'ENABLED' and set it to False. If ENABLED is set to False, the
plugin will only be loaded if it is explicitly named in the plugins whitelist. plugin will only be loaded if it is explicitly named in the plugins whitelist.
''' '''
SCAN = 'scan' SCAN = 'scan'
NEWFILE = 'new_file'
PRESCAN = 'pre_scan' PRESCAN = 'pre_scan'
POSTSCAN = 'post_scan' POSTSCAN = 'post_scan'
MODULE_EXTENSION = '.py' MODULE_EXTENSION = '.py'
...@@ -104,6 +87,7 @@ class Plugins(object): ...@@ -104,6 +87,7 @@ class Plugins(object):
def __init__(self, parent=None): def __init__(self, parent=None):
self.scan = [] self.scan = []
self.pre_scan = [] self.pre_scan = []
self.new_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()
...@@ -117,10 +101,11 @@ class Plugins(object): ...@@ -117,10 +101,11 @@ class Plugins(object):
def _call_plugins(self, callback_list, arg): def _call_plugins(self, callback_list, arg):
for callback in callback_list: for callback in callback_list:
try: try:
if arg: try:
callback(arg)
else:
callback() callback()
except TypeError:
if arg is not None:
callback(arg)
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
raise e raise e
except Exception as e: except Exception as e:
...@@ -243,6 +228,13 @@ class Plugins(object): ...@@ -243,6 +228,13 @@ class Plugins(object):
except Exception as e: except Exception as e:
pass pass
try:
self.new_file.append(getattr(class_instance, self.NEWFILE))
except KeyboardInterrupt as e:
raise e
except Exception as e:
pass
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
raise e raise e
except Exception as e: except Exception as e:
...@@ -251,6 +243,9 @@ class Plugins(object): ...@@ -251,6 +243,9 @@ class Plugins(object):
def pre_scan_callbacks(self, obj): def pre_scan_callbacks(self, obj):
return self._call_plugins(self.pre_scan, None) return self._call_plugins(self.pre_scan, None)
def new_file_callbacks(self, fp):
return self._call_plugins(self.new_file, fp)
def post_scan_callbacks(self, obj): def post_scan_callbacks(self, obj):
return self._call_plugins(self.post_scan, None) return self._call_plugins(self.post_scan, None)
......
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