pwnypack.elf – ELF file parsing

This module contains a parser for, and methods to extract information from ELF files.

class pwnypack.elf.ELF(f=None)

Bases: pwnypack.target.Target

A parser for ELF files. Upon parsing the ELF headers, it will not only fill the ELF specific fields but will also populate the inherited arch, bits and endian properties based on the values it encounters.

Parameters:f (str, file or None) – The (path to) the ELF file to parse.

Example

>>> from pwny import *
>>> e = ELF('my-executable')
>>> print(e.machine)
>>> print(e.program_headers)
>>> print(e.section_headers)
>>> print(e.symbols)
class DynamicSectionEntry(type_id, value)

Bases: object

Contains information about the entry in the .dynamic section.

Parameters:
  • type_id (int) – The type id of the .dynamic section entry.
  • value (int) – The value of the .dynamic section entry.
class Flags

Bases: enum.IntEnum

Flags when type is flags.

class ELF.DynamicSectionEntry.Flags_1

Bases: enum.IntEnum

Flags when type is flags_1.

class ELF.DynamicSectionEntry.Posflags_1

Bases: enum.IntEnum

Flags when type is ELF.DynamicSectionEntry.Type.posflags_1.

class ELF.DynamicSectionEntry.Type

Bases: enum.IntEnum

Describes the dynamic section entry type.

ELF.DynamicSectionEntry.type = None
ELF.DynamicSectionEntry.type_id = None
ELF.DynamicSectionEntry.value = None
class ELF.Machine

Bases: enum.IntEnum

The target machine architecture.

class ELF.OSABI

Bases: enum.IntEnum

Describes the OS- or ABI-specific ELF extensions used by this file.

class ELF.ProgramHeader(elf, data)

Bases: object

Describes how the loader will load a part of a file. Called by the ELF class.

Parameters:
  • elf (ELF) – The ELF instance owning this program header.
  • data – The content of the program header entry.
class Flags

Bases: enum.IntEnum

The individual flags that make up ELF.ProgramHeader.flags.

class ELF.ProgramHeader.Type

Bases: enum.IntEnum

The segment type.

ELF.ProgramHeader.align = None
ELF.ProgramHeader.filesz = None
ELF.ProgramHeader.flags = None
ELF.ProgramHeader.memsz = None
ELF.ProgramHeader.offset = None
ELF.ProgramHeader.paddr = None
ELF.ProgramHeader.type = None
ELF.ProgramHeader.type_id = None
ELF.ProgramHeader.vaddr = None
class ELF.SectionHeader(elf, data)

Bases: object

Describes a section of an ELF file. Called by the ELF class.

Parameters:
  • elf (ELF) – The ELF instance owning this section header.
  • data – The content of the section header entry.
class Flags

Bases: enum.IntEnum

class ELF.SectionHeader.Type

Bases: enum.IntEnum

Describes the section’s type

ELF.SectionHeader.addr = None
ELF.SectionHeader.addralign = None
ELF.SectionHeader.content

The contents of this section.

ELF.SectionHeader.elf = None
ELF.SectionHeader.entsize = None
ELF.SectionHeader.flags = None
ELF.SectionHeader.info = None
ELF.SectionHeader.name = None
ELF.SectionHeader.name_index = None
ELF.SectionHeader.offset = None
ELF.SectionHeader.size = None
ELF.SectionHeader.type = None
ELF.SectionHeader.type_id = None
class ELF.Symbol(elf, data, strs)

Bases: object

Contains information about symbols. Called by the ELF class.

Parameters:
  • elf (ELF) – The ELF instance owning this symbol.
  • data – The content of the symbol definition.
  • strs – The content of the string section associated with the symbol table.
class Binding

Bases: enum.IntEnum

Describes a symbol’s binding.

class ELF.Symbol.SpecialSection

Bases: enum.IntEnum

Special section types.

class ELF.Symbol.Type

Bases: enum.IntEnum

Describes the symbol’s type.

class ELF.Symbol.Visibility

Bases: enum.IntEnum

Describes the symbol’s visibility.

ELF.Symbol.content

The contents of a symbol.

Raises:TypeError – If the symbol isn’t defined until runtime.
ELF.Symbol.elf = None
ELF.Symbol.info = None
ELF.Symbol.name = None
ELF.Symbol.name_index = None
ELF.Symbol.other = None
ELF.Symbol.shndx = None
ELF.Symbol.size = None
ELF.Symbol.type = None
ELF.Symbol.type_id = None
ELF.Symbol.value = None
ELF.Symbol.visibility = None
class ELF.Type

Bases: enum.IntEnum

Describes the object type.

ELF.abi_version = None
ELF.dynamic_section_entries

A list of entries in the .dynamic section.

ELF.entry = None
ELF.f = None
ELF.flags = None
ELF.get_dynamic_section_entry(index)

Get a specific .dynamic section entry by index.

Parameters:symbol (int) – The index of the .dynamic section entry to return.
Returns:The .dynamic section entry.
Return type:ELF.DynamicSectionEntry
Raises:KeyError – The requested entry does not exist.
ELF.get_program_header(index)

Return a specific program header by its index.

Parameters:index (int) – The program header index.
Returns:~ELF.ProgramHeader: The program header.
Return type::class
Raises:KeyError – The specified index does not exist.
ELF.get_section_header(section)

Get a specific section header by index or name.

Parameters:section (int or str) – The index or name of the section header to return.
Returns:~ELF.SectionHeader: The section header.
Return type::class
Raises:KeyError – The requested section header does not exist.
ELF.get_symbol(symbol)

Get a specific symbol by index or name.

Parameters:symbol (int or str) – The index or name of the symbol to return.
Returns:The symbol.
Return type:ELF.Symbol
Raises:KeyError – The requested symbol does not exist.
ELF.hsize = None
ELF.machine = None
ELF.osabi = None
ELF.parse_file(f)

Parse an ELF file and fill the class’ properties.

Parameters:f (file or str) – The (path to) the ELF file to read.
ELF.phentsize = None
ELF.phnum = None
ELF.phoff = None
ELF.program_headers

A list of all program headers.

ELF.section_headers

Return the list of section headers.

ELF.shentsize = None
ELF.shnum = None
ELF.shoff = None
ELF.shstrndx = None
ELF.symbols

Return a list of all symbols.

ELF.type = None