Josperdo/cloud-soc-terraform

GitHub: Josperdo/cloud-soc-terraform

基于 Terraform 的多云安全运营中心实验环境,支持一键部署到 Azure 和 AWS,内置日志收集、威胁检测和 MITRE ATT&CK 映射规则。

Stars: 0 | Forks: 0

# 多云 SOC 实验环境 — 基础设施即代码 ![Terraform CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/7893e2c039075304.svg) 模块化 Terraform 部署方案,用于构建跨越 **Azure** 和 **AWS** 的多云安全运营中心 实验环境。本项目作为作品集展示,演示了跨两大主流云平台的云安全架构、检测工程和 DevSecOps 实践。 通过单个 Makefile 流水线即可部署到任意云平台: ``` make deploy target=azure make deploy target=aws ``` ## ⚠️ AWS Free Tier — 费用警告 在部署 AWS 栈之前,请务必阅读此部分。部分服务提供限时免费试用,**从您运行 `terraform apply` 的那一刻开始计算**。 | 服务 | Free Tier (免费层) | 之后会发生什么 | |---|---|---| | EC2 t3.micro | 750 小时/月 — **仅限 12 个月** | 第 1 年后约 $8/月 | | GuardDuty | **30 天免费试用** | 按分析的 GB 数收费 | | Security Hub | **30 天免费试用** | 按摄入的发现结果收费 | | CloudTrail (首个跟踪) | 免费 — 仅限管理事件 | 数据事件从不免费 — 请勿启用 | | CloudWatch Logs | 5 GB/月 摄入 + 5 GB 存储 | 超出后约 $0.50/GB | | SSM Session Manager | 始终免费 | — | **部署前的建议操作:** 1. 设置账单警报:AWS Console → Billing → Budgets → Create budget ($5/月) 2. 截图后立即运行 `terraform destroy` — 不要闲置资源 3. 仅在账户激活后启用 GuardDuty 和 Security Hub(在 `terraform.tfvars` 中设置 `enable_threat_detection = true`) 4. 如果在免费试用账户上使用 GuardDuty/Security Hub,请在第 30 天之前执行 `terraform destroy` ## 部署内容 ### Azure 栈 - **隔离的虚拟网络** — 具有最小权限 NSG 规则的三个分段子网 - **Azure Bastion** — 基于浏览器的 SSH 访问,VM 无需公网 IP - **Ubuntu 22.04 LTS 工作负载 VM** — 经过加固的攻击模拟目标 - **Log Analytics Workspace** — 集中式 syslog 收集和 KQL 查询 - **Microsoft Sentinel** — 具备 10 条计划分析规则的云原生 SIEM - **检测规则 (KQL)** — 10 条映射到 MITRE ATT&CK 的规则,涵盖凭据访问、权限提升、持久化、防御规避和执行 - **数据收集规则 + Azure Monitor Agent (DCR + AMA)** — 使用托管身份自动将日志从 VM 转发到 Sentinel - **cloud-init 加固** — 首次启动时安装 auditd + audisp-syslog;内核审计规则映射到 MITRE ATT&CK 技术 - **最小权限 RBAC (Least-privilege RBAC)** — VM 托管身份仅被授予工作区上的 `Monitoring Metrics Publisher` 范围权限 - **Azure Policy 护栏** — 在资源组范围内强制执行 `Allowed locations` 内置策略 ### AWS 栈 - **VPC + Subnets** — 管理(为 SSM 提供公共路由)和工作负载(隔离)子网,配置有 Security Groups 和 NACLs - **SSM Session Manager** — 无需开放入站端口即可通过 shell 访问 EC2(无需 Bastion 主机) - **Ubuntu 22.04 LTS EC2 实例** — 强制执行 IMDSv2、加密 EBS 和 IAM Instance Profile 的加固目标 - **CloudTrail** — 账户级 API 活动日志记录到 CloudWatch Logs + S3 - **CloudWatch Log Groups** — `/soc-lab/syslog`, `/soc-lab/auth`, `/soc-lab/ssm-sessions`, `/soc-lab/cloudtrail` - **CloudWatch Dashboard** — SOC 可视化面板,显示身份验证事件、syslog、SSM 会话和 API 活动 - **GuardDuty** *(可选 — 30 天试用)* — 跨 CloudTrail、VPC Flow Logs 和 DNS 的威胁检测 - **Security Hub** *(可选 — 30 天试用)* — 聚合的发现结果,包含 AWS Foundational Security Best Practices 标准 - **cloud-init 加固** — 与 Azure 相同的 auditd 规则集,CloudWatch Agent 在首次启动时发送日志 ## 架构 ### Azure ``` Internet │ │ HTTPS (443) only ▼ ┌───────────────────────────────────────────────────────┐ │ Azure Virtual Network │ │ 10.0.0.0/16 │ │ │ │ ┌─────────────────────┐ ┌──────────────────────┐ │ │ │ AzureBastionSubnet │ │ management-subnet │ │ │ │ 10.0.3.0/26 │ │ 10.0.1.0/24 │ │ │ │ │ │ │ │ │ │ [Azure Bastion]────┼───┼──► SSH (port 22) │ │ │ └─────────────────────┘ └──────────────────────┘ │ │ │ │ │ ┌────────▼─────────────┐ │ │ │ workload-subnet │ │ │ │ 10.0.2.0/24 │ │ │ │ [Ubuntu 22.04 LTS] │ │ │ └──────────┬───────────┘ │ └────────────────────────────────────────┼──────────────┘ │ Syslog (DCR + AMA) ┌──────────▼────────────────┐ │ Log Analytics Workspace │ │ + Microsoft Sentinel │ └───────────────────────────┘ ``` ### AWS ``` Internet │ │ Outbound 443 only (SSM agent) ▼ ┌───────────────────────────────────────────────────────┐ │ VPC │ │ 10.0.0.0/16 │ │ │ │ ┌──────────────────────────────────────────────────┐ │ │ │ management-subnet 10.0.1.0/24 │ │ │ │ │ │ │ │ [Ubuntu 22.04 LTS EC2] │ │ │ │ - IAM Instance Profile (SSM + CloudWatch) │ │ │ │ - IMDSv2 enforced │ │ │ │ - Encrypted EBS │ │ │ └──────────────────┬───────────────────────────────┘ │ │ │ │ │ ┌──────────────────▼───────────────────────────────┐ │ │ │ workload-subnet 10.0.2.0/24 (isolated) │ │ │ └──────────────────────────────────────────────────┘ │ └─────────────────────┬─────────────────────────────────┘ │ CloudWatch Agent + CloudTrail ┌────────────▼──────────────────────┐ │ CloudWatch Log Groups │ │ /soc-lab/syslog │ │ /soc-lab/auth │ │ /soc-lab/ssm-sessions │ │ /soc-lab/cloudtrail │ └────────────┬──────────────────────┘ │ findings ┌────────────▼──────────────────────┐ │ GuardDuty + Security Hub │ │ (optional — 30-day trial) │ └───────────────────────────────────┘ ``` ## Azure vs AWS — 服务映射 | 概念 | Azure | AWS | |---|---|---| | 网络 | VNet + Subnets + NSGs | VPC + Subnets + Security Groups + NACLs | | 安全 Shell 访问 | Azure Bastion (专用主机) | SSM Session Manager (无主机) | | VM 身份 | 系统分配的 Managed Identity | IAM Instance Profile | | 日志收集 | Azure Monitor Agent + DCR | CloudWatch Agent | | 日志存储 | Log Analytics Workspace | CloudWatch Log Groups | | SIEM / 威胁检测 | Microsoft Sentinel | GuardDuty + Security Hub | | API 活动日志 | Azure Activity Log + Diagnostic Settings | CloudTrail | | 护栏 | Azure Policy | AWS Config / Security Hub standards | | CI/CD 扫描 | tfsec + checkov | tfsec + checkov (相同工具) | ## 检测覆盖范围 通过 Terraform 部署 10 条 Sentinel 计划分析规则,每条规则映射到一个 MITRE ATT&CK 技术。 | # | 规则 | 技术 | 战术 | 严重性 | |---|---|---|---|---| | 1 | SSH 暴力破解 | T1110.001 | Credential Access | Medium | | 2 | 用户被添加到 Sudo 组 | T1548.003 | Privilege Escalation | High | | 3 | 重复的 Sudo 失败尝试 | T1548.003 | Privilege Escalation | Medium | | 4 | 非 Root 用户创建 Cron 任务 | T1053.003 | Persistence | Medium | | 5 | 创建了新的本地用户账户 | T1136.001 | Persistence | High | | 6 | 账户密码已更改 | T1098 | Persistence | Medium | | 7 | 安装了新的 Systemd 服务 | T1543.002 | Persistence | Medium | | 8 | 成功的 Root SSH 登录 | T1078 | Defense Evasion | High | | 9 | Syslog 守护进程停止或重启 | T1070.002 | Defense Evasion | High | | 10 | 反向 Shell 指标 | T1059.004 | Execution | High | ## 前置条件 ### Azure | 工具 | 备注 | |---|---| | Terraform >= 1.5.0 | [安装](https://developer.hashicorp.com/terraform/install) | | Azure CLI | [安装](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) | | Azure Subscription | 需要 Owner 角色(策略分配需要 Owner) | ### AWS | 工具 | 备注 | |---|---| | Terraform >= 1.5.0 | 安装方式同上 | | AWS CLI v2 | [安装](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) | | AWS Account | Free Tier 可覆盖所有实验室资源 12 个月 | | IAM User | 创建具有 `AdministratorAccess` 的非 root IAM 用户 — 切勿使用 root 进行 CLI 操作 | ### Makefile (可选但推荐) ``` # Windows winget install GnuWin32.Make # macOS brew install make # Linux sudo apt install make ``` ## 快速开始 ### Azure ``` # 1. 认证 az login az account set --subscription "" # 2. 生成 SSH key ssh-keygen -t rsa -b 4096 -f ~/.ssh/azure_soc_key # 3. 配置变量 cd environments/azure cp terraform.tfvars.example terraform.tfvars # 编辑 terraform.tfvars — 设置 subscription_id 和 admin_ssh_public_key # 4. 部署 make deploy target=azure # 5. 连接 — Azure Portal → Resource Group → VM → Connect → Bastion # 6. 销毁 make destroy target=azure ``` ### AWS ``` # 1. 认证 aws configure aws sts get-caller-identity # verify # 2. 生成 SSH key (或复用您的 Azure key) ssh-keygen -t rsa -b 4096 -f ~/.ssh/aws_soc_key # 3. 配置变量 cd environments/aws cp terraform.tfvars.example terraform.tfvars # 编辑 terraform.tfvars — 设置 admin_ssh_public_key # 在您的账户完全激活后将 enable_threat_detection 设置为 true # 4. 部署 make deploy target=aws # 5. 通过 SSM Session Manager 连接 (无需开放端口) aws ssm start-session --target --region us-east-1 # 6. 销毁 — 如果启用了 GuardDuty/Security Hub,请在第 30 天之前执行此操作 make destroy target=aws ``` ## 关键输出 ### Azure | 输出 | 描述 | |---|---| | `resource_group_name` | 包含所有实验室资源的资源组 | | `vm_private_ip` | 工作负载 VM 的私有 IP | | `log_analytics_workspace_id` | Log Analytics 工作区的资源 ID | | `sentinel_workspace_id` | 启用 Sentinel 的工作区 | ### AWS | 输出 | 描述 | |---|---| | `instance_id` | EC2 实例 ID | | `ssm_connect_command` | 启动 SSM 会话的确切 CLI 命令 | | `soc_dashboard_url` | CloudWatch SOC 面板的直接链接 | | `cloudtrail_arn` | CloudTrail 跟踪的 ARN | | `guardduty_detector_id` | GuardDuty 检测器 ID (如果威胁检测已禁用则为空) | ## 费用估算 ### Azure | 资源 | 每月大致费用 | |---|---| | Azure Bastion Basic SKU | ~$140 | | Standard_B2s VM (24/7 运行) | ~$30 | | Public IP (Standard) | ~$4 | | Log Analytics (30 天保留) | ~$2–5 | | VNet, NSGs, Managed Identity | 免费 | ### AWS | 资源 | Free Tier | Free Tier 后费用 | |---|---|---| | EC2 t3.micro | 750 小时/月 (12 个月) | ~$8/月 | | GuardDuty | 30 天试用 | 按分析 GB 计费 | | Security Hub | 30 天试用 | 按发现结果计费 | | CloudTrail (管理事件) | 免费 | 免费 | | CloudWatch Logs | 5 GB/月 | ~$0.50/GB | | SSM Session Manager | 始终免费 | 免费 | ## 故障排除 **Azure: VM 创建期间出现 `SkuNotAvailable` 错误** B 系列 VM 在某些区域有容量限制。在 `terraform.tfvars` 中设置 `vm_size = "Standard_D2s_v3"`。如果 `East US` 失败,请尝试 `location = "East US 2"`。 **Azure: Sentinel 重新部署时出现 409 Conflict** Azure 会软删除 Sentinel 警报规则 ID,并在重用前设有冷却期。在重新部署前等待 15 分钟,或更改您的 `prefix` 以生成新的资源名称。 **AWS: GuardDuty 或 Security Hub 出现 `SubscriptionRequiredException`** 您的账户尚未完全激活。新账户的信用卡验证可能需要长达 24 小时。请使用 `enable_threat_detection = false`(默认值)进行部署,激活后再设置为 `true`。 **AWS: 运行 SSM start-session 时出现 `TargetNotConnected`** SSM 代理通过 cloud-init 在首次启动时安装 — 在 `terraform apply` 完成后等待 3–4 分钟再连接。首先检查 EC2 → Instance → Status Checks 显示 2/2 通过。 **常规: `terraform apply` 中途失败** 重新运行 `terraform apply` — Terraform 会跳过已存在的资源,仅重试失败的部分。大多数部署中途失败是暂时性的 API 时序问题,重试即可解决。 ## 项目状态 | 阶段 | 描述 | 状态 | |---|---|---| | Phase 1 | Azure 核心基础设施 — VNet, Bastion, VM, Sentinel, AMA | 完成 | | Phase 2 | 检测工程 — KQL 规则,攻击模拟,证据收集 | 完成 | | Phase 3 | CI/CD 流水线 — GitHub Actions, tfsec, checkov | 完成 | | Phase 4 | 多云扩展 — AWS 等效栈,Makefile 流水线 | 完成 | ## 仓库结构 ``` multi-cloud-soc-terraform/ environments/ azure/ # Azure root module — providers, variables, main, outputs aws/ # AWS root module — providers, variables, main, outputs modules/ azure/ # Azure child modules resource_group/ network/ compute/ bastion/ monitoring/ aws/ # AWS child modules network/ compute/ bastion/ # SSM Session Manager config monitoring/ # GuardDuty, Security Hub, CloudTrail, CloudWatch Makefile # make deploy target=azure|aws .github/ workflows/ terraform-ci.yml # Validates + scans both clouds on every PR ``` ## 许可证 MIT — 详见 [LICENSE](LICENSE)。欢迎 Fork 并根据您自己的实验室环境进行调整。 ## 参考资料 **Azure** - [Microsoft Sentinel 文档](https://learn.microsoft.com/en-us/azure/sentinel/) - [Azure Bastion NSG 要求](https://learn.microsoft.com/en-us/azure/bastion/bastion-nsg) - [Azure Monitor Agent 概览](https://learn.microsoft.com/en-us/azure/azure-monitor/agents/azure-monitor-agent-overview) - [Terraform AzureRM Provider](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs) **AWS** - [SSM Session Manager 设置](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started.html) - [GuardDuty 文档](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) - [Security Hub 文档](https://docs.aws.amazon.com/securityhub/latest/userguide/what-is-securityhub.html) - [Terraform AWS Provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) **通用** - [MITRE ATT&CK 框架](https://attack.mitre.org/) - [Atomic Red Team](https://github.com/redcanaryco/atomic-red-team) - [tfsec — Terraform 安全扫描器](https://github.com/aquasecurity/tfsec) - [checkov — IaC 安全扫描器](https://github.com/bridgecrewio/checkov)
标签:AMSI绕过, AWS, Azure, Azure Bastion, CloudTrail, DevSecOps, DNS解析, DPI, EC2, ECS, GuardDuty, IaC, OPA, SOC 实验室, Terraform, 上游代理, 域名收集, 威胁检测, 安全架构, 安全运营中心, 开源项目, 攻击模拟, 特权提升, 私有化部署, 系统硬化, 网络安全, 网络映射, 自动化部署, 配置审计, 防御规避, 隐私保护, 靶场, 驱动签名利用