Commit f6eea731 by heffnercj

deflate.py plugin now supports raw deflate stream extraction/decompression

parent 4989aba0
......@@ -14,7 +14,8 @@ except:
tinfl = ctypes.cdll.LoadLibrary(ctypes.util.find_library("tinfl"))
if tinfl.is_deflated(data, len(data), 1):
print "%s is zlib compressed." % (sys.argv[1])
if tinfl.is_deflated(data, len(data), 0):
print "%s is deflated." % (sys.argv[1])
print "Inflated to %d bytes!" % tinfl.inflate_raw_file(sys.argv[1], sys.argv[1] + '.inflated')
else:
print "%s is not zlib compressed." % sys.argv[1]
print "%s is not deflated." % sys.argv[1]
......@@ -603,6 +603,50 @@ int is_deflated(char *buf, size_t buf_size, int includes_zlib_header)
return 0;
}
int inflate_raw_file(char *in_file, char *out_file)
{
int retval = 0;
size_t out_size = 0, in_size = 0;
FILE *fp_in = NULL, *fp_out = NULL;
char *compressed_data = NULL, *decompressed_data = NULL;
fp_in = fopen(in_file, "rb");
if(fp_in)
{
fp_out = fopen(out_file, "wb");
if(fp_out)
{
fseek(fp_in, 0L, SEEK_END);
in_size = ftell(fp_in);
fseek(fp_in, 0L, SEEK_SET);
compressed_data = malloc(in_size);
if(compressed_data)
{
memset(compressed_data, 0, in_size);
fread(compressed_data, 1, in_size, fp_in);
decompressed_data = (char *) tinfl_decompress_mem_to_heap(compressed_data, in_size, &out_size, 0);
if(decompressed_data)
{
fwrite(decompressed_data, 1, out_size, fp_out);
free(decompressed_data);
retval = out_size;
}
free(compressed_data);
}
}
}
if(fp_in) fclose(fp_in);
if(fp_out) fclose(fp_out);
return retval;
}
#endif // #ifndef TINFL_HEADER_FILE_ONLY
/*
......@@ -631,3 +675,11 @@ int is_deflated(char *buf, size_t buf_size, int includes_zlib_header)
For more information, please refer to <http://unlicense.org/>
*/
#ifdef MAIN
int main(int argc, char *argv[])
{
printf("Inflated to %d bytes.\n", inflate_raw_file(argv[1], argv[2]));
return 0;
}
#endif
......@@ -20,34 +20,18 @@ class Plugin:
# The tinfl library is built and installed with binwalk
self.tinfl = ctypes.cdll.LoadLibrary(ctypes.util.find_library("tinfl"))
# Add an extraction rule
if self.binwalk.extractor.enabled:
# TODO: Add python extractor rule
pass
self.binwalk.extractor.add_rule(regex=self.DESCRIPTION.lower(), extension="deflate", cmd=self._extractor)
def pre_scan(self, fp):
self._deflate_scan(fp)
return PLUGIN_TERMINATE
def _extractor(self, file_name):
processed = 0
inflated_data = ''
fd = BlockFile(file_name, 'rb')
fd.READ_BLOCK_SIZE = self.SIZE
while processed < fd.length:
(data, dlen) = fd.read_block()
inflated_block = self.tinfl.inflate_block(data, dlen)
if inflated_block:
inflated_data += inflated_block
else:
break
processed += dlen
fd.close()
print "%s inflated to %d bytes" % (file_name, len(inflated_data))
if self.tinfl:
out_file = os.path.splitext(file_name)[0]
self.tinfl.inflate_raw_file(file_name, out_file)
def _deflate_scan(self, fp):
fp.MAX_TRAILING_SIZE = self.SIZE
......@@ -70,6 +54,10 @@ class Plugin:
self.binwalk.total_scanned = current_total + i
self.binwalk.display.easy_results(loc, self.DESCRIPTION)
# Extract the file
if self.binwalk.extractor.enabled:
self.binwalk.extractor.extract(loc, self.DESCRIPTION, fp.name, (fp.length - loc))
if (current_total + i) > self.binwalk.scan_length:
break
......
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