# 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)
某服务使用 Python 的 urllib.parse.urlparse 来校验 URL,但随后使用 requests 库发起请求。两个解析器在处理特殊字符(如 @)时的差异,使攻击者能够绕过白名单并访问内部资源。
urlparse 将 [email protected] 视为主机名为 localhost(@ 之前的部分是用户名),而 requests 将主机解析为 evil.com。pip install flask requests
python vulnerable_service.py
python exploit_ssrf_parser_diff.py