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