2019-11-14 16:05:58 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Blueprint Flask: crawler splash endpoints: dashboard, onion crawler ...
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
|
2019-11-19 08:02:25 +00:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for, Response, abort
|
2019-11-14 16:05:58 +00:00
|
|
|
from flask_login import login_required, current_user, login_user, logout_user
|
|
|
|
|
|
|
|
sys.path.append('modules')
|
|
|
|
import Flask_config
|
|
|
|
|
|
|
|
# Import Role_Manager
|
2019-11-20 15:15:08 +00:00
|
|
|
from Role_Manager import login_admin, login_analyst, login_read_only
|
2019-11-14 16:05:58 +00:00
|
|
|
|
2022-08-19 14:53:31 +00:00
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.objects import ail_objects
|
2023-04-05 14:09:06 +00:00
|
|
|
from lib import Tag
|
2022-08-19 14:53:31 +00:00
|
|
|
|
2019-11-14 16:05:58 +00:00
|
|
|
bootstrap_label = Flask_config.bootstrap_label
|
2019-11-15 16:22:50 +00:00
|
|
|
vt_enabled = Flask_config.vt_enabled
|
2019-11-14 16:05:58 +00:00
|
|
|
|
|
|
|
# ============ BLUEPRINT ============
|
2023-04-05 14:09:06 +00:00
|
|
|
correlation = Blueprint('correlation', __name__,
|
|
|
|
template_folder=os.path.join(os.environ['AIL_FLASK'], 'templates/correlation'))
|
2019-11-14 16:05:58 +00:00
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
|
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
|
|
|
def sanitise_graph_mode(graph_mode):
|
|
|
|
if graph_mode not in ('inter', 'union'):
|
|
|
|
return 'union'
|
|
|
|
else:
|
|
|
|
return graph_mode
|
|
|
|
|
|
|
|
def sanitise_nb_max_nodes(nb_max_nodes):
|
|
|
|
try:
|
|
|
|
nb_max_nodes = int(nb_max_nodes)
|
|
|
|
if nb_max_nodes < 2:
|
|
|
|
nb_max_nodes = 300
|
2023-04-05 14:09:06 +00:00
|
|
|
except (TypeError, ValueError):
|
2019-11-14 16:05:58 +00:00
|
|
|
nb_max_nodes = 300
|
|
|
|
return nb_max_nodes
|
|
|
|
|
|
|
|
# ============= ROUTES ==============
|
2023-04-05 14:09:06 +00:00
|
|
|
@correlation.route('/correlation/show', methods=['GET', 'POST'])
|
2019-11-14 16:05:58 +00:00
|
|
|
@login_required
|
2019-11-20 15:15:08 +00:00
|
|
|
@login_read_only
|
2019-11-15 16:22:50 +00:00
|
|
|
def show_correlation():
|
2019-11-14 16:05:58 +00:00
|
|
|
if request.method == 'POST':
|
2022-10-25 14:25:19 +00:00
|
|
|
object_type = request.form.get('obj_type')
|
|
|
|
subtype = request.form.get('subtype')
|
|
|
|
obj_id = request.form.get('obj_id')
|
2019-11-14 16:05:58 +00:00
|
|
|
max_nodes = request.form.get('max_nb_nodes_in')
|
|
|
|
mode = request.form.get('mode')
|
|
|
|
if mode:
|
|
|
|
mode = 'inter'
|
|
|
|
else:
|
|
|
|
mode = 'union'
|
|
|
|
|
|
|
|
## get all selected correlations
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types = []
|
|
|
|
correl_option = request.form.get('CveCheck')
|
|
|
|
if correl_option:
|
|
|
|
filter_types.append('cve')
|
2019-11-14 16:05:58 +00:00
|
|
|
correl_option = request.form.get('CryptocurrencyCheck')
|
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('cryptocurrency')
|
2019-11-14 16:05:58 +00:00
|
|
|
correl_option = request.form.get('PgpCheck')
|
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('pgp')
|
2020-05-11 16:11:38 +00:00
|
|
|
correl_option = request.form.get('UsernameCheck')
|
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('username')
|
2019-11-14 16:05:58 +00:00
|
|
|
correl_option = request.form.get('DecodedCheck')
|
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('decoded')
|
2019-12-17 14:13:36 +00:00
|
|
|
correl_option = request.form.get('ScreenshotCheck')
|
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('screenshot')
|
2019-11-14 16:05:58 +00:00
|
|
|
# correlation_objects
|
|
|
|
correl_option = request.form.get('DomainCheck')
|
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('domain')
|
|
|
|
correl_option = request.form.get('ItemCheck')
|
2019-11-14 16:05:58 +00:00
|
|
|
if correl_option:
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types.append('item')
|
2019-11-14 16:05:58 +00:00
|
|
|
|
|
|
|
# list as params
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types = ",".join(filter_types)
|
2019-11-14 16:05:58 +00:00
|
|
|
|
|
|
|
# redirect to keep history and bookmark
|
2022-10-25 14:25:19 +00:00
|
|
|
return redirect(url_for('correlation.show_correlation', type=object_type, subtype=subtype, id=obj_id, mode=mode,
|
2023-04-05 14:09:06 +00:00
|
|
|
max_nodes=max_nodes, filter=filter_types))
|
2019-11-14 16:05:58 +00:00
|
|
|
|
|
|
|
# request.method == 'GET'
|
|
|
|
else:
|
2022-10-25 14:25:19 +00:00
|
|
|
obj_type = request.args.get('type')
|
|
|
|
subtype = request.args.get('subtype', '')
|
|
|
|
obj_id = request.args.get('id')
|
2019-11-14 16:05:58 +00:00
|
|
|
max_nodes = sanitise_nb_max_nodes(request.args.get('max_nodes'))
|
|
|
|
mode = sanitise_graph_mode(request.args.get('mode'))
|
|
|
|
|
2023-04-06 13:22:04 +00:00
|
|
|
related_btc = bool(request.args.get('related_btc', False))
|
2019-11-22 14:30:11 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types = ail_objects.sanitize_objs_types(request.args.get('filter', '').split(','))
|
2019-11-14 16:05:58 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
# check if obj_id exist
|
|
|
|
if not ail_objects.exists_obj(obj_type, subtype, obj_id):
|
2023-04-05 14:09:06 +00:00
|
|
|
return abort(404)
|
2022-10-25 14:25:19 +00:00
|
|
|
# object exist
|
2019-11-19 08:02:25 +00:00
|
|
|
else:
|
2022-10-25 14:25:19 +00:00
|
|
|
dict_object = {"object_type": obj_type,
|
|
|
|
"correlation_id": obj_id,
|
|
|
|
"max_nodes": max_nodes, "mode": mode,
|
|
|
|
"filter": filter_types, "filter_str": ",".join(filter_types),
|
2023-04-06 14:18:06 +00:00
|
|
|
"metadata": ail_objects.get_object_meta(obj_type, subtype, obj_id,
|
|
|
|
options={'tags'}, flask_context=True)
|
2022-10-25 14:25:19 +00:00
|
|
|
}
|
|
|
|
if subtype:
|
|
|
|
dict_object["metadata"]['type_id'] = subtype
|
|
|
|
dict_object["metadata_card"] = ail_objects.get_object_card_meta(obj_type, subtype, obj_id, related_btc=related_btc)
|
2023-04-05 14:09:06 +00:00
|
|
|
return render_template("show_correlation.html", dict_object=dict_object, bootstrap_label=bootstrap_label,
|
|
|
|
tags_selector_data=Tag.get_tags_selector_data())
|
2019-11-19 08:02:25 +00:00
|
|
|
|
2019-12-09 15:56:07 +00:00
|
|
|
@correlation.route('/correlation/get/description')
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def get_description():
|
|
|
|
object_id = request.args.get('object_id')
|
|
|
|
object_id = object_id.split(';')
|
|
|
|
# unpack object_id # # TODO: put me in lib
|
|
|
|
if len(object_id) == 3:
|
|
|
|
object_type = object_id[0]
|
|
|
|
type_id = object_id[1]
|
|
|
|
correlation_id = object_id[2]
|
|
|
|
elif len(object_id) == 2:
|
|
|
|
object_type = object_id[0]
|
|
|
|
type_id = None
|
|
|
|
correlation_id = object_id[1]
|
|
|
|
else:
|
|
|
|
return jsonify({})
|
|
|
|
|
|
|
|
# check if correlation_id exist
|
|
|
|
# # TODO: return error json
|
2022-09-20 14:11:48 +00:00
|
|
|
if not ail_objects.exists_obj(object_type, type_id, correlation_id):
|
2019-12-10 14:41:47 +00:00
|
|
|
return Response(json.dumps({"status": "error", "reason": "404 Not Found"}, indent=2, sort_keys=True), mimetype='application/json'), 404
|
2023-02-28 10:01:27 +00:00
|
|
|
# object exist
|
2019-12-09 15:56:07 +00:00
|
|
|
else:
|
2023-04-06 14:18:06 +00:00
|
|
|
res = ail_objects.get_object_meta(object_type, type_id, correlation_id, options={'tags'}, flask_context=True)
|
|
|
|
if 'tags' in res:
|
|
|
|
res['tags'] = list(res['tags'])
|
2019-12-09 15:56:07 +00:00
|
|
|
return jsonify(res)
|
2019-11-14 16:05:58 +00:00
|
|
|
|
|
|
|
@correlation.route('/correlation/graph_node_json')
|
|
|
|
@login_required
|
2019-11-20 15:15:08 +00:00
|
|
|
@login_read_only
|
2019-12-09 15:56:07 +00:00
|
|
|
def graph_node_json():
|
2022-10-25 14:25:19 +00:00
|
|
|
obj_id = request.args.get('id')
|
|
|
|
subtype = request.args.get('subtype')
|
|
|
|
obj_type = request.args.get('type')
|
2019-11-14 16:05:58 +00:00
|
|
|
max_nodes = sanitise_nb_max_nodes(request.args.get('max_nodes'))
|
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
filter_types = ail_objects.sanitize_objs_types(request.args.get('filter', '').split(','))
|
2019-11-14 16:05:58 +00:00
|
|
|
|
2022-08-19 14:53:31 +00:00
|
|
|
json_graph = ail_objects.get_correlations_graph_node(obj_type, subtype, obj_id, filter_types=filter_types, max_nodes=max_nodes, level=2, flask_context=True)
|
|
|
|
#json_graph = Correlate_object.get_graph_node_object_correlation(obj_type, obj_id, 'union', correlation_names, correlation_objects, requested_correl_type=subtype, max_nodes=max_nodes)
|
|
|
|
return jsonify(json_graph)
|
2022-04-28 13:57:00 +00:00
|
|
|
|
2023-04-06 13:13:27 +00:00
|
|
|
@correlation.route('/correlation/delete', methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_admin
|
|
|
|
def correlation_delete():
|
|
|
|
obj_type = request.args.get('type')
|
|
|
|
subtype = request.args.get('subtype', '')
|
|
|
|
obj_id = request.args.get('id')
|
|
|
|
|
|
|
|
if not ail_objects.exists_obj(obj_type, subtype, obj_id):
|
|
|
|
return abort(404)
|
|
|
|
|
|
|
|
ail_objects.delete_obj_correlations(obj_type, subtype, obj_id)
|
|
|
|
return redirect(url_for('correlation.show_correlation', type=obj_type, subtype=subtype, id=obj_id))
|
|
|
|
|
2023-04-05 14:09:06 +00:00
|
|
|
@correlation.route('/correlation/tags/add', methods=['POST'])
|
|
|
|
@login_required
|
2023-04-06 13:13:27 +00:00
|
|
|
@login_analyst
|
2023-04-05 14:09:06 +00:00
|
|
|
def correlation_tags_add():
|
|
|
|
obj_id = request.form.get('tag_obj_id')
|
|
|
|
subtype = request.form.get('tag_subtype', '')
|
|
|
|
obj_type = request.form.get('tag_obj_type')
|
|
|
|
nb_max = sanitise_nb_max_nodes(request.form.get('tag_nb_max'))
|
|
|
|
filter_types = ail_objects.sanitize_objs_types(request.form.get('tag_filter', '').split(','))
|
|
|
|
|
|
|
|
if not ail_objects.exists_obj(obj_type, subtype, obj_id):
|
|
|
|
return abort(404)
|
|
|
|
|
|
|
|
# tags
|
|
|
|
taxonomies_tags = request.form.get('taxonomies_tags')
|
|
|
|
if taxonomies_tags:
|
|
|
|
try:
|
|
|
|
taxonomies_tags = json.loads(taxonomies_tags)
|
|
|
|
except Exception:
|
|
|
|
taxonomies_tags = []
|
|
|
|
else:
|
|
|
|
taxonomies_tags = []
|
|
|
|
galaxies_tags = request.form.get('galaxies_tags')
|
|
|
|
if galaxies_tags:
|
|
|
|
try:
|
|
|
|
galaxies_tags = json.loads(galaxies_tags)
|
|
|
|
except Exception:
|
|
|
|
galaxies_tags = []
|
|
|
|
if taxonomies_tags or galaxies_tags:
|
|
|
|
if not Tag.is_valid_tags_taxonomies_galaxy(taxonomies_tags, galaxies_tags):
|
|
|
|
return {'error': 'Invalid tag(s)'}, 400
|
|
|
|
tags = taxonomies_tags + galaxies_tags
|
|
|
|
else:
|
|
|
|
tags = []
|
|
|
|
|
|
|
|
if tags:
|
|
|
|
ail_objects.obj_correlations_objs_add_tags(obj_type, subtype, obj_id, tags, filter_types=filter_types, lvl=2, nb_max=nb_max)
|
|
|
|
|
|
|
|
return redirect(url_for('correlation.show_correlation',
|
|
|
|
type=obj_type, subtype=subtype, id=obj_id,
|
|
|
|
filter=",".join(filter_types)))
|