mrezwanulbari/aws-security
GitHub: mrezwanulbari/aws-security
一个全面的 AWS 云安全框架,提供 IAM 策略、威胁检测、合规自动化及事件响应的架构参考与基础设施即代码实现。
Stars: 0 | Forks: 0
# AWS 安全 — 云安全架构与自动化




## 📋 目录
- [概述](#overview)
- [架构](#architecture)
- [仓库结构](#repository-structure)
- [IAM 安全](#iam-security)
- [GuardDuty 与威胁检测](#guardduty--threat-detection)
- [CloudTrail 与日志记录](#cloudtrail--logging)
- [网络安全](#network-security)
- [Sigma 检测规则](#sigma-detection-rules)
- [合规自动化](#compliance-automation)
- [基础设施即代码](#infrastructure-as-code)
- [事件响应](#incident-response)
- [贡献指南](#contributing)
## 概述
本仓库提供了一个全面的 AWS 安全框架,涵盖身份管理、威胁检测、日志记录、网络隔离、合规自动化以及企业和政府云环境的事件响应。
### 涵盖的安全领域
| 领域 | AWS 服务 | 重点关注 |
|---|---|---|
| **身份与访问** | IAM, Organizations, SSO, STS | 最小权限、基于角色的访问、联邦身份验证 |
| **检测** | GuardDuty, Security Hub, Detective | 威胁检测、异常分析 |
| **日志与监控** | CloudTrail, CloudWatch, VPC Flow Logs | 集中式日志记录、告警 |
| **网络安全** | VPC, Security Groups, NACLs, WAF | 网络隔离、DDoS 防护 |
| **数据保护** | KMS, Secrets Manager, Macie | 加密、密钥管理、数据分类 |
| **合规性** | Config, Audit Manager, Security Hub | CIS, NIST 800-53, SOC 2, PCI-DSS |
| **事件响应** | Lambda, Step Functions, EventBridge | 自动化响应剧本 |
## 架构
### AWS 安全参考架构
```
┌─────────────────────────────────────────────────────────────────┐
│ AWS Organizations │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Management Account │ │
│ │ ┌─────────┐ ┌──────────┐ ┌────────────┐ ┌───────────┐ │ │
│ │ │ IAM │ │CloudTrail│ │ AWS Config│ │ Org Policies│ │ │
│ │ │ Identity│ │ (Org) │ │ (Org) │ │ SCPs │ │ │
│ │ │ Center │ │ │ │ │ │ │ │ │
│ │ └─────────┘ └──────────┘ └────────────┘ └───────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────┐ ┌─────────────────────────────────┐ │
│ │ Security Account │ │ Log Archive Account │ │
│ │ ┌─────────────────┐ │ │ ┌─────────────┐ ┌────────────┐ │ │
│ │ │ GuardDuty │ │ │ │ CloudTrail │ │ VPC Flow │ │ │
│ │ │ (Delegated) │ │ │ │ S3 Bucket │ │ Logs Bucket│ │ │
│ │ │ Security Hub │ │ │ │ │ │ │ │ │
│ │ │ Detective │ │ │ │ Config │ │ GuardDuty │ │ │
│ │ │ Macie │ │ │ │ Snapshots │ │ Findings │ │ │
│ │ └─────────────────┘ │ │ └─────────────┘ └────────────┘ │ │
│ └─────────────────────┘ └─────────────────────────────────┘ │
│ │
│ ┌─────────────────────┐ ┌─────────────────────────────────┐ │
│ │ Production Account │ │ Development Account │ │
│ │ ┌──────┐ ┌──────┐ │ │ ┌──────┐ ┌──────┐ │ │
│ │ │ VPC │ │ WAF │ │ │ │ VPC │ │ IAM │ │ │
│ │ │ EC2 │ │ KMS │ │ │ │ EC2 │ │ Roles│ │ │
│ │ │ RDS │ │ S3 │ │ │ │ S3 │ │ │ │ │
│ │ └──────┘ └──────┘ │ │ └──────┘ └──────┘ │ │
│ └─────────────────────┘ └─────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
## 仓库结构
```
aws-security/
├── README.md
├── iam/
│ ├── policies/
│ │ ├── least-privilege-admin.json
│ │ ├── security-audit-readonly.json
│ │ ├── s3-restricted-access.json
│ │ ├── deny-root-account.json
│ │ └── enforce-mfa.json
│ ├── roles/
│ │ ├── incident-response-role.json
│ │ ├── security-audit-role.json
│ │ └── cross-account-role.json
│ ├── scp/
│ │ ├── deny-regions.json
│ │ ├── deny-leave-org.json
│ │ ├── require-encryption.json
│ │ └── deny-public-s3.json
│ └── docs/
│ └── iam-best-practices.md
├── guardduty/
│ ├── threat-intel-lists/
│ │ └── custom-threat-ips.txt
│ ├── suppression-rules/
│ │ └── guardduty-suppressions.json
│ ├── notification/
│ │ └── guardduty-eventbridge-rule.json
│ └── docs/
│ └── guardduty-operations.md
├── cloudtrail/
│ ├── configurations/
│ │ └── org-trail-config.json
│ ├── athena-queries/
│ │ ├── unauthorized-api-calls.sql
│ │ ├── iam-changes.sql
│ │ ├── console-logins.sql
│ │ ├── s3-data-events.sql
│ │ └── security-group-changes.sql
│ └── docs/
│ └── cloudtrail-analysis.md
├── network/
│ ├── vpc/
│ │ └── secure-vpc-template.yaml
│ ├── security-groups/
│ │ └── baseline-security-groups.yaml
│ ├── nacls/
│ │ └── network-acl-rules.yaml
│ └── waf/
│ └── waf-rules.json
├── sigma-rules/
│ ├── cloudtrail/
│ │ ├── aws-root-account-usage.yml
│ │ ├── aws-iam-user-creation.yml
│ │ ├── aws-s3-public-access.yml
│ │ ├── aws-security-group-modification.yml
│ │ ├── aws-cloudtrail-disabled.yml
│ │ ├── aws-unauthorized-api-call.yml
│ │ ├── aws-console-login-without-mfa.yml
│ │ └── aws-ec2-keypair-created.yml
│ └── guardduty/
│ ├── aws-guardduty-high-severity.yml
│ └── aws-guardduty-crypto-mining.yml
├── compliance/
│ ├── cis-benchmark/
│ │ ├── cis-aws-checks.sh
│ │ └── cis-remediation-guide.md
│ ├── nist-800-53/
│ │ └── nist-control-mapping.md
│ └── config-rules/
│ └── custom-config-rules.yaml
├── terraform/
│ ├── modules/
│ │ ├── guardduty/
│ │ │ └── main.tf
│ │ ├── cloudtrail/
│ │ │ └── main.tf
│ │ ├── security-hub/
│ │ │ └── main.tf
│ │ └── vpc-security/
│ │ └── main.tf
│ └── environments/
│ ├── production/
│ │ └── main.tf
│ └── development/
│ └── main.tf
├── incident-response/
│ ├── playbooks/
│ │ ├── compromised-iam-credentials.md
│ │ ├── compromised-ec2-instance.md
│ │ ├── s3-data-breach.md
│ │ └── ransomware-response.md
│ └── lambda/
│ ├── auto-remediate-sg.py
│ ├── auto-disable-iam-key.py
│ └── quarantine-ec2-instance.py
└── docs/
├── security-architecture.md
├── logging-strategy.md
└── shared-responsibility.md
```
## IAM 安全
### 服务控制策略
SCPs 是组织级别的防护机制,用于限制成员账户可以执行的操作:
```
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyRootAccountUsage",
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"StringLike": {
"aws:PrincipalArn": "arn:aws:iam::*:root"
}
}
},
{
"Sid": "DenyLeavingOrganization",
"Effect": "Deny",
"Action": "organizations:LeaveOrganization",
"Resource": "*"
},
{
"Sid": "RequireIMDSv2",
"Effect": "Deny",
"Action": "ec2:RunInstances",
"Resource": "arn:aws:ec2:*:*:instance/*",
"Condition": {
"StringNotEquals": {
"ec2:MetadataHttpTokens": "required"
}
}
}
]
}
```
### IAM 最佳实践清单
| # | 实践 | 优先级 |
|---|---|---|
| 1 | 为所有 IAM 用户启用 MFA(root 账户使用硬件 MFA) | 关键 |
| 2 | 绝不使用 root 账户进行日常操作 | 关键 |
| 3 | 使用 IAM Access Analyzer 实施最小权限原则 | 高 |
| 4 | 每 90 天轮换一次访问密钥 | 高 |
| 5 | 使用 IAM 角色代替长期凭证 | 高 |
| 6 | 启用 AWS SSO 进行集中式访问管理 | 高 |
| 7 | 为委派管理员使用权限边界 | 中 |
| 8 | 使用 CloudTrail 进行监控并设置告警 | 关键 |
| 9 | 每季度审查 IAM 策略 | 中 |
| 10 | 为所有 IAM 资源打标签以便于治理 | 中 |
## GuardDuty 与威胁检测
### GuardDuty 发现类型(主要威胁)
| 发现 | 严重性 | 描述 | 响应 |
|---|---|---|---|
| UnauthorizedAccess:IAMUser/MaliciousIPCaller | 高 | 来自已知恶意 IP 的 API 调用 | 调查来源,轮换凭证 |
| Recon:EC2/PortProbeUnprotectedPort | 中 | 对未受保护的 EC2 端口进行端口扫描 | 审查安全组,封禁来源 |
| CryptoCurrency:EC2/BitcoinTool.B!DNS | 高 | EC2 查询加密货币挖矿域名 | 隔离实例,调查是否被入侵 |
| Trojan:EC2/BlackholeTraffic | 高 | EC2 向黑洞 IP 发送流量 | 立即隔离实例 |
| UnauthorizedAccess:IAMUser/ConsoleLoginSuccess.B | 中 | 来自异常位置的控制台登录 | 向用户核实,审查 CloudTrail |
| Exfiltration:S3/AnomalousBehavior | 高 | 异常的 S3 数据访问模式 | 审查 S3 访问日志,检查是否存在数据泄露 |
| Impact:EC2/WinRMBruteForce | 高 | WinRM 暴力破解尝试 | 封禁源 IP,修补/加固实例 |
## Sigma 检测规则
### 示例:Root 账户使用检测
```
title: AWS Root Account Usage
id: aws-root-001
status: stable
level: critical
description: Detects any usage of the AWS root account, which should be reserved for emergency access only.
author: Shakil Md. Rezwanul Bari
date: 2024/01/15
references:
- https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html
- https://d1.awsstatic.com/whitepapers/compliance/AWS_CIS_Foundations_Benchmark.pdf
logsource:
product: aws
service: cloudtrail
detection:
selection:
userIdentity.type: Root
userIdentity.invokedBy: ''
filter_service:
eventType: AwsServiceEvent
condition: selection and not filter_service
falsepositives:
- Emergency break-glass access (should be rare and documented)
- Initial account setup
tags:
- attack.initial_access
- attack.privilege_escalation
- attack.t1078.004
```
### 示例:CloudTrail 被禁用
```
title: AWS CloudTrail Logging Disabled
id: aws-ct-001
status: stable
level: critical
description: Detects when CloudTrail logging is stopped or deleted, which may indicate an attacker covering their tracks.
author: Shakil Md. Rezwanul Bari
date: 2024/01/15
logsource:
product: aws
service: cloudtrail
detection:
selection:
eventName:
- StopLogging
- DeleteTrail
- UpdateTrail
condition: selection
falsepositives:
- Authorized trail maintenance (rare)
tags:
- attack.defense_evasion
- attack.t1562.008
```
## 事件响应
### 被入侵的 IAM 凭证处理剧本
```
┌─────────────────────────────────────────────┐
│ PHASE 1: DETECTION & TRIAGE │
│ 1. Alert from GuardDuty/CloudTrail │
│ 2. Identify affected IAM principal │
│ 3. Assess scope of unauthorized activity │
└───────────────────┬─────────────────────────┘
│
┌───────────────────▼─────────────────────────┐
│ PHASE 2: CONTAINMENT │
│ 1. Disable compromised access keys │
│ 2. Revoke active sessions (inline deny) │
│ 3. Attach quarantine policy to user/role │
│ 4. Preserve CloudTrail logs │
└───────────────────┬─────────────────────────┘
│
┌───────────────────▼─────────────────────────┐
│ PHASE 3: INVESTIGATION │
│ 1. Query CloudTrail for all actions taken │
│ 2. Identify resource modifications │
│ 3. Check for persistence mechanisms │
│ 4. Review IAM changes and new credentials │
└───────────────────┬─────────────────────────┘
│
┌───────────────────▼─────────────────────────┐
│ PHASE 4: REMEDIATION │
│ 1. Rotate all credentials │
│ 2. Remove unauthorized resources │
│ 3. Patch access vectors │
│ 4. Update IAM policies │
└───────────────────┬─────────────────────────┘
│
┌───────────────────▼─────────────────────────┐
│ PHASE 5: LESSONS LEARNED │
│ 1. Document timeline and root cause │
│ 2. Update detection rules │
│ 3. Implement preventive controls │
│ 4. Conduct post-incident review │
└─────────────────────────────────────────────┘
```
## 合规自动化
### CIS AWS 基础基准映射
| CIS 控制项 | AWS 服务 | 自动修复 |
|---|---|---|
| 1.1 - 避免使用 root 账户 | CloudTrail + EventBridge | 告警 + 通知 |
| 1.4 - 轮换访问密钥 ≤ 90 天 | IAM Access Analyzer | Lambda 自动禁用 |
| 1.10 - 为所有 IAM 用户启用 MFA | Config Rule | 告警 + 通知 |
| 2.1 - 在所有区域启用 CloudTrail | CloudTrail (组织追踪) | 预防性 (SCP) |
| 2.6 - 启用 S3 存储桶访问日志记录 | Config Rule | Lambda 自动启用 |
| 2.9 - 启用 VPC Flow Logs | Config Rule | Lambda 自动启用 |
| 3.1-3.14 - CloudWatch 指标警报 | CloudWatch + SNS | 自动告警 |
| 4.1 - 没有安全组允许 0.0.0.0/0 的入站流量 | Config Rule | Lambda 自动修复 |
## 许可证
本项目基于 MIT 许可证授权。
标签:AMSI绕过, AWS, DPI, ECS, Terraform, 合规自动化, 基础架构代码, 威胁检测, 安全防护, 漏洞利用检测