


# app.py
#!/usr/bin/env python3
import os
import string
from flask import Flask, request, abort, render_template, session
SECRETS_PATH = 'secrets/'
ALLOWED_CHARACTERS = string.ascii_letters + string.digits + '/'
app = Flask(__name__)
app.secret_key = os.urandom(32)
# create sample file
with open(f'{SECRETS_PATH}/sample', 'w') as f:
f.write('Hello, world :)')
# create flag file
flag_dir = SECRETS_PATH + os.urandom(32).hex()
os.mkdir(flag_dir)
flag_path = flag_dir + '/flag'
with open('/flag', 'r') as f0, open(flag_path, 'w') as f1:
f1.write(f0.read())
@app.route('/', methods=['GET'])
def get_index():
# safely save the secret into session data
session['secret'] = flag_path
# provide file read functionality
path = request.args.get('path')
if not isinstance(path, str) or path == '':
return render_template('index.html', msg='input the path!')
if any(ch not in ALLOWED_CHARACTERS for ch in path):
return render_template('index.html', msg='invalid path!')
full_path = f'./{SECRETS_PATH}{path}'
if not os.path.isfile(full_path):
return render_template('index.html', msg='invalid path!')
try:
with open(full_path, 'r') as f:
return render_template('index.html', msg=f.read())
except:
abort(500)
flag가 있는 위치는 secrets/무작위32자/flag에 있다.
첫번째 시도
세션 값을 그대로 넣자

필터링으로 인한 실패
두번째 시도
app.py 코드를 보면 루트 디렉터리에 flag 내용을 가져와 숨겨진 폴더에 flag 파일을 넣는다
with open('/flag', 'r') as f0, open(flag_path, 'w') as f1:
f1.write(f0.read())
그러면 루트 디렉터리에 flag를 불러오자

필터링으로 인한 실패
필터링을 우회하기 위한 이중 인코딩도 실패
세번째 시도
네번째 시도





블로그나 지피티에 뒤져봐도 앞에 세션정보는 base64로 인코딩된다고한다.
디코딩 시도 했지만 안되었음
flask-unsign 소스코드를 보면


세션을 저장할 때