chinnam-arjun/cloud-threat-detection

GitHub: chinnam-arjun/cloud-threat-detection

基于 AWS 免费套餐构建的事件驱动型云威胁检测与自动化事件响应系统,无需 GuardDuty 等付费服务即可检测 15+ 种威胁模式并自动遏制。

Stars: 0 | Forks: 0

# 云威胁检测与自动化事件响应 一个基于 AWS 构建的事件驱动型云安全监控系统,可检测跨越 IAM、CloudTrail、S3 和 EC2 的 **15+ 种威胁模式** —— 包含自动化事件响应、按严重程度分级的告警、DynamoDB 审计日志记录以及实时的 CloudWatch 仪表板。 完全在 **AWS 免费套餐**内构建,无需使用 GuardDuty、Security Hub 或 Athena。 ## 为什么不使用 GuardDuty? 教科书般的 AWS 安全架构如下所示: ``` CloudTrail → GuardDuty → Security Hub → EventBridge → Lambda → SNS ``` 干净。强大。企业级。但同时也要花费 **$50–200+/月**。 作为一个在个人 AWS 账户上进行构建的实习生,这并不是一个可行的选项。因此,每个托管服务都被替换为免费套餐的等价物: | 组件 | 生产环境标准 | 本项目 | 替换原因 | |--------------|----------------------|--------------------------|-----------------------| | 检测 | GuardDuty (ML) | Lambda THREAT_RULES | $50–200/月 | | 日志分析 | Athena (SQL on S3) | CloudWatch Logs Insights | 按查询付费 | | 仪表板 | Security Hub | CloudWatch Dashboard | 额外成本 | | 遏制 | 需要额外设置 | 内置 ✅ | — | | 成本 | $50–200+/月 | **$0** ✅ | — | ## 架构 ``` AWS Account Activity (IAM, S3, EC2, Console logins, Secrets...) │ ▼ ┌───────────────────────┐ ┌──────────────────────────────┐ │ CloudTrail │──────▶│ S3 Bucket │ │ │ │ (log archive, SSE-S3 enc) │ │ Management events │ └──────────────────────────────┘ │ Multi-region: ON │ │ Global svc events: ON│ └───────────┬───────────┘ │ streams to ▼ ┌───────────────────────┐ │ CloudWatch Logs │◀── Lambda execution logs │ │ │ CloudTrail-Logs │ │ /aws/lambda/ │ │ threat-detection- │ │ engine │ └───────────┬───────────┘ │ ▼ ┌───────────────────────┐ │ EventBridge │ │ (ap-south-2) │ │ │ │ Single broad rule │ │ catches ALL events │ │ including IAM │ │ (works because trail │ │ is multi-region + │ │ global svc events) │ └───────────┬───────────┘ │ triggers ▼ ┌───────────────────────────────────────┐ │ Lambda — Detection Brain │ │ threat-detection-engine │ │ │ │ ┌─────────────────────────────────┐ │ │ │ THREAT_RULES engine │ │ │ │ │ │ │ │ parse CloudTrail event │ │ │ │ match against 15 threat rules │ │ │ │ optional condition check │ │ │ │ score severity │ │ │ │ check recon counter (DynamoDB) │ │ │ └─────────────────────────────────┘ │ │ │ │ ┌─────────────────────────────────┐ │ │ │ Remediation Engine │ │ │ │ │ │ │ │ start_logging() │ │ │ │ detach_user_policy() │ │ │ │ update_access_key(Inactive) │ │ │ └─────────────────────────────────┘ │ └──────┬──────────────────┬─────────────┘ │ │ ▼ ▼ ┌─────────────┐ ┌───────────────────────┐ │ SNS Topics │ │ DynamoDB │ │ │ │ │ │ critical │ │ incident-log │ │ high │ │ recon-counter │ │ medium │ │ │ └──────┬──────┘ └───────────────────────┘ │ ▼ ┌──────────────────────────────────────────┐ │ CloudWatch Dashboard │ │ │ │ Threat Detections │ Errors │ Duration │ │ ─────────────────────────────────────── │ │ Alerts by Severity (bar) │ │ ─────────────────────────────────────── │ │ Recent Security Events │ Threats │ │ ─────────────────────────────────────── │ │ AccessDenied Recon Activity (line) │ └──────────────────────────────────────────┘ │ ▼ ┌─────────────────┐ │ Your Email │ │ │ │ 🚨 [CRITICAL] │ │ 🟠 [HIGH] │ │ 🟡 [MEDIUM] │ └─────────────────┘ Cost: $0 (fully within AWS free tier) ``` ## 使用的服务 | 服务 | 用途 | 免费套餐 | |---------------|----------------------------------------|--------------------| | CloudTrail | 捕获所有 API 调用 | 1 个追踪器免费 | | CloudWatch | 日志流 + 仪表板 | 5GB 日志免费 | | EventBridge | 实时事件路由 | 当前规模下免费 | | Lambda | 检测 + 遏制引擎 | 100 万次请求/月 | | SNS | 按严重程度分级的邮件告警 | 1000 封邮件/月 | | DynamoDB | 事件日志 + 探测计数器 | 25GB 免费 | | IAM | 最小权限 Lambda 执行角色 | 永远免费 | ## 威胁覆盖范围 ### 🔴 严重 | 威胁 | CloudTrail 事件 | 自动遏制 | |--------|-----------------|------------------| | Root 账户登录 | ConsoleLogin | 仅告警 | | CloudTrail 日志记录已禁用 | StopLogging | ✅ 立即重新启用 | | CloudTrail 追踪器已删除 | DeleteTrail | 仅告警 | | AdministratorAccess 附加给用户 | AttachUserPolicy | ✅ 分离策略 | ### 🟠 高危 | 威胁 | CloudTrail 事件 | 自动遏制 | |--------|-----------------|------------------| | 未使用 MFA 的控制台登录 | ConsoleLogin | 仅告警 | | 创建了新的 IAM 用户 | CreateUser | 仅告警 | | 创建了新的访问密钥 | CreateAccessKey | ✅ 停用密钥 | | S3 存储桶 ACL 已更改 | PutBucketAcl | 仅告警 | | S3 存储桶策略已更改 | PutBucketPolicy | 仅告警 | ### 🟡 中危 | 威胁 | CloudTrail 事件 | 自动遏制 | |--------|-----------------|------------------| | 访问了 Secrets Manager 密钥 | GetSecretValue | 仅告警 | | 安全组向 0.0.0.0/0 开放 | AuthorizeSecurityGroupIngress | 仅告警 | | CloudTrail 配置已修改 | UpdateTrail | 仅告警 | | 向 IAM 角色添加了内联策略 | PutRolePolicy | 仅告警 | | IAM 角色信任策略已修改 | UpdateAssumeRolePolicy | 仅告警 | | IAM 访问密钥已删除 | DeleteAccessKey | 仅告警 | | 权限探测 (5 分钟内出现 5 次以上 AccessDenied) | AccessDenied 模式 | 仅告警 | ## 告警格式 每封邮件告警都包含一个唯一的事件 ID,用于在 DynamoDB 中进行交叉引用: ``` 🚨 [CRITICAL] CloudTrail Logging Disabled ======================================================= INCIDENT DETAILS Incident ID : A3F9B2C1 Account : 084149021663 Region : ap-south-2 Time : 2026-07-03T14:30:00Z Actor : Arjun Event : StopLogging Source IP : x.x.x.x WHAT HAPPENED An actor attempted to stop CloudTrail logging. This is a common attacker tactic to cover tracks before performing malicious actions. ACTION TAKEN ✅ AUTO-REMEDIATED: CloudTrail logging re-enabled for trail 'cloud-threat-detection-trail' ======================================================= AWS Cloud Threat Detection System ``` ## DynamoDB 结构 ### incident-log 表 ``` incident_id (PK) → 8-char UUID e.g. "A3F9B2C1" timestamp (SK) → ISO 8601 UTC severity → CRITICAL / HIGH / MEDIUM title → human readable threat name event_name → CloudTrail eventName actor → IAM username or role source_ip → originating IP address region → AWS region of the event action_taken → remediation result or alert only status → OPEN ``` ### recon-counter 表 ``` actor (PK) → IAM username or role ARN count → AccessDenied hits in current window first_seen → window start timestamp last_seen → most recent hit timestamp ``` 阈值:**5 分钟内出现 5 次 AccessDenied 错误** → 触发中危告警,计数器重置。 ## IAM — 最小权限策略 Lambda 角色仅被授予用于遏制的确切所需权限: ``` { "Statements": [ "sns:Publish" → 3 specific topic ARNs only, "cloudtrail:StartLogging" → re-enable if stopped, "cloudtrail:GetTrailStatus" → verify trail state, "iam:DetachUserPolicy" → remove admin policy, "iam:UpdateAccessKey" → deactivate rogue keys, "dynamodb:PutItem/GetItem/UpdateItem" → specific table ARNs only, "logs:PutLogEvents" → Lambda log group only ] } ``` 完整策略 → `/iam/lambda-execution-policy.json` ## 关键工程决策 **1. 单区域 EventBridge 捕获 IAM 事件** IAM 是一项全局的 AWS 服务,其事件通常仅流经 us-east-1 的 EventBridge。然而,在将 CloudTrail 配置为 启用了全局服务事件的多区域追踪器后,IAM 事件将被 传送到本地区域的 CloudWatch Logs,并直接由 ap-south-2 的 EventBridge 规则获取 —— 无需跨区域设置。 **2. 单个宽泛的 EventBridge 规则 → Lambda 进行分类** 与其使用许多狭窄的 EventBridge 规则,不如用一个宽泛的规则将所有 事件输送给 Lambda。THREAT_RULES 字典负责处理分类、 严重性评分和条件匹配。更易于扩展,更易于 调试。 **3. 按严重程度划分的三个独立 SNS 主题** 每个严重级别可独立管理订阅 —— 在夜间屏蔽中危告警 而不影响严重告警。每个主题映射到一个不同的 邮件订阅。 **4. 通过 DynamoDB 滑动窗口进行探测检测** Lambda 是无状态的 —— 它无法跟踪跨调用的模式。 recon-counter DynamoDB 表维护每个执行者的 AccessDenied 计数并带有 5 分钟的滑动窗口,从而在没有 GuardDuty 的 ML 模型的情况下实现基于模式的检测。 **5. 事件 ID 将邮件告警与 DynamoDB 记录关联** 每个检测到的威胁都会生成一个 8 字符的短 UUID。这会显示 在邮件主题行中,并作为 DynamoDB 主键存储 —— 从而允许在收件箱告警和审计日志之间进行交叉引用。 ## 仓库结构 ``` cloud-threat-detection/ │ ├── README.md │ ├── lambda/ │ └── lambda_function.py ← full detection engine │ ├── eventbridge/ │ └── ap-south-2-rule.json ← event pattern (all services) │ ├── iam/ │ └── lambda-execution-policy.json ← least privilege policy │ ├── dynamodb/ │ ├── incident-log-schema.json ← table definition │ └── recon-counter-schema.json ← table definition │ ├── cloudwatch/ │ └── dashboard-body.json ← full dashboard JSON │ ├── docs/ │ └── threat-catalog.md ← all 15 threats documented │ └── tests/ └── test-events/ ├── create-user.json ├── stop-logging.json ├── attach-admin-policy.json ├── console-login-root.json └── create-access-key.json ``` ## 设置指南 ### 前置条件 - AWS 账户(免费套餐) - 配置了 IAM 用户凭证的 AWS CLI - Python 3.12+ ### 部署顺序 ``` 1. CloudTrail trail → Management events: All → Multi-region: ON → Global service events: ON (critical for IAM event capture) 2. CloudWatch Log Group linked to CloudTrail trail 3. S3 bucket for CloudTrail log archive (SSE-S3 encryption) 4. SNS topics × 3 → critical-alerts → high-alerts → medium-alerts → subscribe email to each, confirm all 3 5. DynamoDB tables × 2 (on-demand billing) → incident-log (PK: incident_id, SK: timestamp) → recon-counter (PK: actor) 6. Lambda function → Runtime: Python 3.12 → Paste lambda_function.py → Update SNS ARNs at top of file → Timeout: 30 seconds 7. IAM policy attached to Lambda execution role → Use lambda-execution-policy.json 8. EventBridge rule in ap-south-2 → Single broad rule → Lambda target 9. CloudWatch dashboard → Use dashboard-body.json via put-dashboard CLI command ``` ### 配置 更新 `lambda/lambda_function.py` 中的这 3 行: ``` SNS_CRITICAL = "arn:aws:sns:YOUR_REGION:YOUR_ACCOUNT_ID:critical-alerts" SNS_HIGH = "arn:aws:sns:YOUR_REGION:YOUR_ACCOUNT_ID:high-alerts" SNS_MEDIUM = "arn:aws:sns:YOUR_REGION:YOUR_ACCOUNT_ID:medium-alerts" ``` 同时更新 boto3 客户端区域: ``` sns = boto3.client('sns', region_name='YOUR_REGION') iam = boto3.client('iam', region_name='YOUR_REGION') ct = boto3.client('cloudtrail', region_name='YOUR_REGION') ec2 = boto3.client('ec2', region_name='YOUR_REGION') s3 = boto3.client('s3', region_name='YOUR_REGION') ddb = boto3.resource('dynamodb', region_name='YOUR_REGION') ``` ## 测试 运行这些 CLI 命令以进行端到端的完整流水线验证: ``` # HIGH — 新建 IAM 用户(等待 2-3 分钟接收邮件) aws iam create-user --user-name threat-test --region ap-south-2 # HIGH — 创建 access key(自动停用) aws iam create-access-key --user-name threat-test --region ap-south-2 # 验证密钥已被停用 aws iam list-access-keys --user-name threat-test --region ap-south-2 # CRITICAL — 禁用 CloudTrail(自动重新启用) aws cloudtrail stop-logging \ --name YOUR_TRAIL_NAME --region ap-south-2 # 验证 trail 已重新启用 aws cloudtrail get-trail-status \ --name YOUR_TRAIL_NAME --region ap-south-2 # 清理 aws iam delete-access-key \ --user-name threat-test \ --access-key-id KEY_ID_HERE \ --region ap-south-2 aws iam delete-user --user-name threat-test --region ap-south-2 ``` 每次测试后检查 DynamoDB: ``` DynamoDB → Tables → incident-log → Explore table items ``` ## 构建工具 ![AWS](https://img.shields.io/badge/AWS-CloudTrail-orange) ![AWS](https://img.shields.io/badge/AWS-EventBridge-orange) ![AWS](https://img.shields.io/badge/AWS-Lambda-orange) ![AWS](https://img.shields.io/badge/AWS-SNS-orange) ![AWS](https://img.shields.io/badge/AWS-DynamoDB-orange) ![AWS](https://img.shields.io/badge/AWS-CloudWatch-orange) ![Python](https://img.shields.io/badge/Python-3.12-blue) **区域:** ap-south-2 (印度,海得拉巴) **成本:** $0 — 完全在 AWS 免费套餐内 **构建背景:** APSSDC AWS 云实习
标签:AMSI绕过, AWS, DPI, Serverless, 威胁检测, 逆向工具