Commit 36774f18 by heffnercj

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

parent 6c89cf90
......@@ -4,18 +4,19 @@ import os
import re
import time
import magic
from config import *
from update import *
from filter import *
from parser import *
from plugins import *
from hexdiff import *
from entropy import *
from extractor import *
from prettyprint import *
from smartstrings import *
from smartsignature import *
from common import file_size, unique_file_name, BlockFile
from binwalk.compat import *
from binwalk.config import *
from binwalk.update import *
from binwalk.filter import *
from binwalk.parser import *
from binwalk.plugins import *
from binwalk.hexdiff import *
from binwalk.entropy import *
from binwalk.extractor import *
from binwalk.prettyprint import *
from binwalk.smartstrings import *
from binwalk.smartsignature import *
from binwalk.common import file_size, unique_file_name, BlockFile
class Binwalk(object):
'''
......
# Common functions.
import io
import os
import re
from binwalk.compat import *
def file_size(filename):
'''
......@@ -14,7 +16,7 @@ def file_size(filename):
fd = os.open(filename, os.O_RDONLY)
try:
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)))
finally:
os.close(fd)
......@@ -89,7 +91,7 @@ def unique_file_name(base_name, extension=''):
return fname
class BlockFile(file):
class BlockFile(io.BufferedReader):
'''
Abstraction class to handle reading data from files in blocks.
Necessary for large files.
......@@ -107,12 +109,11 @@ class BlockFile(file):
# limit disk I/O, but small enough to limit the size of processed data blocks.
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.
@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().
@offset - Offset at which to start reading from the file.
......@@ -135,7 +136,7 @@ class BlockFile(file):
else:
self.length = self.size
file.__init__(self, fname, mode)
io.BufferedReader.__init__(self, fname, "rb")
self.seek(self.offset)
......
......@@ -4,7 +4,8 @@
# Inspired by people who actually know what they're doing: http://www.fourmilab.ch/random/
import math
import common
import binwalk.common as common
from binwalk.compat import *
class MonteCarloPi(object):
'''
......
import os
import common
import binwalk.common as common
from binwalk.compat import *
class Config:
'''
......
import zlib
import math
import os.path
import plugins
import common
import compression
import binwalk.plugins as plugins
import binwalk.common as common
import binwalk.compression as compression
from binwalk.compat import *
class PlotEntropy(object):
'''
......
......@@ -4,8 +4,9 @@ import sys
import shlex
import tempfile
import subprocess
from config import *
from common import file_size, unique_file_name, BlockFile
from binwalk.compat import *
from binwalk.config import *
from binwalk.common import file_size, unique_file_name, BlockFile
class Extractor:
'''
......@@ -177,7 +178,7 @@ class Extractor:
# Process each line from the extract file, ignoring comments
for rule in open(fname).readlines():
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)))
def load_defaults(self):
......@@ -195,7 +196,7 @@ class Extractor:
for extract_file in extract_files:
try:
self.load_from_file(extract_file)
except Exception, e:
except Exception as e:
if self.verbose:
raise Exception("Extractor.load_defaults failed to load file '%s': %s" % (extract_file, str(e)))
......@@ -433,7 +434,7 @@ class Extractor:
# Open the output file
try:
fdout = BlockFile(fname, "wb")
except Exception, e:
except Exception as e:
# Fall back to the default name if the requested name fails
fname = unique_file_name(default_bname, extension)
fdout = BlockFile(fname, "wb")
......@@ -446,7 +447,7 @@ class Extractor:
# Cleanup
fdout.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)))
return fname
......@@ -467,7 +468,7 @@ class Extractor:
if callable(cmd):
try:
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)))
else:
# If not in verbose mode, create a temporary file to redirect stdout and stderr to
......@@ -480,7 +481,7 @@ class Extractor:
# Execute.
if subprocess.call(shlex.split(cmd), stdout=tmp, stderr=tmp) != 0:
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
# 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.
......
import re
import common
from smartsignature import SmartSignature
import binwalk.common as common
from binwalk.smartsignature import SmartSignature
from binwalk.compat import *
class MagicFilter:
'''
......
......@@ -5,7 +5,8 @@ import sys
import string
import curses
import platform
import common
import binwalk.common as common
from binwalk.compat import *
class HexDiff(object):
......@@ -83,7 +84,7 @@ class HexDiff(object):
self.block_hex += c
def _simple_footer(self):
print ""
print("")
def _header(self, files, block):
header = "OFFSET "
......@@ -144,7 +145,7 @@ class HexDiff(object):
for f in files:
try:
c = data[f][j+i]
except Exception, e:
except Exception as e:
c = None
if c not in byte_list:
......@@ -168,10 +169,8 @@ class HexDiff(object):
for j in range(0, block):
try:
#print "%s[%d]" % (f, j+i)
self._build_block("%.2X " % ord(data[f][j+i]), highlight=diff_same[j])
except Exception, e:
#print str(e)
except Exception as e:
self._build_block(" ")
if (j+1) == block:
......
......@@ -4,6 +4,7 @@
# Inspired by people who actually know what they're doing: http://www.fourmilab.ch/random/
import math
from binwalk.compat import *
class MonteCarloPi(object):
'''
......@@ -216,18 +217,3 @@ if __name__ == "__main__":
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]))
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 os.path
import tempfile
from common import str2int
from binwalk.compat import *
from binwalk.common import str2int
class MagicParser:
'''
......@@ -165,7 +166,7 @@ class MagicParser:
self.fd.write(line)
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)))
def _parse_line(self, line):
......@@ -202,14 +203,14 @@ class MagicParser:
# The condition line may contain escaped sequences, so be sure to decode it properly.
entry['condition'] = line_parts[2].decode('string_escape')
entry['description'] = ' '.join(line_parts[3:])
except Exception, e:
except Exception as e:
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*
# throw an exception, but let's catch it just in case...
try:
entry['offset'] = str2int(entry['offset'])
except Exception, e:
except Exception as e:
raise Exception("%s :: %s", (str(e), line))
# If this is a string, get the length of the string
......@@ -229,7 +230,7 @@ class MagicParser:
# but needing that is rare.
try:
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)))
# How long is the field type?
......
import os
import sys
import imp
from binwalk.compat import *
# Valid return values for plugins
PLUGIN_CONTINUE = 0x00
......@@ -124,7 +125,7 @@ class Plugins:
val = callback(arg)
if val is not None:
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)))
return retval
......@@ -235,7 +236,7 @@ class Plugins:
except:
pass
except Exception, e:
except Exception as e:
sys.stderr.write("WARNING: Failed to load plugin module '%s': %s\n" % (module, str(e)))
def _pre_scan_callbacks(self, fd):
......
from binwalk.compat import *
class Plugin:
'''
......@@ -16,6 +17,6 @@ class Plugin:
def callback(self, results):
if self.modify_output:
try:
print results['description']
except Exception, e:
print(results['description'])
except Exception as e:
pass
......@@ -2,6 +2,7 @@ import sys
import hashlib
import csv as pycsv
from datetime import datetime
from binwalk.compat import *
class PrettyPrint:
'''
......@@ -200,7 +201,7 @@ class PrettyPrint:
# Get the terminal window width
hw = struct.unpack('hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
self.HEADER_WIDTH = hw[1]
except Exception, e:
except Exception as e:
pass
self.MAX_LINE_LEN = self.HEADER_WIDTH - self.BUFFER_WIDTH
......
import re
from common import str2int, get_quoted_strings
from binwalk.compat import *
from binwalk.common import str2int, get_quoted_strings
class SmartSignature:
'''
......
import string
import entropy
import plugins
import common
import binwalk.entropy as entropy
import binwalk.plugins as plugins
import binwalk.common as common
from binwalk.compat import *
class FileStrings(object):
'''
......
import os
import urllib2
from config import *
from binwalk.config import *
from binwalk.compat import *
class Update:
'''
......@@ -61,11 +62,11 @@ class Update:
try:
if self.verbose:
print "Fetching %s..." % url
print("Fetching %s..." % url)
data = urllib2.urlopen(url).read()
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)))
def update_binwalk(self):
......
......@@ -137,24 +137,24 @@ setup( name = "binwalk",
# This prevents python3 from being used when running binwalk.
# This shouldn't be done on the ./bin/binwalk file, as that would
# cause a conflict between the master branch and the local clone.
python2_path = which("python2")
binwalk_path = which("binwalk")
if python2_path and binwalk_path:
i = 0
data = ''
for line in open(binwalk_path, 'rb').readlines():
if i == 0:
line = "#!/usr/bin/env python2\n"
data += str(line)
i += 1
fd = open(binwalk_path, 'wb')
try:
fd.write(data)
except TypeError:
fd.write(bytes(data, 'UTF-8'))
fd.close()
#python2_path = which("python2")
#binwalk_path = which("binwalk")
#if python2_path and binwalk_path:
# i = 0
# data = ''
# for line in open(binwalk_path, 'rb').readlines():
# if i == 0:
# line = "#!/usr/bin/env python2\n"
# data += str(line)
# i += 1
# fd = open(binwalk_path, 'wb')
# try:
# fd.write(data)
# except TypeError:
# fd.write(bytes(data, 'UTF-8'))
# 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