GO KALI FREE
IntermediateWeb Security

SSRF Explained: Server-Side Request Forgery Vulnerabilities

Understand Server-Side Request Forgery attacks where attackers manipulate server-side requests to access internal resources, cloud metadata, and bypass firewalls.

#SSRF#Web Security#Server-Side Request Forgery#Cloud Security#OWASP

When the Server Becomes Your Proxy

A web app lets you fetch a URL to preview a link. You enter http://169.254.169.254/latest/meta-data/ — the cloud metadata endpoint that should only be accessible from within the server. The server fetches it and returns your cloud provider's access keys. This is Server-Side Request Forgery (SSRF): the attacker tricks the server into making requests to internal services, cloud metadata, or systems behind firewalls that should never be exposed.

Prerequisites

Before studying SSRF, you should understand:

  • **Web Security Fundamentals** — HTTP request flow, server architecture
  • **Cloud Computing Basics** — AWS, GCP, Azure metadata services
  • **Networking** — Internal IP ranges, localhost, firewall concepts
  • **URL Parsing** — How URLs are structured and parsed
  • How SSRF Works

    Applications that fetch remote resources are vulnerable when user input controls the URL. Common functionality includes webhooks, image processing, proxy functionality, API integrations, and URL preview generators.

    Basic SSRF Example

    import requests
    
    @app.route('/fetch-avatar')
    def fetch_avatar():
        url = request.args.get('url')
        response = requests.get(url)
        return response.content
    

    An attacker exploits this to access internal services:

    GET /fetch-avatar?url=http://169.254.169.254/latest/meta-data/ HTTP/1.1
    Host: vulnerable-app.com
    

    Types of SSRF

    Basic SSRF

    The application fetches from user-controlled URLs without restrictions.

    Blind SSRF

    The attacker cannot see the response but can observe side effects like timing differences or DNS lookups.

    Partial SSRF

    Some validation exists but is insufficient — attackers use protocol confusion or URL parsing tricks.

    SSRF Attack Vectors

    Cloud Metadata Endpoints

    # AWS metadata
    http://169.254.169.254/latest/meta-data/
    # GCP metadata
    http://metadata.google.internal/computeMetadata/v1/
    # Azure metadata
    http://169.254.169.254/metadata/instance?api-version=2021-02-01
    

    Internal Service Access

    http://localhost:6379/  # Redis
    http://localhost:9200/  # Elasticsearch
    http://localhost:27017/ # MongoDB
    

    File Protocol Access

    file:///etc/passwd
    file:///proc/self/environ
    file:///app/config/database.yml
    

    SSRF Defenses

    Allowlist Approach (Recommended)

    ALLOWED_DOMAINS = {'api.trusted-service.com', 'cdn.trusted.com'}
    
    def is_allowed_url(url):
        from urllib.parse import urlparse
        parsed = urlparse(url)
        return parsed.hostname in ALLOWED_DOMAINS
    

    Network-Level Defenses

    iptables -A OUTPUT -d 169.254.169.254 -j DROP
    iptables -A OUTPUT -d 127.0.0.0/8 -j DROP
    iptables -A OUTPUT -d 10.0.0.0/8 -j DROP
    

    Disable Unnecessary URL Schemes

    ALLOWED_SCHEMES = {'https'}
    
    def safe_fetch(url):
        parsed = parse_url(url)
        if parsed.scheme not in ALLOWED_SCHEMES:
            raise ValueError("Scheme not allowed")
        return requests.get(url)
    

    Real-World Examples

    Capital One Breach (2019): Paige Thompson exploited an SSRF vulnerability in Capital One's AWS infrastructure to access EC2 metadata, retrieving IAM credentials and accessing over 100 million customer records.

    Uber Breach (2016): Attackers found an SSRF vulnerability in Uber's public application, accessed AWS metadata, and stole credentials for S3 buckets containing 57 million user records.

    HackerOne SSRF (2017): A researcher found an SSRF vulnerability allowing internal file access, leading to a $10,000 bug bounty payout.

    Common Mistakes

    Relying on hostname validation alone: DNS rebinding switches from safe to malicious IP after validation.

    Insufficient blocklist coverage: Blocking 127.0.0.1 but forgetting IPv6 ::1, or blocking AWS metadata but not GCP or Azure equivalents.

    URL parsing inconsistencies: Different libraries parse URLs differently.

    Redirect following without re-validation: The application validates the initial URL but follows redirects to internal IPs.

    Best Practices

  • **Use an allowlist** — Only permit connections to known, trusted domains
  • **Validate at input and network level** — Defense in depth
  • **Disable unnecessary URL schemes** — Only allow http/https
  • **Do not follow redirects blindly** — Re-validate each redirect target
  • **Use a dedicated outbound proxy** — Network-level restrictions
  • **Use cloud metadata protection** — AWS IMDSv2 requires headers for access
  • Related Tools

  • **Burp Suite** — SSRF scanning and manual testing
  • **ncat** — Set up listeners to detect SSRF callbacks
  • **curl** — Test SSRF payloads manually
  • **ffuf** — Fuzz for SSRF endpoints
  • Related Articles

  • Web Security Fundamentals
  • Burp Suite Introduction
  • SQL Injection Basics
  • CSRF Explained
  • Summary

    SSRF allows attackers to turn your server into a proxy to access internal resources. The most effective defense is using an allowlist of trusted domains and validating at both the application and network level. Cloud environments are particularly vulnerable due to accessible metadata endpoints.

    Knowledge Check

  • What is the difference between SSRF and CSRF?
  • Why is the AWS metadata endpoint at 169.254.169.254 particularly dangerous?
  • What is DNS rebinding and how does it bypass hostname validation?
  • Why is a denylist less secure than an allowlist for SSRF defense?
  • Name three URL schemes besides http/https abused in SSRF attacks.
  • Frequently Asked Questions

    What is SSRF and how does it differ from CSRF?

    SSRF (Server-Side Request Forgery) tricks a server into making requests to unintended locations, while CSRF (Cross-Site Request Forgery) tricks a user's browser into making requests. SSRF is particularly dangerous because internal services trust traffic from within the network, and firewalls typically do not block server-to-server communication.

    Why is the AWS metadata endpoint at 169.254.169.254 particularly dangerous?

    Cloud metadata endpoints expose sensitive information including IAM credentials, instance identity, and security configuration. In the Capital One breach (2019), an SSRF vulnerability allowed access to EC2 metadata, retrieving IAM credentials that provided access to over 100 million customer records. AWS IMDSv2 now requires headers for access, mitigating this risk.

    What is DNS rebinding and how does it bypass hostname validation?

    DNS rebinding switches a domain's DNS resolution from a safe IP to a malicious one after hostname validation passes. An attacker registers a domain that first resolves to a safe IP (passes validation), then changes to 169.254.169.254 when the server fetches the URL. This bypasses hostname-based allowlists because validation and fetching happen at different times.

    Why is a denylist less secure than an allowlist for SSRF defense?

    Denylists must block every possible internal IP range (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, IPv6 ::1, cloud metadata IPs). Missing any range creates an bypass. Allowlists only permit known trusted domains, making them inherently more secure — unknown or new targets are blocked by default.

    What URL schemes are abused in SSRF attacks?

    Beyond http/https, attackers use file:/// to read local files (e.g., `/etc/passwd`), gopher:// to interact with internal services, dict:// for protocol interaction, and smb:// for Windows file sharing. Disabling unnecessary URL schemes is a critical defense — most applications only need http and https.

    How does SSRF affect cloud environments?

    Cloud environments expose metadata endpoints accessible only from within the network (169.254.169.254 on AWS/GCP/Azure). SSRF allows attackers to reach these endpoints, stealing IAM credentials, API keys, and instance configuration. Once armed with cloud credentials, attackers can access S3 buckets, databases, and other cloud resources.

    What is blind SSRF?

    Blind SSRF occurs when the attacker cannot see the server's response but can observe side effects like timing differences, DNS lookups, or HTTP callbacks. Detection is harder because there is no direct data exfiltration, but attackers can still trigger internal requests, cause denial of service, or use out-of-band techniques to extract information.

    How do I test for SSRF vulnerabilities?

    Use Burp Suite to identify endpoints that fetch URLs based on user input (webhooks, image processors, URL previewers). Test with internal IPs (127.0.0.1, 169.254.169.254), various URL schemes (file://, gopher://), and DNS rebinding techniques. Set up a callback server (Burp Collaborator) to detect blind SSRF.

    What is the Capital One breach and how did SSRF play a role?

    In 2019, Paige Thompson exploited an SSRF vulnerability in Capital One's AWS infrastructure to access EC2 metadata, retrieving IAM credentials. These credentials provided access to S3 buckets containing over 100 million customer records. The breach cost Capital One over $150 million and highlighted the critical risk of SSRF in cloud environments.

    How do network-level defenses help prevent SSRF?

    Network-level defenses include iptables rules blocking outbound connections to internal IP ranges (`iptables -A OUTPUT -d 169.254.169.254 -j DROP`), using a dedicated outbound proxy with strict rules, and cloud-native protections like AWS IMDSv2 (requires headers for metadata access). Defense in depth combines application allowlists with network restrictions.

    What applications are vulnerable to SSRF?

    Any application that fetches remote resources based on user input is potentially vulnerable. Common examples include webhook handlers, URL preview/meta tag fetchers, image processors, proxy functionality, API integrations, PDF generators, and RSS feed parsers. If a user-controlled URL determines where the server makes a request, SSRF is possible.

    How do I prevent redirect following from bypassing SSRF validation?

    Do not follow redirects blindly — re-validate each redirect target before following it. An attacker can pass validation with a safe URL that redirects (via HTTP 301/302) to an internal IP. Disable automatic redirect following in HTTP libraries, or re-check the URL after each redirect against your allowlist.