说明
- 安装依赖:
pip install requests - 从 syslog、JSON 或纯文本文件中摄取日志条目。
- 对每条条目,计算 SHA-256 哈希值:previous_hash + timestamp + log_content。
- 将链存储为 JSON 账本,包含条目索引、时间戳、内容哈希、前一哈希和链哈希。
- 通过重新计算所有哈希值并检测断点来验证链完整性。
- 可选择将检查点哈希锚定到外部时间戳服务。
python scripts/agent.py --log-file /var/log/syslog --chain-file log_chain.json --verify --output integrity_report.json
示例
链条目结构
{"index": 42, "timestamp": "2024-01-15T10:30:00Z", "content_hash": "a1b2c3...",
"prev_hash": "d4e5f6...", "chain_hash": "SHA256(prev_hash + timestamp + content_hash)"}
篡改检测
若第 42 条条目被修改,chain_hash[42] 将不匹配 SHA256(chain_hash[41] + ...),从第 42 条起的所有条目都将被标记为无效。