Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for dissect.executable.pe #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dissect/executable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dissect.executable.elf import ELF
from dissect.executable.pe import PE

__all__ = [
"ELF",
"PE",
]
41 changes: 32 additions & 9 deletions dissect/executable/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def __init__(self, fh: BinaryIO):
self.header = self.c_elf.Ehdr(fh)
self.segments = SegmentTable.from_elf(self)
self.section_table = SectionTable.from_elf(self)
self.symbol_tables: list[SymbolTable] = self.section_table.by_type([SHT.SYMTAB, SHT.DYNSYM])
self.symbol_tables: list[SymbolTable] = self.section_table.by_type(
[SHT.SYMTAB, SHT.DYNSYM]
)

def __repr__(self) -> str:
return str(self.header)
Expand Down Expand Up @@ -98,7 +100,9 @@ def find(self, condition: Callable, **kwargs) -> list[T]:


class Section:
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
def __init__(
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
):
self.fh = fh
self.idx = idx

Expand Down Expand Up @@ -224,7 +228,9 @@ def dump_data(self) -> list[tuple[int, bytes]]:


class Segment:
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
def __init__(
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
):
self.fh = fh
self.idx = idx
self.c_elf = c_elf
Expand All @@ -246,7 +252,9 @@ def __repr__(self) -> str:
return repr(self.header)

@classmethod
def from_segment_table(cls, table: SegmentTable, idx: Optional[int] = None) -> Segment:
def from_segment_table(
cls, table: SegmentTable, idx: Optional[int] = None
) -> Segment:
fh = table.fh
return cls(fh, idx, table.c_elf)

Expand Down Expand Up @@ -277,7 +285,14 @@ def patch(self, new_data: bytes) -> None:


class SegmentTable(Table[Segment]):
def __init__(self, fh: BinaryIO, offset: int, entries: int, size: int, c_elf: cstruct = c_elf_64):
def __init__(
self,
fh: BinaryIO,
offset: int,
entries: int,
size: int,
c_elf: cstruct = c_elf_64,
):
super().__init__(entries)
self.fh = fh
self.offset = offset
Expand All @@ -297,7 +312,9 @@ def from_elf(cls, elf: ELF) -> SegmentTable:
offset = header.e_phoff
entries = header.e_phnum
size = header.e_phentsize
return cls(fh=elf.fh, offset=offset, entries=entries, size=size, c_elf=elf.c_elf)
return cls(
fh=elf.fh, offset=offset, entries=entries, size=size, c_elf=elf.c_elf
)

def related_segments(self, section: Section) -> list[Segment]:
return self.find(lambda x: x.is_related(section))
Expand All @@ -318,7 +335,9 @@ def dump_table(self) -> tuple[int, bytearray]:


class StringTable(Section):
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
def __init__(
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
):
super().__init__(fh, idx, c_elf)

self._get_string = lru_cache(256)(self._get_string)
Expand All @@ -333,7 +352,9 @@ def _get_string(self, index: int) -> str:


class Symbol:
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
def __init__(
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
):
self.symbol = c_elf.Sym(fh)
self.idx = idx
self.c_elf = c_elf
Expand Down Expand Up @@ -388,7 +409,9 @@ def value_based_on_shndx(self, table: SectionTable) -> int:


class SymbolTable(Section, Table[Symbol]):
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
def __init__(
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
):
# Initializes Section info
Section.__init__(self, fh, idx, c_elf)
count = self.size // self.entry_size
Expand Down
25 changes: 25 additions & 0 deletions dissect/executable/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,28 @@ class Error(Exception):

class InvalidSignatureError(Error):
"""Exception that occurs if the magic in the header does not match."""


class InvalidPE(Error):
"""Exception that occurs if the PE signature does not match."""


class InvalidVA(Error):
"""Exception that occurs when a virtual address is not found within the PE sections."""


class InvalidAddress(Error):
"""Exception that occurs when a raw address is not found within the PE file when translating from a virtual
address."""


class InvalidArchitecture(Error):
"""Exception that occurs when an invalid value is encountered for the PE architecture types."""


class BuildSectionException(Error):
"""Exception that occurs when the section to be build contains an error."""


class ResourceException(Error):
"""Exception that occurs when an error is thrown parsing the resources."""
25 changes: 25 additions & 0 deletions dissect/executable/pe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dissect.executable.pe.helpers.builder import Builder
from dissect.executable.pe.helpers.exports import ExportFunction, ExportManager
from dissect.executable.pe.helpers.imports import (
ImportFunction,
ImportManager,
ImportModule,
)
from dissect.executable.pe.helpers.patcher import Patcher
from dissect.executable.pe.helpers.resources import Resource, ResourceManager
from dissect.executable.pe.helpers.sections import PESection
from dissect.executable.pe.pe import PE

__all__ = [
"Builder",
"ExportFunction",
"ExportManager",
"ImportFunction",
"ImportManager",
"ImportModule",
"Patcher",
"PE",
"PESection",
"Resource",
"ResourceManager",
]
Loading