Commit 44e33c03 by devttys0

Fixed bugs, renamed config.py to settings.py

parent 578eeff2
......@@ -312,6 +312,9 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS):
return data
def reset(self):
self.seek(self.offset)
def write(self, data):
'''
Writes data to the opened file.
......
......@@ -43,7 +43,17 @@ class Display(object):
self._fprint("%s", ["-" * self.HEADER_WIDTH + "\n"], csv=False)
def result(self, *args):
self._fprint(self.result_format, args)
# Convert to list for item assignment
args = list(args)
# Replace multiple spaces with single spaces. This is to prevent accidentally putting
# four spaces in the description string, which would break auto-formatting.
for i in range(len(args)):
if isinstance(args[i], str):
while " " in args[i]:
args[i] = args[i].replace(" " , " ")
self._fprint(self.result_format, tuple(args))
def footer(self):
self._fprint("%s", "\n", csv=False)
......
......@@ -5,7 +5,7 @@ import inspect
import argparse
import traceback
import binwalk.core.common
import binwalk.core.config
import binwalk.core.settings
import binwalk.core.plugin
from binwalk.core.compat import *
......@@ -144,6 +144,10 @@ class Module(object):
#RESULT = ['offset', 'description']
RESULT = ["offset", "offset", "description"]
# If set to True, the progress status will be automatically updated for each result
# containing a valid file attribute.
AUTO_UPDATE_STATUS = True
def __init__(self, dependency=False, **kwargs):
self.errors = []
self.results = []
......@@ -167,6 +171,15 @@ class Module(object):
self.plugins.load_plugins()
def __del__(self):
return None
def __enter__(self):
return self
def __exit__(self, x, z, y):
return None
def load(self):
'''
Invoked at module load time.
......@@ -282,7 +295,7 @@ class Module(object):
self.results.append(r)
# Update the progress status automatically if it is not being done manually by the module
if r.file and not self.status.total:
if r.file and self.AUTO_UPDATE_STATUS:
self.status.total = r.file.length
self.status.completed = r.file.tell() - r.file.offset
......@@ -446,7 +459,7 @@ class Modules(object):
return modules
def help(self):
help_string = "\nBinwalk v%s\nCraig Heffner, http://www.binwalk.core.org\n" % binwalk.core.config.Config.VERSION
help_string = "\nBinwalk v%s\nCraig Heffner, http://www.binwalk.org\n" % binwalk.core.settings.Settings.VERSION
for obj in self.list(attribute="CLI"):
if obj.CLI:
......
import os
import sys
import imp
import binwalk.core.config
import binwalk.core.settings
from binwalk.core.compat import *
class Plugins:
......@@ -54,7 +54,7 @@ class Plugins:
self.pre_scan = []
self.post_scan = []
self.parent = parent
self.config = binwalk.core.config.Config()
self.settings = binwalk.core.settings.Settings()
def __del__(self):
pass
......@@ -112,7 +112,7 @@ class Plugins:
}
for key in plugins.keys():
plugins[key]['path'] = self.config.paths[key][self.config.PLUGINS]
plugins[key]['path'] = self.settings.paths[key][self.settings.PLUGINS]
for file_name in os.listdir(plugins[key]['path']):
if file_name.endswith(self.MODULE_EXTENSION):
......
......@@ -2,36 +2,16 @@ import os
import binwalk.core.common as common
from binwalk.core.compat import *
class Config:
class Settings:
'''
Binwalk configuration class, used for accessing user and system file paths.
Binwalk settings class, used for accessing user and system file paths and general configuration settings.
After instatiating the class, file paths can be accessed via the self.paths dictionary.
System file paths are listed under the 'system' key, user file paths under the 'user' key.
For example, to get the path to both the user and system binwalk magic files:
from binwalk import Config
conf = Config()
user_binwalk_file = conf.paths['user'][conf.BINWALK_MAGIC_FILE]
system_binwalk_file = conf.paths['system'][conf.BINWALK_MAGIC_FILE]
There is also an instance of this class available via the Binwalk.config object:
import binwalk
bw = binwalk.Binwalk()
user_binwalk_file = bw.config.paths['user'][conf.BINWALK_MAGIC_FILE]
system_binwalk_file = bw.config.paths['system'][conf.BINWALK_MAGIC_FILE]
Valid file names under both the 'user' and 'system' keys are as follows:
o BINWALK_MAGIC_FILE - Path to the default binwalk magic file.
o BINCAST_MAGIC_FILE - Path to the bincast magic file (used when -C is specified with the command line binwalk script).
o BINARCH_MAGIC_FILE - Path to the binarch magic file (used when -A is specified with the command line binwalk script).
o EXTRACT_FILE - Path to the extract configuration file (used when -e is specified with the command line binwalk script).
o PLUGINS - Path to the plugins directory.
'''
# Release version
......@@ -47,10 +27,6 @@ class Config:
PLUGINS = "plugins"
EXTRACT_FILE = "extract.conf"
BINWALK_MAGIC_FILE = "binwalk"
BINCAST_MAGIC_FILE = "bincast"
BINARCH_MAGIC_FILE = "binarch"
ZLIB_MAGIC_FILE = "zlib"
COMPRESSD_MAGIC_FILE = "compressd"
def __init__(self):
'''
......@@ -69,17 +45,11 @@ class Config:
# 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.BINCAST_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE)
self.paths['user'][self.BINARCH_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINARCH_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)
# 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.BINCAST_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE)
self.paths['system'][self.BINARCH_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE)
self.paths['system'][self.ZLIB_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.ZLIB_MAGIC_FILE)
self.paths['system'][self.COMPRESSD_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.COMPRESSD_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)
......
......@@ -2,9 +2,8 @@ import os
import sys
import argparse
import binwalk.core.common
import binwalk.core.config
import binwalk.core.display
from binwalk.core.config import *
import binwalk.core.settings
from binwalk.core.compat import *
from binwalk.core.module import Module, Option, Kwarg, show_help
......@@ -82,11 +81,12 @@ class Configuration(Module):
def load(self):
self.target_files = []
# Order is important with these two methods
self._open_target_files()
self._set_verbosity()
self.settings = binwalk.core.config.Config()
self.settings = binwalk.core.settings.Settings()
self.display = binwalk.core.display.Display(log=self.log_file,
csv=self.csv,
quiet=self.quiet,
......@@ -97,15 +97,16 @@ class Configuration(Module):
show_help()
sys.exit(0)
def reset(self):
for fp in self.target_files:
fp.reset()
def __del__(self):
self._cleanup()
def __exit__(self, a, b, c):
self._cleanup()
def __enter__(self):
return self
def _cleanup(self):
if hasattr(self, 'target_files'):
for fp in self.target_files:
......
......@@ -58,11 +58,6 @@ class Signature(Module):
Kwarg(name='include_filters', default=[]),
]
HEADER = ["DECIMAL", "HEX", "DESCRIPTION"]
HEADER_FORMAT = "%-12s %-12s %s\n"
RESULT = ["offset", "offset", "description"]
RESULT_FORMAT = "%-12d 0x%-12X %s\n"
MAGIC_FLAGS = magic.MAGIC_NO_CHECK_TEXT | magic.MAGIC_NO_CHECK_ENCODING | magic.MAGIC_NO_CHECK_APPTYPE | magic.MAGIC_NO_CHECK_TOKENS
def init(self):
......@@ -124,6 +119,11 @@ class Signature(Module):
self.status.completed = block_start - fp.offset
for candidate_offset in self.parser.find_signature_candidates(data, dlen):
# current_block_offset is set when a jump-to-offset keyword is encountered while
# processing signatures. This points to an offset inside the current data block
# that scanning should jump to, so ignore any subsequent candidate signatures that
# occurr before this offset inside the current data block.
if candidate_offset < current_block_offset:
continue
......@@ -134,16 +134,27 @@ class Signature(Module):
magic_result = self.magic.buffer(candidate_data)
if self.filter.valid_magic_result(magic_result):
# The smart filter parser returns a dictionary of keyword values and the signature description.
# The smart filter parser returns a binwalk.core.module.Result object
r = self.smart.parse(magic_result)
# Set the absolute offset inside the target file
r.offset = block_start + candidate_offset + r.adjust
# Provide an instance of the current file object
r.file = fp
# Register the result for futher processing/display
self.result(r=r)
# Is this a valid result and did it specify a jump-to-offset keyword?
if r.valid and r.jump > 0:
fp.seek(r.offset + r.jump)
current_block_offset = r.jump
absolute_jump_offset = r.offset + r.jump
current_block_offset = candidate_offset + r.jump
# If the jump-to-offset is beyond the confines of the current block, seek the file to
# that offset and quit processing this block of data.
if absolute_jump_offset >= fp.tell():
fp.seek(r.offset + r.jump)
break
def run(self):
target_files = self.config.target_files
......@@ -153,7 +164,7 @@ class Signature(Module):
self.header()
self.status.clear()
self.status.total = fp.size
self.status.total = fp.length
self.status.completed = 0
self.scan_file(fp)
......
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