보안 스터디/웹 해킹

[드림핵/워게임] cookie (웹해킹)

성밍쟁 2024. 1. 2. 13:25
728x90
반응형

문제

cookie (beginner)

 

문제파일

#!/usr/bin/python3
from flask import Flask, request, render_template, make_response, redirect, url_for

app = Flask(__name__)

try:
    FLAG = open('./flag.txt', 'r').read()
except:
    FLAG = '[**FLAG**]'

users = {
    'guest': 'guest',
    'admin': FLAG
}

@app.route('/')
def index():
    username = request.cookies.get('username', None)
    if username:
        return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')
    return render_template('index.html')

@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("not found user");history.go(-1);</script>'
        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            resp.set_cookie('username', username)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'

app.run(host='0.0.0.0', port=8000)

 

https://dreamhack.io/wargame/challenges/6

 

cookie

쿠키로 인증 상태를 관리하는 간단한 로그인 서비스입니다. admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다. 플래그 형식은 DH{...} 입니다. Reference Introduction of Webhacking

dreamhack.io

 

 

풀이 전략

@app.route('/')
def index():
    username = request.cookies.get('username', None)
    if username:
        return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')
    return render_template('index.html')

쿠키, 세션 파트를 막 공부하고 실습으로 나온 문제이기에, 쿠키와 관련된 문제일 것이다.

username = request.cookies.get 으로부터 유저이름을 쿠키에서 얻는다는 것을 확인할 수 있다. 그리고 username이 admin이라면 flag을 획득할 수 있다.

 

그렇다면 유저 정보를 어떻게 확인할 수 있는가?

users = {
    'guest': 'guest',
    'admin': FLAG
}

코드에서 확인할 수 있다. id : guest, 비밀번호 guest로 로그인을 한 번 한 후에, 거기서 쿠키 값을 admin으로 바꾸면 해결 될 것 같다.

 

 

 

실행

처음 서버에 접속하면 Welcome! 한 글자만 보인다. 이제 로그인 버튼을 들어가서 id에 guest, pw에 guest를 입력해보자.

창이 바뀌었다. admin이 아니라서 you are not admin 문구가 뜨는 것을 확인 할 수 있다.

F12 -> Application에서 Cookies값을 확인하보면 Value에 guest로 나오는데, 이걸 admin으로 바꾸고 새로고침을 눌러보자

그러면 flag 값을 획득할 수 있다.

 

flag 값은

DH{7952074b69ee388ab45432737f9b0c56}

이다.

 

 

에필로그

웹해킹을 한동안 안 했더니(물론 공부한 것도 없었지만) 다 까먹어서 빠르게 초반부분부터 다 열심히 공부해놔야겠다

 

 

728x90
반응형