| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #!/usr/bin/env python3
- """Capture SOL evidence without power changes, command guesses or takeover.
- Run from an installed checkout. A successful capture only proves that a SOL
- session was opened; it does not validate any UIP, RMT, AMT or UEFI command.
- """
- from __future__ import annotations
- import argparse
- import asyncio
- import json
- from datetime import datetime, timezone
- from pathlib import Path
- import sys
- from stl_backend import __version__
- from stl_backend.devices.base import DeviceError
- from stl_backend.devices.hardware import HardwareDevice
- def utc_now() -> str:
- return datetime.now(timezone.utc).isoformat()
- async def capture(args: argparse.Namespace) -> int:
- directory = args.output / datetime.now(timezone.utc).strftime("sol-%Y%m%dT%H%M%S-%fZ")
- directory.mkdir(parents=True, exist_ok=False)
- record = {
- "backend_version": __version__, "host": args.host, "user": args.user,
- "cipher_suite": args.cipher, "started_at": utc_now(),
- "requested_duration_s": args.duration, "status": "connecting",
- "raw_bytes": 0, "commands_sent": [], "power_commands_sent": [],
- "firmware_commands_verified": False,
- }
- helper = Path(__file__).resolve().parent.parent / "native" / "stl_sol_bridge.py"
- device = HardwareDevice({"bmc": {
- "host": args.host, "user": args.user, "password_env": args.password_env,
- "cipher_suite": args.cipher, "sol_timeout_s": 25,
- "sol_helper_argv": [sys.executable, str(helper)],
- }})
- result = 0
- try:
- with (directory / "sol.raw").open("wb") as raw:
- await device.sol_start()
- record["connected_at"] = utc_now()
- try:
- async with asyncio.timeout(args.duration):
- async for chunk in device.sol_read():
- raw.write(chunk)
- raw.flush()
- record["raw_bytes"] += len(chunk)
- except TimeoutError:
- # The requested observation period ended normally.
- pass
- record["status"] = "capture_complete"
- except (DeviceError, OSError) as exc:
- record.update(status="capture_failed", error=str(exc))
- result = 1
- except asyncio.CancelledError:
- record["status"] = "interrupted"
- raise
- finally:
- await device.close()
- record["finished_at"] = utc_now()
- (directory / "capture.json").write_text(
- json.dumps(record, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
- print(json.dumps({"directory": str(directory), **record}, ensure_ascii=False))
- return result
- def main() -> int:
- parser = argparse.ArgumentParser(description="被动捕获 SOL:不修改电源、不发送 BIOS 命令、不抢占会话")
- parser.add_argument("--host", default="192.168.0.100")
- parser.add_argument("--user", default="ADMIN")
- parser.add_argument("--cipher", type=int, default=17)
- parser.add_argument("--password-env", default="STL_BMC_S1_PASSWORD")
- parser.add_argument("--duration", type=float, default=30)
- parser.add_argument("--output", type=Path, default=Path("data/evidence"))
- args = parser.parse_args()
- if not 1 <= args.duration <= 3600:
- parser.error("--duration 必须在 1~3600 秒内")
- try:
- return asyncio.run(capture(args))
- except KeyboardInterrupt:
- return 130
- if __name__ == "__main__":
- raise SystemExit(main())
|