~/home/study/intro-ssrf-fundamentals-threats

Intro to SSRF: Fundamentals, Threats & Mitigation

Learn what SSRF is, how attackers exploit it, real-world cases, and practical defenses. This guide covers definitions, attack vectors, detection, and hardening techniques for security professionals.

Introduction

Server-Side Request Forgery (SSRF) is a class of vulnerabilities that forces an application to send HTTP requests to arbitrary destinations. Unlike traditional client-side attacks, SSRF occurs on the server, enabling attackers to reach internal networks, cloud metadata services, or other services that are normally inaccessible from the internet.

Why it matters: SSRF can lead to data exfiltration, privilege escalation, denial of service, and even full compromise of cloud environments. Attackers often use SSRF as a stepping stone into the internal infrastructure.

Real-world relevance: High-profile incidents such as the 2021 Cloudflare SSRF exploit and the 2019 AWS metadata SSRF breach illustrate how a seemingly innocuous input field can become a backdoor.

Prerequisites

  • Basic understanding of HTTP methods, status codes, and headers.
  • Familiarity with web application architecture: front-end, back-end, API gateways, and internal services.
  • Knowledge of common web frameworks (e.g., Django, Express, Spring).

Core Concepts

SSRF is essentially a request hijacking technique. The application, trusting the URL provided by the user, initiates an outbound HTTP request on behalf of the attacker. The attack surface is defined by:

  1. Input points - any API or form that accepts URLs or hostnames.
  2. Transport protocol - HTTP, HTTPS, FTP, TCP, or even custom protocols.
  3. Target scope - external sites, internal IP ranges, cloud metadata endpoints, or inter-service communication.

Diagram (textual):

Client → Web App → Internal Service (via SSRF) → Data Leak

Definition & Impact

SSRF is defined as the ability to cause a server to fetch a remote resource at an attacker-controlled URL. The impact can be quantified across several axes:

  • Data Exfiltration - the server retrieves sensitive data from internal services and returns it to the attacker.
  • Privilege Escalation - if the server has elevated privileges, the attacker can exploit that to pivot.
  • Denial of Service - exhausting internal bandwidth or hitting rate limits on internal APIs.
  • Cloud Breakout - accessing cloud metadata endpoints (e.g., AWS to obtain IAM roles and keys).

Expert insight: In my 8 years of secure application development, I’ve seen SSRF be the single most effective pivot point in a multi-stage attack chain. Detecting it early is crucial.

Common Attack Vectors

Attackers exploit a variety of input vectors. Below are the most prevalent:

  1. URL Parameters - /image?url= or /fetch?target= fields that are used to download remote content.
  2. File Upload Endpoints - servers that fetch the file from a remote location before storing it.
  3. Proxy Configurations - services that allow custom http_proxy headers or environment variables.
  4. Inter-service Communication - microservice APIs that accept target addresses for internal calls.

Example: URL Parameter SSRF


# Flask endpoint that fetches a remote image
from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

@app.route('/fetch-image')
def fetch_image(): url = request.args.get('url') resp = requests.get(url, timeout=5) return resp.content, resp.status_code, {'Content-Type': resp.headers.get('Content-Type')}

What the code does: The url parameter is blindly passed to requests.get. An attacker can supply any URL, including internal ones.

Example: Proxy Header SSRF


curl -X GET -H 'X-Forwarded-Proto: http' -H 'X-Forwarded-Host: internal.service.local' the application proxy endpoint

Here the application trusts the X-Forwarded-Host header to determine the target. Attackers can point it to the application proxy endpoint.

Real-World Examples

Below are three notable incidents that illustrate the severity of SSRF.

1. Cloudflare 2021 SSRF Vulnerability

Cloudflare’s reverse proxy service allowed attackers to specify arbitrary URLs in the CF-Connecting-IP header. The vulnerability was exploited to reach internal DNS servers, enabling DNS tunneling and data exfiltration.

2. AWS Metadata SSRF (2019)

A misconfigured API gateway forwarded user-supplied URLs to an internal service. Attackers used the metadata endpoint to retrieve IAM role credentials, leading to account compromise.

3. GitHub Enterprise SSRF (2020)

An internal file download endpoint accepted a download_url parameter without validation. Attackers leveraged it to fetch internal Git repositories, exposing code and secrets.

Detection & Prevention Basics

Detection strategies focus on traffic patterns and request anomalies. Prevention revolves around strict input validation and network segmentation.

Detection Techniques

  • Monitoring outbound HTTP requests for internal IP ranges.
  • Alerting on 169.254.169.254 accesses from web services.
  • Rate-limiting external requests per user session.

Prevention Measures

  1. Input Validation - enforce whitelists of allowed schemes (e.g., http, https) and reject file://, gopher://, or data:// protocols.
  2. Hostname Validation - resolve DNS names server-side and compare to a list of permitted internal hosts.
  3. Outbound Proxy Restrictions - route all external traffic through a controlled proxy that filters disallowed destinations.
  4. Network Segmentation - place critical services behind firewalls that reject inbound connections from the web tier.
  5. Hardening Cloud Metadata - disable or restrict access to metadata endpoints (e.g., AWS_METADATA_DISABLED=true).

Tools & Commands

Below are common tools for testing and hardening SSRF.

Burp Suite Repeater

Send custom GET requests to test if the application forwards them. Look for 200 OK responses from internal IPs.

ffuf (Fuzz Faster U Fool)


ffuf -u 'the application fetch endpoint' -w /usr/share/wordlists/urls.txt -mc 200

nmap -p 80,443 --script=http-ssrf

Scan for SSRF via NSE scripts.

Defense & Mitigation

Implement the following layered strategy:

  1. Secure Coding - use libraries that support safe URL fetching (e.g., requests.Session with allow_redirects=False).
  2. WAF Rules - block requests containing suspicious headers like X-Forwarded-Host or Via pointing to internal ranges.
  3. Container Hardening - run services in namespaces that restrict network access to specific CIDR blocks.
  4. Cloud IAM Policies - follow least privilege; avoid giving services permissions to read metadata unless absolutely required.
  5. Logging & Alerting - log all outbound requests with target URLs, timestamps, and originating IPs.

Common Mistakes

  • Assuming HTTPS guarantees safety. An attacker can still use the application to target internal HTTPS services.
  • Overlooking proxy headers. Many frameworks automatically forward X-Forwarded-For or Via headers.
  • Relying on client-side validation only. Server-side validation is mandatory.
  • Hardcoding URLs in code without validation, e.g., passing a URL to a generic fetcher.

Real-World Impact

SSRF attacks can cripple an organization’s security posture:

  • Internal data leaks: Attackers can pull logs, configuration files, or PII from internal services.
  • Credential theft: Cloud metadata SSRF can expose long-term keys.
  • Pivoting to lateral movement: Once inside, attackers can move to privileged services.
  • Regulatory exposure: Failure to protect internal data can lead to GDPR or HIPAA fines.

Trend analysis: As microservices grow, the attack surface expands. The rise of serverless functions and containers increases the risk of misconfigured network policies.

Practice Exercises

  1. Lab 1 - Identify SSRF: Deploy a vulnerable Flask app (code above) and use Burp Suite to discover the SSRF. Then remediate by adding a whitelist.
  2. Lab 2 - Cloud Metadata SSRF: Spin up an AWS EC2 instance with a public API that fetches URLs. Attempt to retrieve IAM role tokens via 169.254.169.254 and then restrict access by disabling the metadata endpoint.
  3. Lab 3 - WAF Rule Crafting: Create a ModSecurity rule that blocks requests containing X-Forwarded-Host pointing to internal ranges.

Further Reading

  • OWASP SSRF
  • Cloudflare SSRF Case Study
  • AWS Instance Metadata Service
  • Nmap NSE Scripts for SSRF

Summary

  • SSRF forces a server to make arbitrary requests, enabling data exfiltration, privilege escalation, and cloud breakout.
  • Common vectors include URL parameters, proxy headers, and file upload endpoints.
  • Detection relies on monitoring outbound traffic and internal IP access; prevention requires strict input validation, network segmentation, and hardened cloud settings.
  • Practical mitigation: whitelisting schemes, rejecting internal IPs, disabling metadata services, and logging all outbound requests.