Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance error message/code and apply black formatter #19

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,55 +15,64 @@
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)
result = backend.convert(sigma_rule, format)
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)))
2 changes: 2 additions & 0 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down