Skip to content

Commit

Permalink
Merge pull request #877 from onekey-sec/update-lief
Browse files Browse the repository at this point in the history
chore(deps): upgrade lief to 0.14.1
  • Loading branch information
vlaci authored Jul 15, 2024
2 parents 2df62ff + 08f498c commit 112da43
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 36 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ python-magic = "^0.4.27"
pyperscan = "^0.3.0"
lark = "^1.1.8"
lz4 = "^4.3.2"
lief = "^0.12.3"
lief = "^0.14.1"
cryptography = ">=41.0,<43.0"
treelib = "^1.7.0"
unblob-native = "^0.1.1"
Expand Down
33 changes: 17 additions & 16 deletions unblob/handlers/executable/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def extract(self, inpath: Path, outdir: Path):
# however we want to keep carved out ELF files, as they are the interesting stuff!
elf = lief.ELF.parse(str(inpath))

if elf is None:
logger.error(
"Trying to extract an invalid ELF file.", inpath=inpath, outdir=outdir
)
return

is_kernel = (
elf.header.file_type == lief.ELF.E_TYPE.EXECUTABLE
and elf.has_section(KERNEL_INIT_DATA_SECTION)
Expand Down Expand Up @@ -132,21 +138,13 @@ class _ELFBase(StructHandler):
SECTION_HEADER_STRUCT = "elf_shdr_t"
PROGRAM_HEADER_STRUCT = "elf_phdr_t"

@staticmethod
def _check_field(field, value):
# LIEF uses pybind11 where Enum lookup always finds a value, but unknown values are returned as '???'
# https://github.com/pybind/pybind11/blob/68a0b2dfd8cb3f5ac1846f22b6a8d0d539cb493c/include/pybind11/pybind11.h#L1907
# we need to validate if the matched value is indeed a valid value
if field(value).name not in field.__members__:
raise ValueError

def is_valid_header(self, header: Instance) -> bool:
# check that header fields have valid values
try:
self._check_field(lief.ELF.E_TYPE, header.e_type)
self._check_field(lief.ELF.ARCH, header.e_machine)
self._check_field(lief.ELF.VERSION, header.e_version)
except ValueError:
lief.ELF.E_TYPE(header.e_type)
lief.ELF.ARCH(header.e_machine)
lief.ELF.VERSION(header.e_version)
except RuntimeError:
return False
return True

Expand All @@ -167,10 +165,13 @@ def get_last_section_end(
self.SECTION_HEADER_STRUCT, file, endian
)

if (
lief.ELF.SECTION_TYPES(section_header.sh_type)
== lief.ELF.SECTION_TYPES.NOBITS
):
try:
if (
lief.ELF.SECTION_TYPES(section_header.sh_type)
== lief.ELF.SECTION_TYPES.NOBITS
):
continue
except RuntimeError:
continue

section_end = section_header.sh_offset + section_header.sh_size
Expand Down

0 comments on commit 112da43

Please sign in to comment.