First commit!

This commit is contained in:
sda
2022-11-06 15:51:33 +01:00
parent 283cf9630f
commit 64daa44e9f
225 changed files with 94329 additions and 1 deletions

View File

@ -0,0 +1,131 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Blueprint, request, jsonify
from app.decorators import *
from app.classes.config import Config
from app.utils import get_device_uuid
import sys
config_bp = Blueprint("config", __name__)
config = Config()
@config_bp.route('/switch/<cat>/<key>', methods=['GET'])
@require_header_token
def switch(cat, key):
"""Switch the Boolean value of a configuration key.
Args:
cat (str): configuration category
key (key): configuration key
Returns:
dict: operation status
"""
try:
value = config.read_config((cat, key))
if value:
config.write_config(cat, key, False)
res = {"status": True,
"message": "Key switched to false"}
else:
config.write_config(cat, key, True)
res = {"status": True,
"message": "Key switched to true"}
except:
res = {"status": True,
"message": "Issue while changing value"}
return jsonify(res)
@config_bp.route('/ioc-type/add/<tag>', methods=['GET'])
@require_header_token
def ioc_type_add(tag):
"""Add an IOC type - defined via its tag - in the
configuration file for detection.
Args:
tag (str): IOC tag
Returns:
dict: operation status
"""
return jsonify(config.ioc_type_add(tag))
@config_bp.route('/ioc-type/delete/<tag>', methods=['GET'])
@require_header_token
def ioc_type_delete(tag):
"""Delete an IOC type - defined via its tag - in the
configuration file for detection.
Args:
tag (str): IOC tag
Returns:
dict: operation status
"""
return jsonify(config.ioc_type_delete(tag))
@config_bp.route('/edit/<cat>/<key>/<path:value>', methods=['GET'])
@require_header_token
def edit(cat, key, value):
"""Edit the string (or array) value of a configuration key.
Args:
cat (str): configuration category
key (str): configuration key
value (any): configuration value
Returns:
dict: operation status
"""
return jsonify(config.write_config(cat, key, value))
@config_bp.route('/db/export', methods=['GET'])
@require_get_token
def export_db():
"""Export the database.
Returns:
dict: the raw database
"""
return config.export_db()
@config_bp.route('/db/import', methods=['POST'])
@require_header_token
def import_db():
"""Import a database via Flash methods
and replace the existant.
Returns:
dict: operation status
"""
try:
f = request.files["file"]
assert f.read(15) == b"SQLite format 3"
d = "/".join(sys.path[0].split("/")[:-2])
f.save("/{}/database.sqlite3".format(d))
res = {"status": True,
"message": "Database updated"}
except:
res = {"status": False,
"message": "Error while database upload"}
return jsonify(res)
@config_bp.route('/list', methods=['GET'])
def list():
"""List key, values of the configuration
Returns:
dict: configuration content
"""
res = config.export_config()
res["backend"]["password"] = ""
res["device_uuid"] = get_device_uuid()
return jsonify(res)

View File

@ -0,0 +1,97 @@
#!/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
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)
@ioc_bp.route('/delete/<ioc_id>', methods=['GET'])
@require_header_token
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)
@ioc_bp.route('/search/<term>', methods=['GET'])
@require_header_token
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]})
@ioc_bp.route('/get/types')
@require_header_token
def get_types():
"""
Retreive a list of IOCs types.
:return: list of types in JSON.
"""
res = IOCs.get_types()
return jsonify({"types": [t for t in res]})
@ioc_bp.route('/get/tags')
@require_header_token
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]})
@ioc_bp.route('/export')
@require_get_token
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'})

View File

@ -0,0 +1,42 @@
#!/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.misp import MISP
import json
misp_bp = Blueprint("misp", __name__)
misp = MISP()
@misp_bp.route('/add', methods=['POST'])
@require_header_token
def add_instance():
"""
Parse and add a MISP instance to the database.
:return: status of the operation in JSON
"""
data = json.loads(request.data)
res = misp.add_instance(data["data"]["instance"])
return jsonify(res)
@misp_bp.route('/delete/<misp_id>', methods=['GET'])
@require_header_token
def delete_instance(misp_id):
"""
Delete a MISP instance by its id to the database.
:return: status of the operation in JSON
"""
res = misp.delete_instance(misp_id)
return jsonify(res)
@misp_bp.route('/get_all', methods=['GET'])
@require_header_token
def get_all():
"""
Retreive a list of all MISP instances.
:return: list of MISP instances in JSON.
"""
res = misp.get_instances()
return jsonify({"results": [i for i in res]})

View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import jsonify, Blueprint
from app.classes.update import Update
from app.decorators import require_header_token
update_bp = Blueprint("update", __name__)
@update_bp.route("/check", methods=["GET"])
@require_header_token
def check():
""" Check the presence of new version """
return jsonify(Update().check_version())
@update_bp.route("/get-version", methods=["GET"])
def get_version():
""" Check the current version """
return jsonify(Update().get_current_version())
@update_bp.route("/process", methods=["GET"])
@require_header_token
def process():
""" Check the presence of new version """
return jsonify(Update().update_instance())

View File

@ -0,0 +1,42 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Blueprint, jsonify, request
from app.decorators import require_header_token
from app.classes.watchers import Watcher
import json
watchers_bp = Blueprint("watchers", __name__)
watcher = Watcher()
@watchers_bp.route('/add', methods=['POST'])
@require_header_token
def add_instance():
"""
Parse and add a watcher instance.
:return: status of the operation in JSON
"""
data = json.loads(request.data)
res = watcher.add_instance(data["data"]["instance"])
return jsonify(res)
@watchers_bp.route('/delete/<watcher_id>', methods=['GET'])
@require_header_token
def delete_instance(watcher_id):
"""
Delete a watcher by its id.
:return: status of the operation in JSON
"""
res = watcher.delete_instance(watcher_id)
return jsonify(res)
@watchers_bp.route('/get_all', methods=['GET'])
@require_header_token
def get_all():
"""
Retreive a list of all watchers.
:return: list of watcher instances in JSON.
"""
res = watcher.get_instances()
return jsonify({"results": [i for i in res]})

View File

@ -0,0 +1,68 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Blueprint, jsonify, Response
from app.decorators import require_header_token, require_get_token
from app.classes.whitelist import WhiteList
import json
whitelist_bp = Blueprint("whitelist", __name__)
whitelist = WhiteList()
@whitelist_bp.route('/add/<elem_type>/<path:elem_value>', methods=['GET'])
@require_header_token
def add(elem_type, elem_value):
"""
Parse and add an element to be whitelisted.
:return: status of the operation in JSON
"""
source = "backend"
res = whitelist.add(elem_type, elem_value, source)
return jsonify(res)
@whitelist_bp.route('/delete/<elem_id>', methods=['GET'])
@require_header_token
def delete(elem_id):
"""
Delete an element by its id to the database.
:return: status of the operation in JSON
"""
res = whitelist.delete(elem_id)
return jsonify(res)
@whitelist_bp.route('/search/<element>', methods=['GET'])
@require_header_token
def search(element):
"""
Search elements in the database.
:return: potential results in JSON.
"""
res = whitelist.search(element)
return jsonify({"results": [e for e in res]})
@whitelist_bp.route('/get/types')
@require_header_token
def get_types():
"""
Retrieve a list of whitelisted elements types.
:return: list of types in JSON.
"""
res = whitelist.get_types()
return jsonify({"types": [t for t in res]})
@whitelist_bp.route('/export')
@require_get_token
def get_all():
"""
Retreive a list of all elements.
:return: list of elements in JSON.
"""
res = whitelist.get_all()
return Response(json.dumps({"elements": [e for e in res]}),
mimetype='application/json',
headers={'Content-Disposition': 'attachment;filename=whitelist-export.json'})