FaridSec/MyFYP

GitHub: FaridSec/MyFYP

一个自动化分析 AWS IAM 权限提升路径的云安全工具,结合图算法与 AI 辅助实现风险发现与修复建议。

Stars: 0 | Forks: 0

# IAM 攻击路径分析系统 **针对 AWS IAM 的自动化权限提升路径发现与 AI 辅助修复。** 本系统分析 AWS IAM 配置,使用基于图的最短路径算法发现权限提升路径,通过校准的概率权重模型分配风险等级,并提供具有人工干预 (HITL) 监督的 AI 辅助修复建议。 ## 架构 ``` ┌─────────────────────────────────────────────────────────────────┐ │ Layer 6: Dashboard │ │ Streamlit UI · Graph Viz · HITL Workflow │ │ (dashboard/app.py) │ ├─────────────────────────────────────────────────────────────────┤ │ Layer 5: Remediation │ │ LLM Client · Prompt Builder · Audit Log · HITL Workflow │ │ (remediation/*.py) │ ├─────────────────────────────────────────────────────────────────┤ │ Layer 4: Engine │ │ Path Discovery (Yen's K-Shortest) · Graph Loader │ │ Result Writer · Risk Classification │ │ (engine/*.py) │ ├─────────────────────────────────────────────────────────────────┤ │ Layer 3: Graph │ │ Neo4j Schema · Edge Weights (-log₂P) · Graph Builder │ │ (graph/*.py) │ ├─────────────────────────────────────────────────────────────────┤ │ Layer 2: Ingestion │ │ IAM Fetcher · Policy Parser · Admin Classifier │ │ (ingestion/*.py) │ ├─────────────────────────────────────────────────────────────────┤ │ Layer 1: Configuration │ │ Environment · Constants · Privesc Permission Set │ │ (ingestion/config.py) │ └─────────────────────────────────────────────────────────────────┘ │ │ │ ▼ ▼ ▼ AWS IAM API Neo4j Graph SQLite Audit (boto3) (neo4j driver) (remediation_audit.db) ``` ## 前置条件 | 要求 | 版本 | 用途 | |---|---|---| | Python | 3.10+ | 运行时 | | Docker | 20.10+ | Neo4j 容器 | | AWS CLI | v2 | 凭证配置 | | Git | 2.30+ | 版本控制 | ## 安装 ``` # 克隆仓库 git clone cd FYP # 创建虚拟环境(推荐) python -m venv .venv .venv\Scripts\activate # Windows source .venv/bin/activate # Linux/macOS # 安装依赖 pip install -r requirements.txt # 配置环境 copy .env.example .env # Windows cp .env.example .env # Linux/macOS # 使用你的凭据编辑 .env ``` ## Neo4j 设置 ``` # 在 Docker 中启动 Neo4j docker run --name neo4j-iam \ -p 7474:7474 \ -p 7687:7687 \ -e NEO4J_AUTH=neo4j/your_password \ -d neo4j:5 # 验证 Neo4j 是否正在运行 # 在浏览器中打开 http://localhost:7474 # 使用 neo4j / your_password 登录 ``` 更新您的 `.env` 文件: ``` NEO4J_URI=bolt://localhost:7687 NEO4J_USER=neo4j NEO4J_PASSWORD=your_password ``` ## AWS 凭证 所需的最低 IAM 权限: ``` { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iam:ListUsers", "iam:ListRoles", "iam:ListGroups", "iam:ListAttachedUserPolicies", "iam:ListAttachedRolePolicies", "iam:ListAttachedGroupPolicies", "iam:ListUserPolicies", "iam:ListRolePolicies", "iam:ListGroupPolicies", "iam:GetUserPolicy", "iam:GetRolePolicy", "iam:GetGroupPolicy", "iam:GetPolicy", "iam:GetPolicyVersion", "iam:ListGroupsForUser", "iam:GetRole" ], "Resource": "*" } ] } ``` 在 `.env` 中配置凭证: ``` AWS_DEFAULT_REGION=us-east-1 AWS_ACCOUNT_ID=123456789012 ``` AWS 凭证通过 boto3 的凭证链进行解析: 1. 环境变量(`AWS_ACCESS_KEY_ID`、`AWS_SECRET_ACCESS_KEY`) 2. 共享凭证文件(`~/.aws/credentials`) 3. EC2 实例配置文件 / ECS 任务角色 ## 运行系统 ### 步骤 1:验证配置 ``` python main.py --validate ``` ### 步骤 2:估算 API 调用(模拟运行) ``` python main.py --mode ingest --dry-run ``` ### 步骤 3:运行完整流水线 ``` # 所有三个阶段:ingest → build graph → discover paths python main.py --mode all ``` 或单独运行各阶段: ``` python main.py --mode ingest # Fetch IAM data from AWS → iam_data.json python main.py --mode build # Build Neo4j graph from iam_data.json python main.py --mode discover # Run path discovery + write to Neo4j ``` ### 步骤 4:启动仪表板 ``` streamlit run dashboard/app.py ``` ### 重置(测试清理) ``` python main.py --reset ``` ## CloudGoat 测试 [CloudGoat](https://github.com/RhinoSecurityLabs/cloudgoat) 用于真实基准评估。 ``` # 安装 CloudGoat(独立仓库) pip install cloudgoat # 部署测试场景 python cloudgoat.py create iam_privesc_by_attachment # 对 CloudGoat 环境运行全面分析 python main.py --mode all # 评估 ground truth python tests/cloudgoat_scenarios.py ``` ### 支持的场景 | 场景 | 描述 | 预期风险 | |---|---|---| | `iam_privesc_by_attachment` | 直接策略附加提权 | 严重/高 | | `iam_privesc_by_rollback` | 策略版本回滚提权 | 严重/高 | | `cloud_breach_s3` | 通过 PassRole 的多跳跨服务 | 严重-中 | ## 运行测试 ``` # 完整测试套件(100 个测试) pytest tests/ -v # 独立套件 pytest tests/test_ingestion.py -v # 53 ingestion tests pytest tests/test_graph.py -v # 13 graph tests pytest tests/test_engine.py -v # 17 engine tests pytest tests/test_remediation.py -v # 17 remediation tests # 误报回归套件 pytest tests/false_positive_suite.py -v ``` ## 项目结构 ``` FYP/ ├── ingestion/ # Layer 2: IAM data ingestion │ ├── config.py # Configuration & constants │ ├── models.py # Data models (IAMUser, IAMRole, etc.) │ ├── assembler.py # Data assembly & admin classification │ ├── iam_fetcher.py # AWS API calls with backoff │ └── parser.py # Policy document parsing │ ├── graph/ # Layer 3: Neo4j graph construction │ ├── schema.py # Constraints & indexes │ ├── edge_weights.py # -log₂(P) weight model │ └── builder.py # Graph builder with MERGE │ ├── engine/ # Layer 4: Path discovery │ ├── models.py # AttackPath, EdgeDetail, PolicyDoc │ ├── graph_loader.py # Neo4j → NetworkX conversion │ ├── pathfinder.py # Yen's K-Shortest algorithm │ └── result_writer.py # Results → Neo4j persistence │ ├── remediation/ # Layer 5: AI-assisted remediation │ ├── audit_log.py # SQLite audit trail (3 tables) │ ├── llm_client.py # OpenAI + Anthropic abstraction │ ├── prompt_builder.py # Structured prompt construction │ └── hitl_workflow.py # HITL Streamlit component │ ├── dashboard/ # Layer 6: Streamlit dashboard │ ├── app.py # 5-page dashboard application │ └── graph_viz.py # Pyvis network visualisation │ ├── tests/ # Test suites │ ├── test_ingestion.py # 53 tests │ ├── test_graph.py # 13 tests │ ├── test_engine.py # 17 tests │ ├── test_remediation.py # 17 tests │ ├── false_positive_suite.py # False positive regression │ ├── cloudgoat_scenarios.py # Ground truth evaluation │ └── prowler_comparison.py # Prowler comparison tool │ ├── calibration/ │ └── weight_calibration_log.md # Calibration tracking │ ├── main.py # CLI entry point ├── requirements.txt # Python dependencies ├── .env.example # Environment variable template └── README.md # This file ``` ## 已知局限性 | 局限性 | 状态 | 影响 | |---|---|---| | **权限边界** | 已检测,未评估 | 路径显示的风险可能高于实际风险。已显示警告。 | | **服务控制策略** | 未建模 | 未考虑组织级别的限制。 | | **基于资源的策略** | 未建模 | 未分析 S3 存储桶策略、KMS 密钥策略。 | | **单账户范围** | 设计如此 | 跨账户信任将作为指向外部 ARN 存根的边显示。 | | **Pyvis 30 条路径显示限制** | 大小保护 | 为保证可读性,仪表板将图形限制为 30 条路径。 | | **LLM 幻觉风险** | 通过 HITL 缓解 | AI 建议在采取行动前需要人工审查。 | | **会话策略** | 未建模 | 未考虑 STS AssumeRole 会话策略。 | | **CloudTrail 分析** | 未实现 | 没有历史使用数据来优化概率。 | ## 参考 - Rhino Security Labs (2018). *AWS IAM Privilege Escalation — Methods and Mitigations.* - Wang, L., Jajodia, S., & Singhal, A. (2008). *Network Security Metrics.* - Amershi, S., et al. (2019). *Guidelines for Human-AI Interaction.* CHI '19. - CloudGoat: https://github.com/RhinoSecurityLabs/cloudgoat
标签:AWS IAM, DLL 劫持, Kubernetes, Streamlit, 协议分析, 图算法, 大语言模型, 攻击路径分析, 权限提升, 特权检测, 访问控制, 请求拦截, 逆向工具