def lzma_cable_extractor(self, fname): # Try extracting the LZMA file without modification first result = self.module.extractor.execute(self.original_cmd, fname) # If the external extractor was successul (True) or didn't exist (None), don't do anything. if result not in [True, None]: out_name = os.path.splitext(fname)[0] + '-patched' + os.path.splitext(fname)[1] fp_out = BlockFile(out_name, 'w') # Use self.module.config.open_file here to ensure that other config settings (such as byte-swapping) are honored fp_in = self.module.config.open_file(fname, offset=0, length=0) fp_in.set_block_size(peek=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) result = self.module.extractor.execute(self.original_cmd, fname) return result
def lzma_cable_extractor(self, fname): # Try extracting the LZMA file without modification first result = self.module.extractor.execute(self.original_cmd, fname) # If the external extractor was successul (True) or didn't exist (None), don't do anything. if result not in [True, None]: out_name = os.path.splitext( fname)[0] + '-patched' + os.path.splitext(fname)[1] fp_out = BlockFile(out_name, 'w') # Use self.module.config.open_file here to ensure that other config settings (such as byte-swapping) are honored fp_in = self.module.config.open_file(fname, offset=0, length=0) fp_in.set_block_size(peek=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.module.extractor.execute(self.original_cmd, fname)
def lzma_cable_extractor(self, fname): result = False out_name = os.path.splitext(fname)[0] + '-patched' + os.path.splitext(fname)[1] fp_out = BlockFile(out_name, 'w') # Use self.module.config.open_file here to ensure that other config # settings (such as byte-swapping) are honored fp_in = self.module.config.open_file(fname, offset=0, length=0) fp_in.set_block_size(peek=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() for rule in self.module.extractor.get_rules(self.SIGNATURE): if rule['cmd'] != self.lzma_cable_extractor: result = self.module.extractor.execute(rule['cmd'], out_name) if result == True: break os.remove(out_name) return result
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 = self.config.open_file(file_name, length=size, offset=offset) # Open the output file try: fdout = BlockFile(fname, 'w') except KeyboardInterrupt as e: raise e 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() if not data: break else: fdout.write(str2bytes(data[:dlen])) total_size += dlen # Cleanup fdout.close() fdin.close() except KeyboardInterrupt as e: raise e except Exception as e: raise Exception( "Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e))) return 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 = self.config.open_file(file_name, length=size, offset=offset) # Open the output file try: fdout = BlockFile(fname, 'w') except KeyboardInterrupt as e: raise e 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() if not data: break else: fdout.write(str2bytes(data[:dlen])) total_size += dlen # Cleanup fdout.close() fdin.close() except KeyboardInterrupt as e: raise e except Exception as e: raise Exception("Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e))) binwalk.core.common.debug("Carved data block 0x%X - 0x%X from '%s' to '%s'" % (offset, offset+size, file_name, fname)) return 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 <displayed hex offset>.<extension> default_bname = "%X" % (offset + self.config.base) 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: # 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 fdin = self.config.open_file(file_name) fdin.seek(offset) # Open the output file try: fdout = BlockFile(fname, 'w') except KeyboardInterrupt as e: raise e 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() if not data: break else: total_size += (dlen-adjust) if total_size > size: dlen -= (total_size - size) fdout.write(str2bytes(data[adjust:dlen])) adjust = 0 # Cleanup fdout.close() fdin.close() except KeyboardInterrupt as e: raise e except Exception as e: raise Exception("Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e))) binwalk.core.common.debug("Carved data block 0x%X - 0x%X from '%s' to '%s'" % (offset, offset+size, file_name, fname)) return 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 <displayed hex offset>.<extension> default_bname = "%X" % (offset + self.config.base) 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: # 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 fdin = self.config.open_file(file_name) fdin.seek(offset) # Open the output file try: fdout = BlockFile(fname, 'w') except KeyboardInterrupt as e: raise e 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() if dlen < 1: break else: total_size += (dlen - adjust) if total_size > size: dlen -= (total_size - size) fdout.write(str2bytes(data[adjust:dlen])) adjust = 0 # Cleanup fdout.close() fdin.close() except KeyboardInterrupt as e: raise e except Exception as e: raise Exception("Extractor.dd failed to extract data from '%s' to '%s': %s" % (file_name, fname, str(e))) binwalk.core.common.debug("Carved data block 0x%X - 0x%X from '%s' to '%s'" % (offset, offset + size, file_name, fname)) return fname