From 03e4f79a76f0b645ab2ce5fba00019e3dc11a63d Mon Sep 17 00:00:00 2001 From: David Cruciani Date: Thu, 22 Feb 2024 11:31:14 +0100 Subject: [PATCH] add: [website] query as same --- website/app/home.py | 30 ++++++++++++++++- website/app/home_core.py | 57 ++++++++++++++++++++------------ website/app/session.py | 44 ++++++++++++++++++------ website/app/templates/query.html | 33 +++++++++++++++++- 4 files changed, 130 insertions(+), 34 deletions(-) diff --git a/website/app/home.py b/website/app/home.py index 6c2ea422..bc634d94 100644 --- a/website/app/home.py +++ b/website/app/home.py @@ -46,6 +46,31 @@ def query(sid): +@home_blueprint.route("/get_query_info/") +def get_query_info(sid): + """Return info for a query""" + session = HomeModel.get_session(sid) + flag=False + if session: + flag = True + query_loc = session.query_enter + else: + for s in SessionModel.sessions: + if s.uuid == sid: + flag = True + query_loc = s.query + session=s + if flag: + loc_dict = { + "query": query_loc, + "input_query": session.input_query, + "modules": json.loads(session.modules_list), + "query_date": session.query_date.strftime('%Y-%m-%d %H:%M') + } + return loc_dict + return {"message": "Session not found"}, 404 + + @home_blueprint.route("/get_modules") def get_modules(): """Return all modules available""" @@ -70,7 +95,10 @@ def run_modules(): if "query" in request.json: if "input" in request.json: if "modules" in request.json: - session = SessionModel.Session_class(request.json) + if "query_as_same" in request.json: + session = SessionModel.Session_class(request.json, query_as_same=True, parent_id=request.json["parent_id"]) + else: + session = SessionModel.Session_class(request.json) HomeModel.set_flask_session(session, request.json["parent_id"]) session.start() SessionModel.sessions.append(session) diff --git a/website/app/home_core.py b/website/app/home_core.py index 174d42ec..9d08acca 100644 --- a/website/app/home_core.py +++ b/website/app/home_core.py @@ -148,6 +148,29 @@ def get_history(): +def create_new_session_tree(current_session, parent_id): + loc_session = get_session(parent_id) + loc_json = { + "uuid": loc_session.uuid, + "modules": json.loads(loc_session.modules_list), + "query": loc_session.query_enter, + "input": loc_session.input_query, + "query_date": loc_session.query_date.strftime('%Y-%m-%d %H:%M'), + "config": json.loads(loc_session.config_module) + } + loc_json_child = { + "uuid": current_session.uuid, + "modules": current_session.modules_list, + "query": current_session.query, + "input": current_session.input_query, + "query_date": current_session.query_date.strftime('%Y-%m-%d'), + "config": current_session.config_module, + "children": [] + } + + sess["current_query"] = loc_session.uuid + sess[sess.get("current_query")] = loc_json + sess[sess.get("current_query")]["children"] = [loc_json_child] def util_set_flask_session(parent_id, loc_session, current_session): if parent_id == loc_session["uuid"]: @@ -156,7 +179,8 @@ def util_set_flask_session(parent_id, loc_session, current_session): "modules": current_session.modules_list, "query": current_session.query, "input": current_session.input_query, - "query_date": current_session.query_date.strftime('%Y-%m-%d') + "query_date": current_session.query_date.strftime('%Y-%m-%d %H:%M'), + "config": current_session.config_module } loc_session["children"].append(loc_json) return True @@ -172,23 +196,14 @@ def deep_explore(session_dict, parent_id, current_session): return False def set_flask_session(current_session, parent_id): - current_query = sess.get("current_query") - if not current_query or current_query not in sess: - loc_json = { - "uuid": current_session.uuid, - "modules": current_session.modules_list, - "query": current_session.query, - "input": current_session.input_query, - "query_date": current_session.query_date.strftime('%Y-%m-%d') - } - - sess["current_query"] = current_session.uuid - sess[sess.get("current_query")] = loc_json - sess[sess.get("current_query")]["children"] = list() - else: - # sess["uuid"] - loc_session = sess.get(sess.get("current_query")) - if not "children" in loc_session: - loc_session["children"] = list() - if not util_set_flask_session(parent_id, loc_session, current_session): - sess["current_query"] = current_session.uuid + if parent_id: + current_query = sess.get("current_query") + if not current_query or current_query not in sess: + create_new_session_tree(current_session, parent_id) + else: + # sess["uuid"] + loc_session = sess.get(sess.get("current_query")) + if not "children" in loc_session: + loc_session["children"] = list() + if not util_set_flask_session(parent_id, loc_session, current_session): + create_new_session_tree(current_session, parent_id) diff --git a/website/app/session.py b/website/app/session.py index d0165f8c..a3b60f93 100644 --- a/website/app/session.py +++ b/website/app/session.py @@ -8,11 +8,12 @@ from . import home_core as HomeModel import uuid from . import db from .db_class.db import History, History_Tree, Session_db +from flask import session as sess sessions = list() class Session_class: - def __init__(self, request_json) -> None: + def __init__(self, request_json, query_as_same=False, parent_id=None) -> None: self.uuid = str(uuid4()) self.thread_count = 4 self.jobs = Queue(maxsize=0) @@ -24,20 +25,41 @@ class Session_class: self.input_query = request_json["input"] self.modules_list = request_json["modules"] self.nb_errors = 0 - self.config_module = self.config_module_setter(request_json) + self.config_module = self.config_module_setter(request_json, query_as_same, parent_id) self.query_date = datetime.datetime.now(tz=datetime.timezone.utc) - def config_module_setter(self, request_json): + def util_config_as_same(self, child, parent_id): + if child["uuid"] == parent_id: + return child["config"] + elif "children" in child: + for c in child["children"]: + return self.util_config_as_same(c, parent_id) + + + def config_module_setter(self, request_json, query_as_same, parent_id): """Setter for config for all modules used""" - for query in self.modules_list: - if not query in request_json["config"]: - request_json["config"][query] = {} - module = HomeModel.get_module_by_name(query) - mcs = HomeModel.get_module_config_module(module.id) - for mc in mcs: - config_db = HomeModel.get_config(mc.config_id) - request_json["config"][query][config_db.name] = mc.value + flag = False + if query_as_same: + current_query_val = sess.get(sess.get("current_query")) + if current_query_val: + if current_query_val["uuid"] == parent_id: + return current_query_val["config"] + else: + for child in current_query_val["children"]: + res = self.util_config_as_same(child, parent_id) + if res: + flag = True + return res + if not flag: + for query in self.modules_list: + if not query in request_json["config"]: + request_json["config"][query] = {} + module = HomeModel.get_module_by_name(query) + mcs = HomeModel.get_module_config_module(module.id) + for mc in mcs: + config_db = HomeModel.get_config(mc.config_id) + request_json["config"][query][config_db.name] = mc.value return request_json["config"] def start(self): diff --git a/website/app/templates/query.html b/website/app/templates/query.html index fd883692..6de28bee 100644 --- a/website/app/templates/query.html +++ b/website/app/templates/query.html @@ -197,14 +197,21 @@ const status_site = ref() const tab_list = ref("visual") const history = ref({}) + const query_info = ref({}) function actionQuery(){ is_searching.value = true - sid.value = $("#share").val() pollScan(); } + async function queryInfo(){ + sid.value = $("#share").val() + let res = await fetch("/get_query_info/" + sid.value) + let loc = await res.json() + query_info.value = loc + } + function pollScan() { // Loop function to update the list of identified domains $.getJSON('/status/' + sid.value, function(data) { @@ -267,13 +274,36 @@ history.value = loc } + async function query_as_same(value){ + let result_dict = {"modules": query_info.value["modules"], + "input": query_info.value["input_query"], + "query": value, + "parent_id": sid.value, + "query_as_same": true, + "config": {} + } + const res = await fetch('/run_modules',{ + headers: { "X-CSRFToken": $("#csrf_token").val(), "Content-Type": "application/json" }, + method: "POST", + body: JSON.stringify(result_dict) + }) + if(await res.status == 201){ + let loc = await res.json() + await nextTick() + window.location.href="/query/" + loc['id'] + } + } + onMounted(() => { + queryInfo() actionQuery() get_history_session() + window._query_as_same = query_as_same }) return { message_list, + query_info, progress, status_site, is_searching, @@ -283,6 +313,7 @@ generateCoreFormatUI, parseMispObject, active_tab, + query_as_same } } }).mount('.container-fluid')