def lzma_cable_extractor(self, fname): # Try extracting the LZMA file without modification first if not self.binwalk.extractor.execute(self.original_cmd, fname): out_name = os.path.splitext(fname)[0] + '-patched' + os.path.splitext(fname)[1] fp_out = BlockFile(out_name, 'w') fp_in = BlockFile(fname) fp_in.MAX_TRAILING_SIZE = 0 i = 0 while i < fp_in.length: (data, dlen) = fp_in.read_block() if i == 0: out_data = data[0:5] + self.FAKE_LZMA_SIZE + data[5:] else: out_data = data fp_out.write(out_data) i += dlen fp_in.close() fp_out.close() # Overwrite the original file so that it can be cleaned up if -r was specified shutil.move(out_name, fname) self.binwalk.extractor.execute(self.original_cmd, fname)
def _dd(self, file_name, offset, size, extension, output_file_name=None): ''' Extracts a file embedded inside the target file. @file_name - Path to the target file. @offset - Offset inside the target file where the embedded file begins. @size - Number of bytes to extract. @extension - The file exension to assign to the extracted file on disk. @output_file_name - The requested name of the output file. Returns the extracted file name. ''' total_size = 0 # Default extracted file name is <hex offset>.<extension> default_bname = "%X" % offset if self.max_size and size > self.max_size: size = self.max_size if not output_file_name or output_file_name is None: bname = default_bname else: # Strip the output file name of invalid/dangerous characters (like file paths) bname = os.path.basename(output_file_name) fname = unique_file_name(bname, extension) try: # Open the target file and seek to the offset fdin = BlockFile(file_name, 'r', length=size) fdin.seek(offset) # Open the output file try: fdout = BlockFile(fname, 'w') except Exception as e: # Fall back to the default name if the requested name fails fname = unique_file_name(default_bname, extension) fdout = BlockFile(fname, 'w') while total_size < size: (data, dlen) = fdin.read_block() fdout.write(str2bytes(data[:dlen])) total_size += dlen # Cleanup fdout.close() fdin.close() except Exception as e: raise Exception("Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e))) return fname