Commit 36774f18 by heffnercj

Fixed multiple python3 issues; still needs testing, more fixes remain

parent 6c89cf90
...@@ -4,18 +4,19 @@ import os ...@@ -4,18 +4,19 @@ import os
import re import re
import time import time
import magic import magic
from config import * from binwalk.compat import *
from update import * from binwalk.config import *
from filter import * from binwalk.update import *
from parser import * from binwalk.filter import *
from plugins import * from binwalk.parser import *
from hexdiff import * from binwalk.plugins import *
from entropy import * from binwalk.hexdiff import *
from extractor import * from binwalk.entropy import *
from prettyprint import * from binwalk.extractor import *
from smartstrings import * from binwalk.prettyprint import *
from smartsignature import * from binwalk.smartstrings import *
from common import file_size, unique_file_name, BlockFile from binwalk.smartsignature import *
from binwalk.common import file_size, unique_file_name, BlockFile
class Binwalk(object): class Binwalk(object):
''' '''
...@@ -238,7 +239,7 @@ class Binwalk(object): ...@@ -238,7 +239,7 @@ class Binwalk(object):
return data return data
def analyze_entropy(self, files, offset=0, length=0, block=0, plot=True, legend=True, save=False, algorithm=None, load_plugins=True, whitelist=[], blacklist=[], compcheck=False): def analyze_entropy(self, files, offset=0, length=0, block=0, plot=True, legend=True, save=False, algorithm=None, load_plugins=True, whitelist=[], blacklist=[], compcheck=False):
''' '''
Performs an entropy analysis on the specified file(s). Performs an entropy analysis on the specified file(s).
@files - A dictionary containing file names and results data, as returned by Binwalk.scan. @files - A dictionary containing file names and results data, as returned by Binwalk.scan.
......
# Common functions. # Common functions.
import io
import os import os
import re import re
from binwalk.compat import *
def file_size(filename): def file_size(filename):
''' '''
...@@ -14,7 +16,7 @@ def file_size(filename): ...@@ -14,7 +16,7 @@ def file_size(filename):
fd = os.open(filename, os.O_RDONLY) fd = os.open(filename, os.O_RDONLY)
try: try:
return os.lseek(fd, 0, os.SEEK_END) return os.lseek(fd, 0, os.SEEK_END)
except Exception, e: except Exception as e:
raise Exception("file_size failed to obtain the size of '%s': %s" % (filename, str(e))) raise Exception("file_size failed to obtain the size of '%s': %s" % (filename, str(e)))
finally: finally:
os.close(fd) os.close(fd)
...@@ -89,7 +91,7 @@ def unique_file_name(base_name, extension=''): ...@@ -89,7 +91,7 @@ def unique_file_name(base_name, extension=''):
return fname return fname
class BlockFile(file): class BlockFile(io.BufferedReader):
''' '''
Abstraction class to handle reading data from files in blocks. Abstraction class to handle reading data from files in blocks.
Necessary for large files. Necessary for large files.
...@@ -107,12 +109,11 @@ class BlockFile(file): ...@@ -107,12 +109,11 @@ class BlockFile(file):
# limit disk I/O, but small enough to limit the size of processed data blocks. # limit disk I/O, but small enough to limit the size of processed data blocks.
READ_BLOCK_SIZE = 1 * 1024 * 1024 READ_BLOCK_SIZE = 1 * 1024 * 1024
def __init__(self, fname, mode='rb', length=0, offset=0): def __init__(self, fname, length=0, offset=0):
''' '''
Class constructor. Class constructor.
@fname - Path to the file to be opened. @fname - Path to the file to be opened.
@mode - Mode to open the file in.
@length - Maximum number of bytes to read from the file via self.block_read(). @length - Maximum number of bytes to read from the file via self.block_read().
@offset - Offset at which to start reading from the file. @offset - Offset at which to start reading from the file.
...@@ -135,7 +136,7 @@ class BlockFile(file): ...@@ -135,7 +136,7 @@ class BlockFile(file):
else: else:
self.length = self.size self.length = self.size
file.__init__(self, fname, mode) io.BufferedReader.__init__(self, fname, "rb")
self.seek(self.offset) self.seek(self.offset)
......
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
# Inspired by people who actually know what they're doing: http://www.fourmilab.ch/random/ # Inspired by people who actually know what they're doing: http://www.fourmilab.ch/random/
import math import math
import common import binwalk.common as common
from binwalk.compat import *
class MonteCarloPi(object): class MonteCarloPi(object):
''' '''
......
import os import os
import common import binwalk.common as common
from binwalk.compat import *
class Config: class Config:
''' '''
......
import zlib import zlib
import math import math
import os.path import os.path
import plugins import binwalk.plugins as plugins
import common import binwalk.common as common
import compression import binwalk.compression as compression
from binwalk.compat import *
class PlotEntropy(object): class PlotEntropy(object):
''' '''
......
...@@ -4,8 +4,9 @@ import sys ...@@ -4,8 +4,9 @@ import sys
import shlex import shlex
import tempfile import tempfile
import subprocess import subprocess
from config import * from binwalk.compat import *
from common import file_size, unique_file_name, BlockFile from binwalk.config import *
from binwalk.common import file_size, unique_file_name, BlockFile
class Extractor: class Extractor:
''' '''
...@@ -177,7 +178,7 @@ class Extractor: ...@@ -177,7 +178,7 @@ class Extractor:
# Process each line from the extract file, ignoring comments # Process each line from the extract file, ignoring comments
for rule in open(fname).readlines(): for rule in open(fname).readlines():
self.add_rule(rule.split(self.COMMENT_DELIM, 1)[0]) self.add_rule(rule.split(self.COMMENT_DELIM, 1)[0])
except Exception, e: except Exception as e:
raise Exception("Extractor.load_from_file failed to load file '%s': %s" % (fname, str(e))) raise Exception("Extractor.load_from_file failed to load file '%s': %s" % (fname, str(e)))
def load_defaults(self): def load_defaults(self):
...@@ -195,7 +196,7 @@ class Extractor: ...@@ -195,7 +196,7 @@ class Extractor:
for extract_file in extract_files: for extract_file in extract_files:
try: try:
self.load_from_file(extract_file) self.load_from_file(extract_file)
except Exception, e: except Exception as e:
if self.verbose: if self.verbose:
raise Exception("Extractor.load_defaults failed to load file '%s': %s" % (extract_file, str(e))) raise Exception("Extractor.load_defaults failed to load file '%s': %s" % (extract_file, str(e)))
...@@ -433,7 +434,7 @@ class Extractor: ...@@ -433,7 +434,7 @@ class Extractor:
# Open the output file # Open the output file
try: try:
fdout = BlockFile(fname, "wb") fdout = BlockFile(fname, "wb")
except Exception, e: except Exception as e:
# Fall back to the default name if the requested name fails # Fall back to the default name if the requested name fails
fname = unique_file_name(default_bname, extension) fname = unique_file_name(default_bname, extension)
fdout = BlockFile(fname, "wb") fdout = BlockFile(fname, "wb")
...@@ -446,7 +447,7 @@ class Extractor: ...@@ -446,7 +447,7 @@ class Extractor:
# Cleanup # Cleanup
fdout.close() fdout.close()
fdin.close() fdin.close()
except Exception, e: except Exception as e:
raise Exception("Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e))) raise Exception("Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e)))
return fname return fname
...@@ -467,7 +468,7 @@ class Extractor: ...@@ -467,7 +468,7 @@ class Extractor:
if callable(cmd): if callable(cmd):
try: try:
cmd(fname) cmd(fname)
except Exception, e: except Exception as e:
sys.stderr.write("WARNING: Extractor.execute failed to run '%s': %s\n" % (str(cmd), str(e))) sys.stderr.write("WARNING: Extractor.execute failed to run '%s': %s\n" % (str(cmd), str(e)))
else: else:
# If not in verbose mode, create a temporary file to redirect stdout and stderr to # If not in verbose mode, create a temporary file to redirect stdout and stderr to
...@@ -480,7 +481,7 @@ class Extractor: ...@@ -480,7 +481,7 @@ class Extractor:
# Execute. # Execute.
if subprocess.call(shlex.split(cmd), stdout=tmp, stderr=tmp) != 0: if subprocess.call(shlex.split(cmd), stdout=tmp, stderr=tmp) != 0:
retval = False retval = False
except Exception, e: except Exception as e:
# Silently ignore no such file or directory errors. Why? Because these will inevitably be raised when # Silently ignore no such file or directory errors. Why? Because these will inevitably be raised when
# making the switch to the new firmware mod kit directory structure. We handle this elsewhere, but it's # making the switch to the new firmware mod kit directory structure. We handle this elsewhere, but it's
# annoying to see this spammed out to the console every time. # annoying to see this spammed out to the console every time.
......
import re import re
import common import binwalk.common as common
from smartsignature import SmartSignature from binwalk.smartsignature import SmartSignature
from binwalk.compat import *
class MagicFilter: class MagicFilter:
''' '''
......
...@@ -5,7 +5,8 @@ import sys ...@@ -5,7 +5,8 @@ import sys
import string import string
import curses import curses
import platform import platform
import common import binwalk.common as common
from binwalk.compat import *
class HexDiff(object): class HexDiff(object):
...@@ -83,7 +84,7 @@ class HexDiff(object): ...@@ -83,7 +84,7 @@ class HexDiff(object):
self.block_hex += c self.block_hex += c
def _simple_footer(self): def _simple_footer(self):
print "" print("")
def _header(self, files, block): def _header(self, files, block):
header = "OFFSET " header = "OFFSET "
...@@ -144,7 +145,7 @@ class HexDiff(object): ...@@ -144,7 +145,7 @@ class HexDiff(object):
for f in files: for f in files:
try: try:
c = data[f][j+i] c = data[f][j+i]
except Exception, e: except Exception as e:
c = None c = None
if c not in byte_list: if c not in byte_list:
...@@ -168,10 +169,8 @@ class HexDiff(object): ...@@ -168,10 +169,8 @@ class HexDiff(object):
for j in range(0, block): for j in range(0, block):
try: try:
#print "%s[%d]" % (f, j+i)
self._build_block("%.2X " % ord(data[f][j+i]), highlight=diff_same[j]) self._build_block("%.2X " % ord(data[f][j+i]), highlight=diff_same[j])
except Exception, e: except Exception as e:
#print str(e)
self._build_block(" ") self._build_block(" ")
if (j+1) == block: if (j+1) == block:
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
# Inspired by people who actually know what they're doing: http://www.fourmilab.ch/random/ # Inspired by people who actually know what they're doing: http://www.fourmilab.ch/random/
import math import math
from binwalk.compat import *
class MonteCarloPi(object): class MonteCarloPi(object):
''' '''
...@@ -216,18 +217,3 @@ if __name__ == "__main__": ...@@ -216,18 +217,3 @@ if __name__ == "__main__":
sys.stderr.write("Number of deviations: %d\n" % num_error) sys.stderr.write("Number of deviations: %d\n" % num_error)
sys.stderr.write("Largest deviation: %d at offset 0x%X\n" % (largest[1], largest[0])) sys.stderr.write("Largest deviation: %d at offset 0x%X\n" % (largest[1], largest[0]))
print "Data:",
if num_error != 0:
print "Compressed"
else:
print "Encrypted"
print "Confidence:",
if num_error >= 5 or num_error == 0:
print "High"
elif num_error in [3,4]:
print "Medium"
else:
print "Low"
import re import re
import os.path import os.path
import tempfile import tempfile
from common import str2int from binwalk.compat import *
from binwalk.common import str2int
class MagicParser: class MagicParser:
''' '''
...@@ -165,7 +166,7 @@ class MagicParser: ...@@ -165,7 +166,7 @@ class MagicParser:
self.fd.write(line) self.fd.write(line)
self.build_signature_set() self.build_signature_set()
except Exception, e: except Exception as e:
raise Exception("Error parsing magic file '%s' on line %d: %s" % (file_name, line_count, str(e))) raise Exception("Error parsing magic file '%s' on line %d: %s" % (file_name, line_count, str(e)))
def _parse_line(self, line): def _parse_line(self, line):
...@@ -202,14 +203,14 @@ class MagicParser: ...@@ -202,14 +203,14 @@ class MagicParser:
# The condition line may contain escaped sequences, so be sure to decode it properly. # The condition line may contain escaped sequences, so be sure to decode it properly.
entry['condition'] = line_parts[2].decode('string_escape') entry['condition'] = line_parts[2].decode('string_escape')
entry['description'] = ' '.join(line_parts[3:]) entry['description'] = ' '.join(line_parts[3:])
except Exception, e: except Exception as e:
raise Exception("%s :: %s", (str(e), line)) raise Exception("%s :: %s", (str(e), line))
# We've already verified that the first character in this line is a number, so this *shouldn't* # We've already verified that the first character in this line is a number, so this *shouldn't*
# throw an exception, but let's catch it just in case... # throw an exception, but let's catch it just in case...
try: try:
entry['offset'] = str2int(entry['offset']) entry['offset'] = str2int(entry['offset'])
except Exception, e: except Exception as e:
raise Exception("%s :: %s", (str(e), line)) raise Exception("%s :: %s", (str(e), line))
# If this is a string, get the length of the string # If this is a string, get the length of the string
...@@ -229,7 +230,7 @@ class MagicParser: ...@@ -229,7 +230,7 @@ class MagicParser:
# but needing that is rare. # but needing that is rare.
try: try:
intval = str2int(entry['condition'].strip('L')) intval = str2int(entry['condition'].strip('L'))
except Exception, e: except Exception as e:
raise Exception("Failed to evaluate condition for '%s' type: '%s', condition: '%s', error: %s" % (entry['description'], entry['type'], entry['condition'], str(e))) raise Exception("Failed to evaluate condition for '%s' type: '%s', condition: '%s', error: %s" % (entry['description'], entry['type'], entry['condition'], str(e)))
# How long is the field type? # How long is the field type?
......
import os import os
import sys import sys
import imp import imp
from binwalk.compat import *
# Valid return values for plugins # Valid return values for plugins
PLUGIN_CONTINUE = 0x00 PLUGIN_CONTINUE = 0x00
...@@ -124,7 +125,7 @@ class Plugins: ...@@ -124,7 +125,7 @@ class Plugins:
val = callback(arg) val = callback(arg)
if val is not None: if val is not None:
retval |= val retval |= val
except Exception, e: except Exception as e:
sys.stderr.write("WARNING: %s.%s failed: %s\n" % (str(callback.im_class), callback.__name__, str(e))) sys.stderr.write("WARNING: %s.%s failed: %s\n" % (str(callback.im_class), callback.__name__, str(e)))
return retval return retval
...@@ -235,7 +236,7 @@ class Plugins: ...@@ -235,7 +236,7 @@ class Plugins:
except: except:
pass pass
except Exception, e: except Exception as e:
sys.stderr.write("WARNING: Failed to load plugin module '%s': %s\n" % (module, str(e))) sys.stderr.write("WARNING: Failed to load plugin module '%s': %s\n" % (module, str(e)))
def _pre_scan_callbacks(self, fd): def _pre_scan_callbacks(self, fd):
......
from binwalk.compat import *
class Plugin: class Plugin:
''' '''
...@@ -16,6 +17,6 @@ class Plugin: ...@@ -16,6 +17,6 @@ class Plugin:
def callback(self, results): def callback(self, results):
if self.modify_output: if self.modify_output:
try: try:
print results['description'] print(results['description'])
except Exception, e: except Exception as e:
pass pass
...@@ -2,10 +2,11 @@ import sys ...@@ -2,10 +2,11 @@ import sys
import hashlib import hashlib
import csv as pycsv import csv as pycsv
from datetime import datetime from datetime import datetime
from binwalk.compat import *
class PrettyPrint: class PrettyPrint:
''' '''
Class for printing binwalk results to screen/log files. Class for printing binwalk results to screen/log files.
An instance of PrettyPrint is available via the Binwalk.display object. An instance of PrettyPrint is available via the Binwalk.display object.
The PrettyPrint.results() method is of particular interest, as it is suitable for use as a Binwalk.scan() callback function, The PrettyPrint.results() method is of particular interest, as it is suitable for use as a Binwalk.scan() callback function,
...@@ -28,7 +29,7 @@ class PrettyPrint: ...@@ -28,7 +29,7 @@ class PrettyPrint:
bw.display.header() bw.display.header()
bw.single_scan('firmware.bin', callback=bw.display.results) bw.single_scan('firmware.bin', callback=bw.display.results)
bw.display.footer() bw.display.footer()
''' '''
HEADER_WIDTH = 115 HEADER_WIDTH = 115
BUFFER_WIDTH = 32 BUFFER_WIDTH = 32
...@@ -200,7 +201,7 @@ class PrettyPrint: ...@@ -200,7 +201,7 @@ class PrettyPrint:
# Get the terminal window width # Get the terminal window width
hw = struct.unpack('hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234')) hw = struct.unpack('hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
self.HEADER_WIDTH = hw[1] self.HEADER_WIDTH = hw[1]
except Exception, e: except Exception as e:
pass pass
self.MAX_LINE_LEN = self.HEADER_WIDTH - self.BUFFER_WIDTH self.MAX_LINE_LEN = self.HEADER_WIDTH - self.BUFFER_WIDTH
......
import re import re
from common import str2int, get_quoted_strings from binwalk.compat import *
from binwalk.common import str2int, get_quoted_strings
class SmartSignature: class SmartSignature:
''' '''
......
import string import string
import entropy import binwalk.entropy as entropy
import plugins import binwalk.plugins as plugins
import common import binwalk.common as common
from binwalk.compat import *
class FileStrings(object): class FileStrings(object):
''' '''
......
import os import os
import urllib2 import urllib2
from config import * from binwalk.config import *
from binwalk.compat import *
class Update: class Update:
''' '''
...@@ -61,11 +62,11 @@ class Update: ...@@ -61,11 +62,11 @@ class Update:
try: try:
if self.verbose: if self.verbose:
print "Fetching %s..." % url print("Fetching %s..." % url)
data = urllib2.urlopen(url).read() data = urllib2.urlopen(url).read()
open(self.config.paths['system'][fname], "wb").write(data) open(self.config.paths['system'][fname], "wb").write(data)
except Exception, e: except Exception as e:
raise Exception("Update._do_update_from_svn failed to update file '%s': %s" % (url, str(e))) raise Exception("Update._do_update_from_svn failed to update file '%s': %s" % (url, str(e)))
def update_binwalk(self): def update_binwalk(self):
......
...@@ -137,24 +137,24 @@ setup( name = "binwalk", ...@@ -137,24 +137,24 @@ setup( name = "binwalk",
# This prevents python3 from being used when running binwalk. # This prevents python3 from being used when running binwalk.
# This shouldn't be done on the ./bin/binwalk file, as that would # This shouldn't be done on the ./bin/binwalk file, as that would
# cause a conflict between the master branch and the local clone. # cause a conflict between the master branch and the local clone.
python2_path = which("python2") #python2_path = which("python2")
binwalk_path = which("binwalk") #binwalk_path = which("binwalk")
if python2_path and binwalk_path: #if python2_path and binwalk_path:
i = 0 # i = 0
data = '' # data = ''
for line in open(binwalk_path, 'rb').readlines(): # for line in open(binwalk_path, 'rb').readlines():
if i == 0: # if i == 0:
line = "#!/usr/bin/env python2\n" # line = "#!/usr/bin/env python2\n"
data += str(line) # data += str(line)
i += 1 # i += 1
fd = open(binwalk_path, 'wb') # fd = open(binwalk_path, 'wb')
try: # try:
fd.write(data) # fd.write(data)
except TypeError: # except TypeError:
fd.write(bytes(data, 'UTF-8')) # fd.write(bytes(data, 'UTF-8'))
fd.close() # fd.close()
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