Govind-Kandale-1/iac-audit-tool
GitHub: Govind-Kandale-1/iac-audit-tool
一款基于 LLM 驱动的 IaC 安全审计平台,通过多扫描器联合检测、智能去重与 AI 修复建议,帮助团队在部署前发现并修复基础设施代码中的安全配置问题。
Stars: 0 | Forks: 0
# IaC 审计工具
基于 LLM 驱动的“基础设施即代码”安全审查工具。它使用四个独立的扫描器扫描 Terraform、CloudFormation、Kubernetes 和 Dockerfiles,对扫描结果进行去重,并使用 **Claude AI** (claude-opus-4-7) 为每个发现生成通俗易懂的英文解释和具体的修复代码。
## 架构
```
GitHub Actions / Webhook
│
│ (OIDC — no long-lived credentials)
▼
Self-Hosted Runner (private VPC — no IGW)
│
│ Lambda invoke via VPC Interface Endpoint (PrivateLink)
▼
┌────────────────────────────────────────────────────────────┐
│ Orchestrator Lambda │
│ • Detects IaC type (TF / CFN / K8s / Dockerfile) │
│ • Fans out one SQS message per scanner │
└───────────────────────┬────────────────────────────────────┘
│ SQS (VPC endpoint)
┌─────────────┼─────────────┬──────────────┐
▼ ▼ ▼ ▼
Checkov tfsec OPA Snyk IaC
Lambda Lambda Lambda Lambda
│ │ │ │
└───────────────┴─────────────┴───────────────┘
│ S3 raw results (gateway endpoint)
▼
Results Processor Lambda
• Normalises all scanner outputs → unified schema
• Deduplicates findings across scanners
• Bumps severity when ≥2 scanners agree
• Writes to DynamoDB (findings table)
• Invokes LLM Reviewer async (CRITICAL + HIGH only)
│
▼
LLM Reviewer Lambda ◄── Anthropic API (claude-opus-4-7)
• Builds structured prompt per finding │
• adaptive thinking + prompt caching │
• Parses response → explanation + remediation code │
• Writes llm_explanation, llm_remediation to DynamoDB
│
┌──────────────┘
▼
API Gateway (private) + Cognito auth
│
▼
API Lambda → DynamoDB queries
│
CloudFront + S3 → Static Dashboard
```
## 扫描器
| 扫描器 | 检测内容 | IaC 类型 |
|---------|----------------|-----------|
| **Checkov** | 1000+ 内置检查项 (AWS/GCP/Azure 错误配置) | TF, CFN, K8s, Docker |
| **tfsec** | 特定于 Terraform 的安全问题 | Terraform |
| **OPA** | 自定义 Rego 策略 (CIS, SOC2, PCI-DSS, NIST 800-53) | Terraform |
| **Snyk IaC** | Snyk 托管的规则集 + CVE 数据库 | TF, CFN, K8s |
多扫描器交叉验证会提升严重级别——一个被 4 个扫描器确认的 LOW 级别发现会升级为 CRITICAL 级别。
## 合规框架
自定义 OPA 策略涵盖:
| 框架 | 策略 |
|-----------|---------|
| **CIS AWS Foundations** | S3, IAM, EC2, RDS |
| **SOC2 Type II** | 加密、日志记录、访问控制 |
| **PCI-DSS** | 网络隔离、数据保护、监控 |
| **NIST 800-53** | AC (访问), AU (审计), SC (通信), SI (完整性) |
## LLM 审查 (Claude AI)
`llm_reviewer` Lambda 调用开启了 **自适应思考** (adaptive thinking) 和 **提示缓存** (prompt caching) 的 **claude-opus-4-7** 来审查每一个 CRITICAL/HIGH 级别的发现:
- **解释** — 为什么它是一个风险、威胁模型以及受影响的合规控制项
- **修复** — 用于修复该问题的精确 Terraform/CloudFormation/Kubernetes 代码
系统提示使用 `cache_control: ephemeral` 进行缓存——每个发现的提示是唯一的可变内容,从而保持了较低的 API 成本。
## 仓库结构
```
iac-audit-tool/
├── .github/workflows/iac-scan.yml # CI: validate → build images → deploy
├── infra/ # Terraform for the tool itself
│ ├── vpc.tf # Private VPC, no IGW
│ ├── vpc_endpoints.tf # PrivateLink (Lambda, SQS, ECR, S3, DynamoDB, SM)
│ ├── iam.tf # OIDC + Lambda execution roles (least-privilege)
│ ├── sqs.tf # Scan queue + DLQ
│ ├── dynamodb.tf # Findings table (TTL, GSIs, PITR)
│ ├── s3.tf # Raw results + policy bundle + dashboard
│ ├── api_gateway.tf # Private REST API + Cognito authoriser
│ ├── cloudfront.tf # Dashboard CDN + all Lambda functions
│ ├── ecr.tf # Container image repos (immutable tags)
│ └── secrets.tf # Secrets Manager (Snyk + Anthropic keys)
├── lambdas/
│ ├── orchestrator/ # IaC detection + SQS fan-out
│ ├── scanner_checkov/ # Checkov container Lambda
│ ├── scanner_tfsec/ # tfsec container Lambda
│ ├── scanner_opa/ # OPA container Lambda
│ ├── scanner_snyk/ # Snyk IaC container Lambda
│ ├── results_processor/ # Normalise → deduplicate → score → store
│ ├── llm_reviewer/ # Claude AI explanation + remediation
│ └── api/ # Findings API (GET /findings, GET /scans)
├── policies/ # OPA Rego policy bundle
│ ├── cis/ # CIS AWS Foundations
│ ├── soc2/ # SOC2 controls
│ ├── pci_dss/ # PCI-DSS requirements
│ └── nist_800_53/ # NIST 800-53 control families
├── dashboard/ # Static frontend (CloudFront + S3)
├── shared/ # Python shared layer (logger, boto3 clients)
├── tests/
│ ├── unit/ # Parser, dedup, severity scorer, normaliser
│ └── fixtures/ # Sample IaC + scanner output JSON
├── scripts/
│ ├── build_policy_bundle.sh # tar + upload Rego bundle to S3
│ ├── build_images.sh # docker build + push all Lambdas to ECR
│ └── seed_dynamo.py # Local dev: seed test findings
├── docker-compose.yml # LocalStack + scanners for local dev
├── Makefile # make build / test / deploy shortcuts
└── .env.example
```
## 快速开始
### 前置条件
- AWS CLI + 已配置的账号
- Terraform >= 1.7.0
- Docker
- Python 3.12+
- Anthropic API 密钥 (claude-opus-4-7)
- Snyk 令牌 (用于 Snyk IaC 扫描器)
### 1. 初始化 Terraform 后端
```
aws s3 mb s3://iac-audit-tool-tfstate
aws dynamodb create-table \
--table-name iac-audit-tool-tflock \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
```
### 2. 存储密钥
```
aws secretsmanager create-secret --name iac-audit-prod/snyk-token \
--secret-string '{"token":"your-snyk-token"}'
aws secretsmanager create-secret --name iac-audit-prod/anthropic-api-key \
--secret-string '{"api_key":"your-anthropic-api-key"}'
```
### 3. 部署
```
cp .env.example .env # fill in values
# 构建并推送 Lambda 镜像
make build ECR_REGISTRY=.dkr.ecr.us-east-1.amazonaws.com TAG=$(git rev-parse --short HEAD)
# 上传 OPA policy bundle
make policy-bundle POLICY_BUCKET=iac-audit-prod-policy-bundle-
# 部署 infrastructure
cd infra && terraform init && terraform apply \
-var="image_tag=$(git rev-parse --short HEAD)" \
-var="snyk_token_arn=arn:aws:secretsmanager:us-east-1::secret:iac-audit-prod/snyk-token" \
-var="anthropic_api_key_arn=arn:aws:secretsmanager:us-east-1::secret:iac-audit-prod/anthropic-api-key"
```
### 4. 使用 LocalStack 进行本地开发
```
make localstack-up
python scripts/seed_dynamo.py # seed test findings
make test # run unit tests
```
## CI/CD 流水线
| 阶段 | 触发条件 | 操作 |
|-------|---------|--------|
| 验证 | 所有 PR | `terraform validate`、`terraform fmt`、单元测试 |
| Checkov 扫描 | 所有 PR | 扫描 `infra/` — SARIF 上传至 GitHub 安全选项卡 |
| 构建与推送 | 推送到 `main` | Docker 构建 + ECR 推送 (OIDC — 无存储密钥) |
| 部署 | 构建之后 | `terraform apply`,OPA 捆绑包上传 |
## 安全设计
| 控制措施 | 实现方式 |
|---------|---------------|
| 无公共端点 | 所有 Lambda 均位于私有 VPC 中,所有 AWS 流量均通过 VPC 端点传输 |
| 无存储的凭证 | GitHub OIDC → AWS 角色,机密存储在 Secrets Manager 中 |
| 不可变镜像 | ECR `IMAGE_TAG_MUTABILITY = IMMUTABLE` |
| 最小权限 IAM | 每个 Lambda 的执行角色,无通配符 Actions |
| 发现结果 TTL | DynamoDB TTL — 90 天后自动过期 |
| 静态加密 | DynamoDB SSE,S3 AES256 |
| Cognito 身份验证 | API Gateway 拒绝未经身份验证的请求 |
标签:AI, AWS Lambda, CISA项目, Claude, CloudFormation, CVE检测, DevSecOps, DLL 劫持, Dockerfile, EC2, ECS, GitHub Actions, IaC, LLM, OPA, PrivateLink, S3, Snyk, SQS, Terraform, tfsec, Unmanaged PE, VPC, 上游代理, 人工智能, 代码修复, 大语言模型, 安全合规, 安全审查, 安全左移, 漏洞探索, 用户模式Hook绕过, 策略即代码, 网络代理, 聊天机器人安全, 自动化代码审查, 自动化运维, 自动笔记, 请求拦截, 逆向工具, 靶场