Commit 3040e25b by devttys0

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

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