def search(term): """ Search IOCs in the database. :return: potential results in JSON. """ res = IOCs.search(term) return jsonify({"results": [i for i in res]})
def get_tags(): """ Retreive a list of IOCs tags. :return: list of types in JSON. """ res = IOCs.get_tags() return jsonify({"tags": [t for t in res]})
def delete(ioc_id): """ Delete an IOC by its id to the database. :return: status of the operation in JSON """ res = IOCs.delete(ioc_id) return jsonify(res)
def add(ioc_type, ioc_tag, ioc_tlp, ioc_value): """ Parse and add an IOC to the database. :return: status of the operation in JSON """ source = "backend" res = IOCs.add(ioc_type, ioc_tag, ioc_tlp, ioc_value, source) return jsonify(res)
def add(ioc_type, ioc_tag, ioc_tlp, ioc_value): """ Parse and add an IOC to the database. :return: status of the operation in JSON """ source = "backend" if ioc_type == "snort": ioc_value = unquote("/".join(request.full_path.split("/")[7:])) res = IOCs.add(ioc_type, ioc_tag, ioc_tlp, ioc_value, source) return jsonify(res)
def watch_misp(): """ Retrieve IOCs from misp instances. Each new element is tested and then added to the database. """ iocs, misp = IOCs(), MISP() instances = [i for i in misp.get_instances()] while instances: for i, ist in enumerate(instances): status = misp.test_instance(ist["url"], ist["apikey"], ist["verifycert"]) if status: for ioc in misp.get_iocs(ist["id"]): iocs.add(ioc["type"], ioc["tag"], ioc["tlp"], ioc["value"], "misp-{}".format(ist["id"])) misp.update_sync(ist["id"]) instances.pop(i) if instances: time.sleep(60)
def add_post(): """ Parse and add an IOC to the database using the post method. :return: status of the operation in JSON """ data = json.loads(request.data) ioc = data["data"]["ioc"] res = IOCs.add(ioc["ioc_type"], ioc["ioc_tag"], ioc["ioc_tlp"], ioc["ioc_value"], ioc["ioc_source"]) return jsonify(res)
def get_all(): """ Retreive a list of all IOCs. :return: list of iocs in JSON. """ res = IOCs.get_all() return Response(json.dumps({"iocs": [i for i in res]}), mimetype='application/json', headers={ 'Content-Disposition': 'attachment;filename=iocs-export.json' })
def watch_iocs(): """ Retrieve IOCs from the remote URLs defined in config/watchers. For each (new ?) IOC, add it to the DB. """ # Retrieve the URLs from the configuration urls = read_config(("watchers", "iocs")) watchers = [{"url": url, "status": False} for url in urls] while True: for w in watchers: if w["status"] == False: iocs = IOCs() iocs_list = [] try: res = requests.get(w["url"], verify=False) if res.status_code == 200: iocs_list = json.loads(res.content)["iocs"] else: w["status"] = False except: w["status"] = False for ioc in iocs_list: try: iocs.add(ioc["type"], ioc["tag"], ioc["tlp"], ioc["value"], "watcher") w["status"] = True except: continue # If at least one URL haven't be parsed, let's retry in 1min. if False in [w["status"] for w in watchers]: time.sleep(60) else: break
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from flask import Blueprint, jsonify, Response, request from app.decorators import require_header_token, require_get_token from app.classes.iocs import IOCs import json from urllib.parse import unquote ioc_bp = Blueprint("ioc", __name__) ioc = IOCs() @ioc_bp.route('/add/<ioc_type>/<ioc_tag>/<ioc_tlp>/<path:ioc_value>', methods=['GET']) @require_header_token def add(ioc_type, ioc_tag, ioc_tlp, ioc_value): """ Parse and add an IOC to the database. :return: status of the operation in JSON """ source = "backend" if ioc_type == "snort": ioc_value = unquote("/".join(request.full_path.split("/")[7:])) res = IOCs.add(ioc_type, ioc_tag, ioc_tlp, ioc_value, source) return jsonify(res) @ioc_bp.route('/add_post', methods=['POST']) @require_header_token