explore_firmware_uip.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. """Capture SOL evidence without power changes, command guesses or takeover.
  3. Run from an installed checkout. A successful capture only proves that a SOL
  4. session was opened; it does not validate any UIP, RMT, AMT or UEFI command.
  5. """
  6. from __future__ import annotations
  7. import argparse
  8. import asyncio
  9. import json
  10. from datetime import datetime, timezone
  11. from pathlib import Path
  12. import sys
  13. from stl_backend import __version__
  14. from stl_backend.devices.base import DeviceError
  15. from stl_backend.devices.hardware import HardwareDevice
  16. def utc_now() -> str:
  17. return datetime.now(timezone.utc).isoformat()
  18. async def capture(args: argparse.Namespace) -> int:
  19. directory = args.output / datetime.now(timezone.utc).strftime("sol-%Y%m%dT%H%M%S-%fZ")
  20. directory.mkdir(parents=True, exist_ok=False)
  21. record = {
  22. "backend_version": __version__, "host": args.host, "user": args.user,
  23. "cipher_suite": args.cipher, "started_at": utc_now(),
  24. "requested_duration_s": args.duration, "status": "connecting",
  25. "raw_bytes": 0, "commands_sent": [], "power_commands_sent": [],
  26. "firmware_commands_verified": False,
  27. }
  28. helper = Path(__file__).resolve().parent.parent / "native" / "stl_sol_bridge.py"
  29. device = HardwareDevice({"bmc": {
  30. "host": args.host, "user": args.user, "password_env": args.password_env,
  31. "cipher_suite": args.cipher, "sol_timeout_s": 25,
  32. "sol_helper_argv": [sys.executable, str(helper)],
  33. }})
  34. result = 0
  35. try:
  36. with (directory / "sol.raw").open("wb") as raw:
  37. await device.sol_start()
  38. record["connected_at"] = utc_now()
  39. try:
  40. async with asyncio.timeout(args.duration):
  41. async for chunk in device.sol_read():
  42. raw.write(chunk)
  43. raw.flush()
  44. record["raw_bytes"] += len(chunk)
  45. except TimeoutError:
  46. # The requested observation period ended normally.
  47. pass
  48. record["status"] = "capture_complete"
  49. except (DeviceError, OSError) as exc:
  50. record.update(status="capture_failed", error=str(exc))
  51. result = 1
  52. except asyncio.CancelledError:
  53. record["status"] = "interrupted"
  54. raise
  55. finally:
  56. await device.close()
  57. record["finished_at"] = utc_now()
  58. (directory / "capture.json").write_text(
  59. json.dumps(record, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
  60. print(json.dumps({"directory": str(directory), **record}, ensure_ascii=False))
  61. return result
  62. def main() -> int:
  63. parser = argparse.ArgumentParser(description="被动捕获 SOL:不修改电源、不发送 BIOS 命令、不抢占会话")
  64. parser.add_argument("--host", default="192.168.0.100")
  65. parser.add_argument("--user", default="ADMIN")
  66. parser.add_argument("--cipher", type=int, default=17)
  67. parser.add_argument("--password-env", default="STL_BMC_S1_PASSWORD")
  68. parser.add_argument("--duration", type=float, default=30)
  69. parser.add_argument("--output", type=Path, default=Path("data/evidence"))
  70. args = parser.parse_args()
  71. if not 1 <= args.duration <= 3600:
  72. parser.error("--duration 必须在 1~3600 秒内")
  73. try:
  74. return asyncio.run(capture(args))
  75. except KeyboardInterrupt:
  76. return 130
  77. if __name__ == "__main__":
  78. raise SystemExit(main())