Commit bc88ac00 by devttys0

Fixed bug which caused multiple file data to be extracted to the same directory;…

Fixed bug which caused multiple file data to be extracted to the same directory; added binwalk.modules.extractor.output dictionary to provide paths to the base output directory for each file.
parent 94f6b9bb
...@@ -51,7 +51,7 @@ Accessing Scan Results ...@@ -51,7 +51,7 @@ Accessing Scan Results
`binwalk.scan` returns a list of objects. Each object corresponds to a module that was run. For example, if you specified `--signature` and `--entropy`, then both the Signature and Entropy modules would be executed and you would be returned a list of two objects. `binwalk.scan` returns a list of objects. Each object corresponds to a module that was run. For example, if you specified `--signature` and `--entropy`, then both the Signature and Entropy modules would be executed and you would be returned a list of two objects.
The two attributes of interest for each object are the `results` and `errors` objects. Each is a list of binwalk.core.module.Result and binwalk.core.module.Error objects respectively. Each Result or Error object may contain custom attributes set by each module, but are guaranteed to have at least the following attributes (though modules are not required to populate all attributes): The two attributes of greatest interest for each object are the `results` and `errors` objects. Each is a list of binwalk.core.module.Result and binwalk.core.module.Error objects respectively. Each Result or Error object may contain custom attributes set by each module, but are guaranteed to have at least the following attributes (though modules are not required to populate all attributes):
| Attribute | Description | | Attribute | Description |
|-------------|-------------| |-------------|-------------|
...@@ -81,6 +81,17 @@ for module in binwalk.scan('firmware1.bin', 'firmware2.bin', signature=True, qui ...@@ -81,6 +81,17 @@ for module in binwalk.scan('firmware1.bin', 'firmware2.bin', signature=True, qui
Note the above use of the `--quiet` option which prevents the binwalk module from printing its normal output to screen. Note the above use of the `--quiet` option which prevents the binwalk module from printing its normal output to screen.
Each module object will also have an additional `extractor` attribute, which is an instance of the `binwalk.modules.extractor` object used to extract files if `--extract` was specified. In particular, `binwalk.modules.extractor.output` is a dictionary containing the base extraction directory for each scanned file:
```python
for module in binwalk.scan('firmware1.bin', 'firmware2.bin', signature=True, quiet=True, extract=True):
print ("%s Results:" % module.name)
for result in module.results:
print ("\t%s 0x%.8X %s" % (result.file.name, result.offset, result.description))
for (file_path, output_dir) in module.extractor.output:
print ("%s data was extracted to: %s" % (file_path, output_dir))
```
Module Exceptions Module Exceptions
================= =================
......
...@@ -352,7 +352,12 @@ class Module(object): ...@@ -352,7 +352,12 @@ class Module(object):
# Add any pending extracted files to the target_files list and reset the extractor's pending file list # Add any pending extracted files to the target_files list and reset the extractor's pending file list
self.target_file_list += self.extractor.pending self.target_file_list += self.extractor.pending
self.extractor.pending = []
# Reset all dependencies prior to continuing with another file.
# This is particularly important for the extractor module, which must be reset
# in order to reset it's base output directory path for each file, and the
# list of pending files.
self.reset_dependencies()
while self.target_file_list: while self.target_file_list:
next_target_file = self.target_file_list.pop(0) next_target_file = self.target_file_list.pop(0)
...@@ -482,6 +487,12 @@ class Module(object): ...@@ -482,6 +487,12 @@ class Module(object):
''' '''
self.config.display.footer() self.config.display.footer()
def reset_dependencies(self):
# Reset all dependency modules
for dependency in self.dependencies:
if hasattr(self, dependency.attribute):
getattr(self, dependency.attribute).reset()
def main(self, parent): def main(self, parent):
''' '''
Responsible for calling self.init, initializing self.config.display, and calling self.run. Responsible for calling self.init, initializing self.config.display, and calling self.run.
...@@ -496,15 +507,12 @@ class Module(object): ...@@ -496,15 +507,12 @@ class Module(object):
if hasattr(self, "extractor") and self.extractor.config.verbose: if hasattr(self, "extractor") and self.extractor.config.verbose:
self.config.verbose = self.config.display.verbose = True self.config.verbose = self.config.display.verbose = True
# Reset all dependency modules
for dependency in self.dependencies:
if hasattr(self, dependency.attribute):
getattr(self, dependency.attribute).reset()
if not self.config.files: if not self.config.files:
binwalk.core.common.debug("No target files specified, module %s terminated" % self.name) binwalk.core.common.debug("No target files specified, module %s terminated" % self.name)
return False return False
self.reset_dependencies()
try: try:
self.init() self.init()
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
......
...@@ -90,8 +90,10 @@ class Extractor(Module): ...@@ -90,8 +90,10 @@ class Extractor(Module):
def load(self): def load(self):
# Holds a list of extraction rules loaded either from a file or when manually specified. # Holds a list of extraction rules loaded either from a file or when manually specified.
self.extract_rules = [] self.extract_rules = []
# The base extraction directory (to be determined at runtime) # The input file specific output directory path (to be determined at runtime)
self.directory = None self.directory = None
# Key value pairs of input file path and output extraction path
self.output = {}
if self.load_default_rules: if self.load_default_rules:
self.load_defaults() self.load_defaults()
...@@ -128,6 +130,8 @@ class Extractor(Module): ...@@ -128,6 +130,8 @@ class Extractor(Module):
# Holds a dictionary of the last directory listing for a given directory; used for identifying # Holds a dictionary of the last directory listing for a given directory; used for identifying
# newly created/extracted files that need to be appended to self.pending. # newly created/extracted files that need to be appended to self.pending.
self.last_directory_listing = {} self.last_directory_listing = {}
# Reset the base output directory
self.directory = None
def callback(self, r): def callback(self, r):
# Make sure the file attribute is set to a compatible instance of binwalk.core.common.BlockFile # Make sure the file attribute is set to a compatible instance of binwalk.core.common.BlockFile
...@@ -363,6 +367,7 @@ class Extractor(Module): ...@@ -363,6 +367,7 @@ class Extractor(Module):
# Set the initial base extraction directory for later determining the level of recusion # Set the initial base extraction directory for later determining the level of recusion
if self.directory is None: if self.directory is None:
self.directory = os.path.realpath(output_directory) + os.path.sep self.directory = os.path.realpath(output_directory) + os.path.sep
self.output[path] = self.directory
return output_directory return output_directory
......
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