flosell/trailscraper

GitHub: flosell/trailscraper

一款从 AWS CloudTrail 提取并分析日志、自动生成或扩展 IAM 策略的命令行工具。

Stars: 833 | Forks: 38

# TrailScraper [![PyPi Release](https://img.shields.io/pypi/v/trailscraper.svg)](https://pypi.python.org/pypi/trailscraper) [![Build Status](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/341f621e76115123.svg)](https://github.com/flosell/trailscraper/actions/workflows/check.yml) 一个命令行工具,用于从 AWS CloudTrail 中获取有价值的信息,并作为一个通用的 IAM 策略处理工具箱 ## 安装 ### OSX ``` $ brew install trailscraper ``` ### 使用 pip 安装 要求: * Python >= 3.5 * pip ``` $ pip install trailscraper ``` ### 使用 Docker 直接运行 ``` $ docker run --rm --env-file <(env | grep AWS_) -v $HOME/.aws:/root/.aws ghcr.io/flosell/trailscraper:latest ``` 当前版本(从 0.7.0 开始)可在 GitHub 容器注册表(`ghcr.io`)找到,旧版本位于 [DockerHub](https://hub.docker.com/r/flosell/trailscraper/) ## 用法 * [从 CloudTrail API 获取匹配过滤器的 CloudTrail 事件](#get-cloudtrail-events-matching-a-filter-from-cloudtrail-api) * [下载部分日志](#download-some-logs) * [在组织级跟踪中下载部分日志](#download-some-logs-in-organisational-trails) * [在已下载的日志中查找匹配过滤器的 CloudTrail 事件](#find-cloudtrail-events-matching-a-filter-in-downloaded-logs) * [从部分 CloudTrail 记录生成策略](#generate-policy-from-some-cloudtrail-records) * [通过猜测匹配的动作扩展现有策略](#extend-existing-policy-by-guessing-matching-actions) * [查找 CloudTrail 事件并生成 IAM 策略](#find-cloudtrail-events-and-generate-an-iam-policy) ### 从 CloudTrail API 获取匹配过滤器的 CloudTrail 事件 ``` $ trailscraper select --use-cloudtrail-api \ --filter-assumed-role-arn some-arn \ --from 'one hour ago' \ --to 'now' { "Records": [ { "eventTime": "2017-12-11T15:01:51Z", "eventSource": "autoscaling.amazonaws.com", "eventName": "DescribeLaunchConfigurations", ... ``` ### 下载部分日志 ``` $ trailscraper download --bucket some-bucket \ --account-id some-account-id \ --region some-other-region \ --region us-east-1 \ --from 'two days ago' \ --to 'now' \ ``` **注意**:要下载全局服务的日志,请包含 `us-east-1`。详细信息请参见[下方](#)。 ### 在组织级跟踪中下载部分日志 ``` $ trailscraper download --bucket some-bucket \ --account-id some-account-id \ --region us-east-1 \ --org-id o-someorgid \ --from 'two days ago' \ --to 'now' ``` ### 在已下载的日志中查找匹配过滤器的 CloudTrail 事件 ``` $ trailscraper select --filter-assumed-role-arn some-arn \ --from 'one hour ago' \ --to 'now' { "Records": [ { "eventTime": "2017-12-11T15:01:51Z", "eventSource": "autoscaling.amazonaws.com", "eventName": "DescribeLaunchConfigurations", ... ``` ### 从部分 CloudTrail 记录生成策略 ``` $ gzcat some-records.json.gz | trailscraper generate { "Statement": [ { "Action": [ "ec2:DescribeInstances" ], "Effect": "Allow", "Resource": [ "*" ] } ], "Version": "2012-10-17" } ``` ### 通过猜测匹配的动作扩展现有策略 CloudTrail 日志可能不会总是包含所有相关动作。 例如,你的日志可能只包含 Terraform 运行后的 `Create` 动作,而你实际上还需要删除和 更新权限。TrailScraper 可以尝试猜测可能相关的额外语句: ``` $ cat minimal-policy.json | trailscraper guess { "Statement": [ { "Action": [ "s3:PutObject" ], "Effect": "Allow", "Resource": [ "*" ] }, { "Action": [ "s3:DeleteObject", "s3:GetObject", "s3:ListObjects" ], "Effect": "Allow", "Resource": [ "*" ] } ], "Version": "2012-10-17" } $ cat minimal-policy.json | ./go trailscraper guess --only Get { "Statement": [ { "Action": [ "s3:PutObject" ], "Effect": "Allow", "Resource": [ "*" ] }, { "Action": [ "s3:GetObject" ], "Effect": "Allow", "Resource": [ "*" ] } ], "Version": "2012-10-17" } ``` ### 查找 CloudTrail 事件并生成 IAM 策略 ``` $ trailscraper select | trailscraper generate { "Statement": [ { "Action": [ "ec2:DescribeInstances", "ec2:DescribeSecurityGroups", "ec2:DescribeSubnets", "ec2:DescribeVolumes", "ec2:DescribeVpcs", ], "Effect": "Allow", "Resource": [ "*" ] }, { "Action": [ "sts:AssumeRole" ], "Effect": "Allow", "Resource": [ "arn:aws:iam::1111111111:role/someRole" ] } ], "Version": "2012-10-17" } ``` ## 常见问题 ### 如何生成 CloudFormation YAML 格式的策略而不是 JSON? TrailScraper 不提供此功能。但你可以使用 [cfn-flip](https://github.com/awslabs/aws-cfn-template-flip) 来实现: ``` $ trailscraper select | trailscraper generate | cfn-flip Statement: - Action: - ec2:DescribeInstances Effect: Allow Resource: - '*' ``` ### 如何生成 Terraform HCL 格式的策略而不是 JSON? TrailScraper 不提供此功能。但你可以使用 [iam-policy-json-to-terraform](https://github.com/flosell/iam-policy-json-to-terraform) 来实现: ``` $ trailscraper select | trailscraper generate | iam-policy-json-to-terraform data "aws_iam_policy_document" "policy" { statement { sid = "" effect = "Allow" resources = ["*"] actions = [ "ec2:DescribeInstances", ] } } ``` ### 为什么 TrailScraper 遗漏了一些事件? * 确保你有 `us-east-1` 区域的日志。一些全局 AWS 服务(例如 Route53、IAM、STS、CloudFront)使用该区域。详细信息请参见 [CloudTrail 文档](http://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-concepts.html#cloudtrail-concepts-global-service-events) ### 为什么某些由 TrailScraper 生成的动作不是真实的 IAM 动作? 这完全是可能的。不幸的是,没有良好的机器可读文档说明 CloudTrail 事件如何 映射到 IAM 动作,因此 TrailScraper 使用启发式方法来推断正确的动作。这些启发式方法很可能 无法覆盖 AWS 世界的所有特殊情况。 这就是你发挥作用的地方:如果你发现 TrailScraper 未覆盖的特殊情况, 请[新建问题](https://github.com/flosell/trailscraper/issues/new) 或,更佳的是,提交一个拉取请求。 更多详细信息,请查看[贡献指南](./CONTRIBUTING.md) ### 为什么 Click 认为我处于 ASCII 环境? `Click 将中止进一步执行,因为 Python 3 已配置为将 ASCII 用作环境的编码。` 设置描述你区域的环境变量,例如: ``` export LC_ALL=de_DE.utf-8 export LANG=de_DE.utf-8 ``` 或 ``` LC_ALL=C.UTF-8 LANG=C.UTF-8 ``` 详细信息请参见 http://click.pocoo.org/5/python3/#python-3-surrogate-handling ## 开发 ``` $ ./go setup # set up venv, dependencies and tools $ ./go test # run some tests $ ./go check # run some style checks $ ./go # let's see what we can do here ```
标签:AWS, CLI, CloudTrail, Docker, DPI, IAM策略, Python, SaaS, WiFi技术, 事件过滤, 安全防御评估, 开源, 技术工具, 政策扩展, 数据提取, 无后门, 日志挖掘, 权限管理, 模型越狱, 策略生成, 网络调试, 自动化, 请求拦截, 逆向工具