-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
313 lines (271 loc) · 9.23 KB
/
Copy pathcli.py
File metadata and controls
313 lines (271 loc) · 9.23 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
"""CLI interface for SecureAPI Analyzer.
Provides commands for running security scans and generating reports.
Usage:
python cli.py analyze --target https://api.example.com
python cli.py report --input results/scan.json --format html
"""
from __future__ import annotations
import asyncio
import json
import sys
import time
from pathlib import Path
from typing import Optional
import click
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from src import __version__
from src.analyzer import Analyzer
from src.config import Config
from src.reporter import Reporter
from src.utils import ScanResult, normalize_url
console = Console()
@click.group()
@click.version_option(version=__version__, prog_name="SecureAPI Analyzer")
def cli() -> None:
"""SecureAPI Analyzer - REST API Security Testing Tool.
Automated testing for authentication, authorization, and injection
vulnerabilities in REST APIs. Maps findings to OWASP API Security Top 10.
"""
pass
@cli.command()
@click.option(
"--target", "-t",
required=True,
help="Target API base URL (e.g., https://api.example.com)",
)
@click.option(
"--config", "-c",
default="configs/default.yaml",
help="Path to YAML configuration file",
)
@click.option(
"--spec", "-s",
default=None,
help="Path to OpenAPI/Swagger specification (JSON or YAML)",
)
@click.option(
"--token",
default=None,
help="Bearer token for authenticated requests",
)
@click.option(
"--suite",
multiple=True,
help="Run specific test suites (can be repeated). Options: auth_bypass, broken_access, rate_limiting, input_validation",
)
@click.option(
"--output", "-o",
default="results",
help="Output directory for results",
)
@click.option(
"--timeout",
default=None,
type=int,
help="Request timeout in seconds (overrides config)",
)
@click.option(
"--verbose", "-v",
is_flag=True,
default=False,
help="Enable verbose output",
)
@click.option(
"--quiet", "-q",
is_flag=True,
default=False,
help="Suppress all output except errors",
)
def analyze(
target: str,
config: str,
spec: Optional[str],
token: Optional[str],
suite: tuple[str, ...],
output: str,
timeout: Optional[int],
verbose: bool,
quiet: bool,
) -> None:
"""Run security analysis against a target API."""
if not quiet:
console.print(
Panel(
f"[bold]SecureAPI Analyzer[/bold] v{__version__}\n"
f"Target: {target}",
title="Security Scan",
border_style="blue",
)
)
# Load configuration
config_path = Path(config)
if config_path.exists():
cfg = Config.from_yaml(config)
if verbose and not quiet:
console.print(f"[dim]Loaded config from {config}[/dim]")
else:
cfg = Config()
if verbose and not quiet:
console.print("[dim]Using default configuration[/dim]")
# Apply CLI overrides
cfg.merge_cli_args(
target=target,
token=token,
spec=spec,
timeout=timeout,
output_dir=output,
)
# Determine suites
suites_list = list(suite) if suite else None
if suites_list and not quiet:
console.print(f"[dim]Suites: {', '.join(suites_list)}[/dim]")
elif not quiet:
console.print("[dim]Suites: all enabled[/dim]")
console.print()
# Run the scan
analyzer = Analyzer(cfg)
try:
results = asyncio.run(analyzer.run(suites=suites_list))
except KeyboardInterrupt:
console.print("\n[yellow]Scan interrupted by user[/yellow]")
sys.exit(1)
except Exception as exc:
console.print(f"\n[red]Scan failed: {exc}[/red]")
if verbose:
console.print_exception()
sys.exit(1)
# Display results
if not quiet:
_display_results(results, verbose)
# Save results
try:
result_path = analyzer.save_results(output)
if not quiet:
console.print(f"\n[green]Results saved to: {result_path}[/green]")
except Exception as exc:
console.print(f"[red]Failed to save results: {exc}[/red]")
# Summary
summary = analyzer.get_summary()
if not quiet:
_display_summary(summary)
# Exit with non-zero code if critical/high findings exist
if summary["findings"]["critical"] > 0 or summary["findings"]["high"] > 0:
sys.exit(2)
@cli.command()
@click.option(
"--input", "-i",
"input_path",
required=True,
help="Path to saved scan results JSON file",
)
@click.option(
"--format", "-f",
"output_format",
type=click.Choice(["json", "html", "markdown", "md"]),
default="html",
help="Report output format",
)
@click.option(
"--output", "-o",
default=None,
help="Output file path (auto-generated if not specified)",
)
def report(
input_path: str,
output_format: str,
output: Optional[str],
) -> None:
"""Generate a report from saved scan results."""
path = Path(input_path)
if not path.exists():
console.print(f"[red]Results file not found: {input_path}[/red]")
sys.exit(1)
with open(path, "r", encoding="utf-8") as fh:
data = json.load(fh)
# Reconstruct ScanResult objects from saved data
target = data.get("summary", {}).get("target", "unknown")
results: list[ScanResult] = []
for result_data in data.get("results", []):
sr = ScanResult(
target=target,
suite_name=result_data.get("suite_name", "unknown"),
endpoints_tested=result_data.get("endpoints_tested", 0),
)
# Reconstruct findings (simplified - uses dict representation)
from src.utils import Finding
for finding_data in result_data.get("findings", []):
sr.findings.append(
Finding(
title=finding_data.get("title", ""),
severity=finding_data.get("severity", "INFO"),
owasp_category=finding_data.get("owasp_category", ""),
description=finding_data.get("description", ""),
endpoint=finding_data.get("endpoint", ""),
method=finding_data.get("method", "GET"),
evidence=finding_data.get("evidence", ""),
remediation=finding_data.get("remediation", ""),
cvss_score=finding_data.get("cvss_score", 0.0),
response_status=finding_data.get("response_status"),
response_snippet=finding_data.get("response_snippet", ""),
)
)
results.append(sr)
reporter = Reporter(results, target)
# Determine output path
if not output:
timestamp = time.strftime("%Y%m%d_%H%M%S")
ext = "html" if output_format == "html" else ("md" if output_format in ("markdown", "md") else "json")
output = f"reports/report_{timestamp}.{ext}"
# Generate report
if output_format == "html":
report_path = reporter.generate_html(output)
elif output_format in ("markdown", "md"):
report_path = reporter.generate_markdown(output)
else:
report_path = reporter.generate_json(output)
console.print(f"[green]Report generated: {report_path}[/green]")
def _display_results(results: list[ScanResult], verbose: bool) -> None:
"""Display scan results in the terminal."""
severity_styles = {
"CRITICAL": "bold red",
"HIGH": "red",
"MEDIUM": "yellow",
"LOW": "blue",
"INFO": "dim",
}
for result in results:
suite_label = result.suite_name.upper().replace("_", " ")
console.print(f"[bold cyan][{suite_label}][/bold cyan]", end=" ")
console.print(
f"Tested {result.endpoints_tested} endpoints, "
f"found {len(result.findings)} issues "
f"({result.duration:.1f}s)"
)
for finding in result.findings:
style = severity_styles.get(finding.severity, "dim")
console.print(f" [{style}][{finding.severity}][/{style}] {finding.title}")
if verbose:
console.print(f" [dim]{finding.endpoint}[/dim]")
if finding.evidence:
console.print(f" [dim]Evidence: {finding.evidence}[/dim]")
console.print()
def _display_summary(summary: dict) -> None:
"""Display scan summary table."""
console.print()
table = Table(title="Scan Summary", show_header=True, header_style="bold")
table.add_column("Metric", style="cyan")
table.add_column("Value", justify="right")
table.add_row("Target", summary["target"])
table.add_row("Suites Executed", str(summary["suites_executed"]))
table.add_row("Endpoints Tested", str(summary["endpoints_tested"]))
table.add_row("Duration", f"{summary['total_duration_seconds']}s")
table.add_row("Critical", f"[bold red]{summary['findings']['critical']}[/bold red]")
table.add_row("High", f"[red]{summary['findings']['high']}[/red]")
table.add_row("Medium", f"[yellow]{summary['findings']['medium']}[/yellow]")
table.add_row("Low", f"[blue]{summary['findings']['low']}[/blue]")
table.add_row("Total Findings", str(summary["findings"]["total"]))
console.print(table)
if __name__ == "__main__":
cli()