Пример #1
0
    def load_image_fobj(self, fobj):
        """load a BinImage from an ELF file given via file object"""
        # read elf file
        reader = ELFReader()
        elf = reader.load(fobj)
        # create bin image and assign elf file
        bi = BinImage(BIN_IMAGE_TYPE_ELF)
        bi.set_file_data(elf)
        # walk through elf sections
        sect_to_seg = {}
        for sect in elf.sections:
            # determine segment type
            seg_type = None
            name = sect.name_str
            flags = 0
            if name == b".text":
                seg_type = SEGMENT_TYPE_CODE
            elif name == b".data":
                seg_type = SEGMENT_TYPE_DATA
            elif name == b".rodata":
                seg_type = SEGMENT_TYPE_DATA
                flags = SEGMENT_FLAG_READ_ONLY
            elif name == b".bss":
                seg_type = SEGMENT_TYPE_BSS
            # we got a segment
            if seg_type is not None:
                size = sect.header.size
                data = sect.data
                seg = Segment(seg_type, size, data, flags)
                bi.add_segment(seg)
                # assign section to segment
                seg.set_file_data(sect)
                sect_to_seg[sect] = seg

        # now run through segments to add relocations
        bi_segs = bi.get_segments()
        for seg in bi_segs:
            # retrieve associated ELF section
            sect = seg.get_file_data()

            # any relocations?
            rela = sect.get_rela()
            num_rela = len(rela)
            if num_rela > 0:
                self.add_elf_rela(sect, seg, sect_to_seg)

            # any symbols?
            symbols = sect.get_symbols()
            num_syms = len(symbols)
            if num_syms > 0:
                self.add_elf_symbols(symbols, seg)

        # try to add debug info
        ddl = DwarfDebugLine()
        got = ddl.decode(elf)
        if got:
            self.add_debug_line(ddl, bi, sect_to_seg)

        return bi
Пример #2
0
 def extract_bin_img(self, entry, fixes=True, patches=False):
     data, relocs = self.extract_entry(entry, fixes, patches)
     # create a bin image
     bi = BinImage(BIN_IMAGE_TYPE_HUNK)
     seg = Segment(SEGMENT_TYPE_CODE, len(data), data)
     bi.add_segment(seg)
     # create reloc for target segment
     rl = Relocations(seg)
     # add offsets
     for o in relocs:
         r = Reloc(o)
         rl.add_reloc(r)
     seg.add_reloc(seg, rl)
     # return final binary image
     return bi
Пример #3
0
    def create_image_from_load_seg_file(self, lsf):
        """create a BinImage from a HunkLoadSegFile object"""
        bi = BinImage(BIN_IMAGE_TYPE_HUNK)
        bi.set_file_data(lsf)
        segs = lsf.get_segments()
        for seg in segs:
            # what type of segment to we have?
            blk_id = seg.seg_blk.blk_id
            size = seg.size_longs * 4
            data = seg.seg_blk.data
            if blk_id == Hunk.HUNK_CODE:
                seg_type = SEGMENT_TYPE_CODE
            elif blk_id == Hunk.HUNK_DATA:
                seg_type = SEGMENT_TYPE_DATA
            elif blk_id == Hunk.HUNK_BSS:
                seg_type = SEGMENT_TYPE_BSS
            else:
                raise HunkParseError("Unknown Segment Type for BinImage: %d" %
                                     blk_id)
            # create seg
            bs = Segment(seg_type, size, data)
            bs.set_file_data(seg)
            bi.add_segment(bs)
        # add relocations if any
        bi_segs = bi.get_segments()
        for seg in bi_segs:
            # add relocations?
            hseg = seg.file_data
            reloc_blks = hseg.reloc_blks
            if reloc_blks is not None:
                for blk in reloc_blks:
                    self._add_hunk_relocs(blk, seg, bi_segs)
            # add symbol table
            symbol_blk = hseg.symbol_blk
            if symbol_blk is not None:
                self._add_hunk_symbols(symbol_blk, seg)
            # add debug infos
            debug_infos = hseg.debug_infos
            if debug_infos is not None:
                self._add_debug_infos(debug_infos, seg)

        return bi