session-basic

2023. 5. 18. 08:19·Wargame/Dreamhack
728x90
반응형

1. intro

 

2. code 및 분석

2.1.  code

#!/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',
    'user': 'user1234',
    'admin': FLAG
}


# this is our session storage
session_storage = {
}


@app.route('/')
def index():
    session_id = request.cookies.get('sessionid', None)
    try:
        # get username from session_storage
        username = session_storage[session_id]
    except KeyError:
        return render_template('index.html')

    return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')


@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:
            # you cannot know admin's pw
            pw = users[username]
        except:
            return '<script>alert("not found user");history.go(-1);</script>'
        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            session_id = os.urandom(32).hex()
            session_storage[session_id] = username
            resp.set_cookie('sessionid', session_id)
            return resp
        return '<script>alert("wrong password");history.go(-1);</script>'


@app.route('/admin')
def admin():
    # developer's note: review below commented code and uncomment it (TODO)

    #session_id = request.cookies.get('sessionid', None)
    #username = session_storage[session_id]
    #if username != 'admin':
    #    return render_template('index.html')

    return session_storage


if __name__ == '__main__':
    import os
    # create admin sessionid and save it to our storage
    # and also you cannot reveal admin's sesseionid by brute forcing!!! haha
    session_storage[os.urandom(32).hex()] = 'admin'
    print(session_storage)
    app.run(host='0.0.0.0', port=8000)

2.2. 분석

단순한 로그인 페이지이다.

main에서 session_storage에 urandom 값으로 session id를 설정하며 이를 admin의 session id로 정의한다.

index에서 sessionid 값을 요청하며, 만일 session_id 가 session_storage에 없으면 index.html을 리턴한다.

login에서는 username과 password를 받으며, users[username] 값이 존재하지 않으면 not found user를 출력하고,

만일 있으면 password 값과 pw를 비교하며, session_id를 새로 생성하고 이를 session_storage에 저장한 뒤 쿠키 값을 return해준다.

 

3. 취약점 확인 및 공격 준비

3.1. 취약점

숨겨진(?) page로 admin이 있으며 저장된 session_storage를 출력해준다.

더불어 로그인 시 index page를 redirect 하는데, 이 때 username과 password와 상관 없이 session_id 값 만으로 username을 인식한다.

 

3.2. 공격 준비

/admin 페이지로 이동하면 session id를 볼 수 있다.

그러므로 아무 아이디나 로그인 후 이 값으로 교체하면 flag가 출력된다.

 

4. exploit

회사 스샷 첨부 안되는거 디게 짜증남...

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

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

xss-1  (0) 2023.05.18
cookie  (0) 2023.05.18
devtools-sources  (0) 2023.05.17
Operator  (0) 2023.05.17
pwn_patch_2  (0) 2023.05.17
'Wargame/Dreamhack' 카테고리의 다른 글
  • xss-1
  • cookie
  • devtools-sources
  • Operator
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (505) N
      • To do list (7)
        • Doing (1)
        • Complete (6)
      • Diary (35)
      • Tips & theory (73)
      • Kernel Exploit (27)
        • Theory (15)
        • Exercise (5)
      • File Structure (6)
      • Wargame (313)
        • pwn.college (34)
        • Dreamhack (148)
        • pwnable.kr (15)
        • Lord of Sqlinjection (3)
        • Cryptohack (20)
        • Root me (27)
        • CodeEngn (4)
        • Exploit Education (22)
        • ROP Emporium (8)
        • H4C (10)
        • Hackerchool (22)
      • CTF (44) N
        • Solved (42) N
        • Unsolved (2)
      • Script (0)
      • RubiyaLap (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

    • PWN wargame 모음 (및 느낀점)
    • 비공개 글들에 대해.
    • 뭐라도 하나 얻어가시길...
  • 인기 글

  • 태그

    libc
    x64
    _IO_FILE
    64bit
    vtable
    FSB
    BOF
    dreamhack
    tcache
    Buffer Overflow
    x86
    32bit
    heap
    Me
    pwnable.kr
    ROOT ME
    CANARY
    docker
    RTL
    Format String Bug
    la ctf
    hackerschool
    rop
    lob
    cryptohack
    pwntools
    root-me
    root
    phoenix
    exploit education
  • 최근 댓글

  • 최근 글

  • 250x250
    반응형
  • hELLO· Designed By정상우.v4.10.3
wyv3rn
session-basic
상단으로

티스토리툴바