Commit 702f5ec1 by devttys0

Fixed return values of several internal extractors; python-lzma no longer…

Fixed return values of several internal extractors; python-lzma no longer required (but recommended)
parent 2418a0b4
# Base64 index tables
0 string ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= Standard base64 index table
0 string ACEGIKMOQSUWYBDFHJLNPRTVXZacegikmoqsuwybdfhjlnprtvxz0246813579=+/ SerComm base64 index table
# Basic signature scan module. This is the default (and primary) feature of binwalk. # Basic signature scan module. This is the default (and primary) feature of binwalk.
# This module does not directly use the lzma module, but some plugins for this module do.
# If the lzma import fails, this module won't be loaded at all.
import lzma
import binwalk.core.magic import binwalk.core.magic
from binwalk.core.module import Module, Option, Kwarg from binwalk.core.module import Module, Option, Kwarg
......
...@@ -70,4 +70,7 @@ class ArcadyanDeobfuscator(binwalk.core.plugin.Plugin): ...@@ -70,4 +70,7 @@ class ArcadyanDeobfuscator(binwalk.core.plugin.Plugin):
out = binwalk.core.common.BlockFile((os.path.splitext(fname)[0] + '.deobfuscated'), "wb") out = binwalk.core.common.BlockFile((os.path.splitext(fname)[0] + '.deobfuscated'), "wb")
out.write(deobfuscated) out.write(deobfuscated)
out.close() out.close()
return True
else:
return False
...@@ -19,6 +19,7 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): ...@@ -19,6 +19,7 @@ class CPIOPlugin(binwalk.core.plugin.Plugin):
cmd=self.extractor) cmd=self.extractor)
def extractor(self, fname): def extractor(self, fname):
result = None
fname = os.path.abspath(fname) fname = os.path.abspath(fname)
out_dir = os.path.join(os.path.dirname(fname), self.CPIO_OUT_DIR) out_dir = os.path.join(os.path.dirname(fname), self.CPIO_OUT_DIR)
...@@ -36,17 +37,22 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): ...@@ -36,17 +37,22 @@ class CPIOPlugin(binwalk.core.plugin.Plugin):
return return
try: try:
subprocess.call(['cpio', '-d', '-i', '--no-absolute-filenames'], result = subprocess.call(['cpio', '-d', '-i', '--no-absolute-filenames'],
stdin=fpin, stdin=fpin,
stderr=fperr, stderr=fperr,
stdout=fperr) stdout=fperr)
except OSError: except OSError:
pass result = -1
os.chdir(curdir) os.chdir(curdir)
fpin.close() fpin.close()
fperr.close() fperr.close()
if result == 0:
return True
else:
return False
def pre_scan(self): def pre_scan(self):
# Be sure to re-set this at the beginning of every scan # Be sure to re-set this at the beginning of every scan
self.found_archive = False self.found_archive = False
......
...@@ -3,21 +3,24 @@ import binwalk.core.plugin ...@@ -3,21 +3,24 @@ import binwalk.core.plugin
class LZMAExtractPlugin(binwalk.core.plugin.Plugin): class LZMAExtractPlugin(binwalk.core.plugin.Plugin):
''' '''
Gzip extractor plugin. LZMA extractor plugin.
''' '''
MODULES = ['Signature'] MODULES = ['Signature']
def init(self): def init(self):
import lzma try:
self.decompressor = lzma.decompress import lzma
self.decompressor = lzma.decompress
# If the extractor is enabled for the module we're currently loaded
# into, then register self.extractor as a zlib extraction rule. # If the extractor is enabled for the module we're currently loaded
if self.module.extractor.enabled: # into, then register self.extractor as a zlib extraction rule.
self.module.extractor.add_rule(txtrule=None, if self.module.extractor.enabled:
regex="^lzma compressed data", self.module.extractor.add_rule(txtrule=None,
extension="7z", regex="^lzma compressed data",
cmd=self.extractor) extension="7z",
cmd=self.extractor)
except ImportError as e:
pass
def extractor(self, fname): def extractor(self, fname):
fname = os.path.abspath(fname) fname = os.path.abspath(fname)
......
...@@ -22,12 +22,6 @@ class LZMAModPlugin(binwalk.core.plugin.Plugin): ...@@ -22,12 +22,6 @@ class LZMAModPlugin(binwalk.core.plugin.Plugin):
self.original_cmd = rule['cmd'] self.original_cmd = rule['cmd']
rule['cmd'] = self.lzma_cable_extractor rule['cmd'] = self.lzma_cable_extractor
break break
#rules = self.module.extractor.get_rules()
#for i in range(0, len(rules)):
# if rules[i]['regex'] and rules[i]['cmd'] and rules[i]['regex'].match(self.SIGNATURE):
# self.original_cmd = rules[i]['cmd']
# rules[i]['cmd'] = self.lzma_cable_extractor
# break
def lzma_cable_extractor(self, fname): def lzma_cable_extractor(self, fname):
# Try extracting the LZMA file without modification first # Try extracting the LZMA file without modification first
...@@ -59,7 +53,9 @@ class LZMAModPlugin(binwalk.core.plugin.Plugin): ...@@ -59,7 +53,9 @@ class LZMAModPlugin(binwalk.core.plugin.Plugin):
# Overwrite the original file so that it can be cleaned up if -r was specified # Overwrite the original file so that it can be cleaned up if -r was specified
shutil.move(out_name, fname) shutil.move(out_name, fname)
self.module.extractor.execute(self.original_cmd, fname) result = self.module.extractor.execute(self.original_cmd, fname)
return result
def scan(self, result): def scan(self, result):
# The modified cable modem LZMA headers all have valid dictionary sizes and a properties byte of 0x5D. # The modified cable modem LZMA headers all have valid dictionary sizes and a properties byte of 0x5D.
......
...@@ -16,24 +16,28 @@ class LZMAPlugin(binwalk.core.plugin.Plugin): ...@@ -16,24 +16,28 @@ class LZMAPlugin(binwalk.core.plugin.Plugin):
MAX_DATA_SIZE = 64 * 1024 MAX_DATA_SIZE = 64 * 1024
def init(self): def init(self):
import lzma try:
self.decompressor = lzma.decompress import lzma
self.decompressor = lzma.decompress
except ImportError as e:
self.decompressor = None
def is_valid_lzma(self, data): def is_valid_lzma(self, data):
valid = True valid = True
# The only acceptable exceptions are those indicating that the input data was truncated. if self.decompressor is not None:
try: # The only acceptable exceptions are those indicating that the input data was truncated.
self.decompressor(binwalk.core.compat.str2bytes(data)) try:
except IOError as e: self.decompressor(binwalk.core.compat.str2bytes(data))
# The Python2 module gives this error on truncated input data. except IOError as e:
if str(e) != "unknown BUF error": # The Python2 module gives this error on truncated input data.
valid = False if str(e) != "unknown BUF error":
except Exception as e: valid = False
# The Python3 module gives this error on truncated input data. except Exception as e:
# The inconsistency between modules is a bit worrisome. # The Python3 module gives this error on truncated input data.
if str(e) != "Compressed data ended before the end-of-stream marker was reached": # The inconsistency between modules is a bit worrisome.
valid = False if str(e) != "Compressed data ended before the end-of-stream marker was reached":
valid = False
return valid return valid
......
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