2017-03-30 17:26:46 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from cabby import create_client
|
|
|
|
from pyaml import yaml
|
2020-05-07 01:58:21 +02:00
|
|
|
from yaml import Loader
|
2018-02-13 12:58:26 +01:00
|
|
|
import pytz
|
2017-03-30 17:26:46 +02:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import sys
|
2018-02-06 14:35:31 +01:00
|
|
|
from datetime import datetime
|
2017-03-30 17:26:46 +02:00
|
|
|
|
2017-03-30 18:18:33 +02:00
|
|
|
# Create an argument parser for our program
|
|
|
|
# Will just take in a config file and logging options
|
2017-03-30 17:26:46 +02:00
|
|
|
parser = argparse.ArgumentParser(description='Run MISP taxii pull.')
|
2019-02-21 14:50:06 +01:00
|
|
|
parser.add_argument('-c', "--configdir", default="~/.misptaxii",
|
|
|
|
help='Config directory')
|
|
|
|
parser.add_argument("-v", "--verbose", action="store_true",
|
|
|
|
help="More verbose logging")
|
|
|
|
parser.add_argument("-s", "--stdout", action="store_true",
|
|
|
|
help="Log to STDOUT")
|
|
|
|
parser.add_argument("--start",
|
|
|
|
help="Date to poll from (YYYY-MM-DDTHH:MM:SS), Exclusive")
|
|
|
|
parser.add_argument("--end",
|
|
|
|
help="Date to poll to (YYYY-MM-DDTHH:MM:SS), Inclusive")
|
|
|
|
parser.add_argument("--subscription_id", help="The ID of the subscription",
|
|
|
|
default=None)
|
|
|
|
parser.add_argument("--tz",
|
|
|
|
help="Your timezone, e.g Europe/London. Default utc",
|
2018-02-13 12:58:26 +01:00
|
|
|
default="utc")
|
2019-02-21 14:50:06 +01:00
|
|
|
|
2017-03-30 17:26:46 +02:00
|
|
|
args = parser.parse_args()
|
2017-03-30 18:18:33 +02:00
|
|
|
|
|
|
|
# Set up a logger for logging's sake
|
2017-03-30 17:26:46 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2019-02-21 14:50:06 +01:00
|
|
|
logging.basicConfig(
|
|
|
|
filename="poll.log",
|
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
2017-03-30 17:26:46 +02:00
|
|
|
log.setLevel(logging.DEBUG if args.verbose else logging.INFO)
|
|
|
|
|
2017-03-30 18:18:33 +02:00
|
|
|
# If we want, print the output to stdout
|
2017-03-30 17:26:46 +02:00
|
|
|
if args.stdout:
|
2019-02-21 14:50:06 +01:00
|
|
|
formatter = logging.Formatter(
|
|
|
|
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
2017-03-30 17:26:46 +02:00
|
|
|
ch.setFormatter(formatter)
|
|
|
|
log.addHandler(ch)
|
|
|
|
|
2017-03-30 18:18:33 +02:00
|
|
|
# Read in the remote server configurations
|
2019-02-21 14:50:06 +01:00
|
|
|
config_file = "{}/remote-servers.yml".format(
|
|
|
|
os.path.expanduser(args.configdir))
|
|
|
|
|
|
|
|
log.debug("Opening config file %s", config_file)
|
|
|
|
with open(config_file, "r") as f:
|
2020-05-07 01:58:21 +02:00
|
|
|
config = yaml.load(f.read(), Loader=Loader)
|
2017-03-30 17:26:46 +02:00
|
|
|
log.debug("Config read %s", config)
|
|
|
|
|
2017-03-30 18:18:33 +02:00
|
|
|
# Read in the local server configuration
|
2019-02-21 14:50:06 +01:00
|
|
|
local_config = "{}/local-server.yml".format(os.path.expanduser(args.configdir))
|
2017-03-30 18:18:33 +02:00
|
|
|
log.debug("Reading local server config")
|
2019-02-21 14:50:06 +01:00
|
|
|
with open(local_config, "r") as f:
|
2020-05-07 01:58:21 +02:00
|
|
|
local_config = yaml.load(f.read(), Loader=Loader)
|
2017-03-30 18:18:33 +02:00
|
|
|
|
|
|
|
# Attempt to make contact with the local server
|
|
|
|
log.info("Connecting to local server...")
|
2019-02-21 14:50:06 +01:00
|
|
|
local_client = create_client(host=local_config["host"],
|
|
|
|
port=local_config["port"],
|
|
|
|
discovery_path=local_config["discovery_path"],
|
|
|
|
use_https=local_config["use_https"],
|
|
|
|
version=local_config["taxii_version"],
|
|
|
|
headers=local_config["headers"])
|
|
|
|
|
|
|
|
local_client.username = local_config["auth"]["username"]
|
|
|
|
local_client.password = local_config["auth"]["password"]
|
|
|
|
|
|
|
|
|
|
|
|
local_inbox = "{}://{}:{}{}".format(
|
|
|
|
"https" if local_config["use_https"] else "http",
|
|
|
|
local_config["host"], local_config["port"],
|
|
|
|
local_config["inbox_path"])
|
2017-03-31 11:24:32 +02:00
|
|
|
|
2017-03-30 18:18:33 +02:00
|
|
|
# Check that we're all good and authenticated
|
|
|
|
try:
|
2019-02-21 14:50:06 +01:00
|
|
|
list(local_client.discover_services())
|
2017-03-30 18:18:33 +02:00
|
|
|
except Exception as ex:
|
|
|
|
log.fatal("Could not connect to local server")
|
|
|
|
log.fatal(ex)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-03-30 18:29:41 +02:00
|
|
|
log.info("Connected")
|
|
|
|
|
2018-02-09 11:07:30 +01:00
|
|
|
subscription_id = args.subscription_id
|
2018-02-13 12:58:26 +01:00
|
|
|
poll_from = datetime.strptime(args.start, "%Y-%m-%dT%H:%M:%S") if args.start else None
|
|
|
|
poll_to = datetime.strptime(args.end, "%Y-%m-%dT%H:%M:%S") if args.end else datetime.now()
|
|
|
|
|
|
|
|
timezone = args.tz
|
|
|
|
# Try to cast to pytz
|
|
|
|
try:
|
|
|
|
timezone = pytz.timezone(timezone)
|
|
|
|
except pytz.exceptions.UnknownTimeZoneError:
|
|
|
|
log.fatal("Timezone %s unknown", timezone)
|
|
|
|
log.fatal("Please select one of %s", ", ".join(pytz.all_timezones))
|
|
|
|
log.fatal("That's case sensitive!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Add timezone info
|
|
|
|
if poll_from:
|
|
|
|
# (may not exist)
|
|
|
|
poll_from = poll_from.replace(tzinfo=pytz.timezone(args.tz))
|
|
|
|
|
|
|
|
poll_to = poll_to.replace(tzinfo=pytz.timezone(args.tz))
|
|
|
|
|
|
|
|
log.info("Set poll time to %s - %s", poll_from, poll_to)
|
2018-02-06 14:35:31 +01:00
|
|
|
|
2017-03-30 17:26:46 +02:00
|
|
|
for server in config:
|
|
|
|
log.info("== %s ==", server["name"])
|
2017-06-21 16:38:23 +02:00
|
|
|
|
|
|
|
log.debug("Creating client")
|
|
|
|
log.debug("HOST:PORT : %s:%s", server["host"], server["port"])
|
|
|
|
log.debug("DISCPATH: %s", server["discovery_path"])
|
2019-02-21 14:50:06 +01:00
|
|
|
|
|
|
|
# Standard autodiscovery
|
|
|
|
client_args = {
|
|
|
|
"host": server["host"],
|
|
|
|
"port": server["port"],
|
|
|
|
"discovery_path": server["discovery_path"],
|
|
|
|
"use_https": server["use_https"],
|
|
|
|
"version": server["taxii_version"],
|
|
|
|
"headers": server["headers"]
|
|
|
|
}
|
|
|
|
|
|
|
|
cli = create_client(**client_args)
|
2017-03-30 17:26:46 +02:00
|
|
|
|
2017-06-21 16:38:23 +02:00
|
|
|
log.debug("Setting client log level")
|
|
|
|
cli.log.setLevel(logging.DEBUG if args.verbose else logging.INFO)
|
|
|
|
|
|
|
|
log.debug("Setting authentication...")
|
2019-02-21 14:50:06 +01:00
|
|
|
cli.set_auth(username=server["auth"]["username"],
|
|
|
|
password=server["auth"]["password"],
|
|
|
|
ca_cert=server["auth"].get("ca_cert"),
|
|
|
|
cert_file=server["auth"].get("cert_file"),
|
|
|
|
key_file=server["auth"].get("key_file"),
|
|
|
|
key_password=server["auth"].get("key_password"),
|
|
|
|
jwt_auth_url=server["auth"].get("jwt_auth_url"),
|
|
|
|
verify_ssl=server["auth"].get("verify_ssl"))
|
2017-03-30 17:26:46 +02:00
|
|
|
|
2017-06-21 16:38:23 +02:00
|
|
|
log.debug("Discovering services...")
|
|
|
|
services = cli.discover_services()
|
|
|
|
log.debug(services)
|
|
|
|
|
|
|
|
log.debug("Auth set.")
|
2017-03-30 18:18:33 +02:00
|
|
|
for collection in server["collections"]:
|
2017-06-21 16:38:23 +02:00
|
|
|
log.debug("Polling %s", collection)
|
2019-04-02 11:36:55 +02:00
|
|
|
server_uri_override = server.get("uri", "")
|
2019-02-21 14:50:06 +01:00
|
|
|
if not server_uri_override.startswith("http"):
|
|
|
|
server_uri_override = None
|
|
|
|
if server_uri_override:
|
|
|
|
log.debug("Poll URL override set to %s", server_uri_override)
|
|
|
|
|
|
|
|
log.debug("Within date range %s - %s",
|
|
|
|
poll_from or "Beginning of time", poll_to)
|
2017-06-21 17:01:12 +02:00
|
|
|
try:
|
2018-02-07 18:14:59 +01:00
|
|
|
for content_block in cli.poll(collection_name=collection,
|
|
|
|
subscription_id=subscription_id,
|
2018-02-06 14:35:31 +01:00
|
|
|
begin_date=poll_from,
|
2019-02-21 14:50:06 +01:00
|
|
|
end_date=poll_to,
|
|
|
|
uri=server.get("uri", None)):
|
2017-06-21 17:01:12 +02:00
|
|
|
try:
|
|
|
|
log.debug("Pushing block %s", content_block)
|
2019-02-21 14:50:06 +01:00
|
|
|
local_client.push(
|
|
|
|
content_block.content.decode("utf-8"),
|
|
|
|
collection_names=local_config["collections"],
|
|
|
|
content_binding=content_block.binding,
|
|
|
|
uri=local_inbox)
|
2017-06-21 17:01:12 +02:00
|
|
|
except Exception as ex:
|
|
|
|
log.error("FAILED TO PUSH BLOCK!")
|
|
|
|
log.error("%s", content_block)
|
|
|
|
log.exception(ex, exc_info=True)
|
|
|
|
except Exception as ex:
|
|
|
|
log.error("FAILED TO POLL %s", collection)
|
|
|
|
log.exception(ex, exc_info=True)
|
2017-03-31 11:11:36 +02:00
|
|
|
|
|
|
|
log.info("Finished!")
|