Commit 0c2412c4 by devttys0

Fixed file path bug in extractor.py where file names that are all digits were…

Fixed file path bug in extractor.py where file names that are all digits were treated as integers rather than strings
parent ec47069b
...@@ -573,12 +573,12 @@ class Extractor(Module): ...@@ -573,12 +573,12 @@ class Extractor(Module):
# No extraction rules for this file # No extraction rules for this file
if not rules: if not rules:
binwalk.core.common.debug("No extraction rules found for '%s'" % description)
return (None, None, False, str(None)) return (None, None, False, str(None))
else: else:
binwalk.core.common.debug("Found %d matching extraction rules" % len(rules)) binwalk.core.common.debug("Found %d matching extraction rules" % len(rules))
# Generate the output directory name where extracted files will be # Generate the output directory name where extracted files will be stored
# stored
output_directory = self.build_output_directory(file_name) output_directory = self.build_output_directory(file_name)
# Extract to end of file if no size was specified # Extract to end of file if no size was specified
...@@ -586,7 +586,9 @@ class Extractor(Module): ...@@ -586,7 +586,9 @@ class Extractor(Module):
size = file_size(file_path) - offset size = file_size(file_path) - offset
if os.path.isfile(file_path): if os.path.isfile(file_path):
binwalk.core.common.debug("Changing directory to: %s" % output_directory)
os.chdir(output_directory) os.chdir(output_directory)
# Extract into subdirectories named by the offset # Extract into subdirectories named by the offset
if self.extract_into_subdirs: if self.extract_into_subdirs:
# Remove trailing L that is added by hex() # Remove trailing L that is added by hex()
...@@ -598,6 +600,8 @@ class Extractor(Module): ...@@ -598,6 +600,8 @@ class Extractor(Module):
for i in range(0, len(rules)): for i in range(0, len(rules)):
rule = rules[i] rule = rules[i]
binwalk.core.common.debug("Processing extraction rule #%d (%s)" % (i, str(rule['cmd'])))
# Make sure we don't recurse into any extracted directories if # Make sure we don't recurse into any extracted directories if
# instructed not to # instructed not to
if rule['recurse'] in [True, False]: if rule['recurse'] in [True, False]:
...@@ -605,6 +609,8 @@ class Extractor(Module): ...@@ -605,6 +609,8 @@ class Extractor(Module):
else: else:
recurse = True recurse = True
binwalk.core.common.debug("Extracting %s[%d:] to %s" % (file_path, offset, name))
# Copy out the data to disk, if we haven't already # Copy out the data to disk, if we haven't already
fname = self._dd(file_path, offset, size, rule['extension'], output_file_name=name) fname = self._dd(file_path, offset, size, rule['extension'], output_file_name=name)
...@@ -619,6 +625,8 @@ class Extractor(Module): ...@@ -619,6 +625,8 @@ class Extractor(Module):
if self.remove_after_execute: if self.remove_after_execute:
fname_md5 = file_md5(fname) fname_md5 = file_md5(fname)
binwalk.core.common.debug("Executing extraction command %s" % (str(rule['cmd'])))
# Execute the specified command against the extracted file # Execute the specified command against the extracted file
if self.run_extractors: if self.run_extractors:
(extract_ok, command_line) = self.execute(rule['cmd'], fname, rule['codes']) (extract_ok, command_line) = self.execute(rule['cmd'], fname, rule['codes'])
...@@ -626,6 +634,9 @@ class Extractor(Module): ...@@ -626,6 +634,9 @@ class Extractor(Module):
extract_ok = True extract_ok = True
command_line = '' command_line = ''
binwalk.core.common.debug("Ran extraction command: %s" % command_line)
binwalk.core.common.debug("Extraction successful: %s" % extract_ok)
# Only clean up files if remove_after_execute was specified. # Only clean up files if remove_after_execute was specified.
# Only clean up files if the file was extracted sucessfully, or if we've run # Only clean up files if the file was extracted sucessfully, or if we've run
# out of extractors. # out of extractors.
...@@ -659,6 +670,7 @@ class Extractor(Module): ...@@ -659,6 +670,7 @@ class Extractor(Module):
else: else:
break break
binwalk.core.common.debug("Changing directory back to: %s" % original_dir)
os.chdir(original_dir) os.chdir(original_dir)
return (output_directory, fname, recurse, command_line) return (output_directory, fname, recurse, command_line)
...@@ -759,14 +771,17 @@ class Extractor(Module): ...@@ -759,14 +771,17 @@ class Extractor(Module):
# Default extracted file name is <displayed hex offset>.<extension> # Default extracted file name is <displayed hex offset>.<extension>
default_bname = "%X" % (offset + self.config.base) default_bname = "%X" % (offset + self.config.base)
# Make sure the output file name is a string
if output_file_name is not None:
output_file_name = str(output_file_name)
if self.max_size and size > self.max_size: if self.max_size and size > self.max_size:
size = self.max_size size = self.max_size
if not output_file_name or output_file_name is None: if not output_file_name or output_file_name is None:
bname = default_bname bname = default_bname
else: else:
# Strip the output file name of invalid/dangerous characters (like # Strip the output file name of invalid/dangerous characters (like file paths)
# file paths)
bname = os.path.basename(output_file_name) bname = os.path.basename(output_file_name)
fname = unique_file_name(bname, extension) fname = unique_file_name(bname, extension)
......
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