-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
348 lines (278 loc) · 11.4 KB
/
cli.py
File metadata and controls
348 lines (278 loc) · 11.4 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
#!/usr/bin/env python3
"""
SigmaForge CLI — Command-line Sigma rule generation, validation, and conversion.
Usage:
python cli.py generate --title "My Rule" --logsource process_creation --level high
python cli.py validate rule.yml
python cli.py convert rule.yml --backend splunk
python cli.py template suspicious_powershell
python cli.py templates
python cli.py logsources
"""
import argparse
import sys
import os
import yaml
import json
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from src.sigma_engine import (
SigmaRule, SigmaValidator, SIEMConverter,
build_rule_from_template, RULE_TEMPLATES, LOG_SOURCES,
MITRE_ATTACK_MAP, TACTIC_IDS,
)
# ── Colors ──────────────────────────────────
class Colors:
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
CYAN = "\033[96m"
MAGENTA = "\033[95m"
WHITE = "\033[97m"
def banner():
print(f"""
{Colors.CYAN}{Colors.BOLD}╔══════════════════════════════════════════════╗
║ Σ SigmaForge CLI v1.0.0 ║
║ Vendor-Agnostic Sigma Rule Generator ║
╚══════════════════════════════════════════════╝{Colors.RESET}
""")
def print_header(text):
print(f"\n{Colors.BLUE}{Colors.BOLD}── {text} ──{Colors.RESET}")
def print_success(text):
print(f"{Colors.GREEN}✓{Colors.RESET} {text}")
def print_error(text):
print(f"{Colors.RED}✗{Colors.RESET} {text}")
def print_warning(text):
print(f"{Colors.YELLOW}⚠{Colors.RESET} {text}")
def print_info(text):
print(f"{Colors.CYAN}ℹ{Colors.RESET} {text}")
def print_rule(yaml_str):
"""Print YAML with syntax highlighting."""
for line in yaml_str.split("\n"):
if line.strip().startswith("#"):
print(f" {Colors.DIM}{line}{Colors.RESET}")
elif ":" in line:
key, _, val = line.partition(":")
print(f" {Colors.CYAN}{key}{Colors.RESET}:{Colors.WHITE}{val}{Colors.RESET}")
elif line.strip().startswith("- "):
print(f" {Colors.YELLOW}{line}{Colors.RESET}")
else:
print(f" {line}")
# ── Commands ────────────────────────────────
def cmd_generate(args):
"""Generate a Sigma rule from CLI arguments."""
# Parse detection from CLI
detection = {}
if args.field:
selection = {}
for field_spec in args.field:
parts = field_spec.split("=", 1)
if len(parts) != 2:
print_error(f"Invalid field format: {field_spec} (use field=value or field|modifier=value)")
sys.exit(1)
key, val = parts
# Handle comma-separated values
values = [v.strip() for v in val.split(",")]
if len(values) == 1:
try:
selection[key] = int(values[0])
except ValueError:
selection[key] = values[0]
else:
selection[key] = values
detection["selection"] = selection
else:
detection["selection"] = {"Image|endswith": "\\example.exe"}
detection["condition"] = args.condition or "selection"
# Parse MITRE techniques
techniques = []
if args.mitre:
techniques = [t.strip() for t in args.mitre.split(",")]
# Parse false positives
fps = []
if args.falsepositives:
fps = [f.strip() for f in args.falsepositives.split(",")]
rule = SigmaRule(
title=args.title or "Untitled Rule",
description=args.description or "",
log_source_key=args.logsource or "process_creation",
detection=detection,
level=args.level or "medium",
status=args.status or "experimental",
author=args.author or "SigmaForge",
mitre_techniques=techniques,
falsepositives=fps,
)
rule_yaml = rule.to_yaml()
print_header("Generated Sigma Rule")
print_rule(rule_yaml)
# Validate
validation = SigmaValidator.validate(rule_yaml)
print_header("Validation")
if validation["valid"]:
print_success("Rule is valid")
else:
print_error("Rule has errors")
for err in validation["errors"]:
print_error(err)
for warn in validation["warnings"]:
print_warning(warn)
# Convert
if args.backend:
backends = [args.backend]
else:
backends = ["splunk", "elastic", "eql", "sentinel"]
for backend in backends:
print_header(f"Conversion: {backend.upper()}")
try:
query = SIEMConverter.convert(rule_yaml, backend)
print(f" {Colors.WHITE}{query}{Colors.RESET}")
except Exception as e:
print_error(f"Conversion failed: {e}")
# Save to file
if args.output:
with open(args.output, "w") as f:
f.write(rule_yaml)
print_success(f"Rule saved to: {args.output}")
def cmd_validate(args):
"""Validate a Sigma rule file."""
if not os.path.exists(args.file):
print_error(f"File not found: {args.file}")
sys.exit(1)
with open(args.file, "r") as f:
rule_yaml = f.read()
print_header(f"Validating: {args.file}")
validation = SigmaValidator.validate(rule_yaml)
if validation["valid"]:
print_success("Rule is valid")
else:
print_error("Rule has errors")
for err in validation["errors"]:
print_error(err)
for warn in validation["warnings"]:
print_warning(warn)
def cmd_convert(args):
"""Convert a Sigma rule to SIEM query."""
if not os.path.exists(args.file):
print_error(f"File not found: {args.file}")
sys.exit(1)
with open(args.file, "r") as f:
rule_yaml = f.read()
backends = [args.backend] if args.backend else ["splunk", "elastic", "eql", "sentinel"]
for backend in backends:
print_header(f"Conversion: {backend.upper()}")
try:
query = SIEMConverter.convert(rule_yaml, backend)
print(f" {Colors.WHITE}{query}{Colors.RESET}")
except Exception as e:
print_error(f"Conversion failed: {e}")
def cmd_template(args):
"""Generate a rule from a pre-built template."""
try:
rule = build_rule_from_template(args.name)
except ValueError:
print_error(f"Unknown template: {args.name}")
print_info("Available templates:")
for key in RULE_TEMPLATES:
print(f" {Colors.CYAN}{key}{Colors.RESET}")
sys.exit(1)
rule_yaml = rule.to_yaml()
print_header(f"Template: {args.name}")
print_rule(rule_yaml)
# Convert
for backend in ["splunk", "elastic", "eql", "sentinel"]:
print_header(f"Conversion: {backend.upper()}")
try:
query = SIEMConverter.convert(rule_yaml, backend)
print(f" {Colors.WHITE}{query}{Colors.RESET}")
except Exception as e:
print_error(f"Conversion failed: {e}")
if args.output:
with open(args.output, "w") as f:
f.write(rule_yaml)
print_success(f"Rule saved to: {args.output}")
def cmd_templates(args):
"""List all available templates."""
print_header("Available Templates")
for key, tmpl in RULE_TEMPLATES.items():
level_colors = {
"critical": Colors.RED,
"high": Colors.YELLOW,
"medium": Colors.CYAN,
"low": Colors.GREEN,
"informational": Colors.BLUE,
}
lc = level_colors.get(tmpl["level"], Colors.WHITE)
techniques = ", ".join(tmpl["mitre_techniques"])
print(f" {Colors.CYAN}{key:30s}{Colors.RESET} {lc}[{tmpl['level']:13s}]{Colors.RESET} {tmpl['name']}")
print(f" {' ':30s} {Colors.DIM}ATT&CK: {techniques}{Colors.RESET}")
print()
def cmd_logsources(args):
"""List available log sources."""
print_header("Available Log Sources")
for key, src in LOG_SOURCES.items():
desc = src.get("description", key)
fields = ", ".join(src.get("fields", [])[:5])
more = len(src.get("fields", [])) - 5
if more > 0:
fields += f" (+{more} more)"
print(f" {Colors.CYAN}{key:25s}{Colors.RESET} {desc}")
print(f" {' ':25s} {Colors.DIM}Fields: {fields}{Colors.RESET}")
print()
# ── Main ────────────────────────────────────
def main():
banner()
parser = argparse.ArgumentParser(
description="SigmaForge CLI — Vendor-Agnostic Sigma Rule Generator",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# generate
gen_parser = subparsers.add_parser("generate", help="Generate a new Sigma rule")
gen_parser.add_argument("--title", "-t", help="Rule title")
gen_parser.add_argument("--description", "-d", help="Rule description")
gen_parser.add_argument("--logsource", "-l", help="Log source key (e.g. process_creation)")
gen_parser.add_argument("--level", choices=["informational", "low", "medium", "high", "critical"])
gen_parser.add_argument("--status", choices=["experimental", "test", "stable"])
gen_parser.add_argument("--author", help="Rule author")
gen_parser.add_argument("--field", "-f", action="append",
help="Detection field (format: field|modifier=value). Can repeat.")
gen_parser.add_argument("--condition", "-c", help="Detection condition (default: selection)")
gen_parser.add_argument("--mitre", "-m", help="MITRE ATT&CK technique IDs (comma-separated)")
gen_parser.add_argument("--falsepositives", help="False positives (comma-separated)")
gen_parser.add_argument("--backend", "-b", choices=["splunk", "elastic", "eql", "sentinel"])
gen_parser.add_argument("--output", "-o", help="Output file path (.yml)")
# validate
val_parser = subparsers.add_parser("validate", help="Validate a Sigma rule file")
val_parser.add_argument("file", help="Path to Sigma rule YAML file")
# convert
conv_parser = subparsers.add_parser("convert", help="Convert a Sigma rule to SIEM query")
conv_parser.add_argument("file", help="Path to Sigma rule YAML file")
conv_parser.add_argument("--backend", "-b", choices=["splunk", "elastic", "eql", "sentinel"])
# template
tmpl_parser = subparsers.add_parser("template", help="Generate rule from template")
tmpl_parser.add_argument("name", help="Template name")
tmpl_parser.add_argument("--output", "-o", help="Output file path (.yml)")
# templates
subparsers.add_parser("templates", help="List available templates")
# logsources
subparsers.add_parser("logsources", help="List available log sources")
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(0)
commands = {
"generate": cmd_generate,
"validate": cmd_validate,
"convert": cmd_convert,
"template": cmd_template,
"templates": cmd_templates,
"logsources": cmd_logsources,
}
commands[args.command](args)
if __name__ == "__main__":
main()