Commit 55075e57 by devttys0

Fixed handling of module errors and invalid integer options

parent bab7c0c3
...@@ -74,9 +74,8 @@ Most distros don't have pyqtgraph in their default repositories, so it's best to ...@@ -74,9 +74,8 @@ Most distros don't have pyqtgraph in their default repositories, so it's best to
MANUALLY INSTALLING EXTRACTION UTILITIES MANUALLY INSTALLING EXTRACTION UTILITIES
------------------------------------------- -------------------------------------------
Binwalk can automatically invoke external extraction utilities to extract various types of files that it Binwalk can automatically invoke external extraction utilities to extract various types of files that it may find during
may find during a scan. These utilities are optional, but recommended if you plan on using binwalk's a scan. These utilities are optional, but recommended if you plan on using binwalk's extraction features.
extraction features.
Most utilities can be installed from your distro's repositories (package names may vary slightly based Most utilities can be installed from your distro's repositories (package names may vary slightly based
on your particular distro): on your particular distro):
......
# Common functions. # Common functions used throughout various parts of binwalk code.
import io import io
import os import os
import re import re
......
...@@ -5,7 +5,10 @@ import binwalk.core.common ...@@ -5,7 +5,10 @@ import binwalk.core.common
from binwalk.core.compat import * from binwalk.core.compat import *
class Display(object): class Display(object):
'''
Class to handle display of output and writing to log files.
This class is instantiated for all modules implicitly and should not need to be invoked directly by most modules.
'''
SCREEN_WIDTH = 0 SCREEN_WIDTH = 0
HEADER_WIDTH = 150 HEADER_WIDTH = 150
DEFAULT_FORMAT = "%s\n" DEFAULT_FORMAT = "%s\n"
......
...@@ -149,7 +149,7 @@ class Module(object): ...@@ -149,7 +149,7 @@ class Module(object):
attribute='extractor'), attribute='extractor'),
] ]
# A list of dependencies that can be filled in as needed by each individual module. # A list of binwalk.core.module.Dependency instances that can be filled in as needed by each individual module.
DEPENDS = [] DEPENDS = []
# Format string for printing the header during a scan. # Format string for printing the header during a scan.
...@@ -407,6 +407,11 @@ class Module(object): ...@@ -407,6 +407,11 @@ class Module(object):
sys.stderr.write("\n" + e.module + " Error: " + e.description + "\n\n") sys.stderr.write("\n" + e.module + " Error: " + e.description + "\n\n")
def header(self): def header(self):
'''
Displays the scan header, as defined by self.HEADER and self.HEADER_FORMAT.
Returns None.
'''
self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT) self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)
self.config.display.add_custom_header(self.VERBOSE_FORMAT, self.VERBOSE) self.config.display.add_custom_header(self.VERBOSE_FORMAT, self.VERBOSE)
...@@ -416,6 +421,11 @@ class Module(object): ...@@ -416,6 +421,11 @@ class Module(object):
self.config.display.header(self.HEADER, file_name=self.current_target_file_name) self.config.display.header(self.HEADER, file_name=self.current_target_file_name)
def footer(self): def footer(self):
'''
Displays the scan footer.
Returns None.
'''
self.config.display.footer() self.config.display.footer()
def main(self, parent): def main(self, parent):
...@@ -720,20 +730,25 @@ class Modules(object): ...@@ -720,20 +730,25 @@ class Modules(object):
last_priority[name] = module_option.priority last_priority[name] = module_option.priority
# Do this manually as argparse doesn't seem to be able to handle hexadecimal values # Do this manually as argparse doesn't seem to be able to handle hexadecimal values
if module_option.type == int: try:
kwargs[name] = int(value, 0) if module_option.type == int:
elif module_option.type == float: kwargs[name] = int(value, 0)
kwargs[name] = float(value) elif module_option.type == float:
elif module_option.type == dict: kwargs[name] = float(value)
if not has_key(kwargs, name): elif module_option.type == dict:
kwargs[name] = {} if not has_key(kwargs, name):
kwargs[name][len(kwargs[name])] = value kwargs[name] = {}
elif module_option.type == list: kwargs[name][len(kwargs[name])] = value
if not has_key(kwargs, name): elif module_option.type == list:
kwargs[name] = [] if not has_key(kwargs, name):
kwargs[name].append(value) kwargs[name] = []
else: kwargs[name].append(value)
kwargs[name] = value else:
kwargs[name] = value
except KeyboardInterrupt as e:
raise e
except Exception as e:
raise ModuleException("Invalid usage: %s" % str(e))
return kwargs return kwargs
......
...@@ -6,47 +6,48 @@ from threading import Thread ...@@ -6,47 +6,48 @@ from threading import Thread
from binwalk.core.compat import user_input from binwalk.core.compat import user_input
def display_status(m): def display_status(m):
# Display the current scan progress when the enter key is pressed. # Display the current scan progress when the enter key is pressed.
while True: while True:
try: try:
user_input() user_input()
sys.stderr.write("Progress: %.2f%% (%d / %d)\n\n" % (((float(m.status.completed) / float(m.status.total)) * 100), m.status.completed, m.status.total)) sys.stderr.write("Progress: %.2f%% (%d / %d)\n\n" % (((float(m.status.completed) / float(m.status.total)) * 100), m.status.completed, m.status.total))
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
raise e raise e
except Exception: except Exception:
pass pass
def usage(modules): def usage(modules):
sys.stderr.write(modules.help()) sys.stderr.write(modules.help())
sys.exit(1) sys.exit(1)
def main(): def main():
modules = binwalk.Modules() modules = binwalk.Modules()
# Start the display_status function as a daemon thread. # Start the display_status function as a daemon thread.
t = Thread(target=display_status, args=(modules,)) t = Thread(target=display_status, args=(modules,))
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()
try: try:
if len(sys.argv) == 1: if len(sys.argv) == 1:
usage(modules) usage(modules)
elif not modules.execute(): elif not modules.execute():
modules.execute(*sys.argv[1:], signature=True) modules.execute(*sys.argv[1:], signature=True)
except binwalk.ModuleException as e: except binwalk.ModuleException as e:
sys.exit(1) sys.stderr.write(str(e) + '\n')
sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':
try: try:
# Special options for profiling the code. For debug use only. # Special options for profiling the code. For debug use only.
if '--profile' in sys.argv: if '--profile' in sys.argv:
import cProfile import cProfile
sys.argv.pop(sys.argv.index('--profile')) sys.argv.pop(sys.argv.index('--profile'))
cProfile.run('main()') cProfile.run('main()')
else: else:
main() main()
except IOError: except IOError:
pass pass
except KeyboardInterrupt: except KeyboardInterrupt:
sys.stdout.write("\n") sys.stdout.write("\n")
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