-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
402 lines (347 loc) · 15.7 KB
/
app.py
File metadata and controls
402 lines (347 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
SigmaForge - Flask Application
Web UI and API for Sigma rule generation, validation, and SIEM conversion.
"""
from flask import Flask, render_template, request, jsonify, send_file
from werkzeug.utils import secure_filename
import json
import os
import re
import yaml
from datetime import datetime
import logging
import traceback
from src.sigma_engine import (
SigmaRule, SigmaValidator, SIEMConverter,
build_rule_from_form, build_rule_from_template,
RULE_TEMPLATES, LOG_SOURCES, MITRE_ATTACK_MAP, TACTIC_IDS,
)
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(24)
# Basic logging configuration
logging.basicConfig(level=logging.INFO)
# Rule library storage
RULES_DIR = os.path.join(os.path.dirname(__file__), "rules")
os.makedirs(RULES_DIR, exist_ok=True)
# Input validation constants
_MAX_RULE_YAML_BYTES = 50 * 1024 # 50 KB hard limit on rule_yaml / request body
_SAFE_GROUP_RE = re.compile(r'^[A-Za-z0-9._-]{1,64}$') # allowlist for Wazuh group names
def _safe_library_path(filename: str) -> str:
"""Sanitize filename and return safe path within RULES_DIR. Returns None if unsafe."""
sanitized = secure_filename(filename)
if not sanitized or not sanitized.endswith((".yml", ".yaml")):
return None
filepath = os.path.join(RULES_DIR, sanitized)
# Prevent path traversal
if not os.path.abspath(filepath).startswith(os.path.abspath(RULES_DIR)):
return None
return filepath
# ─────────────────────────────────────────────
# Web Routes
# ─────────────────────────────────────────────
@app.route("/")
def index():
"""Main page - rule builder."""
return render_template(
"index.html",
log_sources=LOG_SOURCES,
mitre_map=MITRE_ATTACK_MAP,
tactic_ids=TACTIC_IDS,
templates=RULE_TEMPLATES,
)
# ─────────────────────────────────────────────
# API Endpoints
# ─────────────────────────────────────────────
@app.route("/api/generate", methods=["POST"])
def api_generate():
"""Generate a Sigma rule from form data."""
try:
if request.content_length and request.content_length > _MAX_RULE_YAML_BYTES:
return jsonify({"success": False, "error": "Request payload exceeds 50 KB limit."}), 400
data = request.get_json()
rule = build_rule_from_form(data)
rule_yaml = rule.to_yaml()
# Validate
validation = SigmaValidator.validate(rule_yaml)
# Convert to all backends
wazuh_rule_id = max(1, min(999_999, int(data.get("rule_id", 100001))))
wazuh_group_name = str(data.get("group_name", "sigma_rules"))
if not _SAFE_GROUP_RE.match(wazuh_group_name):
return jsonify({"success": False, "error": "group_name contains invalid characters (allowed: A-Z a-z 0-9 . _ -)"}), 400
conversions = {}
for backend in ["splunk", "elastic", "eql", "sentinel", "wazuh", "qradar", "dac_json"]:
try:
if backend == "wazuh":
conversions[backend] = SIEMConverter.convert(
rule_yaml, backend,
rule_id=wazuh_rule_id, group_name=wazuh_group_name,
)
else:
conversions[backend] = SIEMConverter.convert(rule_yaml, backend)
except Exception as e:
conversions[backend] = f"Conversion error: {str(e)}"
# Get MITRE info
mitre_info = []
for tech_id in rule.mitre_techniques:
info = MITRE_ATTACK_MAP.get(tech_id)
if info:
mitre_info.append({
"id": tech_id,
"name": info["name"],
"tactic": info["tactic"],
"tactic_id": TACTIC_IDS.get(info["tactic"], ""),
})
return jsonify({
"success": True,
"rule_yaml": rule_yaml,
"rule_json": rule.to_dict(),
"validation": validation,
"conversions": conversions,
"mitre_info": mitre_info,
})
except Exception as e:
# Log full exception details server-side, but return a generic error message to the client
logging.exception("Error occurred while generating Sigma rule")
return jsonify({
"success": False,
"error": "An internal error occurred while generating the rule."
}), 400
@app.route("/api/template/<template_key>", methods=["GET"])
def api_template(template_key):
"""Load a pre-built rule template."""
try:
rule = build_rule_from_template(template_key)
rule_yaml = rule.to_yaml()
validation = SigmaValidator.validate(rule_yaml)
conversions = {}
for backend in ["splunk", "elastic", "eql", "sentinel", "wazuh", "qradar", "dac_json"]:
try:
if backend == "wazuh":
conversions[backend] = SIEMConverter.convert(
rule_yaml, backend, rule_id=100001, group_name="sigma_rules",
)
else:
conversions[backend] = SIEMConverter.convert(rule_yaml, backend)
except Exception as e:
conversions[backend] = f"Conversion error: {str(e)}"
mitre_info = []
for tech_id in rule.mitre_techniques:
info = MITRE_ATTACK_MAP.get(tech_id)
if info:
mitre_info.append({
"id": tech_id,
"name": info["name"],
"tactic": info["tactic"],
"tactic_id": TACTIC_IDS.get(info["tactic"], ""),
})
# Return template data for form population
template = RULE_TEMPLATES[template_key]
return jsonify({
"success": True,
"template": template,
"rule_yaml": rule_yaml,
"rule_json": rule.to_dict(),
"validation": validation,
"conversions": conversions,
"mitre_info": mitre_info,
})
except ValueError as e:
# Do not expose internal error details to the client
logging.warning("ValueError in api_template for key %s: %s", template_key, e)
return jsonify({"success": False, "error": "Template not found."}), 404
except Exception as e:
logging.exception("Unexpected error in api_template for key %s", template_key)
return jsonify({"success": False, "error": "An internal error occurred while loading the template."}), 400
@app.route("/api/validate", methods=["POST"])
def api_validate():
"""Validate a Sigma rule YAML string."""
try:
data = request.get_json()
rule_yaml = data.get("rule_yaml", "")
validation = SigmaValidator.validate(rule_yaml)
return jsonify({"success": True, "validation": validation})
except Exception as e:
logging.exception("Unexpected error in api_validate")
return jsonify({"success": False, "error": "An internal error occurred while validating the rule."}), 400
@app.route("/api/convert", methods=["POST"])
def api_convert():
"""Convert a Sigma rule to a specific SIEM backend."""
try:
if request.content_length and request.content_length > _MAX_RULE_YAML_BYTES:
return jsonify({"success": False, "error": "Request payload exceeds 50 KB limit."}), 400
data = request.get_json()
rule_yaml = data.get("rule_yaml", "")
if len(rule_yaml) > _MAX_RULE_YAML_BYTES:
return jsonify({"success": False, "error": "rule_yaml exceeds 50 KB limit."}), 400
backend = data.get("backend", "splunk")
if backend not in ["splunk", "elastic", "eql", "sentinel", "wazuh", "qradar", "dac_json"]:
return jsonify({"success": False, "error": f"Unknown backend: {backend}"}), 400
if backend == "wazuh":
rule_id = max(1, min(999_999, int(data.get("rule_id", 100001))))
group_name = str(data.get("group_name", "sigma_rules"))
if not _SAFE_GROUP_RE.match(group_name):
return jsonify({"success": False, "error": "group_name contains invalid characters (allowed: A-Z a-z 0-9 . _ -)"}), 400
query = SIEMConverter.convert(rule_yaml, backend,
rule_id=rule_id, group_name=group_name)
else:
query = SIEMConverter.convert(rule_yaml, backend)
return jsonify({"success": True, "query": query, "backend": backend})
except Exception as e:
logging.exception("Unexpected error in api_convert for backend %s", backend)
return jsonify({"success": False, "error": "An internal error occurred while converting the rule."}), 400
@app.route("/api/library/save", methods=["POST"])
def api_save_rule():
"""Save a rule to the local rule library."""
try:
data = request.get_json()
rule_yaml = data.get("rule_yaml", "")
# Parse to get ID and title for filename
rule_data = yaml.safe_load(rule_yaml)
rule_id = rule_data.get("id", "unknown")
title = rule_data.get("title", "untitled")
safe_title = "".join(c if c.isalnum() or c in "-_ " else "" for c in title)
safe_title = safe_title.replace(" ", "_").lower()[:50]
filename = secure_filename(f"{safe_title}_{rule_id[:8]}.yml")
filepath = _safe_library_path(filename)
if not filepath:
return jsonify({"success": False, "error": "Invalid filename generated"}), 400
with open(filepath, "w") as f:
f.write(rule_yaml)
return jsonify({
"success": True,
"filename": filename,
"message": f"Rule saved: {filename}",
})
except Exception as e:
logging.exception("Unexpected error in api_save_rule")
return jsonify({"success": False, "error": "An internal error occurred while saving the rule."}), 400
@app.route("/api/library/list", methods=["GET"])
def api_list_rules():
"""List all saved rules in the library."""
try:
rules = []
for filename in sorted(os.listdir(RULES_DIR)):
if filename.endswith((".yml", ".yaml")):
filepath = os.path.join(RULES_DIR, filename)
with open(filepath, "r") as f:
content = f.read()
try:
rule_data = yaml.safe_load(content)
rules.append({
"filename": filename,
"title": rule_data.get("title", "Unknown"),
"level": rule_data.get("level", "unknown"),
"status": rule_data.get("status", "unknown"),
"id": rule_data.get("id", ""),
"description": rule_data.get("description", "")[:100],
"yaml": content,
})
except yaml.YAMLError:
rules.append({
"filename": filename,
"title": filename,
"level": "unknown",
"status": "unknown",
"error": "Failed to parse YAML",
})
return jsonify({"success": True, "rules": rules})
except Exception as e:
logging.exception("Unexpected error in api_list_rules")
return jsonify({"success": False, "error": "An internal error occurred while listing the rules."}), 400
@app.route("/api/library/load/<filename>", methods=["GET"])
def api_load_rule(filename):
"""Load a rule from the library."""
try:
filepath = _safe_library_path(filename)
if not filepath:
return jsonify({"success": False, "error": "Invalid filename"}), 400
if not os.path.exists(filepath):
return jsonify({"success": False, "error": "Rule not found"}), 404
with open(filepath, "r") as f:
content = f.read()
validation = SigmaValidator.validate(content)
conversions = {}
for backend in ["splunk", "elastic", "eql", "sentinel", "wazuh", "qradar", "dac_json"]:
try:
if backend == "wazuh":
conversions[backend] = SIEMConverter.convert(
content, backend, rule_id=100001, group_name="sigma_rules",
)
else:
conversions[backend] = SIEMConverter.convert(content, backend)
except Exception as e:
conversions[backend] = f"Conversion error: {str(e)}"
return jsonify({
"success": True,
"rule_yaml": content,
"validation": validation,
"conversions": conversions,
})
except Exception as e:
logging.exception("Error loading rule from library")
return jsonify({"success": False, "error": "An internal error occurred while loading the rule."}), 400
@app.route("/api/library/delete/<filename>", methods=["DELETE"])
def api_delete_rule(filename):
"""Delete a rule from the library."""
try:
filepath = _safe_library_path(filename)
if not filepath:
return jsonify({"success": False, "error": "Invalid filename"}), 400
if os.path.exists(filepath):
os.remove(filepath)
return jsonify({"success": True, "message": f"Deleted: {filename}"})
return jsonify({"success": False, "error": "File not found"}), 404
except Exception as e:
logging.exception("Error deleting rule from library")
return jsonify({"success": False, "error": "An internal error occurred while deleting the rule."}), 400
@app.route("/api/library/export", methods=["GET"])
def api_export_library():
"""Export all rules as a JSON bundle."""
try:
rules = []
for filename in sorted(os.listdir(RULES_DIR)):
if filename.endswith((".yml", ".yaml")):
filepath = os.path.join(RULES_DIR, filename)
with open(filepath, "r") as f:
content = f.read()
try:
rules.append(yaml.safe_load(content))
except yaml.YAMLError:
pass
return jsonify({
"success": True,
"export_date": datetime.now().isoformat(),
"rule_count": len(rules),
"rules": rules,
})
except Exception as e:
logging.exception("Error exporting rule library")
return jsonify({"success": False, "error": "An internal error occurred while exporting the library."}), 400
@app.route("/api/log-sources", methods=["GET"])
def api_log_sources():
"""Return available log sources and their fields."""
return jsonify({"success": True, "log_sources": LOG_SOURCES})
@app.route("/api/mitre", methods=["GET"])
def api_mitre():
"""Return MITRE ATT&CK technique mapping."""
return jsonify({
"success": True,
"techniques": MITRE_ATTACK_MAP,
"tactics": TACTIC_IDS,
})
@app.route("/api/templates", methods=["GET"])
def api_templates():
"""Return available rule templates."""
templates_summary = {}
for key, tmpl in RULE_TEMPLATES.items():
templates_summary[key] = {
"name": tmpl["name"],
"description": tmpl["description"],
"log_source": tmpl["log_source"],
"level": tmpl["level"],
"mitre_techniques": tmpl["mitre_techniques"],
}
return jsonify({"success": True, "templates": templates_summary})
if __name__ == "__main__":
debug_mode = os.getenv("FLASK_DEBUG", "0") == "1"
app.run(debug=debug_mode, host="0.0.0.0", port=5000)