Commit 5475c714 by devttys0

Working on terminal line wrapping

parent 59ed91d3
...@@ -308,6 +308,16 @@ class BlockFile(io.FileIO): ...@@ -308,6 +308,16 @@ class BlockFile(io.FileIO):
return bytes2str(data) return bytes2str(data)
def seek(self, n, whence=os.SEEK_SET):
if whence == os.SEEK_SET:
self.total_read = n
elif whence == os.SEEK_CUR:
self.total_read += n
elif whence == os.SEEK_END:
self.total_read = self.size + n
io.FileIO.seek(self, n, whence)
def read_block(self): def read_block(self):
''' '''
Reads in a block of data from the target file. Reads in a block of data from the target file.
......
...@@ -4,7 +4,7 @@ import binwalk.common ...@@ -4,7 +4,7 @@ import binwalk.common
class Display(object): class Display(object):
BUFFER_WIDTH = 32 BUFFER_WIDTH = 0
HEADER_WIDTH = 115 HEADER_WIDTH = 115
MAX_LINE_LEN = 0 MAX_LINE_LEN = 0
DEFAULT_FORMAT = "%s\n" DEFAULT_FORMAT = "%s\n"
...@@ -46,7 +46,8 @@ class Display(object): ...@@ -46,7 +46,8 @@ class Display(object):
def _fprint(self, fmt, columns, csv=True): def _fprint(self, fmt, columns, csv=True):
if not self.quiet: if not self.quiet:
sys.stdout.write(fmt % tuple(columns)) line = fmt % tuple(columns)
sys.stdout.write(self._format_line(line.strip()) + "\n")
if not (self.csv and not csv): if not (self.csv and not csv):
self.log(fmt, columns) self.log(fmt, columns)
...@@ -92,7 +93,7 @@ class Display(object): ...@@ -92,7 +93,7 @@ class Display(object):
offset += space_offset offset += space_offset
self._append_to_data_parts(line, offset, offset+len(data[offset:])) self._append_to_data_parts(line, offset, offset+len(line[offset:]))
return delim.join(self.string_parts) return delim.join(self.string_parts)
......
...@@ -18,6 +18,13 @@ class Signature(binwalk.module.Module): ...@@ -18,6 +18,13 @@ class Signature(binwalk.module.Module):
long='signature', long='signature',
kwargs={'enabled' : True}, kwargs={'enabled' : True},
description='Scan target file(s) for file signatures'), description='Scan target file(s) for file signatures'),
binwalk.module.ModuleOption(short='m',
long='magic',
nargs=1,
kwargs={'magic_files' : []},
type=[],
dtype=str,
description='Specify a custom magic file to use'),
] ]
KWARGS = [ KWARGS = [
...@@ -25,17 +32,16 @@ class Signature(binwalk.module.Module): ...@@ -25,17 +32,16 @@ class Signature(binwalk.module.Module):
binwalk.module.ModuleKwarg(name='magic_files', default=[]), binwalk.module.ModuleKwarg(name='magic_files', default=[]),
] ]
HEADER="BINWALK" HEADER = "BINWALK"
HEADER_FORMAT="%s\n" HEADER_FORMAT = "%s\n"
RESULT=["offset", "offset", "description"] RESULT = ["offset", "offset", "description"]
RESULT_FORMAT="%d 0x%X %s\n" 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 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): def init(self):
# Instantiate the config class so we can access file/directory paths # Instantiate the config class so we can access file/directory paths
self.conf = binwalk.config.Config() self.conf = binwalk.config.Config()
self.config = None
# Create SmartSignature and MagicParser class instances. These are mostly for internal use. # Create SmartSignature and MagicParser class instances. These are mostly for internal use.
self.filter = binwalk.filter.MagicFilter() self.filter = binwalk.filter.MagicFilter()
...@@ -43,7 +49,7 @@ class Signature(binwalk.module.Module): ...@@ -43,7 +49,7 @@ class Signature(binwalk.module.Module):
self.parser = binwalk.parser.MagicParser(self.filter, self.smart) self.parser = binwalk.parser.MagicParser(self.filter, self.smart)
# Use the system default magic file if no other was specified # Use the system default magic file if no other was specified
if not self.magic_files or self.magic_files is None: if not self.magic_files:
# Append the user's magic file first so that those signatures take precedence # Append the user's magic file first so that those signatures take precedence
self.magic_files = [ self.magic_files = [
self.conf.paths['user'][self.conf.BINWALK_MAGIC_FILE], self.conf.paths['user'][self.conf.BINWALK_MAGIC_FILE],
...@@ -54,22 +60,39 @@ class Signature(binwalk.module.Module): ...@@ -54,22 +60,39 @@ class Signature(binwalk.module.Module):
self.mfile = self.parser.parse(self.magic_files) self.mfile = self.parser.parse(self.magic_files)
self.magic = magic.open(self.MAGIC_FLAGS) self.magic = magic.open(self.MAGIC_FLAGS)
self.magic.load(str2bytes(self.mfile)) self.magic.load(str2bytes(self.mfile))
# Once the temporary magic file is loaded into libmagic, we don't need it anymore; delete the temp file # Once the temporary magic file is loaded into libmagic, we don't need it anymore; delete the temp file
self.parser.rm_magic_file() self.parser.rm_magic_file()
def scan_file(self, fp): def scan_file(self, fp):
data = fp.read() while True:
current_block_offset = 0
(data, dlen) = fp.read_block()
if not data:
break
block_start = fp.tell() - dlen
for candidate_offset in self.parser.find_signature_candidates(data, dlen):
if candidate_offset < current_block_offset:
continue
for candidate in self.parser.find_signature_candidates(data, len(data)):
# In python3 we need a bytes object to pass to magic.buffer # In python3 we need a bytes object to pass to magic.buffer
candidate_data = str2bytes(data[candidate:candidate+fp.MAX_TRAILING_SIZE]) candidate_data = str2bytes(data[candidate_offset:candidate_offset+fp.MAX_TRAILING_SIZE])
# Pass the data to libmagic, and split out multiple results into a list # Pass the data to libmagic, and split out multiple results into a list
for magic_result in self.parser.split(self.magic.buffer(candidate_data)): magic_result = self.magic.buffer(candidate_data)
# TODO: Should filter process other validations? Reported size, for example?
if not self.filter.invalid(magic_result): if not self.filter.invalid(magic_result):
# The smart filter parser returns a dictionary of keyword values and the signature description. # The smart filter parser returns a dictionary of keyword values and the signature description.
smart = self.smart.parse(magic_result) smart = self.smart.parse(magic_result)
self.result(description=smart['description'], offset=candidate) self.result(description=smart['description'], offset=block_start+candidate_offset)
if smart['jump'] > 0:
fp.seek(block_start + candidate_offset + smart['jump'])
current_block_offset = smart['jump']
def run(self): def run(self):
for fp in self.config.target_files: for fp in self.config.target_files:
......
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