From b0dde78c2e2a8c5d937b51bd1c459fa1c5952764 Mon Sep 17 00:00:00 2001 From: Terrtia Date: Tue, 18 Feb 2020 13:47:47 +0100 Subject: [PATCH] chg: [UI MISP import]show error +imported uuid/global_id --- bin/export/MispImport.py | 19 ++++-- bin/lib/Correlate_object.py | 13 ++++ bin/lib/Decoded.py | 2 +- bin/lib/Screenshot.py | 3 +- bin/packages/Item.py | 2 +- var/www/blueprints/import_export.py | 19 ++++-- .../import_export/block_to_export_input.html | 2 +- .../import_export/export_object.html | 2 - .../import_export/import_object.html | 63 ++++++++++++++++++- 9 files changed, 107 insertions(+), 18 deletions(-) diff --git a/bin/export/MispImport.py b/bin/export/MispImport.py index 1db430b2..6365b6d8 100755 --- a/bin/export/MispImport.py +++ b/bin/export/MispImport.py @@ -207,19 +207,30 @@ def create_obj_relationships(map_uuid_global_id, misp_obj): Correlate_object.create_obj_relationship(obj_meta_src['type'], obj_meta_src['id'], obj_meta_target['type'], obj_meta_target['id'], obj1_subtype=obj_meta_src['subtype'], obj2_subtype=obj_meta_target['subtype']) +def create_map_all_obj_uuid_golbal_id(map_uuid_global_id): + for obj_uuid in map_uuid_global_id: + create_map_objuuid_golbal_id(obj_uuid, map_uuid_global_id[obj_uuid]) + +def create_map_objuuid_golbal_id(obj_uuid, global_id): + print(obj_uuid) + print(global_id) def import_objs_from_file(filepath): - event_to_import = MISPEvent() - event_to_import.load_file(filepath) - map_uuid_global_id = {} + event_to_import = MISPEvent() + try: + event_to_import.load_file(filepath) + except: + return map_uuid_global_id + for misp_obj in event_to_import.objects: get_misp_import_fct(map_uuid_global_id, misp_obj) for misp_obj in event_to_import.objects: create_obj_relationships(map_uuid_global_id, misp_obj) - #print(map_uuid_global_id) + + create_map_all_obj_uuid_golbal_id(map_uuid_global_id) return map_uuid_global_id diff --git a/bin/lib/Correlate_object.py b/bin/lib/Correlate_object.py index b8e603ee..9491a468 100755 --- a/bin/lib/Correlate_object.py +++ b/bin/lib/Correlate_object.py @@ -383,6 +383,19 @@ def get_obj_global_id(obj_type, obj_id, obj_sub_type=None): return '{}:{}'.format(obj_type, obj_id) +def get_global_id_from_id(global_id): + obj_meta = {} + global_id = global_id.split(':', 3) + if len(global_id) > 2: + obj_meta['type'] = global_id[0] + obj_meta['subtype'] = global_id[1] + obj_meta['id'] = global_id[2] + else: + obj_meta['type'] = global_id[0] + obj_meta['subtype'] = None + obj_meta['id'] = global_id[1] + return obj_meta + # used by UI def get_obj_str_type_subtype(obj_type, obj_subtype): if obj_subtype: diff --git a/bin/lib/Decoded.py b/bin/lib/Decoded.py index d00d14a5..08adb28d 100755 --- a/bin/lib/Decoded.py +++ b/bin/lib/Decoded.py @@ -324,7 +324,7 @@ def save_decoded_file_content(sha1_string, file_content, date_from, date_to=None filepath = get_decoded_filepath(sha1_string, mimetype=mimetype) if os.path.isfile(filepath): - print('File already exist') + #print('File already exist') return False # create dir diff --git a/bin/lib/Screenshot.py b/bin/lib/Screenshot.py index def97ecd..c3652af8 100755 --- a/bin/lib/Screenshot.py +++ b/bin/lib/Screenshot.py @@ -167,7 +167,7 @@ def get_screenshot_file_content(sha256_string): def save_screenshot_file(sha256_string, io_content): filepath = get_screenshot_filepath(sha256_string) if os.path.isfile(filepath): - print('File already exist') + #print('File already exist') return False # # TODO: check if is IO file with open(filepath, 'wb') as f: @@ -183,7 +183,6 @@ def delete_screenshot_file(obj_id): return True def create_screenshot(obj_id, obj_meta, io_content): - print(obj_id) # # TODO: check if sha256 res = save_screenshot_file(obj_id, io_content) if res: diff --git a/bin/packages/Item.py b/bin/packages/Item.py index cff8fc8c..530e6408 100755 --- a/bin/packages/Item.py +++ b/bin/packages/Item.py @@ -348,7 +348,7 @@ def get_raw_content(item_id): def save_raw_content(item_id, io_content): filepath = get_item_filename(item_id) if os.path.isfile(filepath): - print('File already exist') + #print('File already exist') return False # # TODO: check if is IO file with open(filepath, 'wb') as f: diff --git a/var/www/blueprints/import_export.py b/var/www/blueprints/import_export.py index 8bb22664..60801bad 100644 --- a/var/www/blueprints/import_export.py +++ b/var/www/blueprints/import_export.py @@ -45,13 +45,13 @@ import_export = Blueprint('import_export', __name__, template_folder=os.path.joi @login_required @login_analyst def import_object(): - tags = request.args.get('tags') - return render_template("import_object.html", bootstrap_label=bootstrap_label) + return render_template("import_object.html") @import_export.route("/import_export/import_file", methods=['POST']) @login_required @login_analyst def import_object_file(): + error = None is_file = False if 'file' in request.files: @@ -60,20 +60,29 @@ def import_object_file(): if file.filename: is_file = True + all_imported_obj = [] if is_file: filename = MispImport.sanitize_import_file_path(file.filename) file.save(filename) map_uuid_global_id = MispImport.import_objs_from_file(filename) os.remove(filename) + for obj_uuid in map_uuid_global_id: + dict_obj = Correlate_object.get_global_id_from_id(map_uuid_global_id[obj_uuid]) + dict_obj['uuid'] = obj_uuid + dict_obj['url'] = Correlate_object.get_item_url(dict_obj['type'], dict_obj['id'], correlation_type=dict_obj['subtype']) + dict_obj['node'] = Correlate_object.get_correlation_node_icon(dict_obj['type'], correlation_type=dict_obj['subtype'], value=dict_obj['id']) + all_imported_obj.append(dict_obj) - return render_template("import_object.html", bootstrap_label=bootstrap_label) + if not all_imported_obj: + error = "error: Empty or invalid JSON file" + + return render_template("import_object.html", all_imported_obj=all_imported_obj, error=error) @import_export.route('/import_export/export') @login_required @login_analyst def export_object(): - object_type = request.args.get('object_type') - return render_template("export_object.html", bootstrap_label=bootstrap_label) + return render_template("export_object.html") @import_export.route("/import_export/export_file", methods=['POST']) @login_required diff --git a/var/www/templates/import_export/block_to_export_input.html b/var/www/templates/import_export/block_to_export_input.html index 03053ba8..b5faf733 100644 --- a/var/www/templates/import_export/block_to_export_input.html +++ b/var/www/templates/import_export/block_to_export_input.html @@ -20,7 +20,7 @@ - +
diff --git a/var/www/templates/import_export/export_object.html b/var/www/templates/import_export/export_object.html index 59e3222b..d53cd8ee 100644 --- a/var/www/templates/import_export/export_object.html +++ b/var/www/templates/import_export/export_object.html @@ -14,9 +14,7 @@ - - diff --git a/var/www/templates/import_export/import_object.html b/var/www/templates/import_export/import_object.html index 2c13aa8a..dbfb084f 100644 --- a/var/www/templates/import_export/import_object.html +++ b/var/www/templates/import_export/import_object.html @@ -10,13 +10,14 @@ + - - + + @@ -48,10 +49,64 @@ + {% if error %} +
+ {{error}} +
+ {% endif %}
+ + {% if all_imported_obj %} +
+
+
+ AIL Objects Imported {{all_imported_obj|length}}
+
+
+ + + + + + + + + + + + {% for dict_obj in all_imported_obj %} + + + + + + + {% endfor %} + +
Object UUIDObject TypeObject ID
{{ dict_obj['uuid'] }} +
+ + + + {{ dict_obj['node']['icon_text'] }} + + +
+
{{ dict_obj['type'] }} + + {{ dict_obj['id'] }} + +
+ + {% include 'correlation/legend_graph_correlation.html' %} + +
+
+ {% endif %} + @@ -59,6 +114,10 @@ $(document).ready(function(){ $("#page-Decoded").addClass("active"); $("#nav_misp_import").addClass("active"); + + {% if all_imported_obj %} + $('#myTable_').DataTable({ "lengthMenu": [ 5, 10, 25, 50, 100 ], "pageLength": 10, "order": [[ 2, "asc" ]] }); + {% endif %} }); function toggle_sidebar(){