MISP-Taxii-Server/scripts/push_published_to_taxii.py

84 lines
2.4 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
import logging
2016-11-23 13:16:30 +01:00
2016-12-28 11:51:43 +01:00
# Set up logger
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
# 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"]
)
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
2017-07-26 15:39:04 +02:00
try:
cli.push(pkg.to_xml().decode("utf-8"), "urn:stix.mitre.org:xml:1.1.1",
uri="http://{}/services/inbox".format(config["domain"]),
2017-07-26 15:40:49 +02:00
collection_names=config["taxii"].get("collections", ["collection"]))
2016-12-28 11:51:43 +01:00
2017-07-26 15:39:04 +02:00
log.info("Pushed!")
except Exception as ex:
log.fatal("COULD NOT PUSH")
log.exception(ex)