ctkqiang/AWSGateKeeper

GitHub: ctkqiang/AWSGateKeeper

AWS 安全审计网关,将威胁检测、漏洞扫描与 IAM 治理统一为自动化事件响应流水线。

Stars: 1 | Forks: 0

# AWSGateKeeper ![](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/ae7bb49c37171152.svg) [![Go 版本](https://img.shields.io/badge/Go-1.26+-00ADD8?style=flat&logo=go)](https://go.dev/) [![AWS Lambda](https://img.shields.io/badge/AWS-Lambda-FF9900?style=flat&logo=aws-lambda)](https://aws.amazon.com/lambda/) [![许可证](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![安全](https://img.shields.io/badge/AWS-GuardDuty%20%7C%20Inspector%20%7C%20Detective-7B2FF7?style=flat)](https://aws.amazon.com/security/) [![GuardDuty 功能](https://img.shields.io/badge/GuardDuty-15+%20APIs%20Integrated-FF9900?style=flat)](https://docs.aws.amazon.com/guardduty/) [![架构](https://img.shields.io/badge/Architecture-PlantUML-2255CC?style=flat)](docs/ARCHITECTURE.puml) [![发布](https://img.shields.io/github/v/release/ctkqiang/AWSGateKeeper?style=flat)](https://gitcode.com/ctkqiang_sr/AWSGateKeeper) [![问题](https://img.shields.io/badge/Issues-Welcome-brightgreen?style=flat)](https://gitcode.com/ctkqiang_sr/AWSGateKeeper/issues) AWS 安全审计网关 —— **GuardDuty 威胁响应**、**Inspector CVE 扫描**、**Detective 根因分析**、**自动化 IAM 角色治理**、**实时通配符策略检测**、**带有动态 IAM 隔离的事件驱动响应**,以及 **SIEM 转发**。可作为 AWS Lambda 函数或端口 8000 上的独立 HTTP 服务器部署。 **作者:** [钟智强 (ctkqiang)](https://github.com/ctkqiang) | **代码库:** [gitcode.com/ctkqiang_sr/AWSGateKeeper](https://gitcode.com/ctkqiang_sr/AWSGateKeeper) [English](#english) | [中文](README_ZH.md) | [文档](docs/index_en.md) | [API 参考](docs/api_en.md) | [报告 Bug](https://gitcode.com/ctkqiang_sr/AWSGateKeeper/issues/new?template=bug_report.md) | [安全问题](https://gitcode.com/ctkqiang_sr/AWSGateKeeper/issues/new?template=security_concern.md) ## 架构图 ### 系统架构 ![](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/8dc45cc2d3171158.png) ### 企业架构 (ArchiMate) ![](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/99f21588e7171203.png) ### 安全扫描时序 ![](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/6a6573b1b9171208.png) ### 冷启动 + 扫描时序 ![](https://raw.githubusercontent.com/ctkqiang/AWSGateKeeper/main/out/docs/TIMING_en/TIMING_en.png) ## 为什么会有 AWSGateKeeper ### 问题所在 AWS 安全**分散在 6 个以上的服务中**(GuardDuty、Inspector、Detective、IAM、CloudTrail、Cognito),没有统一的响应流水线。安全团队面临: - **告警疲劳:** GuardDuty 产生数千条发现;由于手动分类开销,95% 的发现从未被处理。 - **事件响应缓慢:** 从检测到凭证被盗用到停用 IAM 密钥的平均时间为 **4 小时以上** —— 攻击者会利用这个窗口期。 - **无声的策略漂移:** IAM 策略随着时间推移积累通配符权限(`"Resource": "*"`, `"Action": "*"`),却没有自动检测或回滚机制。 - **容器盲目性:** Inspector CVE 发现停留在仪表板中,而易受攻击的镜像仍在生产环境中运行。 - **无跨服务关联:** GuardDuty 标记出被盗用的凭证;Inspector 标记出同一资源的 CVE —— 但没有系统将两者联系起来。 ### 解决方案 AWSGateKeeper 是一个**生产级、开源的 AWS 安全自动化平台**,它能够: 1. **统一** GuardDuty、Inspector、Detective、IAM 和 CloudTrail 到单一的事件驱动流水线 2. **自动化** 整个事件响应生命周期:检测 → 审计 → 关联 → 隔离 → 报告 3. **隔离** 带有显式 Deny-* 策略的被盗用身份 —— 非破坏性、取证合理且可立即撤销 4. **扫描** 每一个实时发生的 IAM 策略变更,对照组织安全基线 5. **交付** 可操作的 Markdown 报告到 Slack、DingTalk、飞书、Teams、Splunk 或 Datadog 6. **运行** 在 AWS Lambda(无服务器、零维护)或本地作为独立 HTTP 服务器 ### 适用对象 | 角色 | 用例 | | ---------------------------- | ----------------------------------------------------------------------- | | **云安全工程师** | 自动化 SOC 2、PCI-DSS、HIPAA IAM 合规审计 | | **DevSecOps 团队** | 将安全扫描集成到 CI/CD 流水线中 | | **AWS 管理员** | 在多账户组织中大规模实施最小权限 IAM | | **事件响应人员** | 在发生实际违规事件时,一键隔离被盗用的凭证 | | **合规官** | 生成具有完整 CloudTrail 可追溯性的审计就绪 Markdown 报告 | ## 目录 - [架构](#architecture) - [项目结构](#project-structure) - [审计规则](#audit-rules) - [核心功能](#core-features) - [设计模式](#design-patterns) - [快速开始](#quick-start) - [部署](#deployment) - [环境变量](#environment-variables) - [实施状态](#implementation-status) - [依赖项](#dependencies) - [安全](#security) - [许可证](#license) ## 架构 ### 系统概述 ``` ┌─────────────────────────────┐ │ API Gateway │ │ (REST / HTTP / URL) │ └─────────────┬───────────────┘ │ ┌─────────────▼───────────────┐ │ Lambda (Go) │ │ │ │ ┌───────────────────────┐ │ │ │ ServeLambdaEndpoint │ │ │ │ or local :8000 │ │ │ └───────────┬───────────┘ │ │ │ │ ┌──────────────┼──────────────┼──────────────┼──────────────┐ │ │ │ │ │ ┌──────▼──────┐ ┌─────▼─────┐ ┌──────▼──────┐ ┌─────▼─────┐ ┌─────▼─────┐ │ CloudTrail │ │ IAM Roles │ │ Cognito │ │ Wildcard │ │ SIEM │ │ Lookup │ │ Builder │ │ Audit │ │ Monitor │ │ Forwarder │ └──────┬──────┘ └─────┬─────┘ └──────┬──────┘ └─────┬─────┘ └─────┬─────┘ │ │ │ │ │ ┌──────▼──────┐ ┌─────▼─────┐ ┌──────▼──────┐ ┌─────▼─────┐ ┌─────▼─────┐ │ S3 Glacier │ │ S3 Roles │ │ CloudWatch │ │Audit Log │ │ Splunk / │ │ (trail) │ │ (policies)│ │ Logs │ │ (stdout) │ │ Datadog │ └─────────────┘ └───────────┘ └─────────────┘ └───────────┘ └───────────┘ ``` ### 分层架构 ``` ┌─────────────────────────────────────────────────────┐ │ main.go │ │ Initialize() → ServeLambdaEndpoint() │ ├─────────────────────────────────────────────────────┤ │ Transport Layer │ │ internal/routes/ HTTP handlers │ │ internal/services/aws/ Lambda + API Gateway │ ├─────────────────────────────────────────────────────┤ │ Service Layer │ │ internal/services/aws/ IAM, CloudTrail, S3, STS │ │ internal/services/governance/ Audit logging │ │ internal/services/siem/ SIEM forwarding │ │ internal/security/ Wildcard policy monitor │ ├─────────────────────────────────────────────────────┤ │ Domain Model │ │ internal/model/ User, Audit, Organisation, etc. │ ├─────────────────────────────────────────────────────┤ │ Infrastructure │ │ internal/preference/ YAML config → env vars │ │ internal/utilities/ Structured CloudWatch logs │ └─────────────────────────────────────────────────────┘ ``` ### 双重执行模式 ``` isLambdaRuntime() ├── _LAMBDA_SERVER_PORT + AWS_LAMBDA_RUNTIME_API set? │ YES → lambda.Start(HandleAPIGatewayEvent) │ NO → http.ListenAndServe(0.0.0.0:8000) ``` ### 审计架构 (三层) ``` AuditEvent ├── Tier 1: Application Audit │ JSON → stdout → CloudWatch Logs ├── Tier 2: Infrastructure Audit │ CloudTrail auto-capture → S3 → Lifecycle → Glacier └── Tier 3: SIEM Forwarding HTTP POST → Splunk / Datadog / generic (fire-and-forget) ``` ## 项目结构 ``` AWSGateKeeper/ ├── main.go # Entry point ├── aws-config.yaml # AWS credentials (gitignored) ├── aws-config.example.yaml # Credential template ├── go.mod / go.sum # Go module dependencies │ ├── internal/ │ ├── model/ │ │ ├── api_gateway.go # APIGatewayEvent type │ │ ├── audit.go # AuditEvent, AuditLogger, Audit struct │ │ ├── aws.go # AWSAuthorisationKeys │ │ ├── event_bridge.go # EventBridgeEvent type │ │ ├── organisation.go # Organisation type │ │ └── user.go # User, UserType, UserPriviledge enums │ │ │ ├── preference/ │ │ └── environment.go # YAML config → env var bridge │ │ │ ├── routes/ │ │ ├── index.go # GET / handler │ │ └── health.go # GET /health handler │ │ │ ├── security/ │ │ ├── credential.go # AWS credential loader + validator │ │ └── monitor.go # Wildcard policy detection │ │ │ ├── services/ │ │ ├── aws/ │ │ │ ├── authorisation.go # AWS auth singleton (STS verified) │ │ │ ├── cloudtrail.go # CloudTrail event lookup │ │ │ ├── lambda.go # Dual-mode HTTP/Lambda server │ │ │ ├── policies.go # 20 ActionGroups × N Actions │ │ │ ├── roles.go # IAM role builder + policy generator │ │ │ └── s3.go # S3 audit logger + CloudTrail bucket │ │ │ │ │ ├── governance/ │ │ │ └── audit.go # App-level audit logging │ │ │ │ │ └── siem/ │ │ └── forwarder.go # SIEM event forwarder │ │ │ └── utilities/ │ └── logger.go # Structured CloudWatch logger │ └── rules/ └── projcect.md # Audit rule definitions ``` ## 审计规则 | 规则 ID | 风险等级 | 描述 | | ----------------------------------- | ------------ | ------------------------------------------------------------------------------------------------- | | `IAM_VENDOR_EXTERNALID_REQUIRED` | **严重** | 跨账户 IAM 角色必须强制执行唯一的 `ExternalId`,以防止混淆代理问题 | | `COGNITO_PRIVILEGED_EXTERNAL_USERS` | **高** | 外部用户(非企业电子邮件域名)不得属于特权 Cognito 组 | 审计引擎每天评估这两条规则。严重级别的发现会通过 CloudWatch → SIEM 触发即时告警。 ## 核心功能 ### IAM 角色治理 | 权限 | 访问权限 | 策略类型 | | ------------------- | -------------------------------------------------- | ----------------------------------- | | Root | 完全管理员权限 | `AdministratorAccess` (AWS 托管) | | SOCAnalyst | Logs, CloudWatch, CloudTrail, GuardDuty | 内联 (最小权限) | | FrontEndDeveloper | S3, CloudFront, Lambda | 内联 (最小权限) | | BackEndDeveloper | DynamoDB, API Gateway, Lambda, SQS | 内联 (最小权限) | | DeploymentOperation | CodeDeploy, CodePipeline, CloudFormation, ECS, ECR | 内联 (最小权限) | | ThirdPartyFrontEnd | S3, CloudFront (预期为只读) | 内联 (最小权限) | | ThirdPartyBackEnd | DynamoDB, Lambda (预期为只读) | 内联 (最小权限) | | BillingOnly | Billing, Cost Explorer | 内联 (最小权限) | - 幂等创建:现有角色会原地更新其信任策略 - 信任策略将 `sts:AssumeRole` 限制在同账户主体内 ### 通配符策略监控器 ``` Policy JSON → AuditWildcardPolicy() ├── Parse Statement[] ├── containsWildcard(Action)? ├── containsWildcard(Resource)? ├── classifySeverity() │ ├── Both wildcards → CRITICAL (equivalent to AdministratorAccess) │ └── One wildcard → HIGH └── RaiseWildcardAlert() → governance.LogFailedAction() → SIEM ``` ### CloudTrail 集成 - 按来源(`iam.amazonaws.com`, `cognito-idp.amazonaws.com`)进行分页事件查找 - 可配置的回溯窗口 - 从嵌入的 JSON payload 中提取来源 IP - S3 生命周期 → Glacier 用于长期保留 ### SIEM 转发 - 后端:Splunk HEC、Datadog、Sumo Logic 或任何 JSON 收集器 - 可通过环境变量切换(`SIEM_ENABLED=true`) - 针对 5xx 进行带退避的重试;针对 4xx 快速失败 - “即发即忘” —— 永远不会阻塞调用者 ## 设计模式 | 模式 | 位置 | 目的 | | --------------------------- | ------------------------- | ------------------------------------------------- | | **单例认证** | `authorisation.go` | STS 验证的配置在所有 AWS 客户端间共享 | | **策略即代码** | `policies.go`, `roles.go` | 20 个 ActionGroups → 类型化 IAM 权限生成 | | **幂等创建** | `roles.go:ensureRole()` | 在现有角色上创建或更新信任策略 | | **职责链** | `monitor.go` | 检测通配符 → 分类严重程度 → 触发告警 | | **“即发即忘”** | `forwarder.go` | 后台 goroutine 进行 SIEM 交付 | | **配置层级** | `environment.go` | 显式环境变量 > YAML 文件 | ## 快速开始 ### 前置条件 - Go 1.26+ - 具备 IAM、STS、CloudTrail、Cognito 和 S3 权限的 AWS 凭证 - (可选)用于文档处理功能的 Tesseract OCR ### 配置 ``` cp aws-config.example.yaml aws-config.yaml # 使用你的 AWS 凭证编辑 aws-config.yaml ``` ``` # aws-config.yaml aws: access_key_id: YOUR_ACCESS_KEY_ID secret_access_key: YOUR_SECRET_ACCESS_KEY region: ap-east-1 ``` ### 本地开发 ``` go run . # HTTP server 在 http://localhost:8000 # GET / → {"message": "Hello from AWSGateKeeper in AWS Lambda!"} # GET /health → {"status": "healthy"} ``` ### SIEM 集成 (可选) ``` export SIEM_ENABLED=true export SIEM_BACKEND=splunk export SIEM_ENDPOINT=https://hec.example.com:8088/services/collector/event export SIEM_TOKEN=your-hec-token ``` ## 部署 ### AWS Lambda (容器镜像) ``` # 为 Lambda 构建 GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-s -w" -o bootstrap . # 或者使用提供的 Docker 工作流 docker build --platform linux/arm64 -t awsgatekeeper . ``` Lambda 配置: | 设置 | 值 | | ------------ | ----------------- | | Runtime | `provided.al2023` | | Handler | `bootstrap` | | Memory | 256 MB | | Timeout | 30 秒 | | Architecture | `arm64` | ### Lambda (Zip) ``` GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -tags lambda.norpc -ldflags="-s -w" -o bootstrap . zip lambda-deployment.zip bootstrap ``` 创建函数: ``` aws lambda create-function \ --function-name AWSGateKeeper \ --runtime provided.al2023 \ --handler bootstrap \ --architectures arm64 \ --role arn:aws:iam::ACCOUNT:role/execution-role \ --zip-file fileb://lambda-deployment.zip ``` ## 环境变量 | 变量 | 必需 | 默认值 | 描述 | | ----------------------- | ------------- | ----------- | -------------------------------------- | | `AWS_ACCESS_KEY_ID` | 是 (或 YAML) | — | IAM 访问密钥 | | `AWS_SECRET_ACCESS_KEY` | 是 (或 YAML) | — | IAM 秘密密钥 | | `AWS_REGION` | 是 (或 YAML) | `us-east-1` | AWS 区域 | | `LOG_LEVEL` | 否 | `INFO` | DEBUG / INFO / WARN / ERROR / VVERBOSE | | `SIEM_ENABLED` | 否 | `false` | 启用 SIEM 转发 | | `SIEM_BACKEND` | 否 | `generic` | splunk / generic | | `SIEM_ENDPOINT` | 条件性 | — | SIEM 的 HTTP endpoint | | `SIEM_TOKEN` | 条件性 | — | 认证 token | | `SIEM_TIMEOUT_MS` | 否 | `5000` | 以毫秒为单位的请求超时时间 | | `SIEM_RETRIES` | 否 | `2` | 5xx 时的重试次数 | | `AUDIT_S3_BUCKET` | 否 | — | 用于持久化审计日志的 S3 bucket | ## 实施状态 | 组件 | 状态 | | --------------------------------------------- | ------ | | Lambda handler + API Gateway 代理 | 已完成 | | IAM 角色创建 + 策略生成 | 已完成 | | CloudTrail 事件查找 (IAM + Cognito) | 已完成 | | S3 审计记录器 + CloudTrail bucket | 已完成 | | SIEM 转发器 (Splunk/Datadog/generic) | 已完成 | | 通配符策略检测 + 告警 | 已完成 | | 应用审计日志记录 (治理) | 已完成 | | 双重模式 HTTP/Lambda 服务器 | 已完成 | | 事件响应 + IAM 隔离 | 已完成 | | GuardDuty ListFindings + GetFindings | 已完成 | | GuardDuty GetFindingsStatistics | 已完成 | | GuardDuty ArchiveFindings / UnarchiveFindings | 已完成 | | GuardDuty UpdateFindingsFeedback | 已完成 | | GuardDuty CreateSampleFindings | 已完成 | | GuardDuty Threat Intel Sets (CRUD) | 已完成 | | GuardDuty Trusted Entity Sets (CRUD) | 已完成 | | GuardDuty Publishing Destinations | 已完成 | | GuardDuty Coverage Statistics | 已完成 | | GuardDuty Member Accounts | 已完成 | | GuardDuty Organization Statistics | 已完成 | | EventBridge 发布 (隔离 + 扫描) | 已完成 | | Security Hub 集成 (双向) | 已完成 | | Cognito 外部用户审计引擎 | 已完成 | | IR 阶段追踪 (检测→恢复) | 已完成 | | SLA 自动升级追踪器 | 已完成 | | KPI 仪表板 (MTTD/MTTC/MTTR) | 已完成 | | S3 审计生命周期 (90天转Glacier,365天过期) | 已完成 | | 隔离回滚 (DELETE endpoint) | 已完成 | | 令牌桶限流器 | 已完成 | | 输入验证 (ARN, policy, detectorID) | 已完成 | | 单元测试 (安全包) | 已完成 | | IAM ExternalId 审计规则执行 | 桩代码 | | Access Analyzer 关联 (公共访问) | 已完成 | | Macie 敏感数据检测 | 已完成 | | Route53 DNS Firewall 阻断 | 已完成 | | VPC Flow Log 模式分析 | 已完成 | | DynamoDB 发现存储 | 已完成 | | EventBridge 事件发布 | 已完成 | ## 依赖项 | 包 | 版本 | 目的 | | ------------------------- | ------- | ---------------------------------- | | `aws-sdk-go-v2` | v1.41 | AWS SDK 核心 | | `aws-lambda-go` | v1.54 | Lambda 运行时 + API Gateway 代理 | | `cloudtrail` | v1.56 | CloudTrail 事件查找 | | `cognitoidentityprovider` | v1.61 | Cognito 用户池检查 | | `iam` | v1.54 | IAM 角色 / 策略管理 | | `s3` | v1.103 | S3 bucket 操作 | | `sts` | v1.43 | STS 身份验证 | | `google/uuid` | v1.6 | 唯一密钥生成 | | `yaml.v3` | v3.0.1 | YAML 配置解析 | ## 安全 - `aws-config.yaml` 已从版本控制中排除 (`.gitignore`) - CloudTrail + 审计 S3 bucket:默认采用 SSE-S3/AES-256 加密 - IAM 信任策略将 `sts:AssumeRole` 限制在同账户主体内 - 临时凭证(`ASIA...` 前缀)绝不硬编码 - SIEM 转发:HTTPS + token 认证 - 通配符策略检测在每次策略变更时运行 ## 许可证 MIT License. 版权所有 (c) 2026 ctkqiang. 如果您觉得本项目对您有帮助,欢迎请我喝杯咖啡 ☕️,您的支持是我持续维护和改进的动力!

微信扫码捐赠
微信扫码捐赠

标签:AWS, DPI, EVTX分析, Go, Ruby工具, 安全运营, 扫描框架, 日志审计