Commit 11ad600c by devttys0

Added gzip and lzma extractor plugins

parent db3018c8
...@@ -15,7 +15,12 @@ ...@@ -15,7 +15,12 @@
# o cpio # o cpio
# o jffs2 # o jffs2
# o Raw LZMA/deflate streams # o Raw LZMA/deflate streams
# #
# There are also alternative extractors for the following file formats, implemented as plugins:
#
# o gzip
# o lzma
#
################################################################################################################# #################################################################################################################
# Assumes these utilities are installed in $PATH. # Assumes these utilities are installed in $PATH.
......
import os
import gzip
import binwalk.core.plugin
class GzipExtractPlugin(binwalk.core.plugin.Plugin):
'''
Gzip extractor plugin.
'''
MODULES = ['Signature']
BLOCK_SIZE = 10 * 1024
def init(self):
# 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="^gzip compressed data",
extension="gz",
cmd=self.extractor)
def extractor(self, fname):
fname = os.path.abspath(fname)
outfile = os.path.splitext(fname)[0]
try:
fpout = open(outfile, "wb")
gz = gzip.GzipFile(fname, "rb")
while True:
data = gz.read(self.BLOCK_SIZE)
if data:
fpout.write(data)
else:
break
gz.close()
fpout.close()
except KeyboardInterrupt as e:
raise e
except Exception as e:
return False
return True
import os
import lzma
import binwalk.core.plugin
class LZMAExtractPlugin(binwalk.core.plugin.Plugin):
'''
Gzip extractor plugin.
'''
MODULES = ['Signature']
def init(self):
# 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)
def extractor(self, fname):
fname = os.path.abspath(fname)
outfile = os.path.splitext(fname)[0]
try:
fpin = open(fname, "rb")
compressed = fpin.read()
fpin.close()
decompressed = lzma.decompress(compressed)
print ("Decompressed %d bytes" % len(decompressed))
fpout = open(outfile, "wb")
fpout.write(decompressed)
fpout.close()
except KeyboardInterrupt as e:
raise e
except Exception as e:
return False
return True
...@@ -31,6 +31,8 @@ class ZLIBExtractPlugin(binwalk.core.plugin.Plugin): ...@@ -31,6 +31,8 @@ class ZLIBExtractPlugin(binwalk.core.plugin.Plugin):
fpin.close() fpin.close()
fpout.close() fpout.close()
except KeyboardInterrupt as e:
raise e
except Exception as e: except Exception as e:
return False return False
......
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