diff --git a/src/binwalk/magic/code b/src/binwalk/magic/code new file mode 100644 index 0000000..2cfdb19 --- /dev/null +++ b/src/binwalk/magic/code @@ -0,0 +1,3 @@ +# Base64 index tables +0 string ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= Standard base64 index table +0 string ACEGIKMOQSUWYBDFHJLNPRTVXZacegikmoqsuwybdfhjlnprtvxz0246813579=+/ SerComm base64 index table diff --git a/src/binwalk/modules/signature.py b/src/binwalk/modules/signature.py index 78fbe8f..03fbda1 100644 --- a/src/binwalk/modules/signature.py +++ b/src/binwalk/modules/signature.py @@ -1,8 +1,4 @@ # 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 from binwalk.core.module import Module, Option, Kwarg diff --git a/src/binwalk/plugins/arcadyan.py b/src/binwalk/plugins/arcadyan.py index 957cf4d..84bb46f 100644 --- a/src/binwalk/plugins/arcadyan.py +++ b/src/binwalk/plugins/arcadyan.py @@ -70,4 +70,7 @@ class ArcadyanDeobfuscator(binwalk.core.plugin.Plugin): out = binwalk.core.common.BlockFile((os.path.splitext(fname)[0] + '.deobfuscated'), "wb") out.write(deobfuscated) out.close() + return True + else: + return False diff --git a/src/binwalk/plugins/cpio.py b/src/binwalk/plugins/cpio.py index 0c1b8a1..83ef234 100644 --- a/src/binwalk/plugins/cpio.py +++ b/src/binwalk/plugins/cpio.py @@ -19,6 +19,7 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): cmd=self.extractor) def extractor(self, fname): + result = None fname = os.path.abspath(fname) out_dir = os.path.join(os.path.dirname(fname), self.CPIO_OUT_DIR) @@ -36,17 +37,22 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): return try: - subprocess.call(['cpio', '-d', '-i', '--no-absolute-filenames'], - stdin=fpin, - stderr=fperr, - stdout=fperr) + result = subprocess.call(['cpio', '-d', '-i', '--no-absolute-filenames'], + stdin=fpin, + stderr=fperr, + stdout=fperr) except OSError: - pass + result = -1 os.chdir(curdir) fpin.close() fperr.close() + if result == 0: + return True + else: + return False + def pre_scan(self): # Be sure to re-set this at the beginning of every scan self.found_archive = False diff --git a/src/binwalk/plugins/lzmaextract.py b/src/binwalk/plugins/lzmaextract.py index 82b438a..4c6aafd 100644 --- a/src/binwalk/plugins/lzmaextract.py +++ b/src/binwalk/plugins/lzmaextract.py @@ -3,21 +3,24 @@ import binwalk.core.plugin class LZMAExtractPlugin(binwalk.core.plugin.Plugin): ''' - Gzip extractor plugin. + LZMA extractor plugin. ''' MODULES = ['Signature'] def init(self): - 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 self.module.extractor.enabled: - self.module.extractor.add_rule(txtrule=None, - regex="^lzma compressed data", - extension="7z", - cmd=self.extractor) + try: + 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 self.module.extractor.enabled: + self.module.extractor.add_rule(txtrule=None, + regex="^lzma compressed data", + extension="7z", + cmd=self.extractor) + except ImportError as e: + pass def extractor(self, fname): fname = os.path.abspath(fname) diff --git a/src/binwalk/plugins/lzmamod.py b/src/binwalk/plugins/lzmamod.py index a573b92..447443c 100644 --- a/src/binwalk/plugins/lzmamod.py +++ b/src/binwalk/plugins/lzmamod.py @@ -22,12 +22,6 @@ class LZMAModPlugin(binwalk.core.plugin.Plugin): self.original_cmd = rule['cmd'] rule['cmd'] = self.lzma_cable_extractor 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): # Try extracting the LZMA file without modification first @@ -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 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): # The modified cable modem LZMA headers all have valid dictionary sizes and a properties byte of 0x5D. diff --git a/src/binwalk/plugins/lzmavalid.py b/src/binwalk/plugins/lzmavalid.py index 8c10b65..a343656 100644 --- a/src/binwalk/plugins/lzmavalid.py +++ b/src/binwalk/plugins/lzmavalid.py @@ -16,24 +16,28 @@ class LZMAPlugin(binwalk.core.plugin.Plugin): MAX_DATA_SIZE = 64 * 1024 def init(self): - import lzma - self.decompressor = lzma.decompress + try: + import lzma + self.decompressor = lzma.decompress + except ImportError as e: + self.decompressor = None def is_valid_lzma(self, data): valid = True - # The only acceptable exceptions are those indicating that the input data was truncated. - try: - self.decompressor(binwalk.core.compat.str2bytes(data)) - except IOError as e: - # The Python2 module gives this error on truncated input data. - if str(e) != "unknown BUF error": - valid = False - except Exception as e: - # The Python3 module gives this error on truncated input data. - # The inconsistency between modules is a bit worrisome. - if str(e) != "Compressed data ended before the end-of-stream marker was reached": - valid = False + if self.decompressor is not None: + # The only acceptable exceptions are those indicating that the input data was truncated. + try: + self.decompressor(binwalk.core.compat.str2bytes(data)) + except IOError as e: + # The Python2 module gives this error on truncated input data. + if str(e) != "unknown BUF error": + valid = False + except Exception as e: + # The Python3 module gives this error on truncated input data. + # The inconsistency between modules is a bit worrisome. + if str(e) != "Compressed data ended before the end-of-stream marker was reached": + valid = False return valid