Commit 9ff6aaa6 by devttys0

Fixed IDA offset/length bugs; added binwalk.execute function; updated example scripts

parent 4ca9b19d
...@@ -149,7 +149,7 @@ if os.getenv("BUILD_PYQTGRAPH") == "1": ...@@ -149,7 +149,7 @@ if os.getenv("BUILD_PYQTGRAPH") == "1":
# Install the module, script, and support files # Install the module, script, and support files
setup(name = MODULE_NAME, setup(name = MODULE_NAME,
version = "2.0.0", version = "2.1.0",
description = "Firmware analysis tool", description = "Firmware analysis tool",
author = "Craig Heffner", author = "Craig Heffner",
url = "https://github.com/devttys0/%s" % MODULE_NAME, url = "https://github.com/devttys0/%s" % MODULE_NAME,
......
__all__ = ['Modules', 'ModuleException'] __all__ = ['execute', 'Modules', 'ModuleException']
import sys import sys
import binwalk.core.common import binwalk.core.common
...@@ -8,3 +8,6 @@ import binwalk.core.common ...@@ -8,3 +8,6 @@ import binwalk.core.common
sys.path.append(binwalk.core.common.get_libs_path()) sys.path.append(binwalk.core.common.get_libs_path())
from binwalk.core.module import Modules, ModuleException from binwalk.core.module import Modules, ModuleException
def execute(*args, **kwargs):
return Modules(*args, **kwargs).execute()
...@@ -176,6 +176,12 @@ def strings(filename, minimum=4): ...@@ -176,6 +176,12 @@ def strings(filename, minimum=4):
else: else:
result = "" result = ""
class GenericContainer(object):
def __init__(self, **kwargs):
for (k,v) in iterator(kwargs):
setattr(self, k, v)
class MathExpression(object): class MathExpression(object):
''' '''
Class for safely evaluating mathematical expressions from a string. Class for safely evaluating mathematical expressions from a string.
...@@ -268,52 +274,66 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS): ...@@ -268,52 +274,66 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS):
Returns None. Returns None.
''' '''
self.total_read = 0 self.total_read = 0
self.swap_size = swap
self.block_read_size = self.DEFAULT_BLOCK_READ_SIZE self.block_read_size = self.DEFAULT_BLOCK_READ_SIZE
self.block_peek_size = self.DEFAULT_BLOCK_PEEK_SIZE self.block_peek_size = self.DEFAULT_BLOCK_PEEK_SIZE
# This is so that custom parent classes can access/modify arguments as necessary
self.args = GenericContainer(fname=fname,
mode=mode,
length=length,
offset=offset,
block=block,
peek=peek,
swap=swap,
size=0)
# Python 2.6 doesn't like modes like 'rb' or 'wb' # Python 2.6 doesn't like modes like 'rb' or 'wb'
mode = mode.replace('b', '') mode = self.args.mode.replace('b', '')
try: super(self.__class__, self).__init__(fname, mode)
self.size = file_size(fname)
except KeyboardInterrupt as e: self.swap_size = self.args.swap
raise e
except Exception: if self.args.size:
self.size = 0 self.size = self.args.size
if offset < 0:
self.offset = self.size + offset
else: else:
self.offset = offset try:
self.size = file_size(self.args.fname)
except KeyboardInterrupt as e:
raise e
except Exception:
self.size = 0
if self.args.offset < 0:
self.offset = self.size + self.args.offset
else:
self.offset = self.args.offset
if self.offset < 0: if self.offset < 0:
self.offset = 0 self.offset = 0
elif self.offset > self.size: elif self.offset > self.size:
self.offset = self.size self.offset = self.size
if offset < 0: if self.args.offset < 0:
self.length = offset * -1 self.length = self.args.offset * -1
elif length: elif self.args.length:
self.length = length self.length = self.args.length
else: else:
self.length = self.size - offset self.length = self.size - self.args.offset
if self.length < 0: if self.length < 0:
self.length = 0 self.length = 0
elif self.length > self.size: elif self.length > self.size:
self.length = self.size self.length = self.size
if block is not None: if self.args.block is not None:
self.block_read_size = block self.block_read_size = self.args.block
self.base_block_size = self.block_read_size self.base_block_size = self.block_read_size
if peek is not None: if self.args.peek is not None:
self.block_peek_size = peek self.block_peek_size = self.args.peek
self.base_peek_size = self.block_peek_size self.base_peek_size = self.block_peek_size
super(self.__class__, self).__init__(fname, mode)
# Work around for python 2.6 where FileIO._name is not defined # Work around for python 2.6 where FileIO._name is not defined
try: try:
self.name self.name
......
...@@ -51,18 +51,22 @@ class IDBFileIO(io.FileIO): ...@@ -51,18 +51,22 @@ class IDBFileIO(io.FileIO):
else: else:
self.__idb__ = True self.__idb__ = True
self.name = fname self.name = fname
self.idb_start = 0 self.idb_start = 0
self.idb_pos = 0 self.idb_pos = 0
self.idb_end = end_address() self.idb_end = end_address()
if self.size == 0: if self.args.size == 0:
self.size = end_address() - start_address() self.args.size = end_address()
if self.length == 0: if self.args.offset == 0:
self.length = self.size self.args.offset = start_address()
elif self.args.offset < 0:
if self.offset == 0: self.args.length = self.args.offset * -1
self.offset = start_address() self.args.offset = end_address() + self.args.offset
if self.args.length == 0 or self.args.length > (end_address() - start_address()):
self.args.length = end_address() - start_address()
def read(self, n=-1): def read(self, n=-1):
if not self.__idb__: if not self.__idb__:
......
...@@ -4,4 +4,4 @@ import binwalk ...@@ -4,4 +4,4 @@ import binwalk
# Since no options are specified, they are by default taken from sys.argv. # Since no options are specified, they are by default taken from sys.argv.
# Effecitvely, this duplicates the functionality of the normal binwalk script. # Effecitvely, this duplicates the functionality of the normal binwalk script.
binwalk.Modules().execute() binwalk.execute()
...@@ -5,7 +5,7 @@ import binwalk ...@@ -5,7 +5,7 @@ import binwalk
try: try:
# Perform a signature scan against the files specified on the command line and suppress the usual binwalk output. # Perform a signature scan against the files specified on the command line and suppress the usual binwalk output.
for module in binwalk.Modules().execute(*sys.argv[1:], signature=True, quiet=True): for module in binwalk.execute(*sys.argv[1:], signature=True, quiet=True):
print ("%s Results:" % module.name) print ("%s Results:" % module.name)
for result in module.results: for result in module.results:
print ("\t%s 0x%.8X %s" % (result.file.name, result.offset, result.description)) print ("\t%s 0x%.8X %s" % (result.file.name, result.offset, result.description))
......
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