PyMISP/pymisp/tools/create_misp_object.py

77 lines
3.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2017-07-21 18:47:10 +02:00
# -*- coding: utf-8 -*-
2019-02-06 11:31:05 +01:00
import sys
2020-01-23 10:27:40 +01:00
from io import BytesIO
2020-02-07 11:51:44 +01:00
from . import FileObject
2017-08-30 12:47:32 +02:00
from ..exceptions import MISPObjectException
import logging
2020-01-23 10:27:40 +01:00
from typing import Optional
logger = logging.getLogger('pymisp')
2017-07-21 18:47:10 +02:00
try:
2020-01-23 10:27:40 +01:00
import lief # type: ignore
from lief import Logger # type: ignore
2017-08-25 15:57:12 +02:00
Logger.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
2017-07-21 18:47:10 +02:00
except ImportError:
HAS_LIEF = False
class FileTypeNotImplemented(MISPObjectException):
pass
2020-07-28 20:05:42 +02:00
def make_binary_objects(filepath: Optional[str] = None, pseudofile: Optional[BytesIO] = None, filename: Optional[str] = None, standalone: bool = True, default_attributes_parameters: dict = {}):
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)
if HAS_LIEF and (filepath or (pseudofile and filename)):
2017-08-25 17:41:58 +02:00
try:
if filepath:
lief_parsed = lief.parse(filepath=filepath)
2020-01-23 10:27:40 +01:00
elif pseudofile and filename:
2019-02-06 11:31:05 +01:00
if sys.version_info < (3, 0):
logger.critical('Pseudofile is not supported in python2. Just update.')
lief_parsed = None
else:
lief_parsed = lief.parse(raw=pseudofile.getvalue(), name=filename)
2020-01-23 10:27:40 +01:00
else:
logger.critical('You need either a filepath, or a pseudofile and a filename.')
lief_parsed = None
2017-08-25 17:41:58 +02:00
if isinstance(lief_parsed, lief.PE.Binary):
2017-12-20 14:27:31 +01:00
return make_pe_objects(lief_parsed, misp_file, standalone, default_attributes_parameters)
2017-08-25 17:41:58 +02:00
elif isinstance(lief_parsed, lief.ELF.Binary):
2017-12-20 14:27:31 +01:00
return make_elf_objects(lief_parsed, misp_file, standalone, default_attributes_parameters)
2017-08-25 17:41:58 +02:00
elif isinstance(lief_parsed, lief.MachO.Binary):
2017-12-20 14:27:31 +01:00
return make_macho_objects(lief_parsed, misp_file, standalone, default_attributes_parameters)
2017-08-25 17:41:58 +02:00
except lief.bad_format as e:
logger.warning('Bad format: {}'.format(e))
2017-08-25 17:41:58 +02:00
except lief.bad_file as e:
logger.warning('Bad file: {}'.format(e))
except lief.conversion_error as e:
logger.warning('Conversion file: {}'.format(e))
except lief.builder_error as e:
logger.warning('Builder file: {}'.format(e))
2017-08-25 17:41:58 +02:00
except lief.parser_error as e:
logger.warning('Parser error: {}'.format(e))
except lief.integrity_error as e:
logger.warning('Integrity error: {}'.format(e))
except lief.pe_error as e:
logger.warning('PE error: {}'.format(e))
except lief.type_error as e:
logger.warning('Type error: {}'.format(e))
except lief.exception as e:
logger.warning('Lief exception: {}'.format(e))
except FileTypeNotImplemented as e:
logger.warning(e)
if not HAS_LIEF:
logger.warning('Please install lief, documentation here: https://github.com/lief-project/LIEF')
return misp_file, None, []