드림핵

[드림핵 워게임] 2. session-basic 풀이

ihatebasil 2024. 8. 19. 20:43

 

⚠️ 작성자는 컴퓨터 관련 지식이 거의 없는 초보자입니다. ⚠️

⚠️ 드림핵 워게임 비기너-쿠키 문제를 풀어본 경험을 바탕으로 유비추리를 통해 해결했습니다.⚠️

2024.08.09 - [분류 전체보기] - [워게임] Cookie - 드림핵 비기너 워게임 2024.07.24

 

[워게임] Cookie - 드림핵 비기너 워게임 2024.07.24

*나의 velog에서 퍼옴* 올해 1월부터 즐겨찾기 추가 해놓고 1번도 들어가지 않은 드림핵..^^멋사 하계 보안 스터디로 드디어 첫 워게임을 풀어볼 수 있었습니다ㅋㅋ===================================지난

ihatebasil.tistory.com

 

지난 Beginner cookie 문제에 이어 Beginner session도 풀어보려 했으나 생각보다 쉽게 풀이를 찾지 못 해서 😂

session-basic 문제를 먼저 풀었다.

서버에 접속 하면 조오금 익숙한 welcome 화면이 뜬다

welcome 화면

 

지난 쿠키 문제와 비슷하게 가장 먼저 로그인을 해보았다

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

다운 받은 app.py 문제 파일을 보면

user는 guest, user, admin 총 3명이 있고 우리가 비번을 모르는 user는 admin이다

가장 먼저 guest(ID: guest, PW: guest) 로 로그인 했다

@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"}')

 

if username == "admin" else "you are not admin"

 

로그인 직후, admin이 아니기 때문에 아래와 같은 화면이 뜬다

참고로, ID: user, PW: user1234 로 로그인해도 같은 화면이 뜬다

@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

 

문제 코드 속 다음 힌트는 '/admin',

login과 index화면 외에도 /admin이 있구나!

 

주소창에 /admin을 추가해본다

접속하면 아래와 같은 화면이 뜨는데, 

나는 블로그 기록을 위해 guest로 2번, user로 1번 로그인해서 4줄이나 뜨는 것 같다.

첫 시도 때에는 guest, admin 이렇게 2줄만 뜸.

 

 

이게 뭐냐면...

문제 코드를 통해 알 수 있다.

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)

session_storage[os.urandom(32).hex()] = 'admin'

32바이트의 무작위 바이트를 생성해서 hex()로 16진수 문자열로 변환한 값, 즉, 0~f로 이루어진 64자리 문자열이 admin의 sessionId로 저장된다는 뜻이다.

 

실제로 문제 코드를 실행해보면, 

무작위 64자리가 생성된 것을 볼 수 있다.

 

0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f 등 16가지 문자가 각 자리에 랜덤으로 하나씩 생겨서

무려 16^64가지 경우의 수가 존재하기 때문에

# and also you cannot reveal admin's sesseionid by brute forcing!!! haha

이런 주석을 달아놓은 것 같다 ^,.^

 

이제 개발자 도구를 살펴보자

문제 코드에 session storage 어쩌구..해서 session storage를 먼저 열어봤으나 아무것도 없어서

Cookie를 열어봣다

오 sessionId가 있다

Value는 /admin 화면에서 현재 접속 중인 user의 Value와 같다

따라서 이걸 admin Value로 바꿔주고 다시 index로 가면...

FLAG가 나왔다 ㅎ,ㅎ

DH~부터 복사해서 답 제출하면

끝!

 

이 문제 풀이를 통해 session에 대한 이해를 다지고, 못 풀고 있던 다음 문제 역시 무난하게 해결할 수 있었다.

 

이어지는 포스팅! 

https://ihatebasil.tistory.com/5

 

[드림핵 워게임] 3. Beginner session 풀이

https://dreamhack.io/wargame/challenges/266 session쿠키와 세션으로 인증 상태를 관리하는 간단한 로그인 서비스입니다. admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다. Reference Background: Coo

ihatebasil.tistory.com