MISP-Taxii-Server/scripts/push_published_to_taxii.py

107 lines
3.3 KiB
Python
Raw Normal View History

2016-11-23 13:16:30 +01:00
import os
import zmq
import sys
import json
import pymisp
2016-12-28 11:51:43 +01:00
import warnings
2016-11-23 13:16:30 +01:00
from pyaml import yaml
2016-12-28 11:51:43 +01:00
from cabby import create_client
from misp_stix_converter.converters import lint_roller
2016-12-28 11:51:43 +01:00
import logging
2016-11-23 13:16:30 +01:00
2016-12-28 11:51:43 +01:00
# Set up logger
log = logging.getLogger(__name__)
2017-12-28 13:21:26 +01:00
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch = logging.FileHandler("push.log")
ch.setFormatter(formatter)
log.addHandler(ch)
log.setLevel(logging.DEBUG)
2016-12-28 11:51:43 +01:00
2017-12-28 13:21:26 +01:00
log.info("Starting...")
2016-12-28 11:51:43 +01:00
# Try to load in config
if "OPENTAXII_CONFIG" in os.environ:
config = yaml.load(open(os.environ["OPENTAXII_CONFIG"], "r"))
2016-11-23 13:16:30 +01:00
else:
2016-12-28 11:51:43 +01:00
config = { "domain" : "127.0.0.1:9000" ,
2016-11-23 13:16:30 +01:00
"zmq" : { "host" : "127.0.0.1", "port" : 50000 }
}
2016-12-28 11:51:43 +01:00
# Set up our ZMQ socket to recieve MISP JSON on publish
2016-11-23 13:16:30 +01:00
context = zmq.Context()
socket = context.socket(zmq.SUB)
2016-12-28 11:51:43 +01:00
log.info("Subscribing to tcp://{}:{}".format(
2016-11-23 13:16:30 +01:00
config["zmq"]["host"],
config["zmq"]["port"]
))
2016-12-28 11:51:43 +01:00
# Connect to the socket
2016-11-23 13:16:30 +01:00
socket.connect("tcp://{}:{}".format(
config["zmq"]["host"],
config["zmq"]["port"]
))
2016-12-28 11:51:43 +01:00
# Set the option to subscribe
2016-11-23 13:16:30 +01:00
socket.setsockopt_string(zmq.SUBSCRIBE, '')
2016-12-28 11:51:43 +01:00
# Connct to TAXII as well
2017-08-17 17:14:06 +02:00
cli = create_client(discovery_path="{}://{}/services/discovery".format(config.get("protocol", "http"), config["domain"]))
2016-12-28 11:51:43 +01:00
cli.set_auth(username = config["taxii"]["auth"]["username"],
password = config["taxii"]["auth"]["password"]
)
2017-08-18 11:56:19 +02:00
if not config.get("verify_ssl", True):
cli.verify_ssl = False
2016-12-28 11:51:43 +01:00
2016-11-23 13:16:30 +01:00
while True:
2016-12-28 11:51:43 +01:00
# Wait for something to come in on the ZMQ socket
message = socket.recv().decode("utf-8")
2016-12-28 11:51:43 +01:00
log.info("Recieved a message!")
topic = message.split(' ', 1)[0]
if topic != 'misp_json':
log.info("Ignoring " + topic + "...")
continue
# Process the JSON payload
2016-12-28 11:51:43 +01:00
log.debug("Processing...")
payload = message[len(topic)+1:]
2016-12-28 11:51:43 +01:00
# Load the message JSON
msg = json.loads(payload)
2016-12-28 11:51:43 +01:00
log.debug(msg)
# Load it as a misp object for easy conversion to STIX
2016-11-23 13:16:30 +01:00
ev = pymisp.mispevent.MISPEvent()
ev.load(msg)
2016-12-28 11:51:43 +01:00
# Convert to STIX
pkg = pymisp.tools.stix.make_stix_package(ev)
log.debug("Loaded successfully!")
# Push the package to TAXII
for version in config.get("stix_versions", ["1.1.1"]):
# Convert to that version
objs = lint_roller.lintRoll(pkg)
for i in objs:
# Set the object's version
if hasattr(i, "version"):
i.version = version
2016-12-28 11:51:43 +01:00
2017-08-18 12:13:37 +02:00
# Set the top-level
pkg.version = version
try:
2017-08-18 12:06:13 +02:00
log.info("Using binding %s", "urn:stix.mitre.org:xml:{}".format(version))
cli.push(content=pkg.to_xml().decode("utf-8"),
content_binding="urn:stix.mitre.org:xml:{}".format(version),
uri="{}://{}/services/inbox".format(config.get("protocol", "http"),
config["domain"]),
collection_names=config["taxii"].get("collections", ["collection"]))
log.info("Pushed! (%s)", version)
except Exception as ex:
2017-12-28 13:21:26 +01:00
logging.fatal("COULD NOT PUSH")
logging.exception(ex)