Commit 03316de3 by devttys0

Added --max-size option to limit extracted file size.

parent a6e3d932
...@@ -110,9 +110,10 @@ def usage(fd): ...@@ -110,9 +110,10 @@ def usage(fd):
fd.write("\t-D, --dd=<type:ext[:cmd]> Extract <type> signatures, give the files an extension of <ext>, and execute <cmd>\n") fd.write("\t-D, --dd=<type:ext[:cmd]> Extract <type> signatures, give the files an extension of <ext>, and execute <cmd>\n")
fd.write("\t-e, --extract=[file] Automatically extract known file types; load rules from file, if specified\n") fd.write("\t-e, --extract=[file] Automatically extract known file types; load rules from file, if specified\n")
fd.write("\t-M, --matryoshka=[n] Recursively scan extracted files, up to n levels deep (8 levels of recursion is the default)\n") fd.write("\t-M, --matryoshka=[n] Recursively scan extracted files, up to n levels deep (8 levels of recursion is the default)\n")
fd.write("\t-j, --max-size=<int> Limit extracted file sizes (default: infinite)\n")
fd.write("\t-r, --rm Cleanup extracted files and zero-size files\n") fd.write("\t-r, --rm Cleanup extracted files and zero-size files\n")
fd.write("\t-d, --honor-footers Only extract files up to their corresponding footer signatures\n") fd.write("\t-d, --honor-footers Only extract files up to their corresponding footer signatures\n")
fd.write("\t-z, --carve Carve data from files, but don't execute extraction utilities (iplies -d)\n") fd.write("\t-z, --carve Carve data from files, but don't execute extraction utilities (implies -d)\n")
fd.write("\n") fd.write("\n")
fd.write("Plugin Options:\n") fd.write("Plugin Options:\n")
...@@ -156,6 +157,7 @@ def main(): ...@@ -156,6 +157,7 @@ def main():
matryoshka = 1 matryoshka = 1
block_size = 0 block_size = 0
failed_open_count = 0 failed_open_count = 0
max_extract_size = None
quiet = False quiet = False
do_comp = False do_comp = False
do_files = False do_files = False
...@@ -196,7 +198,7 @@ def main(): ...@@ -196,7 +198,7 @@ def main():
config = binwalk.Config() config = binwalk.Config()
short_options = "AaBbCcdEeGHhIiJkLMNnOPpQqrSTtUuvWwz?D:F:f:g:K:o:l:m:R:s:X:x:Y:y:" short_options = "AaBbCcdEeGHhIiJkLMNnOPpQqrSTtUuvWwz?D:F:f:g:j:K:o:l:m:R:s:X:x:Y:y:"
long_options = [ long_options = [
"rm", "rm",
"help", "help",
...@@ -215,7 +217,6 @@ def main(): ...@@ -215,7 +217,6 @@ def main():
"show-invalid", "show-invalid",
"ignore-time-skew", "ignore-time-skew",
"honor-footers", "honor-footers",
"carve",
"profile", "profile",
"delay", # delay is depreciated, but kept for backwards compatability "delay", # delay is depreciated, but kept for backwards compatability
"skip-unopened", "skip-unopened",
...@@ -231,12 +232,14 @@ def main(): ...@@ -231,12 +232,14 @@ def main():
"save-plot", "save-plot",
"no-plot", "no-plot",
"no-legend", "no-legend",
"matryoshka=",
"strings", "strings",
"carve",
"matryoshka=",
"list-plugins", "list-plugins",
"disable-plugins", "disable-plugins",
"disable-plugin=", "disable-plugin=",
"enable-plugin=", "enable-plugin=",
"max-size=",
"marker=", "marker=",
"strlen=", "strlen=",
"file=", "file=",
...@@ -342,6 +345,8 @@ def main(): ...@@ -342,6 +345,8 @@ def main():
ignore_time_skew = False ignore_time_skew = False
elif opt in ("-z", "--carve"): elif opt in ("-z", "--carve"):
exec_commands = False exec_commands = False
elif opt in ("-j", "--max-size"):
max_extract_size = binwalk.common.str2int(arg)
elif opt in ("-H", "--heuristic", "--math"): elif opt in ("-H", "--heuristic", "--math"):
do_comp = True do_comp = True
...@@ -467,7 +472,8 @@ def main(): ...@@ -467,7 +472,8 @@ def main():
ignore_smart_keywords=ignore_signature_keywords, ignore_smart_keywords=ignore_signature_keywords,
load_plugins=enable_plugins, load_plugins=enable_plugins,
ignore_time_skews=ignore_time_skew, ignore_time_skews=ignore_time_skew,
exec_commands=exec_commands) exec_commands=exec_commands,
max_extract_size=max_extract_size)
# If a custom signature was specified, create a temporary magic file containing the custom signature # If a custom signature was specified, create a temporary magic file containing the custom signature
# and ensure that it is the only magic file that will be loaded when Binwalk.scan() is called. # and ensure that it is the only magic file that will be loaded when Binwalk.scan() is called.
......
...@@ -68,7 +68,7 @@ class Binwalk(object): ...@@ -68,7 +68,7 @@ class Binwalk(object):
CUSTOM = 0x40 CUSTOM = 0x40
ENTROPY = 0x80 ENTROPY = 0x80
def __init__(self, magic_files=[], flags=magic.MAGIC_NONE, log=None, quiet=False, verbose=0, ignore_smart_keywords=False, ignore_time_skews=False, load_extractor=False, load_plugins=True, exec_commands=True): def __init__(self, magic_files=[], flags=magic.MAGIC_NONE, log=None, quiet=False, verbose=0, ignore_smart_keywords=False, ignore_time_skews=False, load_extractor=False, load_plugins=True, exec_commands=True, max_extract_size=None):
''' '''
Class constructor. Class constructor.
...@@ -82,6 +82,7 @@ class Binwalk(object): ...@@ -82,6 +82,7 @@ class Binwalk(object):
@load_extractor - Set to True to load the default extraction rules automatically. @load_extractor - Set to True to load the default extraction rules automatically.
@load_plugins - Set to False to disable plugin support. @load_plugins - Set to False to disable plugin support.
@exec_commands - Set to False to disable the execution of external utilities when extracting data from files. @exec_commands - Set to False to disable the execution of external utilities when extracting data from files.
@max_extract_size - Limit the size of extracted files.
Returns None. Returns None.
''' '''
...@@ -135,7 +136,7 @@ class Binwalk(object): ...@@ -135,7 +136,7 @@ class Binwalk(object):
# o Specify file extraction rules to be applied during a scan # o Specify file extraction rules to be applied during a scan
# #
self.filter = MagicFilter() self.filter = MagicFilter()
self.extractor = Extractor(verbose=extractor_verbose, exec_commands=exec_commands) self.extractor = Extractor(verbose=extractor_verbose, exec_commands=exec_commands, max_size=max_extract_size)
if load_extractor: if load_extractor:
self.extractor.load_defaults() self.extractor.load_defaults()
......
...@@ -45,12 +45,13 @@ class Extractor: ...@@ -45,12 +45,13 @@ class Extractor:
# Max size of data to read/write at one time when extracting data # Max size of data to read/write at one time when extracting data
MAX_READ_SIZE = 10 * 1024 * 1024 MAX_READ_SIZE = 10 * 1024 * 1024
def __init__(self, verbose=False, exec_commands=True): def __init__(self, verbose=False, exec_commands=True, max_size=None):
''' '''
Class constructor. Class constructor.
@verbose - Set to True to display the output from any executed external applications. @verbose - Set to True to display the output from any executed external applications.
@exec_commands - Set to False to disable the execution of external utilities when extracting data from files. @exec_commands - Set to False to disable the execution of external utilities when extracting data from files.
@max_size - Limit the size of extracted files to max_size.
Returns None. Returns None.
''' '''
...@@ -58,6 +59,7 @@ class Extractor: ...@@ -58,6 +59,7 @@ class Extractor:
self.enabled = False self.enabled = False
self.delayed = True self.delayed = True
self.verbose = verbose self.verbose = verbose
self.max_size = max_size
self.exec_commands = exec_commands self.exec_commands = exec_commands
self.extract_rules = [] self.extract_rules = []
self.remove_after_execute = False self.remove_after_execute = False
...@@ -421,6 +423,9 @@ class Extractor: ...@@ -421,6 +423,9 @@ class Extractor:
# Default extracted file name is <hex offset>.<extension> # Default extracted file name is <hex offset>.<extension>
default_bname = "%X" % offset default_bname = "%X" % offset
if self.max_size and 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:
......
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