-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
305 lines (251 loc) · 10.3 KB
/
main.py
File metadata and controls
305 lines (251 loc) · 10.3 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
"""CLI entry point for RHEL Red Teaming tool."""
from __future__ import annotations
import json
import sys
from pathlib import Path
import click
import yaml
from rich.console import Console
from core.banner import display_compact_banner
from core.engine import ScanEngine
from core.logger import setup_logging
from core.mitre_mapper import MitreMapper
from core.models import ScanConfig, ScanResult, SessionType, Status, Tactic, Target
from core.playbook_generator import PlaybookGenerator
from core.reporter import Reporter
console = Console()
def load_config(config_path: str = "config/settings.yaml") -> dict:
"""Load YAML configuration file."""
path = Path(config_path)
if not path.exists():
console.print(f"[red]Config file not found: {config_path}[/]")
sys.exit(1)
return yaml.safe_load(path.read_text(encoding="utf-8"))
def load_profile(profile_name: str) -> dict:
"""Load a scan profile."""
path = Path(f"config/profiles/{profile_name}.yaml")
if not path.exists():
console.print(f"[yellow]Profile not found: {profile_name}, using defaults[/]")
return {}
return yaml.safe_load(path.read_text(encoding="utf-8"))
def parse_tactic(name: str) -> Tactic | None:
"""Convert tactic name string to Tactic enum."""
try:
return Tactic(name.lower().replace("-", "_").replace(" ", "_"))
except ValueError:
return None
@click.group()
@click.version_option(version="0.1.0", prog_name="rhel-rt")
def cli() -> None:
"""RHEL Red Teaming Tool — MITRE ATT&CK Security Scanner."""
pass
@cli.command()
@click.option("--target", "-t", required=True, help="Target host (IP or hostname)")
@click.option("--port", "-p", default=22, help="SSH port (default: 22)")
@click.option("--username", "-u", default=None, help="SSH username")
@click.option("--password", default=None, help="SSH password")
@click.option("--key-file", "-k", default=None, help="SSH private key file")
@click.option("--profile", default="quick", help="Scan profile (quick/full/stealth)")
@click.option("--simulate", is_flag=True, help="Enable active simulation mode")
@click.option("--tactic", default=None, help="Run only a specific tactic")
@click.option("--technique", default=None, help="Run only a specific technique ID")
@click.option("--config", "config_path", default="config/settings.yaml", help="Config file path")
@click.option("--format", "output_format", default="json", type=click.Choice(["json", "csv", "html"]))
@click.option("--timeout", default=300, help="Command timeout in seconds")
def scan(
target: str,
port: int,
username: str | None,
password: str | None,
key_file: str | None,
profile: str,
simulate: bool,
tactic: str | None,
technique: str | None,
config_path: str,
output_format: str,
timeout: int,
) -> None:
"""Run a security scan against a target."""
settings = load_config(config_path)
profile_config = load_profile(profile)
setup_logging(
log_level=settings.get("output", {}).get("log_level", "INFO"),
log_file=settings.get("output", {}).get("log_file"),
json_output=settings.get("output", {}).get("json_logging", False),
)
# Determine session type
is_local = target in ("localhost", "127.0.0.1", "::1")
session_type = SessionType.LOCAL if is_local else SessionType.SSH
scan_target = Target(
host=target,
port=port,
username=username,
password=password,
key_file=key_file,
session_type=session_type,
)
# Build tactics filter
tactics_filter: list[Tactic] | None = None
if tactic:
parsed = parse_tactic(tactic)
if parsed:
tactics_filter = [parsed]
else:
console.print(f"[red]Unknown tactic: {tactic}[/]")
sys.exit(1)
elif profile_config.get("tactics"):
tactics_filter = []
for t in profile_config["tactics"]:
parsed = parse_tactic(t)
if parsed:
tactics_filter.append(parsed)
# Build techniques filter
techniques_filter: list[str] | None = None
if technique:
techniques_filter = [technique]
scan_config = ScanConfig(
targets=[scan_target],
profile=profile,
simulate=simulate,
tactics=tactics_filter,
techniques=techniques_filter,
timeout=timeout,
output_dir=settings.get("output", {}).get("report_dir", "reports"),
evidence_dir=settings.get("output", {}).get("evidence_dir", "evidence"),
)
# Run scan
engine = ScanEngine(scan_config)
result = engine.run(scan_target)
# Generate reports
reporter = Reporter(output_dir=scan_config.output_dir)
report_path = reporter.generate(result, fmt=output_format)
console.print(f"\nReport saved: [cyan]{report_path}[/]")
# Generate ATT&CK Navigator layer
mapper = MitreMapper(output_dir=scan_config.output_dir)
layer_path = mapper.generate_layer(result)
console.print(f"ATT&CK layer: [cyan]{layer_path}[/]")
# Generate remediation playbook if vulnerabilities found
if result.vulnerable_count > 0:
gen = PlaybookGenerator(output_dir=scan_config.output_dir)
playbook_path = gen.generate(result)
console.print(f"Playbook: [cyan]{playbook_path}[/]")
@cli.command()
@click.option("--input", "input_path", required=True, help="Path to scan result JSON")
@click.option("--format", "output_format", default="html", type=click.Choice(["json", "csv", "html"]))
def report(input_path: str, output_format: str) -> None:
"""Generate a report from a previous scan result."""
path = Path(input_path)
if not path.exists():
console.print(f"[red]Input file not found: {input_path}[/]")
sys.exit(1)
data = json.loads(path.read_text(encoding="utf-8"))
# Reconstruct ScanResult from JSON
scan_result = ScanResult(
scan_id=data["scan_id"],
config=ScanConfig(),
start_time=data.get("start_time", ""),
target_info=data.get("target_info", {}),
)
reporter = Reporter()
report_path = reporter.generate(scan_result, fmt=output_format)
console.print(f"Report saved: [cyan]{report_path}[/]")
@cli.command()
@click.option("--input", "input_path", required=True, help="Path to scan result JSON")
@click.option("--severity", default=None, type=click.Choice(["critical", "high", "medium", "low"]),
help="Minimum severity to include")
@click.option("--tags", default=None, help="Comma-separated tag filter (e.g., ssh,stig)")
@click.option("--per-technique", is_flag=True, help="Generate one playbook per technique")
def remediate(input_path: str, severity: str | None, tags: str | None, per_technique: bool) -> None:
"""Generate Ansible remediation playbook from scan results."""
from core.models import Severity as SevEnum
path = Path(input_path)
if not path.exists():
console.print(f"[red]Input file not found: {input_path}[/]")
sys.exit(1)
data = json.loads(path.read_text(encoding="utf-8"))
scan_result = ScanResult(
scan_id=data["scan_id"],
config=ScanConfig(),
start_time=data.get("start_time", ""),
target_info=data.get("target_info", {}),
)
# Reconstruct ModuleResults
from core.models import ModuleResult, Finding
for r in data.get("results", []):
findings = [
Finding(
title=f["title"],
description=f["description"],
severity=SevEnum(f["severity"]),
evidence=f.get("evidence", ""),
remediation=f.get("remediation", ""),
)
for f in r.get("findings", [])
]
scan_result.results.append(ModuleResult(
technique_id=r["technique_id"],
technique_name=r["technique_name"],
tactic=Tactic(r["tactic"]),
status=Status(r["status"]),
findings=findings,
mitigations=r.get("mitigations", []),
))
severity_filter = SevEnum(severity) if severity else None
tags_filter = [t.strip() for t in tags.split(",")] if tags else None
gen = PlaybookGenerator()
if per_technique:
paths = gen.generate_per_technique(scan_result)
for p in paths:
console.print(f"Playbook: [cyan]{p}[/]")
console.print(f"\n[green]{len(paths)} technique playbooks generated[/]")
else:
playbook_path = gen.generate(scan_result, severity_filter=severity_filter, tags_filter=tags_filter)
console.print(f"\nPlaybook: [cyan]{playbook_path}[/]")
console.print("[green]Review the playbook, then run:[/]")
console.print(f" ansible-playbook -i inventory {playbook_path.name} --check")
@cli.command(name="list-modules")
def list_modules() -> None:
"""List all discovered technique modules."""
display_compact_banner(console)
setup_logging(log_level="WARNING")
config = ScanConfig()
engine = ScanEngine(config)
if not engine.modules:
console.print("[yellow]No modules found. Add technique modules to modules/ directory.[/]")
return
from rich.table import Table
table = Table(title="Available Modules")
table.add_column("Technique ID", style="cyan")
table.add_column("Name", style="white")
table.add_column("Tactic", style="green")
table.add_column("Severity", style="yellow")
table.add_column("Root", justify="center")
table.add_column("Safe", justify="center")
for m in sorted(engine.modules, key=lambda x: x.TECHNIQUE_ID):
table.add_row(
m.TECHNIQUE_ID,
m.TECHNIQUE_NAME,
m.TACTIC.value.replace("_", " ").title(),
m.SEVERITY.value.upper(),
"[red]Yes[/]" if m.REQUIRES_ROOT else "No",
"[green]Yes[/]" if m.SAFE_MODE else "[red]No[/]",
)
console.print(table)
console.print(f"\nTotal: [cyan]{len(engine.modules)}[/] modules")
@cli.command(name="list-tactics")
def list_tactics() -> None:
"""List all supported ATT&CK tactics."""
display_compact_banner(console)
from rich.table import Table
table = Table(title="MITRE ATT&CK Tactics")
table.add_column("Tactic", style="cyan")
table.add_column("CLI Name", style="green")
for tactic in Tactic:
table.add_row(
tactic.value.replace("_", " ").title(),
tactic.value,
)
console.print(table)
if __name__ == "__main__":
cli()