예제 #1
0
    def build_content(self):
        c = StrPatchwork()
        c[self.NThdr.sizeofheaders - 1] = pe.data_null
        c[0] = self.DOShdr.pack()

        # fix image size
        if len(self.SHList):
            s_last = self.SHList.shlist[-1]
            size = s_last.vaddr + s_last.rsize + (self.NThdr.sectionalignment -
                                                  1)
            size &= ~(self.NThdr.sectionalignment - 1)
            self.NThdr.sizeofimage = size

        # headers
        self.build_headers(c)

        # section headers
        off = self.DOShdr.lfanew \
            + self.NTsig.bytelen \
            + self.COFFhdr.bytelen \
            + self.COFFhdr.sizeofoptionalheader
        c[off] = self.SHList.pack()
        off += self.SHList.bytelen
        end_of_headers = off

        # section data
        # note that the content of directories should have been already
        # included section data, which is possible because position and
        # size of sections are known at this point
        for s in sorted(self.SHList, key=lambda _: _.scnptr):
            if s.rawsize == 0:
                continue
            if end_of_headers > s.scnptr:
                log.warning("section %s offset %#x overlap pe hdr %#x", s.name,
                            s.scnptr, off)
            elif off > s.scnptr:
                log.warning("section %s offset %#x overlap previous section",
                            s.name, s.scnptr)
            off = s.scnptr + s.rawsize
            c[s.scnptr:off] = s.section_data.data.pack()

        # symbols and strings
        if self.COFFhdr.numberofsymbols:
            self.COFFhdr.pointertosymboltable = off
            c[off] = self.Symbols.pack()
            assert self.Symbols.bytelen == 18 * self.COFFhdr.numberofsymbols
            off += self.Symbols.bytelen
            c[off] = self.SymbolStrings.pack()

        # some headers may have been updated when building sections or symbols
        self.build_headers(c)

        # final verifications
        l = self.DOShdr.lfanew + self.NTsig.bytelen + self.COFFhdr.bytelen
        if l % 4:
            log.warning("non aligned coffhdr, bad crc calculation")
        crcs = self.patch_crc(c.pack(), self.NThdr.CheckSum)
        c[l + 64] = struct.pack('I', crcs)
        return c.pack()
예제 #2
0
 def pack(self):
     c = StrPatchwork()
     c[0] = self.hdr.pack()
     of = self.hdr.bytelen
     for s in self.sections:
         c[of] = s.pack()
         of += s.bytelen
     return c.pack()
예제 #3
0
 def build_content(self):
     if self.Ehdr.shoff == 0:
         elf_set_offsets(self)
     c = StrPatchwork()
     c[0] = self.Ehdr.pack()
     c[self.Ehdr.phoff] = self.ph.pack()
     for s in self.sh:
         c[s.sh.offset] = s.pack()
     c[self.Ehdr.shoff] = self.sh.pack()
     return c.pack()
예제 #4
0
 def build_content(self):
     if self.Ehdr.shoff == 0:
         elf_set_offsets(self)
     c = StrPatchwork()
     c[0] = self.Ehdr.pack()
     c[self.Ehdr.phoff] = self.ph.pack()
     for s in self.sh:
         c[s.sh.offset] = s.pack()
     sh = self.sh.pack()
     if len(sh):
         # When 'shoff' is invalid, 'sh' is empty, but the line below
         # is very slow because strpatchwork extends the file.
         c[self.Ehdr.shoff] = sh
     return c.pack()
예제 #5
0
 class CDataInstance(CBase):
     def _initialize(self, f=f):
         self._size = f(self.parent)
         self._data = StrPatchwork()
     def unpack(self, c, o):
         self._data[0] = c[o:o+self._size]
     def pack(self):
         return self._data.pack()
     def __str__(self):
         return self.pack().decode('latin1')
     def __getitem__(self, item):
         return self._data[item]
     def __setitem__(self, item, value):
         self._data[item] = value
예제 #6
0
class LinkEditSection(BaseSection):
    type = 'data'

    def unpack(self, c, o):
        if self.parent is not None: assert o == self.offset
        self.content = StrPatchwork(c[o:o + self.size])

    def get_size(self):
        return getattr(self.parent, self.type + 'size')

    def set_size(self, val):
        setattr(self.parent, self.type + 'size', val)

    size = property(get_size, set_size)
    addr = property(lambda _: 0)

    def pack(self):
        return self.content.pack()

    def __str__(self):
        return "%-30s %-10s %#010x %#010x" % (self.__class__.__name__, '',
                                              self.offset, self.size)