Commit d9423b66 by devttys0

Fixed extractor bug with --swap enabled

parent 2384691e
...@@ -532,8 +532,18 @@ class Extractor(Module): ...@@ -532,8 +532,18 @@ class Extractor(Module):
fname = unique_file_name(bname, extension) fname = unique_file_name(bname, extension)
try: try:
# If byte swapping is enabled, we need to start reading at a swap-size
# aligned offset, then index in to the read data appropriately.
if self.config.swap_size:
adjust = offset % self.config.swap_size
else:
adjust = 0
offset -= adjust
# Open the target file and seek to the offset # Open the target file and seek to the offset
fdin = self.config.open_file(file_name, length=size, offset=offset) fdin = self.config.open_file(file_name)
fdin.seek(offset)
# Open the output file # Open the output file
try: try:
...@@ -550,8 +560,9 @@ class Extractor(Module): ...@@ -550,8 +560,9 @@ class Extractor(Module):
if not data: if not data:
break break
else: else:
fdout.write(str2bytes(data[:dlen])) fdout.write(str2bytes(data[adjust:dlen]))
total_size += dlen total_size += (dlen-adjust)
adjust = 0
# Cleanup # Cleanup
fdout.close() fdout.close()
......
...@@ -37,3 +37,4 @@ class ZLIBExtractPlugin(binwalk.core.plugin.Plugin): ...@@ -37,3 +37,4 @@ class ZLIBExtractPlugin(binwalk.core.plugin.Plugin):
return False return False
return True return True
...@@ -14,9 +14,20 @@ class ZlibValidPlugin(binwalk.core.plugin.Plugin): ...@@ -14,9 +14,20 @@ class ZlibValidPlugin(binwalk.core.plugin.Plugin):
def scan(self, result): def scan(self, result):
# If this result is a zlib signature match, try to decompress the data # If this result is a zlib signature match, try to decompress the data
if result.file and result.description.lower().startswith('zlib'): if result.file and result.description.lower().startswith('zlib'):
# If byte swapping is enabled, we need to start reading at a swap-size
# aligned offset, then index in to the read data appropriately.
if self.module.config.swap_size:
adjust = result.offset % self.module.config.swap_size
else:
adjust = 0
offset = result.offset - adjust
# Seek to and read the suspected zlib data # Seek to and read the suspected zlib data
fd = self.module.config.open_file(result.file.name, offset=result.offset, length=self.MAX_DATA_SIZE) fd = self.module.config.open_file(result.file.name)
data = fd.read(self.MAX_DATA_SIZE) fd.seek(offset)
data = fd.read(self.MAX_DATA_SIZE)[adjust:]
fd.close() fd.close()
# Check if this is valid zlib data. It is valid if: # Check if this is valid zlib data. It is valid if:
...@@ -29,3 +40,4 @@ class ZlibValidPlugin(binwalk.core.plugin.Plugin): ...@@ -29,3 +40,4 @@ class ZlibValidPlugin(binwalk.core.plugin.Plugin):
# Error -5, incomplete or truncated data input # Error -5, incomplete or truncated data input
if not str(e).startswith("Error -5"): if not str(e).startswith("Error -5"):
result.valid = False result.valid = 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