PyMISP/pymisp/tools/machoobject.py

97 lines
4.1 KiB
Python
Raw Normal View History

2017-08-25 16:08:05 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from ..exceptions import InvalidMISPObject
2017-08-31 10:40:18 +02:00
from .abstractgenerator import AbstractMISPObjectGenerator
2017-08-25 16:08:05 +02:00
from io import BytesIO
from hashlib import md5, sha1, sha256, sha512
import logging
2020-01-23 10:27:40 +01:00
from typing import Optional, Union
from pathlib import Path
2020-02-07 11:51:44 +01:00
from . import FileObject
2020-02-07 11:51:44 +01:00
import lief # type: ignore
2017-08-25 16:08:05 +02:00
try:
2020-01-23 10:27:40 +01:00
import pydeep # type: ignore
2017-08-25 16:08:05 +02:00
HAS_PYDEEP = True
except ImportError:
HAS_PYDEEP = False
2020-02-07 11:51:44 +01:00
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
2017-08-25 16:08:05 +02:00
2017-08-28 19:01:53 +02:00
class MachOObject(AbstractMISPObjectGenerator):
2017-08-25 16:08:05 +02:00
2020-01-23 10:27:40 +01:00
def __init__(self, parsed: Optional[lief.MachO.Binary]=None, filepath: Optional[Union[Path, str]]=None, pseudofile: Optional[BytesIO]=None, standalone: bool=True, **kwargs):
# Python3 way
# super().__init__('elf')
super(MachOObject, self).__init__('macho', standalone=standalone, **kwargs)
2017-08-25 16:08:05 +02:00
if not HAS_PYDEEP:
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
2017-08-25 16:08:05 +02:00
if pseudofile:
if isinstance(pseudofile, BytesIO):
self.__macho = lief.MachO.parse(raw=pseudofile.getvalue())
2017-08-25 16:08:05 +02:00
elif isinstance(pseudofile, bytes):
self.__macho = lief.MachO.parse(raw=pseudofile)
2017-08-25 16:08:05 +02:00
else:
raise InvalidMISPObject('Pseudo file can be BytesIO or bytes got {}'.format(type(pseudofile)))
2017-08-25 16:08:05 +02:00
elif filepath:
self.__macho = lief.MachO.parse(filepath)
2017-08-25 16:08:05 +02:00
elif parsed:
# Got an already parsed blob
if isinstance(parsed, lief.MachO.Binary):
self.__macho = parsed
2017-08-25 16:08:05 +02:00
else:
raise InvalidMISPObject('Not a lief.MachO.Binary: {}'.format(type(parsed)))
2017-08-25 16:08:05 +02:00
self.generate_attributes()
def generate_attributes(self):
self.add_attribute('type', value=str(self.__macho.header.file_type).split('.')[1])
self.add_attribute('name', value=self.__macho.name)
2017-08-25 16:08:05 +02:00
# General information
if self.__macho.has_entrypoint:
self.add_attribute('entrypoint-address', value=self.__macho.entrypoint)
2017-08-25 16:08:05 +02:00
# Sections
self.sections = []
if self.__macho.sections:
2017-08-25 16:08:05 +02:00
pos = 0
for section in self.__macho.sections:
2017-12-20 14:27:31 +01:00
s = MachOSectionObject(section, self._standalone, default_attributes_parameters=self._default_attributes_parameters)
self.add_reference(s.uuid, 'includes', 'Section {} of MachO'.format(pos))
2017-08-25 16:08:05 +02:00
pos += 1
self.sections.append(s)
2017-08-28 19:01:53 +02:00
self.add_attribute('number-sections', value=len(self.sections))
2017-08-25 16:08:05 +02:00
2017-08-28 19:01:53 +02:00
class MachOSectionObject(AbstractMISPObjectGenerator):
2017-08-25 16:08:05 +02:00
2020-01-23 10:27:40 +01:00
def __init__(self, section: lief.MachO.Section, standalone: bool=True, **kwargs):
2017-08-25 16:08:05 +02:00
# Python3 way
# super().__init__('pe-section')
2017-12-12 17:34:09 +01:00
super(MachOSectionObject, self).__init__('macho-section', standalone=standalone, **kwargs)
self.__section = section
self.__data = bytes(self.__section.content)
2017-08-25 16:08:05 +02:00
self.generate_attributes()
def generate_attributes(self):
self.add_attribute('name', value=self.__section.name)
size = self.add_attribute('size-in-bytes', value=self.__section.size)
2017-08-25 16:08:05 +02:00
if int(size.value) > 0:
self.add_attribute('entropy', value=self.__section.entropy)
self.add_attribute('md5', value=md5(self.__data).hexdigest())
self.add_attribute('sha1', value=sha1(self.__data).hexdigest())
self.add_attribute('sha256', value=sha256(self.__data).hexdigest())
self.add_attribute('sha512', value=sha512(self.__data).hexdigest())
2017-08-25 16:08:05 +02:00
if HAS_PYDEEP:
self.add_attribute('ssdeep', value=pydeep.hash_buf(self.__data).decode())