mirror of https://github.com/MISP/PyMISP
Merge branch 'master' of https://github.com/MISP/PyMISP into first-friendly-contribution-enhance-coverage
commit
207e1f195e
|
@ -39,7 +39,7 @@ if __name__ == '__main__':
|
|||
args = parser.parse_args()
|
||||
|
||||
if args.output is not None and os.path.exists(args.output):
|
||||
print('Output file already exists, abord.')
|
||||
print('Output file already exists, abort.')
|
||||
exit(0)
|
||||
|
||||
misp = init(misp_url, misp_key)
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit c3f88d6901085c651132d4f40274a219deca5250
|
||||
Subproject commit 4eac3539c479e148088578dc2382c0c637c53944
|
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import six
|
||||
|
||||
from . import FileObject, PEObject, ELFObject, MachOObject
|
||||
from ..exceptions import MISPObjectException
|
||||
import logging
|
||||
|
@ -49,9 +51,16 @@ def make_macho_objects(lief_parsed, misp_file):
|
|||
|
||||
def make_binary_objects(filepath=None, pseudofile=None, filename=None):
|
||||
misp_file = FileObject(filepath=filepath, pseudofile=pseudofile, filename=filename)
|
||||
if HAS_LIEF and filepath:
|
||||
if HAS_LIEF and filepath or (pseudofile and filename):
|
||||
try:
|
||||
lief_parsed = lief.parse(filepath)
|
||||
if filepath:
|
||||
lief_parsed = lief.parse(filepath=filepath)
|
||||
else:
|
||||
if six.PY2:
|
||||
logger.critical('Pseudofile is not supported in python2. Just update.')
|
||||
lief_parsed = None
|
||||
else:
|
||||
lief_parsed = lief.parse(raw=pseudofile.getvalue(), name=filename)
|
||||
if isinstance(lief_parsed, lief.PE.Binary):
|
||||
return make_pe_objects(lief_parsed, misp_file)
|
||||
elif isinstance(lief_parsed, lief.ELF.Binary):
|
||||
|
@ -76,7 +85,7 @@ def make_binary_objects(filepath=None, pseudofile=None, filename=None):
|
|||
logger.warning('Type error: {}'.format(e))
|
||||
except lief.exception as e:
|
||||
logger.warning('Lief exception: {}'.format(e))
|
||||
except FileTypeNotImplemented as e: # noqa
|
||||
except FileTypeNotImplemented as e:
|
||||
logger.warning(e)
|
||||
if not HAS_LIEF:
|
||||
logger.warning('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
||||
|
|
|
@ -5,6 +5,8 @@ import unittest
|
|||
import requests_mock
|
||||
import json
|
||||
import os
|
||||
import six
|
||||
from io import BytesIO
|
||||
|
||||
import pymisp as pm
|
||||
from pymisp import PyMISP
|
||||
|
@ -210,9 +212,12 @@ class TestOffline(unittest.TestCase):
|
|||
p.add_internal_other(evt, 'foobar')
|
||||
p.add_attachment(evt, "testFile")
|
||||
|
||||
def make_objects(self, path):
|
||||
def make_objects(self, path=None, pseudofile=None, filename=None):
|
||||
to_return = {'objects': [], 'references': []}
|
||||
fo, peo, seos = make_binary_objects(path)
|
||||
if path:
|
||||
fo, peo, seos = make_binary_objects(path)
|
||||
else:
|
||||
fo, peo, seos = make_binary_objects(pseudofile=pseudofile, filename=filename)
|
||||
|
||||
if seos:
|
||||
for s in seos:
|
||||
|
@ -229,8 +234,31 @@ class TestOffline(unittest.TestCase):
|
|||
to_return['objects'].append(fo)
|
||||
if fo.ObjectReference:
|
||||
to_return['references'] += fo.ObjectReference
|
||||
|
||||
# Remove UUIDs for comparing the objects.
|
||||
for o in to_return['objects']:
|
||||
o.pop('uuid')
|
||||
for o in to_return['references']:
|
||||
o.pop('referenced_uuid')
|
||||
o.pop('object_uuid')
|
||||
return json.dumps(to_return, cls=MISPEncode)
|
||||
|
||||
def test_objects_pseudofile(self, m):
|
||||
if six.PY2:
|
||||
return unittest.SkipTest()
|
||||
paths = ['cmd.exe', 'tmux', 'MachO-OSX-x64-ls']
|
||||
try:
|
||||
for path in paths:
|
||||
with open(os.path.join('tests', 'viper-test-files', 'test_files', path), 'rb') as f:
|
||||
pseudo = BytesIO(f.read())
|
||||
json_blob = self.make_objects(pseudofile=pseudo, filename=path)
|
||||
# Compare pseudo file / path
|
||||
filepath_blob = self.make_objects(os.path.join('tests', 'viper-test-files', 'test_files', path))
|
||||
self.assertEqual(json_blob, filepath_blob)
|
||||
except IOError: # Can be replaced with FileNotFoundError when support for python 2 is dropped
|
||||
return unittest.SkipTest()
|
||||
print(json_blob)
|
||||
|
||||
def test_objects(self, m):
|
||||
paths = ['cmd.exe', 'tmux', 'MachO-OSX-x64-ls']
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue