# SSRF通过重定向或替代IP表示绕过云元数据端点封锁

- **ID:** `security/ssrf-cloud-metadata-bypass`
- **领域:** security
- **类别:** network_error
- **错误码:** `SSRF_METADATA_BYPASS`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

应用程序阻止直接请求169.254.169.254，但未处理重定向到该IP或替代表示形式（如十进制IP 2852039166、IPv6映射IPv4 ::ffff:169.254.169.254或DNS重绑定），使SSRF可以访问云元数据。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| AWS IMDSv1 | active | — | — |
| GCP Metadata Server 2023 | active | — | — |
| Azure IMDS 2024-02-01 | active | — | — |

## 解决方案

1. ```
   Use a DNS resolver that resolves all hostnames and validate that the resolved IP is not in the private or link-local range (169.254.0.0/16, 10.0.0.0/8, etc.) before making the request. Example in Python: import socket; ip = socket.gethostbyname(hostname); if ipaddress.ip_address(ip).is_private: raise Exception('Blocked SSRF')
   ```
2. ```
   Disable redirect following in HTTP clients (e.g., requests.get(url, allow_redirects=False)) to prevent redirect-based SSRF
   ```
3. ```
   Use IMDSv2 with a session token that requires a PUT request, making it harder for SSRF to access metadata even if the IP is reached
   ```

## 无效尝试

- **** — Attackers can use decimal IP (2852039166), hex (0xA9FEA9FE), or DNS names that resolve to that IP. (90% 失败率)
- **** — Does not block alternative ports (e.g., 443) or redirects that come from other IPs. (70% 失败率)
- **** — Attackers can use IPv6 mapped addresses or DNS rebinding to bypass the deny list. (65% 失败率)
