示例#1
0
    def restruct(self):
        secm = SectionManager()
        segm = SegmentManager()

        # get sections from byteList
        se = SectionExtractor(self.byteList)

        for name, body, sh in se.extract():
            secm.append(name, body, sh)
        
        # map sections to segments
        segm.mapping(secm)

        # 0x40 is ELF header size and 56 is program header size
        headerSize = 0x40 + len(segm.getSegmentList()) * 56
        body = secm.makeBody(headerSize)
        phList = segm.makePh(secm)
        secm.resetAddress(0x400000)
        # make shstrtbl
        shStrTab = secm.makeShStrSection(headerSize + len(body))

        endOfBody = headerSize + len(body + shStrTab)

        # make ELF header
        eh = Eh()
        eh.retrieve(self.byteList[0:64])    # get ELF header from origin file
        eh.set('entry_addr', secm.get('.text')['sh'].get('address'))
        eh.set('ph_offset', 0x40)
        eh.set('sh_offset', endOfBody)
        eh.set('ph_num', len(phList))
        eh.set('sh_num', len(secm.get())+1) # plus null section
        eh.set('shstrndx', secm.find('.shstrtab')+1)

        # for debug
        #eh.echo()

        # make file by restructed byteList
        byteList = eh.output()
        for ph in phList:
            byteList += ph.output()

        byteList += body
        byteList += shStrTab

        byteList += [0x00 for x in range(64)]
        for h in secm.get():
            byteList += h['sh'].output()

        p = (c_ubyte * len(byteList))()
        p[:] = byteList
        with open('elf.out', 'wb') as fp:
            fp.write(p)
示例#2
0
class SectionExtractor(object):

    def __init__(self, byteList):
        self.eh = Eh()
        self.eh.retrieve(byteList[0:64])

        self.byteList = byteList

    def extract(self):
        shSize  = self.eh.get('sh_size')
        shNum   = self.eh.get('sh_num')
        shOff   = self.eh.get('sh_offset')
        strTab  = self.retrieveStringTable()

        result = []
        for idx in range(1, shNum):

            if idx == self.eh.get('shstrndx'):
                continue

            shStart = shOff + shSize * idx

            sh = Sh()
            sh.retrieve(self.byteList[shStart:shStart+shSize])

            name = retrieveStr(strTab, sh.get('name_index'))
            body = self.byteList[sh.get('offset'):sh.get('offset')+sh.get('size')]

            if name == '.symtab' or name ==  '.strtab':
                continue

            result.append((name,  body,  sh))

        return result

    def retrieveStringTable(self):
        shSize  = self.eh.get('sh_size')
        shNum   = self.eh.get('sh_num')
        shOff   = self.eh.get('sh_offset')

        # get string table section header start position
        shStrStart  = shOff + shSize * self.eh.get('shstrndx')

        strSh = Sh()
        strSh.retrieve(self.byteList[shStrStart:shStrStart+shSize])

        strOff  = strSh.get('offset')
        strSize = strSh.get('size')
        strTab = ''.join(map(chr, self.byteList[strOff:strOff+strSize]))

        return strTab
示例#3
0
    def restruct(self):
        secm = SectionManager()
        segm = SegmentManager()
        se = SectionExtractor(self.byteList)

        for name, body, sh in se.extract():
            secm.append(name, body, sh)

        segm.mapping(secm)

        headerSize = 0x40 + len(segm.getSegmentList()) * 56
        body = secm.makeBody(headerSize)
        phList = segm.makePh(secm)
        secm.resetAddress(0x400000)
        print(hex(len(body) + headerSize))
        shStrTab = secm.makeShStrSection(headerSize + len(body))

        endOfBody = headerSize + len(body + shStrTab)

        eh = Eh()
        eh.retrieve(self.byteList[0:64])
        eh.set('entry_addr', secm.get('.text')['sh'].get('address'))
        eh.set('ph_offset', 0x40)
        eh.set('sh_offset', endOfBody)
        eh.set('ph_num', len(phList))
        eh.set('sh_num', len(secm.get()) + 1)  # plus null section
        eh.set('shstrndx', secm.find('.shstrtab') + 1)

        #eh.echo()

        byteList = eh.output()
        for ph in phList:
            byteList += ph.output()

        byteList += body
        byteList += shStrTab

        byteList += [0x00 for x in range(64)]
        for h in secm.get():
            byteList += h['sh'].output()

        p = (c_ubyte * len(byteList))()
        p[:] = byteList
        with open('elf.out', 'wb') as fp:
            fp.write(p)
示例#4
0
    def restruct(self):
        secm = SectionManager()
        segm = SegmentManager()
        se = SectionExtractor(self.byteList)

        for name, body, sh in se.extract():
            secm.append(name, body, sh)
        
        segm.mapping(secm)

        headerSize = 0x40 + len(segm.getSegmentList()) * 56
        body = secm.makeBody(headerSize)
        phList = segm.makePh(secm)
        secm.resetAddress(0x400000)
        print(hex(len(body)+headerSize))
        shStrTab = secm.makeShStrSection(headerSize + len(body))

        endOfBody = headerSize + len(body + shStrTab)

        eh = Eh()
        eh.retrieve(self.byteList[0:64])
        eh.set('entry_addr', secm.get('.text')['sh'].get('address'))
        eh.set('ph_offset', 0x40)
        eh.set('sh_offset', endOfBody)
        eh.set('ph_num', len(phList))
        eh.set('sh_num', len(secm.get())+1) # plus null section
        eh.set('shstrndx', secm.find('.shstrtab')+1)

        #eh.echo()

        byteList = eh.output()
        for ph in phList:
            byteList += ph.output()

        byteList += body
        byteList += shStrTab

        byteList += [0x00 for x in range(64)]
        for h in secm.get():
            byteList += h['sh'].output()

        p = (c_ubyte * len(byteList))()
        p[:] = byteList
        with open('elf.out', 'wb') as fp:
            fp.write(p)
示例#5
0
from elf.components.headers.Eh import Eh
from elf.components.headers.Sh import Sh
from elf.components.Section import Section
from elf.components.SectionAggregator import SectionAggregator
from elf.components.SegmentMaker import SegmentMaker

import sys
from ctypes import *

# teardown ELF file
f = open('test.out')
byteList = map(lambda x: int(ord(x)), f.read())

# get ELF header
eh = Eh()
eh.retrieve(byteList[0:64])

shSize = eh.get('sh_size')
shNum = eh.get('sh_num')
shOff = eh.get('sh_offset')

# get sh string table
shStrStart = shOff + shSize * eh.get('shstrndx')

strSh = Sh()
strSh.retrieve(byteList[shStrStart:shStrStart+shSize])

strOff = strSh.get('offset')
strSize= strSh.get('size')
strTab = ''.join(map(chr, byteList[strOff:strOff+strSize]))