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
|
2017-08-18 11:52:42 +02:00
|
|
|
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
|
2017-12-27 11:14:36 +01:00
|
|
|
logging.basicConfig(level=logging.INFO, format="'%(asctime)s - %(name)s - %(levelname)s - %(message)s')")
|
2016-12-28 11:51:43 +01:00
|
|
|
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"]
|
|
|
|
)
|
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
|
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
|
2017-08-18 11:52:42 +02:00
|
|
|
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
|
|
|
|
|
2017-08-18 11:52:42 +02:00
|
|
|
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),
|
2017-08-18 11:52:42 +02:00
|
|
|
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:
|
|
|
|
log.fatal("COULD NOT PUSH")
|
|
|
|
log.exception(ex)
|