Commit 464cb3b0 by devttys0

Added binwalk_simple example

parent 39c081fb
...@@ -14,7 +14,7 @@ class Option(object): ...@@ -14,7 +14,7 @@ class Option(object):
A container class that allows modules to declare command line options. A container class that allows modules to declare command line options.
''' '''
def __init__(self, kwargs={}, priority=0, description="", short="", long="", type=None, dtype=""): def __init__(self, kwargs={}, priority=0, description="", short="", long="", type=None, dtype=None):
''' '''
Class constructor. Class constructor.
...@@ -34,9 +34,9 @@ class Option(object): ...@@ -34,9 +34,9 @@ class Option(object):
self.short = short self.short = short
self.long = long self.long = long
self.type = type self.type = type
self.dtype = str(dtype) self.dtype = dtype
if not self.dtype: if not self.dtype and self.type:
if self.type in [io.FileIO, argparse.FileType, binwalk.core.common.BlockFile]: if self.type in [io.FileIO, argparse.FileType, binwalk.core.common.BlockFile]:
self.dtype = 'file' self.dtype = 'file'
elif self.type in [int, float, str]: elif self.type in [int, float, str]:
...@@ -155,6 +155,9 @@ class Module(object): ...@@ -155,6 +155,9 @@ class Module(object):
# Modules with higher priorities are executed first # Modules with higher priorities are executed first
PRIORITY = 5 PRIORITY = 5
# Modules with a higher order are displayed first in help output
ORDER = 5
def __init__(self, dependency=False, **kwargs): def __init__(self, dependency=False, **kwargs):
self.errors = [] self.errors = []
self.results = [] self.results = []
...@@ -469,28 +472,34 @@ class Modules(object): ...@@ -469,28 +472,34 @@ class Modules(object):
return sorted(modules, key=modules.get, reverse=True) return sorted(modules, key=modules.get, reverse=True)
def help(self): def help(self):
modules = {}
help_string = "\nBinwalk v%s\nCraig Heffner, http://www.binwalk.org\n" % binwalk.core.settings.Settings.VERSION help_string = "\nBinwalk v%s\nCraig Heffner, http://www.binwalk.org\n" % binwalk.core.settings.Settings.VERSION
for obj in self.list(attribute="CLI"): # Build a dictionary of modules and their ORDER attributes.
if obj.CLI: # This makes it easy to sort modules by their ORDER attribute for display.
help_string += "\n%s Options:\n" % obj.TITLE for module in self.list(attribute="CLI"):
if module.CLI:
modules[module] = module.ORDER
for module_option in obj.CLI: for module in sorted(modules, key=modules.get, reverse=True):
if module_option.long: help_string += "\n%s Options:\n" % module.TITLE
long_opt = '--' + module_option.long
for module_option in module.CLI:
if module_option.long:
long_opt = '--' + module_option.long
if module_option.type is not None: if module_option.dtype:
optargs = "=<%s>" % module_option.dtype optargs = "=<%s>" % module_option.dtype
else: else:
optargs = "" optargs = ""
if module_option.short: if module_option.short:
short_opt = "-" + module_option.short + "," short_opt = "-" + module_option.short + ","
else: else:
short_opt = " " short_opt = " "
fmt = " %%s %%s%%-%ds%%s\n" % (32-len(long_opt)) fmt = " %%s %%s%%-%ds%%s\n" % (32-len(long_opt))
help_string += fmt % (short_opt, long_opt, optargs, module_option.description) help_string += fmt % (short_opt, long_opt, optargs, module_option.description)
return help_string + "\n" return help_string + "\n"
...@@ -553,7 +562,8 @@ class Modules(object): ...@@ -553,7 +562,8 @@ class Modules(object):
if not has_key(self.loaded_modules, dependency): if not has_key(self.loaded_modules, dependency):
# self.run will automatically add the dependency class instance to self.loaded_modules # self.run will automatically add the dependency class instance to self.loaded_modules
self.run(dependency) self.run(dependency)
# If a dependency failed, consider this a non-recoverable error and raise an exception
if self.loaded_modules[dependency].errors: if self.loaded_modules[dependency].errors:
raise ModuleException("Failed to load " + str(dependency)) raise ModuleException("Failed to load " + str(dependency))
else: else:
......
...@@ -11,6 +11,7 @@ from binwalk.core.module import Module, Option, Kwarg, show_help ...@@ -11,6 +11,7 @@ from binwalk.core.module import Module, Option, Kwarg, show_help
class Configuration(Module): class Configuration(Module):
TITLE = "General" TITLE = "General"
ORDER = 0
DEPENDS = {} DEPENDS = {}
......
...@@ -18,7 +18,8 @@ class Entropy(Module): ...@@ -18,7 +18,8 @@ class Entropy(Module):
COLORS = ['r', 'g', 'c', 'b', 'm'] COLORS = ['r', 'g', 'c', 'b', 'm']
TITLE = "Entropy" TITLE = "Entropy"
ORDER = 8
CLI = [ CLI = [
Option(short='E', Option(short='E',
long='entropy', long='entropy',
......
...@@ -26,6 +26,7 @@ class Extractor(Module): ...@@ -26,6 +26,7 @@ class Extractor(Module):
MAX_READ_SIZE = 10 * 1024 * 1024 MAX_READ_SIZE = 10 * 1024 * 1024
TITLE = 'Extraction' TITLE = 'Extraction'
ORDER = 9
CLI = [ CLI = [
Option(short='e', Option(short='e',
......
...@@ -7,6 +7,7 @@ from binwalk.core.module import Module, Option, Kwarg ...@@ -7,6 +7,7 @@ from binwalk.core.module import Module, Option, Kwarg
class Signature(Module): class Signature(Module):
TITLE = "Signature Scan" TITLE = "Signature Scan"
ORDER = 10
CLI = [ CLI = [
Option(short='B', Option(short='B',
......
#!/usr/bin/env python
import binwalk
binwalk.Modules().execute()
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