Understanding Server-Side Request Forgery (SSRF)
Server-Side Request Forgery (SSRF) is a critical web security vulnerability that allows attackers to manipulate a server into making unintended requests to internal or external resources. By exploiting this weakness, attackers can bypass security controls, access sensitive data, and compromise internal systems that would otherwise be protected from direct external access.
Key Points
- SSRF exploits server functionality to make requests to unintended locations, turning the server into a proxy for malicious activities
- Common targets include internal APIs, cloud metadata services, databases, and administrative interfaces
- Impact ranges from data exposure and firewall bypass to complete infrastructure compromise
- Prevention requires multiple layers of defense including input validation, network segmentation, and access controls
How SSRF Attacks Work
Attack Flow
SSRF attacks follow a predictable pattern that exploits trust relationships between servers and internal resources:
Step 1: Attacker Input The attacker provides malicious input (typically a URL) through a vulnerable application feature that processes external resources.
Step 2: Server Processing The server accepts the input and makes an HTTP request to the specified location without proper validation.
Step 3: Unauthorized Access The request reaches internal services, cloud metadata endpoints, or other sensitive resources that should not be accessible.
Real-World Example
Consider a web application with a feature that fetches and displays images from user-provided URLs:
Normal use: https://example.com/fetch?url=https://trusted-site.com/image.jpg
Malicious use: https://example.com/fetch?url=http://169.254.169.254/latest/meta-data/
In the malicious scenario, the attacker targets the AWS metadata service to retrieve sensitive credentials and configuration data.
Common Attack Vectors
| Attack Vector | Target | Potential Impact |
|---|---|---|
http://localhost/admin | Internal admin panels | Unauthorized administrative access |
http://169.254.169.254/ | Cloud metadata services | Credential theft, instance takeover |
file:///etc/passwd | Local file system | Sensitive file disclosure |
http://internal-db:5432 | Internal databases | Data exfiltration |
Prevention and Mitigation
Input Validation and Sanitization
Whitelist Approach
The most effective defense against SSRF is implementing a strict whitelist:
- Maintain a list of allowed domains and protocols
- Reject any input that doesn't match the whitelist
- Validate URL schemes (allow only
http://andhttps://, blockfile://,gopher://,dict://, etc.) - Parse and validate URLs before processing
Blacklist Limitations
Warning: Blacklisting internal IP ranges is insufficient. Attackers can bypass blacklists using DNS rebinding, URL encoding, alternative IP representations (e.g.,
127.0.0.1vs0x7f000001vs2130706433), and redirect chains.
Network-Level Controls
Network Segmentation
- Isolate application servers from sensitive internal services
- Use separate network zones for public-facing and internal resources
- Implement strict firewall rules between network segments
- Deploy a DMZ (Demilitarized Zone) for internet-facing applications
Least Privilege Access
- Limit server permissions to only necessary resources
- Run application processes with minimal privileges
- Restrict outbound connections from application servers
- Use service accounts with limited scope
Application-Level Defenses
Response Validation
Implement multiple layers of response checking:
- Verify response content types match expectations
- Implement timeout limits for external requests (e.g., 5-10 seconds)
- Sanitize and validate response data before processing
- Check response size limits to prevent resource exhaustion
Disable Unnecessary Features
- Remove URL redirect functionality if not required
- Disable support for unused protocols
- Limit HTTP methods to only those needed (typically GET and POST)
- Avoid exposing raw URL fetching functionality to users
Code-Level Best Practices
Safe URL Handling
# Example: Safe URL validation
def is_safe_url(url):
parsed = urlparse(url)
# Only allow HTTP/HTTPS
if parsed.scheme not in ['http', 'https']:
return False
# Resolve hostname to IP
ip = socket.gethostbyname(parsed.hostname)
# Block private IP ranges
if ipaddress.ip_address(ip).is_private:
return False
# Check against whitelist
if parsed.hostname not in ALLOWED_DOMAINS:
return False
return True
Security Tools and Monitoring
Web Application Firewalls (WAF)
- Deploy WAFs to detect and block SSRF patterns
- Configure rules to monitor outbound server requests
- Set up alerts for suspicious internal network access
- Use cloud-native WAF solutions for cloud deployments
Regular Security Testing
- Conduct penetration testing focused on SSRF vulnerabilities
- Perform code reviews of URL handling functionality
- Use automated security scanners (Burp Suite, OWASP ZAP) to identify potential SSRF points
- Include SSRF testing in CI/CD pipelines
Detection and Response
Monitoring Indicators
Watch for these signs of potential SSRF exploitation:
- Unusual outbound requests from application servers
- Access attempts to internal IP ranges (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16) - Requests to cloud metadata endpoints (
169.254.169.254) - Unexpected DNS queries for internal hostnames
- Requests to localhost or loopback addresses
- Unusual protocol usage (file://, gopher://, dict://)
Incident Response Steps
- Identify the vulnerable endpoint and attack vector through log analysis
- Isolate affected systems to prevent further exploitation
- Analyze logs to determine scope of unauthorized access and data exposure
- Remediate the vulnerability with proper input validation and access controls
- Review and rotate any potentially compromised credentials, API keys, and tokens
- Document the incident and update security procedures