Commit 96a8e874 by devttys0

Fixed plugin and scan bugs when -A and -B were used together.

parent 05a1633d
......@@ -92,6 +92,7 @@ def main():
show_grids = False
show_legend = True
entropy_scan = False
sig_scan_done = False
enable_plugins = True
exec_commands = True
show_invalid = False
......@@ -212,8 +213,6 @@ def main():
magic_flags |= binwalk.magic.MAGIC_CONTINUE
elif opt in ("-I", "--show-invalid"):
show_invalid = True
elif opt in ("-B", "--binwalk"):
requested_scans.append(binwalk.Binwalk.BINWALK)
elif opt in ("-K", "--block"):
block_size = binwalk.common.str2int(arg)
elif opt in ("-X", "--disable-plugin"):
......@@ -261,6 +260,11 @@ def main():
# Else, use the default rules file
else:
extract_from_config = True
elif opt in ("-B", "--binwalk"):
requested_scans.append(binwalk.Binwalk.BINWALK)
# Load user file first so its signatures take precedence
magic_files.append(config.paths['user'][config.BINWALK_MAGIC_FILE])
magic_files.append(config.paths['system'][config.BINWALK_MAGIC_FILE])
elif opt in ("-A", "--opcodes"):
requested_scans.append(binwalk.Binwalk.BINARCH)
# Load user file first so its signatures take precedence
......@@ -413,10 +417,10 @@ def main():
for scan_type in requested_scans:
if scan_type in [binwalk.Binwalk.BINWALK, binwalk.Binwalk.BINARCH, binwalk.Binwalk.BINCAST, binwalk.Binwalk.CUSTOM]:
if scan_type in [binwalk.Binwalk.BINWALK, binwalk.Binwalk.BINARCH, binwalk.Binwalk.BINCAST, binwalk.Binwalk.CUSTOM] and not sig_scan_done:
# There's no generic way for the binwalk class to know what
# scan type is being run, since these are all signature scans,
# scan type is being run, since all of these are signature scans,
# just with different magic files. Manually set the scan sub-type
# here to ensure that plugins can differentiate between the
# scans being performed.
......@@ -434,6 +438,7 @@ def main():
plugins_blacklist=plugin_blacklist)
bwalk.concatenate_results(results, r)
sig_scan_done = True
elif scan_type == binwalk.Binwalk.STRINGS:
......
......@@ -371,8 +371,8 @@ class Binwalk(object):
if self.load_plugins:
self.plugins._load_plugins()
# Load the default signatures if self.load_signatures has not already been invoked
if self.magic is None:
# Load the magic signatures. This must be done for every scan, as some signature scans
# may use a different list of magic signatures.
self.load_signatures()
while i < self.matryoshka:
......@@ -514,8 +514,8 @@ class Binwalk(object):
# Invoke any pre-scan plugins
plugret_start = self.plugins._pre_scan_callbacks(fd)
# Load the default signatures if self.load_signatures has not already been invoked
if self.magic is None:
# Load the magic signatures if they weren't already loaded.
if not self.magic:
self.load_signatures()
# Main loop, scan through all the data
......
......@@ -43,7 +43,6 @@ class MagicParser:
@filter - Instance of the MagicFilter class. May be None if the parse/parse_file methods are not used.
@smart - Instance of the SmartSignature class. May be None if the parse/parse_file methods are not used.
Returns None.
'''
self.matches = set([])
......@@ -52,7 +51,6 @@ class MagicParser:
self.smart = smart
self.raw_fd = None
self.signature_count = 0
self.fd = tempfile.NamedTemporaryFile()
def __del__(self):
try:
......@@ -111,6 +109,11 @@ class MagicParser:
Returns the name of the generated temporary magic file, which will be automatically
deleted when the class deconstructor is called.
'''
self.matches = set([])
self.signatures = {}
self.signature_count = 0
self.fd = tempfile.NamedTemporaryFile()
if isinstance(file_name, type([])):
files = file_name
else:
......
......@@ -15,11 +15,14 @@ class Plugin:
self.fd = None
self.comp = None
self.binwalk = binwalk
compressd_magic_file = binwalk.config.find_magic_file("compressd")
if binwalk.scan_type == binwalk.BINWALK:
self.comp = ctypes.cdll.LoadLibrary(ctypes.util.find_library("compress42"))
if self.comp:
binwalk.magic_files.append(binwalk.config.find_magic_file('compressd'))
binwalk.magic_files.append(compressd_magic_file)
elif compressd_magic_file in binwalk.magic_files:
binwalk.magic_files.pop(binwalk.magic_files.index(compressd_magic_file))
def __del__(self):
try:
......
......@@ -14,13 +14,18 @@ class Plugin:
def __init__(self, binwalk):
self.fd = None
self.tinfl = None
zlib_magic_file = binwalk.config.find_magic_file('zlib')
# Only initialize this plugin if this is a normal binwalk signature scan
if binwalk.scan_type == binwalk.BINWALK:
# Load libtinfl.so
self.tinfl = ctypes.cdll.LoadLibrary(ctypes.util.find_library('tinfl'))
if self.tinfl:
# Add the zlib file to the list of magic files
binwalk.magic_files.append(binwalk.config.find_magic_file('zlib'))
binwalk.magic_files.append(zlib_magic_file)
# Else, be sure to unload the zlib file from the list of magic signatures
elif zlib_magic_file in binwalk.magic_files:
binwalk.magic_files.pop(binwalk.magic_files.index(zlib_magic_file))
def pre_scan(self, fd):
if self.tinfl:
......
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