From 11353f8ae2b2733d655360e501a8cfebf2573ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Fri, 7 Feb 2020 11:51:44 +0100 Subject: [PATCH] fix: Make lief optional again fix #538 --- pymisp/tools/__init__.py | 11 +++++++--- pymisp/tools/create_misp_object.py | 34 ++++++------------------------ pymisp/tools/elfobject.py | 22 +++++++++++-------- pymisp/tools/machoobject.py | 23 +++++++++++--------- pymisp/tools/peobject.py | 21 +++++++++++------- setup.py | 4 ++-- 6 files changed, 55 insertions(+), 60 deletions(-) diff --git a/pymisp/tools/__init__.py b/pymisp/tools/__init__.py index 4aba8ac..9503adc 100644 --- a/pymisp/tools/__init__.py +++ b/pymisp/tools/__init__.py @@ -1,9 +1,6 @@ from .vtreportobject import VTReportObject # noqa from .neo4j import Neo4j # noqa from .fileobject import FileObject # noqa -from .peobject import PEObject, PESectionObject # noqa -from .elfobject import ELFObject, ELFSectionObject # noqa -from .machoobject import MachOObject, MachOSectionObject # noqa from .create_misp_object import make_binary_objects # noqa from .abstractgenerator import AbstractMISPObjectGenerator # noqa from .genericgenerator import GenericObjectGenerator # noqa @@ -24,3 +21,11 @@ try: except ImportError: # Requires faup, which is a bit difficult to install pass + +try: + from .peobject import PEObject, PESectionObject # noqa + from .elfobject import ELFObject, ELFSectionObject # noqa + from .machoobject import MachOObject, MachOSectionObject # noqa +except ImportError: + # Requires lief, which is a bit difficult to install + pass diff --git a/pymisp/tools/create_misp_object.py b/pymisp/tools/create_misp_object.py index f7a6eee..5eb05e0 100644 --- a/pymisp/tools/create_misp_object.py +++ b/pymisp/tools/create_misp_object.py @@ -4,7 +4,7 @@ import sys from io import BytesIO -from . import FileObject, PEObject, ELFObject, MachOObject +from . import FileObject from ..exceptions import MISPObjectException import logging from typing import Optional @@ -16,6 +16,11 @@ try: from lief import Logger # type: ignore Logger.disable() HAS_LIEF = True + + from .peobject import make_pe_objects + from .elfobject import make_elf_objects + from .machoobject import make_macho_objects + except ImportError: HAS_LIEF = False @@ -24,33 +29,6 @@ class FileTypeNotImplemented(MISPObjectException): pass -def make_pe_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool=True, default_attributes_parameters: dict={}): - pe_object = PEObject(parsed=lief_parsed, standalone=standalone, default_attributes_parameters=default_attributes_parameters) - misp_file.add_reference(pe_object.uuid, 'includes', 'PE indicators') - pe_sections = [] - for s in pe_object.sections: - pe_sections.append(s) - return misp_file, pe_object, pe_sections - - -def make_elf_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool=True, default_attributes_parameters: dict={}): - elf_object = ELFObject(parsed=lief_parsed, standalone=standalone, default_attributes_parameters=default_attributes_parameters) - misp_file.add_reference(elf_object.uuid, 'includes', 'ELF indicators') - elf_sections = [] - for s in elf_object.sections: - elf_sections.append(s) - return misp_file, elf_object, elf_sections - - -def make_macho_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool=True, default_attributes_parameters: dict={}): - macho_object = MachOObject(parsed=lief_parsed, standalone=standalone, default_attributes_parameters=default_attributes_parameters) - misp_file.add_reference(macho_object.uuid, 'includes', 'MachO indicators') - macho_sections = [] - for s in macho_object.sections: - macho_sections.append(s) - return misp_file, macho_object, macho_sections - - def make_binary_objects(filepath: Optional[str]=None, pseudofile: Optional[BytesIO]=None, filename: Optional[str]=None, standalone: bool=True, default_attributes_parameters: dict={}): misp_file = FileObject(filepath=filepath, pseudofile=pseudofile, filename=filename, standalone=standalone, default_attributes_parameters=default_attributes_parameters) diff --git a/pymisp/tools/elfobject.py b/pymisp/tools/elfobject.py index 210b806..f23b797 100644 --- a/pymisp/tools/elfobject.py +++ b/pymisp/tools/elfobject.py @@ -8,14 +8,9 @@ from hashlib import md5, sha1, sha256, sha512 import logging from typing import Union from pathlib import Path +from . import FileObject -logger = logging.getLogger('pymisp') - -try: - import lief # type: ignore - HAS_LIEF = True -except ImportError: - HAS_LIEF = False +import lief # type: ignore try: import pydeep # type: ignore @@ -23,6 +18,17 @@ try: except ImportError: HAS_PYDEEP = False +logger = logging.getLogger('pymisp') + + +def make_elf_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool=True, default_attributes_parameters: dict={}): + elf_object = ELFObject(parsed=lief_parsed, standalone=standalone, default_attributes_parameters=default_attributes_parameters) + misp_file.add_reference(elf_object.uuid, 'includes', 'ELF indicators') + elf_sections = [] + for s in elf_object.sections: + elf_sections.append(s) + return misp_file, elf_object, elf_sections + class ELFObject(AbstractMISPObjectGenerator): @@ -30,8 +36,6 @@ class ELFObject(AbstractMISPObjectGenerator): super(ELFObject, self).__init__('elf', standalone=standalone, **kwargs) if not HAS_PYDEEP: logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git") - if not HAS_LIEF: - raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF') if pseudofile: if isinstance(pseudofile, BytesIO): self.__elf = lief.ELF.parse(raw=pseudofile.getvalue()) diff --git a/pymisp/tools/machoobject.py b/pymisp/tools/machoobject.py index a3b3ae3..88a2f07 100644 --- a/pymisp/tools/machoobject.py +++ b/pymisp/tools/machoobject.py @@ -8,15 +8,9 @@ from hashlib import md5, sha1, sha256, sha512 import logging from typing import Optional, Union from pathlib import Path +from . import FileObject -logger = logging.getLogger('pymisp') - - -try: - import lief # type: ignore - HAS_LIEF = True -except ImportError: - HAS_LIEF = False +import lief # type: ignore try: import pydeep # type: ignore @@ -24,6 +18,17 @@ try: except ImportError: HAS_PYDEEP = False +logger = logging.getLogger('pymisp') + + +def make_macho_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool=True, default_attributes_parameters: dict={}): + macho_object = MachOObject(parsed=lief_parsed, standalone=standalone, default_attributes_parameters=default_attributes_parameters) + misp_file.add_reference(macho_object.uuid, 'includes', 'MachO indicators') + macho_sections = [] + for s in macho_object.sections: + macho_sections.append(s) + return misp_file, macho_object, macho_sections + class MachOObject(AbstractMISPObjectGenerator): @@ -33,8 +38,6 @@ class MachOObject(AbstractMISPObjectGenerator): super(MachOObject, self).__init__('macho', standalone=standalone, **kwargs) if not HAS_PYDEEP: logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git") - if not HAS_LIEF: - raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF') if pseudofile: if isinstance(pseudofile, BytesIO): self.__macho = lief.MachO.parse(raw=pseudofile.getvalue()) diff --git a/pymisp/tools/peobject.py b/pymisp/tools/peobject.py index aa22a19..e592d8c 100644 --- a/pymisp/tools/peobject.py +++ b/pymisp/tools/peobject.py @@ -10,13 +10,9 @@ import logging from typing import Optional, Union from pathlib import Path -logger = logging.getLogger('pymisp') +from . import FileObject -try: - import lief # type: ignore - HAS_LIEF = True -except ImportError: - HAS_LIEF = False +import lief # type: ignore try: import pydeep # type: ignore @@ -24,6 +20,17 @@ try: except ImportError: HAS_PYDEEP = False +logger = logging.getLogger('pymisp') + + +def make_pe_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool=True, default_attributes_parameters: dict={}): + pe_object = PEObject(parsed=lief_parsed, standalone=standalone, default_attributes_parameters=default_attributes_parameters) + misp_file.add_reference(pe_object.uuid, 'includes', 'PE indicators') + pe_sections = [] + for s in pe_object.sections: + pe_sections.append(s) + return misp_file, pe_object, pe_sections + class PEObject(AbstractMISPObjectGenerator): @@ -33,8 +40,6 @@ class PEObject(AbstractMISPObjectGenerator): super(PEObject, self).__init__('pe', standalone=standalone, **kwargs) if not HAS_PYDEEP: logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git") - if not HAS_LIEF: - raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF') if pseudofile: if isinstance(pseudofile, BytesIO): self.__pe = lief.PE.parse(raw=pseudofile.getvalue()) diff --git a/setup.py b/setup.py index 507afd2..0d87810 100644 --- a/setup.py +++ b/setup.py @@ -38,8 +38,8 @@ setup( 'Topic :: Security', 'Topic :: Internet', ], - install_requires=['six', 'requests', 'python-dateutil', 'jsonschema', 'deprecated', 'lief>=0.10.1'], - extras_require={'fileobjects': ['python-magic', 'pydeep'], + install_requires=['six', 'requests', 'python-dateutil', 'jsonschema', 'deprecated'], + extras_require={'fileobjects': ['python-magic', 'pydeep', 'lief>=0.10.1'], 'neo': ['py2neo'], 'openioc': ['beautifulsoup4'], 'virustotal': ['validators'],