modules.reporting.notification

Package modules :: Package reporting :: Module notification |
[hide private]
[frames] | no frames]

# Source Code for Module modules.reporting.notification

     1  # Copyright (C) 2014-2016 Cuckoo Foundation.
     2  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
     3  # See the file 'docs/LICENSE' for copying permission.
     4
     5  import json
     6  import datetime
     7  import calendar
     8
     9  try:
    10      import requests
    11      HAVE_REQUESTS = True
    12  except ImportError:
    13      HAVE_REQUESTS = False
    14
    15  from lib.cuckoo.common.abstracts import Report
    16  from lib.cuckoo.common.exceptions import CuckooReportError
    17  from lib.cuckoo.common.exceptions import CuckooOperationalError
    18
    19 -def default(obj):
    20      if isinstance(obj, datetime.datetime):
    21          if obj.utcoffset() is not None:
    22              obj = obj - obj.utcoffset()
    23          return calendar.timegm(obj.timetuple()) + obj.microsecond / 1000000.0
    24      raise TypeError("%r is not JSON serializable" % obj)
    25
    26 -class Notification(Report):
    27      """Notifies external service about finished analysis via URL."""
    28
    29 -    def run(self, results):
    30          if not HAVE_REQUESTS:
    31              raise CuckooOperationalError(
    32                  "The Notification reporting module requires the requests "
    33                  "library (install with `pip install requests`)"
    34              )
    35
    36          post = {
    37              "identifier": self.options.get("identifier"),
    38              "data": json.dumps(
    39                  results.get("info"), default=default, sort_keys=False
    40              )
    41          }
    42
    43          try:
    44              requests.post(self.options.get("url"), data=post)
    45          except Exception as e:
    46              raise CuckooReportError(
    47                  "Failed posting message via Notification: %s" % e
    48              )
    49