
Demonstrates SSRF exploitation via URL parser differential between urllib.parse and requests, including vulnerable service and PoC exploit script.
# vulnerable_service.py - Validates URL with urllib.parse, fetches with requests
from flask import Flask, request
import requests
import urllib.parse
app = Flask(__name__)
def is_allowed_url(url):
parsed = urllib.parse.urlparse(url)
# Only allow http://localhost and http://127.0.0.1
return parsed.hostname in ('localhost', '127.0.0.1')
@app.route('/fetch')
def fetch():
url = request.args.get('url')
if not is_allowed_url(url):
return "Blocked", 403
# Fetch with requests (different parser)
r = requests.get(url, timeout=5)
return r.text
if __name__ == '__main__':
app.run(port=5000)
A service uses Python’s urllib.parse.urlparse to validate URLs but then fetches them with the requests library. Discrepancies in how the two parsers handle special characters (like @) allow an attacker to bypass the whitelist and access internal resources.
urlparse treats [email protected] as having hostname localhost (the part before @ is username), while requests interprets the host as evil.com.pip install flask requests
python vulnerable_service.py
python exploit_ssrf_parser_diff.py