mirror of https://github.com/MISP/PyMISP
chg: Properly use python logging module.
parent
0e123af546
commit
134df0cafb
|
@ -1,4 +1,9 @@
|
||||||
__version__ = '2.4.81.2'
|
__version__ = '2.4.81.2'
|
||||||
|
import sys
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
FORMAT = "[%(filename)s:%(lineno)s - %(funcName)s() ] %(message)s"
|
||||||
|
logging.basicConfig(stream=sys.stderr, level=logging.WARNING, format=FORMAT)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .exceptions import PyMISPError, NewEventError, NewAttributeError, MissingDependency, NoURL, NoKey, InvalidMISPObject, UnknownMISPObjectTemplate # noqa
|
from .exceptions import PyMISPError, NewEventError, NewAttributeError, MissingDependency, NoURL, NoKey, InvalidMISPObject, UnknownMISPObjectTemplate # noqa
|
||||||
|
@ -9,5 +14,6 @@ try:
|
||||||
from .tools import Neo4j # noqa
|
from .tools import Neo4j # noqa
|
||||||
from .tools import stix # noqa
|
from .tools import stix # noqa
|
||||||
from .tools import openioc # noqa
|
from .tools import openioc # noqa
|
||||||
except ImportError:
|
logger.debug('pymisp loaded properly')
|
||||||
pass
|
except ImportError as e:
|
||||||
|
logger.warning('Unable to load pymisp properly: {}'.format(e))
|
||||||
|
|
|
@ -6,10 +6,12 @@ import json
|
||||||
from json import JSONEncoder
|
from json import JSONEncoder
|
||||||
import collections
|
import collections
|
||||||
import six # Remove that import when discarding python2 support.
|
import six # Remove that import when discarding python2 support.
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
import warnings
|
logger.warning("You're using python 2, it is strongly recommended to use python >=3.5")
|
||||||
warnings.warn("You're using python 2, it is strongly recommended to use python >=3.5")
|
|
||||||
|
|
||||||
|
|
||||||
class MISPEncode(JSONEncoder):
|
class MISPEncode(JSONEncoder):
|
||||||
|
|
|
@ -9,16 +9,19 @@ import datetime
|
||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
import re
|
import re
|
||||||
import warnings
|
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
# Least dirty way to support python 2 and 3
|
||||||
|
basestring = str
|
||||||
|
unicode = str
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urlparse import urljoin
|
from urlparse import urljoin
|
||||||
warnings.warn("You're using python 2, it is strongly recommended to use python >=3.5")
|
loger.warning("You're using python 2, it is strongly recommended to use python >=3.5")
|
||||||
from io import BytesIO, open
|
from io import BytesIO, open
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
|
@ -40,19 +43,6 @@ from .exceptions import PyMISPError, SearchError, MissingDependency, NoURL, NoKe
|
||||||
from .mispevent import MISPEvent, MISPAttribute
|
from .mispevent import MISPEvent, MISPAttribute
|
||||||
from .abstract import MISPEncode
|
from .abstract import MISPEncode
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
# Least dirty way to support python 2 and 3
|
|
||||||
try:
|
|
||||||
basestring
|
|
||||||
unicode
|
|
||||||
warnings.warn("You're using python 2, it is strongly recommended to use python >=3.4")
|
|
||||||
except NameError:
|
|
||||||
basestring = str
|
|
||||||
unicode = str
|
|
||||||
|
|
||||||
|
|
||||||
def deprecated(func):
|
def deprecated(func):
|
||||||
'''This is a decorator which can be used to mark functions
|
'''This is a decorator which can be used to mark functions
|
||||||
as deprecated. It will result in a warning being emitted
|
as deprecated. It will result in a warning being emitted
|
||||||
|
@ -99,14 +89,14 @@ class PyMISP(object):
|
||||||
self.cert = cert
|
self.cert = cert
|
||||||
self.asynch = asynch
|
self.asynch = asynch
|
||||||
if asynch and not ASYNC_OK:
|
if asynch and not ASYNC_OK:
|
||||||
warnings.warn("You turned on Async, but don't have requests_futures installed")
|
logger.warning("You turned on Async, but don't have requests_futures installed")
|
||||||
self.asynch = False
|
self.asynch = False
|
||||||
|
|
||||||
self.resources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
|
self.resources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
|
||||||
if out_type != 'json':
|
if out_type != 'json':
|
||||||
raise PyMISPError('The only output type supported by PyMISP is JSON. If you still rely on XML, use PyMISP v2.4.49')
|
raise PyMISPError('The only output type supported by PyMISP is JSON. If you still rely on XML, use PyMISP v2.4.49')
|
||||||
if debug is not None:
|
if debug is not None:
|
||||||
warnings.warn('debug is deprecated, configure logging in api client')
|
logger.warning('debug is deprecated, configure logging in your script: import logging; logging.getLogger(\'pymisp\').setLevel(logging.DEBUG)')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Make sure the MISP instance is working and the URL is valid
|
# Make sure the MISP instance is working and the URL is valid
|
||||||
|
@ -163,6 +153,8 @@ class PyMISP(object):
|
||||||
'Accept': 'application/{}'.format(output),
|
'Accept': 'application/{}'.format(output),
|
||||||
'content-type': 'application/{}'.format(output),
|
'content-type': 'application/{}'.format(output),
|
||||||
'User-Agent': 'PyMISP {} - Python {}.{}.{}'.format(__version__, *sys.version_info)})
|
'User-Agent': 'PyMISP {} - Python {}.{}.{}'.format(__version__, *sys.version_info)})
|
||||||
|
if logger.isEnabledFor(logging.DEBUG):
|
||||||
|
logger.debug(session.headers)
|
||||||
return session
|
return session
|
||||||
|
|
||||||
# #####################
|
# #####################
|
||||||
|
@ -209,15 +201,16 @@ class PyMISP(object):
|
||||||
|
|
||||||
def _check_response(self, response):
|
def _check_response(self, response):
|
||||||
"""Check if the response from the server is not an unexpected error"""
|
"""Check if the response from the server is not an unexpected error"""
|
||||||
|
errors = []
|
||||||
if response.status_code >= 500:
|
if response.status_code >= 500:
|
||||||
response.raise_for_status()
|
errors.append(response.json())
|
||||||
|
logger.critical('Something bad happened on the server-side: {}'.format(response.json()))
|
||||||
try:
|
try:
|
||||||
to_return = response.json()
|
to_return = response.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
logger.debug(response.text)
|
# It the server didn't return a JSON blob, we've a problem.
|
||||||
raise PyMISPError('Unknown error: {}'.format(response.text))
|
raise PyMISPError('Unknown error (something is very broken server-side: {}'.format(response.text))
|
||||||
|
|
||||||
errors = []
|
|
||||||
if isinstance(to_return, (list, str)):
|
if isinstance(to_return, (list, str)):
|
||||||
to_return = {'response': to_return}
|
to_return = {'response': to_return}
|
||||||
if to_return.get('error'):
|
if to_return.get('error'):
|
||||||
|
@ -905,6 +898,7 @@ class PyMISP(object):
|
||||||
if controller not in ['events', 'attributes']:
|
if controller not in ['events', 'attributes']:
|
||||||
raise Exception('Invalid controller. Can only be {}'.format(', '.join(['events', 'attributes'])))
|
raise Exception('Invalid controller. Can only be {}'.format(', '.join(['events', 'attributes'])))
|
||||||
url = urljoin(self.root_url, '{}/{}'.format(controller, path.lstrip('/')))
|
url = urljoin(self.root_url, '{}/{}'.format(controller, path.lstrip('/')))
|
||||||
|
if logger.isEnabledFor(logging.DEBUG):
|
||||||
logger.debug('URL: %s', url)
|
logger.debug('URL: %s', url)
|
||||||
logger.debug('Query: %s', query)
|
logger.debug('Query: %s', query)
|
||||||
|
|
||||||
|
@ -1699,6 +1693,8 @@ class PyMISP(object):
|
||||||
"""Add an object"""
|
"""Add an object"""
|
||||||
session = self.__prepare_session()
|
session = self.__prepare_session()
|
||||||
url = urljoin(self.root_url, 'objects/add/{}/{}'.format(event_id, template_id))
|
url = urljoin(self.root_url, 'objects/add/{}/{}'.format(event_id, template_id))
|
||||||
|
if logger.isEnabledFor(logging.DEBUG):
|
||||||
|
logger.debug(url)
|
||||||
response = session.post(url, data=misp_object.to_json())
|
response = session.post(url, data=misp_object.to_json())
|
||||||
return self._check_response(response)
|
return self._check_response(response)
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit bbf3e45649af5af50c98ad90a86916cf75e8c74d
|
Subproject commit 6b43b68651a350a26891080ef0feda364b74727a
|
|
@ -16,12 +16,13 @@ from collections import Counter
|
||||||
from .abstract import AbstractMISP
|
from .abstract import AbstractMISP
|
||||||
from .exceptions import UnknownMISPObjectTemplate, InvalidMISPObject, PyMISPError, NewEventError, NewAttributeError
|
from .exceptions import UnknownMISPObjectTemplate, InvalidMISPObject, PyMISPError, NewEventError, NewAttributeError
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
import six # Remove that import when discarding python2 support.
|
import six # Remove that import when discarding python2 support.
|
||||||
|
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
import warnings
|
logger.warning("You're using python 2, it is strongly recommended to use python >=3.5")
|
||||||
warnings.warn("You're using python 2, it is strongly recommended to use python >=3.5")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from dateutil.parser import parse
|
from dateutil.parser import parse
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
|
|
||||||
from . import FileObject, PEObject, ELFObject, MachOObject
|
from . import FileObject, PEObject, ELFObject, MachOObject
|
||||||
from ..exceptions import MISPObjectException
|
from ..exceptions import MISPObjectException
|
||||||
import warnings
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import lief
|
import lief
|
||||||
|
@ -57,15 +59,15 @@ def make_binary_objects(filepath=None, pseudofile=None, filename=None):
|
||||||
elif isinstance(lief_parsed, lief.MachO.Binary):
|
elif isinstance(lief_parsed, lief.MachO.Binary):
|
||||||
return make_macho_objects(lief_parsed, misp_file)
|
return make_macho_objects(lief_parsed, misp_file)
|
||||||
except lief.bad_format as e:
|
except lief.bad_format as e:
|
||||||
warnings.warn('\tBad format: {}'.format(e))
|
logger.warning('Bad format: {}'.format(e))
|
||||||
except lief.bad_file as e:
|
except lief.bad_file as e:
|
||||||
warnings.warn('\tBad file: {}'.format(e))
|
logger.warning('Bad file: {}'.format(e))
|
||||||
except lief.parser_error as e:
|
except lief.parser_error as e:
|
||||||
warnings.warn('\tParser error: {}'.format(e))
|
logger.warning('Parser error: {}'.format(e))
|
||||||
except FileTypeNotImplemented as e: # noqa
|
except FileTypeNotImplemented as e: # noqa
|
||||||
warnings.warn(e)
|
logger.warning(e)
|
||||||
if not HAS_LIEF:
|
if not HAS_LIEF:
|
||||||
warnings.warn('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
logger.warning('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
||||||
if not filepath:
|
if not filepath:
|
||||||
warnings.warn('LIEF currently requires a filepath and not a pseudo file')
|
logger.warning('LIEF currently requires a filepath and not a pseudo file')
|
||||||
return misp_file, None, None
|
return misp_file, None, None
|
||||||
|
|
|
@ -5,8 +5,9 @@ from .abstractgenerator import AbstractMISPObjectGenerator
|
||||||
from ..exceptions import InvalidMISPObject
|
from ..exceptions import InvalidMISPObject
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from hashlib import md5, sha1, sha256, sha512
|
from hashlib import md5, sha1, sha256, sha512
|
||||||
import warnings
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import lief
|
import lief
|
||||||
|
@ -25,7 +26,7 @@ class ELFObject(AbstractMISPObjectGenerator):
|
||||||
|
|
||||||
def __init__(self, parsed=None, filepath=None, pseudofile=None):
|
def __init__(self, parsed=None, filepath=None, pseudofile=None):
|
||||||
if not HAS_PYDEEP:
|
if not HAS_PYDEEP:
|
||||||
warnings.warn("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
||||||
if not HAS_LIEF:
|
if not HAS_LIEF:
|
||||||
raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
||||||
if pseudofile:
|
if pseudofile:
|
||||||
|
|
|
@ -8,7 +8,10 @@ from io import BytesIO
|
||||||
from hashlib import md5, sha1, sha256, sha512
|
from hashlib import md5, sha1, sha256, sha512
|
||||||
import math
|
import math
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
import warnings
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import pydeep
|
import pydeep
|
||||||
|
@ -27,9 +30,9 @@ class FileObject(AbstractMISPObjectGenerator):
|
||||||
|
|
||||||
def __init__(self, filepath=None, pseudofile=None, filename=None):
|
def __init__(self, filepath=None, pseudofile=None, filename=None):
|
||||||
if not HAS_PYDEEP:
|
if not HAS_PYDEEP:
|
||||||
warnings.warn("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
||||||
if not HAS_MAGIC:
|
if not HAS_MAGIC:
|
||||||
warnings.warn("Please install python-magic: pip install python-magic.")
|
logger.warning("Please install python-magic: pip install python-magic.")
|
||||||
if filename:
|
if filename:
|
||||||
# Useful in case the file is copied with a pre-defined name by a script but we want to keep the original name
|
# Useful in case the file is copied with a pre-defined name by a script but we want to keep the original name
|
||||||
self.__filename = filename
|
self.__filename = filename
|
||||||
|
|
|
@ -5,7 +5,9 @@ from ..exceptions import InvalidMISPObject
|
||||||
from .abstractgenerator import AbstractMISPObjectGenerator
|
from .abstractgenerator import AbstractMISPObjectGenerator
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from hashlib import md5, sha1, sha256, sha512
|
from hashlib import md5, sha1, sha256, sha512
|
||||||
import warnings
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -25,7 +27,7 @@ class MachOObject(AbstractMISPObjectGenerator):
|
||||||
|
|
||||||
def __init__(self, parsed=None, filepath=None, pseudofile=None):
|
def __init__(self, parsed=None, filepath=None, pseudofile=None):
|
||||||
if not HAS_PYDEEP:
|
if not HAS_PYDEEP:
|
||||||
warnings.warn("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
||||||
if not HAS_LIEF:
|
if not HAS_LIEF:
|
||||||
raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
||||||
if pseudofile:
|
if pseudofile:
|
||||||
|
|
|
@ -6,8 +6,9 @@ from .abstractgenerator import AbstractMISPObjectGenerator
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from hashlib import md5, sha1, sha256, sha512
|
from hashlib import md5, sha1, sha256, sha512
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import warnings
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('pymisp')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import lief
|
import lief
|
||||||
|
@ -26,7 +27,7 @@ class PEObject(AbstractMISPObjectGenerator):
|
||||||
|
|
||||||
def __init__(self, parsed=None, filepath=None, pseudofile=None):
|
def __init__(self, parsed=None, filepath=None, pseudofile=None):
|
||||||
if not HAS_PYDEEP:
|
if not HAS_PYDEEP:
|
||||||
warnings.warn("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
logger.warning("Please install pydeep: pip install git+https://github.com/kbandla/pydeep.git")
|
||||||
if not HAS_LIEF:
|
if not HAS_LIEF:
|
||||||
raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
raise ImportError('Please install lief, documentation here: https://github.com/lief-project/LIEF')
|
||||||
if pseudofile:
|
if pseudofile:
|
||||||
|
|
Loading…
Reference in New Issue