def __init__( self, data, debug_dir=None, debug_prefix=None, debug_suffix=None, ): # Public properties self.debug_dir = debug_dir self.debug_prefix = debug_prefix self.debug_suffix = debug_suffix # Store the original image self.store_debug_data(df.FILE_DATA_IN, data) """ Extract the various segments/sections of the data: 1. Elf header 2. Prog headers 3. Bin """ # Extract the header self.ehdr = extract_ehdr(data) self.store_debug_data(df.FILE_HDR_IN, self.ehdr.pack()) self.store_debug_data(df.FILE_HDR_IN_REPR, repr(self.ehdr), suffix=df.FILE_HDR_IN_REPR_SUFFIX) # Extract the program headers self.phdrs = extract_phdrs(data, self.ehdr) self.store_debug_data(df.FILE_PHDR_IN, pack_phdrs(self.phdrs)) self.store_debug_data(df.FILE_PHDR_IN_REPR, repr_phdrs(self.phdrs), suffix=df.FILE_PHDR_IN_REPR_SUFFIX) # Dump the individual segments self.segments = extract_segments(data, self.phdrs) for idx, phdr in enumerate(self.phdrs): length = len(self.segments[phdr]) is_load = self._segment_to_put(phdr) if length >= 0 and length <= 16: logger.debug(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) if is_load and (length > 0 and length <= 16): logger.warning(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) self.store_debug_data(df.FILE_SEGMENT_IN.format(idx), self.segments[phdr]) # Zero out the sections for now zero_out_sections(self.ehdr)
def __init__(self, data, debug_dir=None, debug_prefix=None, debug_suffix=None, ): # Public properties self.debug_dir = debug_dir self.debug_prefix = debug_prefix self.debug_suffix = debug_suffix # Store the original image self.store_debug_data(df.FILE_DATA_IN, data) """ Extract the various segments/sections of the data: 1. Elf header 2. Prog headers 3. Bin """ # Extract the header self.ehdr = extract_ehdr(data) self.store_debug_data(df.FILE_HDR_IN, self.ehdr.pack()) self.store_debug_data(df.FILE_HDR_IN_REPR, repr(self.ehdr), suffix=df.FILE_HDR_IN_REPR_SUFFIX) # Extract the program headers self.phdrs = extract_phdrs(data, self.ehdr) self.store_debug_data(df.FILE_PHDR_IN, pack_phdrs(self.phdrs)) self.store_debug_data(df.FILE_PHDR_IN_REPR, repr_phdrs(self.phdrs), suffix=df.FILE_PHDR_IN_REPR_SUFFIX) # Dump the individual segments self.segments = extract_segments(data, self.phdrs) for idx, phdr in enumerate(self.phdrs): length = len(self.segments[phdr]) is_load = self._segment_to_put(phdr) if length >= 0 and length <= 16: logger.debug(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) if is_load and (length > 0 and length <= 16): logger.warning(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) self.store_debug_data(df.FILE_SEGMENT_IN.format(idx), self.segments[phdr]) # Zero out the sections for now zero_out_sections(self.ehdr)
def __init__(self, data=None, debug_dir=None, debug_prefix=None, debug_suffix=None, _class=ELFCLASS32, delegate=None, elf_properties=None): # Create empty elf file is data is None if data is None: data = create_empty_elf(_class) # Set the delegate if delegate is None: self.delegate = ParseGenElfDelegate(self) else: self.delegate = delegate # Public properties self.debug_dir = debug_dir self.debug_prefix = debug_prefix self.debug_suffix = debug_suffix # Store the original image self.store_debug_data(df.FILE_DATA_IN, data) """ Extract the various segments/sections of the data: 1. Elf header 2. Prog headers 3. Bin """ # Extract the header self.ehdr = extract_ehdr(data) self.store_debug_data(df.FILE_HDR_IN, self.ehdr.pack()) self.store_debug_data(df.FILE_HDR_IN_REPR, repr(self.ehdr), suffix=df.FILE_HDR_IN_REPR_SUFFIX) # Extract the program headers self.phdrs = extract_phdrs(data, self.ehdr) self.store_debug_data(df.FILE_PHDR_IN, pack_phdrs(self.phdrs)) self.store_debug_data(df.FILE_PHDR_IN_REPR, repr_phdrs(self.phdrs), suffix=df.FILE_PHDR_IN_REPR_SUFFIX) # Extract the section headers if elf_properties and not elf_properties.has_sections: zero_out_sections(self.ehdr) self.shdrs = extract_shdrs(data, self.ehdr) # Sort sections by whether they are encapsulated by segments self.encapsulated_sections_map, self.non_encapsulated_sections = self.sort_sections( ) # Extract section data from section header info and dump those which are outside segments self.sections = extract_sections(data, self.non_encapsulated_sections) for idx, shdr in enumerate(self.shdrs): if shdr in self.non_encapsulated_sections: self.store_debug_data(df.FILE_SECTION_IN.format(idx), self.sections[shdr]) # Dump the individual segments self.segments = extract_segments(data, self.phdrs) for idx, phdr in enumerate(self.phdrs): length = len(self.segments[phdr]) is_load = self.delegate.segment_to_put(phdr) if 0 <= length <= 16: logger.debug(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) if is_load and 0 < length <= 16: logger.warning(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) self.store_debug_data(df.FILE_SEGMENT_IN.format(idx), self.segments[phdr])
def __init__( self, data=None, debug_dir=None, debug_prefix=None, debug_suffix=None, _class=None, delegate=None, ): # Create empty elf file is data is None if data is None: if _class is None: raise RuntimeError('Either data or class must be given.') data = create_empty_elf(_class) # Set the delegate if delegate is None: self.delegate = ParseGenElfDelegate(self) else: self.delegate = delegate # Public properties self.debug_dir = debug_dir self.debug_prefix = debug_prefix self.debug_suffix = debug_suffix # Store the original image self.store_debug_data(df.FILE_DATA_IN, data) """ Extract the various segments/sections of the data: 1. Elf header 2. Prog headers 3. Bin """ # Extract the header self.ehdr = extract_ehdr(data) self.store_debug_data(df.FILE_HDR_IN, self.ehdr.pack()) self.store_debug_data(df.FILE_HDR_IN_REPR, repr(self.ehdr), suffix=df.FILE_HDR_IN_REPR_SUFFIX) # Extract the program headers self.phdrs = extract_phdrs(data, self.ehdr) self.store_debug_data(df.FILE_PHDR_IN, pack_phdrs(self.phdrs)) self.store_debug_data(df.FILE_PHDR_IN_REPR, repr_phdrs(self.phdrs), suffix=df.FILE_PHDR_IN_REPR_SUFFIX) # Dump the individual segments self.segments = extract_segments(data, self.phdrs) for idx, phdr in enumerate(self.phdrs): length = len(self.segments[phdr]) is_load = self.delegate.segment_to_put(phdr) if length >= 0 and length <= 16: logger.debug(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) if is_load and (length > 0 and length <= 16): logger.warning(('' if is_load else 'Non-') + 'Loadable segment - ' + str(idx + 1) + ' is of size: ' + str(length)) self.store_debug_data(df.FILE_SEGMENT_IN.format(idx), self.segments[phdr]) # Zero out the sections for now zero_out_sections(self.ehdr)