Commit e929bb00 by devttys0

Fixed --term and logging bugs.

parent 5475c714
import sys
import csv as pycsv
import binwalk.common
class Display(object):
BUFFER_WIDTH = 0
HEADER_WIDTH = 115
MAX_LINE_LEN = 0
SCREEN_WIDTH = 0
HEADER_WIDTH = 150
DEFAULT_FORMAT = "%s\n"
def __init__(self, quiet=False, verbose=0, log=None, csv=False, fit_to_screen=False):
......@@ -15,18 +13,22 @@ class Display(object):
self.fit_to_screen = fit_to_screen
self.fp = None
self.csv = None
self.num_columns = 0
self._configure_formatting()
if log:
self.fp = binwalk.common.BlockFile(log, mode="w")
if self.csv:
self.fp = open(log, "w")
if csv:
self.csv = pycsv.writer(self.fp)
def format_strings(self, header, result):
self.result_format = result
self.header_format = header
if self.num_columns == 0:
self.num_columns = len(header.split())
def log(self, fmt, columns):
if self.fp:
if self.csv:
......@@ -35,6 +37,8 @@ class Display(object):
self.fp.write(fmt % tuple(columns))
def header(self, *args):
self.num_columns = len(args)
self._fprint("%s", "\n", csv=False)
self._fprint(self.header_format, args)
self._fprint("%s", ["-" * self.HEADER_WIDTH + "\n"], csv=False)
......@@ -49,7 +53,7 @@ class Display(object):
line = fmt % tuple(columns)
sys.stdout.write(self._format_line(line.strip()) + "\n")
if not (self.csv and not csv):
if self.fp and not (self.csv and not csv):
self.log(fmt, columns)
def _append_to_data_parts(self, data, start, end):
......@@ -81,13 +85,18 @@ class Display(object):
offset = 0
space_offset = 0
self.string_parts = []
delim = '\n' + ' ' * self.BUFFER_WIDTH
delim = '\n'
if self.fit_to_screen:
while len(line[offset:]) > self.MAX_LINE_LEN:
space_offset = line[offset:offset+self.MAX_LINE_LEN].rfind(' ')
if self.fit_to_screen and len(line) > self.SCREEN_WIDTH:
line_columns = line.split(None, self.num_columns-1)
if line_columns:
delim = '\n' + ' ' * line.rfind(line_columns[-1])
while len(line[offset:]) > self.SCREEN_WIDTH:
space_offset = line[offset:offset+self.HEADER_WIDTH].rfind(' ')
if space_offset == -1 or space_offset == 0:
space_offset = self.MAX_LINE_LEN
space_offset = self.SCREEN_WIDTH
self._append_to_data_parts(line, offset, offset+space_offset)
......@@ -113,9 +122,7 @@ class Display(object):
# Get the terminal window width
hw = struct.unpack('hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
self.HEADER_WIDTH = hw[1]
self.SCREEN_WIDTH = self.HEADER_WIDTH = hw[1]
except Exception as e:
pass
self.MAX_LINE_LEN = self.HEADER_WIDTH - self.BUFFER_WIDTH
import io
import os
import sys
import inspect
import argparse
......@@ -103,9 +104,12 @@ class Module(object):
'''
All module classes must be subclassed from this.
'''
# The module name, as displayed in help output
# The module name, automatically populated.
NAME = ""
# The module title, as displayed in help output
TITLE = ""
# A list of binwalk.module.ModuleOption command line options
CLI = []
......@@ -113,7 +117,7 @@ class Module(object):
KWARGS = []
# A dictionary of module dependencies; all modules depend on binwalk.modules.configuration.Configuration
DEPENDS = {}
DEPENDS = {'config' : 'Configuration'}
# Format string for printing the header during a scan
HEADER_FORMAT = "%s\n"
......@@ -136,6 +140,7 @@ class Module(object):
# self.plugins = x
self.errors = []
self.results = []
self.NAME = self.__class__.__name__
process_kwargs(self, kwargs)
......@@ -302,17 +307,41 @@ class Modules(object):
Main class used for running and managing modules.
'''
def __init__(self, argv=sys.argv[1:]):
def __init__(self, *argv, **kargv):
'''
Class constructor.
@argv - List of command line options. Must not include the program name (sys.argv[0]).
@argv - List of command line options. Must not include the program name (e.g., sys.argv[1:]).
@kargv - Keyword dictionary of command line options.
Returns None.
'''
argv = list(argv)
for (k,v) in iterator(kargv):
k = self._parse_api_opt(k)
if v not in [True, False, None]:
argv.append("%s %s" % (k, v))
else:
argv.append(k)
if not argv:
argv = sys.argv[1:]
self.arguments = argv
self.loaded_modules = {}
def _parse_api_opt(self, opt):
# If the argument already starts with a hyphen, don't add hyphens in front of it
if opt.startswith('-'):
return opt
# Short options are only 1 character
elif len(opt) == 1:
return '-' + opt
else:
return '--' + opt
def list(self, attribute="run"):
'''
Finds all modules with the specified attribute.
......@@ -334,7 +363,7 @@ class Modules(object):
for obj in self.list(attribute="CLI"):
if obj.CLI:
help_string += "\n%s Options:\n" % obj.NAME
help_string += "\n%s Options:\n" % obj.TITLE
for module_option in obj.CLI:
if module_option.long:
......@@ -384,6 +413,14 @@ class Modules(object):
if hasattr(module, "DEPENDS"):
for (kwarg, dependency) in iterator(module.DEPENDS):
# The dependency module must be imported by binwalk.modules.__init__.py
if hasattr(binwalk.modules, dependency):
dependency = getattr(binwalk.modules, dependency)
else:
sys.stderr.write("WARNING: %s depends on %s which was not found in binwalk.modules.__init__.py\n" % (str(module), dependency))
continue
# No recursive dependencies, thanks
if dependency == module:
continue
......
from configuration import Configuration
from hashmatch import HashMatch
from signature import Signature
from binvis import Plotter
from hexdiff import HexDiff
from signature import Signature
from hashmatch import HashMatch
from configuration import Configuration
......@@ -2,7 +2,6 @@ import os
import binwalk.module
from binwalk.compat import *
from binwalk.common import BlockFile
from binwalk.modules.configuration import Configuration
class Plotter(binwalk.module.Module):
'''
......@@ -13,9 +12,7 @@ class Plotter(binwalk.module.Module):
MAX_2D_PLOT_POINTS = 12500
MAX_3D_PLOT_POINTS = 25000
NAME = "Binary Visualization"
DEPENDS = {'config' : Configuration}
TITLE = "Binary Visualization"
CLI = [
binwalk.module.ModuleOption(short='3',
......
......@@ -8,8 +8,8 @@ from binwalk.compat import *
class Configuration(binwalk.module.Module):
RUN = False
NAME = "General"
TITLE = "General"
CLI = [
binwalk.module.ModuleOption(long='length',
short='l',
......
......@@ -8,7 +8,6 @@ import binwalk.common
import binwalk.module
import binwalk.smartstrings
from binwalk.compat import *
from binwalk.modules.configuration import Configuration
class HashResult(object):
'''
......@@ -28,15 +27,14 @@ class HashMatch(binwalk.module.Module):
DEFAULT_CUTOFF = 0
CONSERVATIVE_CUTOFF = 90
DEPENDS = {'config' : Configuration }
TITLE = "Fuzzy Hash"
NAME = "Fuzzy Hash"
CLI = [
binwalk.module.ModuleOption(short='F',
long='fuzzy',
kwargs={'enabled' : True},
description='Perform fuzzy hash matching on files/directories'),
binwalk.module.ModuleOption(short='c',
binwalk.module.ModuleOption(short='t',
long='cutoff',
nargs=1,
priority=100,
......
......@@ -5,7 +5,6 @@ import platform
import binwalk.module
import binwalk.common as common
from binwalk.compat import *
from binwalk.modules.configuration import Configuration
# TODO: This code is an effing mess.
class HexDiff(binwalk.module.Module):
......@@ -23,9 +22,8 @@ class HexDiff(binwalk.module.Module):
'blue' : '34',
}
DEPENDS = {'config' : Configuration}
TITLE = "Binary Diffing"
NAME = "Binary Diffing"
CLI = [
binwalk.module.ModuleOption(short='W',
long='hexdump',
......
......@@ -5,13 +5,10 @@ import binwalk.parser
import binwalk.filter
import binwalk.smartsignature
from binwalk.compat import *
from binwalk.modules.configuration import Configuration
class Signature(binwalk.module.Module):
DEPENDS = {'config' : Configuration}
NAME = "Signature Scan"
TITLE = "Signature Scan"
CLI = [
binwalk.module.ModuleOption(short='B',
......@@ -32,8 +29,8 @@ class Signature(binwalk.module.Module):
binwalk.module.ModuleKwarg(name='magic_files', default=[]),
]
HEADER = "BINWALK"
HEADER_FORMAT = "%s\n"
HEADER = ["DECIMAL", "HEX", "DESCRIPTION"]
HEADER_FORMAT = "%-12s %-12s %s\n"
RESULT = ["offset", "offset", "description"]
RESULT_FORMAT = "%-12d 0x%-12X %s\n"
......
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