From 8fd989394bdbc9f5d33c6e30af1f60fd5efb823d Mon Sep 17 00:00:00 2001 From: devttys0 <heffnercj@gmail.com> Date: Sun, 13 Jul 2014 09:54:41 -0400 Subject: [PATCH] Updated settings.py to be more pythonic. --- Makefile.in | 25 +++++++++++++++++++------ configure | 13 +++++++++++-- configure.ac | 13 +++++++++++-- setup.py | 2 +- src/binwalk/core/C.py | 18 ++++++++++++------ src/binwalk/core/plugin.py | 5 ++++- src/binwalk/core/settings.py | 48 +++++++++++++++++++----------------------------- src/binwalk/magic/binwalk | 34 +++++++++++++++++----------------- src/binwalk/modules/extractor.py | 4 ++-- src/binwalk/modules/signature.py | 12 ++++++------ 10 files changed, 102 insertions(+), 72 deletions(-) diff --git a/Makefile.in b/Makefile.in index 42820e0..f752e85 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,6 +13,12 @@ export BUILD_FUZZY=@BUILD_FUZZY@ BUILD_C_LIBS=@BUILD_C_LIBS@ PYTHON=@PYTHON@ SRC_C_DIR="./src/C" +ifeq ($(strip $(prefix)),) + PREFIX="" +else + PREFIX="--prefix=$(prefix)" +endif +START_SCRIPT="binwalk-start" .PHONY: all install build deps clean uninstall @@ -20,25 +26,32 @@ all: build install: build if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) install; fi -ifeq ($(strip $(prefix)),) - $(PYTHON) ./setup.py install -else - $(PYTHON) ./setup.py install --prefix=$(prefix) -endif + $(PYTHON) ./setup.py install $(PREFIX) if [ "$(BUILD_C_LIBS)" -eq "1" ] && [ $(findstring "CYGWIN", $(PLATFORM)) ]; then ldconfig || true; fi build: if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR); fi + echo "$(prefix)" > src/binwalk/config/prefix.conf $(PYTHON) ./setup.py build +start-script: + echo '#!/bin/sh' > $(START_SCRIPT) + echo "PREFIX=\"$(prefix)\"" >> $(START_SCRIPT) + echo 'PYVERSION=`python --version 2>&1 | cut -d' ' -f2 | cut -d'.' -f1,2`' >> $(START_SCRIPT) + echo 'export PYTHONPATH="\$PREFIX/lib/python$PYVERSION/site-packages"' >> $(START_SCRIPT) + echo '\$PREFIX/bin/binwalk "\$@"' >> $(START_SCRIPT) + chmod +x $(START_SCRIPT) + + clean: if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) clean; fi $(PYTHON) ./setup.py clean distclean: clean if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) distclean; fi - rm -rf Makefile config.* *.cache + rm -rf Makefile config.* *.cache $(START_SCRIPT) uninstall: if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) uninstall; fi $(PYTHON) ./setup.py uninstall + diff --git a/configure b/configure index 550533f..f557d98 100755 --- a/configure +++ b/configure @@ -3333,6 +3333,15 @@ fi CFLAGS="-Wall -fPIC $CFLAGS" INSTALL_OPTIONS="-m644" +if test "$prefix" != "NONE" +then + CONFIGURE_PREFIX="--prefix=$prefix" +else + CONFIGURE_PREFIX="" +fi + +echo "prefix: $prefix" + if test "$(uname)" == "Darwin" then SONAME="-install_name" @@ -3346,12 +3355,12 @@ if test "$BUILD_C_LIBS" != "0" then if test "$BUILD_MAGIC" != "0" then - (cd ./src/C/file-*/ && autoreconf -v && ./configure) || exit 1 + (cd ./src/C/file-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1 fi if test "$BUILD_FUZZY" != "0" then - (cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure) || exit 1 + (cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1 fi fi diff --git a/configure.ac b/configure.ac index 333a3e1..06d8b6e 100644 --- a/configure.ac +++ b/configure.ac @@ -30,6 +30,15 @@ AC_ARG_ENABLE([libfuzzy], CFLAGS="-Wall -fPIC $CFLAGS" INSTALL_OPTIONS="-m644" +if test "$prefix" != "NONE" +then + CONFIGURE_PREFIX="--prefix=$prefix" +else + CONFIGURE_PREFIX="" +fi + +echo "prefix: $prefix" + if test "$(uname)" == "Darwin" then SONAME="-install_name" @@ -43,12 +52,12 @@ if test "$BUILD_C_LIBS" != "0" then if test "$BUILD_MAGIC" != "0" then - (cd ./src/C/file-*/ && autoreconf -v && ./configure) || exit 1 + (cd ./src/C/file-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1 fi if test "$BUILD_FUZZY" != "0" then - (cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure) || exit 1 + (cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1 fi fi diff --git a/setup.py b/setup.py index 8f7c182..c91c6e6 100755 --- a/setup.py +++ b/setup.py @@ -53,7 +53,7 @@ def remove_binwalk_module(): remove_tree(path) except OSError as e: pass - + script_path = which(MODULE_NAME) if script_path: try: diff --git a/src/binwalk/core/C.py b/src/binwalk/core/C.py index 4bcb4e2..3c5f8f9 100644 --- a/src/binwalk/core/C.py +++ b/src/binwalk/core/C.py @@ -96,6 +96,7 @@ class Library(object): Returns None. ''' + self.settings = binwalk.core.settings.Settings() self.library = ctypes.cdll.LoadLibrary(self.find_library(library)) if not self.library: raise Exception("Failed to load library '%s'" % library) @@ -114,20 +115,25 @@ class Library(object): ''' lib_path = None + try: + prefix = open(self.settings.system.prefix, 'r').read().strip() + except: + prefix = '' + if isinstance(libraries, str): libraries = [libraries] for library in libraries: system_paths = { - 'linux' : ['/usr/local/lib/lib%s.so' % library], - 'linux2' : ['/usr/local/lib/lib%s.so' % library], - 'linux3' : ['/usr/local/lib/lib%s.so' % library], - 'darwin' : ['/opt/local/lib/lib%s.dylib' % library, + 'linux' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library], + 'linux2' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library], + 'linux3' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library], + 'darwin' : [os.path.join(prefix, 'lib', 'lib%s.dylib' % library), '/opt/local/lib/lib%s.dylib' % library, '/usr/local/lib/lib%s.dylib' % library, ] + glob.glob('/usr/local/Cellar/lib%s/*/lib/lib%s.dylib' % (library, library)), - 'cygwin' : ['/usr/local/lib/lib%s.so' % library], - 'win32' : ['%s.dll' % library] + 'cygwin' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library], + 'win32' : [os.path.join(prefix, 'lib%s.dll' % library), '%s.dll' % library] } # Search the common install directories first; these are usually not in the library search path diff --git a/src/binwalk/core/plugin.py b/src/binwalk/core/plugin.py index 32f43b6..e431860 100644 --- a/src/binwalk/core/plugin.py +++ b/src/binwalk/core/plugin.py @@ -173,7 +173,10 @@ class Plugins(object): } for key in plugins.keys(): - plugins[key]['path'] = self.settings.get_file_path(key, self.settings.PLUGINS) + if key == 'user': + plugins[key]['path'] = self.settings.user.plugins + else: + plugins[key]['path'] = self.settings.system.plugins if plugins[key]['path']: for file_name in os.listdir(plugins[key]['path']): diff --git a/src/binwalk/core/settings.py b/src/binwalk/core/settings.py index 9a1ebe3..cfdba35 100644 --- a/src/binwalk/core/settings.py +++ b/src/binwalk/core/settings.py @@ -4,6 +4,12 @@ import os import binwalk.core.common as common from binwalk.core.compat import * +class Container(object): + + def __init__(self, **kwargs): + for (k,v) in iterator(kwargs): + setattr(self, k, v) + class Settings: ''' Binwalk settings class, used for accessing user and system file paths and general configuration settings. @@ -27,6 +33,7 @@ class Settings: # File names PLUGINS = "plugins" + PREFIX_FILE = "prefix.conf" EXTRACT_FILE = "extract.conf" BINWALK_MAGIC_FILE = "binwalk" BINARCH_MAGIC_FILE = "binarch" @@ -41,38 +48,21 @@ class Settings: # Path to the system wide binwalk directory self.system_dir = self._get_system_dir() - # Dictionary of all absolute user/system file paths - self.paths = { - 'user' : {}, - 'system' : {}, - } - # Build the paths to all user-specific files - self.paths['user'][self.BINWALK_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE) - self.paths['user'][self.BINARCH_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE) - self.paths['user'][self.BINCAST_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE) - self.paths['user'][self.EXTRACT_FILE] = self._user_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE) - self.paths['user'][self.PLUGINS] = self._user_path(self.BINWALK_PLUGINS_DIR) + self.user = Container(binwalk=self._user_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE), + binarch=self._user_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE), + bincast=self._user_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE), + extract=self._user_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE), + plugins=self._user_path(self.BINWALK_PLUGINS_DIR)) - # Build the paths to all system-wide files - self.paths['system'][self.BINWALK_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE) - self.paths['system'][self.BINARCH_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE) - self.paths['system'][self.BINCAST_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE) - 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 has_key(self.paths, usersys) and has_key(self.paths[usersys], fname) and self.paths[usersys][fname]: - return self.paths[usersys][fname] - return None + # Build the paths to all system-wide files + self.system = Container(binwalk=self._system_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE), + binarch=self._system_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE), + bincast=self._system_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE), + extract=self._system_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE), + prefix=self._system_path(self.BINWALK_CONFIG_DIR, self.PREFIX_FILE), + plugins=self._system_path(self.BINWALK_PLUGINS_DIR)) def find_magic_file(self, fname, system_only=False, user_only=False): ''' diff --git a/src/binwalk/magic/binwalk b/src/binwalk/magic/binwalk index f2988e1..a03ff1c 100644 --- a/src/binwalk/magic/binwalk +++ b/src/binwalk/magic/binwalk @@ -859,7 +859,7 @@ 0 string bFLT BFLT executable >4 belong <1 invalid >4 belong >4 invalid ->4 belong x version %ld, +>4 belong x version %d, >4 belong 4 >8 belong x code offset: 0x%.8X, >12 belong x data segment starts at: 0x%.8X, @@ -1191,34 +1191,34 @@ 0 lelong 0x28cd3d45 CramFS filesystem, little endian >4 lelong <0 invalid >4 lelong >1073741824 invalid ->4 lelong x size %lu +>4 lelong x size %u >8 lelong &1 version #2 >8 lelong &2 sorted_dirs >8 lelong &4 hole_support >32 lelong x CRC 0x%x, ->36 lelong x edition %lu, +>36 lelong x edition %u, >40 lelong <0 invalid ->40 lelong x %lu blocks, +>40 lelong x %u blocks, >44 lelong <0 invalid ->44 lelong x %lu files ->4 lelong x {jump-to-offset:%lu} ->4 lelong x {file-size:%lu} +>44 lelong x %u files +>4 lelong x {jump-to-offset:%u} +>4 lelong x {file-size:%u} 0 belong 0x28cd3d45 CramFS filesystem, big endian >4 belong <0 invalid >4 belong >1073741824 invalid ->4 belong x size %lu +>4 belong x size %u >8 belong &1 version #2 >8 belong &2 sorted_dirs >8 belong &4 hole_support >32 belong x CRC 0x%x, ->36 belong x edition %lu, +>36 belong x edition %u, >40 belong <0 invalid ->40 belong x %lu blocks, +>40 belong x %u blocks, >44 belong <0 invalid ->44 belong x %lu files ->4 belong x {jump-to-offset:%lu} ->4 belong x {file-size:%lu} +>44 belong x %u files +>4 belong x {jump-to-offset:%u} +>4 belong x {file-size:%u} @@ -2251,8 +2251,8 @@ >16 belong >10000 invalid >20 belong <1 invalid >20 belong >10000 invalid ->16 belong x \b, %ld x ->20 belong x %ld, +>16 belong x \b, %d x +>20 belong x %d, >24 byte x %d-bit >25 byte 0 grayscale, >25 byte 2 \b/color RGB, @@ -2267,8 +2267,8 @@ 0 string GIF8 GIF image data >4 string 7a \b, version "8%s", >4 string 9a \b, version "8%s", ->6 leshort >0 %hd x ->8 leshort >0 %hd +>6 leshort >0 %d x +>8 leshort >0 %d #>10 byte &0x80 color mapped, #>10 byte&0x07 =0x00 2 colors #>10 byte&0x07 =0x01 4 colors diff --git a/src/binwalk/modules/extractor.py b/src/binwalk/modules/extractor.py index 78e9626..669e7c8 100644 --- a/src/binwalk/modules/extractor.py +++ b/src/binwalk/modules/extractor.py @@ -270,8 +270,8 @@ class Extractor(Module): ''' # Load the user extract file first to ensure its rules take precedence. extract_files = [ - self.config.settings.get_file_path('user', self.config.settings.EXTRACT_FILE), - self.config.settings.get_file_path('system', self.config.settings.EXTRACT_FILE), + self.config.settings.user.extract, + self.config.settings.system.extract, ] for extract_file in extract_files: diff --git a/src/binwalk/modules/signature.py b/src/binwalk/modules/signature.py index f778256..c2e70bd 100644 --- a/src/binwalk/modules/signature.py +++ b/src/binwalk/modules/signature.py @@ -64,20 +64,20 @@ class Signature(Module): # Append the user's magic file first so that those signatures take precedence elif self.search_for_opcodes: self.magic_files = [ - 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), + self.config.settings.user.binarch, + self.config.settings.system.binarch, ] elif self.cast_data_types: self.magic_files = [ - 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), + self.config.settings.user.bincast, + self.config.settings.system.bincast, ] # Use the system default magic file if no other was specified, or if -B was explicitly specified if (not self.magic_files) or (self.explicit_signature_scan and not self.cast_data_types): - self.magic_files.append(self.config.settings.get_file_path('user', self.config.settings.BINWALK_MAGIC_FILE)) - self.magic_files.append(self.config.settings.get_file_path('system', self.config.settings.BINWALK_MAGIC_FILE)) + self.magic_files.append(self.config.settings.user.binwalk) + self.magic_files.append(self.config.settings.system.binwalk) # Parse the magic file(s) and initialize libmagic binwalk.core.common.debug("Loading magic files: %s" % str(self.magic_files)) -- libgit2 0.26.0