What is SSRF?
Server-Side Request Forgery (SSRF) is a security vulnerability that allows attackers to make the server hosting a web application send requests to internal or external resources accessible by the server. This vulnerability can lead to internal network discovery, cloud metadata access, and even escalation to remote code execution.
How SSRF Works
SSRF occurs when an application fetches remote resources without validating the user-supplied URL. Attackers can manipulate the URL to make the server access internal services, cloud metadata endpoints (AWS/GCP/Azure), or perform port scanning.
β Vulnerable Code Example
1// PHP - Unsafe URL fetch2$url = $_GET['url'];3$content = file_get_contents($url);4echo $content;56// Attacker input:7// url=http://169.254.169.254/latest/meta-data/iam/security-credentials/8// Server fetches AWS credentials!β Secure Code
1// PHP - URL allowlist validation2$url = $_GET['url'];3$allowed = ['api.example.com'];4$host = parse_url($url, PHP_URL_HOST);5if (in_array($host, $allowed)) {6 $content = file_get_contents($url);7 echo $content;8} else {9 die("Invalid URL");10}Types of SSRF
Access internal network resources and services via server-side requests
Retrieve credentials from AWS, GCP, or Azure metadata endpoints
No visible response; detect via timing analysis or out-of-band callbacks
Bypass URL filters using encoding, DNS rebinding, or open redirects
Escalate to RCE via Redis, Memcached, or internal service exploitation
What Can Happen?
- πInternal Network Access:
Scan and access internal services, databases, admin panels behind firewalls
- βοΈCloud Credentials Theft:
Steal AWS IAM credentials, GCP tokens, Azure access tokens from metadata endpoints
- πFirewall Bypass:
Bypass network restrictions, access services that should be unreachable
- π₯Remote Code Execution:
Exploit internal services (Redis, Memcached) via gopher:// protocol for RCE
How to Prevent SSRF
- βURL Allowlisting: Only allow requests to specific, trusted domains and IPs
- βBlock Private IPs: Reject requests to localhost, 127.0.0.1, 169.254.x.x, and RFC1918 ranges
- βDisable Dangerous Protocols: Block file://, gopher://, dict://, and other non-HTTP protocols
- βNetwork Segmentation: Isolate application servers from internal networks
- βCloud Metadata Protection: Use IMDSv2 (AWS), require headers (GCP), implement network controls
- βResponse Validation: Don't return raw responses to users, validate content types