网站做网站词怎么推广,一个网站seo做哪些工作内容,跨境电商平台都有哪些,专业网站设计联系[NISACTF 2022]babyupload wp
信息搜集
进入页面#xff1a; 尝试文件上传#xff0c;但是各种后缀名我都试过了#xff0c;过不去。
在源码中发现提示#xff0c;存在 ./source 路径#xff1a; 访问该路径得到源码#xff1a;
from flask import Flask, request, r…[NISACTF 2022]babyupload wp
信息搜集
进入页面 尝试文件上传但是各种后缀名我都试过了过不去。
在源码中发现提示存在 ./source 路径 访问该路径得到源码
from flask import Flask, request, redirect, g, send_from_directory
import sqlite3
import os
import uuidapp 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_dbapp.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 methodpost enctypemultipart/form-dataSelect image to upload:input typefile namefileinput typesubmit valueUpload File namesubmit
/form
!-- /source --
/body
/htmlapp.route(/source)
def source():return send_from_directory(directory/var/www/html/, pathwww.zip, as_attachmentTrue)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!, 403conn db()cur conn.cursor()uid uuid.uuid4().hextry:cur.execute(insert into files (id, path) values (?, ?), (uid, file.filename,))except sqlite3.IntegrityError:return Duplicate fileconn.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(host0.0.0.0, port80)这道题的要求就两个看懂 python 代码os.path.join 绝对路径拼接。
加强 python 基础
为了能更好的看懂本次 python 代码我推荐以下文章
python 连接数据库
Python getattr() 函数
关于python中的查询数据库内容中用到的fetchone()函数
request.files 介绍
在 Flask 框架中request.files 是一个代表上传文件的特殊字典对象。它允许您访问用户通过 HTTP 请求上传的文件。具体来说在 Flask 中当您的应用程序收到一个带有文件上传的 POST 请求时可以使用 request.files 来获取上传文件的信息。
request.files 是一个字典它的键是文件域( input 标签的 name 属性值)而值是一个 FileStorage 对象该对象提供了访问和操作上传文件的方法和属性。
例如假设您的 HTML 表单中有一个文件上传域其 name 属性为 file
form action/upload methodpost enctypemultipart/form-dataSelect image to upload:input typefile namefileinput typesubmit valueUpload File namesubmit
/form那么您可以通过以下方式访问上传文件
file request.files[file]上面的代码将返回上传文件的 FileStorage 对象您可以使用该对象的方法和属性来获取文件的详细信息如文件名、内容类型、保存文件等
filename file.filename # 获取上传文件的文件名
file.save(/path/to/save/file) # 将上传文件保存到指定路径通过 request.files您可以轻松地处理和管理用户上传的文件数据。注意为了使用 request.files您的请求必须使用 enctypemultipart/form-data 编码类型这是用于文件上传的标准编码类型。
关于 python 的 flask 框架在学 SSTI 模板注入时有所学习。
源码分析
# 设置了一个路由当以 POST 方式访问 ./upload 页面时就会触发 upload() 函数
app.route(/upload, methods[POST])
def upload():if file not in request.files:return redirect(/)
# 从 HTML 表单中获取 file 对象即用户上传的文件file request.files[file]
# 文件名中不能有小数点if . in file.filename:return Bad filename!, 403
# 调用 db() 函数连接数据库conn db()cur conn.cursor()
# 调用 uuid 模块以某种方式生成文件的 uiduid uuid.uuid4().hextry:cur.execute(insert into files (id, path) values (?, ?), (uid, file.filename,))except sqlite3.IntegrityError:return Duplicate fileconn.commit()
# 将文件保存到 ./uploads/ 路径下file.save(uploads/ file.filename)
# 返回最终保存的文件路径return redirect(/file/ uid)不过在此段代码中 file.save(uploads/ file.filename) 并没有起到作用推测原因是 uploads/ 路径不存在。
下面来看这段代码
# 设定路由当访问 /file/id 路径时触发 file 函数其中 id 为用户输入的任意值该值会被作为 file 函数的参数值
app.route(/file/id)
def file(id):
# 连接数据库conn db()cur conn.cursor()
# 执行 SQL 语句根据 id 查找 pathcur.execute(select path from files where id?, (id,))
# fetchone 函数的作用在上面的博客中已提到执行该语句后res[0] 就是 SQL 语句查询到的 path 根据源代码path 就是传入的文件名res cur.fetchone()if res is None:return File not found, 404# print(res[0])
# os.path.join 函数拼接路径再 open 打开文件with open(os.path.join(uploads/, res[0]), r) as f:return f.read()os.path.join 绝对路径拼接
os.path.join(path, *paths) 函数用于将多个文件路径连接成一个组合的路径。第一个参数通常包含了基础路径而之后的每个参数都被当做组件拼接到基础路径后。
但是该函数有个特性如果拼接的某个路径以 / 开头那么包括基础路径在内的所有前缀路径都将被删除该路径将被视为绝对路径。下面的示例揭示了开发者可能遇到的这个陷阱。
比如上述代码中若 res[0] 以 / 开头则前面的 uploads/ 将被抹去最后拼接的结果只有 res[0] 。
漏洞利用
据此我们可以传入一个名为 “/flag” 的文件那么最后经过路径拼接抹去前面的 uploads/ 路径就会直接打开根目录下的 flag 文件。
当然这种方法存在猜测性前提是根目录下确实有一个 flag 文件。
文件名为 /flag 文件内容任意 返回结果中给出了路径/file/de087626346e4fc390a272957aa5b88e 那么直接访问 拿到 flag 。