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
|
|
|
|
cli = create_client(discovery_path="http://{}/services/discovery".format(config["domain"]))
|
|
|
|
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
|
2017-06-01 16:12:54 +02:00
|
|
|
message = socket.recv().decode("utf-8")
|
2016-12-28 11:51:43 +01:00
|
|
|
log.info("Recieved a message!")
|
2017-06-01 16:12:54 +02:00
|
|
|
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...")
|
2017-06-01 16:12:54 +02:00
|
|
|
payload = message[len(topic)+1:]
|
2016-12-28 11:51:43 +01:00
|
|
|
|
|
|
|
# Load the message JSON
|
2017-06-01 16:12:54 +02:00
|
|
|
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
|
|
|
|
cli.push(pkg.to_xml().decode("utf-8"), "urn:stix.mitre.org:xml:1.1.1",
|
|
|
|
uri="http://{}/services/inbox".format(config["domain"]),
|
|
|
|
collection_names=["collection"])
|
|
|
|
|
|
|
|
log.info("Pushed!")
|