fix: Make lief optional again

fix #538
pull/551/head
Raphaël Vinot 2020-02-07 11:51:44 +01:00
parent 70dca1d408
commit 11353f8ae2
6 changed files with 55 additions and 60 deletions

View File

@ -1,9 +1,6 @@
from .vtreportobject import VTReportObject # noqa from .vtreportobject import VTReportObject # noqa
from .neo4j import Neo4j # noqa from .neo4j import Neo4j # noqa
from .fileobject import FileObject # 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 .create_misp_object import make_binary_objects # noqa
from .abstractgenerator import AbstractMISPObjectGenerator # noqa from .abstractgenerator import AbstractMISPObjectGenerator # noqa
from .genericgenerator import GenericObjectGenerator # noqa from .genericgenerator import GenericObjectGenerator # noqa
@ -24,3 +21,11 @@ try:
except ImportError: except ImportError:
# Requires faup, which is a bit difficult to install # Requires faup, which is a bit difficult to install
pass 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

View File

@ -4,7 +4,7 @@
import sys import sys
from io import BytesIO from io import BytesIO
from . import FileObject, PEObject, ELFObject, MachOObject from . import FileObject
from ..exceptions import MISPObjectException from ..exceptions import MISPObjectException
import logging import logging
from typing import Optional from typing import Optional
@ -16,6 +16,11 @@ try:
from lief import Logger # type: ignore from lief import Logger # type: ignore
Logger.disable() Logger.disable()
HAS_LIEF = True HAS_LIEF = True
from .peobject import make_pe_objects
from .elfobject import make_elf_objects
from .machoobject import make_macho_objects
except ImportError: except ImportError:
HAS_LIEF = False HAS_LIEF = False
@ -24,33 +29,6 @@ class FileTypeNotImplemented(MISPObjectException):
pass 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={}): 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, misp_file = FileObject(filepath=filepath, pseudofile=pseudofile, filename=filename,
standalone=standalone, default_attributes_parameters=default_attributes_parameters) standalone=standalone, default_attributes_parameters=default_attributes_parameters)

View File

@ -8,14 +8,9 @@ from hashlib import md5, sha1, sha256, sha512
import logging import logging
from typing import Union from typing import Union
from pathlib import Path from pathlib import Path
from . import FileObject
logger = logging.getLogger('pymisp') import lief # type: ignore
try:
import lief # type: ignore
HAS_LIEF = True
except ImportError:
HAS_LIEF = False
try: try:
import pydeep # type: ignore import pydeep # type: ignore
@ -23,6 +18,17 @@ try:
except ImportError: except ImportError:
HAS_PYDEEP = False 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): class ELFObject(AbstractMISPObjectGenerator):
@ -30,8 +36,6 @@ class ELFObject(AbstractMISPObjectGenerator):
super(ELFObject, self).__init__('elf', standalone=standalone, **kwargs) super(ELFObject, self).__init__('elf', standalone=standalone, **kwargs)
if not HAS_PYDEEP: if not HAS_PYDEEP:
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git") 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 pseudofile:
if isinstance(pseudofile, BytesIO): if isinstance(pseudofile, BytesIO):
self.__elf = lief.ELF.parse(raw=pseudofile.getvalue()) self.__elf = lief.ELF.parse(raw=pseudofile.getvalue())

View File

@ -8,15 +8,9 @@ from hashlib import md5, sha1, sha256, sha512
import logging import logging
from typing import Optional, Union from typing import Optional, Union
from pathlib import Path from pathlib import Path
from . import FileObject
logger = logging.getLogger('pymisp') import lief # type: ignore
try:
import lief # type: ignore
HAS_LIEF = True
except ImportError:
HAS_LIEF = False
try: try:
import pydeep # type: ignore import pydeep # type: ignore
@ -24,6 +18,17 @@ try:
except ImportError: except ImportError:
HAS_PYDEEP = False 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): class MachOObject(AbstractMISPObjectGenerator):
@ -33,8 +38,6 @@ class MachOObject(AbstractMISPObjectGenerator):
super(MachOObject, self).__init__('macho', standalone=standalone, **kwargs) super(MachOObject, self).__init__('macho', standalone=standalone, **kwargs)
if not HAS_PYDEEP: if not HAS_PYDEEP:
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git") 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 pseudofile:
if isinstance(pseudofile, BytesIO): if isinstance(pseudofile, BytesIO):
self.__macho = lief.MachO.parse(raw=pseudofile.getvalue()) self.__macho = lief.MachO.parse(raw=pseudofile.getvalue())

View File

@ -10,13 +10,9 @@ import logging
from typing import Optional, Union from typing import Optional, Union
from pathlib import Path from pathlib import Path
logger = logging.getLogger('pymisp') from . import FileObject
try: import lief # type: ignore
import lief # type: ignore
HAS_LIEF = True
except ImportError:
HAS_LIEF = False
try: try:
import pydeep # type: ignore import pydeep # type: ignore
@ -24,6 +20,17 @@ try:
except ImportError: except ImportError:
HAS_PYDEEP = False 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): class PEObject(AbstractMISPObjectGenerator):
@ -33,8 +40,6 @@ class PEObject(AbstractMISPObjectGenerator):
super(PEObject, self).__init__('pe', standalone=standalone, **kwargs) super(PEObject, self).__init__('pe', standalone=standalone, **kwargs)
if not HAS_PYDEEP: if not HAS_PYDEEP:
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git") 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 pseudofile:
if isinstance(pseudofile, BytesIO): if isinstance(pseudofile, BytesIO):
self.__pe = lief.PE.parse(raw=pseudofile.getvalue()) self.__pe = lief.PE.parse(raw=pseudofile.getvalue())

View File

@ -38,8 +38,8 @@ setup(
'Topic :: Security', 'Topic :: Security',
'Topic :: Internet', 'Topic :: Internet',
], ],
install_requires=['six', 'requests', 'python-dateutil', 'jsonschema', 'deprecated', 'lief>=0.10.1'], install_requires=['six', 'requests', 'python-dateutil', 'jsonschema', 'deprecated'],
extras_require={'fileobjects': ['python-magic', 'pydeep'], extras_require={'fileobjects': ['python-magic', 'pydeep', 'lief>=0.10.1'],
'neo': ['py2neo'], 'neo': ['py2neo'],
'openioc': ['beautifulsoup4'], 'openioc': ['beautifulsoup4'],
'virustotal': ['validators'], 'virustotal': ['validators'],