[Dreamhack] cookie

2025. 3. 15. 14:47·Webhacking/Dreamhack
 

cookie

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

dreamhack.io


app.py

#!/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)

코드 해석

users 변수 선언

  1. flag.txt 파일로부터 FLAG 데이터를 가져온다.
  2. FLAG 데이터를 패스워드로 선언한다.

엔드포인트 : /

  1. 페이지 라우팅
  2. 이용자가 전송한 쿠키의 username 입력값을 가져온다.
  3. "admin"인 경우 FLAG 출력, 아닌 경우 "you are not admin"을 출력한다.

엔드포인트 : /login

  1. login 페이지 라우팅, GET/POST 메소드로 접근 가능하다.
  2. GET 메소드로 요청 시, login.html 페이지를 출력한다.
  3. POST 메소드로 요청 시, 이용자가 전송한 username, password 입력값을 가져온다.
  4. users 변수에서 이용자가 전송한 username이 존재하는지 확인한다.
  5. 존재하지 않는 username인 경우 경고를 출력한다.
  6. password를 체크한다.
  7. index 페이지로 이동하는 응답을 생성한다.
  8. username 쿠키를 설정한다.
  9. password가 동일하지 않은 경우 경고를 출력한다.

문제 풀이

링크에 접속했을 때의 화면이다.

ID와 Password를 잘못 입력했을 때 wrong password 라는 alert 창이 뜬다.

ID : guest, PW : guest를 입력했을 때 로그인 된 것을 확인할 수 있다.

익스플로잇

F12를 눌러 Application에서 Cookies를 확인한다.

username이 guest로 되어있는 것을 확인 가능하다.

guest를 admin으로 변경해준다.

결과

창을 새로고침 하면 플래그 값을 획득할 수 있다.

저작자표시 비영리 동일조건 (새창열림)

'Webhacking > Dreamhack' 카테고리의 다른 글

[Dreamhack] csrf-1  (0) 2025.03.15
[Dreamhack] xss-2  (0) 2025.03.15
[Dreamhack] xss-1  (0) 2025.03.15
[Dreamhack] session-basic  (0) 2025.03.15
[Dreamhack] devtools-sources  (0) 2025.03.15
'Webhacking/Dreamhack' 카테고리의 다른 글
  • [Dreamhack] xss-2
  • [Dreamhack] xss-1
  • [Dreamhack] session-basic
  • [Dreamhack] devtools-sources
배움이 머무는 곳
배움이 머무는 곳
  • 배움이 머무는 곳
    wlgus
    배움이 머무는 곳
  • 전체
    오늘
    어제
    • 분류 전체보기 (69) N
      • 이것저것.zip (7)
      • CVE (6)
      • CTF (2)
      • Wargame (24) N
      • Webhacking (19)
        • WebGoat (2)
        • Dreamhack (15)
      • Web (5)
      • Pwnable (5)
        • Dreamhack (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
  • hELLO· Designed By정상우.v4.10.5
배움이 머무는 곳
[Dreamhack] cookie
상단으로

티스토리툴바