Skip to content

Commit 1e9ca48

Browse files
jstuckeeuwint
authored andcommitted
fix: updated lief to fix attribute error when parsing ELF
1 parent 9f98049 commit 1e9ca48

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/install/requirements_common.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ testresources==2.0.1
44
# General python dependencies
55
docker~=7.1.0
66
pytest-rerunfailures~=14.0
7-
lief~=0.16.2
7+
lief~=0.17.0
88
psutil~=6.1.1
99
psycopg2-binary~=2.9.10
1010
pylint~=3.2.7

src/plugins/analysis/elf_analysis/code/elf_analysis.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
import logging
45
import string
56
from difflib import SequenceMatcher
67
from pathlib import Path
@@ -52,17 +53,17 @@ class ElfHeader(BaseModel):
5253
def from_lief_header(cls, header: lief.ELF.Header) -> ElfHeader:
5354
return cls(
5455
entrypoint=header.entrypoint,
55-
file_type=header.file_type.__name__,
56+
file_type=header.file_type.name,
5657
header_size=header.header_size,
5758
identity_abi_version=header.identity_abi_version,
58-
identity_class=header.identity_class.__name__,
59-
identity_data=header.identity_data.__name__,
60-
identity_os_abi=header.identity_os_abi.__name__,
61-
identity_version=header.identity_version.__name__,
62-
machine_type=header.machine_type.__name__.lower(),
59+
identity_class=header.identity_class.name,
60+
identity_data=header.identity_data.name,
61+
identity_os_abi=header.identity_os_abi.name,
62+
identity_version=header.identity_version.name,
63+
machine_type=header.machine_type.name.lower(),
6364
numberof_sections=header.numberof_sections,
6465
numberof_segments=header.numberof_segments,
65-
object_file_version=header.object_file_version.__name__,
66+
object_file_version=header.object_file_version.name,
6667
processor_flag=header.processor_flag,
6768
program_header_size=header.program_header_size,
6869
program_headers_offset=header.program_header_offset,
@@ -83,10 +84,10 @@ class ElfSection(BaseModel):
8384
@classmethod
8485
def from_lief_section(cls, section: lief.ELF.Section) -> ElfSection:
8586
return cls(
86-
flags=[f.__name__ for f in section.flags_list],
87+
flags=[f.name for f in section.flags_list if isinstance(f.name, str)],
8788
name=section.name,
8889
size=section.size,
89-
type=section.type.__name__,
90+
type=section.type.name,
9091
offset=section.offset,
9192
virtual_address=section.virtual_address,
9293
)
@@ -108,7 +109,7 @@ def from_lief_segment(cls, segment: lief.ELF.Segment) -> ElfSegment:
108109
flags=[ELF_SEGMENT_FLAGS.get(segment.flags.value, 'None')],
109110
physical_address=segment.physical_address,
110111
physical_size=segment.physical_size,
111-
type=segment.type.__name__,
112+
type=segment.type.name,
112113
virtual_address=segment.virtual_address,
113114
virtual_size=segment.virtual_size,
114115
)
@@ -124,10 +125,10 @@ class DynamicEntry(BaseModel):
124125
@classmethod
125126
def from_lief_dyn_entry(cls, entry: lief.ELF.DynamicEntry) -> DynamicEntry:
126127
return cls(
127-
tag=entry.tag.__name__,
128+
tag=entry.tag.name,
128129
value=entry.value,
129130
library=getattr(entry, 'name', None),
130-
flags=[f.__name__ for f in entry.flags] if hasattr(entry, 'flags') else None,
131+
flags=[f.name for f in entry.flags] if hasattr(entry, 'flags') else None,
131132
array=[str(i) for i in entry.array] if hasattr(entry, 'array') else None,
132133
)
133134

@@ -183,7 +184,7 @@ def analyze(self, file_handle: FileIO, virtual_file_path: str, analyses: dict) -
183184
imported_functions=[
184185
ElfSymbol(name=name, offset=address) for address, name in _deduplicate_functions(elf.imported_functions)
185186
],
186-
sections=[ElfSection.from_lief_section(s) for s in elf.sections],
187+
sections=self._get_sections(elf),
187188
segments=[ElfSegment.from_lief_segment(s) for s in elf.segments],
188189
dynamic_entries=[DynamicEntry.from_lief_dyn_entry(e) for e in elf.dynamic_entries],
189190
libraries=elf.libraries,
@@ -192,6 +193,16 @@ def analyze(self, file_handle: FileIO, virtual_file_path: str, analyses: dict) -
192193
behavior_classes=_get_behavior_classes(elf),
193194
)
194195

196+
def _get_sections(self, elf: lief.ELF.Binary) -> list[ElfSection]:
197+
sections = []
198+
for sec in elf.sections:
199+
try:
200+
sections.append(ElfSection.from_lief_section(sec))
201+
except Exception as error:
202+
logging.exception(f'[{self.metadata.name}]: section {sec} could not be parsed: {error}')
203+
continue
204+
return sections
205+
195206
def summarize(self, result: Schema) -> list[str]:
196207
keys = ['sections', 'dynamic_entries', 'exported_functions', 'imported_functions', 'note_sections', 'mod_info']
197208
return [k for k, v in result.model_dump().items() if k in keys and v]
@@ -265,7 +276,7 @@ def _behaviour_class_applies(functions: list[str], libraries: list[str], indicat
265276
return False
266277

267278

268-
def _get_modinfo(elf: lief.ELF) -> dict[str, str] | None:
279+
def _get_modinfo(elf: lief.ELF.Binary) -> dict[str, str] | None:
269280
# getting the information from the *.ko files .modinfo section
270281
for section in elf.sections:
271282
if section.name == '.modinfo':

0 commit comments

Comments
 (0)