Commit d67a78da by devttys0

Updated extractor to support generating unique output file names for extraction rules

parent 7ab9d0fd
#################################################################################################################
# Default extract rules loaded when --extract is specified.
# Default extraction rules, loaded when --extract is specified.
#
# <lower-case unique string from binwalk output text>:<desired file extension>:<command to execute>
#
# Note that %e is a place holder for the extracted file name.
#
# The %% place holder is used when a unique file path is required.
# For example '%%squashfs-root%%' will be replaced with 'squashfs-root-0'
# if 'squashfs-root' already exists.
#################################################################################################################
# Assumes these utilities are installed in $PATH.
......@@ -22,9 +26,15 @@
^microsoft cabinet archive:cab:cabextract '%e'
# Try sasquatch first, or if not installed, the standard unsquashfs
^squashfs filesystem:squashfs:sasquatch '%e'
^squashfs filesystem:squashfs:unsquashfs '%e'
^squashfs filesystem:squashfs:sasquatch -d '%%squashfs-root%%' '%e'
^squashfs filesystem:squashfs:unsquashfs -d '%%squashfs-root%%' '%e'
# Try cramfsck first; if that fails, swap the file system and try again
^cramfs filesystem:cramfs:cramfsck -x '%%cramfs-root%%' '%e'
^cramfs filesystem:cramfs:cramfsswap '%e' '%e.swap' && cramfsck -x '%%cramfs-root%%' '%e.swap'
# Try mounting the file system (this requires root privileges)
^squashfs filesystem:mkdir squashfs-root && mount -t squashfs '%e' squashfs-root
^cramfs filesystem:cramfs:mkdir cramfs-root && mount -t cramfs '%e' cramfs-root
^ext2 filesystem:ext2:mkdir ext2-root && mount -t ext2 '%e' ext2-root
^romfs filesystem:romfs:mkdir romfs-root && mount -t romfs '%e' romfs-root
......
......@@ -27,6 +27,10 @@ class Extractor(Module):
# Place holder for the extracted file name in the command
FILE_NAME_PLACEHOLDER = '%e'
# Unique path delimiter, used for generating unique output file/directory names.
# Useful when, for example, extracting two squashfs images (squashfs-root, squashfs-root-0).
UNIQUE_PATH_DELIMITER = '%%'
TITLE = 'Extraction'
ORDER = 9
PRIMARY = False
......@@ -565,6 +569,13 @@ class Extractor(Module):
# Execute.
for command in cmd.split("&&"):
# Generate unique file paths for all paths in the current command that are surrounded by UNIQUE_PATH_DELIMITER
while self.UNIQUE_PATH_DELIMITER in command:
need_unique_path = command.split(self.UNIQUE_PATH_DELIMITER)[1].split(self.UNIQUE_PATH_DELIMITER)[0]
unique_path = binwalk.core.common.unique_file_name(need_unique_path)
command = command.replace(self.UNIQUE_PATH_DELIMITER + need_unique_path + self.UNIQUE_PATH_DELIMITER, unique_path)
# Replace all instances of FILE_NAME_PLACEHOLDER in the command with fname
command = command.strip().replace(self.FILE_NAME_PLACEHOLDER, fname)
......@@ -581,11 +592,7 @@ class Extractor(Module):
except KeyboardInterrupt as e:
raise e
except Exception as e:
# Silently ignore no such file or directory errors. Why? Because these will inevitably be raised when
# making the switch to the new firmware mod kit directory structure. We handle this elsewhere, but it's
# annoying to see this spammed out to the console every time.
if binwalk.core.common.DEBUG or (not hasattr(e, 'errno') or e.errno != 2):
binwalk.core.common.warning("Extractor.execute failed to run external extrator '%s': %s" % (str(cmd), str(e)))
binwalk.core.common.warning("Extractor.execute failed to run external extrator '%s': %s" % (str(cmd), str(e)))
retval = None
if tmp is not None:
......
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