PyMISP/pymisp/tools/create_misp_object.py

70 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
from __future__ import annotations
2017-07-21 18:47:10 +02:00
2024-01-31 12:15:08 +01:00
import logging
2020-01-23 10:27:40 +01:00
from io import BytesIO
2024-01-31 15:20:31 +01:00
from typing import Any, TYPE_CHECKING
2017-08-30 12:47:32 +02:00
from ..exceptions import MISPObjectException
from . import FileObject
logger = logging.getLogger('pymisp')
2017-07-21 18:47:10 +02:00
try:
2023-04-19 10:47:41 +02:00
import lief
2024-01-22 13:45:25 +01:00
import lief.logging
lief.logging.disable()
2017-07-21 18:47:10 +02:00
HAS_LIEF = True
2020-02-07 11:51:44 +01:00
from .peobject import make_pe_objects
from .elfobject import make_elf_objects
from .machoobject import make_macho_objects
except AttributeError:
HAS_LIEF = False
logger.critical('You need lief >= 0.11.0. The quick and dirty fix is: pip3 install --force pymisp[fileobjects]')
2017-07-21 18:47:10 +02:00
except ImportError:
HAS_LIEF = False
2024-01-31 15:20:31 +01:00
if TYPE_CHECKING:
2024-02-01 14:56:57 +01:00
from . import PEObject, ELFObject, MachOObject, PESectionObject, ELFSectionObject, MachOSectionObject
2024-01-31 15:20:31 +01:00
2017-07-21 18:47:10 +02:00
class FileTypeNotImplemented(MISPObjectException):
pass
2024-01-31 12:15:08 +01:00
def make_binary_objects(filepath: str | None = None,
pseudofile: BytesIO | bytes | None = None,
filename: str | None = None,
standalone: bool = True,
default_attributes_parameters: dict[str, Any] = {}) -> tuple[FileObject, PEObject | ELFObject | MachOObject | None, list[PESectionObject] | list[ELFSectionObject] | list[MachOSectionObject]]:
2017-12-12 17:34:09 +01:00
misp_file = FileObject(filepath=filepath, pseudofile=pseudofile, filename=filename,
2017-12-20 14:27:31 +01:00
standalone=standalone, default_attributes_parameters=default_attributes_parameters)
2024-01-22 13:45:25 +01:00
if HAS_LIEF and (filepath or pseudofile):
2023-04-19 10:47:41 +02:00
if filepath:
lief_parsed = lief.parse(filepath=filepath)
2024-01-22 13:45:25 +01:00
elif pseudofile:
if isinstance(pseudofile, bytes):
lief_parsed = lief.parse(raw=pseudofile)
else: # BytesIO
lief_parsed = lief.parse(obj=pseudofile)
2023-04-19 10:47:41 +02:00
else:
logger.critical('You need either a filepath, or a pseudofile and a filename.')
lief_parsed = None
2023-05-12 11:58:38 +02:00
if isinstance(lief_parsed, lief.lief_errors):
logger.warning('Got an error parsing the file: {lief_parsed}')
elif isinstance(lief_parsed, lief.PE.Binary):
return make_pe_objects(lief_parsed, misp_file, standalone, default_attributes_parameters)
elif isinstance(lief_parsed, lief.ELF.Binary):
return make_elf_objects(lief_parsed, misp_file, standalone, default_attributes_parameters)
elif isinstance(lief_parsed, lief.MachO.Binary):
return make_macho_objects(lief_parsed, misp_file, standalone, default_attributes_parameters)
else:
logger.critical(f'Unexpected type from lief: {type(lief_parsed)}')
if not HAS_LIEF:
logger.warning('Please install lief, documentation here: https://github.com/lief-project/LIEF')
return misp_file, None, []