Skip to content

Instantly share code, notes, and snippets.

@warabanshi
Created March 7, 2013 16:20
Show Gist options
  • Select an option

  • Save warabanshi/5109238 to your computer and use it in GitHub Desktop.

Select an option

Save warabanshi/5109238 to your computer and use it in GitHub Desktop.
from elf.Utils import *
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
# 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]))
# get sections
secAggr = SectionAggregator()
for idx in range(1, shNum):
if idx == eh.get('shstrndx'):
continue
shStart = shOff + shSize * idx
sh = Sh()
sh.retrieve(byteList[shStart:shStart+shSize])
name = retrieveStr(strTab, sh.get('name_index'))
body = byteList[sh.get('offset'):sh.get('offset')+sh.get('size')]
secAggr.append(Section(body, name, sh))
secAggr.dump()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment