
urllib.parse と requests の間の URL パーサー差分を利用した SSRF 悪用を実演し、脆弱なサービスと PoC エクスプロイトスクリプトを含みます。
# 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 ライブラリで取得しています。2つのパーサーが特殊文字(@ など)を処理する方法の違いにより、攻撃者はホワイトリストを回避して内部リソースにアクセスできます。
urlparse は [email protected] のホスト名が localhost であると見なします(@ より前の部分はユーザー名)。一方、requests はホストを evil.com として解釈します。pip install flask requests
python vulnerable_service.py
python exploit_ssrf_parser_diff.py