π SSRF Tutorial β Flask Lab + WebGoat + Docker¶
π What is SSRF?¶
Server-Side Request Forgery (SSRF) allows an attacker to make the server send requests on their behalf, often to internal systems.
β οΈ Real-World Example¶
A website lets users fetch profile pictures from a URL:
What if an attacker supplies:
The server will access internal systems, which the attacker canβt reach directly.
π οΈ 1. Build Your Own SSRF-Vulnerable Flask App¶
π Project Structure:¶
π app.py¶
from flask import Flask, request
import requests
app = Flask(__name__)
@app.route("/")
def index():
return '''
<h2>πΈ Fetch Image From URL</h2>
<form action="/fetch">
<input name="url" placeholder="Enter image URL">
<button type="submit">Fetch</button>
</form>
'''
@app.route("/fetch")
def fetch():
url = request.args.get("url")
try:
r = requests.get(url, timeout=3)
return f"<h4>β
Response from {url}</h4><pre>{r.text[:300]}</pre>"
except Exception as e:
return f"<p style='color:red'>β Error: {str(e)}</p>"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
π requirements.txt¶
π Dockerfile¶
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
βΆοΈ Run the Lab¶
Visit http://localhost:5000
π§ͺ SSRF Testing¶
Try inputs like:
-
http://localhost:5000 -
http://127.0.0.1:5000 -
http://169.254.169.254(AWS metadata) -
file:///etc/passwd(for local file read)
If successful, the server fetches data it shouldnβt.
π SSRF Prevention¶
β Tips to prevent SSRF:
-
Block internal IPs (
127.0.0.1,localhost,169.254.*,10.*,192.168.*) -
Use a URL allowlist
-
Avoid user-controlled URLs
-
Use DNS pinning (optional)
-
Restrict outbound traffic from servers
π§ͺ 2. Practice SSRF in WebGoat¶
π³ Run WebGoat + WebWolf:¶
Access:
-
WebGoat: http://localhost:8080/WebGoat
-
Login:
guest / guest
Go to:
It includes:
-
URL parsing attacks
-
Bypassing filters (e.g.
127.1,0x7f000001) -
Cloud metadata exploitation
π§ Summary¶
| Concept | Details |
|---|---|
| SSRF | Server makes requests attacker controls |
| Impact | Internal service access, cloud metadata leaks |
| Fix 1 | Block internal IPs |
| Fix 2 | Use allowlist of domains |
| Fix 3 | Avoid user-controlled URLs |
| Fix 4 | Sanitize and validate URL schemes |