# AWS Pentesting
{{#include ../../banners/hacktricks-training.md}}
## 基本信息
**在开始对** AWS **环境进行渗透测试之前,您需要了解一些关于 AWS 工作原理的基本知识,以帮助您理解需要做什么、如何查找错误配置以及如何利用它们。**
组织层级、IAM 和其他基本概念等概念在以下内容中进行了说明:
{{#ref}}
aws-basic-information/
{{#endref}}
## 学习实验室
- [https://github.com/RhinoSecurityLabs/cloudgoat](https://github.com/RhinoSecurityLabs/cloudgoat)
- [https://github.com/BishopFox/iam-vulnerable](https://github.com/BishopFox/iam-vulnerable)
- [https://github.com/nccgroup/sadcloud](https://github.com/nccgroup/sadcloud)
- [https://github.com/bridgecrewio/terragoat](https://github.com/bridgecrewio/terragoat)
- [https://github.com/ine-labs/AWSGoat](https://github.com/ine-labs/AWSGoat)
- [http://flaws.cloud/](http://flaws.cloud/)
- [http://flaws2.cloud/](http://flaws2.cloud/)
模拟攻击的工具:
- [https://github.com/Datadog/stratus-red-team/](https://github.com/Datadog/stratus-red-team/)
- [https://github.com/sbasu7241/AWS-Threat-Simulation-and-Detection/tree/main](https://github.com/sbasu7241/AWS-Threat-Simulation-and-Detection/tree/main)
## AWS 渗透测试/红队方法论
为了审计 AWS 环境,了解以下内容非常重要:哪些 **服务正在使用**,什么 **被暴露**,谁对什么 **有访问权限**,以及内部 AWS 服务与 **外部服务** 是如何连接的。
从红队的角度来看,**攻陷 AWS 环境的第一步**是设法获取一些 **凭证**。以下是一些获取凭证的想法:
- **泄露** 在 github(或类似平台)- OSINT
- **社交** 工程
- **密码** 重用(密码泄露)
- AWS 托管应用程序中的漏洞
- [**服务器端请求伪造**](https://book.hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf.html) 访问元数据端点
- **本地文件读取**
- `/home/USERNAME/.aws/credentials`
- `C:\Users\USERNAME\.aws\credentials`
- 第三方 **被攻破**
- **内部** 员工
- [**Cognito** ](aws-services/aws-cognito-enum/index.html#cognito)凭证
或者通过 **攻陷一个未认证的服务**:
{{#ref}}
aws-unauthenticated-enum-access/
{{#endref}}
或者如果您正在进行 **审查**,您可以直接 **请求凭证**,使用这些角色:
{{#ref}}
aws-permissions-for-a-pentest.md
{{#endref}}
> [!NOTE]
> 在您成功获取凭证后,您需要知道 **这些凭证属于谁**,以及 **他们可以访问什么**,因此您需要进行一些基本的枚举:
## 基本枚举
### SSRF
如果您在 AWS 内部的机器上发现了 SSRF,请查看此页面以获取技巧:
{{#ref}}
https://book.hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf.html
{{#endref}}
### Whoami
您需要了解的第一件事是您是谁(您所在的账户以及有关 AWS 环境的其他信息):
```bash
# Easiest way, but might be monitored?
aws sts get-caller-identity
aws iam get-user # This will get your own user
# If you have a Key ID
aws sts get-access-key-info --access-key-id=ASIA1234567890123456
# Get inside error message
aws sns publish --topic-arn arn:aws:sns:us-east-1:*account id*:aaa --message aaa
# From metadata
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/dynamic/instance-identity/document
```
> [!CAUTION]
> 注意,公司可能会使用 **canary tokens** 来识别 **令牌被盗用和使用** 的情况。在使用令牌之前,建议检查该令牌是否为 canary token。\
> 更多信息请 [**查看此页面**](aws-services/aws-security-and-detection-services/aws-cloudtrail-enum.md#honeytokens-bypass)。
### Org Enumeration
{{#ref}}
aws-services/aws-organizations-enum.md
{{#endref}}
### IAM Enumeration
如果您拥有足够的权限,**检查 AWS 账户内每个实体的权限** 将帮助您了解您和其他身份可以做什么,以及如何 **提升权限**。
如果您没有足够的权限来枚举 IAM,您可以 **通过暴力破解来获取它们**。\
请查看 **如何进行枚举和暴力破解**:
{{#ref}}
aws-services/aws-iam-enum.md
{{#endref}}
> [!NOTE]
> 现在您 **已经获得了一些关于您凭据的信息**(如果您是红队,希望您 **没有被检测到**)。是时候找出环境中正在使用哪些服务了。\
> 在接下来的部分中,您可以查看一些 **枚举常见服务** 的方法。
## Services Enumeration, Post-Exploitation & Persistence
AWS 拥有惊人的服务数量,在以下页面中,您将找到 **基本信息、枚举** 备忘单\*\*,\*\* 如何 **避免检测**,获取 **持久性**,以及其他关于其中一些服务的 **后期利用** 技巧:
{{#ref}}
aws-services/
{{#endref}}
请注意,您 **不** 需要 **手动** 执行所有工作,下面的帖子中您可以找到关于 [**自动工具**](#automated-tools) 的 **部分**。
此外,在此阶段,您可能会发现 **更多暴露给未认证用户的服务**,您可能能够利用它们:
{{#ref}}
aws-unauthenticated-enum-access/
{{#endref}}
## Privilege Escalation
如果您可以 **检查至少自己的权限** 在不同资源上,您可以 **检查是否能够获得更多权限**。您应该至少关注以下权限:
{{#ref}}
aws-privilege-escalation/
{{#endref}}
## Publicly Exposed Services
在枚举 AWS 服务时,您可能发现其中一些 **向互联网暴露元素**(虚拟机/容器端口、数据库或队列服务、快照或存储桶...)。\
作为渗透测试者/红队成员,您应该始终检查是否可以在它们上找到 **敏感信息/漏洞**,因为它们可能为您提供 **进一步访问 AWS 账户** 的机会。
在本书中,您应该找到关于如何查找 **暴露的 AWS 服务以及如何检查它们** 的 **信息**。关于如何查找 **暴露网络服务中的漏洞**,我建议您 **搜索** 特定的 **服务**:
{{#ref}}
https://book.hacktricks.wiki/
{{#endref}}
## Compromising the Organization
### From the root/management account
当管理账户在组织中创建新账户时,会在新账户中创建一个 **新角色**,默认命名为 **`OrganizationAccountAccessRole`**,并给予 **AdministratorAccess** 策略以便管理账户访问新账户。
因此,要以管理员身份访问子账户,您需要:
- **攻陷** **管理** 账户并找到 **子账户的 ID** 和 **角色的名称**(默认是 OrganizationAccountAccessRole),以允许管理账户以管理员身份访问。
- 要查找子账户,请转到 AWS 控制台中的组织部分或运行 `aws organizations list-accounts`
- 您无法直接找到角色的名称,因此请检查所有自定义 IAM 策略,并搜索任何允许 **`sts:AssumeRole` 的策略,针对之前发现的子账户**。
- **攻陷** 管理账户中的 **主体**,并具有 **`sts:AssumeRole` 权限,针对子账户中的角色**(即使该账户允许管理账户中的任何人进行冒充,由于这是外部账户,特定的 `sts:AssumeRole` 权限是必要的)。
## Automated Tools
### Recon
- [**aws-recon**](https://github.com/darkbitio/aws-recon): 一个多线程的 AWS 安全专注的 **库存收集工具**,用 Ruby 编写。
```bash
# Install
gem install aws_recon
# Recon and get json
AWS_PROFILE= aws_recon \
--services S3,EC2 \
--regions global,us-east-1,us-east-2 \
--verbose
```
- [**cloudlist**](https://github.com/projectdiscovery/cloudlist): Cloudlist 是一个 **多云工具,用于获取资产**(主机名,IP 地址)来自云服务提供商。
- [**cloudmapper**](https://github.com/duo-labs/cloudmapper): CloudMapper 帮助您分析您的亚马逊网络服务(AWS)环境。它现在包含更多功能,包括安全问题的审计。
```bash
# Installation steps in github
# Create a config.json file with the aws info, like:
{
"accounts": [
{
"default": true,
"id": "",
"name": "dev"
}
],
"cidrs":
{
"2.2.2.2/28": {"name": "NY Office"}
}
}
# Enumerate
python3 cloudmapper.py collect --profile dev
## Number of resources discovered
python3 cloudmapper.py stats --accounts dev
# Create HTML report
## In the report you will find all the info already
python3 cloudmapper.py report --accounts dev
# Identify potential issues
python3 cloudmapper.py audit --accounts dev --json > audit.json
python3 cloudmapper.py audit --accounts dev --markdow > audit.md
python3 cloudmapper.py iam_report --accounts dev
# Identify admins
## The permissions search for are in https://github.com/duo-labs/cloudmapper/blob/4df9fd7303e0337ff16a08f5e58f1d46047c4a87/shared/iam_audit.py#L163-L175
python3 cloudmapper.py find_admins --accounts dev
# Identify unused elements
python3 cloudmapper.py find_unused --accounts dev
# Identify publivly exposed resources
python3 cloudmapper.py public --accounts dev
python cloudmapper.py prepare #Prepare webserver
python cloudmapper.py webserver #Show webserver
```
- [**cartography**](https://github.com/lyft/cartography): Cartography 是一个 Python 工具,它将基础设施资产及其之间的关系整合在一个由 Neo4j 数据库驱动的直观图形视图中。
```bash
# Install
pip install cartography
## At the time of this writting you need neo4j version 3.5.*
# Get AWS info
AWS_PROFILE=dev cartography --neo4j-uri bolt://127.0.0.1:7687 --neo4j-password-prompt --neo4j-user neo4j
```
- [**starbase**](https://github.com/JupiterOne/starbase): Starbase 收集来自服务和系统的资产和关系,包括云基础设施、SaaS 应用程序、安全控制等,形成一个直观的图形视图,支持 Neo4j 数据库。
- [**aws-inventory**](https://github.com/nccgroup/aws-inventory): (使用 python2) 这是一个尝试 **发现所有** [**AWS 资源**](https://docs.aws.amazon.com/general/latest/gr/glos-chap.html#resource) 的工具,这些资源是在一个账户中创建的。
- [**aws_public_ips**](https://github.com/arkadiyt/aws_public_ips): 这是一个 **获取所有公共 IP 地址**(包括 IPv4/IPv6)与 AWS 账户关联的工具。
### Privesc & Exploiting
- [**SkyArk**](https://github.com/cyberark/SkyArk)**:** 发现扫描的 AWS 环境中最特权的用户,包括 AWS Shadow Admins。它使用 powershell。您可以在 [https://github.com/cyberark/SkyArk/blob/master/AWStealth/AWStealth.ps1](https://github.com/cyberark/SkyArk/blob/master/AWStealth/AWStealth.ps1) 中的函数 **`Check-PrivilegedPolicy`** 找到 **特权策略的定义**。
- [**pacu**](https://github.com/RhinoSecurityLabs/pacu): Pacu 是一个开源的 **AWS 利用框架**,旨在针对云环境进行攻击性安全测试。它可以 **枚举**、查找 **错误配置** 并 **利用** 它们。您可以在 **`user_escalation_methods`** 字典中找到 [https://github.com/RhinoSecurityLabs/pacu/blob/866376cd711666c775bbfcde0524c817f2c5b181/pacu/modules/iam\_\_privesc_scan/main.py#L134](https://github.com/RhinoSecurityLabs/pacu/blob/866376cd711666c775bbfcde0524c817f2c5b181/pacu/modules/iam__privesc_scan/main.py#L134) 中的 **特权权限的定义**。
- 请注意,pacu **仅检查您自己的 privescs 路径**(而不是账户范围内)。
```bash
# Install
## Feel free to use venvs
pip3 install pacu
# Use pacu CLI
pacu
> import_keys # import 1 profile from .aws/credentials
> import_keys --all # import all profiles
> list # list modules
> exec iam__enum_permissions # Get permissions
> exec iam__privesc_scan # List privileged permissions
```
- [**PMapper**](https://github.com/nccgroup/PMapper): Principal Mapper (PMapper) 是一个脚本和库,用于识别 AWS 账户或 AWS 组织中 AWS 身份和访问管理 (IAM) 配置的风险。它将账户中的不同 IAM 用户和角色建模为有向图,从而能够检查 **权限提升** 和攻击者可能采取的获取 AWS 中资源或操作的替代路径。您可以在 [https://github.com/nccgroup/PMapper/tree/master/principalmapper/graphing](https://github.com/nccgroup/PMapper/tree/master/principalmapper/graphing) 中检查以 `_edges.py` 结尾的文件名中使用的 **权限以查找 privesc** 路径。
```bash
# Install
pip install principalmapper
# Get data
pmapper --profile dev graph create
pmapper --profile dev graph display # Show basic info
# Generate graph
pmapper --profile dev visualize # Generate svg graph file (can also be png, dot and graphml)
pmapper --profile dev visualize --only-privesc # Only privesc permissions
# Generate analysis
pmapper --profile dev analysis
## Run queries
pmapper --profile dev query 'who can do iam:CreateUser'
pmapper --profile dev query 'preset privesc *' # Get privescs with admins
# Get organization hierarchy data
pmapper --profile dev orgs create
pmapper --profile dev orgs display
```
- [**cloudsplaining**](https://github.com/salesforce/cloudsplaining): Cloudsplaining 是一个 AWS IAM 安全评估工具,识别最小权限的违规行为并生成风险优先级的 HTML 报告。\
它将向您显示潜在的 **过度权限** 客户、内联和 AWS **策略** 以及哪些 **主体可以访问它们**。 (它不仅检查权限提升,还检查其他有趣的权限,建议使用)。
```bash
# Install
pip install cloudsplaining
# Download IAM policies to check
## Only the ones attached with the versions used
cloudsplaining download --profile dev
# Analyze the IAM policies
cloudsplaining scan --input-file /private/tmp/cloudsplaining/dev.json --output /tmp/files/
```
- [**cloudjack**](https://github.com/prevade/cloudjack): CloudJack 评估 AWS 账户的 **子域劫持漏洞**,这是由于 Route53 和 CloudFront 配置的解耦造成的。
- [**ccat**](https://github.com/RhinoSecurityLabs/ccat): 列出 ECR 仓库 -> 拉取 ECR 仓库 -> 后门化 -> 推送后门镜像
- [**Dufflebag**](https://github.com/bishopfox/dufflebag): Dufflebag 是一个工具,**搜索**公共弹性块存储 (**EBS**) 快照中的秘密,这些秘密可能被意外遗留。
### 审计
- [**cloudsploit**](https://github.com/aquasecurity/cloudsploit)**:** CloudSploit 由 Aqua 提供,是一个开源项目,旨在检测云基础设施账户中的 **安全风险**,包括:亚马逊网络服务 (AWS)、微软 Azure、谷歌云平台 (GCP)、甲骨文云基础设施 (OCI) 和 GitHub(它不查找 ShadowAdmins)。
```bash
./index.js --csv=file.csv --console=table --config ./config.js
# Compiance options: --compliance {hipaa,cis,cis1,cis2,pci}
## use "cis" for cis level 1 and 2
```
- [**Prowler**](https://github.com/prowler-cloud/prowler): Prowler 是一个开源安全工具,用于执行 AWS 安全最佳实践评估、审计、事件响应、持续监控、加固和取证准备。
```bash
# Install python3, jq and git
# Install
pip install prowler
prowler -v
# Run
prowler
prowler aws --profile custom-profile [-M csv json json-asff html]
```
- [**CloudFox**](https://github.com/BishopFox/cloudfox): CloudFox 帮助您在不熟悉的云环境中获得情境意识。它是一个开源命令行工具,旨在帮助渗透测试人员和其他攻击性安全专业人员在云基础设施中找到可利用的攻击路径。
```bash
cloudfox aws --profile [profile-name] all-checks
```
- [**ScoutSuite**](https://github.com/nccgroup/ScoutSuite): Scout Suite 是一个开源的多云安全审计工具,能够评估云环境的安全态势。
```bash
# Install
virtualenv -p python3 venv
source venv/bin/activate
pip install scoutsuite
scout --help
# Get info
scout aws -p dev
```
- [**cs-suite**](https://github.com/SecurityFTW/cs-suite): 云安全套件 (使用 python2.7,似乎未维护)
- [**Zeus**](https://github.com/DenizParlak/Zeus): Zeus 是一个强大的工具,用于 AWS EC2 / S3 / CloudTrail / CloudWatch / KMS 最佳加固实践 (似乎未维护)。它仅检查系统内默认配置的凭据。
### 持续审计
- [**cloud-custodian**](https://github.com/cloud-custodian/cloud-custodian): Cloud Custodian 是一个用于管理公共云账户和资源的规则引擎。它允许用户 **定义政策以启用良好管理的云基础设施**,既安全又成本优化。它将组织中许多临时脚本整合为一个轻量级和灵活的工具,具有统一的指标和报告。
- [**pacbot**](https://github.com/tmobile/pacbot)**: 代码政策机器人 (PacBot)** 是一个用于 **持续合规监控、合规报告和云安全自动化** 的平台。在 PacBot 中,安全和合规政策以代码形式实现。PacBot 发现的所有资源都根据这些政策进行评估,以衡量政策符合性。PacBot **自动修复** 框架提供了通过采取预定义措施自动响应政策违规的能力。
- [**streamalert**](https://github.com/airbnb/streamalert)**:** StreamAlert 是一个无服务器的 **实时** 数据分析框架,使您能够 **摄取、分析和警报** 来自任何环境的数据,**使用您定义的数据源和警报逻辑**。计算机安全团队使用 StreamAlert 每天扫描数 TB 的日志数据以进行事件检测和响应。
## DEBUG: 捕获 AWS cli 请求
```bash
# Set proxy
export HTTP_PROXY=http://localhost:8080
export HTTPS_PROXY=http://localhost:8080
# Capture with burp nor verifying ssl
aws --no-verify-ssl ...
# Dowload brup cert and transform it to pem
curl http://127.0.0.1:8080/cert --output Downloads/certificate.cer
openssl x509 -inform der -in Downloads/certificate.cer -out Downloads/certificate.pem
# Indicate the ca cert to trust
export AWS_CA_BUNDLE=~/Downloads/certificate.pem
# Run aws cli normally trusting burp cert
aws ...
```
## 参考
- [https://www.youtube.com/watch?v=8ZXRw4Ry3mQ](https://www.youtube.com/watch?v=8ZXRw4Ry3mQ)
- [https://cloudsecdocs.com/aws/defensive/tooling/audit/](https://cloudsecdocs.com/aws/defensive/tooling/audit/)
{{#include ../../banners/hacktricks-training.md}}