[NISACTF 2022]babyupload

php 复制代码
from flask import Flask, request, redirect, g, send_from_directory
import sqlite3
import os
import uuid

app = Flask(__name__)

SCHEMA = """CREATE TABLE files (
id text primary key,
path text
);
"""


def db():
    g_db = getattr(g, '_database', None)
    if g_db is None:
        g_db = g._database = sqlite3.connect("database.db")
    return g_db


@app.before_first_request
def setup():
    os.remove("database.db")
    cur = db().cursor()
    cur.executescript(SCHEMA)


@app.route('/')
def hello_world():
    return """<!DOCTYPE html>
<html>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="file">
    <input type="submit" value="Upload File" name="submit">
</form>
<!-- /source -->
</body>
</html>"""


@app.route('/source')
def source():
    return send_from_directory(directory="/var/www/html/", path="www.zip", as_attachment=True)


@app.route('/upload', methods=['POST'])
def upload():
    if 'file' not in request.files:
        return redirect('/')
    file = request.files['file']
    if "." in file.filename:
        return "Bad filename!", 403
    conn = db()
    cur = conn.cursor()
    uid = uuid.uuid4().hex    #生成uid
    try:
        cur.execute("insert into files (id, path) values (?, ?)", (uid, file.filename,))
    except sqlite3.IntegrityError:
        return "Duplicate file"
    conn.commit()

    file.save('uploads/' + file.filename)
    return redirect('/file/' + uid)


@app.route('/file/<id>')
def file(id):
    conn = db()
    cur = conn.cursor()
    cur.execute("select path from files where id=?", (id,))
    res = cur.fetchone()
    if res is None:
        return "File not found", 404

    # print(res[0])

    with open(os.path.join("uploads/", res[0]), "r") as f:
        return f.read()


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

def upload() 要求上传的文件不能有后缀,且文件名前会拼接一个前缀upload/,使得输出的文件只能是在目录upload/下的,这里就涉及到os.path.join()的绝对路径拼接漏洞:

绝对路径拼接漏洞

os.path.join(path,*paths)函数用于将多个文件路径连接成一个组合的路径。第一个函数通常包含了基础路径,而之后的每个参数被当作组件拼接到基础路径之后。

然而,这个函数有一个少有人知的特性,如果拼接的某个路径以 / 开头,那么包括基础路径在内的所有前缀路径都将被删除,该路径将视为绝对路径。

php 复制代码
with open(os.path.join("uploads/", res[0]), "r") as f:
    return f.read() 

就是说传/flag,那么之前的upload/就会删除,就直接读取了根目录下的flag文件。抓包将文件名改为/flag。

直接访问得到的路径,获得flag:

相关推荐
F1FJJ5 分钟前
Shield CLI v0.3.0:插件系统上线,首发 MySQL Web 管理
网络·数据库·网络协议·mysql·容器·golang
Seven976 分钟前
MySQL锁机制:从全局锁到行级锁的深度解读
mysql
波波七7 分钟前
maven导入spring框架
数据库·spring·maven
深蓝轨迹8 分钟前
Redis 分布式锁实现流程
数据库·redis·分布式
程序猿阿伟10 分钟前
《OpenClaw端口通信失效全解:监听修改与防火墙规则落地指南》
服务器·数据库·windows
进击的雷神11 分钟前
突破增量抓取困境:基于数据库状态判断的高效新闻爬虫设计
数据库·爬虫·spiderflow
一叶飘零_sweeeet15 分钟前
击穿 MySQL 事务隔离级别:底层实现原理 + 生产级架构选型避坑指南
数据库·mysql·架构·mysql事务隔离级别
虾..17 分钟前
Linux 五种IO模型
linux·服务器·数据库
波兰的蓝17 分钟前
《内网渗透实战:红日 1 号全流程复现(Web 入侵 / 提权 / 横向移动 / 黄金票据)》
web安全·网络安全
程序边界23 分钟前
深度Oracle替换工程实践的技术解读(上篇)
数据库·oracle