导语
单一协议实现容易变形。工程级系统需要插件化、异步队列、资源隔离与运维接口。本文把你现有的 handler 抽象成可注册插件,给出接口契约并提供实现建议。
目录
- 总体架构(模块图与职责)
- 核心接口契约(ProtocolHandler / attack_logger / persist)
- 异步设计与资源控制
- 插件化与热插拔实现路径
- 运维接口(Control Plane)与监控指标
- 数据管道与存储方案
1. 总体架构(模块与职责)
Control Plane
├─ Config Store (templates, policies)
├─ ProtocolRegistry (load/unload handlers)
└─ API (status, start/stop)
ProtocolRegistry --> handlers (HTTP/FTP/Telnet/SSH)
handlers -> Event Bus -> Persist Layer (JSONL / Kafka / DB)
-> Attack Logger -> SIEM/ELK
职责分明,保证 handler 专注于协议交互,持久化与上报由通用组件处理。
2. 核心接口契约
ProtocolHandler(抽象类)应实现:
start() -> Awaitable[Task]stop() -> Awaitablepersist(entry: Dict)(统一 schema)protocol_name属性
attack_logger(统一上报):
log_auth_attempt(source_ip, destination_port, protocol, username=None, password=None, pubkey_fingerprint=None)log_command_execution(source_ip, destination_port, protocol, command, command_type)log_file_access(source_ip, destination_port, protocol, file_path, access_type)
3. 异步设计与资源控制
- 使用
asyncio.Queue解耦网络 I/O 与持久化。Handler 推事件到队列,后端消费者异步批量写入 Kafka/文件/ES。 - 每个连接设置
asyncio.wait_for(reader.read(...), timeout=X),避免资源长期占用。 - 速率限制:Token Bucket 控制每 IP 的请求速率,防止单 IP 刷爆日志与资源。
4. 插件化与热插拔
实现路径:
handlers/目录,模块名与protocol_name一致。ProtocolRegistry读取配置并按需 importlib 加载模块。- 热插拔:通过 Control Plane API 动态创建/取消
asyncio.create_task(handler.start()),并管理任务句柄。
优点:新增协议只需实现 ProtocolHandler 子类并放入 handlers/。
5. 运维接口(Control Plane)
建议暴露 HTTP REST 管理接口或 CLI:
GET /status-> 返回监听端口、活跃会话数、队列长度。POST /handlers/start-> 启动某 handler。POST /handlers/stop-> 停止 handler。GET /sessions/{id}-> 返回 session 详情(脱敏策略适配)。
监控指标(Prometheus):
honeypot_active_sessions_totalhoneypot_auth_attempts_totalhoneypot_events_queue_depthhoneypot_bytes_in/out
6. 数据管道与存储
- 开发初期:JSONL 文件(每日 rotate)。
- 生产:Kafka topic -> Worker -> Elasticsearch / ClickHouse。
- 建议使用 schema registry(或 internal mapping)确保字段稳定性。
行动清单(优先级)
- 抽象
ProtocolHandler接口,重构 handler 以实现该契约。 - 引入
asyncio.Queue作为事件总线。 - 实装 Control Plane 的最小 API(status/start/stop)。
- 将
persist()改为异步写入队列,避免阻塞 handler。