Merge branch 'master' of https://github.com/MISP/PyMISP into first-friendly-contribution-enhance-coverage

pull/150/head
Stefan Hagen (Individual) 2017-12-11 14:04:04 +01:00
commit 207e1f195e
4 changed files with 44 additions and 7 deletions

View File

@ -39,7 +39,7 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
if args.output is not None and os.path.exists(args.output): 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) exit(0)
misp = init(misp_url, misp_key) misp = init(misp_url, misp_key)

@ -1 +1 @@
Subproject commit c3f88d6901085c651132d4f40274a219deca5250 Subproject commit 4eac3539c479e148088578dc2382c0c637c53944

View File

@ -1,6 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import six
from . import FileObject, PEObject, ELFObject, MachOObject from . import FileObject, PEObject, ELFObject, MachOObject
from ..exceptions import MISPObjectException from ..exceptions import MISPObjectException
import logging import logging
@ -49,9 +51,16 @@ def make_macho_objects(lief_parsed, misp_file):
def make_binary_objects(filepath=None, pseudofile=None, filename=None): def make_binary_objects(filepath=None, pseudofile=None, filename=None):
misp_file = FileObject(filepath=filepath, pseudofile=pseudofile, filename=filename) misp_file = FileObject(filepath=filepath, pseudofile=pseudofile, filename=filename)
if HAS_LIEF and filepath: if HAS_LIEF and filepath or (pseudofile and filename):
try: 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): if isinstance(lief_parsed, lief.PE.Binary):
return make_pe_objects(lief_parsed, misp_file) return make_pe_objects(lief_parsed, misp_file)
elif isinstance(lief_parsed, lief.ELF.Binary): 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)) logger.warning('Type error: {}'.format(e))
except lief.exception as e: except lief.exception as e:
logger.warning('Lief exception: {}'.format(e)) logger.warning('Lief exception: {}'.format(e))
except FileTypeNotImplemented as e: # noqa except FileTypeNotImplemented as e:
logger.warning(e) logger.warning(e)
if not HAS_LIEF: if not HAS_LIEF:
logger.warning('Please install lief, documentation here: https://github.com/lief-project/LIEF') logger.warning('Please install lief, documentation here: https://github.com/lief-project/LIEF')

View File

@ -5,6 +5,8 @@ import unittest
import requests_mock import requests_mock
import json import json
import os import os
import six
from io import BytesIO
import pymisp as pm import pymisp as pm
from pymisp import PyMISP from pymisp import PyMISP
@ -210,9 +212,12 @@ class TestOffline(unittest.TestCase):
p.add_internal_other(evt, 'foobar') p.add_internal_other(evt, 'foobar')
p.add_attachment(evt, "testFile") p.add_attachment(evt, "testFile")
def make_objects(self, path): def make_objects(self, path=None, pseudofile=None, filename=None):
to_return = {'objects': [], 'references': []} 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: if seos:
for s in seos: for s in seos:
@ -229,8 +234,31 @@ class TestOffline(unittest.TestCase):
to_return['objects'].append(fo) to_return['objects'].append(fo)
if fo.ObjectReference: if fo.ObjectReference:
to_return['references'] += 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) 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): def test_objects(self, m):
paths = ['cmd.exe', 'tmux', 'MachO-OSX-x64-ls'] paths = ['cmd.exe', 'tmux', 'MachO-OSX-x64-ls']
try: try: