build 4.6 Demo
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""Headless CLI for IoT PreTester 4.6 Demo.
|
||||
|
||||
Examples:
|
||||
python -m analyzer.cli capture.pcapng
|
||||
python -m analyzer.cli capture.pcapng --format json -o report.json
|
||||
python -m analyzer.cli capture.pcapng --format txt --packet-limit 50000
|
||||
|
||||
Exit codes:
|
||||
0 Analysis completed without high or critical findings
|
||||
2 Analysis completed with at least one high or critical finding
|
||||
1 Capture or runtime dependency error
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from . import __version__
|
||||
from .engine import AnalysisEngine
|
||||
from .findings import Severity
|
||||
from .reporting import render, FORMATS
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
p = argparse.ArgumentParser(
|
||||
prog="analyzer",
|
||||
description="Analyze the security of a PCAP or PCAPNG network capture.",
|
||||
)
|
||||
p.add_argument("capture", help="Path to a .pcap or .pcapng file")
|
||||
p.add_argument("-f", "--format", choices=FORMATS, default="md",
|
||||
help="Output format (default: md)")
|
||||
p.add_argument("-o", "--output", default=None,
|
||||
help="Output file (default: standard output)")
|
||||
p.add_argument("--packet-limit", type=int, default=None,
|
||||
help="Maximum packets to read from a large capture")
|
||||
p.add_argument(
|
||||
"--version",
|
||||
action="version",
|
||||
version=f"IoT PreTester 4.6 Demo analyzer {__version__}",
|
||||
)
|
||||
return p
|
||||
|
||||
|
||||
def main(argv=None) -> int:
|
||||
args = build_parser().parse_args(argv)
|
||||
|
||||
try:
|
||||
result = AnalysisEngine().analyze_pcap(args.capture, packet_limit=args.packet_limit)
|
||||
except FileNotFoundError as exc:
|
||||
print(f"Error: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
except RuntimeError as exc:
|
||||
print(f"Error: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
report = render(result, args.format)
|
||||
|
||||
if args.output:
|
||||
with open(args.output, "w", encoding="utf-8") as fh:
|
||||
fh.write(report)
|
||||
print(f"Report written: {args.output} "
|
||||
f"({result.meta.get('finding_count', 0)} Findings)", file=sys.stderr)
|
||||
else:
|
||||
print(report)
|
||||
|
||||
worst = max((f.severity.rank for f in result.findings), default=0)
|
||||
return 2 if worst >= Severity.HIGH.rank else 0
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user