针对 Flask 中 TOCTOU 文件权限竞态的教育性 CVE PoC;利用检查-打开窗口期间的符号链接替换来泄露敏感文件。
# toctou_server.py - File server that checks permission then opens
import os, time, tempfile
from flask import Flask, request
app = Flask(__name__)
SAFE_DIR = '/tmp/safe'
@app.route('/read')
def read_file():
filename = request.args.get('file')
filepath = os.path.join(SAFE_DIR, filename)
# Check: ensure it's a regular file and owned by user
if not os.path.isfile(filepath):
return "Not a file", 403
# Race window: attacker replaces file with symlink to /etc/shadow
time.sleep(0.2) # simulate processing delay
with open(filepath, 'r') as f:
return f.read()
if __name__ == '__main__':
app.run(port=5000)
一个文件服务器会先检查某个路径是否为常规文件,然后经过一段延迟后再打开该文件。攻击者可在竞态窗口期间将该文件替换为指向敏感系统文件(例如 /etc/shadow)的符号链接,从而绕过检查并读取受保护的数据。
isfile)与使用(open)并非原子操作,攻击者可在两者之间更改文件系统对象。pip install flask
python toctou_server.py
python exploit_toctou.py
如果竞态条件利用成功,响应中会包含 shadow 文件内容。