Commit 3040e25b by devttys0

Consolidated code into the binwalk.module.Module parent class.

parent 70432760
......@@ -3,7 +3,7 @@ import binwalk.module
from binwalk.compat import *
from binwalk.common import BlockFile
class Plotter(object):
class Plotter(binwalk.module.Module):
'''
Base class for plotting binaries in Qt.
Other plotter classes are derived from this.
......@@ -41,21 +41,13 @@ class Plotter(object):
binwalk.module.ModuleKwarg(name='show_grids', default=False),
]
def __init__(self, **kwargs):
'''
Class constructor.
@axis - Set to 2 for 2D plotting, 3 for 3D plotting.
@max_points - The maximum number of data points to display.
@show_grids - Set to True to display x-y-z grids.
HEADER = None
RESULT = None
Returns None.
'''
def init(self):
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtGui
binwalk.module.process_kwargs(self, kwargs)
self.verbose = self.config.verbose
self.offset = self.config.offset
self.length = self.config.length
......@@ -142,6 +134,7 @@ class Plotter(object):
for point in sorted(data_points, key=data_points.get, reverse=True):
plot_points[point] = data_points[point]
self.result(point=point)
total += 1
if total >= self.max_points:
break
......@@ -305,5 +298,5 @@ class Plotter(object):
def run(self):
self.plot()
return self.plot_points
return True
......@@ -6,7 +6,7 @@ import binwalk.display
from binwalk.config import *
from binwalk.compat import *
class Configuration(object):
class Configuration(binwalk.module.Module):
NAME = "General"
CLI = [
......@@ -28,12 +28,6 @@ class Configuration(object):
type=int,
kwargs={'block' : 0},
description='Set file block size'),
binwalk.module.ModuleOption(long='grep',
short='g',
nargs=1,
kwargs={'grep' : []},
type=list,
description='Grep results for the specified text'),
binwalk.module.ModuleOption(long='log',
short='f',
nargs=1,
......@@ -81,10 +75,8 @@ class Configuration(object):
binwalk.module.ModuleKwarg(name='log_file', default=None),
binwalk.module.ModuleKwarg(name='csv', default=False),
binwalk.module.ModuleKwarg(name='format_to_terminal', default=False),
binwalk.module.ModuleKwarg(name='grep', default=[]),
binwalk.module.ModuleKwarg(name='quiet', default=False),
binwalk.module.ModuleKwarg(name='verbose', default=[]),
binwalk.module.ModuleKwarg(name='debug_verbose', default=False),
binwalk.module.ModuleKwarg(name='skip_unopened', default=False),
binwalk.module.ModuleKwarg(name='files', default=[]),
binwalk.module.ModuleKwarg(name='show_help', default=False),
......
......@@ -20,7 +20,7 @@ class HashResult(object):
self.hash = hash
self.strings = strings
class HashMatch(object):
class HashMatch(binwalk.module.Module):
'''
Class for fuzzy hash matching of files and directories.
'''
......@@ -74,11 +74,12 @@ class HashMatch(object):
# Files smaller than this won't produce meaningful fuzzy results (from ssdeep.h)
FUZZY_MIN_FILE_SIZE = 4096
HEADER = ["SIMILARITY", "FILE NAME"]
HEADER_FORMAT = "\n%s" + " " * 11 + "%s\n"
RESULT_FORMAT = "%4d%%" + " " * 16 + "%s\n"
HEADER = ["SIMILARITY", "FILE NAME"]
RESULT = ["percentage", "description"]
def __init__(self, **kwargs):
def init(self):
'''
Class constructor.
......@@ -94,8 +95,6 @@ class HashMatch(object):
Returns None.
'''
binwalk.module.process_kwargs(self, kwargs)
self.total = 0
self.last_file1 = HashResult(None)
self.last_file2 = HashResult(None)
......@@ -112,13 +111,10 @@ class HashMatch(object):
def _get_strings(self, fname):
return ''.join(list(binwalk.common.strings(fname, minimum=10)))
def _print(self, match, fname):
def _show_result(self, match, fname):
if self.abspath:
fname = os.path.abspath(fname)
self.config.display.result(match, fname)
def _print_footer(self):
self.config.display.footer()
self.result(percentage=match, description=fname)
def _compare_files(self, file1, file2):
'''
......@@ -270,21 +266,17 @@ class HashMatch(object):
Returns a list of tuple results.
'''
results = []
self.total = 0
for f in haystack:
m = self._compare_files(needle, f)
if m is not None and self.is_match(m):
self._print(m, f)
results.append((m, f))
self._show_result(m, f)
self.total += 1
if self.max_results and self.total >= self.max_results:
break
return results
def hash_file(self, needle, haystack):
'''
Search for one file inside one or more directories.
......@@ -300,7 +292,7 @@ class HashMatch(object):
f = os.path.join(directory, f)
m = self._compare_files(needle, f)
if m is not None and self.is_match(m):
self._print(m, f)
self._show_result(m, f)
matching_files.append((m, f))
self.total += 1
......@@ -319,7 +311,6 @@ class HashMatch(object):
Returns a list of tuple results.
'''
done = False
results = []
self.total = 0
source_files = self._get_file_list(needle)
......@@ -334,8 +325,7 @@ class HashMatch(object):
m = self._compare_files(file1, file2)
if m is not None and self.is_match(m):
self._print(m, file2)
results.append((m, file2))
self._show_result(m, file2)
self.total += 1
if self.max_results and self.total >= self.max_results:
......@@ -344,31 +334,22 @@ class HashMatch(object):
if done:
break
return results
def run(self):
'''
Main module method.
'''
results = None
needle = self.config.target_files[0].name
haystack = []
for fp in self.config.target_files[1:]:
haystack.append(fp.name)
self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)
self.config.display.header(*self.HEADER)
if os.path.isfile(needle):
if os.path.isfile(haystack[0]):
results = self.hash_files(needle, haystack)
self.hash_files(needle, haystack)
else:
results = self.hash_file(needle, haystack)
self.hash_file(needle, haystack)
else:
results = self.hash_directories(needle, haystack)
self.config.display.footer()
return results
self.hash_directories(needle, haystack)
return True
......@@ -6,7 +6,8 @@ import binwalk.module
import binwalk.common as common
from binwalk.compat import *
class HexDiff(object):
# TODO: This code is an effing mess.
class HexDiff(binwalk.module.Module):
ALL_SAME = 0
ALL_DIFF = 1
......@@ -52,12 +53,10 @@ class HexDiff(object):
binwalk.module.ModuleKwarg(name='terse', default=False),
]
def __init__(self, **kwargs):
binwalk.module.process_kwargs(self, kwargs)
self.block_hex = ""
self.printed_alt_text = False
HEADER_FORMAT = "\n%s\n"
RESULT_FORMAT = "%s\n"
RESULT = ['description']
def _no_colorize(self, c, color="red", bold=True):
return c
......@@ -84,19 +83,16 @@ class HexDiff(object):
return False
def _print_block_hex(self, alt_text="*"):
printed = False
if self._color_filter(self.block_hex):
self.config.display.result(self.block_hex)
desc = self.block_hex
self.printed_alt_text = False
printed = True
elif not self.printed_alt_text:
self.config.display.result("%s" % alt_text)
desc = "%s" % alt_text
self.printed_alt_text = True
printed = True
self.result(description=desc)
self.block_hex = ""
return printed
return True
def _build_block(self, c, highlight=None):
if highlight == self.ALL_DIFF:
......@@ -117,12 +113,33 @@ class HexDiff(object):
return header
def init(self):
block = self.config.block
if not block:
block = self.DEFAULT_BLOCK_SIZE
if self.terse:
header_files = self.config.target_files[:1]
else:
header_files = self.config.target_files
self.HEADER = self._build_header(header_files, block)
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() and platform.system() != 'Windows':
curses.setupterm()
self.colorize = self._colorize
else:
self.colorize = self._no_colorize
def run(self):
i = 0
total = 0
data = {}
delim = '/'
self.block_hex = ""
self.printed_alt_text = False
offset = self.config.offset
size = self.config.length
block = self.config.block
......@@ -130,25 +147,10 @@ class HexDiff(object):
if not block:
block = self.DEFAULT_BLOCK_SIZE
self.config.display.format_strings("\n%s\n", "%s\n")
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() and platform.system() != 'Windows':
curses.setupterm()
self.colorize = self._colorize
else:
self.colorize = self._no_colorize
# If negative offset, then we're going that far back from the end of the file
if offset < 0:
size = offset * -1
if self.terse:
header = self._build_header(self.config.target_files[:1], block)
else:
header = self._build_header(self.config.target_files, block)
self.config.display.header(header)
if common.BlockFile.READ_BLOCK_SIZE < block:
read_block_size = block
else:
......@@ -242,7 +244,5 @@ class HexDiff(object):
i += block
total += read_block_size
self.config.display.footer()
return True
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