|
3 | 3 | import json |
4 | 4 | import logging |
5 | 5 | import string |
| 6 | +import warnings |
6 | 7 | from difflib import SequenceMatcher |
7 | 8 | from pathlib import Path |
8 | 9 | from typing import TYPE_CHECKING, Dict, Iterable, List, Optional |
|
14 | 15 | from analysis.plugin import AnalysisPluginV0, Tag |
15 | 16 | from helperFunctions.tag import TagColor |
16 | 17 |
|
17 | | -FUNCTION_MATCHING_THRESHOLD = 0.85 |
18 | | - |
19 | 18 | if TYPE_CHECKING: |
20 | 19 | from io import FileIO |
21 | 20 |
|
| 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 |
22 | 26 | TEMPLATE_FILE_PATH = Path(__file__).parent.parent / 'internal/matching_template.json' |
23 | 27 | BEHAVIOUR_CLASSES = json.loads(TEMPLATE_FILE_PATH.read_text()) |
24 | 28 | PRINTABLE_BYTES = set(string.printable.encode()) |
@@ -87,7 +91,8 @@ def from_lief_section(cls, section: lief.ELF.Section) -> ElfSection: |
87 | 91 | flags=[f.name for f in section.flags_list if isinstance(f.name, str)], |
88 | 92 | name=section.name, |
89 | 93 | 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), |
91 | 96 | offset=section.offset, |
92 | 97 | virtual_address=section.virtual_address, |
93 | 98 | ) |
@@ -125,7 +130,8 @@ class DynamicEntry(BaseModel): |
125 | 130 | @classmethod |
126 | 131 | def from_lief_dyn_entry(cls, entry: lief.ELF.DynamicEntry) -> DynamicEntry: |
127 | 132 | 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), |
129 | 135 | value=entry.value, |
130 | 136 | library=getattr(entry, 'name', None), |
131 | 137 | flags=[f.name for f in entry.flags] if hasattr(entry, 'flags') else None, |
@@ -160,7 +166,7 @@ def __init__(self): |
160 | 166 | metadata = self.MetaData( |
161 | 167 | name='elf_analysis', |
162 | 168 | description='Analyzes and tags ELF executables and libraries', |
163 | | - version=Version(1, 0, 0), |
| 169 | + version=Version(1, 0, 1), |
164 | 170 | Schema=self.Schema, |
165 | 171 | mime_whitelist=[ |
166 | 172 | 'application/x-executable', |
@@ -236,7 +242,7 @@ def _get_color_codes(behavior_class: str) -> str: |
236 | 242 | return TagColor.GRAY |
237 | 243 |
|
238 | 244 |
|
239 | | -def _get_behavior_classes(elf: lief.ELF) -> list[str]: |
| 245 | +def _get_behavior_classes(elf: lief.ELF.Binary) -> list[str]: |
240 | 246 | libraries = _get_symbols_version_entries([str(s) for s in elf.symbols_version]) |
241 | 247 | libraries.extend([str(lib) for lib in elf.libraries]) |
242 | 248 | 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: |
288 | 294 | return None |
289 | 295 |
|
290 | 296 |
|
291 | | -def _get_note_sections_content(elf: lief.ELF) -> Iterable[InfoSectionData]: |
| 297 | +def _get_note_sections_content(elf: lief.ELF.Binary) -> Iterable[InfoSectionData]: |
292 | 298 | for section in elf.sections: # type: lief.ELF.Section |
293 | 299 | if section.type == lief.ELF.Section.TYPE.NOTE: |
294 | 300 | readable_content = bytes([c for c in section.content.tobytes() if c in PRINTABLE_BYTES]) |
|
0 commit comments