Skip to content

Commit d5466a8

Browse files
jstuckeeuwint
authored andcommitted
fix: fix lief section.type and tag parsing errors
1 parent 1e9ca48 commit d5466a8

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import string
6+
import warnings
67
from difflib import SequenceMatcher
78
from pathlib import Path
89
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional
@@ -14,11 +15,14 @@
1415
from analysis.plugin import AnalysisPluginV0, Tag
1516
from helperFunctions.tag import TagColor
1617

17-
FUNCTION_MATCHING_THRESHOLD = 0.85
18-
1918
if TYPE_CHECKING:
2019
from io import FileIO
2120

21+
# disable lief logging in cases where it cannot parse sections types or tags
22+
warnings.filterwarnings('ignore', message='.*is not a valid TYPE.*')
23+
warnings.filterwarnings('ignore', message='.*is not a valid TAG.*')
24+
25+
FUNCTION_MATCHING_THRESHOLD = 0.85
2226
TEMPLATE_FILE_PATH = Path(__file__).parent.parent / 'internal/matching_template.json'
2327
BEHAVIOUR_CLASSES = json.loads(TEMPLATE_FILE_PATH.read_text())
2428
PRINTABLE_BYTES = set(string.printable.encode())
@@ -87,7 +91,8 @@ def from_lief_section(cls, section: lief.ELF.Section) -> ElfSection:
8791
flags=[f.name for f in section.flags_list if isinstance(f.name, str)],
8892
name=section.name,
8993
size=section.size,
90-
type=section.type.name,
94+
# if lief section type resolution fails, section.type will be an int
95+
type=section.type.name if not isinstance(section.type, int) else str(section.type),
9196
offset=section.offset,
9297
virtual_address=section.virtual_address,
9398
)
@@ -125,7 +130,8 @@ class DynamicEntry(BaseModel):
125130
@classmethod
126131
def from_lief_dyn_entry(cls, entry: lief.ELF.DynamicEntry) -> DynamicEntry:
127132
return cls(
128-
tag=entry.tag.name,
133+
# if lief symbol flag resolution fails, entry.tag will be an int
134+
tag=entry.tag.name if not isinstance(entry.tag, int) else str(entry.tag),
129135
value=entry.value,
130136
library=getattr(entry, 'name', None),
131137
flags=[f.name for f in entry.flags] if hasattr(entry, 'flags') else None,
@@ -160,7 +166,7 @@ def __init__(self):
160166
metadata = self.MetaData(
161167
name='elf_analysis',
162168
description='Analyzes and tags ELF executables and libraries',
163-
version=Version(1, 0, 0),
169+
version=Version(1, 0, 1),
164170
Schema=self.Schema,
165171
mime_whitelist=[
166172
'application/x-executable',
@@ -236,7 +242,7 @@ def _get_color_codes(behavior_class: str) -> str:
236242
return TagColor.GRAY
237243

238244

239-
def _get_behavior_classes(elf: lief.ELF) -> list[str]:
245+
def _get_behavior_classes(elf: lief.ELF.Binary) -> list[str]:
240246
libraries = _get_symbols_version_entries([str(s) for s in elf.symbols_version])
241247
libraries.extend([str(lib) for lib in elf.libraries])
242248
functions = _get_relevant_imp_functions([f.name for f in elf.imported_functions])
@@ -288,7 +294,7 @@ def _get_modinfo(elf: lief.ELF.Binary) -> dict[str, str] | None:
288294
return None
289295

290296

291-
def _get_note_sections_content(elf: lief.ELF) -> Iterable[InfoSectionData]:
297+
def _get_note_sections_content(elf: lief.ELF.Binary) -> Iterable[InfoSectionData]:
292298
for section in elf.sections: # type: lief.ELF.Section
293299
if section.type == lief.ELF.Section.TYPE.NOTE:
294300
readable_content = bytes([c for c in section.content.tobytes() if c in PRINTABLE_BYTES])

0 commit comments

Comments
 (0)