2017-08-25 16:08:05 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-08-31 10:40:18 +02:00
|
|
|
from .abstractgenerator import AbstractMISPObjectGenerator
|
2017-09-20 12:44:55 +02:00
|
|
|
from ..exceptions import InvalidMISPObject
|
2017-08-25 16:08:05 +02:00
|
|
|
from io import BytesIO
|
|
|
|
from hashlib import md5, sha1, sha256, sha512
|
2017-11-08 03:10:04 +01:00
|
|
|
import logging
|
2020-01-23 10:27:40 +01:00
|
|
|
from typing import Union
|
|
|
|
from pathlib import Path
|
2020-02-07 11:51:44 +01:00
|
|
|
from . import FileObject
|
2017-08-25 16:08:05 +02:00
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2020-07-28 20:05:42 +02:00
|
|
|
def make_elf_objects(lief_parsed: lief.Binary, misp_file: FileObject, standalone: bool = True, default_attributes_parameters: dict = {}):
|
2020-02-07 11:51:44 +01:00
|
|
|
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
|
|
|
|
|
2017-08-25 16:08:05 +02:00
|
|
|
|
2017-08-28 19:01:53 +02:00
|
|
|
class ELFObject(AbstractMISPObjectGenerator):
|
2017-08-25 16:08:05 +02:00
|
|
|
|
2020-07-28 20:05:42 +02:00
|
|
|
def __init__(self, parsed: lief.ELF.Binary = None, filepath: Union[Path, str] = None, pseudofile: Union[BytesIO, bytes] = None, **kwargs):
|
2020-09-15 12:39:59 +02:00
|
|
|
"""Creates an ELF object, with lief"""
|
2021-10-26 02:37:12 +02:00
|
|
|
super().__init__('elf', **kwargs)
|
2017-08-25 16:08:05 +02:00
|
|
|
if not HAS_PYDEEP:
|
2017-11-08 03:10:04 +01:00
|
|
|
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):
|
2017-09-12 16:46:06 +02:00
|
|
|
self.__elf = lief.ELF.parse(raw=pseudofile.getvalue())
|
2017-08-25 16:08:05 +02:00
|
|
|
elif isinstance(pseudofile, bytes):
|
2017-09-12 16:46:06 +02:00
|
|
|
self.__elf = lief.ELF.parse(raw=pseudofile)
|
2017-08-25 16:08:05 +02:00
|
|
|
else:
|
2017-09-20 12:44:55 +02:00
|
|
|
raise InvalidMISPObject('Pseudo file can be BytesIO or bytes got {}'.format(type(pseudofile)))
|
2017-08-25 16:08:05 +02:00
|
|
|
elif filepath:
|
2017-09-12 16:46:06 +02:00
|
|
|
self.__elf = lief.ELF.parse(filepath)
|
2017-08-25 16:08:05 +02:00
|
|
|
elif parsed:
|
|
|
|
# Got an already parsed blob
|
|
|
|
if isinstance(parsed, lief.ELF.Binary):
|
2017-09-12 16:46:06 +02:00
|
|
|
self.__elf = parsed
|
2017-08-25 16:08:05 +02:00
|
|
|
else:
|
2017-09-20 12:44:55 +02:00
|
|
|
raise InvalidMISPObject('Not a lief.ELF.Binary: {}'.format(type(parsed)))
|
2017-08-25 16:08:05 +02:00
|
|
|
self.generate_attributes()
|
|
|
|
|
|
|
|
def generate_attributes(self):
|
|
|
|
# General information
|
2017-09-12 16:46:06 +02:00
|
|
|
self.add_attribute('type', value=str(self.__elf.header.file_type).split('.')[1])
|
|
|
|
self.add_attribute('entrypoint-address', value=self.__elf.entrypoint)
|
|
|
|
self.add_attribute('arch', value=str(self.__elf.header.machine_type).split('.')[1])
|
|
|
|
self.add_attribute('os_abi', value=str(self.__elf.header.identity_os_abi).split('.')[1])
|
2017-08-25 16:08:05 +02:00
|
|
|
# Sections
|
|
|
|
self.sections = []
|
2017-09-12 16:46:06 +02:00
|
|
|
if self.__elf.sections:
|
2017-08-25 16:08:05 +02:00
|
|
|
pos = 0
|
2017-09-12 16:46:06 +02:00
|
|
|
for section in self.__elf.sections:
|
2021-03-16 17:56:04 +01:00
|
|
|
if not section.name:
|
|
|
|
continue
|
2020-06-30 12:40:08 +02:00
|
|
|
s = ELFSectionObject(section, standalone=self._standalone, default_attributes_parameters=self._default_attributes_parameters)
|
2019-07-24 13:57:36 +02:00
|
|
|
self.add_reference(s.uuid, 'includes', 'Section {} of ELF'.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 ELFSectionObject(AbstractMISPObjectGenerator):
|
2017-08-25 16:08:05 +02:00
|
|
|
|
2020-06-30 12:40:08 +02:00
|
|
|
def __init__(self, section: lief.ELF.Section, **kwargs):
|
2020-09-15 12:39:59 +02:00
|
|
|
"""Creates an ELF Section object. Object generated by ELFObject."""
|
2017-08-25 16:08:05 +02:00
|
|
|
# Python3 way
|
|
|
|
# super().__init__('pe-section')
|
2020-06-30 12:40:08 +02:00
|
|
|
super(ELFSectionObject, self).__init__('elf-section', **kwargs)
|
2017-09-12 16:46:06 +02:00
|
|
|
self.__section = section
|
|
|
|
self.__data = bytes(self.__section.content)
|
2017-08-25 16:08:05 +02:00
|
|
|
self.generate_attributes()
|
|
|
|
|
|
|
|
def generate_attributes(self):
|
2017-09-12 16:46:06 +02:00
|
|
|
self.add_attribute('name', value=self.__section.name)
|
|
|
|
self.add_attribute('type', value=str(self.__section.type).split('.')[1])
|
|
|
|
for flag in self.__section.flags_list:
|
2017-08-28 19:01:53 +02:00
|
|
|
self.add_attribute('flag', value=str(flag).split('.')[1])
|
2017-09-12 16:46:06 +02:00
|
|
|
size = self.add_attribute('size-in-bytes', value=self.__section.size)
|
2017-08-25 16:08:05 +02:00
|
|
|
if int(size.value) > 0:
|
2017-09-12 16:46:06 +02:00
|
|
|
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:
|
2017-09-12 16:46:06 +02:00
|
|
|
self.add_attribute('ssdeep', value=pydeep.hash_buf(self.__data).decode())
|