diff --git a/run.py b/run.py index eebda61..cc5d8ee 100755 --- a/run.py +++ b/run.py @@ -4,7 +4,7 @@ import os import yaml import base64 -from flask import Flask, render_template, request +from flask import Flask, render_template, request, Response from sigma.conversion.base import Backend from sigma.plugins import InstalledSigmaPlugins @@ -15,45 +15,53 @@ plugins = InstalledSigmaPlugins.autodiscover() backends = plugins.backends pipeline_resolver = plugins.get_pipeline_resolver() -pipelines = list(pipeline_resolver.list_pipelines()) +pipelines = list(pipeline_resolver.list_pipelines()) -@app.route('/') + +@app.route("/") def home(): formats = [] for backend in backends.keys(): for name, description in plugins.backends[backend].formats.items(): - formats.append({"name": name, "description": description, "backend": backend}) - + formats.append( + {"name": name, "description": description, "backend": backend} + ) + for name, pipeline in pipelines: if len(pipeline.allowed_backends) > 0: pipeline.backends = ", ".join(pipeline.allowed_backends) else: pipeline.backends = "all" - return render_template('index.html', backends=backends, pipelines=pipelines, formats=formats) + return render_template( + "index.html", backends=backends, pipelines=pipelines, formats=formats + ) -@app.route('/sigma', methods=['POST']) -def convert(): +@app.route("/sigma", methods=["POST"]) +def convert(): # get params from request - rule = str(base64.b64decode(request.json['rule']), "utf-8") + rule = str(base64.b64decode(request.json["rule"]), "utf-8") # check if input is valid yaml try: yaml.safe_load(rule) except: print("error") - return ("Error: No valid yaml input") + return Response( + f"YamlError: Malformed yaml file", status=400, mimetype="text/html" + ) pipeline = [] - if request.json['pipeline']: - for p in request.json['pipeline']: + if request.json["pipeline"]: + for p in request.json["pipeline"]: pipeline.append(p) - target = request.json['target'] - format = request.json['format'] + + target = request.json["target"] + format = request.json["format"] backend_class = backends[target] processing_pipeline = pipeline_resolver.resolve(pipeline) - backend : Backend = backend_class(processing_pipeline=processing_pipeline) + backend: Backend = backend_class(processing_pipeline=processing_pipeline) try: sigma_rule = SigmaCollection.from_yaml(rule) @@ -61,9 +69,10 @@ def convert(): if isinstance(result, list): result = result[0] except SigmaError as e: - return "Error: " + str(e) + return Response(f"SigmaError: {str(e)}", status=400, mimetype="text/html") return result -if __name__ == '__main__': - app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8000))) + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8000))) diff --git a/static/js/index.js b/static/js/index.js index cef73db..f53c595 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -127,6 +127,8 @@ function convert(sigmaRule) { queryCode.innerHTML = xhr.response; queryCode.value = xhr.response; Prism.highlightElement(queryCode); // rerun code highlighting + } else if (xhr.status === 400) { + queryCode.innerHTML = xhr.response; } else if (xhr.status === 500) { queryCode.innerHTML = "Error: Something went wrong"; }