Catheren/terraform-security-lab

GitHub: Catheren/terraform-security-lab

一个 Terraform 基础设施安全实验室,通过 GitHub Actions CI/CD 流水线集成多种自动化扫描工具和自定义 Python 策略,在代码合并前拦截 IaC 配置不当与机密泄漏问题。

Stars: 0 | Forks: 0

# Terraform 安全实验室 [![Terraform](https://img.shields.io/badge/Terraform-%E2%89%A51.0-7B42BC?logo=terraform&logoColor=white)](https://www.terraform.io/) [![GitHub Actions](https://img.shields.io/badge/GitHub_Actions-CI%2FCD-2088FF?logo=github-actions&logoColor=white)](https://github.com/features/actions) [![Checkov](https://img.shields.io/badge/Checkov-Policy_Scan-4CAF50?logo=checkmarx&logoColor=white)](https://www.checkov.io/) [![tfsec](https://img.shields.io/badge/tfsec-Security_Scan-blue)](https://aquasecurity.github.io/tfsec/) [![Gitleaks](https://img.shields.io/badge/Gitleaks-Secret_Scan-red)](https://github.com/gitleaks/gitleaks) [![Python](https://img.shields.io/badge/Python-3.12-3776AB?logo=python&logoColor=white)](https://www.python.org/) [![AWS](https://img.shields.io/badge/AWS-Provider-FF9900?logo=amazon-aws&logoColor=white)](https://registry.terraform.io/providers/hashicorp/aws/latest) [![License](https://img.shields.io/badge/License-MIT-blue)](LICENSE) 一个生产级的基础设施安全工程项目,演示了如何在每个 pull request 上构建并强制执行安全门——在 insecure 代码触及 AWS 之前。 每个 PR 都会自动扫描基础设施的配置不当、合规违规、提交的机密信息以及特定于组织的策略违规。**一个故意包含安全缺陷的 PR 被提交,以证明 pipeline 能捕获真实的攻击——而且它确实做到了。** ## 这演示了什么 | 技能 | 实现 | |---|---| | Infrastructure as Code | 模块化的 Terraform,带有默认安全的 AWS 资源 | | CI/CD 安全 pipeline | 5 个作业的 GitHub Actions 工作流,遇到安全失败时阻断 | | IaC 配置不当扫描 | tfsec + Checkov(CIS、SOC2、PCI-DSS 基准) | | 机密信息检测 | Gitleaks 在每个 PR 上扫描完整的 git 历史 | | 自定义策略工具 | Python 扫描器,强制执行 5 条组织特定规则 | | 合规文档 | SECURITY.md,包含发现的问题和已接受的风险理由 | | 对抗性测试 | Bad PR 模拟——捕获并阻止了 3 个真实的漏洞 | ## 项目结构 ``` . ├── .github/ │ └── workflows/ │ └── terraform.yml # 5-job security pipeline ├── modules/ │ ├── vpc/ # Network topology — public IP disabled, default SG locked down │ ├── ec2/ # Compute — IMDSv2 enforced, EBS encrypted │ ├── iam/ # Roles — least-privilege, no wildcard actions │ ├── s3/ # Storage — encryption, versioning, public access blocked │ └── logging/ # CloudTrail — log file validation, lifecycle policy ├── scanner/ │ └── scanner.py # Custom Python security scanner ├── tests/ │ └── bad-infra/ │ └── main.tf # Intentionally insecure Terraform (pipeline proof) ├── main.tf ├── variables.tf ├── outputs.tf ├── provider.tf └── SECURITY.md ``` ## CI/CD 安全 pipeline 在每次 push 和 pull request 时触发。每个作业独立运行——其中一个失败不会抑制其他作业的结果。 ``` Pull Request opened │ ▼ ┌─────────────────────────┐ │ terraform fmt + validate│ Syntax and formatting gate └────────────┬────────────┘ │ ┌────────┴──────────────────────────┐ ▼ ▼ ▼ ▼ ┌────────┐ ┌────────┐ ┌─────────┐ ┌──────────────┐ │ tfsec │ │Checkov │ │Gitleaks │ │Custom Scanner│ │IaC │ │CIS/SOC2│ │Secret │ │Org policy │ │security│ │PCI-DSS │ │scanning │ │enforcement │ └────────┘ └────────┘ └─────────┘ └──────────────┘ │ All must pass │ PR can merge ``` | 作业 | 工具 | 是否阻断合并? | 捕获内容 | |---|---|---|---| | 格式与验证 | terraform fmt/validate | 是 | 语法错误、格式偏差 | | IaC 安全扫描 | tfsec | 是 | 基础设施配置不当 | | 策略扫描 | Checkov | 软失败(记录在案的例外情况) | CIS、SOC2、PCI-DSS 违规 | | 机密信息扫描 | Gitleaks | 是 | 提交到 git 历史中的凭据 | | 自定义扫描器 | scanner.py | 是 (CRITICAL/HIGH) | 组织特定的策略违规 | ## 自定义 Python 安全扫描器 `scanner/scanner.py` 强制执行通用工具不知道的组织特定策略。它在 pipeline 中作为独立的 CLI 运行,如果发现 CRITICAL 或 HIGH 级别的问题,则以退出代码 1 退出,从而自动阻断 PR。 ### 强制执行的规则 | 规则 | 严重性 | 策略 | |---|---|---| | TAG-001 | HIGH | 所有资源必须具有:Environment、Owner、Project、DataClassification | | NAME-001 | MEDIUM | 资源必须遵循命名规范:`{project}-{env}-{purpose}` | | RGN-001 | HIGH | 仅允许在 us-east-1 和 us-west-2 中部署 | | ACCT-001 | CRITICAL | 禁止硬编码 AWS 账户 ID — 使用 `var.aws_account_id` | | S3-001 | MEDIUM | 所有 S3 存储桶必须配置 `aws_s3_bucket_logging` | ``` # 本地运行 python scanner/scanner.py --path . python scanner/scanner.py --path . --fail-on-findings ``` ## Bad PR 模拟 — pipeline 验证 为了验证 pipeline 能捕获真实的攻击(而不仅仅是理论上的),在 `bad-infra/simulate-insecure-pr` 分支上引入了一个故意包含安全缺陷的 Terraform 文件作为 pull request。模拟了三个漏洞: - **公开的 S3 bucket** — 所有 `block_public_*` 控件设置为 false,将存储的数据暴露给互联网 - **不受限制的 SSH** — 安全组允许来自 `0.0.0.0/0` 的入站端口 22 - **通配符 IAM 策略** — 在 `Resource: "*"` 上使用 `Action: ["*"]`,实际上授予了 root 级别的 AWS 访问权限 **Checkov 扫描检测到了所有这三个漏洞,并阻止了该 PR 的合并。** | 发现 | 规则 | 漏洞 | |---|---|---| | CKV_AWS_53/54/55/56 | S3 公开访问 | 公开的 S3 bucket | | CKV_AWS_24 | 不受限制的 SSH | 向 0.0.0.0/0 开放端口 22 | | CKV_AWS_62 | 通配符 IAM | 完整的账户权限 | 这个 Bad PR 在本仓库中保持打开状态,作为 pipeline 按预期工作的永久证据。请查看[打开的 pull request](../../pulls)以查看发现的问题输出。 ![Checkov 阻断 bad PR](https://raw.githubusercontent.com/Catheren/terraform-security-lab/main/screenshot_bad_pr.png) ## AWS 模块 — 安全控制 | 控制 | 资源 | 实现 | |---|---|---| | 强制执行 IMDSv2 | EC2 | `http_tokens = required` | | EBS 加密 | EC2 | `root_block_device encrypted = true` | | 详细监控 | EC2 | `monitoring = true` | | 阻断公开访问 | S3 | `block_public_* = true` | | 日志文件验证 | CloudTrail | `enable_log_file_validation = true` | | 日志保留策略 | S3/CloudTrail | 生命周期:90天 → STANDARD_IA,365天 → 过期 | | 默认 SG 锁定 | VPC | `aws_default_security_group`,无规则 | | 禁用公共 IP | VPC 子网 | `map_public_ip_on_launch = false` | 已接受的风险及其理由记录在 [SECURITY.md](SECURITY.md) 中。 ## 快速开始 ### 前置条件 - [Terraform](https://developer.hashicorp.com/terraform/install) >= 1.0 - Python 3.12 - [Checkov](https://www.checkov.io/2.Basics/Installing%20Checkov.html) *(可选 — 用于本地扫描)* ### 在本地运行安全检查 ``` # Terraform terraform init -backend=false terraform fmt -check -recursive terraform validate # 自定义 scanner python scanner/scanner.py --path . python scanner/scanner.py --path . --fail-on-findings # Checkov checkov -d . --framework terraform ``` ## 技术栈 | 工具 | 用途 | |---|---| | [Terraform](https://www.terraform.io/) ≥ 1.0 | Infrastructure as Code | | [GitHub Actions](https://github.com/features/actions) | CI/CD 编排 | | [tfsec](https://aquasecurity.github.io/tfsec/) | Terraform 安全扫描 | | [Checkov](https://www.checkov.io/) | IaC 合规策略扫描 | | [Gitleaks](https://github.com/gitleaks/gitleaks) | git 历史中的机密信息检测 | | Python 3.12 | 自定义安全扫描器 | | AWS Provider | AWS 资源定义 | ## 许可证 [MIT 许可证](LICENSE)
标签:DevSecOps, ECS, GitHub Actions, Terraform, 上游代理, 安全合规, 漏洞利用检测, 网络代理, 自动笔记, 逆向工具, 静态代码扫描