xoity/AuditAgent
GitHub: xoity/AuditAgent
AuditAgent 是一个基于 Python 的 Linux iptables 防火墙策略声明式管理工具,解决跨服务器防火墙规则审计、漂移检测与自动修复的合规问题。
Stars: 3 | Forks: 0
# AuditAgent - Linux iptables 策略执行器与审计工具 [](https://pypi.org/project/audit-agent/) [](LICENSE) [](https://github.com/xoity/AuditAgent/actions)
- 一个 Python 框架,用于声明式地定义和跨 Linux 服务器执行 iptables 防火墙策略,而无需在服务器本身上安装 agent。
## 目录
- [功能](#features)
- [快速入门](#getting-started)
- [自动修复](#automated-remediation)
- [确定性沙盒测试](#deterministic-sandbox-testing)
- [配置指南](#configuration-guide)
- [安全认证](#secure-authentication)
- [示例](#examples)
- [安装](#installation)
- [项目结构](#project-structure)
- [支持的设备](#supported-devices)
- [贡献](#contributing)
- [许可证](#license)
## 功能
- **声明式策略定义**:使用 Python DSL 定义 iptables 策略
- **Linux iptables 支持**:全面支持 iptables 防火墙规则
- **策略审计与漂移检测**:将实时 iptables 规则与声明的策略进行比较
- **🤖 AI 驱动的修复**:使用 Google AI 或 OpenAI 自动生成修复策略
- **自动修复**:智能自动修复检测到的策略违规
- **基于风险的策略**:保守、平衡和激进的修复方法
- **回滚功能**:验证失败时自动回滚
- **幂等执行**:仅在需要时应用更改
- **预检验证**:在应用更改之前进行模拟
- **安全认证**:动态凭证提示和 SSH agent 集成
- **SSH 认证**:支持密码和基于密钥的认证
## 快速入门
请参阅[快速入门指南](docs/GETTING_STARTED.md)了解安装步骤、示例代码和 CLI 用法。
### 验证工作流
针对 Alpine Linux 虚拟机的端到端实时测试,展示审计、试运行、执行和验证过程:
```
# 1. 创建 policy(允许 SSH、HTTP;阻止 telnet)
cat > policy.yaml << 'EOF'
metadata:
name: "vm-policy"
version: "1.0"
description: "Test policy for SSH + HTTP"
firewall_rules:
- name: "allow-ssh"
enabled: true
priority: 10
action: "allow"
direction: "inbound"
protocol: { name: "tcp", number: 6 }
destination_ports: [{ number: 22 }]
- name: "allow-http"
enabled: true
priority: 20
action: "allow"
direction: "inbound"
protocol: { name: "tcp", number: 6 }
destination_ports: [{ number: 80 }]
- name: "block-telnet"
enabled: true
priority: 50
action: "deny"
direction: "inbound"
protocol: { name: "tcp", number: 6 }
destination_ports: [{ number: 23 }]
log_traffic: true
EOF
# 2. 创建设备 config(VM 位于 127.0.0.1:2200)
cat > devices.yaml << 'EOF'
devices:
- type: "linux_iptables"
name: "test-vm"
host: "127.0.0.1"
port: 2200
username: "vagrant"
EOF
# 3. 审计当前状态
audit-agent audit policy.yaml devices.yaml --full-report
# 输出:0% compliance,缺失 3 条 rules
# 4. Dry-run enforce(验证 commands,不作更改)
audit-agent enforce --dry-run policy.yaml devices.yaml
# 输出:计划执行 3 个 actions,全部已验证
# 5. Live enforce(应用 rules)
audit-agent enforce --no-dry-run policy.yaml devices.yaml
# 输出:执行 3 个 actions,3 个成功,0 个失败
# 6. 验证设备上的 iptables
ssh vagrant@127.0.0.1 -p 2200 "sudo iptables -L -n"
# 显示:ACCEPT tcp dpt:22, ACCEPT tcp dpt:80, LOG+DROP tcp dpt:23
# 7. 重新审计以确认 100% compliance
audit-agent audit policy.yaml devices.yaml
# 输出:100% compliance,0 个 issues
```
## AI 驱动的修复 🤖
AuditAgent 现在包含 AI 驱动的自动修复功能,它使用高级语言模型来分析合规性问题并生成修正后的策略:
```
# 设置您的 Google AI Studio API key(提供 free tier)
export GOOGLE_AI_API_KEY="your-key-here"
# 生成并应用 AI 驱动的 remediation
audit-agent ai-remediate policy.yaml devices.yaml --apply
```
**功能:**
- 🎯 自动实现 100% 合规
- 🔄 迭代优化以获得最佳结果
- 📊 详细的分析和摘要报告
- 🌐 支持 Google AI Studio (Gemini)、OpenAI、Azure OpenAI
- 🔒 安全的本地 API key 管理
- 🚀 为未来的 Web 管理而设计
完整文档请参阅 [AI 修复指南](docs/AI_REMEDIATION.md)。
## 自动修复
AuditAgent 现在支持智能自动修复,无需人工干预即可修复检测到的策略违规。此功能提供:
- **智能决策**:基于风险分析,判断是否应自动修复违规
- **多种策略**:从保守、平衡或激进的修复方法中选择
- **安全第一**:默认进行试运行,对高风险更改需要明确确认
- **回滚保护**:如果修复后验证失败,则自动回滚
- **全面报告**:详细记录所有已执行的修复操作
### 快速开始
```
# Dry-run 自动化 remediation(安全,显示将要执行的操作)
audit-agent auto-remediate --devices devices.yaml --policy policy.yaml
# 使用 conservative 策略执行 remediation
audit-agent auto-remediate --devices devices.yaml --policy policy.yaml --execute --strategy conservative
# 查看详细帮助
audit-agent auto-remediate --help
```
完整文档请参阅[自动修复指南](docs/AUTOMATED_REMEDIATION.md)。
## 确定性沙盒测试
使用持久化沙盒后端进行可重复的审计/执行测试,无需虚拟机、SSH 目标或更改本地防火墙。
```
# 重置 sandbox 状态
rm -f .audit-agent/sandbox-42.json
# 针对 deterministic sandbox 运行
audit-agent audit examples/simple-linux-policy.yaml examples/sandbox-devices.yaml
audit-agent enforce --dry-run examples/simple-linux-policy.yaml examples/sandbox-devices.yaml
audit-agent enforce --no-dry-run examples/simple-linux-policy.yaml examples/sandbox-devices.yaml
# 验证 idempotency
audit-agent enforce --no-dry-run examples/simple-linux-policy.yaml examples/sandbox-devices.yaml
```
这将为您提供:
- `audit` 在缺少规则的情况下启动并显示漂移
- `--dry-run` 验证计划的更改,而不会更改沙盒状态
- 实时 `enforce` 将更改应用到持久化沙盒文件
- 第二次实时 `enforce` 保持干净,且不会创建重复的规则
- 回滚由测试套件通过强制失败路径覆盖
运行专用测试:
```
rtk pytest tests/test_sandbox_enforcement.py -vv
```
运行全套测试:
```
rtk pytest tests -vv
```
## 配置指南
有关详细的 YAML schema 和参考,请参阅[配置指南](docs/CONFIGURATION_GUIDE.md)。
## 安全认证
AuditAgent 支持无需硬编码凭证的安全认证。请参阅[安全认证指南](docs/SECURE_AUTHENTICATION.md)了解:
- **SSH agent 集成**:使用 SSH agent 进行 key 管理
- **动态凭证提示**:在运行时提示输入密码
- **从硬编码凭证迁移**:消除配置文件中的安全风险
快速示例:
```
devices:
- type: "linux_iptables"
name: "web-server-01"
host: "192.168.0.111"
username: "vagrant"
private_key: "~/.ssh/id_rsa"
# No hardcoded passwords - prompts when needed
```
## 示例
`examples/` 目录包含示例策略和设备配置:
- **simple-linux-policy.yaml**:最小化策略示例
- **devices.yaml**:示例设备清单配置
- **devices-secure.yaml**:无硬编码凭证的安全设备配置
- **web-server-policy.yaml**:端到端 Web 服务器策略
- **automated_remediation_demo.py**:演示自动修复功能
- 在 [examples/](examples/) 文件夹中查看其他配置。
## 安装
### 标准安装
```
pip install audit-agent
```
### 包含 AI 支持
```
pip install audit-agent[ai]
```
或从源码安装:
```
git clone https://github.com/xoity/AuditAgent
cd AuditAgent
pip install -e ".[ai]"
```
## 项目结构
```
audit_agent/
├── core/ # Core policy and rule definitions
├── devices/ # Linux iptables implementation
├── audit/ # Audit and compliance checking
├── enforcement/ # Policy enforcement engine
├── validation/ # Pre-flight checks and validation
└── utils/ # Utilities and helpers
```
## 支持的设备
- 带有 iptables 防火墙的 Linux 服务器
## 贡献
欢迎贡献、提出问题和功能请求。请在 [GitHub 仓库](https://github.com/xoity/AuditAgent)中开启 issue 或提交 pull request。
## 许可证
MIT License
标签:AI驱动, Petitpotam, Python, 内存分配, 无后门, 策略审计, 网络防火墙, 自动化运维, 运维, 逆向工具