Commit cf10f5b2 by devttys0

Added sanity checks to user and system file paths in case they don't exist.

parent 7c705dd9
......@@ -145,10 +145,11 @@ class MagicParser(object):
files = [file_name]
for fname in files:
if os.path.exists(fname):
self.parse_file(fname)
else:
sys.stdout.write("WARNING: Magic file '%s' does not exist!\n" % fname)
if fname:
if os.path.exists(fname):
self.parse_file(fname)
else:
sys.stdout.write("WARNING: Magic file '%s' does not exist!\n" % fname)
self.fd.seek(0)
return self.fd.name
......
......@@ -41,7 +41,7 @@ class Settings:
# Dictionary of all absolute user/system file paths
self.paths = {
'user' : {},
'user' : {},
'system' : {},
}
......@@ -59,6 +59,19 @@ class Settings:
self.paths['system'][self.EXTRACT_FILE] = self._system_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE)
self.paths['system'][self.PLUGINS] = self._system_path(self.BINWALK_PLUGINS_DIR)
def get_file_path(self, usersys, fname):
'''
Retrieves the specified file path from self.paths.
@usersys - One of: 'user', 'system'.
@fname - The file name (e.g., self.BINWALK_MAGIC_FILE, self.PLUGINS, etc)
Returns the path, if it exists; returns None otherwise.
'''
if self.paths.has_key(usersys) and has_key(self.paths[usersys], fname) and self.paths[usersys][fname]:
return self.paths[usersys][fname]
return None
def find_magic_file(self, fname, system_only=False, user_only=False):
'''
Finds the specified magic file name in the system / user magic file directories.
......@@ -149,7 +162,12 @@ class Settings:
Returns the full path to the 'subdir/basename' file.
'''
return self._file_path(os.path.join(self.user_dir, self.BINWALK_USER_DIR, subdir), basename)
try:
return self._file_path(os.path.join(self.user_dir, self.BINWALK_USER_DIR, subdir), basename)
except KeyboardInterrupt as e :
raise e
except Exception:
return None
def _system_path(self, subdir, basename=''):
'''
......@@ -160,5 +178,10 @@ class Settings:
Returns the full path to the 'subdir/basename' file.
'''
return self._file_path(os.path.join(self.system_dir, subdir), basename)
try:
return self._file_path(os.path.join(self.system_dir, subdir), basename)
except KeyboardInterrupt as e :
raise e
except Exception:
return None
......@@ -258,18 +258,19 @@ class Extractor(Module):
'''
# Load the user extract file first to ensure its rules take precedence.
extract_files = [
self.config.settings.paths['user'][self.config.settings.EXTRACT_FILE],
self.config.settings.paths['system'][self.config.settings.EXTRACT_FILE],
self.config.settings.get_file_path('user', self.config.settings.EXTRACT_FILE),
self.config.settings.get_file_path('system', self.config.settings.EXTRACT_FILE),
]
for extract_file in extract_files:
try:
self.load_from_file(extract_file)
except KeyboardInterrupt as e:
raise e
except Exception as e:
if self.config.verbose:
raise Exception("Extractor.load_defaults failed to load file '%s': %s" % (extract_file, str(e)))
if extract_file:
try:
self.load_from_file(extract_file)
except KeyboardInterrupt as e:
raise e
except Exception as e:
if self.config.verbose:
raise Exception("Extractor.load_defaults failed to load file '%s': %s" % (extract_file, str(e)))
def build_output_directory(self, path):
'''
......
......@@ -62,21 +62,21 @@ class Signature(Module):
# Append the user's magic file first so that those signatures take precedence
if self.search_for_opcodes:
self.magic_files += [
self.config.settings.paths['user'][self.config.settings.BINARCH_MAGIC_FILE],
self.config.settings.paths['system'][self.config.settings.BINARCH_MAGIC_FILE],
self.config.settings.get_file_path('user', self.config.settings.BINARCH_MAGIC_FILE),
self.config.settings.get_file_path('system', self.config.settings.BINARCH_MAGIC_FILE),
]
if self.cast_data_types:
self.magic_files += [
self.config.settings.paths['user'][self.config.settings.BINCAST_MAGIC_FILE],
self.config.settings.paths['system'][self.config.settings.BINCAST_MAGIC_FILE],
self.config.settings.get_file_path('user', self.config.settings.BINCAST_MAGIC_FILE),
self.config.settings.get_file_path('system', self.config.settings.BINCAST_MAGIC_FILE),
]
# Use the system default magic file if no other was specified, or if -B was explicitly specified
if not self.magic_files or self.force_default_scan:
self.magic_files += [
self.config.settings.paths['user'][self.config.settings.BINWALK_MAGIC_FILE],
self.config.settings.paths['system'][self.config.settings.BINWALK_MAGIC_FILE],
self.config.settings.get_file_path('user', self.config.settings.BINWALK_MAGIC_FILE),
self.config.settings.get_file_path('system', self.config.settings.BINWALK_MAGIC_FILE),
]
# Parse the magic file(s) and initialize libmagic
......
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